|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import type { Address, PublicClient } from "viem"; |
| 3 | +import { |
| 4 | + Decimal, |
| 5 | + type SmartMarketExtraInfo, |
| 6 | +} from "@lista-dao/moolah-sdk-core"; |
| 7 | + |
| 8 | +import { getSmartMarketUserData } from "../../../read/smart/getSmartMarketUserData.js"; |
| 9 | + |
| 10 | +const MOOLAH = "0x1111111111111111111111111111111111111111" as Address; |
| 11 | +const MARKET_ID = |
| 12 | + "0xe20b1f2a32a83b51ec4357fdf4eb5a18c0dc790d2f33dc2d244cd4b1ceb99c50" as Address; |
| 13 | +const USER = "0x05E3A7a66945ca9aF73f66660f22ffB36332FA54" as Address; |
| 14 | + |
| 15 | +const LP_TOKEN = "0x2222222222222222222222222222222222222222" as Address; |
| 16 | +const USD1 = "0x8d0D000Ee44948FC98c9B98A4FA4921476f08B0d" as Address; |
| 17 | +const USDT = "0xdAC17F958D2ee523a2206206994597C13D831ec7" as Address; |
| 18 | + |
| 19 | +describe("getSmartMarketUserData", () => { |
| 20 | + it("uses token-specific decimals when reading ERC20 balances", async () => { |
| 21 | + const mockReadContract = vi.fn( |
| 22 | + async ({ |
| 23 | + functionName, |
| 24 | + address, |
| 25 | + }: { |
| 26 | + functionName: string; |
| 27 | + address: Address; |
| 28 | + }) => { |
| 29 | + if (functionName === "position") return [0n, 0n, 0n]; |
| 30 | + if (functionName === "isWhiteList") return false; |
| 31 | + if (functionName === "balanceOf" && address === LP_TOKEN) { |
| 32 | + return 1000000000000000000n; |
| 33 | + } |
| 34 | + if (functionName === "balanceOf" && address === USD1) { |
| 35 | + return 20450713159115803238n; |
| 36 | + } |
| 37 | + if (functionName === "balanceOf" && address === USDT) { |
| 38 | + return 13834453n; |
| 39 | + } |
| 40 | + throw new Error(`Unexpected call: ${functionName} @ ${address}`); |
| 41 | + }, |
| 42 | + ); |
| 43 | + |
| 44 | + const mockPublicClient = { |
| 45 | + readContract: mockReadContract, |
| 46 | + getBalance: vi.fn().mockResolvedValue(0n), |
| 47 | + } as unknown as PublicClient; |
| 48 | + |
| 49 | + const extraInfo = { |
| 50 | + loanInfo: { address: USD1, decimals: 18, symbol: "USD1" }, |
| 51 | + lpInfo: { address: LP_TOKEN, decimals: 18, symbol: "LP" }, |
| 52 | + tokenAInfo: { address: USD1, decimals: 18, symbol: "USD1" }, |
| 53 | + tokenBInfo: { address: USDT, decimals: 6, symbol: "USDT" }, |
| 54 | + LLTV: new Decimal(8n, 1), |
| 55 | + loanIsNative: false, |
| 56 | + tokenAIsNative: false, |
| 57 | + tokenBIsNative: false, |
| 58 | + rateView: 0n, |
| 59 | + borrowRate: Decimal.ZERO, |
| 60 | + priceRate: Decimal.ONE, |
| 61 | + balances: [Decimal.ZERO, Decimal.ZERO] as [Decimal, Decimal], |
| 62 | + totalLp: Decimal.ONE, |
| 63 | + lastUpdate: 0n, |
| 64 | + totalBorrowShares: 0n, |
| 65 | + totalBorrowAssets: 0n, |
| 66 | + } as unknown as SmartMarketExtraInfo; |
| 67 | + |
| 68 | + const result = await getSmartMarketUserData( |
| 69 | + mockPublicClient, |
| 70 | + { moolah: MOOLAH }, |
| 71 | + MARKET_ID, |
| 72 | + USER, |
| 73 | + extraInfo, |
| 74 | + ); |
| 75 | + |
| 76 | + expect(result.balances.tokenA.decimal).toBe(18); |
| 77 | + expect(result.balances.tokenA.toString(8)).toBe("20.45071315"); |
| 78 | + |
| 79 | + expect(result.balances.tokenB.decimal).toBe(6); |
| 80 | + expect(result.balances.tokenB.toString(6)).toBe("13.834453"); |
| 81 | + }); |
| 82 | +}); |
0 commit comments