From c4e7d480154f3a61a87b393c38792da6488b180c Mon Sep 17 00:00:00 2001 From: sidenaio <50700001+sidenaio@users.noreply.github.com> Date: Fri, 1 Oct 2021 22:07:59 +0500 Subject: [PATCH] Add RPC method to estimate tx (#809) --- api/blockchain_api.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/api/blockchain_api.go b/api/blockchain_api.go index f5c2481a..d324a89e 100644 --- a/api/blockchain_api.go +++ b/api/blockchain_api.go @@ -221,6 +221,11 @@ type Transactions struct { Token *hexutil.Bytes `json:"token"` } +type EstimateTxResponse struct { + TxHash common.Hash `json:"txHash"` + TxFee decimal.Decimal `json:"txFee"` +} + // sorted by epoch \ nonce desc (the newest transactions are first) func (api *BlockchainApi) PendingTransactions(args TransactionsArgs) Transactions { txs := api.pool.GetPendingByAddress(args.Address) @@ -287,6 +292,27 @@ func (api *BlockchainApi) GetRawTx(args SendTxArgs) (hexutil.Bytes, error) { return data, nil } +func (api *BlockchainApi) EstimateTx(args SendTxArgs) (*EstimateTxResponse, error) { + var payload []byte + if args.Payload != nil { + payload = *args.Payload + } + + tx, err := api.baseApi.getSignedTx(args.From, args.To, args.Type, args.Amount, args.MaxFee, args.Tips, args.Nonce, args.Epoch, payload, nil) + if err != nil { + return nil, err + } + err = api.baseApi.txpool.Validate(tx) + if err != nil { + return nil, err + } + + return &EstimateTxResponse{ + TxHash: tx.Hash(), + TxFee: blockchain.ConvertToFloat(fee.CalculateFee(1, api.baseApi.getReadonlyAppState().State.FeePerGas(), tx)), + }, nil +} + func (api *BlockchainApi) Transactions(args TransactionsArgs) Transactions { txs, nextToken := api.bc.ReadTxs(args.Address, args.Count, args.Token)