From a28f4765bf7c635042a71704c5de6ca78668f7b6 Mon Sep 17 00:00:00 2001 From: ash-krishnan Date: Wed, 8 Jun 2022 12:26:42 -0700 Subject: [PATCH] remove gas price --- .../construction/construction_service_test.go | 10 +--- services/construction/metadata.go | 1 - services/construction/metadata_test.go | 27 --------- services/construction/parse.go | 3 - services/construction/parse_test.go | 15 ++--- services/construction/payloads.go | 2 - services/construction/payloads_test.go | 13 +---- services/construction/preprocess.go | 19 ++++++ services/construction/preprocess_test.go | 58 ++++++++++++++++++- services/construction/types.go | 35 ----------- services/errors/errors.go | 7 +++ 11 files changed, 92 insertions(+), 98 deletions(-) diff --git a/services/construction/construction_service_test.go b/services/construction/construction_service_test.go index 4c64a3c..a22caac 100644 --- a/services/construction/construction_service_test.go +++ b/services/construction/construction_service_test.go @@ -53,7 +53,6 @@ var ( constructionToAddress = "0x3Fa177c2E87Cb24148EC403921dB577d140CC07c" transferValue = uint64(20211004) - transferGasPrice = uint64(0) transferGasLimit = uint64(21000) transferGasLimitERC20 = uint64(65000) transferNonce = uint64(67) @@ -62,7 +61,6 @@ var ( transferGasTip = uint64(1000000000) transferValueHex = hexutil.EncodeUint64(transferValue) - transferGasPriceHex = hexutil.EncodeUint64(transferGasPrice) transferGasLimitHex = hexutil.EncodeUint64(transferGasLimit) transferGasLimitERC20Hex = hexutil.EncodeUint64(transferGasLimitERC20) transferNonceHex = hexutil.EncodeUint64(transferNonce) @@ -160,7 +158,6 @@ func TestConstructionFlowWithPendingNonce(t *testing.T) { // Test Metadata metadata := &metadata{ GasLimit: 21000, - GasPrice: big.NewInt(0), GasTip: big.NewInt(1500000000), GasCap: big.NewInt(1501000000), Nonce: 0, @@ -209,7 +206,7 @@ func TestConstructionFlowWithPendingNonce(t *testing.T) { }, metadataResponse) // Test Payloads - unsignedRaw := `{"from":"0x5aCB42b3cfCD734a57AFF800139ba1354b549159","to":"0x3Fa177c2E87Cb24148EC403921dB577d140CC07c","value":"0x3e8","data":"0x","nonce":"0x0","gas_price":"0x0","max_fee_per_gas":"0x59777140","max_priority_fee_per_gas":"0x59682f00","gas":"0x5208","chain_id":"0x13881"}` + unsignedRaw := `{"from":"0x5aCB42b3cfCD734a57AFF800139ba1354b549159","to":"0x3Fa177c2E87Cb24148EC403921dB577d140CC07c","value":"0x3e8","data":"0x","nonce":"0x0","max_fee_per_gas":"0x59777140","max_priority_fee_per_gas":"0x59682f00","gas":"0x5208","chain_id":"0x13881"}` payloadsResponse, err := servicer.ConstructionPayloads(ctx, &types.ConstructionPayloadsRequest{ NetworkIdentifier: networkIdentifier, Operations: ops, @@ -236,7 +233,6 @@ func TestConstructionFlowWithPendingNonce(t *testing.T) { assert.Nil(t, err) parseMetadata := &parseMetadata{ Nonce: metadata.Nonce, - GasPrice: metadata.GasPrice, GasCap: metadata.GasCap, GasTip: metadata.GasTip, GasLimit: metadata.GasLimit, @@ -373,7 +369,6 @@ func TestConstructionFlowWithInputNonce(t *testing.T) { // Test Metadata metadata := &metadata{ GasLimit: 21000, - GasPrice: big.NewInt(0), GasTip: big.NewInt(1500000000), GasCap: big.NewInt(1501000000), Nonce: 1, @@ -414,7 +409,7 @@ func TestConstructionFlowWithInputNonce(t *testing.T) { }, metadataResponse) // Test Payloads - unsignedRaw := `{"from":"0x5aCB42b3cfCD734a57AFF800139ba1354b549159","to":"0x3Fa177c2E87Cb24148EC403921dB577d140CC07c","value":"0x3e8","data":"0x","nonce":"0x1","gas_price":"0x0","max_fee_per_gas":"0x59777140","max_priority_fee_per_gas":"0x59682f00","gas":"0x5208","chain_id":"0x13881"}` + unsignedRaw := `{"from":"0x5aCB42b3cfCD734a57AFF800139ba1354b549159","to":"0x3Fa177c2E87Cb24148EC403921dB577d140CC07c","value":"0x3e8","data":"0x","nonce":"0x1","max_fee_per_gas":"0x59777140","max_priority_fee_per_gas":"0x59682f00","gas":"0x5208","chain_id":"0x13881"}` payloadsResponse, err := servicer.ConstructionPayloads(ctx, &types.ConstructionPayloadsRequest{ NetworkIdentifier: networkIdentifier, Operations: ops, @@ -441,7 +436,6 @@ func TestConstructionFlowWithInputNonce(t *testing.T) { assert.Nil(t, err) parseMetadata := &parseMetadata{ Nonce: metadata.Nonce, - GasPrice: metadata.GasPrice, GasLimit: metadata.GasLimit, GasCap: metadata.GasCap, GasTip: metadata.GasTip, diff --git a/services/construction/metadata.go b/services/construction/metadata.go index f90c578..2001867 100644 --- a/services/construction/metadata.go +++ b/services/construction/metadata.go @@ -129,7 +129,6 @@ func (a *APIService) ConstructionMetadata( metadata := &metadata{ Nonce: nonce, - GasPrice: big.NewInt(0), GasLimit: gasLimit, GasCap: gasCap, GasTip: gasTip, diff --git a/services/construction/metadata_test.go b/services/construction/metadata_test.go index 337f7e1..d32af82 100644 --- a/services/construction/metadata_test.go +++ b/services/construction/metadata_test.go @@ -40,7 +40,6 @@ var ( maticTokenContract = "0x0000000000000000000000000000000000001010" metadataMaticWithdrawData = "0x2e1a7d4d0000000000000000000000000000000000000000000000000000000005f5e100" - metadataGasPriceHex = "0x0" ) func TestMetadata_Offline(t *testing.T) { @@ -77,7 +76,6 @@ func TestMetadata(t *testing.T) { "to": metadataTo, "value": transferValueHex, "nonce": transferNonceHex2, - "gas_price": metadataGasPriceHex, "gas_limit": transferGasLimitHex, "gas_cap": transferGasCapHex, "gas_tip": transferGasTipHex, @@ -98,10 +96,6 @@ func TestMetadata(t *testing.T) { client.On("SuggestGasTipCap", ctx). Return(big.NewInt(int64(transferGasTip)), nil) - // var gasPrice *big.Int = nil - - // client.On("SuggestGasPrice", ctx, gasPrice). - // Return(big.NewInt(int64(transferGasPrice)), nil) }, }, "happy path: native currency without nonce": { @@ -123,17 +117,12 @@ func TestMetadata(t *testing.T) { client.On("SuggestGasTipCap", ctx). Return(big.NewInt(int64(transferGasTip)), nil) - // var gasPrice *big.Int = nil - - //client.On("SuggestGasPrice", ctx, gasPrice). - // Return(big.NewInt(int64(transferGasTip)), nil) }, expectedResponse: &types.ConstructionMetadataResponse{ Metadata: map[string]interface{}{ "to": metadataTo, "value": transferValueHex, "nonce": transferNonceHex, - "gas_price": metadataGasPriceHex, "gas_limit": transferGasLimitHex, "gas_cap": transferGasCapHex, "gas_tip": transferGasTipHex, @@ -173,17 +162,12 @@ func TestMetadata(t *testing.T) { client.On("SuggestGasTipCap", ctx). Return(big.NewInt(int64(transferGasTip)), nil) - // var gasPrice *big.Int = nil - - // client.On("SuggestGasPrice", ctx, gasPrice). - // Return(big.NewInt(int64(transferGasPrice)), nil) }, expectedResponse: &types.ConstructionMetadataResponse{ Metadata: map[string]interface{}{ "to": tokenContractAddress, "value": "0x0", "nonce": transferNonceHex2, - "gas_price": metadataGasPriceHex, "gas_limit": transferGasLimitERC20Hex, "gas_cap": transferGasCapHex, "gas_tip": transferGasTipHex, @@ -226,17 +210,12 @@ func TestMetadata(t *testing.T) { client.On("SuggestGasTipCap", ctx). Return(big.NewInt(int64(transferGasTip)), nil) - // var gasPrice *big.Int = nil - - // client.On("SuggestGasPrice", ctx, gasPrice). - // Return(big.NewInt(int64(transferGasPrice)), nil) }, expectedResponse: &types.ConstructionMetadataResponse{ Metadata: map[string]interface{}{ "to": tokenContractAddress, "value": "0x0", "nonce": transferNonceHex2, - "gas_price": metadataGasPriceHex, "gas_limit": transferGasLimitERC20Hex, "gas_cap": transferGasCapHex, "gas_tip": transferGasTipHex, @@ -274,11 +253,6 @@ func TestMetadata(t *testing.T) { Value: big.NewInt(100000000), }).Return(transferGasLimitERC20, nil) - // var gasPrice *big.Int = nil - // - // client.On("SuggestGasPrice", ctx, gasPrice). - // Return(big.NewInt(int64(transferGasPrice)), nil) - var blockNum *big.Int = nil client.On("BlockHeader", ctx, blockNum). @@ -292,7 +266,6 @@ func TestMetadata(t *testing.T) { "to": maticTokenContract, "value": "0x5f5e100", "nonce": transferNonceHex2, - "gas_price": metadataGasPriceHex, "gas_limit": transferGasLimitERC20Hex, "gas_cap": transferGasCapHex, "gas_tip": transferGasTipHex, diff --git a/services/construction/parse.go b/services/construction/parse.go index 1628826..a51c15c 100644 --- a/services/construction/parse.go +++ b/services/construction/parse.go @@ -18,7 +18,6 @@ import ( "context" "encoding/json" "fmt" - "math/big" "github.com/coinbase/rosetta-sdk-go/types" ethTypes "github.com/ethereum/go-ethereum/core/types" @@ -49,7 +48,6 @@ func (a *APIService) ConstructionParse( tx.Value = t.Value() tx.Data = t.Data() tx.Nonce = t.Nonce() - tx.GasPrice = big.NewInt(0) tx.GasCap = t.GasFeeCap() tx.GasTip = t.GasTipCap() tx.GasLimit = t.Gas() @@ -104,7 +102,6 @@ func (a *APIService) ConstructionParse( metadata := &parseMetadata{ Nonce: tx.Nonce, - GasPrice: tx.GasPrice, GasCap: tx.GasCap, GasTip: tx.GasTip, GasLimit: tx.GasLimit, diff --git a/services/construction/parse_test.go b/services/construction/parse_test.go index ad4ce11..e269286 100644 --- a/services/construction/parse_test.go +++ b/services/construction/parse_test.go @@ -28,19 +28,18 @@ import ( ) var ( - unsignedMaticTransferTx = `{"from":"0x5aCB42b3cfCD734a57AFF800139ba1354b549159","to":"0x3Fa177c2E87Cb24148EC403921dB577d140CC07c","value":"0x3e8","data":"0x","nonce":"0x2","gas_price":"0x0","max_fee_per_gas":"0x59682f15","max_priority_fee_per_gas":"0x59682eff","gas":"0x5208","chain_id":"0x13881"}` //nolint:lll + unsignedMaticTransferTx = `{"from":"0x5aCB42b3cfCD734a57AFF800139ba1354b549159","to":"0x3Fa177c2E87Cb24148EC403921dB577d140CC07c","value":"0x3e8","data":"0x","nonce":"0x2","max_fee_per_gas":"0x59682f15","max_priority_fee_per_gas":"0x59682eff","gas":"0x5208","chain_id":"0x13881"}` //nolint:lll signedMaticTransferTx = `{"type":"0x2","nonce":"0x2","gasPrice":null,"maxPriorityFeePerGas":"0x59682eff","maxFeePerGas":"0x59682f15","gas":"0x5208","value":"0x3e8","input":"0x","v":"0x1","r":"0x6afe2f65d311ff2430ca7388335b86e42606ea4728924d91564405df83d2cea5","s":"0x443a04f2d96ea9877ed67f2b45266446ab01de2154c268470f57bb12effa1563","to":"0x3fa177c2e87cb24148ec403921db577d140cc07c","chainId":"0x13881","accessList":[],"hash":"0x554c2edbd04b2be9d1314ef31201e3382eedb24a733f1b15448af2d16252db73"}` //nolint:lll - unsignedERC20TransferTx = `{"from":"0x5aCB42b3cfCD734a57AFF800139ba1354b549159","to":"0x2d7882beDcbfDDce29Ba99965dd3cdF7fcB10A1e","value":"0x0","data":"0xa9059cbb0000000000000000000000003fa177c2e87cb24148ec403921db577d140cc07c0000000000000000000000000000000000000000000000000000000000000064","nonce":"0x2","gas_price":"0x0","max_fee_per_gas":"0x9502f914","max_priority_fee_per_gas":"0x9502f900","gas":"0xb2cb","chain_id":"0x13881"}` //nolint:lll + unsignedERC20TransferTx = `{"from":"0x5aCB42b3cfCD734a57AFF800139ba1354b549159","to":"0x2d7882beDcbfDDce29Ba99965dd3cdF7fcB10A1e","value":"0x0","data":"0xa9059cbb0000000000000000000000003fa177c2e87cb24148ec403921db577d140cc07c0000000000000000000000000000000000000000000000000000000000000064","nonce":"0x2","max_fee_per_gas":"0x9502f914","max_priority_fee_per_gas":"0x9502f900","gas":"0xb2cb","chain_id":"0x13881"}` //nolint:lll signedERC20TransferTx = `{"type":"0x2","nonce":"0x2","gasPrice":null,"maxPriorityFeePerGas":"0x9502f900","maxFeePerGas":"0x9502f914","gas":"0xb2cb","value":"0x0","input":"0xa9059cbb0000000000000000000000003fa177c2e87cb24148ec403921db577d140cc07c0000000000000000000000000000000000000000000000000000000000000064","v":"0x1","r":"0x2a8799b115741f62d5da931a53428ad1e3bf3055e9ea8427ce196a44cc590fca","s":"0x4779ab01b496c8b27e19efd24817557609b50da0d7e1a3790c435ca2225b43ae","to":"0x2d7882bedcbfddce29ba99965dd3cdf7fcb10a1e","chainId":"0x13881","accessList":[],"hash":"0xaa0f2056a79315e60a2012aee5f582692817e12153c6e45f57215f848893ec9e"}` //nolint:lll - unsignedERC20TransferTxInvalidData = `"{"from":"0x5aCB42b3cfCD734a57AFF800139ba1354b549159","to":"0x2d7882beDcbfDDce29Ba99965dd3cdF7fcB10A1e","value":"0x0","data":"0xaaaaaaaa000000000000000000000000efd3dc58d60af3295b92ecd484caeb3a2f30b3e7000000000000000000000000000000000000000000000000000000000134653c","nonce":"0x2","gas_price":"0x0","max_fee_per_gas":"0x9502f914","max_priority_fee_per_gas":"0x9502f900","gas":"0xb2cb","chain_id":"0x13881"}"` //nolint:lll - unsignedMaticTransferTxInvalidFrom = `{"from":"invalid_from","to":"0x3Fa177c2E87Cb24148EC403921dB577d140CC07c","value":"0x3e8","data":"0x","nonce":"0x2","gas_price":"0x0","max_fee_per_gas":"0x59682f15","max_priority_fee_per_gas":"0x59682eff","gas":"0x5208","chain_id":"0x13881"}` //nolint:lll - unsignedMaticTransferTxInvalidTo = `{"from":"0x5aCB42b3cfCD734a57AFF800139ba1354b549159","to":"invalid_to","value":"0x3e8","data":"0x","nonce":"0x2","gas_price":"0x0","max_fee_per_gas":"0x59682f15","max_priority_fee_per_gas":"0x59682eff","gas":"0x5208","chain_id":"0x13881"}` //nolint:lll + unsignedERC20TransferTxInvalidData = `"{"from":"0x5aCB42b3cfCD734a57AFF800139ba1354b549159","to":"0x2d7882beDcbfDDce29Ba99965dd3cdF7fcB10A1e","value":"0x0","data":"0xaaaaaaaa000000000000000000000000efd3dc58d60af3295b92ecd484caeb3a2f30b3e7000000000000000000000000000000000000000000000000000000000134653c","nonce":"0x2","max_fee_per_gas":"0x9502f914","max_priority_fee_per_gas":"0x9502f900","gas":"0xb2cb","chain_id":"0x13881"}"` //nolint:lll + unsignedMaticTransferTxInvalidFrom = `{"from":"invalid_from","to":"0x3Fa177c2E87Cb24148EC403921dB577d140CC07c","value":"0x3e8","data":"0x","nonce":"0x2","max_fee_per_gas":"0x59682f15","max_priority_fee_per_gas":"0x59682eff","gas":"0x5208","chain_id":"0x13881"}` //nolint:lll + unsignedMaticTransferTxInvalidTo = `{"from":"0x5aCB42b3cfCD734a57AFF800139ba1354b549159","to":"invalid_to","value":"0x3e8","data":"0x","nonce":"0x2","max_fee_per_gas":"0x59682f15","max_priority_fee_per_gas":"0x59682eff","gas":"0x5208","chain_id":"0x13881"}` //nolint:lll parseFromAddress = "0x5aCB42b3cfCD734a57AFF800139ba1354b549159" parseToAddress = "0x3Fa177c2E87Cb24148EC403921dB577d140CC07c" parseTokenContractAddress = "0x2d7882beDcbfDDce29Ba99965dd3cdF7fcB10A1e" - gasPriceHex = "0x0" gasCapHex = "0x59682f15" gasTipHex = "0x59682eff" nonceHex = "0x2" @@ -70,7 +69,6 @@ func TestParse(t *testing.T) { AccountIdentifierSigners: []*types.AccountIdentifier{}, Metadata: map[string]interface{}{ "nonce": nonceHex, - "gas_price": gasPriceHex, "gas_limit": transferGasLimitHex, "gas_cap": gasCapHex, "gas_tip": gasTipHex, @@ -93,7 +91,6 @@ func TestParse(t *testing.T) { }, Metadata: map[string]interface{}{ "nonce": nonceHex, - "gas_price": gasPriceHex, "gas_limit": transferGasLimitHex, "gas_cap": gasCapHex, "gas_tip": gasTipHex, @@ -118,7 +115,6 @@ func TestParse(t *testing.T) { AccountIdentifierSigners: []*types.AccountIdentifier{}, Metadata: map[string]interface{}{ "nonce": nonceERC20Hex, - "gas_price": gasPriceHex, "gas_limit": gasLimitERC20Hex, "gas_cap": gasCapERC20Hex, "gas_tip": gasTipERC20Hex, @@ -147,7 +143,6 @@ func TestParse(t *testing.T) { }, Metadata: map[string]interface{}{ "nonce": nonceERC20Hex, - "gas_price": gasPriceHex, "gas_limit": gasLimitERC20Hex, "gas_cap": gasCapERC20Hex, "gas_tip": gasTipERC20Hex, diff --git a/services/construction/payloads.go b/services/construction/payloads.go index 7663fdd..9463d40 100644 --- a/services/construction/payloads.go +++ b/services/construction/payloads.go @@ -58,7 +58,6 @@ func (a *APIService) ConstructionPayloads( amount := metadata.Value toAdd := metadata.To nonce := metadata.Nonce - gasPrice := metadata.GasPrice gasCap := metadata.GasCap gasTip := metadata.GasTip chainID := a.config.Params.ChainID @@ -95,7 +94,6 @@ func (a *APIService) ConstructionPayloads( Value: amount, Data: tx.Data(), Nonce: tx.Nonce(), - GasPrice: gasPrice, GasLimit: tx.Gas(), GasCap: tx.GasFeeCap(), GasTip: tx.GasTipCap(), diff --git a/services/construction/payloads_test.go b/services/construction/payloads_test.go index fbde94d..f696ac7 100644 --- a/services/construction/payloads_test.go +++ b/services/construction/payloads_test.go @@ -33,7 +33,6 @@ import ( var ( invalidTransferData = "0xaaaaaaaa000000000000000000000000efd3dc58d60af3295b92ecd484caeb3a2f30b3e70000000000000000000000000000000000000000000000000000000000000001" // nolint - payloadsGasPriceHex = "0x0" ) @@ -196,7 +195,6 @@ func templateNativeCurrencyTxMetadata(amount string) map[string]interface{} { "nonce": transferNonceHex, "to": metadataTo, "value": amount, - "gas_price": payloadsGasPriceHex, "gas_limit": transferGasLimitHex, "gas_cap": transferGasCapHex, "gas_tip": transferGasTipHex, @@ -208,7 +206,6 @@ func templateERC20CurrencyTxMetadata() map[string]interface{} { "nonce": transferNonceHex, "to": tokenContractAddress, "value": "0x0", - "gas_price": payloadsGasPriceHex, "gas_limit": transferGasLimitERC20Hex, "gas_cap": transferGasCapHex, "gas_tip": transferGasTipHex, @@ -218,13 +215,12 @@ func templateERC20CurrencyTxMetadata() map[string]interface{} { func templateNativeCurrencyUnsigned() string { return fmt.Sprintf( - `{"from":"%s","to":"%s","value":"%s","data":"%s","nonce":"%s","gas_price":"%s","max_fee_per_gas":"%s","max_priority_fee_per_gas":"%s","gas":"%s","chain_id":"%s"}`, //nolint:lll + `{"from":"%s","to":"%s","value":"%s","data":"%s","nonce":"%s","max_fee_per_gas":"%s","max_priority_fee_per_gas":"%s","gas":"%s","chain_id":"%s"}`, //nolint:lll metadataFrom, metadataTo, transferValueHex, "0x", transferNonceHex, - payloadsGasPriceHex, transferGasCapHex, transferGasTipHex, transferGasLimitHex, @@ -234,13 +230,12 @@ func templateNativeCurrencyUnsigned() string { func templateERC20CurrencyUnsigned() string { return fmt.Sprintf( - `{"from":"%s","to":"%s","value":"%s","data":"%s","nonce":"%s","gas_price":"%s","max_fee_per_gas":"%s","max_priority_fee_per_gas":"%s","gas":"%s","chain_id":"%s"}`, //nolint:lll + `{"from":"%s","to":"%s","value":"%s","data":"%s","nonce":"%s","max_fee_per_gas":"%s","max_priority_fee_per_gas":"%s","gas":"%s","chain_id":"%s"}`, //nolint:lll metadataFrom, tokenContractAddress, "0x0", metadataData, transferNonceHex, - payloadsGasPriceHex, transferGasCapHex, transferGasTipHex, transferGasLimitERC20Hex, @@ -253,7 +248,6 @@ func templateGenericContractCallTxMetadata() map[string]interface{} { "nonce": transferNonceHex, "to": tokenContractAddress, "value": "0x0", - "gas_price": payloadsGasPriceHex, "gas_limit": transferGasLimitERC20Hex, "gas_cap": transferGasCapHex, "gas_tip": transferGasTipHex, @@ -265,13 +259,12 @@ func templateGenericContractCallTxMetadata() map[string]interface{} { func templateGenericContractCallUnsigned() string { return fmt.Sprintf( - `{"from":"%s","to":"%s","value":"%s","data":"%s","nonce":"%s","gas_price":"%s","max_fee_per_gas":"%s","max_priority_fee_per_gas":"%s","gas":"%s","chain_id":"%s"}`, //nolint:lll + `{"from":"%s","to":"%s","value":"%s","data":"%s","nonce":"%s","max_fee_per_gas":"%s","max_priority_fee_per_gas":"%s","gas":"%s","chain_id":"%s"}`, //nolint:lll metadataFrom, tokenContractAddress, "0x0", metadataGenericData, transferNonceHex, - payloadsGasPriceHex, transferGasCapHex, transferGasTipHex, transferGasLimitERC20Hex, diff --git a/services/construction/preprocess.go b/services/construction/preprocess.go index e000b39..2718b44 100644 --- a/services/construction/preprocess.go +++ b/services/construction/preprocess.go @@ -95,6 +95,25 @@ func (a *APIService) ConstructionPreprocess( preprocessOutputOptions.Nonce = bigObj } + // Override gas_tip + if v, ok := request.Metadata["gas_tip"]; ok { + stringObj, ok := v.(string) + if !ok { + return nil, svcErrors.WrapErr( + svcErrors.ErrInvalidGasTip, + fmt.Errorf("%s is not a valid gas_tip string", v), + ) + } + bigObj, ok := new(big.Int).SetString(stringObj, 10) //nolint:gomnd + if !ok { + return nil, svcErrors.WrapErr( + svcErrors.ErrInvalidGasTip, + fmt.Errorf("%s is not a valid gas_tip", v), + ) + } + preprocessOutputOptions.GasTip = bigObj + } + // Override gas_cap if v, ok := request.Metadata["gas_cap"]; ok { stringObj, ok := v.(string) diff --git a/services/construction/preprocess_test.go b/services/construction/preprocess_test.go index b9eae32..f822e44 100644 --- a/services/construction/preprocess_test.go +++ b/services/construction/preprocess_test.go @@ -35,10 +35,10 @@ var ( preprocessTransferValueHex = hexutil.EncodeUint64(preprocessTransferValue) preprocessTransferValueLargeHex = hexutil.EncodeUint64(preprocessTransferValueLargeValue) preprocessData = "0xa9059cbb000000000000000000000000efd3dc58d60af3295b92ecd484caeb3a2f30b3e70000000000000000000000000000000000000000000000000000000000000001" // nolint - preprocessGasPrice = uint64(100000000000) - preprocessGasPriceHex = hexutil.EncodeUint64(preprocessGasPrice) preprocessGasLimit = uint64(600000) preprocessGasLimitHex = hexutil.EncodeUint64(preprocessGasLimit) + preprocessGasTip = uint64(20000000) + preprocessGasTipHex = hexutil.EncodeUint64(preprocessGasTip) preprocessGasCap = uint64(5000000000) preprocessGasCapHex = hexutil.EncodeUint64(preprocessGasCap) preprocessGenericData = "0x095ea7b3000000000000000000000000d10a72cf054650931365cc44d912a4fd7525705800000000000000000000000000000000000000000000000000000000000003e8" @@ -241,6 +241,42 @@ func TestPreprocess(t *testing.T) { }, }, }, + "happy path: native currency with gas tip": { + operations: templateOperations(preprocessTransferValue, polygon.Currency), + metadata: map[string]interface{}{ + "gas_tip": "20000000", + }, + expectedResponse: &types.ConstructionPreprocessResponse{ + Options: map[string]interface{}{ + "from": preprocessFromAddress, + "to": preprocessToAddress, + "value": preprocessTransferValueHex, + "gas_tip": preprocessGasTipHex, + }, + }, + }, + "happy path: ERC20 currency with gas tip": { + operations: templateOperations(preprocessTransferValue, &types.Currency{ + Symbol: "USDC", + Decimals: 18, + Metadata: map[string]interface{}{ + "token_address": preprocessTokenContractAddress, + }, + }), + metadata: map[string]interface{}{ + "gas_tip": "20000000", + }, + expectedResponse: &types.ConstructionPreprocessResponse{ + Options: map[string]interface{}{ + "from": preprocessFromAddress, + "to": preprocessToAddress, + "value": "0x0", + "token_address": preprocessTokenContractAddress, + "data": preprocessData, + "gas_tip": preprocessGasTipHex, + }, + }, + }, "error: both positive amount": { operations: func() []*types.Operation { operations := templateOperations(preprocessTransferValue, polygon.Currency) @@ -348,6 +384,24 @@ func TestPreprocess(t *testing.T) { expectedError: templateError( svcErrors.ErrInvalidGasCap, "gas_cap is not a valid gas_cap"), }, + "error: invalid gas tip string": { + operations: templateOperations(preprocessTransferValue, polygon.Currency), + metadata: map[string]interface{}{ + "gas_tip": map[string]string{}, + }, + expectedResponse: nil, + expectedError: templateError( + svcErrors.ErrInvalidGasTip, "map[] is not a valid gas_tip string"), + }, + "error: invalid gas tip": { + operations: templateOperations(preprocessTransferValue, polygon.Currency), + metadata: map[string]interface{}{ + "gas_tip": "gas_tip", + }, + expectedResponse: nil, + expectedError: templateError( + svcErrors.ErrInvalidGasTip, "gas_tip is not a valid gas_tip"), + }, "error: missing token address": { operations: templateOperations(preprocessTransferValue, &types.Currency{ Symbol: "USDC", diff --git a/services/construction/types.go b/services/construction/types.go index 5b50133..64e413f 100644 --- a/services/construction/types.go +++ b/services/construction/types.go @@ -80,7 +80,6 @@ type options struct { TokenAddress string `json:"token_address,omitempty"` ContractAddress string `json:"contract_address,omitempty"` Value *big.Int `json:"value,omitempty"` - GasPrice *big.Int `json:"gas_price,omitempty"` GasLimit *big.Int `json:"gas_limit,omitempty"` GasCap *big.Int `json:"gas_cap,omitempty"` GasTip *big.Int `json:"gas_tip,omitempty"` @@ -96,7 +95,6 @@ type optionsWire struct { TokenAddress string `json:"token_address,omitempty"` ContractAddress string `json:"contract_address,omitempty"` Value string `json:"value,omitempty"` - GasPrice string `json:"gas_price,omitempty"` GasLimit string `json:"gas_limit,omitempty"` GasCap string `json:"gas_cap,omitempty"` GasTip string `json:"gas_tip,omitempty"` @@ -126,10 +124,6 @@ func (o *options) MarshalJSON() ([]byte, error) { ow.Value = hexutil.EncodeBig(o.Value) } - if o.GasPrice != nil { - ow.GasPrice = hexutil.EncodeBig(o.GasPrice) - } - if o.GasLimit != nil { ow.GasLimit = hexutil.EncodeBig(o.GasLimit) } @@ -181,14 +175,6 @@ func (o *options) UnmarshalJSON(data []byte) error { o.Value = value } - if len(ow.GasPrice) > 0 { - gasPrice, err := hexutil.DecodeBig(ow.GasPrice) - if err != nil { - return err - } - o.GasPrice = gasPrice - } - if len(ow.GasLimit) > 0 { gasLimit, err := hexutil.DecodeBig(ow.GasLimit) if err != nil { @@ -218,7 +204,6 @@ func (o *options) UnmarshalJSON(data []byte) error { type metadata struct { Nonce uint64 `json:"nonce"` - GasPrice *big.Int `json:"gas_price"` GasCap *big.Int `json:"gas_cap"` GasTip *big.Int `json:"gas_tip"` GasLimit uint64 `json:"gas_limit,omitempty"` @@ -231,7 +216,6 @@ type metadata struct { type metadataWire struct { Nonce string `json:"nonce"` - GasPrice string `json:"gas_price"` GasCap string `json:"gas_cap"` GasTip string `json:"gas_tip"` GasLimit string `json:"gas_limit,omitempty"` @@ -245,7 +229,6 @@ type metadataWire struct { func (m *metadata) MarshalJSON() ([]byte, error) { mw := &metadataWire{ Nonce: hexutil.Uint64(m.Nonce).String(), - GasPrice: hexutil.EncodeBig(m.GasPrice), GasCap: hexutil.EncodeBig(m.GasCap), GasTip: hexutil.EncodeBig(m.GasTip), To: m.To, @@ -279,11 +262,6 @@ func (m *metadata) UnmarshalJSON(data []byte) error { return err } - gasPrice, err := hexutil.DecodeBig(mw.GasPrice) - if err != nil { - return err - } - gasCap, err := hexutil.DecodeBig(mw.GasCap) if err != nil { return err @@ -294,7 +272,6 @@ func (m *metadata) UnmarshalJSON(data []byte) error { return err } - m.GasPrice = gasPrice m.GasCap = gasCap m.GasTip = gasTip m.Nonce = nonce @@ -331,7 +308,6 @@ func (m *metadata) UnmarshalJSON(data []byte) error { type parseMetadata struct { Nonce uint64 `json:"nonce"` - GasPrice *big.Int `json:"gas_price"` GasLimit uint64 `json:"gas_limit"` GasCap *big.Int `json:"gas_cap"` GasTip *big.Int `json:"gas_tip"` @@ -340,7 +316,6 @@ type parseMetadata struct { type parseMetadataWire struct { Nonce string `json:"nonce"` - GasPrice string `json:"gas_price"` GasLimit string `json:"gas_limit"` GasCap string `json:"gas_cap"` GasTip string `json:"gas_tip"` @@ -350,7 +325,6 @@ type parseMetadataWire struct { func (p *parseMetadata) MarshalJSON() ([]byte, error) { pmw := &parseMetadataWire{ Nonce: hexutil.Uint64(p.Nonce).String(), - GasPrice: hexutil.EncodeBig(p.GasPrice), GasLimit: hexutil.Uint64(p.GasLimit).String(), GasCap: hexutil.EncodeBig(p.GasCap), GasTip: hexutil.EncodeBig(p.GasTip), @@ -366,7 +340,6 @@ type transaction struct { Value *big.Int `json:"value"` Data []byte `json:"data"` Nonce uint64 `json:"nonce"` - GasPrice *big.Int `json:"gas_price"` GasCap *big.Int `json:"max_fee_per_gas"` GasTip *big.Int `json:"max_priority_fee_per_gas"` GasLimit uint64 `json:"gas"` @@ -379,7 +352,6 @@ type transactionWire struct { Value string `json:"value"` Data string `json:"data"` Nonce string `json:"nonce"` - GasPrice string `json:"gas_price"` GasCap string `json:"max_fee_per_gas"` GasTip string `json:"max_priority_fee_per_gas"` GasLimit string `json:"gas"` @@ -393,7 +365,6 @@ func (t *transaction) MarshalJSON() ([]byte, error) { Value: hexutil.EncodeBig(t.Value), Data: hexutil.Encode(t.Data), Nonce: hexutil.EncodeUint64(t.Nonce), - GasPrice: hexutil.EncodeBig(t.GasPrice), GasLimit: hexutil.EncodeUint64(t.GasLimit), GasCap: hexutil.EncodeBig(t.GasCap), GasTip: hexutil.EncodeBig(t.GasTip), @@ -425,11 +396,6 @@ func (t *transaction) UnmarshalJSON(data []byte) error { return err } - gasPrice, err := hexutil.DecodeBig(tw.GasPrice) - if err != nil { - return err - } - gasLimit, err := hexutil.DecodeUint64(tw.GasLimit) if err != nil { return err @@ -454,7 +420,6 @@ func (t *transaction) UnmarshalJSON(data []byte) error { t.Value = value t.Data = twData t.Nonce = nonce - t.GasPrice = gasPrice t.GasLimit = gasLimit t.GasCap = gasCap t.GasTip = gasTip diff --git a/services/errors/errors.go b/services/errors/errors.go index 855126b..8a6bb3e 100644 --- a/services/errors/errors.go +++ b/services/errors/errors.go @@ -209,6 +209,13 @@ var ( Code: 23, //nolint Message: "Gas cap invalid", } + + // ErrInvalidGasTip is returned when input gas cap + // is invalid. + ErrInvalidGasTip = &types.Error{ + Code: 24, //nolint + Message: "Gas tip invalid", + } ) // WrapErr adds details to the types.Error provided. We use a function