UNPKG

@admin-jigsaw/jigsaw-sdk

Version:

Returns predefined data for Jigsaw platform and exposes functionality to retrieve the necessary data

172 lines (171 loc) 10.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const index_1 = require("../index"); const chains_1 = require("viem/chains"); const testClient_1 = require("./utils/testClient"); // Mock the getChainConfig function vitest_1.vi.mock("../constants/chains", () => ({ getChainConfig: vitest_1.vi.fn().mockImplementation((chainId) => { if (chainId === chains_1.sonic.id) { return { constants: { strategyManagerAddress: "0x1234567890123456789012345678901234567890", }, strategies: { aave: { aaveStrategies: { "USDC.e": { stratAddress: testClient_1.MOCK_STRATEGY_ADDRESS_1 }, scUSD: { stratAddress: testClient_1.MOCK_STRATEGY_ADDRESS_2 }, }, }, pendle: { strategies: { wS: [{ stratAddress: testClient_1.MOCK_STRATEGY_ADDRESS_1 }], }, }, }, }; } // Default to mainnet config return { constants: { strategyManagerAddress: "0x1234567890123456789012345678901234567890", }, strategies: { aave: { aaveStrategies: { "USD0++": { stratAddress: testClient_1.MOCK_STRATEGY_ADDRESS_1 }, USDC: { stratAddress: testClient_1.MOCK_STRATEGY_ADDRESS_2 }, }, }, reservoir: { rUSD: { apy: "5.0", stratAddress: testClient_1.MOCK_STRATEGY_ADDRESS_1 }, }, dinero: { pxETH: { apy: "7.5", stratAddress: testClient_1.MOCK_STRATEGY_ADDRESS_2 }, }, pendle: { strategies: { WETH: [{ stratAddress: testClient_1.MOCK_STRATEGY_ADDRESS_1 }], wstETH: [{ stratAddress: testClient_1.MOCK_STRATEGY_ADDRESS_2 }], }, }, }, }; }), })); (0, vitest_1.describe)("JigsawClient Symbol Filtering", () => { let client; let mockReadContract; (0, vitest_1.beforeEach)(() => { // Create a new client instance for each test client = new index_1.JigsawClient("http://localhost:8545", chains_1.mainnet.id); // Mock the readContract method mockReadContract = vitest_1.vi.fn(); client.client.readContract = mockReadContract; }); (0, vitest_1.describe)("getUserStrategies", () => { (0, vitest_1.it)("should return all strategies when no symbol filter is provided", async () => { const mockStrategies = [testClient_1.MOCK_STRATEGY_ADDRESS_1, testClient_1.MOCK_STRATEGY_ADDRESS_2]; mockReadContract.mockResolvedValue(mockStrategies); const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS); (0, vitest_1.expect)(result).toEqual(mockStrategies); (0, vitest_1.expect)(mockReadContract).toHaveBeenCalledWith({ address: "0x1234567890123456789012345678901234567890", abi: vitest_1.expect.any(Array), functionName: "getHoldingToStrategy", args: [testClient_1.MOCK_HOLDING_ADDRESS], }); }); (0, vitest_1.it)("should filter strategies by exact symbol match - USD0++ on mainnet", async () => { const mockStrategies = [testClient_1.MOCK_STRATEGY_ADDRESS_1, testClient_1.MOCK_STRATEGY_ADDRESS_2]; mockReadContract.mockResolvedValue(mockStrategies); const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, "USD0++"); (0, vitest_1.expect)(result).toEqual([testClient_1.MOCK_STRATEGY_ADDRESS_1]); }); (0, vitest_1.it)("should filter strategies by exact symbol match - USDC.e on sonic", async () => { // Create client for Sonic chain const sonicClient = new index_1.JigsawClient("http://localhost:8545", chains_1.sonic.id); const mockSonicReadContract = vitest_1.vi.fn(); sonicClient.client.readContract = mockSonicReadContract; const mockStrategies = [testClient_1.MOCK_STRATEGY_ADDRESS_1, testClient_1.MOCK_STRATEGY_ADDRESS_2]; mockSonicReadContract.mockResolvedValue(mockStrategies); const result = await sonicClient.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, "USDC.e"); (0, vitest_1.expect)(result).toEqual([testClient_1.MOCK_STRATEGY_ADDRESS_1]); }); (0, vitest_1.it)("should be case insensitive when filtering", async () => { const mockStrategies = [testClient_1.MOCK_STRATEGY_ADDRESS_1, testClient_1.MOCK_STRATEGY_ADDRESS_2]; mockReadContract.mockResolvedValue(mockStrategies); const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, "usd0++"); (0, vitest_1.expect)(result).toEqual([testClient_1.MOCK_STRATEGY_ADDRESS_1]); }); (0, vitest_1.it)("should handle mixed case symbols correctly", async () => { const mockStrategies = [testClient_1.MOCK_STRATEGY_ADDRESS_1, testClient_1.MOCK_STRATEGY_ADDRESS_2]; mockReadContract.mockResolvedValue(mockStrategies); const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, "Usd0++"); (0, vitest_1.expect)(result).toEqual([testClient_1.MOCK_STRATEGY_ADDRESS_1]); }); (0, vitest_1.it)("should return empty array for non-existent symbol", async () => { const mockStrategies = [testClient_1.MOCK_STRATEGY_ADDRESS_1, testClient_1.MOCK_STRATEGY_ADDRESS_2]; mockReadContract.mockResolvedValue(mockStrategies); const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, "NONEXISTENT"); (0, vitest_1.expect)(result).toEqual([]); }); (0, vitest_1.it)("should filter reservoir strategies correctly", async () => { const mockStrategies = [testClient_1.MOCK_STRATEGY_ADDRESS_1, testClient_1.MOCK_STRATEGY_ADDRESS_2]; mockReadContract.mockResolvedValue(mockStrategies); const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, "rUSD"); (0, vitest_1.expect)(result).toEqual([testClient_1.MOCK_STRATEGY_ADDRESS_1]); }); (0, vitest_1.it)("should filter dinero strategies correctly", async () => { const mockStrategies = [testClient_1.MOCK_STRATEGY_ADDRESS_1, testClient_1.MOCK_STRATEGY_ADDRESS_2]; mockReadContract.mockResolvedValue(mockStrategies); const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, "pxETH"); (0, vitest_1.expect)(result).toEqual([testClient_1.MOCK_STRATEGY_ADDRESS_2]); }); (0, vitest_1.it)("should filter pendle strategies correctly", async () => { const mockStrategies = [testClient_1.MOCK_STRATEGY_ADDRESS_1, testClient_1.MOCK_STRATEGY_ADDRESS_2]; mockReadContract.mockResolvedValue(mockStrategies); const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, "WETH"); (0, vitest_1.expect)(result).toEqual([testClient_1.MOCK_STRATEGY_ADDRESS_1]); }); (0, vitest_1.it)("should handle symbols with special characters without modification", async () => { const mockStrategies = [testClient_1.MOCK_STRATEGY_ADDRESS_1, testClient_1.MOCK_STRATEGY_ADDRESS_2]; mockReadContract.mockResolvedValue(mockStrategies); // Test that USD0++ is searched for exactly, not mapped from USD0 const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, "USD0++"); (0, vitest_1.expect)(result).toEqual([testClient_1.MOCK_STRATEGY_ADDRESS_1]); }); (0, vitest_1.it)("should NOT match USD0 to USD0++ (special case handling removed)", async () => { const mockStrategies = [testClient_1.MOCK_STRATEGY_ADDRESS_1, testClient_1.MOCK_STRATEGY_ADDRESS_2]; mockReadContract.mockResolvedValue(mockStrategies); // This should return empty array since USD0 should not be mapped to USD0++ const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, "USD0"); (0, vitest_1.expect)(result).toEqual([]); }); (0, vitest_1.it)("should handle contract read errors gracefully", async () => { mockReadContract.mockRejectedValue(new Error("Contract read failed")); const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, "USD0++"); (0, vitest_1.expect)(result).toEqual([]); }); (0, vitest_1.it)("should handle non-array responses from contract", async () => { mockReadContract.mockResolvedValue(null); const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, "USD0++"); (0, vitest_1.expect)(result).toEqual([]); }); (0, vitest_1.it)("should handle empty strategy arrays", async () => { mockReadContract.mockResolvedValue([]); const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, "USD0++"); (0, vitest_1.expect)(result).toEqual([]); }); (0, vitest_1.it)("should handle whitespace in symbols correctly", async () => { const mockStrategies = [testClient_1.MOCK_STRATEGY_ADDRESS_1, testClient_1.MOCK_STRATEGY_ADDRESS_2]; mockReadContract.mockResolvedValue(mockStrategies); // Test that trimmed symbols don't get special handling const result = await client.getUserStrategies(testClient_1.MOCK_HOLDING_ADDRESS, " USD0++ "); // Since we removed special case handling, this should return empty (0, vitest_1.expect)(result).toEqual([]); }); }); });