How to query Ethereum contract to get data from it?

3 Answers 3


You can use eth_call as follows:


Pass the first 4 bytes (8 characters) of the Keccak hash of the method, with 0x prepended, as the data parameter. You can use a tool like this to get the hash. In your case, you want to hash the string buyPrice() (different story if the method takes arguments). The first 8 characters of this are 8620410b, leading to the data parameter provided above.

The returned value is a hexadecimal. Use your preferred approach to convert back to decimal. Here's an online one. You can drop any leading zeroes if your language chokes on the string length. For example, at the time of posting, the returned value was 0x0000000000000000000000000000000000000000000000000002e1d9ae30ae00. You can reduce this to 0x2e1d9ae30ae00, which is 811275000000000 wei (0.000811275ETH).

Note that there's a rate limit of 5 requests per second. Exceed that and you might be blacklisted.


You can use the API. The function you need to use is eth_call. However, you will need the bytecode of: function buyPrice which you can get with web3 1.0.

Another way is using also web3, like this:

First, the buyPrice is returned by a function called the same buyPrice:

function buyPrice() public

Second you need to create an instance to the contract, for that you need the ABI of the contract, which you can obtain by copy-pasting the code of the contract in remix then you can do can use Infura to access ethereum (is free) like this:

web3 = new Web3(new Web3.providers.HttpProvider("use infura acces token here")); 
ABI = "here all the ABI string"
var pyramidContract = web3.eth.contract(ABI);
var pyramidInstance = contrato.at('0xBc1869a652F68260428b382fd06C96196D92d02D')
    console.log(result) // this is the returned value

Hope this help.

  • What should be the HTTP provider for the contract which is on etherum not in local?
    – vikasvmads
    Commented Feb 27, 2020 at 11:30

A perhaps more current realtime way based on web3 1.0 is:

web3 = new Web3(Web3.givenProvider || "ws://localhost:7545"); // local eth node
const contract = require('/path/contract.json');
const contractId = "0xcontractId";
const MyContract = new web3.eth.Contract(Contract.abi, contractId);

// Use any contract methods, or watch any events/logs
MyContract.methods.myMethod(function(error, result) { /* callback */});
MyContract.events.myEvent(function(error, result) { /* callback */ });

You can use the MyContract object to access any of your contract methods, which may also return contract data. Additionally you can subscribe to a broader scope of events (by contract, contract event etc.) as decsribe in the web3 1.0 documentation

var subscription = web3.eth.subscribe('logs', {
    address: '0x123456..',
    topics: ['0x12345...']
    }, function(error, result){
    if (!error)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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