Skip to content

Conversation

@0xpApaSmURf
Copy link
Contributor

@0xpApaSmURf 0xpApaSmURf commented Dec 14, 2022

React hooks for Humanbound token helpers.

Use useHumanbound providing an Alchemy configuration with api keys to expose functions that return humanbound token data for supported networks:

Usage:

const alchemyConfig = {
    [1]: "mainnetApiKey",
    [10]: "optimismApiKey",
    ...
};

const Component: React.FC = () => {
    const { hasHBT, getHBTIdOfOwner, getOwnerOf} = useHumanbound(alchemyConfig);

    useEffect(() => {
        const fetchHBTInfo = async () => {
            await hasHbt(address);                    // returns bool
            await getHBTIdOfOwner(address);           // returns uint256
            await getOwnerOf(hbtId);                  // returns string
        };
        
        fetchHBTInfo();
    }, [hasHBT, getHBTIdOfOwner, getOwnerOf]);
}

Currently only supported networks are:

  • Ethereum Mainnet: chainId: 1
  • Optimism: chainId: 10
  • Optimism Goerli: chainId: 420
  • Arbitrum One: chainId: 42161
  • Arbitrum Goerli: chainId: 421613
  • Polygon Mainnet: chainId: 137
  • Polygon Mumbai: chainId: 80001

Alchemy config must be provided with api keys corresponding to the networks above for any network that your dApp intends to support. Using useHumanbound on networks that are not supported will throw an error.

@0xpApaSmURf 0xpApaSmURf requested a review from ra-phael December 15, 2022 13:50
@0xpApaSmURf 0xpApaSmURf marked this pull request as ready for review December 15, 2022 14:03
Copy link
Contributor

@ra-phael ra-phael left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really cool! ✨


const provider = new providers.JsonRpcProvider(
`https://${AlchemyNetworkNames[chainId as Web3ChainReference]}.g.alchemy.com/v2/${
alchemyConfig[chainId as Web3ChainReference]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite annoying to have to repeat "as Web3ChainReference" so many times 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh... I don't think it's much of an issue from a user perspective since it's abstracted away but in any case is there a better way to do this whilst keeping the type?

const events = await humanboundContract.queryFilter(filter);

if (events.length == 0) return BigNumber.from(null);
return events[0].args[1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know how this array of events is sorted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's latest first from what I remember.

// returns a boolean reporting if `address` owns a humanbound token
const hasHBT = useCallback(
async (address: string) => {
if (!humanboundContract) throw new Error("hasHBT: contract is null, check your usage of useHumanbound");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These checks seem unecessary given what this hook returns, the functions can't be called unless humanbounContract is defined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, this was here before I changed the way the hook was returning

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah so the compiler doesn't like this because from a type point of view the value of humanboundContract could be null

Comment on lines +27 to +28
* - getHBTIdOfOwner: @param `address`
* - getOwnerOf: @param `tokenId`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't apply for hasHBT as it's meant to be has HBT on any network but I wonder if for the 2 other functions we should give the option to pass in a specific network to check. Since HBTs are humanbound and not address-bound theoretically you could have different addresses for a same HBT ID on 2 different networks?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouuh actually this is checking one network at a time for hasHBT 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh it's checking one network at a time because we're assuming the same kind of stack of web3 providers, which are always one network at a time. It's the simplest IMO.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants