-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(bridge-ui-v2): minting logic (#14149)
Co-authored-by: Korbinian <[email protected]>
- Loading branch information
1 parent
40ec18c
commit 5d856df
Showing
7 changed files
with
238 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.DS_Store | ||
node_modules | ||
coverage | ||
/build | ||
/.svelte-kit | ||
/package | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.DS_Store | ||
node_modules | ||
coverage | ||
/build | ||
/.svelte-kit | ||
/package | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
packages/bridge-ui-v2/src/libs/token/checkMintable.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
Oops, something went wrong.