Skip to content

Commit

Permalink
removed gas price from preprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
ash-krishnan committed Jun 6, 2022
1 parent 7075fdf commit 844e849
Show file tree
Hide file tree
Showing 10 changed files with 9 additions and 218 deletions.
23 changes: 0 additions & 23 deletions mocks/services/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 3 additions & 28 deletions polygon/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,6 @@ func (ec *Client) PendingNonceAt(ctx context.Context, account common.Address) (u
return uint64(result), err
}

// SuggestGasTipCap retrieves the currently suggested gas tip cap after 1559 to
// allow a timely execution of a transaction.
//func (ec *Client) GetFeeHistory(ctx context.Context) (*big.Int, error) {
// historicalBlocks := 4
// var hex hexutil.Big
// if err := ec.c.CallContext(ctx, &hex, "eth_feeHistory", historicalBlocks, "pending", nil); err != nil {
// return nil, err
// }
// return (*big.Int)(&hex), nil
//}

// SuggestGasTipCap retrieves the currently suggested gas tip cap after 1559 to
// allow a timely execution of a transaction.
func (ec *Client) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
Expand All @@ -215,20 +204,6 @@ func (ec *Client) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
return (*big.Int)(&hex), nil
}

// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
// execution of a transaction.
func (ec *Client) SuggestGasPrice(ctx context.Context, gasPrice *big.Int) (*big.Int, error) {
if gasPrice != nil {
return gasPrice, nil
}

var hex hexutil.Big
if err := ec.c.CallContext(ctx, &hex, "eth_gasPrice"); err != nil {
return nil, err
}
return (*big.Int)(&hex), nil
}

