Skip to content

Commit

Permalink
Merge pull request #4 from everFinance/upgrade-ethrpc
Browse files Browse the repository at this point in the history
fix(): support eip1559 tx
  • Loading branch information
zyjblockchain authored Nov 16, 2021
2 parents e69b980 + 12ac401 commit 21215c3
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 39 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example/wallet_send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
18 changes: 18 additions & 0 deletions signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,25 @@ 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, gasTipCap *big.Int, gasFeeCap *big.Int,
data []byte, chainID *big.Int,
) (tx *types.Transaction, err error) {
baseTx := &types.DynamicFeeTx{
Nonce: uint64(nonce),
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Gas: uint64(gasLimit),
To: &to,
Value: amount,
Data: data,
}
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,
Expand Down
6 changes: 5 additions & 1 deletion signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ 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), 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())
}

func TestSignMsg(t *testing.T) {
Expand Down
107 changes: 73 additions & 34 deletions wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rlp"
"github.com/everFinance/ethrpc"
)

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 {
Expand Down Expand Up @@ -61,66 +62,104 @@ 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
}

var nonce, gasLimit int
var gasPrice big.Int
ethrpcTx := ethrpc.T{
From: w.Address.String(),
To: to.String(),
Value: amount,
Data: hexutil.Encode(data),
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
}

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{}
}

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
}

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
}

raw, err := rlp.EncodeToBytes(tx)
if err != nil {
return
if opts.GasTipCap == nil || opts.GasFeeCap == nil {
opts.GasTipCap = opts.GasPrice
opts.GasFeeCap = opts.GasPrice
}

return w.Client.EthSendRawTransaction(hexutil.Encode(raw))
return opts, nil
}

func (w *Wallet) GetAddress() string {
Expand Down

0 comments on commit 21215c3

Please sign in to comment.