Skip to content

Commit

Permalink
feat: update signer, transaction types to eth2
Browse files Browse the repository at this point in the history
* closes #26
  • Loading branch information
kamikazechaser committed Apr 17, 2024
1 parent c842494 commit e4d86ef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
2 changes: 1 addition & 1 deletion provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
66 changes: 45 additions & 21 deletions transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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())
}
})
}
}
Expand All @@ -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",
Expand All @@ -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())
}
})
}
}
Expand Down Expand Up @@ -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",
Expand All @@ -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())
}
})
}
}
Expand All @@ -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,
Expand All @@ -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())
}
}
}

0 comments on commit e4d86ef

Please sign in to comment.