Skip to content

Commit d52e750

Browse files
authored
Add support for ERC721 tokens in TokenGate (#223)
* Add support for ERC721 tokens in TokenGate * Create changeset
1 parent bbb4fcd commit d52e750

File tree

6 files changed

+396
-65
lines changed

6 files changed

+396
-65
lines changed

.changeset/shy-trees-rest.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@web3-ui/core': minor
3+
'@web3-ui/hooks': patch
4+
---
5+
6+
The TokenGate component in the core package now supports ERC721 tokens. The hooks package now also exports the ERC721 ABI

packages/core/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import { ConnectWallet } from '@web3-ui/core';
7272

7373
### TokenGate
7474

75-
`TokenGate` lets you conditionally render some content depending on whether the current connected user has enough amount of a specific token. The component only supports ERC20 tokens at the moment but support for other standards is coming soon.
75+
`TokenGate` lets you conditionally render some content depending on whether the current connected user has enough amount of a specific token. The component only supports ERC20 and ERC721 (NFTs) tokens at the moment but support for other standards is coming soon.
7676

7777
#### Usage
7878

@@ -85,6 +85,7 @@ import { TokenGate } from '@web3-ui/core';
8585
deniedContent={
8686
<p>This message will show up if the user doesn't have enough tokens.</p>
8787
}
88+
tokenType="ERC721" // optional, defaults to ERC20
8889
>
8990
<h1>This message will be visible if the user has enough tokens.</h1>
9091
</TokenGate>;
Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Container, Heading } from '@chakra-ui/react';
22
import { NETWORKS } from '@web3-ui/hooks';
3+
import { ethers } from 'ethers';
34
import React from 'react';
45
import { ConnectWallet, Provider } from '..';
56
import { TokenGate } from './TokenGate';
@@ -11,8 +12,8 @@ export default {
1112
Story => {
1213
return (
1314
<Provider
14-
network={NETWORKS.rinkeby}
15-
rpcUrl="https://rinkeby.infura.io/v3/21bc321f21a54c528dc084f5ed7f8df7"
15+
network={NETWORKS.mainnet}
16+
rpcUrl="https://mainnet.infura.io/v3/21bc321f21a54c528dc084f5ed7f8df7"
1617
>
1718
<ConnectWallet />
1819
<Story />
@@ -22,22 +23,41 @@ export default {
2223
]
2324
};
2425

25-
export const Default = () => {
26+
export const ERC20 = () => {
2627
return (
2728
<TokenGate
28-
tokenContractAddress="0x08149745590e9025b52b6801e9dd3E752e60F3A2"
29+
tokenContractAddress="0x6B175474E89094C44Da98b954EedeAC495271d0F"
2930
deniedContent={
3031
<Container mt={10}>
31-
<Heading>You don't own enough $dUSDT</Heading>
32+
<Heading>You don't own enough $DAI</Heading>
3233
</Container>
3334
}
35+
requiredQuantity={+ethers.utils.parseEther('0.1')}
3436
>
3537
<Container mt={10}>
3638
<Heading>
3739
{' '}
38-
You are a fellow $dUSDT holder. Welcome to the club! 🥳
40+
You are a fellow $DAI holder. Welcome to the club! 🥳
3941
</Heading>
4042
</Container>
4143
</TokenGate>
4244
);
4345
};
46+
47+
export const ERC721 = () => {
48+
return (
49+
<TokenGate
50+
tokenContractAddress="0x25ed58c027921e14d86380ea2646e3a1b5c55a8b"
51+
deniedContent={
52+
<Container mt={10}>
53+
<Heading>You don't own a Developer DAO genesis NFT.</Heading>
54+
</Container>
55+
}
56+
requiredQuantity={1}
57+
>
58+
<Container mt={10}>
59+
<Heading> You are a fellow D_D member. Welcome to the club! 🥳</Heading>
60+
</Container>
61+
</TokenGate>
62+
);
63+
};

packages/core/src/components/TokenGate/TokenGate.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useCallback, useEffect, useState } from 'react';
22
import { TokenGate as PrivTokenGate } from '@web3-ui/components';
33
import { useReadOnlyContract, useWallet } from '@web3-ui/hooks';
4-
import { ERC20ABI } from '@web3-ui/hooks/src/constants';
4+
import { ERC20ABI, ERC721ABI } from '@web3-ui/hooks/src/constants';
55
import { BigNumber, ethers } from 'ethers';
66

77
export interface TokenGateProps {
@@ -21,6 +21,10 @@ export interface TokenGateProps {
2121
* Optional message that is displayed if access denied i.e. the user does not hold enough tokens. Default=null
2222
*/
2323
deniedContent?: React.ReactNode;
24+
/**
25+
* The token type. ERC20 and ERC721 are supported. Default=ERC20
26+
*/
27+
tokenType?: 'ERC20' | 'ERC721';
2428
}
2529

2630
/**
@@ -30,13 +34,15 @@ export const TokenGate = ({
3034
children,
3135
tokenContractAddress,
3236
requiredQuantity = +ethers.utils.parseEther('1'),
33-
deniedContent = null
37+
deniedContent = null,
38+
tokenType = 'ERC20'
3439
}: TokenGateProps) => {
40+
const contractAbi = tokenType === 'ERC20' ? ERC20ABI : ERC721ABI;
3541
const { connection } = useWallet();
3642
const { userAddress } = connection;
3743
const [tokenContract, isReady] = useReadOnlyContract(
3844
tokenContractAddress,
39-
ERC20ABI
45+
contractAbi
4046
);
4147
const [balance, setBalance] = useState<BigNumber>();
4248

0 commit comments

Comments
 (0)