-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: differentiate legacy and dyn fee json response
- Loading branch information
1 parent
042005f
commit cce4058
Showing
4 changed files
with
184 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package transactions | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/common/math" | ||
"github.com/vechain/thor/v2/block" | ||
"github.com/vechain/thor/v2/thor" | ||
"github.com/vechain/thor/v2/tx" | ||
) | ||
|
||
type Transaction struct { | ||
ID thor.Bytes32 `json:"id"` | ||
ChainTag byte `json:"chainTag"` | ||
BlockRef string `json:"blockRef"` | ||
Expiration uint32 `json:"expiration"` | ||
Clauses Clauses `json:"clauses"` | ||
GasPriceCoef uint8 `json:"gasPriceCoef"` | ||
Gas uint64 `json:"gas"` | ||
MaxFeePerGas *big.Int `json:"maxFeePerGas,omitempty"` | ||
MaxPriorityFeePerGas *big.Int `json:"maxPriorityFeePerGas,omitempty"` | ||
TxType math.HexOrDecimal64 `json:"txType"` | ||
Origin thor.Address `json:"origin"` | ||
Delegator *thor.Address `json:"delegator"` | ||
Nonce math.HexOrDecimal64 `json:"nonce"` | ||
DependsOn *thor.Bytes32 `json:"dependsOn"` | ||
Size uint32 `json:"size"` | ||
Meta *TxMeta `json:"meta"` | ||
} | ||
|
||
// convertTransaction convert a raw transaction into a json format transaction | ||
func convertTransaction(trx *tx.Transaction, header *block.Header) *Transaction { | ||
//tx origin | ||
origin, _ := trx.Origin() | ||
delegator, _ := trx.Delegator() | ||
|
||
cls := make(Clauses, len(trx.Clauses())) | ||
for i, c := range trx.Clauses() { | ||
cls[i] = convertClause(c) | ||
} | ||
br := trx.BlockRef() | ||
t := &Transaction{ | ||
ChainTag: trx.ChainTag(), | ||
TxType: math.HexOrDecimal64(trx.Type()), | ||
ID: trx.ID(), | ||
Origin: origin, | ||
BlockRef: hexutil.Encode(br[:]), | ||
Expiration: trx.Expiration(), | ||
Nonce: math.HexOrDecimal64(trx.Nonce()), | ||
Size: uint32(trx.Size()), | ||
Gas: trx.Gas(), | ||
DependsOn: trx.DependsOn(), | ||
Clauses: cls, | ||
Delegator: delegator, | ||
} | ||
|
||
switch trx.Type() { | ||
case tx.LegacyTxType: | ||
t.GasPriceCoef = trx.GasPriceCoef() | ||
default: | ||
t.MaxFeePerGas = trx.MaxFeePerGas() | ||
t.MaxPriorityFeePerGas = trx.MaxPriorityFeePerGas() | ||
} | ||
|
||
if header != nil { | ||
t.Meta = &TxMeta{ | ||
BlockID: header.ID(), | ||
BlockNumber: header.Number(), | ||
BlockTimestamp: header.Timestamp(), | ||
} | ||
} | ||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package transactions | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/common/math" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/vechain/thor/v2/block" | ||
"github.com/vechain/thor/v2/thor" | ||
"github.com/vechain/thor/v2/tx" | ||
) | ||
|
||
func TestConvertLegacyTransaction_Success(t *testing.T) { | ||
addr := thor.BytesToAddress([]byte("to")) | ||
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000)) | ||
cla2 := tx.NewClause(&addr).WithValue(big.NewInt(10000)) | ||
br := tx.NewBlockRef(0) | ||
transaction := new(tx.LegacyBuilder). | ||
ChainTag(123). | ||
GasPriceCoef(1). | ||
Expiration(10). | ||
Gas(37000). | ||
Nonce(1). | ||
Clause(cla). | ||
Clause(cla2). | ||
BlockRef(br). | ||
Build() | ||
|
||
header := new(block.Builder).Build().Header() | ||
|
||
result := convertTransaction(transaction, header) | ||
// Common fields | ||
assert.Equal(t, hexutil.Encode(br[:]), result.BlockRef) | ||
assert.Equal(t, transaction.ChainTag(), result.ChainTag) | ||
assert.Equal(t, transaction.Expiration(), result.Expiration) | ||
assert.Equal(t, transaction.Gas(), result.Gas) | ||
assert.Equal(t, math.HexOrDecimal64(transaction.Nonce()), result.Nonce) | ||
assert.Equal(t, 2, len(result.Clauses)) | ||
assert.Equal(t, addr, *result.Clauses[0].To) | ||
assert.Equal(t, convertClause(cla), result.Clauses[0]) | ||
assert.Equal(t, addr, *result.Clauses[1].To) | ||
assert.Equal(t, convertClause(cla2), result.Clauses[1]) | ||
// Legacy fields | ||
assert.Equal(t, uint8(1), result.GasPriceCoef) | ||
// Non legacy fields | ||
assert.Empty(t, result.MaxFeePerGas) | ||
assert.Empty(t, result.MaxPriorityFeePerGas) | ||
} | ||
|
||
func TestConvertDynTransaction_Success(t *testing.T) { | ||
addr := thor.BytesToAddress([]byte("to")) | ||
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000)) | ||
cla2 := tx.NewClause(&addr).WithValue(big.NewInt(10000)) | ||
br := tx.NewBlockRef(0) | ||
maxFeePerGas := big.NewInt(25000) | ||
maxPriorityFeePerGas := big.NewInt(100) | ||
transaction := new(tx.DynFeeBuilder). | ||
ChainTag(123). | ||
MaxFeePerGas(maxFeePerGas). | ||
MaxPriorityFeePerGas(maxPriorityFeePerGas). | ||
Expiration(10). | ||
Gas(37000). | ||
Nonce(1). | ||
Clause(cla). | ||
Clause(cla2). | ||
BlockRef(br). | ||
Build() | ||
|
||
header := new(block.Builder).Build().Header() | ||
|
||
result := convertTransaction(transaction, header) | ||
// Common fields | ||
assert.Equal(t, hexutil.Encode(br[:]), result.BlockRef) | ||
assert.Equal(t, transaction.ChainTag(), result.ChainTag) | ||
assert.Equal(t, transaction.Expiration(), result.Expiration) | ||
assert.Equal(t, transaction.Gas(), result.Gas) | ||
assert.Equal(t, math.HexOrDecimal64(transaction.Nonce()), result.Nonce) | ||
assert.Equal(t, 2, len(result.Clauses)) | ||
assert.Equal(t, addr, *result.Clauses[0].To) | ||
assert.Equal(t, convertClause(cla), result.Clauses[0]) | ||
assert.Equal(t, addr, *result.Clauses[1].To) | ||
assert.Equal(t, convertClause(cla2), result.Clauses[1]) | ||
// DynFee fields | ||
assert.Equal(t, maxFeePerGas, result.MaxFeePerGas) | ||
assert.Equal(t, maxPriorityFeePerGas, result.MaxPriorityFeePerGas) | ||
// Non dynFee fields | ||
assert.Empty(t, result.GasPriceCoef) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters