|
| 1 | +import { vi } from 'vitest' |
| 2 | +import { ContractState, BlueprintInfo, ContractHistory } from '@/types/hathor' |
| 3 | +import { Network } from '@/lib/config' |
| 4 | + |
| 5 | +export const mockBlueprintInfo: BlueprintInfo = { |
| 6 | + id: 'test-blueprint-id', |
| 7 | + name: 'HathorDice', |
| 8 | + attributes: { |
| 9 | + public_methods: ['place_bet', 'add_liquidity', 'remove_liquidity', 'claim_balance'], |
| 10 | + }, |
| 11 | +} |
| 12 | + |
| 13 | +export const mockContractStates: Record<string, ContractState> = { |
| 14 | + 'contract-htr': { |
| 15 | + token_uid: '00', |
| 16 | + max_bet_amount: 10000n, |
| 17 | + house_edge_basis_points: 190, |
| 18 | + random_bit_length: 16, |
| 19 | + available_tokens: 100000000n, |
| 20 | + total_liquidity_provided: 100000000n, |
| 21 | + }, |
| 22 | + 'contract-usdc': { |
| 23 | + token_uid: '01', |
| 24 | + max_bet_amount: 5000n, |
| 25 | + house_edge_basis_points: 250, |
| 26 | + random_bit_length: 20, |
| 27 | + available_tokens: 50000000n, |
| 28 | + total_liquidity_provided: 50000000n, |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | +export const mockTransactions = [ |
| 33 | + { |
| 34 | + tx_id: '0000000000000001', |
| 35 | + timestamp: Date.now() - 60000, |
| 36 | + nc_method: 'place_bet', |
| 37 | + nc_caller: 'WYBwT3xLpDnHNtYZiU52oanupVeDKhAvNp', |
| 38 | + first_block: '0000000000000abc', |
| 39 | + is_voided: false, |
| 40 | + nc_args_decoded: { |
| 41 | + threshold: 32768, |
| 42 | + }, |
| 43 | + nc_events: [ |
| 44 | + { |
| 45 | + type: 'BetPlaced', |
| 46 | + data: '{"user":"WYBwT3xLpDnHNtYZiU52oanupVeDKhAvNp","amount":1000,"threshold":32768,"result":12345,"won":true,"payout":1900}', |
| 47 | + }, |
| 48 | + ], |
| 49 | + }, |
| 50 | + { |
| 51 | + tx_id: '0000000000000002', |
| 52 | + timestamp: Date.now() - 120000, |
| 53 | + nc_method: 'place_bet', |
| 54 | + nc_caller: 'WYBwT3xLpDnHNtYZiU52oanupVeDKhAvNp', |
| 55 | + first_block: '0000000000000abd', |
| 56 | + is_voided: false, |
| 57 | + nc_args_decoded: { |
| 58 | + threshold: 16384, |
| 59 | + }, |
| 60 | + nc_events: [ |
| 61 | + { |
| 62 | + type: 'BetPlaced', |
| 63 | + data: '{"user":"WYBwT3xLpDnHNtYZiU52oanupVeDKhAvNp","amount":500,"threshold":16384,"result":50000,"won":false,"payout":0}', |
| 64 | + }, |
| 65 | + ], |
| 66 | + }, |
| 67 | +] |
| 68 | + |
| 69 | +export const mockContractHistory: ContractHistory = { |
| 70 | + transactions: mockTransactions, |
| 71 | + total: mockTransactions.length, |
| 72 | + hasMore: false, |
| 73 | +} |
| 74 | + |
| 75 | +export class MockHathorCoreAPI { |
| 76 | + private baseUrl: string |
| 77 | + public network: Network |
| 78 | + |
| 79 | + constructor(network: Network) { |
| 80 | + this.network = network |
| 81 | + this.baseUrl = `https://mock-${network}.hathor.network/v1a` |
| 82 | + } |
| 83 | + |
| 84 | + async getBlueprintInfo(blueprintId: string): Promise<BlueprintInfo> { |
| 85 | + return mockBlueprintInfo |
| 86 | + } |
| 87 | + |
| 88 | + async getContractState(contractId: string): Promise<ContractState> { |
| 89 | + return mockContractStates[contractId] || mockContractStates['contract-htr'] |
| 90 | + } |
| 91 | + |
| 92 | + async getContractHistory( |
| 93 | + contractId: string, |
| 94 | + limit: number = 50, |
| 95 | + after?: string |
| 96 | + ): Promise<ContractHistory> { |
| 97 | + // Simulate pagination |
| 98 | + const allTransactions = mockTransactions |
| 99 | + const startIndex = after ? allTransactions.findIndex(tx => tx.tx_id === after) + 1 : 0 |
| 100 | + const transactions = allTransactions.slice(startIndex, startIndex + limit) |
| 101 | + |
| 102 | + return { |
| 103 | + transactions, |
| 104 | + total: transactions.length, |
| 105 | + hasMore: startIndex + limit < allTransactions.length, |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + async getTransaction(txId: string): Promise<any> { |
| 110 | + const tx = mockTransactions.find(t => t.tx_id === txId) |
| 111 | + if (!tx) { |
| 112 | + throw new Error(`Transaction not found: ${txId}`) |
| 113 | + } |
| 114 | + return tx |
| 115 | + } |
| 116 | + |
| 117 | + async callViewFunction( |
| 118 | + contractId: string, |
| 119 | + method: string, |
| 120 | + args: any[] = [], |
| 121 | + callerAddress?: string |
| 122 | + ): Promise<any> { |
| 123 | + const callKey = `${method}(${args.map(arg => JSON.stringify(arg)).join(', ')})` |
| 124 | + |
| 125 | + // Mock different view function responses |
| 126 | + if (method === 'get_address_balance') { |
| 127 | + return { |
| 128 | + calls: { |
| 129 | + [callKey]: { |
| 130 | + value: 5000, |
| 131 | + }, |
| 132 | + }, |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if (method === 'calculate_address_maximum_liquidity_removal') { |
| 137 | + return { |
| 138 | + calls: { |
| 139 | + [callKey]: { |
| 140 | + value: 10000, |
| 141 | + }, |
| 142 | + }, |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return { |
| 147 | + calls: { |
| 148 | + [callKey]: { |
| 149 | + value: 0, |
| 150 | + }, |
| 151 | + }, |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + async getMaximumLiquidityRemoval(contractId: string, callerAddress: string): Promise<bigint> { |
| 156 | + const result = await this.callViewFunction( |
| 157 | + contractId, |
| 158 | + 'calculate_address_maximum_liquidity_removal', |
| 159 | + [callerAddress], |
| 160 | + callerAddress |
| 161 | + ) |
| 162 | + const callKey = Object.keys(result.calls)[0] |
| 163 | + if (callKey && result.calls[callKey]?.value !== undefined) { |
| 164 | + return BigInt(result.calls[callKey].value) |
| 165 | + } |
| 166 | + return 0n |
| 167 | + } |
| 168 | + |
| 169 | + async getClaimableBalance(contractId: string, callerAddress: string): Promise<bigint> { |
| 170 | + const result = await this.callViewFunction(contractId, 'get_address_balance', [callerAddress], callerAddress) |
| 171 | + const callKey = Object.keys(result.calls)[0] |
| 172 | + if (callKey && result.calls[callKey]?.value !== undefined) { |
| 173 | + return BigInt(result.calls[callKey].value) |
| 174 | + } |
| 175 | + return 0n |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +// Create mock instance |
| 180 | +export const createMockHathorCoreAPI = (network: Network = 'testnet') => { |
| 181 | + return new MockHathorCoreAPI(network) |
| 182 | +} |
| 183 | + |
| 184 | +// Vitest mock factory |
| 185 | +export const mockHathorCoreAPIFactory = () => { |
| 186 | + const mockInstance = createMockHathorCoreAPI() |
| 187 | + |
| 188 | + return { |
| 189 | + HathorCoreAPI: vi.fn().mockImplementation((network: Network) => { |
| 190 | + const instance = createMockHathorCoreAPI(network) |
| 191 | + return { |
| 192 | + getBlueprintInfo: vi.fn().mockImplementation(instance.getBlueprintInfo.bind(instance)), |
| 193 | + getContractState: vi.fn().mockImplementation(instance.getContractState.bind(instance)), |
| 194 | + getContractHistory: vi.fn().mockImplementation(instance.getContractHistory.bind(instance)), |
| 195 | + getTransaction: vi.fn().mockImplementation(instance.getTransaction.bind(instance)), |
| 196 | + callViewFunction: vi.fn().mockImplementation(instance.callViewFunction.bind(instance)), |
| 197 | + getMaximumLiquidityRemoval: vi.fn().mockImplementation(instance.getMaximumLiquidityRemoval.bind(instance)), |
| 198 | + getClaimableBalance: vi.fn().mockImplementation(instance.getClaimableBalance.bind(instance)), |
| 199 | + } |
| 200 | + }), |
| 201 | + } |
| 202 | +} |
0 commit comments