Skip to content

Commit

Permalink
Add RPC method to estimate tx (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidenaio authored Oct 1, 2021
1 parent 4b9adcb commit c4e7d48
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions api/blockchain_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c4e7d48

Please sign in to comment.