Skip to content

Commit

Permalink
fix: seperate types and fix fees
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Jul 15, 2022
1 parent 8bcfddf commit 577a836
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 32 deletions.
12 changes: 4 additions & 8 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { SignerInterface } from '../signer';
import {
Abi,
Call,
EstimateFee,
EstimateFeeDetails,
EstimateFeeResponse,
InvocationsDetails,
InvokeFunctionResponse,
Signature,
Expand All @@ -31,7 +31,7 @@ export abstract class AccountInterface extends ProviderInterface {
public abstract estimateFee(
calls: Call | Call[],
estimateFeeDetails?: EstimateFeeDetails
): Promise<EstimateFee>;
): Promise<EstimateFeeResponse>;

/**
* Invoke execute function in account contract
Expand Down
2 changes: 1 addition & 1 deletion src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
5 changes: 2 additions & 3 deletions src/types/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
17 changes: 10 additions & 7 deletions src/types/api/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
3 changes: 2 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
9 changes: 5 additions & 4 deletions src/types/provider.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/responseParser/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
GetTransactionReceiptResponse,
GetTransactionResponse,
InvokeFunctionResponse,
RPC,
} from '../../types';
import { RPC } from '../../types/api';
import { toBN } from '../number';
import { ResponseParser } from '.';

Expand Down
19 changes: 18 additions & 1 deletion src/utils/responseParser/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
GetTransactionReceiptResponse,
GetTransactionResponse,
InvokeFunctionResponse,
Sequencer,
} from '../../types';
import { Sequencer } from '../../types/api';
import { toBN } from '../number';
import { ResponseParser } from '.';

Expand Down Expand Up @@ -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),
};
Expand Down

0 comments on commit 577a836

Please sign in to comment.