Skip to content

Commit

Permalink
fix(account): adding overhead on estimateFee for maxFee
Browse files Browse the repository at this point in the history
  • Loading branch information
MilGard91 committed Apr 7, 2022
1 parent 2ae36b2 commit ec52f61
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
transactionVersion,
} from '../utils/hash';
import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
import { compileCalldata } from '../utils/stark';
import { compileCalldata, estimatedFeeToMaxFee } from '../utils/stark';
import { fromCallsToExecuteCalldata } from '../utils/transaction';
import { TypedData, getMessageHash } from '../utils/typedData';
import { AccountInterface } from './interface';
Expand Down Expand Up @@ -95,8 +95,13 @@ export class Account extends Provider implements AccountInterface {
): Promise<AddTransactionResponse> {
const transactions = Array.isArray(calls) ? calls : [calls];
const nonce = toBN(transactionsDetail.nonce ?? (await this.getNonce()));
const maxFee =
transactionsDetail.maxFee ?? (await this.estimateFee(transactions, { nonce })).amount;
let maxFee: BigNumberish = '0';
if (transactionsDetail.maxFee) {
maxFee = transactionsDetail.maxFee;
} else {
const estimatedFee = (await this.estimateFee(transactions, { nonce })).amount;
maxFee = estimatedFeeToMaxFee(estimatedFee).toString();
}
const signerDetails = {
walletAddress: this.address,
nonce,
Expand Down
8 changes: 7 additions & 1 deletion src/utils/stark.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import BN, { isBN } from 'bn.js';
import { gzip } from 'pako';

import { Calldata, CompressedProgram, Program, RawArgs, Signature } from '../types';
import { genKeyPair, getStarkKey } from './ellipticCurve';
import { addHexPrefix, btoaUniversal } from './encode';
import { stringify } from './json';
import { toBN } from './number';
import { BigNumberish, toBN } from './number';

/**
* Function to compress compiled cairo program
Expand Down Expand Up @@ -48,3 +49,8 @@ export function compileCalldata(args: RawArgs): Calldata {
return toBN(value).toString();
});
}

export function estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: number = 0.15): BN {
const fee = isBN(estimatedFee) ? estimatedFee : toBN(estimatedFee);
return fee.mul(toBN(overhead).add(toBN(1)));
}

0 comments on commit ec52f61

Please sign in to comment.