Skip to content

Commit

Permalink
refactor: transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
swarmHaseeb committed Nov 7, 2022
1 parent c0c8239 commit 4eef0c5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
6 changes: 6 additions & 0 deletions pkg/api/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type transactionInfo struct {
Nonce uint64 `json:"nonce"`
GasPrice *bigint.BigInt `json:"gasPrice"`
GasLimit uint64 `json:"gasLimit"`
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 +71,8 @@ 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),
Data: hexutil.Encode(storedTransaction.Data),
Created: time.Unix(storedTransaction.Created, 0),
Description: storedTransaction.Description,
Expand Down Expand Up @@ -111,6 +115,8 @@ 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),
Data: hexutil.Encode(storedTransaction.Data),
Created: time.Unix(storedTransaction.Created, 0),
Description: storedTransaction.Description,
Expand Down
30 changes: 21 additions & 9 deletions pkg/api/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func TestTransactionStoredTransaction(t *testing.T) {
Data: data,
GasPrice: gasPrice,
GasLimit: gasLimit,
GasFeeCap: gasPrice,
GasTipCap: gasPrice,
Value: value,
Nonce: nonce,
Description: description,
Expand All @@ -66,6 +68,8 @@ func TestTransactionStoredTransaction(t *testing.T) {
To: &recipient,
GasPrice: bigint.Wrap(gasPrice),
GasLimit: gasLimit,
GasFeeCap: bigint.Wrap(gasPrice),
GasTipCap: bigint.Wrap(gasPrice),
Value: bigint.Wrap(value),
Nonce: nonce,
Description: description,
Expand Down Expand Up @@ -121,19 +125,23 @@ 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,
GasTipCap: big.NewInt(12),
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),
GasTipCap: nil,
GasFeeCap: big.NewInt(42),
GasLimit: 53451,
Value: big.NewInt(41),
Nonce: 32,
Expand All @@ -158,25 +166,29 @@ 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].GasPrice),
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: nil,
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
16 changes: 12 additions & 4 deletions pkg/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ 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
GasTipCap *big.Int // adds a tip for the miner for prioritizing transaction
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 @@ -60,6 +62,8 @@ type StoredTransaction struct {
Data []byte // transaction data
GasPrice *big.Int // used gas price
GasLimit uint64 // used gas limit
GasTipCap *big.Int // adds a tip for the miner for prioritizing transaction
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 @@ -180,6 +184,8 @@ func (t *transactionService) Send(ctx context.Context, request *TxRequest, boost
Data: signedTx.Data(),
GasPrice: signedTx.GasPrice(),
GasLimit: signedTx.Gas(),
GasTipCap: signedTx.GasTipCap(),
GasFeeCap: signedTx.GasFeeCap(),
Value: signedTx.Value(),
Nonce: signedTx.Nonce(),
Created: time.Now().Unix(),
Expand Down Expand Up @@ -277,10 +283,12 @@ func (t *transactionService) prepareTransaction(ctx context.Context, request *Tx
if err != nil {
return nil, err
}
if boostPercent != 0 {
request.GasPrice = new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(boostPercent)+100), request.GasPrice), big.NewInt(100))
}
}

if boostPercent != 0 {
request.GasPrice = new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(boostPercent)+100), request.GasPrice), big.NewInt(100))
}

if request.GasPrice.Cmp(minGasPrice) < 0 {
return nil, ErrGasPriceTooLow
}
Expand All @@ -291,7 +299,7 @@ func (t *transactionService) prepareTransaction(ctx context.Context, request *Tx
To: request.To,
Value: request.Value,
Gas: gasLimit,
GasTipCap: request.GasPrice,
GasTipCap: nil,
GasFeeCap: request.GasPrice,
Data: request.Data,
}), nil
Expand Down

0 comments on commit 4eef0c5

Please sign in to comment.