// EstimateGas retrieves the currently gas limit
func (ec *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error) {
var hex hexutil.Uint64
Expand Down Expand Up @@ -391,7 +366,7 @@ func (ec *Client) getBlock(
txs[i] = tx.tx
receipt := receipts[i]
gasUsed := new(big.Int).SetUint64(receipt.GasUsed)
gasPrice, err := EffectiveGasPrice(txs[i], head.BaseFee)
gasPrice, err := effectiveGasPrice(txs[i], head.BaseFee)
if err != nil {
return nil, nil, fmt.Errorf("%w: failure getting gas price", err)
}
Expand Down Expand Up @@ -424,9 +399,9 @@ func (ec *Client) getBlock(
return types.NewBlockWithHeader(&head).WithBody(txs, uncles), loadedTxs, nil
}

// EffectiveGasPrice returns the price of gas charged to this transaction to be included in the
// effectiveGasPrice returns the price of gas charged to this transaction to be included in the
// block.
func EffectiveGasPrice(tx *EthTypes.Transaction, baseFee *big.Int) (*big.Int, error) {
func effectiveGasPrice(tx *EthTypes.Transaction, baseFee *big.Int) (*big.Int, error) {
if tx.Type() != eip1559TxType {
return tx.GasPrice(), nil
}
Expand Down
67 changes: 0 additions & 67 deletions polygon/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1360,73 +1360,6 @@ func TestPendingNonceAt(t *testing.T) {
mockGraphQL.AssertExpectations(t)
}

func TestSuggestGasPrice_Nil(t *testing.T) {
mockJSONRPC := &mocks.JSONRPC{}
mockGraphQL := &mocks.GraphQL{}

cf, err := newERC20CurrencyFetcher(mockGraphQL)
assert.NoError(t, err)

c := &Client{
c: mockJSONRPC,
g: mockGraphQL,
currencyFetcher: cf,
traceSemaphore: semaphore.NewWeighted(100),
}

ctx := context.Background()
mockJSONRPC.On(
"CallContext",
ctx,
mock.Anything,
"eth_gasPrice",
).Return(
nil,
).Run(
func(args mock.Arguments) {
r := args.Get(1).(*hexutil.Big)

*r = *(*hexutil.Big)(big.NewInt(100000))
},
).Once()
resp, err := c.SuggestGasPrice(
ctx,
nil,
)
assert.Equal(t, big.NewInt(100000), resp)
assert.NoError(t, err)

mockJSONRPC.AssertExpectations(t)
mockGraphQL.AssertExpectations(t)
}

func TestSuggestGasPrice_Valid(t *testing.T) {
mockJSONRPC := &mocks.JSONRPC{}
mockGraphQL := &mocks.GraphQL{}

cf, err := newERC20CurrencyFetcher(mockGraphQL)
assert.NoError(t, err)

c := &Client{
c: mockJSONRPC,
g: mockGraphQL,
currencyFetcher: cf,
traceSemaphore: semaphore.NewWeighted(100),
}

ctx := context.Background()
gasPrice, _ := new(big.Int).SetString("100000000000", 10)
resp, err := c.SuggestGasPrice(
ctx,
gasPrice,
)
assert.Equal(t, gasPrice, resp)
assert.NoError(t, err)

mockJSONRPC.AssertExpectations(t)
mockGraphQL.AssertExpectations(t)
}

func TestSendTransaction(t *testing.T) {
mockJSONRPC := &mocks.JSONRPC{}
mockGraphQL := &mocks.GraphQL{}
Expand Down
1 change: 1 addition & 0 deletions services/construction/combine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"encoding/json"
"errors"

"github.com/coinbase/rosetta-sdk-go/types"
"github.com/ethereum/go-ethereum/common"
ethTypes "github.com/ethereum/go-ethereum/core/types"
Expand Down
5 changes: 3 additions & 2 deletions services/construction/combine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ package construction
import (
"context"
"encoding/json"
"testing"

"github.com/coinbase/rosetta-sdk-go/types"
svcError "github.com/maticnetwork/polygon-rosetta/services/errors"
"github.com/stretchr/testify/assert"
"testing"
)
)


func TestConstructionCombine(t *testing.T) {
Expand Down
18 changes: 0 additions & 18 deletions services/construction/construction_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func TestConstructionFlowWithPendingNonce(t *testing.T) {
Value: big.NewInt(1000),
}

//var gasPrice *big.Int = nil
var blockNum *big.Int = nil

mockClient.On(
Expand All @@ -186,14 +185,6 @@ func TestConstructionFlowWithPendingNonce(t *testing.T) {
big.NewInt(1500000000),
nil,
).Once()
//mockClient.On(
// "SuggestGasPrice",
// ctx,
// gasPrice,
//).Return(
// big.NewInt(0),
// nil,
//).Once()
mockClient.On(
"PendingNonceAt",
ctx,
Expand Down Expand Up @@ -390,7 +381,6 @@ func TestConstructionFlowWithInputNonce(t *testing.T) {
Value: big.NewInt(1000),
}

// var gasPrice *big.Int = nil
var blockNum *big.Int = nil

mockClient.On(
Expand All @@ -408,14 +398,6 @@ func TestConstructionFlowWithInputNonce(t *testing.T) {
big.NewInt(1500000000),
nil,
).Once()
//mockClient.On(
// "SuggestGasPrice",
// ctx,
// gasPrice,
//).Return(
// big.NewInt(1001000000),
// nil,
//).Once()
metadataResponse, err := servicer.ConstructionMetadata(ctx, &types.ConstructionMetadataRequest{
NetworkIdentifier: networkIdentifier,
Options: preprocessResponse.Options,
Expand Down
7 changes: 2 additions & 5 deletions services/construction/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,14 @@ func (a *APIService) ConstructionMetadata(

var gasCap *big.Int
if input.GasCap == nil {
// Set default max fee to double the last base fee plus priority tip
// to ensure tx is highly likely to go out in the next block
multiplier := big.NewInt(2)
gasCap = new(big.Int).Add(gasTip, new(big.Int).Mul(header.BaseFee, multiplier))
} else {
gasCap = input.GasCap
}

// gasPrice, err := a.client.SuggestGasPrice(ctx, input.GasPrice)
// if err != nil {
// return nil, svcErrors.WrapErr(svcErrors.ErrGeth, err)
//}

metadata := &metadata{
Nonce: nonce,
GasPrice: big.NewInt(0),
Expand Down
19 changes: 0 additions & 19 deletions services/construction/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,6 @@ func (a *APIService) ConstructionPreprocess(
preprocessOutputOptions.GasCap = bigObj
}

// Override gas_price
if v, ok := request.Metadata["gas_price"]; ok {
stringObj, ok := v.(string)
if !ok {
return nil, svcErrors.WrapErr(
svcErrors.ErrInvalidGasPrice,
fmt.Errorf("%s is not a valid gas_price string", v),
)
}
bigObj, ok := new(big.Int).SetString(stringObj, 10) //nolint:gomnd
if !ok {
return nil, svcErrors.WrapErr(
svcErrors.ErrInvalidGasPrice,
fmt.Errorf("%s is not a valid gas_price", v),
)
}
preprocessOutputOptions.GasPrice = bigObj
}

// Override gas_limit
if v, ok := request.Metadata["gas_limit"]; ok {
stringObj, ok := v.(string)
Expand Down
54 changes: 0 additions & 54 deletions services/construction/preprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,42 +169,6 @@ func TestPreprocess(t *testing.T) {
},
},
},
"happy path: native currency with gas price": {
operations: templateOperations(preprocessTransferValue, polygon.Currency),
metadata: map[string]interface{}{
"gas_price": "100000000000",
},
expectedResponse: &types.ConstructionPreprocessResponse{
Options: map[string]interface{}{
"from": preprocessFromAddress,
"to": preprocessToAddress,
"value": preprocessTransferValueHex,
"gas_price": preprocessGasPriceHex,
},
},
},
"happy path: ERC20 currency with gas price": {
operations: templateOperations(preprocessTransferValue, &types.Currency{
Symbol: "USDC",
Decimals: 18,
Metadata: map[string]interface{}{
"token_address": preprocessTokenContractAddress,
},
}),
metadata: map[string]interface{}{
"gas_price": "100000000000",
},
expectedResponse: &types.ConstructionPreprocessResponse{
Options: map[string]interface{}{
"from": preprocessFromAddress,
"to": preprocessToAddress,
"value": "0x0",
"token_address": preprocessTokenContractAddress,
"data": preprocessData,
"gas_price": preprocessGasPriceHex,
},
},
},
"happy path: native currency with gas limit": {
operations: templateOperations(preprocessTransferValue, polygon.Currency),
metadata: map[string]interface{}{
Expand Down Expand Up @@ -348,24 +312,6 @@ func TestPreprocess(t *testing.T) {
expectedError: templateError(
svcErrors.ErrInvalidNonce, "invalid_nonce is not a valid nonce"),
},
"error: invalid gas price string": {
operations: templateOperations(preprocessTransferValue, polygon.Currency),
metadata: map[string]interface{}{
"gas_price": map[string]string{},
},
expectedResponse: nil,
expectedError: templateError(
svcErrors.ErrInvalidGasPrice, "map[] is not a valid gas_price string"),
},
"error: invalid gas price": {
operations: templateOperations(preprocessTransferValue, polygon.Currency),
metadata: map[string]interface{}{
"gas_price": "gas_price",
},
expectedResponse: nil,
expectedError: templateError(
svcErrors.ErrInvalidGasPrice, "gas_price is not a valid gas_price"),
},
"error: invalid gas limit string": {
operations: templateOperations(preprocessTransferValue, polygon.Currency),
metadata: map[string]interface{}{
Expand Down
2 changes: 0 additions & 2 deletions services/construction/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ type Client interface {

EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error)

SuggestGasPrice(ctx context.Context, gasPrice *big.Int) (*big.Int, error)

BlockHeader(ctx context.Context, number *big.Int) (*ethTypes.Header, error)

SendTransaction(ctx context.Context, tx *ethTypes.Transaction) error
Expand Down

0 comments on commit 844e849

Please sign in to comment.