From 8977772763ecd895006c00c9cbe1397429a19b6f Mon Sep 17 00:00:00 2001 From: Janek Rahrt Date: Tue, 24 May 2022 10:01:16 +0200 Subject: [PATCH] feat: add suggestedMaxFee --- src/account/default.ts | 20 ++++++++++++++------ src/account/interface.ts | 4 ++-- src/types/account.ts | 7 +++++++ src/types/lib.ts | 2 +- src/utils/stark.ts | 2 +- 5 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 src/types/account.ts diff --git a/src/account/default.ts b/src/account/default.ts index b591c2255..77aff51ff 100644 --- a/src/account/default.ts +++ b/src/account/default.ts @@ -8,7 +8,6 @@ import { Abi, AddTransactionResponse, Call, - EstimateFeeResponse, InvocationsDetails, InvocationsSignerDetails, InvokeFunctionTransaction, @@ -16,6 +15,7 @@ import { Signature, Transaction, } from '../types'; +import { EstimateFee } from '../types/account'; import { sign } from '../utils/ellipticCurve'; import { computeHashOnElements, @@ -60,7 +60,7 @@ export class Account extends Provider implements AccountInterface { nonce: providedNonce, blockIdentifier = 'pending', }: { nonce?: BigNumberish; blockIdentifier?: BlockIdentifier } = {} - ): Promise { + ): Promise { const transactions = Array.isArray(calls) ? calls : [calls]; const nonce = providedNonce ?? (await this.getNonce()); const version = toBN(feeTransactionVersion); @@ -76,7 +76,7 @@ export class Account extends Provider implements AccountInterface { const signature = await this.signer.signTransaction(transactions, signerDetails); const calldata = fromCallsToExecuteCalldataWithNonce(transactions, nonce); - return this.fetchEndpoint( + const fetchedEstimate = await this.fetchEndpoint( 'estimate_fee', { blockIdentifier }, { @@ -87,6 +87,12 @@ export class Account extends Provider implements AccountInterface { signature: bigNumberishArrayToDecimalStringArray(signature), } ); + const suggestedMaxFee = estimatedFeeToMaxFee(fetchedEstimate.amount); + + return { + ...fetchedEstimate, + suggestedMaxFee, + }; } /** @@ -94,7 +100,9 @@ export class Account extends Provider implements AccountInterface { * * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17) * - * @param transaction - transaction to be invoked + * @param calls - one or more calls to be executed + * @param abis - one or more abis which can be used to display the calls + * @param transactionsDetail - optional transaction details * @returns a confirmation of invoking a function on the starknet contract */ public async execute( @@ -108,8 +116,8 @@ export class Account extends Provider implements AccountInterface { if (transactionsDetail.maxFee || transactionsDetail.maxFee === 0) { maxFee = transactionsDetail.maxFee; } else { - const estimatedFee = (await this.estimateFee(transactions, { nonce })).amount; - maxFee = estimatedFeeToMaxFee(estimatedFee).toString(); + const { suggestedMaxFee } = await this.estimateFee(transactions, { nonce }); + maxFee = suggestedMaxFee.toString(); } const signerDetails: InvocationsSignerDetails = { diff --git a/src/account/interface.ts b/src/account/interface.ts index c78aaa952..f994a4691 100644 --- a/src/account/interface.ts +++ b/src/account/interface.ts @@ -5,11 +5,11 @@ import { AddTransactionResponse, Call, DeployContractPayload, - EstimateFeeResponse, Invocation, InvocationsDetails, Signature, } from '../types'; +import { EstimateFee } from '../types/account'; import { BigNumberish } from '../utils/number'; import { TypedData } from '../utils/typedData/types'; @@ -44,7 +44,7 @@ export abstract class AccountInterface extends ProviderInterface { * * @returns response from addTransaction */ - public abstract estimateFee(invocation: Invocation): Promise; + public abstract estimateFee(invocation: Invocation): Promise; /** * Invoke execute function in account contract diff --git a/src/types/account.ts b/src/types/account.ts new file mode 100644 index 000000000..75bdafc7b --- /dev/null +++ b/src/types/account.ts @@ -0,0 +1,7 @@ +import BN from 'bn.js'; + +import { EstimateFeeResponse } from './api'; + +export interface EstimateFee extends EstimateFeeResponse { + suggestedMaxFee: BN; +} diff --git a/src/types/lib.ts b/src/types/lib.ts index e74515b89..9e692be20 100644 --- a/src/types/lib.ts +++ b/src/types/lib.ts @@ -60,7 +60,7 @@ export type Abi = Array; export type EntryPointsByType = object; export type Program = Record; -export type BlockNumber = 'pending' | null | number; +export type BlockNumber = 'pending' | 'latest' | null | number; export type CompiledContract = { abi: Abi; diff --git a/src/utils/stark.ts b/src/utils/stark.ts index 66b40ad23..9096bb326 100644 --- a/src/utils/stark.ts +++ b/src/utils/stark.ts @@ -50,7 +50,7 @@ export function compileCalldata(args: RawArgs): Calldata { }); } -export function estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: number = 0.15): BN { +export function estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: number = 0.5): BN { // BN can only handle Integers, so we need to do all calulations with integers const overHeadPercent = Math.round((1 + overhead) * 100); return toBN(estimatedFee).mul(toBN(overHeadPercent)).div(toBN(100));