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

test(bridge-ui-v2): minting logic #14149

Merged
merged 31 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
25b2902
wip
jscriptcoder Jul 10, 2023
d55c7ba
wip: faucet
jscriptcoder Jul 10, 2023
b64b0a2
wip: Modal
jscriptcoder Jul 11, 2023
2d1f1d2
wip
jscriptcoder Jul 11, 2023
bd2e4a7
wip
jscriptcoder Jul 11, 2023
2fd98ee
wip
jscriptcoder Jul 11, 2023
1ed70be
wip
jscriptcoder Jul 11, 2023
397a40b
Faucet
jscriptcoder Jul 11, 2023
2c00949
Merge branch 'main' into mint_test_tokens
jscriptcoder Jul 11, 2023
93166b7
minor change
jscriptcoder Jul 12, 2023
5b9cdb9
Merge branch 'mint_test_tokens' of https://github.com/taikoxyz/taiko-…
jscriptcoder Jul 12, 2023
8275a4b
Merge branch 'main' into mint_test_tokens
jscriptcoder Jul 12, 2023
1773ec5
wip: mobile
jscriptcoder Jul 12, 2023
8b4a0d0
Merge branch 'mint_test_tokens' of https://github.com/taikoxyz/taiko-…
jscriptcoder Jul 12, 2023
b374fd9
DesltopOrLarger helper component
jscriptcoder Jul 12, 2023
c95e2b7
minor change
jscriptcoder Jul 12, 2023
f9ad657
minor change
jscriptcoder Jul 12, 2023
2606310
Update packages/bridge-ui-v2/src/i18n/en.json
jscriptcoder Jul 12, 2023
c2e7283
fix eslint
jscriptcoder Jul 12, 2023
7834cce
Merge branch 'mint_test_tokens' of https://github.com/taikoxyz/taiko-…
jscriptcoder Jul 12, 2023
1a5c23f
minor change
jscriptcoder Jul 12, 2023
faa1986
rename free to fee
jscriptcoder Jul 12, 2023
888cf3f
typo
jscriptcoder Jul 12, 2023
30570c2
better use of conditions
jscriptcoder Jul 12, 2023
e972b2d
add minor comment
jscriptcoder Jul 12, 2023
c7d11d3
wip
jscriptcoder Jul 12, 2023
61cd279
add checkMintable test
jscriptcoder Jul 12, 2023
0029e85
minor change
jscriptcoder Jul 12, 2023
ae9999a
token minting coverage
jscriptcoder Jul 12, 2023
cb284a8
excluding coverage
jscriptcoder Jul 12, 2023
b18931a
merge main
jscriptcoder Jul 12, 2023
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
1 change: 1 addition & 0 deletions packages/bridge-ui-v2/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
node_modules
coverage
/build
/.svelte-kit
/package
Expand Down
1 change: 1 addition & 0 deletions packages/bridge-ui-v2/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
node_modules
coverage
/build
/.svelte-kit
/package
Expand Down
6 changes: 4 additions & 2 deletions packages/bridge-ui-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"build": "vite build",
"preview": "vite preview",
"test:pw": "playwright test",
"test:unit": "vitest run",
"test:unit:coverage": "vitest run --coverage",
"test:unit": "vitest run --silent",
"test:unit:debug": "vitest run",
"test:unit:coverage": "vitest run --silent --coverage",
"test:unit:watch": "vitest",
"svelte:check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --ignore ./wagmi.config.ts",
"svelte:check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --ignore ./wagmi.config.ts --watch",
Expand All @@ -25,6 +26,7 @@
"@types/debug": "^4.1.7",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"@vitest/coverage-v8": "^0.33.0",
"@wagmi/cli": "^1.0.1",
"autoprefixer": "^10.4.14",
"daisyui": "3.1.7",
Expand Down
120 changes: 120 additions & 0 deletions packages/bridge-ui-v2/src/libs/token/checkMintable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import {
type Chain,
getContract,
type GetContractResult,
getPublicClient,
getWalletClient,
type PublicClient,
type WalletClient,
} from '@wagmi/core';

import { checkMintable } from './checkMintable';
import { MintableError, type Token } from './types';

vi.mock('@wagmi/core', () => {
return {
getWalletClient: vi.fn(),
getPublicClient: vi.fn(),
getContract: vi.fn(),
};
});

const mockNetwork = { id: 1 } as Chain;

const mockToken = {
addresses: { 1: '0x123' },
} as unknown as Token;

const mockWalletClient = {
account: { address: '0x123' },
} as unknown as WalletClient;

const mockTokenContract = {
read: {
minters: vi.fn(),
},
estimateGas: {
mint: vi.fn(),
},
} as unknown as GetContractResult<readonly unknown[], unknown>;

