Skip to content

Commit

Permalink
Jp/hotfix optional gas price in rpc transaction type (#1699)
Browse files Browse the repository at this point in the history
* Make GasPrice optional in toTransaction

* HOTFIX: Fixing linting issues

* Mark GasPrice as 'omitempty' for JSON marshalling
  • Loading branch information
jp-imx committed Jul 11, 2023
1 parent 62148c1 commit 8565d45
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
30 changes: 17 additions & 13 deletions jsonrpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type transactionOrHash interface {

type transaction struct {
Nonce argUint64 `json:"nonce"`
GasPrice argBig `json:"gasPrice"`
GasPrice *argBig `json:"gasPrice,omitempty"`
GasTipCap *argBig `json:"gasTipCap,omitempty"`
GasFeeCap *argBig `json:"gasFeeCap,omitempty"`
Gas argUint64 `json:"gas"`
Expand Down Expand Up @@ -58,18 +58,22 @@ func toTransaction(
txIndex *int,
) *transaction {
res := &transaction{
Nonce: argUint64(t.Nonce),
GasPrice: argBig(*t.GasPrice),
Gas: argUint64(t.Gas),
To: t.To,
Value: argBig(*t.Value),
Input: t.Input,
V: argBig(*t.V),
R: argBig(*t.R),
S: argBig(*t.S),
Hash: t.Hash,
From: t.From,
Type: argUint64(t.Type),
Nonce: argUint64(t.Nonce),
Gas: argUint64(t.Gas),
To: t.To,
Value: argBig(*t.Value),
Input: t.Input,
V: argBig(*t.V),
R: argBig(*t.R),
S: argBig(*t.S),
Hash: t.Hash,
From: t.From,
Type: argUint64(t.Type),
}

if t.GasPrice != nil {
gasPrice := argBig(*t.GasPrice)
res.GasPrice = &gasPrice
}

if t.GasTipCap != nil {
Expand Down
35 changes: 34 additions & 1 deletion jsonrpc/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,39 @@ func TestToTransaction_Returns_V_R_S_ValuesWithoutLeading0(t *testing.T) {
assert.Equal(t, hexWithoutLeading0, string(jsonS))
}

func TestToTransaction_EIP1559(t *testing.T) {
hexWithLeading0 := "0x0ba93811466694b3b3cb8853cb8227b7c9f49db10bf6e7db59d20ac904961565"
hexWithoutLeading0 := "0xba93811466694b3b3cb8853cb8227b7c9f49db10bf6e7db59d20ac904961565"
v, _ := hex.DecodeHex(hexWithLeading0)
r, _ := hex.DecodeHex(hexWithLeading0)
s, _ := hex.DecodeHex(hexWithLeading0)
txn := types.Transaction{
Nonce: 0,
GasPrice: nil,
GasTipCap: big.NewInt(10),
GasFeeCap: big.NewInt(10),
Gas: 0,
To: nil,
Value: big.NewInt(0),
Input: nil,
V: new(big.Int).SetBytes(v),
R: new(big.Int).SetBytes(r),
S: new(big.Int).SetBytes(s),
Hash: types.Hash{},
From: types.Address{},
}

jsonTx := toTransaction(&txn, nil, nil, nil)

jsonV, _ := jsonTx.V.MarshalText()
jsonR, _ := jsonTx.R.MarshalText()
jsonS, _ := jsonTx.S.MarshalText()

assert.Equal(t, hexWithoutLeading0, string(jsonV))
assert.Equal(t, hexWithoutLeading0, string(jsonR))
assert.Equal(t, hexWithoutLeading0, string(jsonS))
}

func TestBlock_Copy(t *testing.T) {
b := &block{
ExtraData: []byte{0x1},
Expand Down Expand Up @@ -206,7 +239,7 @@ func mockTxn() *transaction {

tt := &transaction{
Nonce: 1,
GasPrice: argBig(*big.NewInt(10)),
GasPrice: argBigPtr(big.NewInt(10)),
Gas: 100,
To: &to,
Value: argBig(*big.NewInt(1000)),
Expand Down

0 comments on commit 8565d45

Please sign in to comment.