From f7a663498416c457de325f95f25f7a69acc6e421 Mon Sep 17 00:00:00 2001 From: sandy <18382255942@163.com> Date: Mon, 15 Nov 2021 18:13:40 +0800 Subject: [PATCH 1/3] fix(): support eip1559 tx --- go.mod | 4 ++-- signer.go | 17 ++++++++++------- signer_test.go | 3 ++- wallet.go | 3 +-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index aa01a62..42ae1d8 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/everFinance/goether go 1.15 require ( - github.com/ethereum/go-ethereum v1.10.5 - github.com/everFinance/ethrpc v1.0.3 + github.com/ethereum/go-ethereum v1.10.12 + github.com/everFinance/ethrpc v1.0.4 github.com/stretchr/testify v1.7.0 ) diff --git a/signer.go b/signer.go index 36c1e6d..02a7890 100644 --- a/signer.go +++ b/signer.go @@ -59,13 +59,16 @@ func (s *Signer) SignTx( gasLimit int, gasPrice *big.Int, data []byte, chainID *big.Int, ) (tx *types.Transaction, err error) { - return types.SignTx( - types.NewTransaction( - uint64(nonce), to, amount, - uint64(gasLimit), gasPrice, data), - types.NewEIP155Signer(chainID), - s.key, - ) + basTx := &types.DynamicFeeTx{ + Nonce: uint64(nonce), + GasTipCap: gasPrice, + GasFeeCap: gasPrice, + Gas: uint64(gasLimit), + To: &to, + Value: amount, + Data: data, + } + return types.SignNewTx(s.key, types.LatestSignerForChainID(chainID), basTx) } func (s Signer) SignMsg(msg []byte) (sig []byte, err error) { diff --git a/signer_test.go b/signer_test.go index c1f9814..b87d3a5 100644 --- a/signer_test.go +++ b/signer_test.go @@ -22,8 +22,9 @@ func TestAddress(t *testing.T) { } func TestSignTx(t *testing.T) { - _, err := TestSigner.SignTx(1, common.HexToAddress("0xab6c371B6c466BcF14d4003601951e5873dF2AcA"), big.NewInt(0), 21000, big.NewInt(100000000000), nil, big.NewInt(42)) + tx, err := TestSigner.SignTx(1, common.HexToAddress("0xab6c371B6c466BcF14d4003601951e5873dF2AcA"), big.NewInt(0), 21000, big.NewInt(100000000000), nil, big.NewInt(42)) assert.NoError(t, err) + assert.Equal(t, big.NewInt(42), tx.ChainId()) } func TestSignMsg(t *testing.T) { diff --git a/wallet.go b/wallet.go index 336f6cf..d0cebed 100644 --- a/wallet.go +++ b/wallet.go @@ -8,7 +8,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/ethereum/go-ethereum/rlp" "github.com/everFinance/ethrpc" ) @@ -115,7 +114,7 @@ func (w *Wallet) SendTx( return } - raw, err := rlp.EncodeToBytes(tx) + raw, err := tx.MarshalBinary() if err != nil { return } From 10fb2908ef8b8503dd16565f306115783d92658f Mon Sep 17 00:00:00 2001 From: sandy <18382255942@163.com> Date: Tue, 16 Nov 2021 11:35:20 +0800 Subject: [PATCH 2/3] fix(): add LegacyTx --- example/wallet_send_test.go | 2 +- signer.go | 25 +++++++-- signer_test.go | 5 +- wallet.go | 106 +++++++++++++++++++++++++----------- 4 files changed, 98 insertions(+), 40 deletions(-) diff --git a/example/wallet_send_test.go b/example/wallet_send_test.go index 4ca4c65..6472910 100644 --- a/example/wallet_send_test.go +++ b/example/wallet_send_test.go @@ -18,7 +18,7 @@ func TestWalletSend(t *testing.T) { // without opts txHash, err := testWallet.SendTx( - common.HexToAddress("0xa06b79E655Db7D7C3B3E7B2ccEEb068c3259d0C9"), // To + common.HexToAddress("0xa2026731B31E4DFBa78314bDBfBFDC8cF5F761F8"), // To goether.EthToBN(0.12), // Value []byte("123"), // Data nil) diff --git a/signer.go b/signer.go index 02a7890..148f731 100644 --- a/signer.go +++ b/signer.go @@ -54,21 +54,36 @@ func (s Signer) GetPublicKeyHex() string { return hexutil.Encode(s.GetPublicKey()) } +// SignTx DynamicFeeTx func (s *Signer) SignTx( nonce int, to common.Address, amount *big.Int, - gasLimit int, gasPrice *big.Int, + gasLimit int, gasTipCap *big.Int, gasFeeCap *big.Int, data []byte, chainID *big.Int, ) (tx *types.Transaction, err error) { - basTx := &types.DynamicFeeTx{ + baseTx := &types.DynamicFeeTx{ Nonce: uint64(nonce), - GasTipCap: gasPrice, - GasFeeCap: gasPrice, + GasTipCap: gasTipCap, + GasFeeCap: gasFeeCap, Gas: uint64(gasLimit), To: &to, Value: amount, Data: data, } - return types.SignNewTx(s.key, types.LatestSignerForChainID(chainID), basTx) + return types.SignNewTx(s.key, types.LatestSignerForChainID(chainID), baseTx) +} + +func (s *Signer) SignLegacyTx( + nonce int, to common.Address, amount *big.Int, + gasLimit int, gasPrice *big.Int, + data []byte, chainID *big.Int, +) (tx *types.Transaction, err error) { + return types.SignTx( + types.NewTransaction( + uint64(nonce), to, amount, + uint64(gasLimit), gasPrice, data), + types.NewEIP155Signer(chainID), + s.key, + ) } func (s Signer) SignMsg(msg []byte) (sig []byte, err error) { diff --git a/signer_test.go b/signer_test.go index b87d3a5..e9d4881 100644 --- a/signer_test.go +++ b/signer_test.go @@ -22,7 +22,10 @@ func TestAddress(t *testing.T) { } func TestSignTx(t *testing.T) { - tx, err := TestSigner.SignTx(1, common.HexToAddress("0xab6c371B6c466BcF14d4003601951e5873dF2AcA"), big.NewInt(0), 21000, big.NewInt(100000000000), nil, big.NewInt(42)) + tx, err := TestSigner.SignTx(1, common.HexToAddress("0xab6c371B6c466BcF14d4003601951e5873dF2AcA"), big.NewInt(0), 21000, big.NewInt(100000000000), big.NewInt(100000000000), nil, big.NewInt(42)) + assert.NoError(t, err) + assert.Equal(t, big.NewInt(42), tx.ChainId()) + tx, err = TestSigner.SignLegacyTx(1, common.HexToAddress("0xab6c371B6c466BcF14d4003601951e5873dF2AcA"), big.NewInt(0), 21000, big.NewInt(100000000000), nil, big.NewInt(42)) assert.NoError(t, err) assert.Equal(t, big.NewInt(42), tx.ChainId()) } diff --git a/wallet.go b/wallet.go index d0cebed..581dde5 100644 --- a/wallet.go +++ b/wallet.go @@ -12,9 +12,11 @@ import ( ) type TxOpts struct { - Nonce *int - GasLimit *int - GasPrice *big.Int + Nonce *int + GasLimit *int + GasPrice *big.Int + GasTipCap *big.Int + GasFeeCap *big.Int } type Wallet struct { @@ -60,20 +62,64 @@ func NewWalletFromPath(prvPath, rpc string) (*Wallet, error) { return NewWallet(strings.TrimSpace(string(b)), rpc) } -func (w *Wallet) SendTx( - to common.Address, amount *big.Int, - data []byte, opts *TxOpts, -) (txHash string, err error) { +func (w *Wallet) SendTx(to common.Address, amount *big.Int, data []byte, opts *TxOpts) (txHash string, err error) { + opts, err = w.initTxOpts(to, amount, data, opts) + if err != nil { + return + } + + if amount == nil { + amount = big.NewInt(0) + } + + tx, err := w.Signer.SignTx( + *opts.Nonce, to, amount, + *opts.GasLimit, opts.GasTipCap, opts.GasFeeCap, + data, w.ChainID) + if err != nil { + return + } + + raw, err := tx.MarshalBinary() + if err != nil { + return + } + + return w.Client.EthSendRawTransaction(hexutil.Encode(raw)) +} + +func (w *Wallet) SendLegacyTx(to common.Address, amount *big.Int, data []byte, opts *TxOpts) (txHash string, err error) { + opts, err = w.initTxOpts(to, amount, data, opts) + if err != nil { + return + } + + if amount == nil { + amount = big.NewInt(0) + } + tx, err := w.Signer.SignLegacyTx( + *opts.Nonce, to, amount, + *opts.GasLimit, opts.GasPrice, + data, w.ChainID) + if err != nil { + return + } - var nonce, gasLimit int - var gasPrice big.Int - ethrpcTx := ethrpc.T{ - From: w.Address.String(), - To: to.String(), - Value: amount, - Data: hexutil.Encode(data), + raw, err := tx.MarshalBinary() + if err != nil { + return } + return w.Client.EthSendRawTransaction(hexutil.Encode(raw)) +} + +func (w *Wallet) initTxOpts(to common.Address, amount *big.Int, data []byte, opts *TxOpts) (*TxOpts, error) { + var ( + nonce, gasLimit int + gasPrice big.Int + err error + ) + if opts == nil { opts = &TxOpts{} } @@ -81,15 +127,21 @@ func (w *Wallet) SendTx( if opts.Nonce == nil { nonce, err = w.GetPendingNonce() if err != nil { - return + return nil, err } opts.Nonce = &nonce } if opts.GasLimit == nil { + ethrpcTx := ethrpc.T{ + From: w.Address.String(), + To: to.String(), + Value: amount, + Data: hexutil.Encode(data), + } gasLimit, err = w.Client.EthEstimateGas(ethrpcTx) if err != nil { - return + return nil, err } opts.GasLimit = &gasLimit } @@ -97,29 +149,17 @@ func (w *Wallet) SendTx( if opts.GasPrice == nil { gasPrice, err = w.Client.EthGasPrice() if err != nil { - return + return nil, err } opts.GasPrice = &gasPrice } - if amount == nil { - amount = big.NewInt(0) - } - - tx, err := w.Signer.SignTx( - *opts.Nonce, to, amount, - *opts.GasLimit, opts.GasPrice, - data, w.ChainID) - if err != nil { - return + if opts.GasTipCap == nil || opts.GasFeeCap == nil { + opts.GasTipCap = opts.GasPrice + opts.GasFeeCap = opts.GasPrice } - raw, err := tx.MarshalBinary() - if err != nil { - return - } - - return w.Client.EthSendRawTransaction(hexutil.Encode(raw)) + return opts, nil } func (w *Wallet) GetAddress() string { From 12ac401a5ad3c72ea4367804a40fa6a99cf63e53 Mon Sep 17 00:00:00 2001 From: sandy <18382255942@163.com> Date: Tue, 16 Nov 2021 11:40:27 +0800 Subject: [PATCH 3/3] fix(): fix readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3598685..5956fa9 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ testWallet, err := goether.NewWallet(prvHex, rpc) if err != nil { panic(err) } - +// default send DynamicFeeTx txHash, err := testWallet.SendTx( common.HexToAddress("0xa06b79E655Db7D7C3B3E7B2ccEEb068c3259d0C9"), // To goether.EthToBN(0.12), // Value @@ -94,6 +94,7 @@ Ethereum Account which can be used to sign messages and transactions. Connect to Ethereum Network, execute state changing operations. - [x] SendTx +- [x] SendLegacyTx - [x] GetAddress - [x] GetBalance - [x] GetNonce