I want to call the balanceOf
method from the ERC20 smart contract. Is it possible to do so using Metamask's RPC provider, or do I always need to provide my own host?
For example, this following example works fine, but I need to provide my own host with my API key, which I suspect is not ideal for a public DApp.
const erc20GetBalanceAbi: any = [
{
constant: true,
inputs: [{ name: '_owner', type: 'address' }],
name: 'balanceOf',
outputs: [{ name: 'balance', type: 'uint256' }],
type: 'function'
}
];
async function getErc20Balance(wallet: string, tokenAddress: string) {
const web3 = new Web3(new Web3.providers.HttpProvider('https://my.personal.rpc?apikey=123'));
const contract = new web3.eth.Contract(erc20GetBalanceAbi, tokenAddress);
return = await contract.methods.balanceOf(wallet).call();
}
To summarize: is there a way to leverage a connected Metamask account to make this kind of ABI function call? If not, what is the preferred way to provide an RPC host on a public DApp?