Skip to content

Commit

Permalink
chore: refine unit test for starknetUtils method - getEstimatedFees (
Browse files Browse the repository at this point in the history
…#338)

* chore: refine test for getEstimatedFees

* chore: refine

* chore: refine the test
  • Loading branch information
stanleyyconsensys authored Sep 3, 2024
1 parent 98c0224 commit 3acbacf
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 72 deletions.
5 changes: 0 additions & 5 deletions packages/starknet-snap/src/utils/rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ describe('validateRequest', () => {
expect(() =>
validateRequest(requestParams, validateStruct as unknown as Struct),
).toThrow(InvalidParamsError);
expect(() =>
validateRequest(requestParams, validateStruct as unknown as Struct),
).toThrow(
'At path: signerAddress -- Expected a string, but received: 1234',
);
});
});

Expand Down
139 changes: 72 additions & 67 deletions packages/starknet-snap/src/utils/starknetUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import { constants, TransactionType } from 'starknet';
import { mockAccount, prepareMockAccount } from '../rpcs/__tests__/helper';
import { FeeTokenUnit } from '../types/snapApi';
import type { SnapState } from '../types/snapState';
import {
ACCOUNT_CLASS_HASH,
STARKNET_SEPOLIA_TESTNET_NETWORK,
} from './constants';
import type { TransactionVersion } from '../types/starknet';
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from './constants';
import * as starknetUtils from './starknetUtils';

describe('getEstimatedFees', () => {
Expand Down Expand Up @@ -49,7 +47,7 @@ describe('getEstimatedFees', () => {
suggestedMaxFee,
// eslint-disable-next-line @typescript-eslint/naming-convention
gas_price: BigInt('0x0'),
} as EstimateFee;
} as unknown as EstimateFee;
const estimateBulkFeeSpy = jest.spyOn(starknetUtils, 'estimateFeeBulk');
estimateBulkFeeSpy.mockResolvedValue([estimateFeeResp]);

Expand All @@ -62,39 +60,71 @@ describe('getEstimatedFees', () => {
};

it.each([
constants.TRANSACTION_VERSION.V1,
constants.TRANSACTION_VERSION.V2,
constants.TRANSACTION_VERSION.V3,
])('estimate fees for transaction version %s', async (transactionVersion) => {
const deployed = true;
const { account, invocations } = await prepareSpy(deployed);

const resp = await starknetUtils.getEstimatedFees(
STARKNET_SEPOLIA_TESTNET_NETWORK,
account.address,
account.privateKey,
account.publicKey,
invocations,
{
version: transactionVersion,
},
);

expect(resp).toStrictEqual({
suggestedMaxFee: suggestedMaxFee.toString(10),
overallFee: overallFee.toString(10),
unit:
transactionVersion === constants.TRANSACTION_VERSION.V3
? FeeTokenUnit.STRK
: FeeTokenUnit.ETH,
includeDeploy: !deployed,
});
});

it('estimate fees including deployment if the account is not deployed', async () => {
const deployed = false;
{
txVersion: constants.TRANSACTION_VERSION.V2,
expectedUnit: FeeTokenUnit.ETH,
},
{
txVersion: constants.TRANSACTION_VERSION.V3,
expectedUnit: FeeTokenUnit.STRK,
},
{
txVersion: undefined,
expectedUnit: FeeTokenUnit.ETH,
},
])(
'estimates fees correctly and assigns `$expectedUnit` to the unit of the result if the transaction version is $version',
async ({
txVersion,
expectedUnit,
}: {
txVersion?: TransactionVersion;
expectedUnit: FeeTokenUnit;
}) => {
const network = STARKNET_SEPOLIA_TESTNET_NETWORK;
const { account, invocations, estimateBulkFeeSpy } = await prepareSpy(
true,
);
const call = invocations[0];

const resp = await starknetUtils.getEstimatedFees(
network,
account.address,
account.privateKey,
account.publicKey,
[call],
{
version: txVersion,
},
);

expect(estimateBulkFeeSpy).toHaveBeenCalledWith(
network,
account.address,
account.privateKey,
[
{
payload: (call as any).payload,
type: TransactionType.INVOKE,
},
],
{
version: txVersion, // to verify if the version is passed to the estimateFeeBulk correctly
},
);
expect(resp).toStrictEqual({
suggestedMaxFee: suggestedMaxFee.toString(10),
overallFee: overallFee.toString(10),
unit: expectedUnit, // to verify if the unit is return correctly
includeDeploy: false,
});
},
);

it('estimates fees with account deploy payload if the account is not deployed', async () => {
const network = STARKNET_SEPOLIA_TESTNET_NETWORK;
const { account, estimateBulkFeeSpy, invocations } = await prepareSpy(
deployed,
false,
);
const deployAccountpayload = starknetUtils.createAccountDeployPayload(
account.address,
Expand All @@ -103,29 +133,24 @@ describe('getEstimatedFees', () => {
const call = invocations[0];

const resp = await starknetUtils.getEstimatedFees(
STARKNET_SEPOLIA_TESTNET_NETWORK,
network,
account.address,
account.privateKey,
account.publicKey,
[call],
);

expect(estimateBulkFeeSpy).toHaveBeenCalledWith(
STARKNET_SEPOLIA_TESTNET_NETWORK,
network,
account.address,
account.privateKey,
[
{
payload: {
addressSalt: account.addressSalt,
classHash: ACCOUNT_CLASS_HASH,
constructorCalldata: deployAccountpayload.constructorCalldata,
contractAddress: deployAccountpayload.contractAddress,
},
payload: deployAccountpayload,
type: TransactionType.DEPLOY_ACCOUNT,
},
{
payload: (invocations[0] as any).payload,
payload: (call as any).payload,
type: TransactionType.INVOKE,
},
],
Expand All @@ -135,27 +160,7 @@ describe('getEstimatedFees', () => {
suggestedMaxFee: suggestedMaxFee.toString(10),
overallFee: overallFee.toString(10),
unit: FeeTokenUnit.ETH,
includeDeploy: !deployed,
});
});

it('estimate fees without transaction version', async () => {
const deployed = true;
const { account, invocations } = await prepareSpy(true);

const resp = await starknetUtils.getEstimatedFees(
STARKNET_SEPOLIA_TESTNET_NETWORK,
account.address,
account.privateKey,
account.publicKey,
invocations,
);

expect(resp).toStrictEqual({
suggestedMaxFee: suggestedMaxFee.toString(10),
overallFee: overallFee.toString(10),
unit: FeeTokenUnit.ETH,
includeDeploy: !deployed,
includeDeploy: true,
});
});
});

0 comments on commit 3acbacf

Please sign in to comment.