Skip to content

Commit 8977772

Browse files
committed
feat: add suggestedMaxFee
1 parent 4c06260 commit 8977772

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

src/account/default.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import {
88
Abi,
99
AddTransactionResponse,
1010
Call,
11-
EstimateFeeResponse,
1211
InvocationsDetails,
1312
InvocationsSignerDetails,
1413
InvokeFunctionTransaction,
1514
KeyPair,
1615
Signature,
1716
Transaction,
1817
} from '../types';
18+
import { EstimateFee } from '../types/account';
1919
import { sign } from '../utils/ellipticCurve';
2020
import {
2121
computeHashOnElements,
@@ -60,7 +60,7 @@ export class Account extends Provider implements AccountInterface {
6060
nonce: providedNonce,
6161
blockIdentifier = 'pending',
6262
}: { nonce?: BigNumberish; blockIdentifier?: BlockIdentifier } = {}
63-
): Promise<EstimateFeeResponse> {
63+
): Promise<EstimateFee> {
6464
const transactions = Array.isArray(calls) ? calls : [calls];
6565
const nonce = providedNonce ?? (await this.getNonce());
6666
const version = toBN(feeTransactionVersion);
@@ -76,7 +76,7 @@ export class Account extends Provider implements AccountInterface {
7676
const signature = await this.signer.signTransaction(transactions, signerDetails);
7777

7878
const calldata = fromCallsToExecuteCalldataWithNonce(transactions, nonce);
79-
return this.fetchEndpoint(
79+
const fetchedEstimate = await this.fetchEndpoint(
8080
'estimate_fee',
8181
{ blockIdentifier },
8282
{
@@ -87,14 +87,22 @@ export class Account extends Provider implements AccountInterface {
8787
signature: bigNumberishArrayToDecimalStringArray(signature),
8888
}
8989
);
90+
const suggestedMaxFee = estimatedFeeToMaxFee(fetchedEstimate.amount);
91+
92+
return {
93+
...fetchedEstimate,
94+
suggestedMaxFee,
95+
};
9096
}
9197

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

115123
const signerDetails: InvocationsSignerDetails = {

src/account/interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {
55
AddTransactionResponse,
66
Call,
77
DeployContractPayload,
8-
EstimateFeeResponse,
98
Invocation,
109
InvocationsDetails,
1110
Signature,
1211
} from '../types';
12+
import { EstimateFee } from '../types/account';
1313
import { BigNumberish } from '../utils/number';
1414
import { TypedData } from '../utils/typedData/types';
1515

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

4949
/**
5050
* Invoke execute function in account contract

src/types/account.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import BN from 'bn.js';
2+
3+
import { EstimateFeeResponse } from './api';
4+
5+
export interface EstimateFee extends EstimateFeeResponse {
6+
suggestedMaxFee: BN;
7+
}

src/types/lib.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export type Abi = Array<FunctionAbi | StructAbi>;
6060

6161
export type EntryPointsByType = object;
6262
export type Program = Record<any, any>;
63-
export type BlockNumber = 'pending' | null | number;
63+
export type BlockNumber = 'pending' | 'latest' | null | number;
6464

6565
export type CompiledContract = {
6666
abi: Abi;

src/utils/stark.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function compileCalldata(args: RawArgs): Calldata {
5050
});
5151
}
5252

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

0 commit comments

Comments
 (0)