Skip to content

Commit

Permalink
feat: add suggestedMaxFee
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed May 24, 2022
1 parent 4c06260 commit 8977772
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
20 changes: 14 additions & 6 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {
Abi,
AddTransactionResponse,
Call,
EstimateFeeResponse,
InvocationsDetails,
InvocationsSignerDetails,
InvokeFunctionTransaction,
KeyPair,
Signature,
Transaction,
} from '../types';
import { EstimateFee } from '../types/account';
import { sign } from '../utils/ellipticCurve';
import {
computeHashOnElements,
Expand Down Expand Up @@ -60,7 +60,7 @@ export class Account extends Provider implements AccountInterface {
nonce: providedNonce,
blockIdentifier = 'pending',
}: { nonce?: BigNumberish; blockIdentifier?: BlockIdentifier } = {}
): Promise<EstimateFeeResponse> {
): Promise<EstimateFee> {
const transactions = Array.isArray(calls) ? calls : [calls];
const nonce = providedNonce ?? (await this.getNonce());
const version = toBN(feeTransactionVersion);
Expand All @@ -76,7 +76,7 @@ export class Account extends Provider implements AccountInterface {
const signature = await this.signer.signTransaction(transactions, signerDetails);

const calldata = fromCallsToExecuteCalldataWithNonce(transactions, nonce);
return this.fetchEndpoint(
const fetchedEstimate = await this.fetchEndpoint(
'estimate_fee',
{ blockIdentifier },
{
Expand All @@ -87,14 +87,22 @@ export class Account extends Provider implements AccountInterface {
signature: bigNumberishArrayToDecimalStringArray(signature),
}
);
const suggestedMaxFee = estimatedFeeToMaxFee(fetchedEstimate.amount);

return {
...fetchedEstimate,
suggestedMaxFee,
};
}

/**
* Invoke execute function in account contract
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
*
* @param transaction - transaction to be invoked
* @param calls - one or more calls to be executed
* @param abis - one or more abis which can be used to display the calls
* @param transactionsDetail - optional transaction details
* @returns a confirmation of invoking a function on the starknet contract
*/
public async execute(
Expand All @@ -108,8 +116,8 @@ export class Account extends Provider implements AccountInterface {
if (transactionsDetail.maxFee || transactionsDetail.maxFee === 0) {
maxFee = transactionsDetail.maxFee;
} else {
const estimatedFee = (await this.estimateFee(transactions, { nonce })).amount;
maxFee = estimatedFeeToMaxFee(estimatedFee).toString();
const { suggestedMaxFee } = await this.estimateFee(transactions, { nonce });
maxFee = suggestedMaxFee.toString();
}

const signerDetails: InvocationsSignerDetails = {
Expand Down
4 changes: 2 additions & 2 deletions src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
AddTransactionResponse,
Call,
DeployContractPayload,
EstimateFeeResponse,
Invocation,
InvocationsDetails,
Signature,
} from '../types';
import { EstimateFee } from '../types/account';
import { BigNumberish } from '../utils/number';
import { TypedData } from '../utils/typedData/types';

Expand Down Expand Up @@ -44,7 +44,7 @@ export abstract class AccountInterface extends ProviderInterface {
*
* @returns response from addTransaction
*/
public abstract estimateFee(invocation: Invocation): Promise<EstimateFeeResponse>;
public abstract estimateFee(invocation: Invocation): Promise<EstimateFee>;

/**
* Invoke execute function in account contract
Expand Down
7 changes: 7 additions & 0 deletions src/types/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import BN from 'bn.js';

import { EstimateFeeResponse } from './api';

export interface EstimateFee extends EstimateFeeResponse {
suggestedMaxFee: BN;
}
2 changes: 1 addition & 1 deletion src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export type Abi = Array<FunctionAbi | StructAbi>;

export type EntryPointsByType = object;
export type Program = Record<any, any>;
export type BlockNumber = 'pending' | null | number;
export type BlockNumber = 'pending' | 'latest' | null | number;

export type CompiledContract = {
abi: Abi;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/stark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function compileCalldata(args: RawArgs): Calldata {
});
}

export function estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: number = 0.15): BN {
export function estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: number = 0.5): BN {
// BN can only handle Integers, so we need to do all calulations with integers
const overHeadPercent = Math.round((1 + overhead) * 100);
return toBN(estimatedFee).mul(toBN(overHeadPercent)).div(toBN(100));
Expand Down

0 comments on commit 8977772

Please sign in to comment.