Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RPC method to estimate tx #809

Merged
merged 1 commit into from
Oct 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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