Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call a function at a specific block number #226

Closed
thekevinbrown opened this issue Jul 11, 2018 · 8 comments
Closed

Call a function at a specific block number #226

thekevinbrown opened this issue Jul 11, 2018 · 8 comments
Assignees
Labels
enhancement New feature or improvement.

Comments

@thekevinbrown
Copy link

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.

@ricmoo
Copy link
Member

ricmoo commented Jul 11, 2018

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! :)

@ricmoo ricmoo self-assigned this Jul 11, 2018
@ricmoo ricmoo added enhancement New feature or improvement. on-deck This Enhancement or Bug is currently being worked on. v4.0 labels Jul 11, 2018
@ricmoo ricmoo removed the v4.0 label Sep 30, 2018
ricmoo added a commit that referenced this issue Oct 11, 2018
@ricmoo
Copy link
Member

ricmoo commented Oct 11, 2018

The key chosen for contract overrides is blockTag.

@ricmoo
Copy link
Member

ricmoo commented Oct 11, 2018

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

Thanks! :)

@jjgonecrypto
Copy link

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

@ricmoo
Copy link
Member

ricmoo commented May 15, 2019

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

@ricmoo
Copy link
Member

ricmoo commented May 15, 2019

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

@hungdoansy
Copy link

hungdoansy commented Jul 5, 2022

Just spent 2 hours on this. Here is a detailed example for whom stumbled this issue without knowing exactly what to do next.
https://gist.github.com/hungdoansy/6f859b0b37c5e8e0a29b38c303e08012

ethers.js@5.6.9

@VladimirMikulic
Copy link

@ricmoo is this behavior the same for ethers v6?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement.
Projects
None yet
Development

No branches or pull requests

5 participants