From e4d86ef2e8c4f47134af8a6b6a1898d681753062 Mon Sep 17 00:00:00 2001 From: Mohammed Sohail Date: Wed, 17 Apr 2024 17:28:53 +0800 Subject: [PATCH] feat: update signer, transaction types to eth2 * closes #26 --- provider.go | 2 +- transaction.go | 6 ++--- transaction_test.go | 66 ++++++++++++++++++++++++++++++--------------- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/provider.go b/provider.go index 054a301..ae8d33d 100644 --- a/provider.go +++ b/provider.go @@ -37,7 +37,7 @@ func NewProvider(o ProviderOpts) (*Provider, error) { return &Provider{ ChainId: o.ChainId, Client: w3.NewClient(rpcClient), - Signer: types.NewLondonSigner(big.NewInt(o.ChainId)), + Signer: types.LatestSignerForChainID(big.NewInt(o.ChainId)), }, nil } diff --git a/transaction.go b/transaction.go index e32bd68..423bdd5 100644 --- a/transaction.go +++ b/transaction.go @@ -36,7 +36,7 @@ type ( ) func (p *Provider) SignContractExecutionTx(privateKey *ecdsa.PrivateKey, txData ContractExecutionTxOpts) (*types.Transaction, error) { - tx, err := types.SignNewTx(privateKey, p.Signer, &types.CeloDynamicFeeTx{ + tx, err := types.SignNewTx(privateKey, p.Signer, &types.DynamicFeeTx{ To: &txData.ContractAddress, Nonce: txData.Nonce, Data: txData.InputData, @@ -52,7 +52,7 @@ func (p *Provider) SignContractExecutionTx(privateKey *ecdsa.PrivateKey, txData } func (p *Provider) SignGasTransferTx(privateKey *ecdsa.PrivateKey, txData GasTransferTxOpts) (*types.Transaction, error) { - tx, err := types.SignNewTx(privateKey, p.Signer, &types.CeloDynamicFeeTx{ + tx, err := types.SignNewTx(privateKey, p.Signer, &types.DynamicFeeTx{ Value: txData.Value, To: &txData.To, Nonce: txData.Nonce, @@ -68,7 +68,7 @@ func (p *Provider) SignGasTransferTx(privateKey *ecdsa.PrivateKey, txData GasTra } func (p *Provider) SignContractPublishTx(privateKey *ecdsa.PrivateKey, txData ContractPublishTxOpts) (*types.Transaction, error) { - tx, err := types.SignNewTx(privateKey, p.Signer, &types.CeloDynamicFeeTx{ + tx, err := types.SignNewTx(privateKey, p.Signer, &types.DynamicFeeTx{ Data: txData.ContractByteCode, Nonce: txData.Nonce, Gas: txData.GasLimit, diff --git a/transaction_test.go b/transaction_test.go index 3273321..af85dc4 100644 --- a/transaction_test.go +++ b/transaction_test.go @@ -43,9 +43,10 @@ func TestProvider_SignContractExecutionTx(t *testing.T) { txData ContractExecutionTxOpts } tests := []struct { - name string - args args - wantErr bool + name string + args args + wantErr bool + wantType uint8 }{ { name: "Sign ERC20 transfer", @@ -61,16 +62,21 @@ func TestProvider_SignContractExecutionTx(t *testing.T) { Nonce: 0, }, }, - wantErr: false, + wantErr: false, + wantType: 2, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, err := p.SignContractExecutionTx(tt.args.privateKey, tt.args.txData) + tx, err := p.SignContractExecutionTx(tt.args.privateKey, tt.args.txData) if (err != nil) != tt.wantErr { t.Errorf("Provider.SignContractExecutionTx() error = %v, wantErr %v", err, tt.wantErr) return } + + if tx.Type() != tt.wantType { + t.Errorf("Provider.SignContractExecutionTx() want type = %d, got %d", tt.wantType, tx.Type()) + } }) } } @@ -94,9 +100,10 @@ func TestProvider_SignGasTransferTx(t *testing.T) { txData GasTransferTxOpts } tests := []struct { - name string - args args - wantErr bool + name string + args args + wantErr bool + wantType uint8 }{ { name: "Sign gas transfer", @@ -110,17 +117,22 @@ func TestProvider_SignGasTransferTx(t *testing.T) { Nonce: 0, }, }, - wantErr: false, + wantErr: false, + wantType: 2, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, err := p.SignGasTransferTx(tt.args.privateKey, tt.args.txData) + tx, err := p.SignGasTransferTx(tt.args.privateKey, tt.args.txData) if (err != nil) != tt.wantErr { t.Errorf("Provider.SignGasTransferTx() error = %v, wantErr %v", err, tt.wantErr) return } + + if tx.Type() != tt.wantType { + t.Errorf("Provider.SignGasTransferTx() want type = %d, got %d", tt.wantType, tx.Type()) + } }) } } @@ -156,9 +168,10 @@ func TestProvider_SignContractPublishTx(t *testing.T) { txData ContractPublishTxOpts } tests := []struct { - name string - args args - wantErr bool + name string + args args + wantErr bool + wantType uint8 }{ { name: "Sign contract publish", @@ -172,17 +185,22 @@ func TestProvider_SignContractPublishTx(t *testing.T) { Nonce: 0, }, }, - wantErr: false, + wantErr: false, + wantType: 2, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, err := p.SignContractPublishTx(tt.args.privateKey, tt.args.txData) + tx, err := p.SignContractPublishTx(tt.args.privateKey, tt.args.txData) if (err != nil) != tt.wantErr { t.Errorf("Provider.SignContractPublishTx() error = %v, wantErr %v", err, tt.wantErr) return } + + if tx.Type() != tt.wantType { + t.Errorf("Provider.SignContractPublishTx() want type = %d, got %d", tt.wantType, tx.Type()) + } }) } } @@ -208,15 +226,16 @@ func TestProvider_SignGasTransferTxPayWithCUSD(t *testing.T) { txData types.TxData } tests := []struct { - name string - args args - wantErr bool + name string + args args + wantErr bool + wantType uint8 }{ { name: "Sign gas transfer, pay with cUSD", args: args{ privateKey: privateKey, - txData: &types.CeloDynamicFeeTx{ + txData: &types.CeloDynamicFeeTxV2{ To: &deadAddress, Gas: 21000 + 50000, FeeCurrency: &cUSD, @@ -225,15 +244,20 @@ func TestProvider_SignGasTransferTxPayWithCUSD(t *testing.T) { Nonce: 0, }, }, - wantErr: false, + wantErr: false, + wantType: 123, }, } for _, tt := range tests { - _, err := types.SignNewTx(tt.args.privateKey, p.Signer, tt.args.txData) + tx, err := types.SignNewTx(tt.args.privateKey, p.Signer, tt.args.txData) if (err != nil) != tt.wantErr { t.Errorf("types.SignNewTx error = %v, wantErr %v", err, tt.wantErr) return } + + if tx.Type() != tt.wantType { + t.Errorf("Provider.SignGasTransferTxPayWithCUSD() want type = %d, got %d", tt.wantType, tx.Type()) + } } }