diff --git a/src/account/default.ts b/src/account/default.ts index 3b9fc27a0..3afc3cf93 100644 --- a/src/account/default.ts +++ b/src/account/default.ts @@ -59,20 +59,16 @@ export class Account extends Provider implements AccountInterface { const signature = await this.signer.signTransaction(transactions, signerDetails); const calldata = fromCallsToExecuteCalldataWithNonce(transactions, nonce); - const fetchedEstimate = await super.getEstimateFee( + const response = await super.getEstimateFee( { contractAddress: this.address, entrypoint: '__execute__', calldata, signature }, blockIdentifier, { version } ); - const fee = fetchedEstimate.overall_fee ?? fetchedEstimate.amount; - if (fee === undefined) { - throw new Error('Expected either amount or overall_fee in estimate_fee response'); - } - const suggestedMaxFee = estimatedFeeToMaxFee(fee); + + const suggestedMaxFee = estimatedFeeToMaxFee(response.overall_fee); return { - amount: fee, - unit: fetchedEstimate.unit, + ...response, suggestedMaxFee, }; } diff --git a/src/account/interface.ts b/src/account/interface.ts index 160ac507c..6ca606c85 100644 --- a/src/account/interface.ts +++ b/src/account/interface.ts @@ -3,8 +3,8 @@ import { SignerInterface } from '../signer'; import { Abi, Call, - EstimateFee, EstimateFeeDetails, + EstimateFeeResponse, InvocationsDetails, InvokeFunctionResponse, Signature, @@ -31,7 +31,7 @@ export abstract class AccountInterface extends ProviderInterface { public abstract estimateFee( calls: Call | Call[], estimateFeeDetails?: EstimateFeeDetails - ): Promise; + ): Promise; /** * Invoke execute function in account contract diff --git a/src/provider/rpc.ts b/src/provider/rpc.ts index d4c21e0e1..338aeca57 100644 --- a/src/provider/rpc.ts +++ b/src/provider/rpc.ts @@ -15,8 +15,8 @@ import { Invocation, InvocationsDetails, InvokeFunctionResponse, - RPC, } from '../types'; +import { RPC } from '../types/api'; import { getSelectorFromName } from '../utils/hash'; import { stringify } from '../utils/json'; import { diff --git a/src/provider/sequencer.ts b/src/provider/sequencer.ts index cdd0146ee..7a4c9e3a8 100644 --- a/src/provider/sequencer.ts +++ b/src/provider/sequencer.ts @@ -11,16 +11,18 @@ import { DeployContractResponse, EstimateFeeResponse, GetBlockResponse, - GetContractAddressesResponse, GetTransactionReceiptResponse, GetTransactionResponse, - GetTransactionStatusResponse, - GetTransactionTraceResponse, Invocation, InvocationsDetails, InvokeFunctionResponse, - Sequencer, } from '../types'; +import { + GetContractAddressesResponse, + GetTransactionStatusResponse, + GetTransactionTraceResponse, + Sequencer, +} from '../types/api'; import { getSelectorFromName } from '../utils/hash'; import { parse, parseAlwaysAsBig, stringify } from '../utils/json'; import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number'; diff --git a/src/types/account.ts b/src/types/account.ts index ce64e8323..fb20c6cd3 100644 --- a/src/types/account.ts +++ b/src/types/account.ts @@ -2,10 +2,9 @@ import BN from 'bn.js'; import { BlockIdentifier } from '../provider/utils'; import { BigNumberish } from '../utils/number'; +import { EstimateFeeResponse } from './provider'; -export interface EstimateFee { - amount: BN; - unit: string; +export interface EstimateFee extends EstimateFeeResponse { suggestedMaxFee: BN; } diff --git a/src/types/api/sequencer.ts b/src/types/api/sequencer.ts index cb8a763eb..f8f1390eb 100644 --- a/src/types/api/sequencer.ts +++ b/src/types/api/sequencer.ts @@ -209,13 +209,16 @@ export namespace Sequencer { }; // Support 0.9.1 changes in a backward-compatible way - export type EstimateFeeResponse = { - overall_fee?: BN; - amount?: BN; - unit: string; - gas_price?: BN; - gas_usage?: BigNumberish; - }; + export type EstimateFeeResponse = + | { + overall_fee: number; + gas_price: number; + gas_usage: number; + } + | { + amount: BN; + unit: string; + }; export type Endpoints = { get_contract_addresses: { diff --git a/src/types/index.ts b/src/types/index.ts index fe9af424b..450cfb3d0 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,5 +1,6 @@ export * from './lib'; -export * from './api'; +export * as api from './api'; +export { Calldata, Overrides, RawArgs } from './api'; export * from './signer'; export * from './contract'; export * from './account'; diff --git a/src/types/provider.ts b/src/types/provider.ts index 6a42f6fa0..8aace924f 100644 --- a/src/types/provider.ts +++ b/src/types/provider.ts @@ -1,4 +1,5 @@ -import { BigNumberish } from '../utils/number'; +import BN from 'bn.js'; + import { Abi, CompressedProgram, EntryPointsByType, RawCalldata, Signature, Status } from './lib'; export interface GetBlockResponse { @@ -82,9 +83,9 @@ export interface InvokeTransactionReceiptResponse extends CommonTransactionRecei export type DeclareTransactionReceiptResponse = CommonTransactionReceiptResponse; export interface EstimateFeeResponse { - overall_fee: BigNumberish; - gas_consumed?: BigNumberish; - gas_price?: BigNumberish; + overall_fee: BN; + gas_consumed?: BN; + gas_price?: BN; } export interface InvokeFunctionResponse { diff --git a/src/utils/responseParser/rpc.ts b/src/utils/responseParser/rpc.ts index 4194d429f..987cd638f 100644 --- a/src/utils/responseParser/rpc.ts +++ b/src/utils/responseParser/rpc.ts @@ -7,8 +7,8 @@ import { GetTransactionReceiptResponse, GetTransactionResponse, InvokeFunctionResponse, - RPC, } from '../../types'; +import { RPC } from '../../types/api'; import { toBN } from '../number'; import { ResponseParser } from '.'; diff --git a/src/utils/responseParser/sequencer.ts b/src/utils/responseParser/sequencer.ts index 10c0639fd..74f3baebe 100644 --- a/src/utils/responseParser/sequencer.ts +++ b/src/utils/responseParser/sequencer.ts @@ -7,8 +7,8 @@ import { GetTransactionReceiptResponse, GetTransactionResponse, InvokeFunctionResponse, - Sequencer, } from '../../types'; +import { Sequencer } from '../../types/api'; import { toBN } from '../number'; import { ResponseParser } from '.'; @@ -71,6 +71,23 @@ export class SequencerAPIResponseParser extends ResponseParser { } public parseFeeEstimateResponse(res: Sequencer.EstimateFeeResponse): EstimateFeeResponse { + if ('overall_fee' in res) { + let gasInfo = {}; + + try { + gasInfo = { + gas_consumed: toBN(res.gas_usage), + gas_price: toBN(res.gas_price), + }; + } catch { + // do nothing + } + + return { + overall_fee: toBN(res.overall_fee), + ...gasInfo, + }; + } return { overall_fee: toBN(res.amount), };