Skip to content

Commit

Permalink
refactor(frontend): move decodeQrCode util to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysKarmazynDFINITY committed Oct 9, 2024
1 parent 35b5e15 commit 82e5a57
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 111 deletions.
4 changes: 2 additions & 2 deletions src/frontend/src/icp/components/send/IcSendTokenWizard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
} from '$icp/stores/ethereum-fee.store';
import type { IcToken } from '$icp/types/ic';
import type { IcTransferParams } from '$icp/types/ic-send';
import { icDecodeQrCode } from '$icp/utils/qr-code.utils';
import { SEND_CONTEXT_KEY, type SendContext } from '$icp-eth/stores/send.store';
import {
isConvertCkErc20ToErc20,
Expand Down Expand Up @@ -58,6 +57,7 @@
import { invalidAmount, isNullishOrEmpty } from '$lib/utils/input.utils';
import { isNetworkIdBitcoin } from '$lib/utils/network.utils';
import { parseToken } from '$lib/utils/parse.utils';
import { decodeQrCode } from '$lib/utils/qr-code.utils';
/**
* Props
Expand Down Expand Up @@ -238,7 +238,7 @@
expectedToken={$sendToken}
bind:destination
bind:amount
decodeQrCode={icDecodeQrCode}
{decodeQrCode}
on:icQRCodeBack
/>
{:else}
Expand Down
36 changes: 0 additions & 36 deletions src/frontend/src/icp/utils/qr-code.utils.ts

This file was deleted.

38 changes: 37 additions & 1 deletion src/frontend/src/lib/utils/qr-code.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import {
DecodedUrnSchema,
URN_NUMERIC_PARAMS,
URN_STRING_PARAMS,
type DecodedUrn
type DecodedUrn,
type QrResponse,
type QrStatus
} from '$lib/types/qr-code';
import type { OptionToken } from '$lib/types/token';
import { decodePayment } from '@dfinity/ledger-icrc';
import { isNullish, nonNullish } from '@dfinity/utils';

/**
Expand Down Expand Up @@ -81,3 +85,35 @@ export const decodeQrCodeUrn = (urn: string): DecodedUrn | undefined => {
}
return result.data;
};

export const decodeQrCode = ({
status,
code,
expectedToken
}: {
status: QrStatus;
code?: string | undefined;
expectedToken?: OptionToken;
}): QrResponse => {
if (status !== 'success') {
return { status };
}

if (isNullish(code)) {
return { status: 'cancelled' };
}

const payment = decodePayment(code);

if (isNullish(payment)) {
return { status: 'success', destination: code };
}

const { token: symbol, identifier: destination, amount } = payment;

if (nonNullish(expectedToken) && symbol.toLowerCase() !== expectedToken.symbol.toLowerCase()) {
return { status: 'token_incompatible' };
}

return { status: 'success', destination, symbol, amount };
};
71 changes: 0 additions & 71 deletions src/frontend/src/tests/icp/utils/qr-code.utils.spec.ts

This file was deleted.

71 changes: 70 additions & 1 deletion src/frontend/src/tests/lib/utils/qr-code.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { ICP_TOKEN } from '$env/tokens.env';
import type { EthereumNetwork } from '$eth/types/network';
import { tokens } from '$lib/derived/tokens.derived';
import type { DecodedUrn } from '$lib/types/qr-code';
import { decodeQrCodeUrn } from '$lib/utils/qr-code.utils';
import { decodeQrCode, decodeQrCodeUrn } from '$lib/utils/qr-code.utils';
import { generateUrn } from '$tests/mocks/qr-generator.mock';
import { decodePayment } from '@dfinity/ledger-icrc';
import { assertNonNullish } from '@dfinity/utils';
import { get } from 'svelte/store';
import type { MockedFunction } from 'vitest';

vi.mock('@dfinity/ledger-icrc', () => ({
decodePayment: vi.fn()
}));

describe('decodeUrn', () => {
const tokenList = get(tokens);
Expand Down Expand Up @@ -49,3 +56,65 @@ describe('decodeUrn', () => {
});
});
});

describe('decodeQrCode', () => {
const token = ICP_TOKEN;
const address = 'some-address';
const amount = 1.23;
const code = 'some-qr-code';
const mockDecodePayment = decodePayment as MockedFunction<typeof decodePayment>;

beforeEach(() => {
vi.clearAllMocks();
});

it('should return { result } when result is not success', () => {
const response = decodeQrCode({ status: 'cancelled' });
expect(response).toEqual({ status: 'cancelled' });
});

it('should return { status: "cancelled" } when code is nullish', () => {
const response = decodeQrCode({ status: 'success', code: undefined });
expect(response).toEqual({ status: 'cancelled' });
});

it('should return { status: "success", identifier: code } when payment is nullish', () => {
mockDecodePayment.mockReturnValue(undefined);

const response = decodeQrCode({ status: 'success', code });
expect(response).toEqual({ status: 'success', destination: code });

expect(mockDecodePayment).toHaveBeenCalledWith(code);
});

it('should return { status: "token_incompatible" } when tokens do not match', () => {
const payment = {
token: `not-${token.symbol}`,
identifier: address,
amount: amount
};
mockDecodePayment.mockReturnValue(payment);

const response = decodeQrCode({ status: 'success', code: code, expectedToken: token });
expect(response).toEqual({ status: 'token_incompatible' });
expect(mockDecodePayment).toHaveBeenCalledWith(code);
});

it('should return { status: "success", identifier, token, amount } when everything matches', () => {
const payment = {
token: token.symbol,
identifier: address,
amount: amount
};
mockDecodePayment.mockReturnValue(payment);

const response = decodeQrCode({ status: 'success', code: code, expectedToken: token });
expect(response).toEqual({
status: 'success',
destination: address,
symbol: token.symbol,
amount: amount
});
expect(mockDecodePayment).toHaveBeenCalledWith(code);
});
});

0 comments on commit 82e5a57

Please sign in to comment.