build-onchain-apps icon indicating copy to clipboard operation
build-onchain-apps copied to clipboard

Documentation Request: use contract hook with contract address passed as param

Open michaelcohen716 opened this issue 1 year ago • 14 comments

Is your documentation request related to a problem? Please describe.

Current generateContractHook hooks have fixed addresses. Documentation requested for passing the contract address to that hook

michaelcohen716 avatar Jan 26 '24 18:01 michaelcohen716

These are defined per contract type, in src/hooks/contracts.ts. Can you elaborate on the kind of documentation you're looking for?

GioLogist avatar Jan 26 '24 22:01 GioLogist

for a factory pattern, it's not obvious how to generate contract hooks where the address is dynamically passed from a frontend component

michaelcohen716 avatar Jan 30 '24 01:01 michaelcohen716

where the address is dynamically passed from a frontend component

out of curiosity, why the address is dynamic?

Zizzamia avatar Jan 30 '24 02:01 Zizzamia

I just mean dynamically passed. the current hook examples hardcode the contract address.

michaelcohen716 avatar Jan 30 '24 03:01 michaelcohen716

Can you show us a code example on how you think it should behave, so I can better follow the type of developer experience you wish to have.

And thank you again for helping us shape this. We really appreciate your input.

Zizzamia avatar Jan 30 '24 03:01 Zizzamia

export const useCoffeeFactoryContract = generateContractHook({
  abi: CoffeeFactoryABI, // deploy different kinds of coffee from this contract
  [baseSepolia.id]: {
    chain: baseSepolia,
    address: '0xabcbcbcb...',
  },
});

code below does not work obviously.

export const useCoffeeContract = (contractAddress) => generateContractHook({
  abi: CoffeeABI, // instances of CoffeeContract
  [baseSepolia.id]: {
    chain: baseSepolia,
    address: contractAddress,
  },
});

I hacked this clunky version quickly and it works, but I'm sure there's a way to do it nicely with generateContractHook:

import { getContract } from 'wagmi/actions';
export function useCoffeeContract(contractAddress, walletClient) {
  const contract = getContract({
    address,
    abi: CoffeeABI,
    walletClient,
  });
 return {
    contract,
    abi: CoffeeABI,
    address,
    status: 'ready',
  };
}

michaelcohen716 avatar Jan 30 '24 22:01 michaelcohen716

@Sneh1999 thoughts on this one?

Zizzamia avatar Jan 31 '24 22:01 Zizzamia

@michaelcohen716 we are actually building onchain-kit which will provide utilities/functions which are generic similar to what you are requesting.

Build on chain apps on the other hand can be thought of more like a toolkit which gives you already setup end to end web3 examples/experiences of common use cases.

When onchain-kit gains momentum, you will see us replacing a lot of common functionality. We will add documentation for functions like these within onchain-kit itself and just use them here.

CC @Zizzamia feel free to add your thoughts

Sneh1999 avatar Jan 31 '24 23:01 Sneh1999

the current hook examples hardcode the contract address.

I still believe the hook, should not be dynamic, as that could cause some security issue.

I still don't understand the use case for address is dynamically passed from a frontend component, it feels a quite rare experience.

@michaelcohen716 what am I missing? Can you share a bit more what are you trying to build.

Zizzamia avatar Feb 01 '24 06:02 Zizzamia

hey - i think me calling it "dynamically passed" is tripping the conversation up. i'm really just asking about any factory pattern. like, if i was building uniswap, how would i generate a hook for an individual pool. there are thousands, so I need to pass the contract address for a specific pool

michaelcohen716 avatar Feb 04 '24 19:02 michaelcohen716

Ok, that's very interesting, and also a bit of an edge case.

@Sneh1999 @cnasc what's your take, is it something we should document or explore as experience?

Zizzamia avatar Feb 07 '24 07:02 Zizzamia

i would probably dispute the "edge case" characterization. it would be necessary for many AMMs or NFT marketplaces. I figured out how to do it, but a basic hook standard in the docs could be useful to others. not a huge deal either way, didn't mean to blow this out of proportion as mission critical or anything

michaelcohen716 avatar Feb 08 '24 19:02 michaelcohen716

@michaelcohen716 sorry for the delay here, this got lost in a bunch of GH notifications.

When you said

code below does not work obviously.

export const useCoffeeContract = (contractAddress) => generateContractHook({
  abi: CoffeeABI, // instances of CoffeeContract
  [baseSepolia.id]: {
    chain: baseSepolia,
    address: contractAddress,
  },
});

What issue did you run into? Something like the following should work (haven't had a chance to try, just made sure it typechecked)

export const useCoffeeContract = (contractAddress: `0x${string}`) => generateContractHook({
  abi: CoffeeABI, // instances of CoffeeContract
  [baseSepolia.id]: {
    chain: baseSepolia,
    address: contractAddress,
  },
});

cnasc avatar Feb 08 '24 20:02 cnasc

not a huge deal either way, didn't mean to blow this out of proportion as mission critical or anything

All good, we just really love unpack each case scenario. :)

Zizzamia avatar Feb 08 '24 23:02 Zizzamia