From 5a524552d3dc130cc0d21762ee786721c33473b2 Mon Sep 17 00:00:00 2001 From: ralph Date: Fri, 10 Jun 2022 15:28:35 +0200 Subject: [PATCH 01/11] feat: use EIP1559 txs --- pkg/transaction/transaction.go | 31 +++++----- pkg/transaction/transaction_test.go | 89 +++++++++++++++++++++-------- 2 files changed, 78 insertions(+), 42 deletions(-) diff --git a/pkg/transaction/transaction.go b/pkg/transaction/transaction.go index b77b0384110..1ba17c10eb5 100644 --- a/pkg/transaction/transaction.go +++ b/pkg/transaction/transaction.go @@ -289,13 +289,13 @@ func (t *transactionService) prepareTransaction(ctx context.Context, request *Tx } return types.NewTx(&types.DynamicFeeTx{ - Nonce: nonce, ChainID: t.chainID, + Nonce: nonce, To: request.To, Value: request.Value, Gas: gasLimit, - GasFeeCap: gasFeeCap, - GasTipCap: gasTipCap, + GasTipCap: gasPrice, + GasFeeCap: gasPrice, Data: request.Data, }), nil } @@ -415,19 +415,14 @@ func (t *transactionService) ResendTransaction(ctx context.Context, txHash commo return err } - gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), storedTransaction.GasTipBoost) - if err != nil { - return err - } - tx := types.NewTx(&types.DynamicFeeTx{ - Nonce: storedTransaction.Nonce, ChainID: t.chainID, + Nonce: storedTransaction.Nonce, To: storedTransaction.To, Value: storedTransaction.Value, Gas: storedTransaction.GasLimit, - GasTipCap: gasTipCap, - GasFeeCap: gasFeeCap, + GasTipCap: storedTransaction.GasPrice, + GasFeeCap: storedTransaction.GasPrice, Data: storedTransaction.Data, }) @@ -455,19 +450,21 @@ func (t *transactionService) CancelTransaction(ctx context.Context, originalTxHa return common.Hash{}, err } - gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), storedTransaction.GasTipBoost) - if err != nil { - return common.Hash{}, err + gasPrice := sctx.GetGasPrice(ctx) + if gasPrice == nil { + gasPrice = new(big.Int).Add(storedTransaction.GasPrice, big.NewInt(1)) + } else if gasPrice.Cmp(storedTransaction.GasPrice) <= 0 { + return common.Hash{}, ErrGasPriceTooLow } signedTx, err := t.signer.SignTx(types.NewTx(&types.DynamicFeeTx{ - Nonce: storedTransaction.Nonce, ChainID: t.chainID, + Nonce: storedTransaction.Nonce, To: &t.sender, Value: big.NewInt(0), Gas: 21000, - GasTipCap: gasTipCap, - GasFeeCap: gasFeeCap, + GasTipCap: gasPrice, + GasFeeCap: gasPrice, Data: []byte{}, }), t.chainID) if err != nil { diff --git a/pkg/transaction/transaction_test.go b/pkg/transaction/transaction_test.go index 6388504a42a..f931548ebbd 100644 --- a/pkg/transaction/transaction_test.go +++ b/pkg/transaction/transaction_test.go @@ -51,13 +51,19 @@ func signerMockForTransaction(t *testing.T, signedTx *types.Transaction, sender t.Fatalf("signing transaction with wrong value. wanted %d, got %d", signedTx.Value(), transaction.Value()) } if chainID.Cmp(signerChainID) != 0 { - t.Fatalf("signing transaction with wrong chainID. wanted %d, got %d", signerChainID, transaction.ChainId()) + t.Fatalf("signing transaction with wrong chainID. wanted %d, got %d", signerChainID, chainID) + } + if transaction.ChainId().Cmp(signedTx.ChainId()) != 0 { + t.Fatalf("signing transaction with wrong chainID. wanted %d, got %d", signedTx.ChainId(), transaction.ChainId()) } if transaction.Gas() != signedTx.Gas() { t.Fatalf("signing transaction with wrong gas. wanted %d, got %d", signedTx.Gas(), transaction.Gas()) } - if transaction.GasPrice().Cmp(signedTx.GasPrice()) != 0 { - t.Fatalf("signing transaction with wrong gasprice. wanted %d, got %d", signedTx.GasPrice(), transaction.GasPrice()) + if transaction.GasFeeCap().Cmp(signedTx.GasFeeCap()) != 0 { + t.Fatalf("signing transaction with wrong gasfeecap. wanted %d, got %d", signedTx.GasFeeCap(), transaction.GasFeeCap()) + } + if transaction.GasTipCap().Cmp(signedTx.GasTipCap()) != 0 { + t.Fatalf("signing transaction with wrong gastipcap. wanted %d, got %d", signedTx.GasTipCap(), transaction.GasTipCap()) } if transaction.Nonce() != signedTx.Nonce() { @@ -88,16 +94,14 @@ func TestTransactionSend(t *testing.T) { chainID := big.NewInt(5) t.Run("send", func(t *testing.T) { - t.Parallel() - signedTx := types.NewTx(&types.DynamicFeeTx{ ChainID: chainID, Nonce: nonce, To: &recipient, Value: value, Gas: estimatedGasLimit, - GasFeeCap: defaultGasFee, - GasTipCap: suggestedGasTip, + GasTipCap: suggestedGasPrice, + GasFeeCap: suggestedGasPrice, Data: txData, }) request := &transaction.TxRequest{ @@ -343,16 +347,14 @@ func TestTransactionSend(t *testing.T) { }) t.Run("send_no_nonce", func(t *testing.T) { - t.Parallel() - signedTx := types.NewTx(&types.DynamicFeeTx{ ChainID: chainID, Nonce: nonce, To: &recipient, Value: value, Gas: estimatedGasLimit, - GasTipCap: suggestedGasTip, - GasFeeCap: defaultGasFee, + GasTipCap: suggestedGasPrice, + GasFeeCap: suggestedGasPrice, Data: txData, }) request := &transaction.TxRequest{ @@ -428,8 +430,8 @@ func TestTransactionSend(t *testing.T) { To: &recipient, Value: value, Gas: estimatedGasLimit, - GasTipCap: suggestedGasTip, - GasFeeCap: defaultGasFee, + GasTipCap: suggestedGasPrice, + GasFeeCap: suggestedGasPrice, Data: txData, }) request := &transaction.TxRequest{ @@ -582,8 +584,8 @@ func TestTransactionResend(t *testing.T) { To: &recipient, Value: value, Gas: gasLimit, - GasTipCap: gasTip, - GasFeeCap: gasFee, + GasTipCap: gasPrice, + GasFeeCap: gasPrice, Data: data, }) @@ -653,8 +655,8 @@ func TestTransactionCancel(t *testing.T) { To: &recipient, Value: value, Gas: gasLimit, - GasFeeCap: gasFee, - GasTipCap: gasTip, + GasTipCap: gasPrice, + GasFeeCap: gasPrice, Data: data, }) err := store.Put(transaction.StoredTransactionKey(signedTx.Hash()), transaction.StoredTransaction{ @@ -670,16 +672,14 @@ func TestTransactionCancel(t *testing.T) { } t.Run("ok", func(t *testing.T) { - t.Parallel() - cancelTx := types.NewTx(&types.DynamicFeeTx{ ChainID: chainID, Nonce: nonce, To: &recipient, Value: big.NewInt(0), Gas: 21000, - GasTipCap: gasTip, - GasFeeCap: gasFee, + GasTipCap: new(big.Int).Add(gasPrice, big.NewInt(1)), + GasFeeCap: new(big.Int).Add(gasPrice, big.NewInt(1)), Data: []byte{}, }) @@ -722,16 +722,14 @@ func TestTransactionCancel(t *testing.T) { t.Parallel() customGasPrice := big.NewInt(5) - customGasFee := new(big.Int).Add(customGasPrice, gasTip) - cancelTx := types.NewTx(&types.DynamicFeeTx{ ChainID: chainID, Nonce: nonce, To: &recipient, Value: big.NewInt(0), Gas: 21000, - GasFeeCap: customGasFee, - GasTipCap: gasTip, + GasTipCap: customGasPrice, + GasFeeCap: customGasPrice, Data: []byte{}, }) @@ -770,4 +768,45 @@ func TestTransactionCancel(t *testing.T) { t.Fatalf("returned wrong hash. wanted %v, got %v", cancelTx.Hash(), cancelTxHash) } }) + + t.Run("too low gas price", func(t *testing.T) { + t.Parallel() + + customGasPrice := big.NewInt(0) + cancelTx := types.NewTx(&types.DynamicFeeTx{ + ChainID: chainID, + Nonce: nonce, + To: &recipient, + Value: big.NewInt(0), + Gas: 21000, + GasTipCap: customGasPrice, + GasFeeCap: customGasPrice, + Data: []byte{}, + }) + + transactionService, err := transaction.NewService(logger, + backendmock.New( + backendmock.WithSendTransactionFunc(func(ctx context.Context, tx *types.Transaction) error { + if tx != cancelTx { + t.Fatal("not sending signed transaction") + } + return nil + }), + ), + signerMockForTransaction(t, cancelTx, recipient, chainID), + store, + chainID, + monitormock.New(), + ) + if err != nil { + t.Fatal(err) + } + defer transactionService.Close() + + ctx := sctx.SetGasPrice(context.Background(), customGasPrice) + _, err = transactionService.CancelTransaction(ctx, signedTx.Hash()) + if !errors.Is(err, transaction.ErrGasPriceTooLow) { + t.Fatalf("returned wrong error. wanted %v, got %v", transaction.ErrGasPriceTooLow, err) + } + }) } From 02ddd22e2a31e5e0ce218fc4a768c7e30b56f15d Mon Sep 17 00:00:00 2001 From: swarmHaseeb Date: Thu, 27 Oct 2022 11:01:21 +0500 Subject: [PATCH 02/11] refactor: rebase and merge --- pkg/transaction/transaction_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/transaction/transaction_test.go b/pkg/transaction/transaction_test.go index f931548ebbd..684c5106104 100644 --- a/pkg/transaction/transaction_test.go +++ b/pkg/transaction/transaction_test.go @@ -51,19 +51,13 @@ func signerMockForTransaction(t *testing.T, signedTx *types.Transaction, sender t.Fatalf("signing transaction with wrong value. wanted %d, got %d", signedTx.Value(), transaction.Value()) } if chainID.Cmp(signerChainID) != 0 { - t.Fatalf("signing transaction with wrong chainID. wanted %d, got %d", signerChainID, chainID) - } - if transaction.ChainId().Cmp(signedTx.ChainId()) != 0 { - t.Fatalf("signing transaction with wrong chainID. wanted %d, got %d", signedTx.ChainId(), transaction.ChainId()) + t.Fatalf("signing transaction with wrong chainID. wanted %d, got %d", signerChainID, transaction.ChainId()) } if transaction.Gas() != signedTx.Gas() { t.Fatalf("signing transaction with wrong gas. wanted %d, got %d", signedTx.Gas(), transaction.Gas()) } - if transaction.GasFeeCap().Cmp(signedTx.GasFeeCap()) != 0 { - t.Fatalf("signing transaction with wrong gasfeecap. wanted %d, got %d", signedTx.GasFeeCap(), transaction.GasFeeCap()) - } - if transaction.GasTipCap().Cmp(signedTx.GasTipCap()) != 0 { - t.Fatalf("signing transaction with wrong gastipcap. wanted %d, got %d", signedTx.GasTipCap(), transaction.GasTipCap()) + if transaction.GasPrice().Cmp(signedTx.GasPrice()) != 0 { + t.Fatalf("signing transaction with wrong gasprice. wanted %d, got %d", signedTx.GasPrice(), transaction.GasPrice()) } if transaction.Nonce() != signedTx.Nonce() { @@ -94,6 +88,8 @@ func TestTransactionSend(t *testing.T) { chainID := big.NewInt(5) t.Run("send", func(t *testing.T) { + t.Parallel() + signedTx := types.NewTx(&types.DynamicFeeTx{ ChainID: chainID, Nonce: nonce, @@ -347,6 +343,8 @@ func TestTransactionSend(t *testing.T) { }) t.Run("send_no_nonce", func(t *testing.T) { + t.Parallel() + signedTx := types.NewTx(&types.DynamicFeeTx{ ChainID: chainID, Nonce: nonce, @@ -672,6 +670,8 @@ func TestTransactionCancel(t *testing.T) { } t.Run("ok", func(t *testing.T) { + t.Parallel() + cancelTx := types.NewTx(&types.DynamicFeeTx{ ChainID: chainID, Nonce: nonce, From a16b0f616b9c8776f80df9641e15a301abc9be7d Mon Sep 17 00:00:00 2001 From: swarmHaseeb Date: Tue, 1 Nov 2022 05:44:40 +0500 Subject: [PATCH 03/11] refactor: updated tx --- pkg/postage/postagecontract/contract.go | 4 +- pkg/postage/postagecontract/contract_test.go | 6 +- pkg/settlement/swap/chequebook/cashout.go | 2 +- pkg/settlement/swap/chequebook/chequebook.go | 2 +- pkg/settlement/swap/chequebook/factory.go | 2 +- pkg/settlement/swap/erc20/erc20.go | 2 +- .../redistribution/redistribution.go | 7 +- .../redistribution/redistribution_test.go | 10 +-- pkg/storageincentives/staking/contract.go | 2 +- .../staking/contract_test.go | 12 ++-- pkg/transaction/mock/transaction.go | 8 +-- pkg/transaction/transaction.go | 66 ++++++------------- 12 files changed, 48 insertions(+), 75 deletions(-) diff --git a/pkg/postage/postagecontract/contract.go b/pkg/postage/postagecontract/contract.go index 37babc52b4d..c1547ac3385 100644 --- a/pkg/postage/postagecontract/contract.go +++ b/pkg/postage/postagecontract/contract.go @@ -96,7 +96,7 @@ func (c *postageContract) sendApproveTransaction(ctx context.Context, amount *bi GasLimit: 65000, Value: big.NewInt(0), Description: approveDescription, - }, transaction.DefaultTipBoostPercent) + }, 0) if err != nil { return nil, err } @@ -123,7 +123,7 @@ func (c *postageContract) sendTransaction(ctx context.Context, callData []byte, Description: desc, } - txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) + txHash, err := c.transactionService.Send(ctx, request, 0) if err != nil { return nil, err } diff --git a/pkg/postage/postagecontract/contract_test.go b/pkg/postage/postagecontract/contract_test.go index be80771eafd..e0d87cecd41 100644 --- a/pkg/postage/postagecontract/contract_test.go +++ b/pkg/postage/postagecontract/contract_test.go @@ -54,7 +54,7 @@ func TestCreateBatch(t *testing.T) { postageStampAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return txHashApprove, nil } else if *request.To == postageStampAddress { @@ -244,7 +244,7 @@ func TestTopUpBatch(t *testing.T) { postageStampAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return txHashApprove, nil } else if *request.To == postageStampAddress { @@ -400,7 +400,7 @@ func TestDiluteBatch(t *testing.T) { postageStampAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == postageStampAddress { if !bytes.Equal(expectedCallData[:64], request.Data[:64]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) diff --git a/pkg/settlement/swap/chequebook/cashout.go b/pkg/settlement/swap/chequebook/cashout.go index 157af01ad56..a7d00d87f40 100644 --- a/pkg/settlement/swap/chequebook/cashout.go +++ b/pkg/settlement/swap/chequebook/cashout.go @@ -149,7 +149,7 @@ func (s *cashoutService) CashCheque(ctx context.Context, chequebook, recipient c Description: "cheque cashout", } - txHash, err := s.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) + txHash, err := s.transactionService.Send(ctx, request, 0) if err != nil { return common.Hash{}, err } diff --git a/pkg/settlement/swap/chequebook/chequebook.go b/pkg/settlement/swap/chequebook/chequebook.go index 347f6a7bfe7..845f9aac201 100644 --- a/pkg/settlement/swap/chequebook/chequebook.go +++ b/pkg/settlement/swap/chequebook/chequebook.go @@ -334,7 +334,7 @@ func (s *service) Withdraw(ctx context.Context, amount *big.Int) (hash common.Ha Description: fmt.Sprintf("chequebook withdrawal of %d BZZ", amount), } - txHash, err := s.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) + txHash, err := s.transactionService.Send(ctx, request, 0) if err != nil { return common.Hash{}, err } diff --git a/pkg/settlement/swap/chequebook/factory.go b/pkg/settlement/swap/chequebook/factory.go index 6b4cc106b6f..ad890b95491 100644 --- a/pkg/settlement/swap/chequebook/factory.go +++ b/pkg/settlement/swap/chequebook/factory.go @@ -87,7 +87,7 @@ func (c *factory) Deploy(ctx context.Context, issuer common.Address, defaultHard Description: "chequebook deployment", } - txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) + txHash, err := c.transactionService.Send(ctx, request, 0) if err != nil { return common.Hash{}, err } diff --git a/pkg/settlement/swap/erc20/erc20.go b/pkg/settlement/swap/erc20/erc20.go index 677ac35b8fb..4866350a81c 100644 --- a/pkg/settlement/swap/erc20/erc20.go +++ b/pkg/settlement/swap/erc20/erc20.go @@ -83,7 +83,7 @@ func (c *erc20Service) Transfer(ctx context.Context, address common.Address, val Description: "token transfer", } - txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) + txHash, err := c.transactionService.Send(ctx, request, 0) if err != nil { return common.Hash{}, err } diff --git a/pkg/storageincentives/redistribution/redistribution.go b/pkg/storageincentives/redistribution/redistribution.go index 560c3534f2c..15877c69d7e 100644 --- a/pkg/storageincentives/redistribution/redistribution.go +++ b/pkg/storageincentives/redistribution/redistribution.go @@ -110,7 +110,7 @@ func (c *contract) Claim(ctx context.Context) error { Value: big.NewInt(0), Description: "claim win transaction", } - err = c.sendAndWait(ctx, request, 50) + err = c.sendAndWait(ctx, request, 0) if err != nil { return fmt.Errorf("claim: %w", err) } @@ -132,7 +132,7 @@ func (c *contract) Commit(ctx context.Context, obfusHash []byte) error { Value: big.NewInt(0), Description: "commit transaction", } - err = c.sendAndWait(ctx, request, 50) + err = c.sendAndWait(ctx, request, 0) if err != nil { return fmt.Errorf("commit: obfusHash %v overlay %v: %w", common.BytesToHash(obfusHash), common.BytesToHash(c.overlay.Bytes()), err) } @@ -182,7 +182,8 @@ func (c *contract) ReserveSalt(ctx context.Context) ([]byte, error) { return salt[:], nil } -func (c *contract) sendAndWait(ctx context.Context, request *transaction.TxRequest, boostPercent int) error { +// sendAndWait simulates a transaction based on tx request and waits until the tx is either mined or ctx is cancelled. +func (c *contract) sendAndWait(ctx context.Context, request *transaction.TxRequest, boostPercent uint64) error { txHash, err := c.txService.Send(ctx, request, boostPercent) if err != nil { return err diff --git a/pkg/storageincentives/redistribution/redistribution_test.go b/pkg/storageincentives/redistribution/redistribution_test.go index 06de1f27550..874abae4c02 100644 --- a/pkg/storageincentives/redistribution/redistribution_test.go +++ b/pkg/storageincentives/redistribution/redistribution_test.go @@ -138,7 +138,7 @@ func TestRedistribution(t *testing.T) { } contract := redistribution.New(owner, log.Noop, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == redistributionAddress { if !bytes.Equal(expectedCallData[:32], request.Data[:32]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) @@ -173,7 +173,7 @@ func TestRedistribution(t *testing.T) { } contract := redistribution.New(owner, log.Noop, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == redistributionAddress { if !bytes.Equal(expectedCallData[:32], request.Data[:32]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) @@ -210,7 +210,7 @@ func TestRedistribution(t *testing.T) { } contract := redistribution.New(owner, log.Noop, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == redistributionAddress { if !bytes.Equal(expectedCallData[:32], request.Data[:32]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) @@ -249,7 +249,7 @@ func TestRedistribution(t *testing.T) { } contract := redistribution.New(owner, log.Noop, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == redistributionAddress { if !bytes.Equal(expectedCallData[:32], request.Data[:32]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) @@ -331,7 +331,7 @@ func TestRedistribution(t *testing.T) { } contract := redistribution.New(owner, log.Noop, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == redistributionAddress { if !bytes.Equal(expectedCallData[:], request.Data[:]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) diff --git a/pkg/storageincentives/staking/contract.go b/pkg/storageincentives/staking/contract.go index b22fc8d6f3f..d0df5c44932 100644 --- a/pkg/storageincentives/staking/contract.go +++ b/pkg/storageincentives/staking/contract.go @@ -106,7 +106,7 @@ func (s *contract) sendTransaction(ctx context.Context, callData []byte, desc st Description: desc, } - txHash, err := s.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) + txHash, err := s.transactionService.Send(ctx, request, 0) if err != nil { return nil, err } diff --git a/pkg/storageincentives/staking/contract_test.go b/pkg/storageincentives/staking/contract_test.go index 9e5f73bb409..39b017e8611 100644 --- a/pkg/storageincentives/staking/contract_test.go +++ b/pkg/storageincentives/staking/contract_test.go @@ -45,7 +45,7 @@ func TestDepositStake(t *testing.T) { contract := staking2.New(addr, owner, stakingAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return txHashApprove, nil } @@ -99,7 +99,7 @@ func TestDepositStake(t *testing.T) { contract := staking2.New(addr, owner, stakingAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return txHashApprove, nil } @@ -231,7 +231,7 @@ func TestDepositStake(t *testing.T) { contract := staking2.New(addr, owner, stakingAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return common.Hash{}, errors.New("send transaction failed") } @@ -267,7 +267,7 @@ func TestDepositStake(t *testing.T) { contract := staking2.New(addr, owner, stakingAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { if !bytes.Equal(expectedCallData[:], request.Data[:]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) @@ -308,7 +308,7 @@ func TestDepositStake(t *testing.T) { contract := staking2.New(addr, owner, stakingAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return txHashApprove, nil } @@ -362,7 +362,7 @@ func TestDepositStake(t *testing.T) { contract := staking2.New(addr, owner, stakingAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return txHashApprove, nil } diff --git a/pkg/transaction/mock/transaction.go b/pkg/transaction/mock/transaction.go index 5d1563de360..ef2b317ccca 100644 --- a/pkg/transaction/mock/transaction.go +++ b/pkg/transaction/mock/transaction.go @@ -18,7 +18,7 @@ import ( ) type transactionServiceMock struct { - send func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) + send func(ctx context.Context, request *transaction.TxRequest, boostPercentage uint64) (txHash common.Hash, err error) waitForReceipt func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) watchSentTransaction func(txHash common.Hash) (chan types.Receipt, chan error, error) call func(ctx context.Context, request *transaction.TxRequest) (result []byte, err error) @@ -28,7 +28,7 @@ type transactionServiceMock struct { cancelTransaction func(ctx context.Context, originalTxHash common.Hash) (common.Hash, error) } -func (m *transactionServiceMock) Send(ctx context.Context, request *transaction.TxRequest, boostPercent int) (txHash common.Hash, err error) { +func (m *transactionServiceMock) Send(ctx context.Context, request *transaction.TxRequest, boostPercent uint64) (txHash common.Hash, err error) { if m.send != nil { return m.send(ctx, request, boostPercent) } @@ -97,7 +97,7 @@ type optionFunc func(*transactionServiceMock) func (f optionFunc) apply(r *transactionServiceMock) { f(r) } -func WithSendFunc(f func(context.Context, *transaction.TxRequest, int) (txHash common.Hash, err error)) Option { +func WithSendFunc(f func(ctx context.Context, request *transaction.TxRequest, boostPercentage uint64) (txHash common.Hash, err error)) Option { return optionFunc(func(s *transactionServiceMock) { s.send = f }) @@ -203,7 +203,7 @@ func WithABICall(abi *abi.ABI, to common.Address, result []byte, method string, func WithABISend(abi *abi.ABI, txHash common.Hash, expectedAddress common.Address, expectedValue *big.Int, method string, params ...interface{}) Option { return optionFunc(func(s *transactionServiceMock) { - s.send = func(ctx context.Context, request *transaction.TxRequest, boost int) (common.Hash, error) { + s.send = func(ctx context.Context, request *transaction.TxRequest, _ uint64) (common.Hash, error) { data, err := abi.Pack(method, params...) if err != nil { return common.Hash{}, err diff --git a/pkg/transaction/transaction.go b/pkg/transaction/transaction.go index 1ba17c10eb5..2cee717c8ba 100644 --- a/pkg/transaction/transaction.go +++ b/pkg/transaction/transaction.go @@ -68,8 +68,8 @@ type StoredTransaction struct { // limit and nonce management. type Service interface { io.Closer - // Send creates a transaction based on the request (with gasprice increased by provided percentage) and sends it. - Send(ctx context.Context, request *TxRequest, tipCapBoostPercent int) (txHash common.Hash, err error) + // Send creates a transaction based on the request and sends it (with optional gasprice increased by provided percentage). + Send(ctx context.Context, request *TxRequest, boostPercent uint64) (txHash common.Hash, err error) // Call simulate a transaction based on the request. Call(ctx context.Context, request *TxRequest) (result []byte, err error) // WaitForReceipt waits until either the transaction with the given hash has been mined or the context is cancelled. @@ -137,8 +137,8 @@ func NewService(logger log.Logger, backend Backend, signer crypto.Signer, store return t, nil } -// Send creates and signs a transaction based on the request and sends it. -func (t *transactionService) Send(ctx context.Context, request *TxRequest, tipCapBoostPercent int) (txHash common.Hash, err error) { +// Send creates and signs a transaction based on the request and sends it (with optional gasprice increased by provided percentage). +func (t *transactionService) Send(ctx context.Context, request *TxRequest, boostPercent uint64) (txHash common.Hash, err error) { loggerV1 := t.logger.V(1).Register() t.lock.Lock() @@ -149,7 +149,7 @@ func (t *transactionService) Send(ctx context.Context, request *TxRequest, tipCa return common.Hash{}, err } - tx, err := t.prepareTransaction(ctx, request, nonce, tipCapBoostPercent) + tx, err := t.prepareTransaction(ctx, request, nonce, boostPercent) if err != nil { return common.Hash{}, err } @@ -253,7 +253,7 @@ func (t *transactionService) StoredTransaction(txHash common.Hash) (*StoredTrans } // prepareTransaction creates a signable transaction based on a request. -func (t *transactionService) prepareTransaction(ctx context.Context, request *TxRequest, nonce uint64, tipBoostPercent int) (tx *types.Transaction, err error) { +func (t *transactionService) prepareTransaction(ctx context.Context, request *TxRequest, nonce uint64, boostPercent uint64) (tx *types.Transaction, err error) { var gasLimit uint64 if request.GasLimit == 0 { gasLimit, err = t.backend.EstimateGas(ctx, ethereum.CallMsg{ @@ -271,21 +271,17 @@ func (t *transactionService) prepareTransaction(ctx context.Context, request *Tx gasLimit = request.GasLimit } - /* - Transactions are EIP 1559 dynamic transactions where there are three fee related fields: - 1. base fee is the price that will be burned as part of the transaction. - 2. max fee is the max price we are willing to spend as gas price. - 3. max priority fee is max price want to give to the miner to prioritize the transaction. - as an example: - if base fee is 15, max fee is 20, and max priority is 3, gas price will be 15 + 3 = 18 - if base is 15, max fee is 20, and max priority fee is 10, - gas price will be 15 + 10 = 25, but since 25 > 20, gas price is 20. - notice that gas price does not exceed 20 as defined by max fee. - */ - - gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, request.GasPrice, tipBoostPercent) - if err != nil { - return nil, err + if request.GasPrice == nil { + request.GasPrice, err = t.backend.SuggestGasPrice(ctx) + 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 request.GasPrice.Cmp(minGasPrice) < 0 { + return nil, ErrGasPriceTooLow } return types.NewTx(&types.DynamicFeeTx{ @@ -294,36 +290,12 @@ func (t *transactionService) prepareTransaction(ctx context.Context, request *Tx To: request.To, Value: request.Value, Gas: gasLimit, - GasTipCap: gasPrice, - GasFeeCap: gasPrice, + GasTipCap: request.GasPrice, + GasFeeCap: request.GasPrice, Data: request.Data, }), nil } -func (t *transactionService) suggestedFeeAndTip(ctx context.Context, gasPrice *big.Int, tipBoostPercent int) (*big.Int, *big.Int, error) { - var err error - - if gasPrice == nil { - gasPrice, err = t.backend.SuggestGasPrice(ctx) - if err != nil { - return nil, nil, err - } - } - - gasTipCap, err := t.backend.SuggestGasTipCap(ctx) - if err != nil { - return nil, nil, err - } - - gasTipCap = new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(tipBoostPercent)+100), gasTipCap), big.NewInt(100)) - gasFeeCap := new(big.Int).Add(gasTipCap, gasPrice) - - t.logger.Debug("prepare transaction", "gas_price", gasPrice, "gas_max_fee", gasFeeCap, "gas_max_tip", gasTipCap) - - return gasFeeCap, gasTipCap, nil - -} - func (t *transactionService) nonceKey() string { return fmt.Sprintf("%s%x", noncePrefix, t.sender) } From ba8ccefddc0953c7b99c4b9f70fa739a546ac7aa Mon Sep 17 00:00:00 2001 From: swarmHaseeb Date: Mon, 7 Nov 2022 13:18:02 +0500 Subject: [PATCH 04/11] refactor: transactions --- pkg/api/transaction.go | 6 ++++++ pkg/api/transaction_test.go | 30 +++++++++++++++++++++--------- pkg/transaction/transaction.go | 16 ++++++++++++---- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/pkg/api/transaction.go b/pkg/api/transaction.go index cabeee7157c..ca1bf070af1 100644 --- a/pkg/api/transaction.go +++ b/pkg/api/transaction.go @@ -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"` @@ -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, @@ -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, diff --git a/pkg/api/transaction_test.go b/pkg/api/transaction_test.go index ddb1cdc5f82..c187dd2185a 100644 --- a/pkg/api/transaction_test.go +++ b/pkg/api/transaction_test.go @@ -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, @@ -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, @@ -121,12 +125,14 @@ 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: { @@ -134,6 +140,8 @@ func TestTransactionList(t *testing.T) { 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, @@ -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), }, }, }), diff --git a/pkg/transaction/transaction.go b/pkg/transaction/transaction.go index 2cee717c8ba..4be46cc3cd5 100644 --- a/pkg/transaction/transaction.go +++ b/pkg/transaction/transaction.go @@ -48,6 +48,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 } @@ -58,6 +60,8 @@ type StoredTransaction struct { GasPrice *big.Int // used gas price GasTipBoost int // percentage used to boost the priority fee (eg: tip) 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 @@ -179,6 +183,8 @@ func (t *transactionService) Send(ctx context.Context, request *TxRequest, boost GasPrice: signedTx.GasPrice(), GasTipBoost: tipCapBoostPercent, GasLimit: signedTx.Gas(), + GasTipCap: signedTx.GasTipCap(), + GasFeeCap: signedTx.GasFeeCap(), Value: signedTx.Value(), Nonce: signedTx.Nonce(), Created: time.Now().Unix(), @@ -276,10 +282,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 } @@ -290,7 +298,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 From 6640343b00e935fc07f45f50b0c0cb5c00215056 Mon Sep 17 00:00:00 2001 From: swarmHaseeb Date: Mon, 7 Nov 2022 13:27:40 +0500 Subject: [PATCH 05/11] refactor: docs --- openapi/SwarmCommon.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/openapi/SwarmCommon.yaml b/openapi/SwarmCommon.yaml index a70cfa3d9e0..dc3723e0c25 100644 --- a/openapi/SwarmCommon.yaml +++ b/openapi/SwarmCommon.yaml @@ -655,6 +655,10 @@ components: $ref: "#/components/schemas/BigInt" gasLimit: type: integer + gasTipCap: + $ref: "#/components/schemas/BigInt" + gasFeeCap: + $ref: "#/components/schemas/BigInt" data: type: string created: From df3aca96278ea5dde0219faae6e0ed7341795e99 Mon Sep 17 00:00:00 2001 From: swarmHaseeb Date: Wed, 9 Nov 2022 05:38:51 +0500 Subject: [PATCH 06/11] fix: rebase --- pkg/postage/postagecontract/contract.go | 4 +- pkg/postage/postagecontract/contract_test.go | 6 +- pkg/settlement/swap/chequebook/cashout.go | 2 +- pkg/settlement/swap/chequebook/chequebook.go | 2 +- pkg/settlement/swap/chequebook/factory.go | 2 +- pkg/settlement/swap/erc20/erc20.go | 2 +- .../redistribution/redistribution.go | 7 +- .../redistribution/redistribution_test.go | 10 +- pkg/storageincentives/staking/contract.go | 2 +- .../staking/contract_test.go | 12 +-- pkg/transaction/mock/transaction.go | 8 +- pkg/transaction/transaction.go | 95 ++++++++++++------- pkg/transaction/transaction_test.go | 71 ++++---------- 13 files changed, 105 insertions(+), 118 deletions(-) diff --git a/pkg/postage/postagecontract/contract.go b/pkg/postage/postagecontract/contract.go index c1547ac3385..37babc52b4d 100644 --- a/pkg/postage/postagecontract/contract.go +++ b/pkg/postage/postagecontract/contract.go @@ -96,7 +96,7 @@ func (c *postageContract) sendApproveTransaction(ctx context.Context, amount *bi GasLimit: 65000, Value: big.NewInt(0), Description: approveDescription, - }, 0) + }, transaction.DefaultTipBoostPercent) if err != nil { return nil, err } @@ -123,7 +123,7 @@ func (c *postageContract) sendTransaction(ctx context.Context, callData []byte, Description: desc, } - txHash, err := c.transactionService.Send(ctx, request, 0) + txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) if err != nil { return nil, err } diff --git a/pkg/postage/postagecontract/contract_test.go b/pkg/postage/postagecontract/contract_test.go index e0d87cecd41..be80771eafd 100644 --- a/pkg/postage/postagecontract/contract_test.go +++ b/pkg/postage/postagecontract/contract_test.go @@ -54,7 +54,7 @@ func TestCreateBatch(t *testing.T) { postageStampAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return txHashApprove, nil } else if *request.To == postageStampAddress { @@ -244,7 +244,7 @@ func TestTopUpBatch(t *testing.T) { postageStampAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return txHashApprove, nil } else if *request.To == postageStampAddress { @@ -400,7 +400,7 @@ func TestDiluteBatch(t *testing.T) { postageStampAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { if *request.To == postageStampAddress { if !bytes.Equal(expectedCallData[:64], request.Data[:64]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) diff --git a/pkg/settlement/swap/chequebook/cashout.go b/pkg/settlement/swap/chequebook/cashout.go index a7d00d87f40..157af01ad56 100644 --- a/pkg/settlement/swap/chequebook/cashout.go +++ b/pkg/settlement/swap/chequebook/cashout.go @@ -149,7 +149,7 @@ func (s *cashoutService) CashCheque(ctx context.Context, chequebook, recipient c Description: "cheque cashout", } - txHash, err := s.transactionService.Send(ctx, request, 0) + txHash, err := s.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) if err != nil { return common.Hash{}, err } diff --git a/pkg/settlement/swap/chequebook/chequebook.go b/pkg/settlement/swap/chequebook/chequebook.go index 845f9aac201..347f6a7bfe7 100644 --- a/pkg/settlement/swap/chequebook/chequebook.go +++ b/pkg/settlement/swap/chequebook/chequebook.go @@ -334,7 +334,7 @@ func (s *service) Withdraw(ctx context.Context, amount *big.Int) (hash common.Ha Description: fmt.Sprintf("chequebook withdrawal of %d BZZ", amount), } - txHash, err := s.transactionService.Send(ctx, request, 0) + txHash, err := s.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) if err != nil { return common.Hash{}, err } diff --git a/pkg/settlement/swap/chequebook/factory.go b/pkg/settlement/swap/chequebook/factory.go index ad890b95491..6b4cc106b6f 100644 --- a/pkg/settlement/swap/chequebook/factory.go +++ b/pkg/settlement/swap/chequebook/factory.go @@ -87,7 +87,7 @@ func (c *factory) Deploy(ctx context.Context, issuer common.Address, defaultHard Description: "chequebook deployment", } - txHash, err := c.transactionService.Send(ctx, request, 0) + txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) if err != nil { return common.Hash{}, err } diff --git a/pkg/settlement/swap/erc20/erc20.go b/pkg/settlement/swap/erc20/erc20.go index 4866350a81c..677ac35b8fb 100644 --- a/pkg/settlement/swap/erc20/erc20.go +++ b/pkg/settlement/swap/erc20/erc20.go @@ -83,7 +83,7 @@ func (c *erc20Service) Transfer(ctx context.Context, address common.Address, val Description: "token transfer", } - txHash, err := c.transactionService.Send(ctx, request, 0) + txHash, err := c.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) if err != nil { return common.Hash{}, err } diff --git a/pkg/storageincentives/redistribution/redistribution.go b/pkg/storageincentives/redistribution/redistribution.go index 15877c69d7e..560c3534f2c 100644 --- a/pkg/storageincentives/redistribution/redistribution.go +++ b/pkg/storageincentives/redistribution/redistribution.go @@ -110,7 +110,7 @@ func (c *contract) Claim(ctx context.Context) error { Value: big.NewInt(0), Description: "claim win transaction", } - err = c.sendAndWait(ctx, request, 0) + err = c.sendAndWait(ctx, request, 50) if err != nil { return fmt.Errorf("claim: %w", err) } @@ -132,7 +132,7 @@ func (c *contract) Commit(ctx context.Context, obfusHash []byte) error { Value: big.NewInt(0), Description: "commit transaction", } - err = c.sendAndWait(ctx, request, 0) + err = c.sendAndWait(ctx, request, 50) if err != nil { return fmt.Errorf("commit: obfusHash %v overlay %v: %w", common.BytesToHash(obfusHash), common.BytesToHash(c.overlay.Bytes()), err) } @@ -182,8 +182,7 @@ func (c *contract) ReserveSalt(ctx context.Context) ([]byte, error) { return salt[:], nil } -// sendAndWait simulates a transaction based on tx request and waits until the tx is either mined or ctx is cancelled. -func (c *contract) sendAndWait(ctx context.Context, request *transaction.TxRequest, boostPercent uint64) error { +func (c *contract) sendAndWait(ctx context.Context, request *transaction.TxRequest, boostPercent int) error { txHash, err := c.txService.Send(ctx, request, boostPercent) if err != nil { return err diff --git a/pkg/storageincentives/redistribution/redistribution_test.go b/pkg/storageincentives/redistribution/redistribution_test.go index 874abae4c02..06de1f27550 100644 --- a/pkg/storageincentives/redistribution/redistribution_test.go +++ b/pkg/storageincentives/redistribution/redistribution_test.go @@ -138,7 +138,7 @@ func TestRedistribution(t *testing.T) { } contract := redistribution.New(owner, log.Noop, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { if *request.To == redistributionAddress { if !bytes.Equal(expectedCallData[:32], request.Data[:32]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) @@ -173,7 +173,7 @@ func TestRedistribution(t *testing.T) { } contract := redistribution.New(owner, log.Noop, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { if *request.To == redistributionAddress { if !bytes.Equal(expectedCallData[:32], request.Data[:32]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) @@ -210,7 +210,7 @@ func TestRedistribution(t *testing.T) { } contract := redistribution.New(owner, log.Noop, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { if *request.To == redistributionAddress { if !bytes.Equal(expectedCallData[:32], request.Data[:32]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) @@ -249,7 +249,7 @@ func TestRedistribution(t *testing.T) { } contract := redistribution.New(owner, log.Noop, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ int) (txHash common.Hash, err error) { if *request.To == redistributionAddress { if !bytes.Equal(expectedCallData[:32], request.Data[:32]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) @@ -331,7 +331,7 @@ func TestRedistribution(t *testing.T) { } contract := redistribution.New(owner, log.Noop, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { if *request.To == redistributionAddress { if !bytes.Equal(expectedCallData[:], request.Data[:]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) diff --git a/pkg/storageincentives/staking/contract.go b/pkg/storageincentives/staking/contract.go index d0df5c44932..b22fc8d6f3f 100644 --- a/pkg/storageincentives/staking/contract.go +++ b/pkg/storageincentives/staking/contract.go @@ -106,7 +106,7 @@ func (s *contract) sendTransaction(ctx context.Context, callData []byte, desc st Description: desc, } - txHash, err := s.transactionService.Send(ctx, request, 0) + txHash, err := s.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent) if err != nil { return nil, err } diff --git a/pkg/storageincentives/staking/contract_test.go b/pkg/storageincentives/staking/contract_test.go index 39b017e8611..9e5f73bb409 100644 --- a/pkg/storageincentives/staking/contract_test.go +++ b/pkg/storageincentives/staking/contract_test.go @@ -45,7 +45,7 @@ func TestDepositStake(t *testing.T) { contract := staking2.New(addr, owner, stakingAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return txHashApprove, nil } @@ -99,7 +99,7 @@ func TestDepositStake(t *testing.T) { contract := staking2.New(addr, owner, stakingAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return txHashApprove, nil } @@ -231,7 +231,7 @@ func TestDepositStake(t *testing.T) { contract := staking2.New(addr, owner, stakingAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return common.Hash{}, errors.New("send transaction failed") } @@ -267,7 +267,7 @@ func TestDepositStake(t *testing.T) { contract := staking2.New(addr, owner, stakingAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { if !bytes.Equal(expectedCallData[:], request.Data[:]) { return common.Hash{}, fmt.Errorf("got wrong call data. wanted %x, got %x", expectedCallData, request.Data) @@ -308,7 +308,7 @@ func TestDepositStake(t *testing.T) { contract := staking2.New(addr, owner, stakingAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return txHashApprove, nil } @@ -362,7 +362,7 @@ func TestDepositStake(t *testing.T) { contract := staking2.New(addr, owner, stakingAddress, bzzTokenAddress, transactionMock.New( - transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, _ uint64) (txHash common.Hash, err error) { + transactionMock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) { if *request.To == bzzTokenAddress { return txHashApprove, nil } diff --git a/pkg/transaction/mock/transaction.go b/pkg/transaction/mock/transaction.go index ef2b317ccca..5d1563de360 100644 --- a/pkg/transaction/mock/transaction.go +++ b/pkg/transaction/mock/transaction.go @@ -18,7 +18,7 @@ import ( ) type transactionServiceMock struct { - send func(ctx context.Context, request *transaction.TxRequest, boostPercentage uint64) (txHash common.Hash, err error) + send func(ctx context.Context, request *transaction.TxRequest, boost int) (txHash common.Hash, err error) waitForReceipt func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) watchSentTransaction func(txHash common.Hash) (chan types.Receipt, chan error, error) call func(ctx context.Context, request *transaction.TxRequest) (result []byte, err error) @@ -28,7 +28,7 @@ type transactionServiceMock struct { cancelTransaction func(ctx context.Context, originalTxHash common.Hash) (common.Hash, error) } -func (m *transactionServiceMock) Send(ctx context.Context, request *transaction.TxRequest, boostPercent uint64) (txHash common.Hash, err error) { +func (m *transactionServiceMock) Send(ctx context.Context, request *transaction.TxRequest, boostPercent int) (txHash common.Hash, err error) { if m.send != nil { return m.send(ctx, request, boostPercent) } @@ -97,7 +97,7 @@ type optionFunc func(*transactionServiceMock) func (f optionFunc) apply(r *transactionServiceMock) { f(r) } -func WithSendFunc(f func(ctx context.Context, request *transaction.TxRequest, boostPercentage uint64) (txHash common.Hash, err error)) Option { +func WithSendFunc(f func(context.Context, *transaction.TxRequest, int) (txHash common.Hash, err error)) Option { return optionFunc(func(s *transactionServiceMock) { s.send = f }) @@ -203,7 +203,7 @@ func WithABICall(abi *abi.ABI, to common.Address, result []byte, method string, func WithABISend(abi *abi.ABI, txHash common.Hash, expectedAddress common.Address, expectedValue *big.Int, method string, params ...interface{}) Option { return optionFunc(func(s *transactionServiceMock) { - s.send = func(ctx context.Context, request *transaction.TxRequest, _ uint64) (common.Hash, error) { + s.send = func(ctx context.Context, request *transaction.TxRequest, boost int) (common.Hash, error) { data, err := abi.Pack(method, params...) if err != nil { return common.Hash{}, err diff --git a/pkg/transaction/transaction.go b/pkg/transaction/transaction.go index 4be46cc3cd5..0ac84fb49d0 100644 --- a/pkg/transaction/transaction.go +++ b/pkg/transaction/transaction.go @@ -58,7 +58,6 @@ 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 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 @@ -72,8 +71,8 @@ type StoredTransaction struct { // limit and nonce management. type Service interface { io.Closer - // Send creates a transaction based on the request and sends it (with optional gasprice increased by provided percentage). - Send(ctx context.Context, request *TxRequest, boostPercent uint64) (txHash common.Hash, err error) + // Send creates a transaction based on the request (with gasprice increased by provided percentage) and sends it. + Send(ctx context.Context, request *TxRequest, tipCapBoostPercent int) (txHash common.Hash, err error) // Call simulate a transaction based on the request. Call(ctx context.Context, request *TxRequest) (result []byte, err error) // WaitForReceipt waits until either the transaction with the given hash has been mined or the context is cancelled. @@ -141,8 +140,8 @@ func NewService(logger log.Logger, backend Backend, signer crypto.Signer, store return t, nil } -// Send creates and signs a transaction based on the request and sends it (with optional gasprice increased by provided percentage). -func (t *transactionService) Send(ctx context.Context, request *TxRequest, boostPercent uint64) (txHash common.Hash, err error) { +// Send creates and signs a transaction based on the request and sends it. +func (t *transactionService) Send(ctx context.Context, request *TxRequest, tipCapBoostPercent int) (txHash common.Hash, err error) { loggerV1 := t.logger.V(1).Register() t.lock.Lock() @@ -153,7 +152,7 @@ func (t *transactionService) Send(ctx context.Context, request *TxRequest, boost return common.Hash{}, err } - tx, err := t.prepareTransaction(ctx, request, nonce, boostPercent) + tx, err := t.prepareTransaction(ctx, request, nonce, tipCapBoostPercent) if err != nil { return common.Hash{}, err } @@ -181,7 +180,6 @@ func (t *transactionService) Send(ctx context.Context, request *TxRequest, boost To: signedTx.To(), Data: signedTx.Data(), GasPrice: signedTx.GasPrice(), - GasTipBoost: tipCapBoostPercent, GasLimit: signedTx.Gas(), GasTipCap: signedTx.GasTipCap(), GasFeeCap: signedTx.GasFeeCap(), @@ -259,7 +257,7 @@ func (t *transactionService) StoredTransaction(txHash common.Hash) (*StoredTrans } // prepareTransaction creates a signable transaction based on a request. -func (t *transactionService) prepareTransaction(ctx context.Context, request *TxRequest, nonce uint64, boostPercent uint64) (tx *types.Transaction, err error) { +func (t *transactionService) prepareTransaction(ctx context.Context, request *TxRequest, nonce uint64, tipBoostPercent int) (tx *types.Transaction, err error) { var gasLimit uint64 if request.GasLimit == 0 { gasLimit, err = t.backend.EstimateGas(ctx, ethereum.CallMsg{ @@ -277,33 +275,59 @@ func (t *transactionService) prepareTransaction(ctx context.Context, request *Tx gasLimit = request.GasLimit } - if request.GasPrice == nil { - request.GasPrice, err = t.backend.SuggestGasPrice(ctx) - if err != nil { - return nil, err - } - } + /* + Transactions are EIP 1559 dynamic transactions where there are three fee related fields: + 1. base fee is the price that will be burned as part of the transaction. + 2. max fee is the max price we are willing to spend as gas price. + 3. max priority fee is max price want to give to the miner to prioritize the transaction. + as an example: + if base fee is 15, max fee is 20, and max priority is 3, gas price will be 15 + 3 = 18 + if base is 15, max fee is 20, and max priority fee is 10, + gas price will be 15 + 10 = 25, but since 25 > 20, gas price is 20. + notice that gas price does not exceed 20 as defined by max fee. + */ - 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 + gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, request.GasPrice, tipBoostPercent) + if err != nil { + return nil, err } return types.NewTx(&types.DynamicFeeTx{ - ChainID: t.chainID, Nonce: nonce, + ChainID: t.chainID, To: request.To, Value: request.Value, Gas: gasLimit, - GasTipCap: nil, - GasFeeCap: request.GasPrice, + GasFeeCap: gasFeeCap, + GasTipCap: gasTipCap, Data: request.Data, }), nil } +func (t *transactionService) suggestedFeeAndTip(ctx context.Context, gasPrice *big.Int, tipBoostPercent int) (*big.Int, *big.Int, error) { + var err error + + if gasPrice == nil { + gasPrice, err = t.backend.SuggestGasPrice(ctx) + if err != nil { + return nil, nil, err + } + } + + gasTipCap, err := t.backend.SuggestGasTipCap(ctx) + if err != nil { + return nil, nil, err + } + + gasTipCap = new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(tipBoostPercent)+100), gasTipCap), big.NewInt(100)) + gasFeeCap := new(big.Int).Add(gasTipCap, gasPrice) + + t.logger.Debug("prepare transaction", "gas_price", gasPrice, "gas_max_fee", gasFeeCap, "gas_max_tip", gasTipCap) + + return gasFeeCap, gasTipCap, nil + +} + func (t *transactionService) nonceKey() string { return fmt.Sprintf("%s%x", noncePrefix, t.sender) } @@ -395,14 +419,19 @@ func (t *transactionService) ResendTransaction(ctx context.Context, txHash commo return err } + gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), storedTransaction.GasTipCap) + if err != nil { + return err + } + tx := types.NewTx(&types.DynamicFeeTx{ - ChainID: t.chainID, Nonce: storedTransaction.Nonce, + ChainID: t.chainID, To: storedTransaction.To, Value: storedTransaction.Value, Gas: storedTransaction.GasLimit, - GasTipCap: storedTransaction.GasPrice, - GasFeeCap: storedTransaction.GasPrice, + GasTipCap: gasTipCap, + GasFeeCap: gasFeeCap, Data: storedTransaction.Data, }) @@ -430,21 +459,19 @@ func (t *transactionService) CancelTransaction(ctx context.Context, originalTxHa return common.Hash{}, err } - gasPrice := sctx.GetGasPrice(ctx) - if gasPrice == nil { - gasPrice = new(big.Int).Add(storedTransaction.GasPrice, big.NewInt(1)) - } else if gasPrice.Cmp(storedTransaction.GasPrice) <= 0 { - return common.Hash{}, ErrGasPriceTooLow + gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), storedTransaction.GasTipCap) + if err != nil { + return common.Hash{}, err } signedTx, err := t.signer.SignTx(types.NewTx(&types.DynamicFeeTx{ - ChainID: t.chainID, Nonce: storedTransaction.Nonce, + ChainID: t.chainID, To: &t.sender, Value: big.NewInt(0), Gas: 21000, - GasTipCap: gasPrice, - GasFeeCap: gasPrice, + GasTipCap: gasTipCap, + GasFeeCap: gasFeeCap, Data: []byte{}, }), t.chainID) if err != nil { diff --git a/pkg/transaction/transaction_test.go b/pkg/transaction/transaction_test.go index 684c5106104..6388504a42a 100644 --- a/pkg/transaction/transaction_test.go +++ b/pkg/transaction/transaction_test.go @@ -96,8 +96,8 @@ func TestTransactionSend(t *testing.T) { To: &recipient, Value: value, Gas: estimatedGasLimit, - GasTipCap: suggestedGasPrice, - GasFeeCap: suggestedGasPrice, + GasFeeCap: defaultGasFee, + GasTipCap: suggestedGasTip, Data: txData, }) request := &transaction.TxRequest{ @@ -351,8 +351,8 @@ func TestTransactionSend(t *testing.T) { To: &recipient, Value: value, Gas: estimatedGasLimit, - GasTipCap: suggestedGasPrice, - GasFeeCap: suggestedGasPrice, + GasTipCap: suggestedGasTip, + GasFeeCap: defaultGasFee, Data: txData, }) request := &transaction.TxRequest{ @@ -428,8 +428,8 @@ func TestTransactionSend(t *testing.T) { To: &recipient, Value: value, Gas: estimatedGasLimit, - GasTipCap: suggestedGasPrice, - GasFeeCap: suggestedGasPrice, + GasTipCap: suggestedGasTip, + GasFeeCap: defaultGasFee, Data: txData, }) request := &transaction.TxRequest{ @@ -582,8 +582,8 @@ func TestTransactionResend(t *testing.T) { To: &recipient, Value: value, Gas: gasLimit, - GasTipCap: gasPrice, - GasFeeCap: gasPrice, + GasTipCap: gasTip, + GasFeeCap: gasFee, Data: data, }) @@ -653,8 +653,8 @@ func TestTransactionCancel(t *testing.T) { To: &recipient, Value: value, Gas: gasLimit, - GasTipCap: gasPrice, - GasFeeCap: gasPrice, + GasFeeCap: gasFee, + GasTipCap: gasTip, Data: data, }) err := store.Put(transaction.StoredTransactionKey(signedTx.Hash()), transaction.StoredTransaction{ @@ -678,8 +678,8 @@ func TestTransactionCancel(t *testing.T) { To: &recipient, Value: big.NewInt(0), Gas: 21000, - GasTipCap: new(big.Int).Add(gasPrice, big.NewInt(1)), - GasFeeCap: new(big.Int).Add(gasPrice, big.NewInt(1)), + GasTipCap: gasTip, + GasFeeCap: gasFee, Data: []byte{}, }) @@ -722,14 +722,16 @@ func TestTransactionCancel(t *testing.T) { t.Parallel() customGasPrice := big.NewInt(5) + customGasFee := new(big.Int).Add(customGasPrice, gasTip) + cancelTx := types.NewTx(&types.DynamicFeeTx{ ChainID: chainID, Nonce: nonce, To: &recipient, Value: big.NewInt(0), Gas: 21000, - GasTipCap: customGasPrice, - GasFeeCap: customGasPrice, + GasFeeCap: customGasFee, + GasTipCap: gasTip, Data: []byte{}, }) @@ -768,45 +770,4 @@ func TestTransactionCancel(t *testing.T) { t.Fatalf("returned wrong hash. wanted %v, got %v", cancelTx.Hash(), cancelTxHash) } }) - - t.Run("too low gas price", func(t *testing.T) { - t.Parallel() - - customGasPrice := big.NewInt(0) - cancelTx := types.NewTx(&types.DynamicFeeTx{ - ChainID: chainID, - Nonce: nonce, - To: &recipient, - Value: big.NewInt(0), - Gas: 21000, - GasTipCap: customGasPrice, - GasFeeCap: customGasPrice, - Data: []byte{}, - }) - - transactionService, err := transaction.NewService(logger, - backendmock.New( - backendmock.WithSendTransactionFunc(func(ctx context.Context, tx *types.Transaction) error { - if tx != cancelTx { - t.Fatal("not sending signed transaction") - } - return nil - }), - ), - signerMockForTransaction(t, cancelTx, recipient, chainID), - store, - chainID, - monitormock.New(), - ) - if err != nil { - t.Fatal(err) - } - defer transactionService.Close() - - ctx := sctx.SetGasPrice(context.Background(), customGasPrice) - _, err = transactionService.CancelTransaction(ctx, signedTx.Hash()) - if !errors.Is(err, transaction.ErrGasPriceTooLow) { - t.Fatalf("returned wrong error. wanted %v, got %v", transaction.ErrGasPriceTooLow, err) - } - }) } From 33c9f2a299a9b72588670219166ced3052970d9f Mon Sep 17 00:00:00 2001 From: swarmHaseeb Date: Wed, 9 Nov 2022 05:50:16 +0500 Subject: [PATCH 07/11] refactor: merge conflicts --- pkg/api/transaction.go | 6 +++--- pkg/api/transaction_test.go | 14 +++++++------- pkg/transaction/transaction.go | 10 +++++----- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pkg/api/transaction.go b/pkg/api/transaction.go index ca1bf070af1..ea928e541b9 100644 --- a/pkg/api/transaction.go +++ b/pkg/api/transaction.go @@ -32,7 +32,7 @@ type transactionInfo struct { Nonce uint64 `json:"nonce"` GasPrice *bigint.BigInt `json:"gasPrice"` GasLimit uint64 `json:"gasLimit"` - GasTipCap *bigint.BigInt `json:"gasTipCap"` + GasTipCap int `json:"gasTipCap"` GasFeeCap *bigint.BigInt `json:"gasFeeCap"` Data string `json:"data"` Created time.Time `json:"created"` @@ -72,7 +72,7 @@ func (s *Service) transactionListHandler(w http.ResponseWriter, _ *http.Request) GasPrice: bigint.Wrap(storedTransaction.GasPrice), GasLimit: storedTransaction.GasLimit, GasFeeCap: bigint.Wrap(storedTransaction.GasFeeCap), - GasTipCap: bigint.Wrap(storedTransaction.GasTipCap), + GasTipCap: storedTransaction.GasTipBoost, Data: hexutil.Encode(storedTransaction.Data), Created: time.Unix(storedTransaction.Created, 0), Description: storedTransaction.Description, @@ -116,7 +116,7 @@ func (s *Service) transactionDetailHandler(w http.ResponseWriter, r *http.Reques GasPrice: bigint.Wrap(storedTransaction.GasPrice), GasLimit: storedTransaction.GasLimit, GasFeeCap: bigint.Wrap(storedTransaction.GasFeeCap), - GasTipCap: bigint.Wrap(storedTransaction.GasTipCap), + GasTipCap: storedTransaction.GasTipBoost, Data: hexutil.Encode(storedTransaction.Data), Created: time.Unix(storedTransaction.Created, 0), Description: storedTransaction.Description, diff --git a/pkg/api/transaction_test.go b/pkg/api/transaction_test.go index c187dd2185a..db03ef0bd2b 100644 --- a/pkg/api/transaction_test.go +++ b/pkg/api/transaction_test.go @@ -36,7 +36,7 @@ func TestTransactionStoredTransaction(t *testing.T) { value := big.NewInt(50) nonce := uint64(12) description := "test" - + gasTipBoost := 10 t.Run("found", func(t *testing.T) { t.Parallel() @@ -51,7 +51,7 @@ func TestTransactionStoredTransaction(t *testing.T) { GasPrice: gasPrice, GasLimit: gasLimit, GasFeeCap: gasPrice, - GasTipCap: gasPrice, + GasTipBoost: gasTipBoost, Value: value, Nonce: nonce, Description: description, @@ -69,7 +69,7 @@ func TestTransactionStoredTransaction(t *testing.T) { GasPrice: bigint.Wrap(gasPrice), GasLimit: gasLimit, GasFeeCap: bigint.Wrap(gasPrice), - GasTipCap: bigint.Wrap(gasPrice), + GasTipCap: gasTipBoost, Value: bigint.Wrap(value), Nonce: nonce, Description: description, @@ -128,7 +128,7 @@ func TestTransactionList(t *testing.T) { Data: []byte{1, 2, 3, 4}, GasPrice: big.NewInt(12), GasLimit: 5345, - GasTipCap: big.NewInt(12), + GasTipBoost: 10, GasFeeCap: big.NewInt(12), Value: big.NewInt(4), Nonce: 3, @@ -140,7 +140,7 @@ func TestTransactionList(t *testing.T) { Created: 2, Data: []byte{3, 2, 3, 4}, GasPrice: big.NewInt(42), - GasTipCap: nil, + GasTipBoost: 10, GasFeeCap: big.NewInt(42), GasLimit: 53451, Value: big.NewInt(41), @@ -170,7 +170,7 @@ func TestTransactionList(t *testing.T) { Nonce: storedTransactions[txHash1].Nonce, GasPrice: bigint.Wrap(storedTransactions[txHash1].GasPrice), GasLimit: storedTransactions[txHash1].GasLimit, - GasTipCap: bigint.Wrap(storedTransactions[txHash1].GasPrice), + GasTipCap: 10, GasFeeCap: bigint.Wrap(storedTransactions[txHash1].GasPrice), Data: hexutil.Encode(storedTransactions[txHash1].Data), Created: time.Unix(storedTransactions[txHash1].Created, 0), @@ -183,7 +183,7 @@ func TestTransactionList(t *testing.T) { Nonce: storedTransactions[txHash2].Nonce, GasPrice: bigint.Wrap(storedTransactions[txHash2].GasPrice), GasLimit: storedTransactions[txHash2].GasLimit, - GasTipCap: nil, + GasTipCap: 10, GasFeeCap: bigint.Wrap(storedTransactions[txHash2].GasPrice), Data: hexutil.Encode(storedTransactions[txHash2].Data), Created: time.Unix(storedTransactions[txHash2].Created, 0), diff --git a/pkg/transaction/transaction.go b/pkg/transaction/transaction.go index 0ac84fb49d0..d5de40f11db 100644 --- a/pkg/transaction/transaction.go +++ b/pkg/transaction/transaction.go @@ -48,7 +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 - GasTipCap *big.Int // adds a tip for the miner for prioritizing transaction + GasTipBoost 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 @@ -59,7 +59,7 @@ 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 + GasTipBoost 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 @@ -181,7 +181,7 @@ func (t *transactionService) Send(ctx context.Context, request *TxRequest, tipCa Data: signedTx.Data(), GasPrice: signedTx.GasPrice(), GasLimit: signedTx.Gas(), - GasTipCap: signedTx.GasTipCap(), + GasTipBoost: tipCapBoostPercent, GasFeeCap: signedTx.GasFeeCap(), Value: signedTx.Value(), Nonce: signedTx.Nonce(), @@ -419,7 +419,7 @@ func (t *transactionService) ResendTransaction(ctx context.Context, txHash commo return err } - gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), storedTransaction.GasTipCap) + gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), storedTransaction.GasTipBoost) if err != nil { return err } @@ -459,7 +459,7 @@ func (t *transactionService) CancelTransaction(ctx context.Context, originalTxHa return common.Hash{}, err } - gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), storedTransaction.GasTipCap) + gasFeeCap, gasTipCap, err := t.suggestedFeeAndTip(ctx, sctx.GetGasPrice(ctx), storedTransaction.GasTipBoost) if err != nil { return common.Hash{}, err } From 55f56a6e96bed937e8f90f5311fba4a46cd88bea Mon Sep 17 00:00:00 2001 From: swarmHaseeb Date: Thu, 10 Nov 2022 11:02:53 +0500 Subject: [PATCH 08/11] refactor: tx errors --- pkg/transaction/transaction.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/transaction/transaction.go b/pkg/transaction/transaction.go index d5de40f11db..6d6fbc06ffd 100644 --- a/pkg/transaction/transaction.go +++ b/pkg/transaction/transaction.go @@ -489,6 +489,8 @@ func (t *transactionService) CancelTransaction(ctx context.Context, originalTxHa Data: signedTx.Data(), GasPrice: signedTx.GasPrice(), GasLimit: signedTx.Gas(), + GasFeeCap: signedTx.GasFeeCap(), + GasTipBoost: storedTransaction.GasTipBoost, Value: signedTx.Value(), Nonce: signedTx.Nonce(), Created: time.Now().Unix(), From dae4547dfd9e324e664a4d2596f41e1a0255da1c Mon Sep 17 00:00:00 2001 From: swarmHaseeb Date: Thu, 10 Nov 2022 21:02:23 +0500 Subject: [PATCH 09/11] refactor: type change and version bump --- openapi/SwarmCommon.yaml | 2 +- pkg/api/transaction.go | 6 +++--- pkg/api/transaction_test.go | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/openapi/SwarmCommon.yaml b/openapi/SwarmCommon.yaml index dc3723e0c25..9efe9dfd18b 100644 --- a/openapi/SwarmCommon.yaml +++ b/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***** diff --git a/pkg/api/transaction.go b/pkg/api/transaction.go index ea928e541b9..05921f761b3 100644 --- a/pkg/api/transaction.go +++ b/pkg/api/transaction.go @@ -32,7 +32,7 @@ type transactionInfo struct { Nonce uint64 `json:"nonce"` GasPrice *bigint.BigInt `json:"gasPrice"` GasLimit uint64 `json:"gasLimit"` - GasTipCap int `json:"gasTipCap"` + GasTipCap *bigint.BigInt `json:"gasTipCap"` GasFeeCap *bigint.BigInt `json:"gasFeeCap"` Data string `json:"data"` Created time.Time `json:"created"` @@ -72,7 +72,7 @@ func (s *Service) transactionListHandler(w http.ResponseWriter, _ *http.Request) GasPrice: bigint.Wrap(storedTransaction.GasPrice), GasLimit: storedTransaction.GasLimit, GasFeeCap: bigint.Wrap(storedTransaction.GasFeeCap), - GasTipCap: storedTransaction.GasTipBoost, + GasTipCap: bigint.Wrap(big.NewInt(int64(storedTransaction.GasTipBoost))), Data: hexutil.Encode(storedTransaction.Data), Created: time.Unix(storedTransaction.Created, 0), Description: storedTransaction.Description, @@ -116,7 +116,7 @@ func (s *Service) transactionDetailHandler(w http.ResponseWriter, r *http.Reques GasPrice: bigint.Wrap(storedTransaction.GasPrice), GasLimit: storedTransaction.GasLimit, GasFeeCap: bigint.Wrap(storedTransaction.GasFeeCap), - GasTipCap: storedTransaction.GasTipBoost, + GasTipCap: bigint.Wrap(big.NewInt(int64(storedTransaction.GasTipBoost))), Data: hexutil.Encode(storedTransaction.Data), Created: time.Unix(storedTransaction.Created, 0), Description: storedTransaction.Description, diff --git a/pkg/api/transaction_test.go b/pkg/api/transaction_test.go index db03ef0bd2b..46498f9e9ba 100644 --- a/pkg/api/transaction_test.go +++ b/pkg/api/transaction_test.go @@ -69,7 +69,7 @@ func TestTransactionStoredTransaction(t *testing.T) { GasPrice: bigint.Wrap(gasPrice), GasLimit: gasLimit, GasFeeCap: bigint.Wrap(gasPrice), - GasTipCap: gasTipBoost, + GasTipCap: bigint.Wrap(big.NewInt(int64(gasTipBoost))), Value: bigint.Wrap(value), Nonce: nonce, Description: description, @@ -170,7 +170,7 @@ func TestTransactionList(t *testing.T) { Nonce: storedTransactions[txHash1].Nonce, GasPrice: bigint.Wrap(storedTransactions[txHash1].GasPrice), GasLimit: storedTransactions[txHash1].GasLimit, - GasTipCap: 10, + GasTipCap: bigint.Wrap(big.NewInt(int64(10))), GasFeeCap: bigint.Wrap(storedTransactions[txHash1].GasPrice), Data: hexutil.Encode(storedTransactions[txHash1].Data), Created: time.Unix(storedTransactions[txHash1].Created, 0), @@ -183,7 +183,7 @@ func TestTransactionList(t *testing.T) { Nonce: storedTransactions[txHash2].Nonce, GasPrice: bigint.Wrap(storedTransactions[txHash2].GasPrice), GasLimit: storedTransactions[txHash2].GasLimit, - GasTipCap: 10, + GasTipCap: bigint.Wrap(big.NewInt(int64(10))), GasFeeCap: bigint.Wrap(storedTransactions[txHash2].GasPrice), Data: hexutil.Encode(storedTransactions[txHash2].Data), Created: time.Unix(storedTransactions[txHash2].Created, 0), From 6bd4ba05fd1bfb4c2302f642a66bd85bb7b09851 Mon Sep 17 00:00:00 2001 From: swarmHaseeb Date: Thu, 10 Nov 2022 22:24:49 +0500 Subject: [PATCH 10/11] refactor: tx struct field --- pkg/transaction/transaction.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/transaction/transaction.go b/pkg/transaction/transaction.go index 6d6fbc06ffd..f019f88f26c 100644 --- a/pkg/transaction/transaction.go +++ b/pkg/transaction/transaction.go @@ -48,7 +48,6 @@ 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 - GasTipBoost 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 From 95850c294b8545c0c68b541f656a11cb8e67ef40 Mon Sep 17 00:00:00 2001 From: swarmHaseeb Date: Mon, 14 Nov 2022 14:31:24 +0500 Subject: [PATCH 11/11] refactor: gastipcap and gastipboost in api tx --- openapi/SwarmCommon.yaml | 2 ++ pkg/api/transaction.go | 7 +++++-- pkg/api/transaction_test.go | 15 +++++++++++---- pkg/node/devnode.go | 3 +++ pkg/transaction/transaction.go | 3 +++ 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/openapi/SwarmCommon.yaml b/openapi/SwarmCommon.yaml index 9efe9dfd18b..8ac5cb931b7 100644 --- a/openapi/SwarmCommon.yaml +++ b/openapi/SwarmCommon.yaml @@ -657,6 +657,8 @@ components: type: integer gasTipCap: $ref: "#/components/schemas/BigInt" + gasTipBoost: + type: integer gasFeeCap: $ref: "#/components/schemas/BigInt" data: diff --git a/pkg/api/transaction.go b/pkg/api/transaction.go index 05921f761b3..9261c172282 100644 --- a/pkg/api/transaction.go +++ b/pkg/api/transaction.go @@ -32,6 +32,7 @@ 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"` @@ -72,7 +73,8 @@ func (s *Service) transactionListHandler(w http.ResponseWriter, _ *http.Request) GasPrice: bigint.Wrap(storedTransaction.GasPrice), GasLimit: storedTransaction.GasLimit, GasFeeCap: bigint.Wrap(storedTransaction.GasFeeCap), - GasTipCap: bigint.Wrap(big.NewInt(int64(storedTransaction.GasTipBoost))), + GasTipCap: bigint.Wrap(storedTransaction.GasTipCap), + GasTipBoost: storedTransaction.GasTipBoost, Data: hexutil.Encode(storedTransaction.Data), Created: time.Unix(storedTransaction.Created, 0), Description: storedTransaction.Description, @@ -116,7 +118,8 @@ func (s *Service) transactionDetailHandler(w http.ResponseWriter, r *http.Reques GasPrice: bigint.Wrap(storedTransaction.GasPrice), GasLimit: storedTransaction.GasLimit, GasFeeCap: bigint.Wrap(storedTransaction.GasFeeCap), - GasTipCap: bigint.Wrap(big.NewInt(int64(storedTransaction.GasTipBoost))), + GasTipCap: bigint.Wrap(storedTransaction.GasTipCap), + GasTipBoost: storedTransaction.GasTipBoost, Data: hexutil.Encode(storedTransaction.Data), Created: time.Unix(storedTransaction.Created, 0), Description: storedTransaction.Description, diff --git a/pkg/api/transaction_test.go b/pkg/api/transaction_test.go index 46498f9e9ba..7c202aa9def 100644 --- a/pkg/api/transaction_test.go +++ b/pkg/api/transaction_test.go @@ -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() @@ -52,6 +53,7 @@ func TestTransactionStoredTransaction(t *testing.T) { GasLimit: gasLimit, GasFeeCap: gasPrice, GasTipBoost: gasTipBoost, + GasTipCap: gasTipCap, Value: value, Nonce: nonce, Description: description, @@ -69,7 +71,8 @@ func TestTransactionStoredTransaction(t *testing.T) { GasPrice: bigint.Wrap(gasPrice), GasLimit: gasLimit, GasFeeCap: bigint.Wrap(gasPrice), - GasTipCap: bigint.Wrap(big.NewInt(int64(gasTipBoost))), + GasTipCap: bigint.Wrap(gasTipCap), + GasTipBoost: gasTipBoost, Value: bigint.Wrap(value), Nonce: nonce, Description: description, @@ -129,6 +132,7 @@ func TestTransactionList(t *testing.T) { 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, @@ -141,6 +145,7 @@ func TestTransactionList(t *testing.T) { 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), @@ -170,7 +175,8 @@ func TestTransactionList(t *testing.T) { Nonce: storedTransactions[txHash1].Nonce, GasPrice: bigint.Wrap(storedTransactions[txHash1].GasPrice), GasLimit: storedTransactions[txHash1].GasLimit, - GasTipCap: bigint.Wrap(big.NewInt(int64(10))), + 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), @@ -183,7 +189,8 @@ func TestTransactionList(t *testing.T) { Nonce: storedTransactions[txHash2].Nonce, GasPrice: bigint.Wrap(storedTransactions[txHash2].GasPrice), GasLimit: storedTransactions[txHash2].GasLimit, - GasTipCap: bigint.Wrap(big.NewInt(int64(10))), + 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), diff --git a/pkg/node/devnode.go b/pkg/node/devnode.go index fc1a3b4e45d..40817b14733 100644 --- a/pkg/node/devnode.go +++ b/pkg/node/devnode.go @@ -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, diff --git a/pkg/transaction/transaction.go b/pkg/transaction/transaction.go index f019f88f26c..682eb5fb657 100644 --- a/pkg/transaction/transaction.go +++ b/pkg/transaction/transaction.go @@ -59,6 +59,7 @@ type StoredTransaction struct { GasPrice *big.Int // used gas price 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 @@ -181,6 +182,7 @@ func (t *transactionService) Send(ctx context.Context, request *TxRequest, tipCa GasPrice: signedTx.GasPrice(), GasLimit: signedTx.Gas(), GasTipBoost: tipCapBoostPercent, + GasTipCap: signedTx.GasTipCap(), GasFeeCap: signedTx.GasFeeCap(), Value: signedTx.Value(), Nonce: signedTx.Nonce(), @@ -490,6 +492,7 @@ func (t *transactionService) CancelTransaction(ctx context.Context, originalTxHa GasLimit: signedTx.Gas(), GasFeeCap: signedTx.GasFeeCap(), GasTipBoost: storedTransaction.GasTipBoost, + GasTipCap: signedTx.GasTipCap(), Value: signedTx.Value(), Nonce: signedTx.Nonce(), Created: time.Now().Unix(),