Ethers.js: Call a function at a specific block number

Created on 11 Jul 2018  路  6Comments  路  Source: ethers-io/ethers.js

First off, thanks for ethers, it's great!

I'm trying to do a balance snapshot and I'm wondering if there's an equivalent to Web3's ability to pass in a block number when doing a function call like they do here, e.g.

myContract.methods.methodName().call(transactionObject, blockNumber, callback)

Is that something we can do with ethers? I couldn't find anything about it in the documentation, and looking through the code I'm not sure this is handled.

enhancement

Most helpful comment

To do that currently you need to use the provider directly, but I will add that feature since it is quite useful. Keep in mind that a pruning node may not be able to respond correctly to blocks over 1024 blocks old.

To do it manually, for now:

var iface = new ethers.Interface(abi);
var info = iface.functions.methodName();
var tx = {
    to: contractAddress,
    data: info.data
}
provider.call(tx, blockNumber).then((result) => {
    console.log(result);
});

I will add it to the Contract object this weekend though to the v4 branch, so then it can be overridden like normal contract overrides:

contract.methodName({ blockNumber: blockNumber }).then((result) => {
    console.log(result);
});

Thanks! :)

All 6 comments

To do that currently you need to use the provider directly, but I will add that feature since it is quite useful. Keep in mind that a pruning node may not be able to respond correctly to blocks over 1024 blocks old.

To do it manually, for now:

var iface = new ethers.Interface(abi);
var info = iface.functions.methodName();
var tx = {
    to: contractAddress,
    data: info.data
}
provider.call(tx, blockNumber).then((result) => {
    console.log(result);
});

I will add it to the Contract object this weekend though to the v4 branch, so then it can be overridden like normal contract overrides:

contract.methodName({ blockNumber: blockNumber }).then((result) => {
    console.log(result);
});

Thanks! :)

The key chosen for contract overrides is blockTag.

This feature is now available in 4.0.5. I will update the documentation shortly.

Thanks! :)

Hi @ricmoo - did you ever add the documentation for this? I can't see it.

The documentation for v5 is currently only on my laptop. I鈥檒l be working on it while on the plane to NY for ETHNewYork shortly. :)

Oh! I think I confused this with another issue. I will check on the v4 docs too.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dave-dm picture dave-dm  路  3Comments

GFJHogue picture GFJHogue  路  3Comments

dagogodboss picture dagogodboss  路  3Comments

naddison36 picture naddison36  路  3Comments

abhishekp1996 picture abhishekp1996  路  3Comments