Developers Forum for XinFin XDC Network

SilentZero
SilentZero

Posted on

How to call totalSupply and balanceOf of an XRC-20 (ERC-20) token on XDC/Ethereum using Rust + Alloy.rs?

I'm trying to interact with a deployed XRC-20 token contract on the XDC Network (EVM-compatible) using Rust and the alloy crate.

I have already deployed my token using MetaMask and Remix IDE, and I can see it on the XDC Apothem explorer.

Now I want to use alloy in Rust to read the totalSupply and balanceOf of that token.

I'm trying this basic setup using the sol! macro:

sol! {
    interface IERC20 {
        function totalSupply() external view returns (uint256);
        function balanceOf(address) external view returns (uint256);
    }
}
Enter fullscreen mode Exit fullscreen mode

But I’m not sure what the minimal working code looks like to connect to XDC RPC https://rpc.apothem.network by using a PrivateKeySigner from MetaMask to call totalSupply() and balanceOf(my_addr)

I tried to take help from https://alloy.rs/examples/wallets/README but could not find something easy to usable.

I already have my token contract address, MetaMask wallet private key and RPC address.

So I am needing a minimal working example in Rust using alloy that can loads a private key and Connects to an RPC and call IERC20::totalSupply() and IERC20::balanceOf() on the deployed contract

Discussion (4)

Collapse
gzliudan profile image
Daniel Liu • Edited on

Here is the demo for you:

github.com/gzliudan/alloy-demo

Collapse
gzliudan profile image
Daniel Liu

I will try to write an demo for you

Collapse
sean_ profile image
Sean

Please wait. Someone will respond to your query soon.