Skip to content

Commit

Permalink
Use block input in Call (umbracle#194)
Browse files Browse the repository at this point in the history
* Use block input in Call

* Modify Changelog

* Add test

* Fix testcases in ci
  • Loading branch information
ferranbt authored May 4, 2022
1 parent 361ad04 commit 980c5ac
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
with:
node-version: '14'
- name: Install ethers testcases
run: cd ./testsuite && npm install
run: cd ./testcases && npm install
- name: Setup go
uses: actions/setup-go@v1
with:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

# 0.1.2 (Unreleased)

- Fix. Use `ethgo.BlockNumber` input to make `Call` in contract [[GH-194](https://github.com/umbracle/ethgo/issues/194)]
- Add `testcases` for contract signature and transaction signing [[GH-193](https://github.com/umbracle/ethgo/issues/193)]
- Add `eth_feeHistory` rpc endpoint [[GH-192](https://github.com/umbracle/ethgo/issues/192)]
- Update `testserver` to `go-ethereum:v1.10.15` [[GH-191](https://github.com/umbracle/ethgo/issues/191)]
Expand Down
2 changes: 1 addition & 1 deletion contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (a *Contract) Call(method string, block ethgo.BlockNumber, args ...interfac
}

opts := &CallOpts{
Block: ethgo.Latest,
Block: block,
}
if a.key != nil {
opts.From = a.key.Address()
Expand Down
59 changes: 59 additions & 0 deletions contract/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,62 @@ func TestContract_Transaction(t *testing.T) {
assert.Len(t, receipt.Logs, 1)
}
}

func TestContract_CallAtBlock(t *testing.T) {
s := testutil.NewTestServer(t, nil)
defer s.Close()

// create an address and fund it
key, _ := wallet.GenerateKey()
s.Transfer(key.Address(), big.NewInt(1000000000000000000))

cc := &testutil.Contract{}
cc.AddCallback(func() string {
return `
uint256 val = 1;
function getVal() public view returns (uint256) {
return val;
}
function change() public payable {
val = 2;
}`
})

artifact, addr := s.DeployContract(cc)

abi, err := abi.NewABI(artifact.Abi)
assert.NoError(t, err)

contract := NewContract(addr, abi, WithJsonRPCEndpoint(s.HTTPAddr()), WithSender(key))

checkVal := func(block ethgo.BlockNumber, expected *big.Int) {
resp, err := contract.Call("getVal", block)
assert.NoError(t, err)
assert.Equal(t, resp["0"], expected)
}

// initial value is 1
checkVal(ethgo.Latest, big.NewInt(1))

// send a transaction to update the state
var receipt *ethgo.Receipt
{
txn, err := contract.Txn("change")
assert.NoError(t, err)

err = txn.Do()
assert.NoError(t, err)

receipt, err = txn.Wait()
assert.NoError(t, err)
}

// validate the state at different blocks
{
// value at receipt block is 2
checkVal(ethgo.BlockNumber(receipt.BlockNumber), big.NewInt(2))

// value at previous block is 1
checkVal(ethgo.BlockNumber(receipt.BlockNumber-1), big.NewInt(1))
}
}

0 comments on commit 980c5ac

Please sign in to comment.