0

I know in ethereum if a method of smart contract don't change state variables we can see it's result without making any transaction.

now I want to know how can see result of this method programatically?

for example by api of an explorer website that I can send some data to it and it show me some results.

I'm using php. so please don't offer me web3.js or python and so on.

for example I want to get result of totalSupply method of a smart contract called DS2Token on ropsten network in this address: 0x53778036dce23a22303142923316af257d7ee813

1

I imagine someone, somewhere, has written a web3 implementation for PHP, but regardless of that, you can do this via simple HTTP calls to a node, or a proxy such as infura.

To read from a function, you need to know its signature and the parameters you want to pass. For totalSupply(), this is simply the first 4 bytes of the keccak256 hash of the function name as a string, i.e. keccak256("totalSupply()"), which is 0x18160ddd. You can set this as the data for the transaction, and your contract's address as the to field, and then make a POST request to eth_call. In curl, this would look something like:

curl https://ropsten.infura.io/ \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc":"2.0","method":"eth_call","params": [
    {"from": "0x53778036dce23a22303142923316af257d7ee813",
     "to": "0x53778036dce23a22303142923316af257d7ee813",
     "gas": "0x76c0","gasPrice": "0x9184e72a000","value": "0x0",
     "data": "0x18160ddd"}, "latest"],"id":1}'

The from and gasPrice values can be anything, but gas needs to be large enough to allow completion of your read operation.

You will get a result along the lines of {"jsonrpc":"2.0","id":1,"result":"0x00000000000000000000000000000000000000000052b7d2c7f6fac568ea0000"}.

Then, simply convert the received hex number into decimal, and you will have your total supply.

  • I found gas,gasPrice,value are optional in eth_call method and I can only use from,to and data. thanks for your help :) – saeid ezzati Sep 25 '18 at 13:57

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.