import { fetchWithTimeout } from 'src/utils/fetchWithTimeout' import { publicClient } from 'src/viem' import networkConfig from 'src/web3/networkConfig' import { mockAccount2 } from 'test/values' import { jumpstartLinkHandler } from './jumpstartLinkHandler' jest.mock('src/utils/fetchWithTimeout') jest.mock('viem', () => ({ ...jest.requireActual('viem'), readContract: jest.fn(), })) const mockUserAddress = mockAccount2 const mockPrivateKey = '0x9954ab9a9573b0c1c498909d635be6a40494e8c3f1c964b8a78a6c0491bf2da2' const mockBeneficiary = '0x148536D69982C432709c41f8ff1107f5E808e5A4' // matches account generated by mockPrivateKey const mockSignature = '0xce9763ecc418d33537805e7e2675deaf7a447c1e2a9f4f4969cadaa6c4e03813704cdf7695d5d26d976c1eb0069636891ee768fab397de2a2f9d221f3fb63eb21c' // matches expected abi encoded message with mockUserAddress signed by the mockPrivateKey account describe('jumpstartLinkHandler', () => { beforeEach(() => { jest.clearAllMocks() }) it('claims any unclaimed funds associated with the private key', async () => { jest .spyOn(publicClient.celo, 'readContract') .mockImplementation(async ({ functionName, args }) => { if (functionName === 'erc20Claims' && args) { if (args[1] === BigInt(0)) { return ['0xtoken', '0xdepositor', BigInt(10), true] } else if (args[1] === BigInt(1)) { return ['0xtoken', '0xdepositor', BigInt(10), false] } } throw new Error('Execution reverted for an unknown reason.') }) jest .mocked(fetchWithTimeout) .mockResolvedValue(new Response(JSON.stringify({ result: { transactionHash: '0xHASH' } }))) const contractAddress = '0xTEST' const result = await jumpstartLinkHandler( networkConfig.defaultNetworkId, contractAddress, mockPrivateKey, mockUserAddress ) expect(result).toEqual(['0xHASH']) expect(fetchWithTimeout).toHaveBeenCalledTimes(1) expect(fetchWithTimeout).toHaveBeenCalledWith( `${networkConfig.walletJumpstartUrl}?index=1&beneficiary=${mockBeneficiary}&signature=${mockSignature}&sendTo=${mockUserAddress}&assetType=erc20&networkId=celo-alfajores`, expect.any(Object), expect.any(Number) ) }) it('throws an error if all funds were already claimed', async () => { jest .spyOn(publicClient.celo, 'readContract') .mockImplementation(async ({ functionName, args }) => { if (functionName === 'erc20Claims' && args) { if (args[1] === BigInt(0)) { return ['0xtoken', '0xdepositor', BigInt(10), true] } else if (args[1] === BigInt(1)) { return ['0xtoken', '0xdepositor', BigInt(10), true] } } throw new Error('Execution reverted for an unknown reason.') }) await expect( jumpstartLinkHandler( networkConfig.defaultNetworkId, '0xTEST', mockPrivateKey, mockUserAddress ) ).rejects.toThrow('Already claimed all jumpstart rewards for celo-alfajores') expect(fetchWithTimeout).not.toHaveBeenCalled() }) })