Skip to content

Commit 52110b2

Browse files
committed
fix: update types & add separate responseParser for estimateFeeBulk
1 parent 792b2dc commit 52110b2

File tree

10 files changed

+67
-24
lines changed

10 files changed

+67
-24
lines changed

src/account/default.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
EstimateFee,
2222
EstimateFeeAction,
2323
EstimateFeeDetails,
24+
EstimateFeeResponseBulk,
2425
Invocation,
2526
InvocationsDetails,
2627
InvocationsSignerDetails,
@@ -219,7 +220,7 @@ export class Account extends Provider implements AccountInterface {
219220
public async estimateFeeBulk(
220221
transactions: TransactionBulk,
221222
{ nonce: providedNonce, blockIdentifier }: EstimateFeeDetails = {}
222-
): Promise<Array<EstimateFee>> {
223+
): Promise<EstimateFeeResponseBulk> {
223224
const nonce = toBN(providedNonce ?? (await this.getNonce()));
224225
const version = toBN(feeTransactionVersion);
225226
const chainId = await this.getChainId();
@@ -293,7 +294,7 @@ export class Account extends Provider implements AccountInterface {
293294
});
294295
}
295296

296-
private async buildInvocation(
297+
public async buildInvocation(
297298
call: Array<Call>,
298299
signerDetails: InvocationsSignerDetails
299300
): Promise<Invocation> {
@@ -557,7 +558,7 @@ export class Account extends Provider implements AccountInterface {
557558
return feeEstimate.suggestedMaxFee.toString();
558559
}
559560

560-
private async buildDeclarePayload(
561+
public async buildDeclarePayload(
561562
{ classHash, contract }: DeclareContractPayload,
562563
{ nonce, chainId, version, walletAddress, maxFee }: InvocationsSignerDetails
563564
): Promise<DeclareContractTransaction> {
@@ -578,7 +579,7 @@ export class Account extends Provider implements AccountInterface {
578579
};
579580
}
580581

581-
private async buildAccountDeployPayload(
582+
public async buildAccountDeployPayload(
582583
{
583584
classHash,
584585
addressSalt = 0,
@@ -610,7 +611,7 @@ export class Account extends Provider implements AccountInterface {
610611
};
611612
}
612613

613-
private buildUDCContractPayload(
614+
public buildUDCContractPayload(
614615
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[]
615616
): Call[] {
616617
const calls = [].concat(payload as []).map((it) => {

src/account/interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
EstimateFeeAction,
1616
EstimateFeeDetails,
1717
EstimateFeeResponse,
18+
EstimateFeeResponseBulk,
1819
InvocationsDetails,
1920
InvokeFunctionResponse,
2021
MultiDeployContractResponse,
@@ -124,7 +125,7 @@ export abstract class AccountInterface extends ProviderInterface {
124125
public abstract estimateFeeBulk(
125126
transactions: TransactionBulk,
126127
estimateFeeDetails?: EstimateFeeDetails
127-
): Promise<AllowArray<EstimateFeeResponse>>;
128+
): Promise<EstimateFeeResponseBulk>;
128129

129130
/**
130131
* Invoke execute function in account contract

src/provider/default.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
DeployAccountContractTransaction,
99
DeployContractResponse,
1010
EstimateFeeResponse,
11+
EstimateFeeResponseBulk,
1112
GetBlockResponse,
1213
GetCodeResponse,
1314
GetTransactionReceiptResponse,
@@ -107,7 +108,7 @@ export class Provider implements ProviderInterface {
107108
public async getEstimateFeeBulk(
108109
invocations: InvocationBulk,
109110
blockIdentifier?: BlockIdentifier
110-
): Promise<Array<EstimateFeeResponse>> {
111+
): Promise<EstimateFeeResponseBulk> {
111112
return this.provider.getEstimateFeeBulk(invocations, blockIdentifier);
112113
}
113114

src/provider/interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
DeployAccountContractTransaction,
1010
DeployContractResponse,
1111
EstimateFeeResponse,
12+
EstimateFeeResponseBulk,
1213
GetBlockResponse,
1314
GetCodeResponse,
1415
GetTransactionReceiptResponse,
@@ -283,7 +284,7 @@ export abstract class ProviderInterface {
283284
public abstract getEstimateFeeBulk(
284285
invocations: InvocationBulk,
285286
blockIdentifier?: BlockIdentifier
286-
): Promise<Array<EstimateFeeResponse>>;
287+
): Promise<EstimateFeeResponseBulk>;
287288

288289
/**
289290
* Wait for the transaction to be accepted

src/provider/rpc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
DeployAccountContractTransaction,
88
DeployContractResponse,
99
EstimateFeeResponse,
10+
EstimateFeeResponseBulk,
1011
GetBlockResponse,
1112
GetCodeResponse,
1213
GetTransactionResponse,
@@ -312,7 +313,7 @@ export class RpcProvider implements ProviderInterface {
312313
public async getEstimateFeeBulk(
313314
_invocations: InvocationBulk,
314315
_blockIdentifier: BlockIdentifier = this.blockIdentifier
315-
): Promise<Array<EstimateFeeResponse>> {
316+
): Promise<EstimateFeeResponseBulk> {
316317
throw new Error('RPC does not implement getInvokeEstimateFeeBulk function');
317318
}
318319

src/provider/sequencer.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
DeployAccountContractTransaction,
1212
DeployContractResponse,
1313
EstimateFeeResponse,
14+
EstimateFeeResponseBulk,
1415
GetBlockResponse,
1516
GetContractAddressesResponse,
1617
GetTransactionReceiptResponse,
@@ -442,7 +443,7 @@ export class SequencerProvider implements ProviderInterface {
442443
public async getEstimateFeeBulk(
443444
invocations: InvocationBulk,
444445
blockIdentifier: BlockIdentifier = this.blockIdentifier
445-
): Promise<Array<EstimateFeeResponse>> {
446+
): Promise<EstimateFeeResponseBulk> {
446447
const params: any = [].concat(invocations as []).map((invocation: any) => {
447448
let res;
448449
if (invocation.type === 'INVOKE_FUNCTION') {
@@ -477,11 +478,9 @@ export class SequencerProvider implements ProviderInterface {
477478
};
478479
});
479480

480-
return this.fetchEndpoint('estimate_fee_bulk', { blockIdentifier }, params).then((result) => {
481-
return [].concat(result as []).map((elem: any) => {
482-
return this.responseParser.parseFeeEstimateResponse(elem);
483-
});
484-
});
481+
return this.fetchEndpoint('estimate_fee_bulk', { blockIdentifier }, params).then(
482+
this.responseParser.parseFeeEstimateBulkResponse
483+
);
485484
}
486485

487486
public async getCode(

src/types/api/sequencer.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ export namespace Sequencer {
241241

242242
export type SimulateTransaction = Omit<InvokeFunctionTransaction, 'max_fee' | 'entry_point_type'>;
243243

244+
export type EstimateFeeRequestBulk = AllowArray<
245+
InvokeEstimateFee | DeclareEstimateFee | DeployEstimateFee | DeployAccountEstimateFee
246+
>;
247+
244248
// Support 0.9.1 changes in a backward-compatible way
245249
export type EstimateFeeResponse =
246250
| {
@@ -254,6 +258,8 @@ export namespace Sequencer {
254258
unit: string;
255259
};
256260

261+
export type EstimateFeeResponseBulk = AllowArray<EstimateFeeResponse>;
262+
257263
export type Endpoints = {
258264
get_contract_addresses: {
259265
QUERY: never;
@@ -385,8 +391,8 @@ export namespace Sequencer {
385391
QUERY: {
386392
blockIdentifier: BlockIdentifier;
387393
};
388-
REQUEST: any;
389-
RESPONSE: AllowArray<EstimateFeeResponse>;
394+
REQUEST: EstimateFeeRequestBulk;
395+
RESPONSE: EstimateFeeResponseBulk;
390396
};
391397
};
392398
}

src/types/lib.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,19 @@ export type InvocationsDetails = {
7878
export type InvocationsDetailsWithNonce = InvocationsDetails & { nonce: BigNumberish };
7979

8080
export type TransactionBulk = Array<
81-
{ type: TransactionType } & {
82-
payload: DeclareContractPayload | AllowArray<Call> | DeployAccountContractPayload;
83-
}
81+
| ({ type: 'DECLARE' } & { payload: DeclareContractPayload })
82+
| ({ type: 'DEPLOY' } & {
83+
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[];
84+
})
85+
| ({ type: 'DEPLOY_ACCOUNT' } & { payload: DeployAccountContractPayload })
86+
| ({ type: 'INVOKE_FUNCTION' } & { payload: AllowArray<Call> })
8487
>;
8588

8689
export type InvocationBulk = Array<
87-
{ type: TransactionType } & (
88-
| Invocation
89-
| DeclareContractTransaction
90-
| DeployAccountContractTransaction
90+
(
91+
| ({ type: 'DECLARE' } & DeclareContractTransaction)
92+
| ({ type: 'DEPLOY_ACCOUNT' } & DeployAccountContractTransaction)
93+
| ({ type: 'INVOKE_FUNCTION' } & Invocation)
9194
) &
9295
InvocationsDetailsWithNonce & { blockIdentifier: BlockNumber | BigNumberish }
9396
>;

src/types/provider.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,5 @@ export type EstimateFeeAction =
132132
type: 'DEPLOY';
133133
payload: UniversalDeployerContractPayload;
134134
};
135+
136+
export type EstimateFeeResponseBulk = Array<EstimateFeeResponse>;

src/utils/responseParser/sequencer.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
DeclareContractResponse,
88
DeployContractResponse,
99
EstimateFeeResponse,
10+
EstimateFeeResponseBulk,
1011
GetBlockResponse,
1112
GetTransactionReceiptResponse,
1213
GetTransactionResponse,
@@ -101,6 +102,33 @@ export class SequencerAPIResponseParser extends ResponseParser {
101102
};
102103
}
103104

105+
public parseFeeEstimateBulkResponse(
106+
res: Sequencer.EstimateFeeResponseBulk
107+
): EstimateFeeResponseBulk {
108+
return [].concat(res as []).map((item: Sequencer.EstimateFeeResponse) => {
109+
if ('overall_fee' in item) {
110+
let gasInfo = {};
111+
112+
try {
113+
gasInfo = {
114+
gas_consumed: toBN(item.gas_usage),
115+
gas_price: toBN(item.gas_price),
116+
};
117+
} catch {
118+
// do nothing
119+
}
120+
121+
return {
122+
overall_fee: toBN(item.overall_fee),
123+
...gasInfo,
124+
};
125+
}
126+
return {
127+
overall_fee: toBN(item.amount),
128+
};
129+
});
130+
}
131+
104132
public parseCallContractResponse(res: Sequencer.CallContractResponse): CallContractResponse {
105133
return {
106134
result: res.result,

0 commit comments

Comments
 (0)