Skip to content

Commit

Permalink
Merge pull request #31 from ash-krishnan/add-eip-1559
Browse files Browse the repository at this point in the history
Adding EIP-1559 Support for Tx Construction
  • Loading branch information
VAIBHAVJINDAL3012 authored Jun 13, 2022
2 parents 4538f49 + a28f476 commit 0b60290
Show file tree
Hide file tree
Showing 18 changed files with 506 additions and 294 deletions.
2 changes: 1 addition & 1 deletion mocks/polygon/graph_ql.go

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

2 changes: 1 addition & 1 deletion mocks/polygon/jsonrpc.go

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

39 changes: 31 additions & 8 deletions mocks/services/client.go

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

25 changes: 12 additions & 13 deletions polygon/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Client struct {
burntContract map[string]string
}

// ClientConfig holds asset config information
type ClientConfig struct {
URL string
ChainConfig *params.ChainConfig
Expand Down Expand Up @@ -149,7 +150,7 @@ func (ec *Client) Status(ctx context.Context) (
[]*RosettaTypes.Peer,
error,
) {
header, err := ec.blockHeader(ctx, nil)
header, err := ec.BlockHeader(ctx, nil)
if err != nil {
return nil, -1, nil, nil, err
}
Expand Down Expand Up @@ -193,15 +194,11 @@ func (ec *Client) PendingNonceAt(ctx context.Context, account common.Address) (u
return uint64(result), err
}

// 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
}

// 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) {
var hex hexutil.Big
if err := ec.c.CallContext(ctx, &hex, "eth_gasPrice"); err != nil {
if err := ec.c.CallContext(ctx, &hex, "eth_maxPriorityFeePerGas"); err != nil {
return nil, err
}
return (*big.Int)(&hex), nil
Expand Down Expand Up @@ -255,7 +252,9 @@ func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction) er
if err != nil {
return err
}
return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", hexutil.Encode(data))
// We have to remove the first two bytes otherwise DynamicFeeTxs will not send:
// https://ethereum.stackexchange.com/questions/124447/eth-sendrawtransaction-with-dynamicfeetx-returns-expected-input-list-for-types
return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", hexutil.Encode(data[2:]))
}

