Skip to content

Commit

Permalink
fix: update types & add separate responseParser for estimateFeeBulk
Browse files Browse the repository at this point in the history
  • Loading branch information
irisdv committed Jan 9, 2023
1 parent 792b2dc commit 52110b2
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 24 deletions.
11 changes: 6 additions & 5 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
EstimateFee,
EstimateFeeAction,
EstimateFeeDetails,
EstimateFeeResponseBulk,
Invocation,
InvocationsDetails,
InvocationsSignerDetails,
Expand Down Expand Up @@ -219,7 +220,7 @@ export class Account extends Provider implements AccountInterface {
public async estimateFeeBulk(
transactions: TransactionBulk,
{ nonce: providedNonce, blockIdentifier }: EstimateFeeDetails = {}
): Promise<Array<EstimateFee>> {
): Promise<EstimateFeeResponseBulk> {
const nonce = toBN(providedNonce ?? (await this.getNonce()));
const version = toBN(feeTransactionVersion);
const chainId = await this.getChainId();
Expand Down Expand Up @@ -293,7 +294,7 @@ export class Account extends Provider implements AccountInterface {
});
}

private async buildInvocation(
public async buildInvocation(
call: Array<Call>,
signerDetails: InvocationsSignerDetails
): Promise<Invocation> {
Expand Down Expand Up @@ -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<DeclareContractTransaction> {
Expand All @@ -578,7 +579,7 @@ export class Account extends Provider implements AccountInterface {
};
}

private async buildAccountDeployPayload(
public async buildAccountDeployPayload(
{
classHash,
addressSalt = 0,
Expand Down Expand Up @@ -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) => {
Expand Down
3 changes: 2 additions & 1 deletion src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
EstimateFeeAction,
EstimateFeeDetails,
EstimateFeeResponse,
EstimateFeeResponseBulk,
InvocationsDetails,
InvokeFunctionResponse,
MultiDeployContractResponse,
Expand Down Expand Up @@ -124,7 +125,7 @@ export abstract class AccountInterface extends ProviderInterface {
public abstract estimateFeeBulk(
transactions: TransactionBulk,
estimateFeeDetails?: EstimateFeeDetails
): Promise<AllowArray<EstimateFeeResponse>>;
): Promise<EstimateFeeResponseBulk>;

/**
* Invoke execute function in account contract
Expand Down
3 changes: 2 additions & 1 deletion src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DeployAccountContractTransaction,
DeployContractResponse,
EstimateFeeResponse,
EstimateFeeResponseBulk,
GetBlockResponse,
GetCodeResponse,
GetTransactionReceiptResponse,
Expand Down Expand Up @@ -107,7 +108,7 @@ export class Provider implements ProviderInterface {
public async getEstimateFeeBulk(
invocations: InvocationBulk,
blockIdentifier?: BlockIdentifier
): Promise<Array<EstimateFeeResponse>> {
): Promise<EstimateFeeResponseBulk> {
return this.provider.getEstimateFeeBulk(invocations, blockIdentifier);
}

Expand Down
3 changes: 2 additions & 1 deletion src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
DeployAccountContractTransaction,
DeployContractResponse,
EstimateFeeResponse,
EstimateFeeResponseBulk,
GetBlockResponse,
GetCodeResponse,
GetTransactionReceiptResponse,
Expand Down Expand Up @@ -283,7 +284,7 @@ export abstract class ProviderInterface {
public abstract getEstimateFeeBulk(
invocations: InvocationBulk,
blockIdentifier?: BlockIdentifier
): Promise<Array<EstimateFeeResponse>>;
): Promise<EstimateFeeResponseBulk>;

/**
* Wait for the transaction to be accepted
Expand Down
3 changes: 2 additions & 1 deletion src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DeployAccountContractTransaction,
DeployContractResponse,
EstimateFeeResponse,
EstimateFeeResponseBulk,
GetBlockResponse,
GetCodeResponse,
GetTransactionResponse,
Expand Down Expand Up @@ -312,7 +313,7 @@ export class RpcProvider implements ProviderInterface {
public async getEstimateFeeBulk(
_invocations: InvocationBulk,
_blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<Array<EstimateFeeResponse>> {
): Promise<EstimateFeeResponseBulk> {
throw new Error('RPC does not implement getInvokeEstimateFeeBulk function');
}

Expand Down
11 changes: 5 additions & 6 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DeployAccountContractTransaction,
DeployContractResponse,
EstimateFeeResponse,
EstimateFeeResponseBulk,
GetBlockResponse,
GetContractAddressesResponse,
GetTransactionReceiptResponse,
Expand Down Expand Up @@ -442,7 +443,7 @@ export class SequencerProvider implements ProviderInterface {
public async getEstimateFeeBulk(
invocations: InvocationBulk,
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<Array<EstimateFeeResponse>> {
): Promise<EstimateFeeResponseBulk> {
const params: any = [].concat(invocations as []).map((invocation: any) => {
let res;
if (invocation.type === 'INVOKE_FUNCTION') {
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 8 additions & 2 deletions src/types/api/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ export namespace Sequencer {

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

export type EstimateFeeRequestBulk = AllowArray<
InvokeEstimateFee | DeclareEstimateFee | DeployEstimateFee | DeployAccountEstimateFee
>;

// Support 0.9.1 changes in a backward-compatible way
export type EstimateFeeResponse =
| {
Expand All @@ -254,6 +258,8 @@ export namespace Sequencer {
unit: string;
};

export type EstimateFeeResponseBulk = AllowArray<EstimateFeeResponse>;

export type Endpoints = {
get_contract_addresses: {
QUERY: never;
Expand Down Expand Up @@ -385,8 +391,8 @@ export namespace Sequencer {
QUERY: {
blockIdentifier: BlockIdentifier;
};
REQUEST: any;
RESPONSE: AllowArray<EstimateFeeResponse>;
REQUEST: EstimateFeeRequestBulk;
RESPONSE: EstimateFeeResponseBulk;
};
};
}
17 changes: 10 additions & 7 deletions src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,19 @@ export type InvocationsDetails = {
export type InvocationsDetailsWithNonce = InvocationsDetails & { nonce: BigNumberish };

export type TransactionBulk = Array<
{ type: TransactionType } & {
payload: DeclareContractPayload | AllowArray<Call> | DeployAccountContractPayload;
}
| ({ type: 'DECLARE' } & { payload: DeclareContractPayload })
| ({ type: 'DEPLOY' } & {
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[];
})
| ({ type: 'DEPLOY_ACCOUNT' } & { payload: DeployAccountContractPayload })
| ({ type: 'INVOKE_FUNCTION' } & { payload: AllowArray<Call> })
>;

export type InvocationBulk = Array<
{ type: TransactionType } & (
| Invocation
| DeclareContractTransaction
| DeployAccountContractTransaction
(
| ({ type: 'DECLARE' } & DeclareContractTransaction)
| ({ type: 'DEPLOY_ACCOUNT' } & DeployAccountContractTransaction)
| ({ type: 'INVOKE_FUNCTION' } & Invocation)
) &
InvocationsDetailsWithNonce & { blockIdentifier: BlockNumber | BigNumberish }
>;
Expand Down
2 changes: 2 additions & 0 deletions src/types/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,5 @@ export type EstimateFeeAction =
type: 'DEPLOY';
payload: UniversalDeployerContractPayload;
};

export type EstimateFeeResponseBulk = Array<EstimateFeeResponse>;
28 changes: 28 additions & 0 deletions src/utils/responseParser/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DeclareContractResponse,
DeployContractResponse,
EstimateFeeResponse,
EstimateFeeResponseBulk,
GetBlockResponse,
GetTransactionReceiptResponse,
GetTransactionResponse,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 52110b2

Please sign in to comment.