Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
michaelcohen716 opened this issue Jan 26, 2024 · 14 comments

Comments

@michaelcohen716
Copy link

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

@GioLogist
Copy link
Contributor

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

@michaelcohen716
Copy link
Author

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

@Zizzamia
Copy link
Contributor

Zizzamia commented Jan 30, 2024

where the address is dynamically passed from a frontend component

out of curiosity, why the address is dynamic?

@michaelcohen716
Copy link
Author

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

@Zizzamia
Copy link
Contributor

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.

@michaelcohen716
Copy link
Author

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',
  };
}

@Zizzamia
Copy link
Contributor

@Sneh1999 thoughts on this one?

@Sneh1999
Copy link
Contributor

@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

@Zizzamia
Copy link
Contributor

Zizzamia commented Feb 1, 2024

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.

@michaelcohen716
Copy link
Author

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

@Zizzamia
Copy link
Contributor

Zizzamia commented Feb 7, 2024

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?

@michaelcohen716
Copy link
Author

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

@cnasc
Copy link
Contributor

cnasc commented Feb 8, 2024

@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,
  },
});

@Zizzamia
Copy link
Contributor

Zizzamia commented Feb 8, 2024

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. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants