Skip to content

Commit

Permalink
chore: fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
khanti42 committed Nov 25, 2024
1 parent e2bda06 commit 0faa28b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 86 deletions.
4 changes: 4 additions & 0 deletions packages/starknet-snap/src/__tests__/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { v4 as uuidv4 } from 'uuid';
import { FeeToken } from '../types/snapApi';
import type {
AccContract,
Erc20Token,
Transaction,
TransactionRequest,
} from '../types/snapState';
Expand Down Expand Up @@ -298,11 +299,13 @@ export function generateTransactions({
export function generateTransactionRequests({
chainId,
address,
selectedFeeToken,
contractAddresses = PRELOADED_TOKENS.map((token) => token.address),
cnt = 1,
}: {
chainId: constants.StarknetChainId | string;
address: string;
selectedFeeToken?: Erc20Token;
contractAddresses?: string[];
cnt?: number;
}): TransactionRequest[] {
Expand Down Expand Up @@ -330,6 +333,7 @@ export function generateTransactionRequests({
addressIndex: 0,
maxFee: '100',
selectedFeeToken:
selectedFeeToken?.symbol ??
feeTokens[Math.floor(generateRandomValue() * feeTokens.length)].symbol,
calls: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ describe('UserInputEventController', () => {
describe('handleFeeTokenChange', () => {
const prepareHandleFeeTokenChange = async (
feeToken: FeeToken = FeeToken.STRK,
selectedFeeToken?: Erc20Token,
) => {
const network = STARKNET_TESTNET_NETWORK;
const { chainId } = network;
Expand All @@ -325,6 +326,7 @@ describe('UserInputEventController', () => {
const [transactionRequest] = generateTransactionRequests({
chainId,
address: account.address,
selectedFeeToken,
});

const event = generateInputEvent({
Expand Down Expand Up @@ -353,6 +355,10 @@ describe('UserInputEventController', () => {
'updates the transaction request with the updated estimated fee: feeToken - %symbol',
async (token: Erc20Token) => {
const feeToken = FeeToken[token.symbol];
const selectedFeeToken =
token.symbol === FeeToken.ETH
? STRK_SEPOLIA_TESTNET
: ETHER_SEPOLIA_TESTNET;
const {
event,
account,
Expand All @@ -363,7 +369,7 @@ describe('UserInputEventController', () => {
mockGetEstimatedFeesResponse,
upsertTransactionRequestSpy,
transactionRequest,
} = await prepareHandleFeeTokenChange(feeToken);
} = await prepareHandleFeeTokenChange(feeToken, selectedFeeToken);
const feeTokenAddress = token.address;
const { signer, calls } = transactionRequest;
const { publicKey, privateKey, address } = account;
Expand Down Expand Up @@ -404,87 +410,10 @@ describe('UserInputEventController', () => {
controller.eventId,
transactionRequest,
);
expect(upsertTransactionRequestSpy).toHaveBeenCalledWith(
transactionRequest,
);
},
);

it.each([STRK_SEPOLIA_TESTNET, ETHER_SEPOLIA_TESTNET])(
'rollsback the transaction request with the original request state if state update fails: feeToken - %symbol',
async (token: Erc20Token) => {
const feeToken = FeeToken[token.symbol];
const {
event,
account,
network,
getEstimatedFeesSpy,
hasSufficientFundsForFeeSpy,
updateExecuteTxnFlowSpy,
mockGetEstimatedFeesResponse,
upsertTransactionRequestSpy,
transactionRequest,
} = await prepareHandleFeeTokenChange(feeToken);
const feeTokenAddress = token.address;
const { signer, calls } = transactionRequest;
const { publicKey, privateKey, address } = account;
const { suggestedMaxFee } = mockGetEstimatedFeesResponse;
const rollbackSnapshot = {
maxFee: transactionRequest.maxFee,
selectedFeeToken: transactionRequest.selectedFeeToken,
includeDeploy: transactionRequest.includeDeploy,
resourceBounds: [...transactionRequest.resourceBounds],
};

upsertTransactionRequestSpy.mockRejectedValue(new Error('Failed!'));

const controller = createMockController(event);
await controller.handleFeeTokenChange();

expect(getEstimatedFeesSpy).toHaveBeenCalledWith(
network,
signer,
privateKey,
publicKey,
[
{
type: TransactionType.INVOKE,
payload: calls.map((call) => ({
calldata: call.calldata,
contractAddress: call.contractAddress,
entrypoint: call.entrypoint,
})),
},
],
{
version: controller.feeTokenToTransactionVersion(feeToken),
},
);
expect(hasSufficientFundsForFeeSpy).toHaveBeenCalledWith({
address,
network,
calls,
feeTokenAddress,
suggestedMaxFee,
expect(upsertTransactionRequestSpy).toHaveBeenCalledWith({
...transactionRequest,
selectedFeeToken: feeToken,
});
// transactionRequest will be pass by reference, so we can use this to check the updated value
expect(transactionRequest.maxFee).toStrictEqual(suggestedMaxFee);
expect(updateExecuteTxnFlowSpy).toHaveBeenCalledWith(
controller.eventId,
transactionRequest,
);
expect(upsertTransactionRequestSpy).toHaveBeenCalledWith(
transactionRequest,
);
expect(updateExecuteTxnFlowSpy).toHaveBeenCalledWith(
controller.eventId,
{ ...transactionRequest, ...rollbackSnapshot },
{
errors: {
fees: `Failed to calculate the fees, switching back to ${transactionRequest.selectedFeeToken}`,
},
},
);
},
);

Expand Down Expand Up @@ -514,24 +443,28 @@ describe('UserInputEventController', () => {
);
});

it('updates the transaction request with an general error message if other error was thrown.', async () => {
it('rollback the transaction request and show a general error message if other error was thrown.', async () => {
const {
event,
hasSufficientFundsForFeeSpy,
transactionRequest,
updateExecuteTxnFlowSpy,
upsertTransactionRequestSpy,
} = await prepareHandleFeeTokenChange();
// Simulate an error thrown to test the error handling
hasSufficientFundsForFeeSpy.mockRejectedValue(false);
upsertTransactionRequestSpy.mockRejectedValue(new Error('Failed!'));
const rollbackSnapshot = {
maxFee: transactionRequest.maxFee,
selectedFeeToken: transactionRequest.selectedFeeToken,
includeDeploy: transactionRequest.includeDeploy,
resourceBounds: [...transactionRequest.resourceBounds],
};

const controller = createMockController(event);
await controller.handleFeeTokenChange();

expect(upsertTransactionRequestSpy).not.toHaveBeenCalled();
expect(updateExecuteTxnFlowSpy).toHaveBeenCalledWith(
controller.eventId,
transactionRequest,
{ ...transactionRequest, ...rollbackSnapshot },
{
errors: {
fees: `Failed to calculate the fees, switching back to ${transactionRequest.selectedFeeToken}`,
Expand Down

0 comments on commit 0faa28b

Please sign in to comment.