Skip to content

Commit 980c5ac

Browse files
authored
Use block input in Call (#194)
* Use block input in Call * Modify Changelog * Add test * Fix testcases in ci
1 parent 361ad04 commit 980c5ac

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

.github/workflows/pr.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
with:
1111
node-version: '14'
1212
- name: Install ethers testcases
13-
run: cd ./testsuite && npm install
13+
run: cd ./testcases && npm install
1414
- name: Setup go
1515
uses: actions/setup-go@v1
1616
with:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
# 0.1.2 (Unreleased)
33

4+
- Fix. Use `ethgo.BlockNumber` input to make `Call` in contract [[GH-194](https://github.com/umbracle/ethgo/issues/194)]
45
- Add `testcases` for contract signature and transaction signing [[GH-193](https://github.com/umbracle/ethgo/issues/193)]
56
- Add `eth_feeHistory` rpc endpoint [[GH-192](https://github.com/umbracle/ethgo/issues/192)]
67
- Update `testserver` to `go-ethereum:v1.10.15` [[GH-191](https://github.com/umbracle/ethgo/issues/191)]

contract/contract.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func (a *Contract) Call(method string, block ethgo.BlockNumber, args ...interfac
313313
}
314314

315315
opts := &CallOpts{
316-
Block: ethgo.Latest,
316+
Block: block,
317317
}
318318
if a.key != nil {
319319
opts.From = a.key.Address()

contract/contract_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,62 @@ func TestContract_Transaction(t *testing.T) {
164164
assert.Len(t, receipt.Logs, 1)
165165
}
166166
}
167+
168+
func TestContract_CallAtBlock(t *testing.T) {
169+
s := testutil.NewTestServer(t, nil)
170+
defer s.Close()
171+
172+
// create an address and fund it
173+
key, _ := wallet.GenerateKey()
174+
s.Transfer(key.Address(), big.NewInt(1000000000000000000))
175+
176+
cc := &testutil.Contract{}
177+
cc.AddCallback(func() string {
178+
return `
179+
uint256 val = 1;
180+
function getVal() public view returns (uint256) {
181+
return val;
182+
}
183+
function change() public payable {
184+
val = 2;
185+
}`
186+
})
187+
188+
artifact, addr := s.DeployContract(cc)
189+
190+
abi, err := abi.NewABI(artifact.Abi)
191+
assert.NoError(t, err)
192+
193+
contract := NewContract(addr, abi, WithJsonRPCEndpoint(s.HTTPAddr()), WithSender(key))
194+
195+
checkVal := func(block ethgo.BlockNumber, expected *big.Int) {
196+
resp, err := contract.Call("getVal", block)
197+
assert.NoError(t, err)
198+
assert.Equal(t, resp["0"], expected)
199+
}
200+
201+
// initial value is 1
202+
checkVal(ethgo.Latest, big.NewInt(1))
203+
204+
// send a transaction to update the state
205+
var receipt *ethgo.Receipt
206+
{
207+
txn, err := contract.Txn("change")
208+
assert.NoError(t, err)
209+
210+
err = txn.Do()
211+
assert.NoError(t, err)
212+
213+
receipt, err = txn.Wait()
214+
assert.NoError(t, err)
215+
}
216+
217+
// validate the state at different blocks
218+
{
219+
// value at receipt block is 2
220+
checkVal(ethgo.BlockNumber(receipt.BlockNumber), big.NewInt(2))
221+
222+
// value at previous block is 1
223+
checkVal(ethgo.BlockNumber(receipt.BlockNumber-1), big.NewInt(1))
224+
}
225+
}

0 commit comments

Comments
 (0)