Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
KorbinianK committed Oct 30, 2023
1 parent 8b0554a commit f597f2f
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 35 deletions.
6 changes: 3 additions & 3 deletions packages/bridge-ui-v2/src/components/Bridge/Actions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import { Button } from '$components/Button';
import { Icon } from '$components/Icon';
import { bridges, ContractType,type RequireApprovalArgs } from '$libs/bridge';
import { bridges, ContractType, type RequireApprovalArgs } from '$libs/bridge';
import type { ERC721Bridge } from '$libs/bridge/ERC721Bridge';
import type { ERC1155Bridge } from '$libs/bridge/ERC1155Bridge';
import { getContractAddressBasedOnType } from '$libs/bridge/getContractAddressBasedOnType';
import { getContractAddressByType } from '$libs/bridge/getContractAddressByType';
import { type NFT, TokenType } from '$libs/token';
import { checkOwnershipOfNFT } from '$libs/token/checkOwnership';
import { getConnectedWallet } from '$libs/util/getConnectedWallet';
Expand Down Expand Up @@ -63,7 +63,7 @@
const wallet = await getConnectedWallet();
const spenderAddress = getContractAddressBasedOnType({
const spenderAddress = getContractAddressByType({
srcChainId: currentChainId,
destChainId: destinationChainId,
tokenType: token.type,
Expand Down

This file was deleted.

166 changes: 166 additions & 0 deletions packages/bridge-ui-v2/src/libs/bridge/getContractAddressByType.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import { routingContractsMap } from '$bridgeConfig';
import { ContractType, type GetContractAddressType } from '$libs/bridge';
import { TokenType } from '$libs/token';

import { getContractAddressByType } from './getContractAddressByType';

const SRC_CHAIN_ID = 1;
const DEST_CHAIN_ID = 2;

vi.mock('$customToken', () => ({ customToken: [] }));

vi.mock('$bridgeConfig', () => ({
routingContractsMap: {
1: {
2: {
erc20VaultAddress: '0x00001',
bridgeAddress: '0x00002',
erc721VaultAddress: '0x00003',
erc1155VaultAddress: '0x00004',
crossChainSyncAddress: '0x00005',
signalServiceAddress: '0x00006',
},
},
2: {
1: {
erc20VaultAddress: '0x00007',
bridgeAddress: '0x00008',
erc721VaultAddress: '0x00009',
erc1155VaultAddress: '0x00010',
crossChainSyncAddress: '0x00011',
signalServiceAddress: '0x00012',
},
},
},
}));

describe('getContractAddressBasedOnType', () => {
it('should return the correct vault contract address based on contract type (ERC20)', () => {
// Given
const args: GetContractAddressType = {
srcChainId: SRC_CHAIN_ID,
destChainId: DEST_CHAIN_ID,
tokenType: TokenType.ERC20,
contractType: ContractType.VAULT,
};

const expectedAddress = routingContractsMap[SRC_CHAIN_ID][DEST_CHAIN_ID].erc20VaultAddress;

// When
const address = getContractAddressByType(args);

// Then
expect(address).to.equal(expectedAddress);
expect(address).not.toBeUndefined();
});

it('should return the correct vault contract address based on token type (ERC721)', () => {
// Given
const args: GetContractAddressType = {
srcChainId: SRC_CHAIN_ID,
destChainId: DEST_CHAIN_ID,
tokenType: TokenType.ERC721,
contractType: ContractType.VAULT,
};

const expectedAddress = routingContractsMap[SRC_CHAIN_ID][DEST_CHAIN_ID].erc721VaultAddress;

// When
const address = getContractAddressByType(args);

// Then
expect(address).to.equal(expectedAddress);
expect(address).not.toBeUndefined();
});

it('should return the correct vault contract address based on token type (ERC1155)', () => {
// Given
const args: GetContractAddressType = {
srcChainId: SRC_CHAIN_ID,
destChainId: DEST_CHAIN_ID,
tokenType: TokenType.ERC1155,
contractType: ContractType.VAULT,
};

const expectedAddress = routingContractsMap[SRC_CHAIN_ID][DEST_CHAIN_ID].erc1155VaultAddress;

// When
const address = getContractAddressByType(args);

// Then
expect(address).to.equal(expectedAddress);
expect(address).not.toBeUndefined();
});

it('should return the correct bridge contract address', () => {
// Given
const args: GetContractAddressType = {
srcChainId: SRC_CHAIN_ID,
destChainId: DEST_CHAIN_ID,
contractType: ContractType.BRIDGE,
};

const expectedAddress = routingContractsMap[SRC_CHAIN_ID][DEST_CHAIN_ID].bridgeAddress;

// When
const address = getContractAddressByType(args);

// Then
expect(address).to.equal(expectedAddress);
expect(address).not.toBeUndefined();
});

it('should return the correct crosschain sync contract address', () => {
// Given
const args: GetContractAddressType = {
srcChainId: SRC_CHAIN_ID,
destChainId: DEST_CHAIN_ID,
contractType: ContractType.CROSSCHAINSYNC,
};

const expectedAddress = routingContractsMap[SRC_CHAIN_ID][DEST_CHAIN_ID].crossChainSyncAddress;

// When
const address = getContractAddressByType(args);

// Then
expect(address).to.equal(expectedAddress);
expect(address).not.toBeUndefined();
});

it('should return the correct signalservice contract address', () => {
// Given
const args: GetContractAddressType = {
srcChainId: SRC_CHAIN_ID,
destChainId: DEST_CHAIN_ID,
contractType: ContractType.SIGNALSERVICE,
};

const expectedAddress = routingContractsMap[SRC_CHAIN_ID][DEST_CHAIN_ID].signalServiceAddress;

// When
const address = getContractAddressByType(args);

// Then
expect(address).to.equal(expectedAddress);
expect(address).not.toBeUndefined();
});

it('should return the correct contract address for different configs', () => {
// Given
const args: GetContractAddressType = {
srcChainId: DEST_CHAIN_ID,
destChainId: SRC_CHAIN_ID,
contractType: ContractType.SIGNALSERVICE,
};

const expectedAddress = routingContractsMap[DEST_CHAIN_ID][SRC_CHAIN_ID].signalServiceAddress;

// When
const address = getContractAddressByType(args);

// Then
expect(address).to.equal(expectedAddress);
expect(address).not.toBeUndefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const getVaultAddress = (args: GetContractAddressType): Address => {
[TokenType.ETH]: (config) => config.etherVaultAddress || zeroAddress,
};

if (!args.tokenType) throw new Error('Token type is required');
const getAddress = addressGetters[args.tokenType];

if (!getAddress) {
Expand All @@ -23,7 +24,7 @@ const getVaultAddress = (args: GetContractAddressType): Address => {
return getAddress(addressConfig);
};

export const getContractAddressBasedOnType = (args: GetContractAddressType): Address => {
export const getContractAddressByType = (args: GetContractAddressType): Address => {
const addressConfig = routingContractsMap[args.srcChainId][args.destChainId];

switch (args.contractType) {
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge-ui-v2/src/libs/bridge/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,6 @@ export enum ContractType {
export type GetContractAddressType = {
srcChainId: number;
destChainId: number;
tokenType: TokenType;
tokenType?: TokenType;
contractType: ContractType;
};

0 comments on commit f597f2f

Please sign in to comment.