Skip to content

Commit

Permalink
Add nonce when sending transaction in contract (#185)
Browse files Browse the repository at this point in the history
* Add nonce when sending transaction in contract

* Add Changelog entry
  • Loading branch information
ferranbt authored Apr 15, 2022
1 parent c0024dd commit 91240b7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

# 0.1.1 (Not released)

- Retrieve latest nonce when sending a transaction on `contract` [[GH-185](https://github.com/umbracle/ethgo/issues/185)]
- Add `etherscan.GasPrice` function to return last block gas price [[GH-182](https://github.com/umbracle/ethgo/issues/182)]
- Add `4byte` package and cli [[GH-178](https://github.com/umbracle/ethgo/issues/178)]
- Install and use `ethers.js` spec tests for wallet private key decoding [[GH-177](https://github.com/umbracle/ethgo/issues/177)]
Expand Down
9 changes: 9 additions & 0 deletions contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ func (j *jsonRPCNodeProvider) Txn(addr ethgo.Address, key ethgo.Key, input []byt
return nil, err
}
}
// calculate the nonce
if opts.Nonce == 0 {
opts.Nonce, err = j.client.GetNonce(from, ethgo.Latest)
if err != nil {
return nil, fmt.Errorf("failed to calculate nonce: %v", err)
}
}

chainID, err := j.client.ChainID()
if err != nil {
Expand All @@ -82,6 +89,7 @@ func (j *jsonRPCNodeProvider) Txn(addr ethgo.Address, key ethgo.Key, input []byt
GasPrice: opts.GasPrice,
Gas: opts.GasLimit,
Value: opts.Value,
Nonce: opts.Nonce,
}
if addr != ethgo.ZeroAddress {
rawTxn.To = &addr
Expand Down Expand Up @@ -244,6 +252,7 @@ type TxnOpts struct {
Value *big.Int
GasPrice uint64
GasLimit uint64
Nonce uint64
}

func (a *Contract) Txn(method string, args ...interface{}) (Txn, error) {
Expand Down
21 changes: 12 additions & 9 deletions contract/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,18 @@ func TestContract_Transaction(t *testing.T) {
abi, err := abi.NewABI(artifact.Abi)
assert.NoError(t, err)

// create a transaction
i := NewContract(addr, abi, WithJsonRPCEndpoint(s.HTTPAddr()), WithSender(key))
txn, err := i.Txn("setA")
assert.NoError(t, err)
// send multiple transactions
contract := NewContract(addr, abi, WithJsonRPCEndpoint(s.HTTPAddr()), WithSender(key))

err = txn.Do()
assert.NoError(t, err)
for i := 0; i < 10; i++ {
txn, err := contract.Txn("setA")
assert.NoError(t, err)

receipt, err := txn.Wait()
assert.NoError(t, err)
assert.Len(t, receipt.Logs, 1)
err = txn.Do()
assert.NoError(t, err)

receipt, err := txn.Wait()
assert.NoError(t, err)
assert.Len(t, receipt.Logs, 1)
}
}
14 changes: 12 additions & 2 deletions wallet/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ func (e *EIP1155Signer) RecoverSender(tx *ethgo.Transaction) (ethgo.Address, err
return addr, nil
}

func trimBytesZeros(b []byte) []byte {
var i int
for i = 0; i < len(b); i++ {
if b[i] != 0x0 {
break
}
}
return b[i:]
}

func (e *EIP1155Signer) SignTx(tx *ethgo.Transaction, key ethgo.Key) (*ethgo.Transaction, error) {
hash := signHash(tx, e.chainID)

Expand All @@ -50,8 +60,8 @@ func (e *EIP1155Signer) SignTx(tx *ethgo.Transaction, key ethgo.Key) (*ethgo.Tra

vv := uint64(sig[64]) + 35 + e.chainID*2

tx.R = sig[:32]
tx.S = sig[32:64]
tx.R = trimBytesZeros(sig[:32])
tx.S = trimBytesZeros(sig[32:64])
tx.V = new(big.Int).SetUint64(vv).Bytes()
return tx, nil
}
Expand Down
6 changes: 6 additions & 0 deletions wallet/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ func TestSigner_EIP1155(t *testing.T) {
assert.NotEqual(t, from, from2)
*/
}

func TestTrimBytesZeros(t *testing.T) {
assert.Equal(t, trimBytesZeros([]byte{0x1, 0x2}), []byte{0x1, 0x2})
assert.Equal(t, trimBytesZeros([]byte{0x0, 0x1}), []byte{0x1})
assert.Equal(t, trimBytesZeros([]byte{0x0, 0x0}), []byte{})
}

1 comment on commit 91240b7

@vercel
Copy link

@vercel vercel bot commented on 91240b7 Apr 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.