2

I'm trying to call a function on my smart contract that's deployed on the main Ethereum network.

The function signature is balanceOf(address).

So far I've followed the instructions of doing the SHA3 hash of this so I get: 0x70a08231.

Now I want to send the address there, in the instructions it says to pad the parameters to 32 bytes. As I understand it, Ethereum addresses are 20 bytes like so: 0x4Ef58B0097A47fb07e9e10e4da92ed27DF36aFFF. Does this mean I should do something like 0000000000000000000000004Ef58B0097A47fb07e9e10e4da92ed27DF36aFFF?

Could someone help me to create the 'params' part for a eth_call. I've followed this tutorial here: How to call a contract method using the eth_call JSON-RPC API. And I understand how to do it if the parameters were an int for example. But how to do it for the address above?

An ideal answer would show the data that I need to send and explain the steps :)

So far I have the data part as this:

[{"to": "0x722180f08646A73f744f33eD66fD3C990DC6a78B", "data": "0x70a08231000000000000000000000000E676c814EaD2Ae27f706fC3067015a75eb4A94A5"}]

The error I currently get is "Invalid method parameters - missing value for required argument 1".

Thanks

3

You forgot the 2nd argument. The request should look like this:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "eth_call",
  "params": [
    {
      "to": "0x583cbbb8a8443b38abcc0c956bece47340ea1367",
      "data": "0xbeabacc80000000000000000000000008a7784D22eeD131953D0B95f32Adf092A0C0A571000000000000000000000000145e99f7bc840f3ea42d9a64221f041f9955dca2",
      "from":"0x8a7784D22eeD131953D0B95f32Adf092A0C0A571"
    },
    "latest"
  ]
}

Notice the latest argument.

1

This seems like it would be simplest with web3.js and the human-standard-token-abi Node.js package, with Infura as the RPC connection:

These are the two Node.js packages you'll need to install:

npm i web3

npm i human-standard-token-abi

And this is the code:

// Include the packages
const Web3 = require('web3')
const abi = require('human-standard-token-abi')

// Set up Infura as your RPC connection
const web3 = new Web3('https://mainnet.infura.io/')

// Define the contract addresses and the contract instance
const contractAddress = '0x39bb259f66e1c59d5abef88375979b4d20d98022'
const contract = new web3.eth.Contract(abi, contractAddress)

// Define the address and call the balanceOf method
const address = '0x9a066ad67f38035558f4ae8d34a8d2e63056fbe9'
contract.methods.balanceOf(address).call().then(console.log)

Let me know if you are unfamiliar with Node.js and need help setting it up. I hope this helps.

  • Ah yes I've found it too now actually. You were right to start with. One weird thing though, when I deploy the app to Heroku, its saying "Error: Cannot find module 'web3'". I've also tried just doing git add node_modules/web3 (bad practice) I know! – SwimmingG Jan 14 '18 at 14:35
  • I see web3 in both the package.json and the package-lock.json – SwimmingG Jan 14 '18 at 14:37
  • P.s do send your ETH address and i'll sort it out :) – SwimmingG Jan 14 '18 at 14:37
  • Unfortunately, I'm not too familiar with Heroku but have you tried just running a fresh npm install in the directory after the deployment? (if its included in package.json it should just reinstall it) – smirzo Jan 14 '18 at 14:44
  • No need for the compensation, I'm happy to help :) – smirzo Jan 14 '18 at 14:46
0

Why don't you use web3.js. You could use following code snipped to query a function of your smart contract.

var contractInstace = new web3.eth.contract(contractAbi, contractAddress);
contarctInstance.methods.balanceOf(address).call({from: fromAddress})
   .then(function(balance){
       console.log(balance);
}

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.