func toBlockNumArg(number *big.Int) string {
Expand Down Expand Up @@ -289,9 +288,9 @@ func (ec *Client) Block(
return ec.getParsedBlock(ctx, "eth_getBlockByNumber", toBlockNumArg(nil), true)
}

// Header returns a block header from the current canonical chain. If number is
// BlockHeader returns a block header from the current canonical chain. If number is
// nil, the latest known header is returned.
func (ec *Client) blockHeader(ctx context.Context, number *big.Int) (*types.Header, error) {
func (ec *Client) BlockHeader(ctx context.Context, number *big.Int) (*types.Header, error) {
var head *types.Header
err := ec.c.CallContext(ctx, &head, "eth_getBlockByNumber", toBlockNumArg(number), false)
if err == nil && head == nil {
Expand Down Expand Up @@ -1517,7 +1516,7 @@ func toCallArg(msg ethereum.CallMsg) interface{} {
return arg
}

// This implementation is taken from:
// CalculateBurntContract implementation is taken from:
// https://github.com/maticnetwork/bor/blob/c227a072418626dd758ceabffd2ea7dadac6eecb/params/config.go#L527
//
// TODO: Depend on maticnetwork fork of go-ethereum instead of stock geth so we don't need to
Expand Down
69 changes: 1 addition & 68 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 All @@ -1447,7 +1380,7 @@ func TestSendTransaction(t *testing.T) {
ctx,
mock.Anything,
"eth_sendRawTransaction",
"0xf86a80843b9aca00825208941ff502f9fe838cd772874cb67d0d96b93fd1d6d78725d4b6199a415d8029a01d110bf9fd468f7d00b3ce530832e99818835f45e9b08c66f8d9722264bb36c7a02711f47ec99f9ac585840daef41b7118b52ec72f02fcb30d874d36b10b668b59", // nolint
"0x80843b9aca00825208941ff502f9fe838cd772874cb67d0d96b93fd1d6d78725d4b6199a415d8029a01d110bf9fd468f7d00b3ce530832e99818835f45e9b08c66f8d9722264bb36c7a02711f47ec99f9ac585840daef41b7118b52ec72f02fcb30d874d36b10b668b59", // nolint
).Return(
nil,
).Once()
Expand Down
22 changes: 12 additions & 10 deletions services/construction/combine.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ func (a *APIService) ConstructionCombine(
return nil, svcErrors.WrapErr(svcErrors.ErrUnableToParseIntermediateResult, err)
}

ethTransaction := ethTypes.NewTransaction(
unsignedTx.Nonce,
common.HexToAddress(unsignedTx.To),
unsignedTx.Value,
unsignedTx.GasLimit,
unsignedTx.GasPrice,
unsignedTx.Data,
)

signer := ethTypes.NewEIP155Signer(unsignedTx.ChainID)
toAddress := common.HexToAddress(unsignedTx.To)
ethTransaction := ethTypes.NewTx(
&ethTypes.DynamicFeeTx{
Nonce: unsignedTx.Nonce,
To: &toAddress,
Value: unsignedTx.Value,
Gas: unsignedTx.GasLimit,
GasFeeCap: unsignedTx.GasCap,
GasTipCap: unsignedTx.GasTip,
Data: unsignedTx.Data,
})
signer := ethTypes.NewLondonSigner(unsignedTx.ChainID)
signedTx, err := ethTransaction.WithSignature(signer, request.Signatures[0].Bytes)
if err != nil {
return nil, svcErrors.WrapErr(svcErrors.ErrInvalidSignature, err)
Expand Down
9 changes: 5 additions & 4 deletions services/construction/combine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
"github.com/coinbase/rosetta-sdk-go/types"
svcError "github.com/maticnetwork/polygon-rosetta/services/errors"
"github.com/stretchr/testify/assert"
)
)


func TestConstructionCombine(t *testing.T) {
unsignedRaw := `{"from":"0xD10a72Cf054650931365Cc44D912a4FD75257058","to":"0x57B414a0332B5CaB885a451c2a28a07d1e9b8a8d","value":"0x9864aac3510d02","data":"0x","nonce":"0x0","gas_price":"0x3b9aca00","gas":"0x5208","chain_id":"0x13881"}` // nolint
signedRaw := `{"type":"0x0","nonce":"0x0","gasPrice":"0x3b9aca00","maxPriorityFeePerGas":null,"maxFeePerGas":null,"gas":"0x5208","value":"0x9864aac3510d02","input":"0x","v":"0x27126","r":"0x303b2ff05024c20f1775dad9a6e8152fa75bec47c051d7fd2e39572fbddd048e","s":"0xf2c494280dfa0465d384280dc918c930aae0874714e893382c16058aadf505","to":"0x57b414a0332b5cab885a451c2a28a07d1e9b8a8d","hash":"0x2500ef3f8531452210cfdfe3c11111e9605a2acdd260ac75c8c3ade30258228e"}` // nolint
signaturesRaw := `[{"hex_bytes":"303b2ff05024c20f1775dad9a6e8152fa75bec47c051d7fd2e39572fbddd048e00f2c494280dfa0465d384280dc918c930aae0874714e893382c16058aadf50501","signing_payload":{"address":"0xD10a72Cf054650931365Cc44D912a4FD75257058","hex_bytes":"375623b2f9164db0bc050c357fb4e6b57a60ffa1eba0161fe12e96384103218c","account_identifier":{"address":"0xD10a72Cf054650931365Cc44D912a4FD75257058"},"signature_type":"ecdsa_recovery"},"public_key":{"hex_bytes":"0212e9f98d9750e5f74b4b4b00df39074f86c79187943bdb3c5a9c89ffc1ed0188","curve_type":"secp256k1"},"signature_type":"ecdsa_recovery"}]` // nolint
unsignedRaw := `{"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
signedRaw := `{"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
signaturesRaw := `[{"hex_bytes": "6afe2f65d311ff2430ca7388335b86e42606ea4728924d91564405df83d2cea5443a04f2d96ea9877ed67f2b45266446ab01de2154c268470f57bb12effa156301", "public_key": {"hex_bytes": "0405e82ac561143aafc13ba109677a597c8f797b07417d0addd7a346ad35882b3c4a006620e02127b9a32e90979ff93ecad0a2f577db238163a50023e393e354ff", "curve_type": "secp256k1"}, "signing_payload": {"hex_bytes": "15ff43e2bc6aacc7d0f0ed76eb3102aaf9b1292e2ba07575a4e4f3ddb5b54780", "address": "0x5aCB42b3cfCD734a57AFF800139ba1354b549159"}, "signature_type": "ecdsa_recovery"}]` // nolint
var signatures []*types.Signature
_ = json.Unmarshal([]byte(signaturesRaw), &signatures)

Expand Down
Loading

0 comments on commit 0b60290

Please sign in to comment.