Skip to content

Commit

Permalink
Enable CPLR token on useERC20Balance hook
Browse files Browse the repository at this point in the history
  • Loading branch information
gantoreno committed Feb 20, 2022
1 parent 6a3751e commit b357023
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/hooks/useERC20balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useMoralisDapp } from '../providers/MoralisDappProvider/MoralisDappProv

export const useERC20Balance = (props) => {
const { account } = useMoralisWeb3Api();
const { isInitialized } = useMoralis();
const { Moralis, isInitialized } = useMoralis();
const { walletAddress, chainId } = useMoralisDapp();

const [assets, setAssets] = useState();
Expand All @@ -15,17 +15,42 @@ export const useERC20Balance = (props) => {
.then((balance) => setAssets(balance))
.catch((e) => alert(e.message));
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isInitialized, chainId, walletAddress]);

const fetchERC20Balance = async () => {
return await account
const erc20Balances = await account
.getTokenBalances({
address: walletAddress,
chain: props?.chain || chainId,
})
.then((result) => result)
.catch((e) => alert(e.message));

const [cuplrToken] = await Moralis.Web3API.token
.getTokenMetadata({
chain: '0x38',
addresses: ['0x0D3608CE95Cd2b561298BeeD3F9fd3DDD3083163'],
})
.then((data) => data)
.catch((e) => alert(e.message));

if (
!erc20Balances.find((token) => token.token_address === cuplrToken.address)
) {
erc20Balances.unshift({
balance: '0',
decimals: cuplrToken.decimals,
logo: cuplrToken.logo,
name: cuplrToken.name,
symbol: cuplrToken.symbol,
thumbnail: cuplrToken.thumbnail,
token_address: cuplrToken.address,
});
}

return erc20Balances;
};

return { fetchERC20Balance, assets };
Expand Down

0 comments on commit b357023

Please sign in to comment.