From eb03f61dce48de8fbb2051ef210d097aceb35274 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Fri, 24 Jan 2025 19:10:59 -0500 Subject: [PATCH] add check for gas price --- rpc/types/utils.go | 5 +++ rpc/types/utils_test.go | 85 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 rpc/types/utils_test.go diff --git a/rpc/types/utils.go b/rpc/types/utils.go index acc5eb553a..65185aea78 100644 --- a/rpc/types/utils.go +++ b/rpc/types/utils.go @@ -17,6 +17,7 @@ package types import ( "context" + "errors" "fmt" "math/big" "strings" @@ -308,6 +309,10 @@ func CheckTxFee(gasPrice *big.Int, gas uint64, feeCap float64) error { if feeCap == 0 { return nil } + // Return an error if gasPrice is nil + if gasPrice == nil { + return errors.New("gasPrice is nil") + } totalfee := new(big.Float).SetInt(new(big.Int).Mul(gasPrice, new(big.Int).SetUint64(gas))) // 1 photon in 10^18 aphoton oneToken := new(big.Float).SetInt(big.NewInt(params.Ether)) diff --git a/rpc/types/utils_test.go b/rpc/types/utils_test.go new file mode 100644 index 0000000000..890e65be11 --- /dev/null +++ b/rpc/types/utils_test.go @@ -0,0 +1,85 @@ +package types_test + +import ( + "math/big" + "testing" + + "github.com/zeta-chain/node/rpc/types" +) + +func TestCheckTxFee(t *testing.T) { + tests := []struct { + name string + gasPrice *big.Int + gas uint64 + feeCap float64 + wantErr bool + errMsg string + }{ + { + name: "valid transaction under cap", + gasPrice: big.NewInt(100000000000), // 100 Gwei + gas: 21000, // Standard ETH transfer + feeCap: 1.0, // 1 ETH cap + wantErr: false, + errMsg: "", + }, + { + name: "transaction exceeds cap", + gasPrice: big.NewInt(500000000000), // 500 Gwei + gas: 1000000, // Complex contract interaction + feeCap: 0.1, // 0.1 ETH cap + wantErr: true, + errMsg: "tx fee (0.50 ether) exceeds the configured cap (0.10 ether)", + }, + { + name: "nil gas price", + gasPrice: nil, + gas: 21000, + feeCap: 1.0, + wantErr: true, + errMsg: "gasPrice is nil", + }, + { + name: "zero fee cap", + gasPrice: big.NewInt(100000000000), + gas: 21000, + feeCap: 0, + wantErr: false, + errMsg: "", + }, + { + name: "very low gas price", + gasPrice: big.NewInt(1), + gas: 21000, + feeCap: 1.0, + wantErr: false, + errMsg: "", + }, + { + name: "very high gas price", + gasPrice: new(big.Int).Mul(big.NewInt(1e18), big.NewInt(100)), // 100 ETH per gas unit + gas: 21000, + feeCap: 1000.0, + wantErr: true, + errMsg: "tx fee (2100000.00 ether) exceeds the configured cap (1000.00 ether)", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := types.CheckTxFee(tt.gasPrice, tt.gas, tt.feeCap) + + // Check if we expected an error + if (err != nil) != tt.wantErr { + t.Errorf("CheckTxFee() error = %v, wantErr %v", err, tt.wantErr) + return + } + + // If we expected an error, verify the error message + if tt.wantErr && err.Error() != tt.errMsg { + t.Errorf("CheckTxFee() error message = %v, want %v", err.Error(), tt.errMsg) + } + }) + } +}