1

I am looking for a solution that does not require having the contract solidiity code compiled and deployed. For example, if I want to send approve on WETH on live network from the console, without downloading the Solidity code, compiling it, and deploying it. Since the contract is already deployed, it would make sense to be able to interact with it directly.

The idea is that I can get either the abi code (using getCode(contract_address)), or the json (from etherscan API). And using the abi or json, and the contract_address, I assume I should be able to interact with the contract. But how?

Here is what I tried to do (in Truffle console):

# get json from contract
https://api.etherscan.io/api?module=contract&action=getabi&address=0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2&apikey=xxx

Saved it as ./build/contracts/WETH9.json in my truffle project

var weth_contract = require("./build/contracts/WETH9.json")
# seems to work

var wethContract= new web3.eth.Contract(JSON.parse(weth_contract.result), "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2");

wethContract.totalSupply()
# fails
# Uncaught TypeError: wethContract.totalSupply is not a function


wethContract.methods.totalSupply()
# returns lots of stuffs, but I am not sure if this is helping...

I am stuck at this point. How can I check totalSupply, or send approve on WETH from the truffle console?

(I am using web3.version '1.2.1'.)

1
  • Try await wethContract.methods.symbol() it should print the token symbol. Methods like totalSupply return a BN object to read it you can convert to something readable using toString().
    – Ismael
    Oct 15 '20 at 15:20
1

What worked for me :

  • Launch truffle console : truffle console --network live --verbose-rpc with live the main network configured in the truffle-config.js file.

  • Instantiate the contract :

    truffle(live)> const abi = require("./wethAbi.json");
    truffle(live)> var wethContract = new web3.eth.Contract(abi,"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2");
    
  • As far as i know, truffle API let you interact only with contracts saved in the "contracts" folder. However you can use web3 like this :

    truffle(live)> wethContract.methods.totalSupply().call();
    

It returned me the following string : '7805478583602650216411375'.

Note that truffle is a development tool and web3 or other similar API are much more suitable when interacting with the mainnet.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.