Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(frontend): add sol addresses to derived #3999

Merged
merged 18 commits into from
Dec 17, 2024
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(frontend): add sol to idb.api, add tests for all networks in idb…
….api.spec.ts
loki344 committed Dec 17, 2024
commit cf5c907f38ff241e44e0e543b7bc3152cb19e98d
53 changes: 51 additions & 2 deletions src/frontend/src/lib/api/idb.api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { browser } from '$app/environment';
import { ETHEREUM_NETWORK_SYMBOL } from '$env/networks/networks.env';
import {
SOLANA_DEVNET_NETWORK_SYMBOL,
SOLANA_LOCAL_NETWORK_SYMBOL,
SOLANA_MAINNET_NETWORK_SYMBOL,
SOLANA_TESTNET_NETWORK_SYMBOL
} from '$env/networks/networks.sol.env';
import { BTC_MAINNET_SYMBOL, BTC_TESTNET_SYMBOL } from '$env/tokens/tokens.btc.env';
import type { BtcAddress, EthAddress } from '$lib/types/address';
import type { IdbBtcAddress, IdbEthAddress, SetIdbAddressParams } from '$lib/types/idb';
import type { BtcAddress, EthAddress, SolAddress } from '$lib/types/address';
import type {
IdbBtcAddress,
IdbEthAddress,
IdbSolAddress,
SetIdbAddressParams
} from '$lib/types/idb';
import type { Principal } from '@dfinity/principal';
import { isNullish } from '@dfinity/utils';
import { createStore, del, get, set, update, type UseStore } from 'idb-keyval';
@@ -16,6 +27,11 @@ const idbBtcAddressesStoreTestnet = idbAddressesStore(BTC_TESTNET_SYMBOL.toLower

const idbEthAddressesStore = idbAddressesStore(ETHEREUM_NETWORK_SYMBOL.toLowerCase());

const idbSolAddressesStoreMainnet = idbAddressesStore(SOLANA_MAINNET_NETWORK_SYMBOL.toLowerCase());
const idbSolAddressesStoreTestnet = idbAddressesStore(SOLANA_TESTNET_NETWORK_SYMBOL.toLowerCase());
const idbSolAddressesStoreDevnet = idbAddressesStore(SOLANA_DEVNET_NETWORK_SYMBOL.toLowerCase());
const idbSolAddressesStoreLocal = idbAddressesStore(SOLANA_LOCAL_NETWORK_SYMBOL.toLowerCase());

export const setIdbBtcAddressMainnet = ({
address,
principal
@@ -34,6 +50,30 @@ export const setIdbEthAddress = ({
}: SetIdbAddressParams<EthAddress>): Promise<void> =>
set(principal.toText(), address, idbEthAddressesStore);

export const setIdbSolAddressMainnet = ({
address,
principal
}: SetIdbAddressParams<SolAddress>): Promise<void> =>
set(principal.toText(), address, idbSolAddressesStoreMainnet);

export const setIdbSolAddressTestnet = ({
address,
principal
}: SetIdbAddressParams<SolAddress>): Promise<void> =>
set(principal.toText(), address, idbSolAddressesStoreTestnet);

export const setIdbSolAddressDevnet = ({
address,
principal
}: SetIdbAddressParams<SolAddress>): Promise<void> =>
set(principal.toText(), address, idbSolAddressesStoreDevnet);

export const setIdbSolAddressLocal = ({
address,
principal
}: SetIdbAddressParams<SolAddress>): Promise<void> =>
set(principal.toText(), address, idbSolAddressesStoreLocal);

const updateIdbAddressLastUsage = ({
principal,
idbAddressesStore
@@ -62,14 +102,23 @@ export const updateIdbBtcAddressMainnetLastUsage = (principal: Principal): Promi
export const updateIdbEthAddressLastUsage = (principal: Principal): Promise<void> =>
updateIdbAddressLastUsage({ principal, idbAddressesStore: idbEthAddressesStore });

export const updateIdbSolAddressMainnetLastUsage = (principal: Principal): Promise<void> =>
updateIdbAddressLastUsage({ principal, idbAddressesStore: idbSolAddressesStoreMainnet });

export const getIdbBtcAddressMainnet = (principal: Principal): Promise<IdbBtcAddress | undefined> =>
get(principal.toText(), idbBtcAddressesStoreMainnet);

export const getIdbEthAddress = (principal: Principal): Promise<IdbEthAddress | undefined> =>
get(principal.toText(), idbEthAddressesStore);

export const getIdbSolAddressMainnet = (principal: Principal): Promise<IdbSolAddress | undefined> =>
get(principal.toText(), idbSolAddressesStoreMainnet);

export const deleteIdbBtcAddressMainnet = (principal: Principal): Promise<void> =>
del(principal.toText(), idbBtcAddressesStoreMainnet);

export const deleteIdbEthAddress = (principal: Principal): Promise<void> =>
del(principal.toText(), idbEthAddressesStore);

export const deleteIdbSolAddressMainnet = (principal: Principal): Promise<void> =>
del(principal.toText(), idbSolAddressesStoreMainnet);
188 changes: 188 additions & 0 deletions src/frontend/src/tests/lib/api/idb.api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import {
deleteIdbBtcAddressMainnet,
deleteIdbEthAddress,
deleteIdbSolAddressMainnet,
getIdbBtcAddressMainnet,
getIdbEthAddress,
getIdbSolAddressMainnet,
setIdbBtcAddressMainnet,
setIdbEthAddress,
setIdbSolAddressMainnet,
updateIdbBtcAddressMainnetLastUsage,
updateIdbEthAddressLastUsage,
updateIdbSolAddressMainnetLastUsage
} from '$lib/api/idb.api';
import { Principal } from '@dfinity/principal';
import * as idbKeyval from 'idb-keyval';
import { beforeEach, describe, expect, it, vi } from 'vitest';

vi.mock('idb-keyval', () => ({
createStore: vi.fn(() => ({
/* mock store implementation */
})),
set: vi.fn(),
get: vi.fn(),
del: vi.fn(),
update: vi.fn()
}));

vi.mock('$app/environment', () => ({
browser: true
}));

describe('idb.api', () => {
const mockPrincipal = Principal.fromText('2vxsx-fae');
const mockAddress = {
address: '0x123',
lastUsedTimestamp: Date.now(),
createdAtTimestamp: Date.now()
};

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

describe('BTC operations', () => {
it('should set BTC address', async () => {
await setIdbBtcAddressMainnet({
principal: mockPrincipal,
address: mockAddress
});

expect(idbKeyval.set).toHaveBeenCalledWith(
mockPrincipal.toText(),
mockAddress,
expect.any(Object)
);
});

it('should get BTC address', async () => {
vi.mocked(idbKeyval.get).mockResolvedValue(mockAddress);

const result = await getIdbBtcAddressMainnet(mockPrincipal);

expect(result).toEqual(mockAddress);
expect(idbKeyval.get).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object));
});

it('should delete BTC address', async () => {
await deleteIdbBtcAddressMainnet(mockPrincipal);

expect(idbKeyval.del).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object));
});

it('should update BTC address last usage', async () => {
vi.mocked(idbKeyval.update).mockImplementation(async (_, updater) => {

Check warning on line 75 in src/frontend/src/tests/lib/api/idb.api.spec.ts

GitHub Actions / lint

Functions with more than one parameter should accept an object and use destructuring

Check failure on line 75 in src/frontend/src/tests/lib/api/idb.api.spec.ts

GitHub Actions / lint

Async arrow function has no 'await' expression
const updated = updater(mockAddress) as typeof mockAddress;
expect(updated.lastUsedTimestamp).toBeGreaterThan(mockAddress.lastUsedTimestamp);
});

await updateIdbBtcAddressMainnetLastUsage(mockPrincipal);

expect(idbKeyval.update).toHaveBeenCalled();
});
});

describe('ETH operations', () => {
it('should set ETH address', async () => {
await setIdbEthAddress({
principal: mockPrincipal,
address: mockAddress
});

expect(idbKeyval.set).toHaveBeenCalledWith(
mockPrincipal.toText(),
mockAddress,
expect.any(Object)
);
});

it('should get ETH address', async () => {
vi.mocked(idbKeyval.get).mockResolvedValue(mockAddress);

const result = await getIdbEthAddress(mockPrincipal);

expect(result).toEqual(mockAddress);
expect(idbKeyval.get).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object));
});

it('should delete ETH address', async () => {
await deleteIdbEthAddress(mockPrincipal);

expect(idbKeyval.del).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object));
});

it('should update ETH address last usage', async () => {
vi.mocked(idbKeyval.update).mockImplementation(async (_, updater) => {

Check warning on line 116 in src/frontend/src/tests/lib/api/idb.api.spec.ts

GitHub Actions / lint

Functions with more than one parameter should accept an object and use destructuring

Check failure on line 116 in src/frontend/src/tests/lib/api/idb.api.spec.ts

GitHub Actions / lint

Async arrow function has no 'await' expression
const updated = updater(mockAddress) as typeof mockAddress;
expect(updated.lastUsedTimestamp).toBeGreaterThan(mockAddress.lastUsedTimestamp);
});

await updateIdbEthAddressLastUsage(mockPrincipal);

expect(idbKeyval.update).toHaveBeenCalled();
});
});

