Skip to content

Commit

Permalink
fix: allow multiple consecutive pending transactions in estimateFee
Browse files Browse the repository at this point in the history
  • Loading branch information
khanti42 committed Jul 12, 2024
1 parent e4c65e2 commit e9e0265
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
16 changes: 3 additions & 13 deletions packages/starknet-snap/src/estimateFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { getNetworkFromChainId } from './utils/snapUtils';
import {
getKeysFromAddress,
getCallDataArray,
estimateFee as estimateFeeUtil,
getAccContractAddressAndCallData,
estimateFeeBulk,
addFeesFromAllTransactions,
Expand Down Expand Up @@ -90,18 +89,9 @@ export async function estimateFee(params: ApiParams) {
];
}

let estimateFeeResp;

if (accountDeployed) {
// This condition branch will be removed later when starknet.js
// supports estimateFeeBulk in rpc mode
estimateFeeResp = await estimateFeeUtil(network, senderAddress, senderPrivateKey, txnInvocation);
logger.log(`estimateFee:\nestimateFeeUtil estimateFeeResp: ${toJson(estimateFeeResp)}`);
} else {
const estimateBulkFeeResp = await estimateFeeBulk(network, senderAddress, senderPrivateKey, bulkTransactions);
logger.log(`estimateFee:\nestimateFeeBulk estimateBulkFeeResp: ${toJson(estimateBulkFeeResp)}`);
estimateFeeResp = addFeesFromAllTransactions(estimateBulkFeeResp);
}
const estimateBulkFeeResp = await estimateFeeBulk(network, senderAddress, senderPrivateKey, bulkTransactions);
logger.log(`estimateFee:\nestimateFeeBulk estimateBulkFeeResp: ${toJson(estimateBulkFeeResp)}`);
const estimateFeeResp = addFeesFromAllTransactions(estimateBulkFeeResp);

logger.log(`estimateFee:\nestimateFeeResp: ${toJson(estimateFeeResp)}`);

Expand Down
36 changes: 29 additions & 7 deletions packages/starknet-snap/src/utils/starknetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ export const declareContract = async (
});
};

export const getAccountNonce = async (
network: Network,
senderAddress: string,
privateKey: string | Uint8Array,
cairoVersion?: CairoVersion,
): Promise<BigNumberish> => {
const nonceString = await getAccountInstance(network, senderAddress, privateKey, cairoVersion).getNonce('latest');
return parseInt(nonceString.toLowerCase(), 16);
};

export const estimateFee = async (
network: Network,
senderAddress: string,
Expand All @@ -139,11 +149,22 @@ export const estimateFee = async (
cairoVersion?: CairoVersion,
invocationsDetails?: UniversalDetails,
): Promise<EstimateFee> => {
return getAccountInstance(network, senderAddress, privateKey, cairoVersion).estimateInvokeFee(txnInvocation, {
...invocationsDetails,
skipValidate: false,
blockIdentifier: 'latest',
});
try {
return await getAccountInstance(network, senderAddress, privateKey, cairoVersion).estimateInvokeFee(txnInvocation, {
...invocationsDetails,
skipValidate: false,
blockIdentifier: 'latest',
});
} catch (e) {
// [Todo only if nonce error]
const nonce = await getAccountNonce(network, senderAddress, privateKey);
return await getAccountInstance(network, senderAddress, privateKey, cairoVersion).estimateInvokeFee(txnInvocation, {
...invocationsDetails,
nonce,
skipValidate: false,
blockIdentifier: 'latest',
});
}
};

export const estimateFeeBulk = async (
Expand All @@ -154,7 +175,7 @@ export const estimateFeeBulk = async (
invocationsDetails?: UniversalDetails,
cairoVersion?: CairoVersion,
): Promise<EstimateFee[]> => {
return getAccountInstance(network, senderAddress, privateKey, cairoVersion).estimateFeeBulk(txnInvocation, {
return await getAccountInstance(network, senderAddress, privateKey, cairoVersion).estimateFeeBulk(txnInvocation, {
...invocationsDetails,
skipValidate: false,
blockIdentifier: 'latest',
Expand Down Expand Up @@ -902,7 +923,8 @@ export const getStarkNameUtil = async (network: Network, userAddress: string) =>
export const validateAccountRequireUpgradeOrDeploy = async (network: Network, address: string, pubKey: string) => {
if (await isUpgradeRequired(network, address)) {
throw new UpgradeRequiredError('Upgrade required');
} else if (!(await isDeployRequired(network, address, pubKey))) {
}
if (await isDeployRequired(network, address, pubKey)) {
throw new DeployRequiredError(`Cairo 0 contract address ${address} balance is not empty, deploy required`);
}
};
17 changes: 3 additions & 14 deletions packages/starknet-snap/test/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@ import chai, { expect } from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import { WalletMock } from '../wallet.mock.test';
import { getValue } from '../../src/getValue';
import {
createAccountProxyTxn,
testnetAccAddresses,
testnetPublicKeys,
mainnetPublicKeys,
mainnetAccAddresses,
invalidNetwork as INVALID_NETWORK,
getBip44EntropyStub,
account1,
} from '../constants.test';
import { getBip44EntropyStub, account1 } from '../constants.test';
import { SnapState } from '../../src/types/snapState';
import {
ETHER_MAINNET,
ETHER_SEPOLIA_TESTNET,
STARKNET_MAINNET_NETWORK,
STARKNET_SEPOLIA_TESTNET_NETWORK,
} from '../../src/utils/constants';
import { Mutex } from 'async-mutex';
import * as snapUtils from '../../src/utils/snapUtils';

import * as starknetUtils from '../../src/utils/starknetUtils';
import { onHomePage } from '../../src';

Expand All @@ -31,7 +20,7 @@ const sandbox = sinon.createSandbox();
describe('Test function: onHomePage', function () {
const walletStub = new WalletMock();
// eslint-disable-next-line no-restricted-globals
const globalAny: any = global;
const globalAny = global as unknown as { snap: WalletMock };
const state: SnapState = {
accContracts: [],
erc20Tokens: [ETHER_MAINNET, ETHER_SEPOLIA_TESTNET],
Expand Down

0 comments on commit e9e0265

Please sign in to comment.