Skip to content

Commit

Permalink
refactor: transactions (#3502)
Browse files Browse the repository at this point in the history
  • Loading branch information
Haseeb Raja committed Nov 14, 2022
1 parent c269f2d commit cc1f281
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
8 changes: 7 additions & 1 deletion openapi/SwarmCommon.yaml
@@ -1,6 +1,6 @@
openapi: 3.0.3
info:
version: 2.2.0
version: 2.3.0
title: Common Data Types
description: |
\*****bzzz*****
Expand Down Expand Up @@ -655,6 +655,12 @@ components:
$ref: "#/components/schemas/BigInt"
gasLimit:
type: integer
gasTipCap:
$ref: "#/components/schemas/BigInt"
gasTipBoost:
type: integer
gasFeeCap:
$ref: "#/components/schemas/BigInt"
data:
type: string
created:
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/transaction.go
Expand Up @@ -32,6 +32,9 @@ type transactionInfo struct {
Nonce uint64 `json:"nonce"`
GasPrice *bigint.BigInt `json:"gasPrice"`
GasLimit uint64 `json:"gasLimit"`
GasTipBoost int `json:"gasTipBoost"`
GasTipCap *bigint.BigInt `json:"gasTipCap"`
GasFeeCap *bigint.BigInt `json:"gasFeeCap"`
Data string `json:"data"`
Created time.Time `json:"created"`
Description string `json:"description"`
Expand Down Expand Up @@ -69,6 +72,9 @@ func (s *Service) transactionListHandler(w http.ResponseWriter, _ *http.Request)
Nonce: storedTransaction.Nonce,
GasPrice: bigint.Wrap(storedTransaction.GasPrice),
GasLimit: storedTransaction.GasLimit,
GasFeeCap: bigint.Wrap(storedTransaction.GasFeeCap),
GasTipCap: bigint.Wrap(storedTransaction.GasTipCap),
GasTipBoost: storedTransaction.GasTipBoost,
Data: hexutil.Encode(storedTransaction.Data),
Created: time.Unix(storedTransaction.Created, 0),
Description: storedTransaction.Description,
Expand Down Expand Up @@ -111,6 +117,9 @@ func (s *Service) transactionDetailHandler(w http.ResponseWriter, r *http.Reques
Nonce: storedTransaction.Nonce,
GasPrice: bigint.Wrap(storedTransaction.GasPrice),
GasLimit: storedTransaction.GasLimit,
GasFeeCap: bigint.Wrap(storedTransaction.GasFeeCap),
GasTipCap: bigint.Wrap(storedTransaction.GasTipCap),
GasTipBoost: storedTransaction.GasTipBoost,
Data: hexutil.Encode(storedTransaction.Data),
Created: time.Unix(storedTransaction.Created, 0),
Description: storedTransaction.Description,
Expand Down
41 changes: 30 additions & 11 deletions pkg/api/transaction_test.go
Expand Up @@ -31,12 +31,13 @@ func TestTransactionStoredTransaction(t *testing.T) {
data := common.Hex2Bytes(dataStr)
created := int64(1616451040)
recipient := common.HexToAddress("fffe")
gasPrice := big.NewInt(23)
gasPrice := big.NewInt(12)
gasLimit := uint64(200)
value := big.NewInt(50)
nonce := uint64(12)
description := "test"

gasTipBoost := 10
gasTipCap := new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(gasTipBoost)+100), gasPrice), big.NewInt(100))
t.Run("found", func(t *testing.T) {
t.Parallel()

Expand All @@ -50,6 +51,9 @@ func TestTransactionStoredTransaction(t *testing.T) {
Data: data,
GasPrice: gasPrice,
GasLimit: gasLimit,
GasFeeCap: gasPrice,
GasTipBoost: gasTipBoost,
GasTipCap: gasTipCap,
Value: value,
Nonce: nonce,
Description: description,
Expand All @@ -66,6 +70,9 @@ func TestTransactionStoredTransaction(t *testing.T) {
To: &recipient,
GasPrice: bigint.Wrap(gasPrice),
GasLimit: gasLimit,
GasFeeCap: bigint.Wrap(gasPrice),
GasTipCap: bigint.Wrap(gasTipCap),
GasTipBoost: gasTipBoost,
Value: bigint.Wrap(value),
Nonce: nonce,
Description: description,
Expand Down Expand Up @@ -121,19 +128,25 @@ func TestTransactionList(t *testing.T) {
storedTransactions := map[common.Hash]*transaction.StoredTransaction{
txHash1: {
To: &recipient,
Created: 1,
Data: []byte{1, 2, 3, 4},
GasPrice: big.NewInt(12),
GasLimit: 5345,
GasTipBoost: 10,
GasTipCap: new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(10)+100), big.NewInt(12)), big.NewInt(100)),
GasFeeCap: big.NewInt(12),
Value: big.NewInt(4),
Nonce: 3,
Created: 1,
Description: "test",
},
txHash2: {
To: &recipient,
Created: 2,
Data: []byte{3, 2, 3, 4},
GasPrice: big.NewInt(42),
GasTipBoost: 10,
GasTipCap: new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(10)+100), big.NewInt(42)), big.NewInt(100)),
GasFeeCap: big.NewInt(42),
GasLimit: 53451,
Value: big.NewInt(41),
Nonce: 32,
Expand All @@ -158,25 +171,31 @@ func TestTransactionList(t *testing.T) {
PendingTransactions: []api.TransactionInfo{
{
TransactionHash: txHash1,
Created: time.Unix(storedTransactions[txHash1].Created, 0),
Data: hexutil.Encode(storedTransactions[txHash1].Data),
To: storedTransactions[txHash1].To,
Nonce: storedTransactions[txHash1].Nonce,
GasPrice: bigint.Wrap(storedTransactions[txHash1].GasPrice),
GasLimit: storedTransactions[txHash1].GasLimit,
Value: bigint.Wrap(storedTransactions[txHash1].Value),
Nonce: storedTransactions[txHash1].Nonce,
GasTipCap: bigint.Wrap(storedTransactions[txHash1].GasTipCap),
GasTipBoost: storedTransactions[txHash1].GasTipBoost,
GasFeeCap: bigint.Wrap(storedTransactions[txHash1].GasPrice),
Data: hexutil.Encode(storedTransactions[txHash1].Data),
Created: time.Unix(storedTransactions[txHash1].Created, 0),
Description: storedTransactions[txHash1].Description,
Value: bigint.Wrap(storedTransactions[txHash1].Value),
},
{
TransactionHash: txHash2,
Created: time.Unix(storedTransactions[txHash2].Created, 0),
Data: hexutil.Encode(storedTransactions[txHash2].Data),
To: storedTransactions[txHash2].To,
Nonce: storedTransactions[txHash2].Nonce,
GasPrice: bigint.Wrap(storedTransactions[txHash2].GasPrice),
GasLimit: storedTransactions[txHash2].GasLimit,
Value: bigint.Wrap(storedTransactions[txHash2].Value),
Nonce: storedTransactions[txHash2].Nonce,
GasTipCap: bigint.Wrap(storedTransactions[txHash2].GasTipCap),
GasTipBoost: storedTransactions[txHash2].GasTipBoost,
GasFeeCap: bigint.Wrap(storedTransactions[txHash2].GasPrice),
Data: hexutil.Encode(storedTransactions[txHash2].Data),
Created: time.Unix(storedTransactions[txHash2].Created, 0),
Description: storedTransactions[txHash2].Description,
Value: bigint.Wrap(storedTransactions[txHash2].Value),
},
},
}),
Expand Down
3 changes: 3 additions & 0 deletions pkg/node/devnode.go
Expand Up @@ -159,6 +159,9 @@ func NewDevBee(logger log.Logger, o *DevOptions) (b *DevBee, err error) {
Created: 1,
Data: []byte{1, 2, 3, 4},
GasPrice: big.NewInt(12),
GasTipBoost: 10,
GasFeeCap: big.NewInt(12),
GasTipCap: new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(10)+100), big.NewInt(12)), big.NewInt(100)),
GasLimit: 5345,
Value: big.NewInt(4),
Nonce: 3,
Expand Down
12 changes: 10 additions & 2 deletions pkg/transaction/transaction.go
Expand Up @@ -48,6 +48,7 @@ type TxRequest struct {
Data []byte // transaction data
GasPrice *big.Int // gas price or nil if suggested gas price should be used
GasLimit uint64 // gas limit or 0 if it should be estimated
GasFeeCap *big.Int // adds a cap to maximum fee user is willing to pay
Value *big.Int // amount of wei to send
Description string // optional description
}
Expand All @@ -56,8 +57,10 @@ type StoredTransaction struct {
To *common.Address // recipient of the transaction
Data []byte // transaction data
GasPrice *big.Int // used gas price
GasTipBoost int // percentage used to boost the priority fee (eg: tip)
GasLimit uint64 // used gas limit
GasTipBoost int // adds a tip for the miner for prioritizing transaction
GasTipCap *big.Int // adds a cap to the tip
GasFeeCap *big.Int // adds a cap to maximum fee user is willing to pay
Value *big.Int // amount of wei to send
Nonce uint64 // used nonce
Created int64 // creation timestamp
Expand Down Expand Up @@ -177,8 +180,10 @@ func (t *transactionService) Send(ctx context.Context, request *TxRequest, tipCa
To: signedTx.To(),
Data: signedTx.Data(),
GasPrice: signedTx.GasPrice(),
GasTipBoost: tipCapBoostPercent,
GasLimit: signedTx.Gas(),
GasTipBoost: tipCapBoostPercent,
GasTipCap: signedTx.GasTipCap(),
GasFeeCap: signedTx.GasFeeCap(),
Value: signedTx.Value(),
Nonce: signedTx.Nonce(),
Created: time.Now().Unix(),
Expand Down Expand Up @@ -485,6 +490,9 @@ func (t *transactionService) CancelTransaction(ctx context.Context, originalTxHa
Data: signedTx.Data(),
GasPrice: signedTx.GasPrice(),
GasLimit: signedTx.Gas(),
GasFeeCap: signedTx.GasFeeCap(),
GasTipBoost: storedTransaction.GasTipBoost,
GasTipCap: signedTx.GasTipCap(),
Value: signedTx.Value(),
Nonce: signedTx.Nonce(),
Created: time.Now().Unix(),
Expand Down

0 comments on commit cc1f281

Please sign in to comment.