describe('SOL operations', () => {
it('should set SOL address', async () => {
await setIdbSolAddressMainnet({
principal: mockPrincipal,
address: mockAddress
});

expect(idbKeyval.set).toHaveBeenCalledWith(
mockPrincipal.toText(),
mockAddress,
expect.any(Object)
);
});

it('should get SOL address', async () => {
vi.mocked(idbKeyval.get).mockResolvedValue(mockAddress);

const result = await getIdbSolAddressMainnet(mockPrincipal);

expect(result).toEqual(mockAddress);
expect(idbKeyval.get).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object));
});

it('should delete SOL address', async () => {
await deleteIdbSolAddressMainnet(mockPrincipal);

expect(idbKeyval.del).toHaveBeenCalledWith(mockPrincipal.toText(), expect.any(Object));
});

it('should update SOL address last usage', async () => {
vi.mocked(idbKeyval.update).mockImplementation(async (_, updater) => {

Check warning on line 157 in src/frontend/src/tests/lib/api/idb.api.spec.ts

GitHub Actions / lint

Functions with more than one parameter should accept an object and use destructuring

Check failure on line 157 in src/frontend/src/tests/lib/api/idb.api.spec.ts

GitHub Actions / lint

Async arrow function has no 'await' expression
const updated = updater(mockAddress) as typeof mockAddress;
expect(updated.lastUsedTimestamp).toBeGreaterThan(mockAddress.lastUsedTimestamp);
});

await updateIdbSolAddressMainnetLastUsage(mockPrincipal);

expect(idbKeyval.update).toHaveBeenCalled();
});
});

describe('Edge cases', () => {
it('should handle undefined address when updating last usage', async () => {
vi.mocked(idbKeyval.update).mockImplementation(async (_, updater) => {

Check warning on line 170 in src/frontend/src/tests/lib/api/idb.api.spec.ts

GitHub Actions / lint

Functions with more than one parameter should accept an object and use destructuring

Check failure on line 170 in src/frontend/src/tests/lib/api/idb.api.spec.ts

GitHub Actions / lint

Async arrow function has no 'await' expression
const result = updater(undefined);
expect(result).toBeUndefined();
});

await updateIdbBtcAddressMainnetLastUsage(mockPrincipal);

expect(idbKeyval.update).toHaveBeenCalled();
});

it('should return undefined when getting non-existent address', async () => {
vi.mocked(idbKeyval.get).mockResolvedValue(undefined);

const result = await getIdbBtcAddressMainnet(mockPrincipal);

expect(result).toBeUndefined();
});
});
});