diff --git a/src/provider/default.ts b/src/provider/default.ts index 1d4d88d08..dd11faa89 100644 --- a/src/provider/default.ts +++ b/src/provider/default.ts @@ -81,7 +81,7 @@ export class Provider implements ProviderInterface { } private getFetchMethod(endpoint: keyof Endpoints) { - const postMethodEndpoints = ['add_transaction', 'call_contract']; + const postMethodEndpoints = ['add_transaction', 'call_contract', 'estimate_fee']; return postMethodEndpoints.includes(endpoint) ? 'POST' : 'GET'; } @@ -333,6 +333,17 @@ export class Provider implements ProviderInterface { }); } + public estimateFee(invocation: Invocation): Promise { + return this.fetchEndpoint('estimate_fee', undefined, { + // TODO: change the TYPE of the call + type: 'INVOKE_FUNCTION', + contract_address: invocation.contractAddress, + entry_point_selector: getSelectorFromName(invocation.entrypoint), + calldata: bigNumberishArrayToDecimalStringArray(invocation.calldata ?? []), + signature: bigNumberishArrayToDecimalStringArray(invocation.signature ?? []), + }); + } + public async waitForTransaction(txHash: BigNumberish, retryInterval: number = 8000) { let onchain = false; await wait(retryInterval); diff --git a/src/types/api.ts b/src/types/api.ts index ab411a269..318397f61 100644 --- a/src/types/api.ts +++ b/src/types/api.ts @@ -67,6 +67,11 @@ export type Endpoints = { REQUEST: CallContractTransaction; RESPONSE: CallContractResponse; }; + estimate_fee: { + QUERY: never; + REQUEST: Transaction; + RESPONSE: EstimateFeeResponse; + }; }; export type GetContractAddressesResponse = { @@ -168,6 +173,8 @@ export type TransactionReceipt = { l2_to_l1_messages: string[]; events: string[]; }; +// TODO: Add response data +export type EstimateFeeResponse = {}; export type RawArgs = { [inputName: string]: string | string[] | { type: 'struct'; [k: string]: BigNumberish }; diff --git a/src/types/lib.ts b/src/types/lib.ts index 438bcadfa..1ce27a7a2 100644 --- a/src/types/lib.ts +++ b/src/types/lib.ts @@ -70,3 +70,7 @@ export type CompiledContract = { export type CompressedCompiledContract = Omit & { program: CompressedProgram; }; + +export type ParsedStruct = { + [key: string]: BigNumberish | ParsedStruct; +}; diff --git a/src/utils/transaction.ts b/src/utils/transaction.ts index dbbecfecd..3aa927a9e 100644 --- a/src/utils/transaction.ts +++ b/src/utils/transaction.ts @@ -1,5 +1,4 @@ -import { ParsedStruct } from '../contract'; -import { Call } from '../types'; +import { Call, ParsedStruct } from '../types'; import { getSelectorFromName } from './hash'; import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN } from './number';