Skip to content

Commit c4e7d48

Browse files
authored
Add RPC method to estimate tx (#809)
1 parent 4b9adcb commit c4e7d48

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

api/blockchain_api.go

+26
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ type Transactions struct {
221221
Token *hexutil.Bytes `json:"token"`
222222
}
223223

224+
type EstimateTxResponse struct {
225+
TxHash common.Hash `json:"txHash"`
226+
TxFee decimal.Decimal `json:"txFee"`
227+
}
228+
224229
// sorted by epoch \ nonce desc (the newest transactions are first)
225230
func (api *BlockchainApi) PendingTransactions(args TransactionsArgs) Transactions {
226231
txs := api.pool.GetPendingByAddress(args.Address)
@@ -287,6 +292,27 @@ func (api *BlockchainApi) GetRawTx(args SendTxArgs) (hexutil.Bytes, error) {
287292
return data, nil
288293
}
289294

295+
func (api *BlockchainApi) EstimateTx(args SendTxArgs) (*EstimateTxResponse, error) {
296+
var payload []byte
297+
if args.Payload != nil {
298+
payload = *args.Payload
299+
}
300+
301+
tx, err := api.baseApi.getSignedTx(args.From, args.To, args.Type, args.Amount, args.MaxFee, args.Tips, args.Nonce, args.Epoch, payload, nil)
302+
if err != nil {
303+
return nil, err
304+
}
305+
err = api.baseApi.txpool.Validate(tx)
306+
if err != nil {
307+
return nil, err
308+
}
309+
310+
return &EstimateTxResponse{
311+
TxHash: tx.Hash(),
312+
TxFee: blockchain.ConvertToFloat(fee.CalculateFee(1, api.baseApi.getReadonlyAppState().State.FeePerGas(), tx)),
313+
}, nil
314+
}
315+
290316
func (api *BlockchainApi) Transactions(args TransactionsArgs) Transactions {
291317

292318
txs, nextToken := api.bc.ReadTxs(args.Address, args.Count, args.Token)

0 commit comments

Comments
 (0)