Search code examples
rustethereum

How to call a function of a smart contract with Ethers.rs?


I'm using ethers.rs & want to call a function of a deployed smart contract. I don't to use the ABI of smart contract for this.

Based on my research so far I've found a way of using the function selector. This has to be encoded along with the function arguments in the data field of the transaction.

How can I do so using ethers.rs if I just know the contract address & the function that I want to call?


Solution

  • First you need to parse your contract abi with abigen:

    abigen!(ERC20Token, "./erc20.json",);
    

    more information here

    next you create your contract object:

    let contract = ERC20Token::new(address, client);
    

    and finally you call it:

    contract.total_supply().call().await
    
    contract.transfer(to, amount).call().await
    

    you can check the full example here