const mockPublicClient = {
getGasPrice: vi.fn(),
getBalance: vi.fn(),
} as unknown as PublicClient;

describe('checkMintable', () => {
beforeAll(() => {
vi.mocked(getWalletClient).mockResolvedValue(mockWalletClient);
vi.mocked(getContract).mockReturnValue(mockTokenContract);
vi.mocked(getPublicClient).mockReturnValue(mockPublicClient);
});

it('should throw when wallet is not connected', async () => {
vi.mocked(getWalletClient).mockResolvedValueOnce(null);

try {
await checkMintable(mockToken, mockNetwork);
expect.fail('should have thrown');
} catch (error) {
const { cause } = error as Error;
expect(cause).toBe(MintableError.NOT_CONNECTED);
}
});

it('should throw when user has already minted', async () => {
vi.mocked(mockTokenContract.read.minters).mockResolvedValueOnce(true);

try {
await checkMintable(mockToken, mockNetwork);
expect.fail('should have thrown');
} catch (error) {
const { cause } = error as Error;
expect(cause).toBe(MintableError.TOKEN_MINTED);
}
});

it('should throw when user has insufficient balance', async () => {
vi.mocked(mockTokenContract.read.minters).mockResolvedValueOnce(false);

// Estimated gas to mint is 100
vi.mocked(mockTokenContract.estimateGas.mint).mockResolvedValueOnce(BigInt(100));

// Gas price is 2
vi.mocked(mockPublicClient.getGasPrice).mockResolvedValueOnce(BigInt(2));

// Estimated cost is 100 * 2 = 200

// User balance is 100 (less than 200)
vi.mocked(mockPublicClient.getBalance).mockResolvedValueOnce(BigInt(100));

try {
await checkMintable(mockToken, mockNetwork);
expect.fail('should have thrown');
} catch (error) {
const { cause } = error as Error;
expect(cause).toBe(MintableError.INSUFFICIENT_BALANCE);
}
});

it('should not throw', async () => {
vi.mocked(mockTokenContract.read.minters).mockResolvedValueOnce(false);

// Estimated gas to mint is 100
vi.mocked(mockTokenContract.estimateGas.mint).mockResolvedValueOnce(BigInt(100));

// Gas price is 2
vi.mocked(mockPublicClient.getGasPrice).mockResolvedValueOnce(BigInt(2));

// Estimated cost is 100 * 2 = 200

// User balance is 300 (more than 200)
vi.mocked(mockPublicClient.getBalance).mockResolvedValueOnce(BigInt(300));

try {
await checkMintable(mockToken, mockNetwork);
} catch (error) {
expect.fail('should not have thrown');
}
});
});
2 changes: 1 addition & 1 deletion packages/bridge-ui-v2/src/libs/token/checkMintable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function checkMintable(token: Token, network: Chain) {
const walletClient = await getWalletClient({ chainId });

if (!walletClient) {
throw new Error(`user is not connected to ${network.name}`, { cause: MintableError.NOT_CONNECTED });
throw Error(`user is not connected to ${network.name}`, { cause: MintableError.NOT_CONNECTED });
}

const tokenContract = getContract({
Expand Down
46 changes: 46 additions & 0 deletions packages/bridge-ui-v2/src/libs/token/mint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { getContract, type GetContractResult, getWalletClient, type WalletClient } from '@wagmi/core';

import { mint } from './mint';
import type { Token } from './types';

vi.mock('@wagmi/core', () => {
return {
getWalletClient: vi.fn(),
getContract: vi.fn(),
};
});

const mockToken = {
symbol: 'MKT',
addresses: { 1: '0x123' },
} as unknown as Token;

const mockWalletClient = {
account: { address: '0x123' },
chain: { id: 1 },
} as unknown as WalletClient;

const mockTokenContract = {
write: {
mint: vi.fn(),
},
} as unknown as GetContractResult<readonly unknown[], WalletClient>;

describe('mint', () => {
beforeAll(() => {
vi.mocked(getWalletClient).mockResolvedValue(mockWalletClient);
vi.mocked(getContract).mockReturnValue(mockTokenContract);
});

it('should throw an error when minting', async () => {
vi.mocked(mockTokenContract.write.mint).mockRejectedValue(new Error('BAM!!'));

await expect(mint(mockToken, mockWalletClient)).rejects.toThrow(`found a problem minting ${mockToken.symbol}`);
});

it('should return a tx hash when minting', async () => {
vi.mocked(mockTokenContract.write.mint).mockResolvedValue('0x123');

await expect(mint(mockToken, mockWalletClient)).resolves.toEqual('0x123');
});
});
Loading