From 52110b289e3dfbbe449cf195709b2714f295a70d Mon Sep 17 00:00:00 2001 From: Iris Date: Mon, 9 Jan 2023 15:01:57 +0100 Subject: [PATCH] fix: update types & add separate responseParser for estimateFeeBulk --- src/account/default.ts | 11 ++++++----- src/account/interface.ts | 3 ++- src/provider/default.ts | 3 ++- src/provider/interface.ts | 3 ++- src/provider/rpc.ts | 3 ++- src/provider/sequencer.ts | 11 +++++------ src/types/api/sequencer.ts | 10 ++++++++-- src/types/lib.ts | 17 +++++++++------- src/types/provider.ts | 2 ++ src/utils/responseParser/sequencer.ts | 28 +++++++++++++++++++++++++++ 10 files changed, 67 insertions(+), 24 deletions(-) diff --git a/src/account/default.ts b/src/account/default.ts index 283a6091d..700b02d62 100644 --- a/src/account/default.ts +++ b/src/account/default.ts @@ -21,6 +21,7 @@ import { EstimateFee, EstimateFeeAction, EstimateFeeDetails, + EstimateFeeResponseBulk, Invocation, InvocationsDetails, InvocationsSignerDetails, @@ -219,7 +220,7 @@ export class Account extends Provider implements AccountInterface { public async estimateFeeBulk( transactions: TransactionBulk, { nonce: providedNonce, blockIdentifier }: EstimateFeeDetails = {} - ): Promise> { + ): Promise { const nonce = toBN(providedNonce ?? (await this.getNonce())); const version = toBN(feeTransactionVersion); const chainId = await this.getChainId(); @@ -293,7 +294,7 @@ export class Account extends Provider implements AccountInterface { }); } - private async buildInvocation( + public async buildInvocation( call: Array, signerDetails: InvocationsSignerDetails ): Promise { @@ -557,7 +558,7 @@ export class Account extends Provider implements AccountInterface { return feeEstimate.suggestedMaxFee.toString(); } - private async buildDeclarePayload( + public async buildDeclarePayload( { classHash, contract }: DeclareContractPayload, { nonce, chainId, version, walletAddress, maxFee }: InvocationsSignerDetails ): Promise { @@ -578,7 +579,7 @@ export class Account extends Provider implements AccountInterface { }; } - private async buildAccountDeployPayload( + public async buildAccountDeployPayload( { classHash, addressSalt = 0, @@ -610,7 +611,7 @@ export class Account extends Provider implements AccountInterface { }; } - private buildUDCContractPayload( + public buildUDCContractPayload( payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[] ): Call[] { const calls = [].concat(payload as []).map((it) => { diff --git a/src/account/interface.ts b/src/account/interface.ts index c6d9d186a..9bf4ea54b 100644 --- a/src/account/interface.ts +++ b/src/account/interface.ts @@ -15,6 +15,7 @@ import { EstimateFeeAction, EstimateFeeDetails, EstimateFeeResponse, + EstimateFeeResponseBulk, InvocationsDetails, InvokeFunctionResponse, MultiDeployContractResponse, @@ -124,7 +125,7 @@ export abstract class AccountInterface extends ProviderInterface { public abstract estimateFeeBulk( transactions: TransactionBulk, estimateFeeDetails?: EstimateFeeDetails - ): Promise>; + ): Promise; /** * Invoke execute function in account contract diff --git a/src/provider/default.ts b/src/provider/default.ts index 9735ced6f..e5d77afb8 100644 --- a/src/provider/default.ts +++ b/src/provider/default.ts @@ -8,6 +8,7 @@ import { DeployAccountContractTransaction, DeployContractResponse, EstimateFeeResponse, + EstimateFeeResponseBulk, GetBlockResponse, GetCodeResponse, GetTransactionReceiptResponse, @@ -107,7 +108,7 @@ export class Provider implements ProviderInterface { public async getEstimateFeeBulk( invocations: InvocationBulk, blockIdentifier?: BlockIdentifier - ): Promise> { + ): Promise { return this.provider.getEstimateFeeBulk(invocations, blockIdentifier); } diff --git a/src/provider/interface.ts b/src/provider/interface.ts index 75d6db342..42d851c0b 100644 --- a/src/provider/interface.ts +++ b/src/provider/interface.ts @@ -9,6 +9,7 @@ import type { DeployAccountContractTransaction, DeployContractResponse, EstimateFeeResponse, + EstimateFeeResponseBulk, GetBlockResponse, GetCodeResponse, GetTransactionReceiptResponse, @@ -283,7 +284,7 @@ export abstract class ProviderInterface { public abstract getEstimateFeeBulk( invocations: InvocationBulk, blockIdentifier?: BlockIdentifier - ): Promise>; + ): Promise; /** * Wait for the transaction to be accepted diff --git a/src/provider/rpc.ts b/src/provider/rpc.ts index 5b6720b60..a46dd1d0f 100644 --- a/src/provider/rpc.ts +++ b/src/provider/rpc.ts @@ -7,6 +7,7 @@ import { DeployAccountContractTransaction, DeployContractResponse, EstimateFeeResponse, + EstimateFeeResponseBulk, GetBlockResponse, GetCodeResponse, GetTransactionResponse, @@ -312,7 +313,7 @@ export class RpcProvider implements ProviderInterface { public async getEstimateFeeBulk( _invocations: InvocationBulk, _blockIdentifier: BlockIdentifier = this.blockIdentifier - ): Promise> { + ): Promise { throw new Error('RPC does not implement getInvokeEstimateFeeBulk function'); } diff --git a/src/provider/sequencer.ts b/src/provider/sequencer.ts index d41391bf1..ae4279252 100644 --- a/src/provider/sequencer.ts +++ b/src/provider/sequencer.ts @@ -11,6 +11,7 @@ import { DeployAccountContractTransaction, DeployContractResponse, EstimateFeeResponse, + EstimateFeeResponseBulk, GetBlockResponse, GetContractAddressesResponse, GetTransactionReceiptResponse, @@ -442,7 +443,7 @@ export class SequencerProvider implements ProviderInterface { public async getEstimateFeeBulk( invocations: InvocationBulk, blockIdentifier: BlockIdentifier = this.blockIdentifier - ): Promise> { + ): Promise { const params: any = [].concat(invocations as []).map((invocation: any) => { let res; if (invocation.type === 'INVOKE_FUNCTION') { @@ -477,11 +478,9 @@ export class SequencerProvider implements ProviderInterface { }; }); - return this.fetchEndpoint('estimate_fee_bulk', { blockIdentifier }, params).then((result) => { - return [].concat(result as []).map((elem: any) => { - return this.responseParser.parseFeeEstimateResponse(elem); - }); - }); + return this.fetchEndpoint('estimate_fee_bulk', { blockIdentifier }, params).then( + this.responseParser.parseFeeEstimateBulkResponse + ); } public async getCode( diff --git a/src/types/api/sequencer.ts b/src/types/api/sequencer.ts index 222d414e6..6b32c2f3e 100644 --- a/src/types/api/sequencer.ts +++ b/src/types/api/sequencer.ts @@ -241,6 +241,10 @@ export namespace Sequencer { export type SimulateTransaction = Omit; + export type EstimateFeeRequestBulk = AllowArray< + InvokeEstimateFee | DeclareEstimateFee | DeployEstimateFee | DeployAccountEstimateFee + >; + // Support 0.9.1 changes in a backward-compatible way export type EstimateFeeResponse = | { @@ -254,6 +258,8 @@ export namespace Sequencer { unit: string; }; + export type EstimateFeeResponseBulk = AllowArray; + export type Endpoints = { get_contract_addresses: { QUERY: never; @@ -385,8 +391,8 @@ export namespace Sequencer { QUERY: { blockIdentifier: BlockIdentifier; }; - REQUEST: any; - RESPONSE: AllowArray; + REQUEST: EstimateFeeRequestBulk; + RESPONSE: EstimateFeeResponseBulk; }; }; } diff --git a/src/types/lib.ts b/src/types/lib.ts index 98f9246bc..98265c68e 100644 --- a/src/types/lib.ts +++ b/src/types/lib.ts @@ -78,16 +78,19 @@ export type InvocationsDetails = { export type InvocationsDetailsWithNonce = InvocationsDetails & { nonce: BigNumberish }; export type TransactionBulk = Array< - { type: TransactionType } & { - payload: DeclareContractPayload | AllowArray | DeployAccountContractPayload; - } + | ({ type: 'DECLARE' } & { payload: DeclareContractPayload }) + | ({ type: 'DEPLOY' } & { + payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[]; + }) + | ({ type: 'DEPLOY_ACCOUNT' } & { payload: DeployAccountContractPayload }) + | ({ type: 'INVOKE_FUNCTION' } & { payload: AllowArray }) >; export type InvocationBulk = Array< - { type: TransactionType } & ( - | Invocation - | DeclareContractTransaction - | DeployAccountContractTransaction + ( + | ({ type: 'DECLARE' } & DeclareContractTransaction) + | ({ type: 'DEPLOY_ACCOUNT' } & DeployAccountContractTransaction) + | ({ type: 'INVOKE_FUNCTION' } & Invocation) ) & InvocationsDetailsWithNonce & { blockIdentifier: BlockNumber | BigNumberish } >; diff --git a/src/types/provider.ts b/src/types/provider.ts index ae904e056..ffdd7fac0 100644 --- a/src/types/provider.ts +++ b/src/types/provider.ts @@ -132,3 +132,5 @@ export type EstimateFeeAction = type: 'DEPLOY'; payload: UniversalDeployerContractPayload; }; + +export type EstimateFeeResponseBulk = Array; diff --git a/src/utils/responseParser/sequencer.ts b/src/utils/responseParser/sequencer.ts index ebd6ba083..d6e774220 100644 --- a/src/utils/responseParser/sequencer.ts +++ b/src/utils/responseParser/sequencer.ts @@ -7,6 +7,7 @@ import { DeclareContractResponse, DeployContractResponse, EstimateFeeResponse, + EstimateFeeResponseBulk, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse, @@ -101,6 +102,33 @@ export class SequencerAPIResponseParser extends ResponseParser { }; } + public parseFeeEstimateBulkResponse( + res: Sequencer.EstimateFeeResponseBulk + ): EstimateFeeResponseBulk { + return [].concat(res as []).map((item: Sequencer.EstimateFeeResponse) => { + if ('overall_fee' in item) { + let gasInfo = {}; + + try { + gasInfo = { + gas_consumed: toBN(item.gas_usage), + gas_price: toBN(item.gas_price), + }; + } catch { + // do nothing + } + + return { + overall_fee: toBN(item.overall_fee), + ...gasInfo, + }; + } + return { + overall_fee: toBN(item.amount), + }; + }); + } + public parseCallContractResponse(res: Sequencer.CallContractResponse): CallContractResponse { return { result: res.result,