|
| 1 | +import { encodeFunctionData, parseUnits, type Hex } from "viem"; |
| 2 | +import { EvmWalletProvider } from "../../wallet-providers/evmWalletProvider"; |
| 3 | +import { FloeActionProvider } from "./floeActionProvider"; |
| 4 | +import { LENDING_MATCHER_ADDRESSES, LENDING_MATCHER_ABI, USDC_DECIMALS } from "./constants"; |
| 5 | +import { Network } from "../../network"; |
| 6 | + |
| 7 | +describe("Floe Action Provider", () => { |
| 8 | + const actionProvider = new FloeActionProvider("https://mock-api.test"); |
| 9 | + let mockWallet: jest.Mocked<EvmWalletProvider>; |
| 10 | + |
| 11 | + const MOCK_NETWORK: Network = { protocolFamily: "evm", networkId: "base-mainnet" }; |
| 12 | + const MOCK_ADDRESS = "0x1234567890abcdef1234567890abcdef12345678"; |
| 13 | + const MOCK_TX_HASH = "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as Hex; |
| 14 | + const MOCK_RECEIPT = { status: 1, blockNumber: 123456 }; |
| 15 | + const MOCK_SIGNATURE = "0xmocksignature"; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + mockWallet = { |
| 19 | + getAddress: jest.fn().mockResolvedValue(MOCK_ADDRESS), |
| 20 | + getNetwork: jest.fn().mockReturnValue(MOCK_NETWORK), |
| 21 | + sendTransaction: jest.fn().mockResolvedValue(MOCK_TX_HASH), |
| 22 | + waitForTransactionReceipt: jest.fn().mockResolvedValue(MOCK_RECEIPT), |
| 23 | + signMessage: jest.fn().mockResolvedValue(MOCK_SIGNATURE), |
| 24 | + readContract: jest.fn(), |
| 25 | + } as unknown as jest.Mocked<EvmWalletProvider>; |
| 26 | + |
| 27 | + global.fetch = jest.fn(); |
| 28 | + jest.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe("supportsNetwork", () => { |
| 32 | + it("returns true for base-mainnet", () => { |
| 33 | + expect(actionProvider.supportsNetwork(MOCK_NETWORK)).toBe(true); |
| 34 | + }); |
| 35 | + |
| 36 | + it("returns false for base-sepolia", () => { |
| 37 | + expect( |
| 38 | + actionProvider.supportsNetwork({ protocolFamily: "evm", networkId: "base-sepolia" }), |
| 39 | + ).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + it("returns false for unsupported networks", () => { |
| 43 | + expect( |
| 44 | + actionProvider.supportsNetwork({ protocolFamily: "evm", networkId: "ethereum-mainnet" }), |
| 45 | + ).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it("returns false for non-evm networks", () => { |
| 49 | + expect( |
| 50 | + actionProvider.supportsNetwork({ protocolFamily: "solana", networkId: "base-mainnet" }), |
| 51 | + ).toBe(false); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe("getMarkets", () => { |
| 56 | + it("returns formatted market data", async () => { |
| 57 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 58 | + ok: true, |
| 59 | + json: async () => ({ |
| 60 | + markets: [ |
| 61 | + { collateralSymbol: "USDC", loanSymbol: "USDC" }, |
| 62 | + ], |
| 63 | + }), |
| 64 | + }); |
| 65 | + |
| 66 | + const result = await actionProvider.getMarkets(mockWallet, {}); |
| 67 | + expect(result).toContain("Floe Lending Markets"); |
| 68 | + expect(result).toContain("USDC/USDC"); |
| 69 | + }); |
| 70 | + |
| 71 | + it("handles empty markets", async () => { |
| 72 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 73 | + ok: true, |
| 74 | + json: async () => ({ markets: [] }), |
| 75 | + }); |
| 76 | + |
| 77 | + const result = await actionProvider.getMarkets(mockWallet, {}); |
| 78 | + expect(result).toContain("No active markets"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("handles API errors", async () => { |
| 82 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 83 | + ok: false, |
| 84 | + status: 500, |
| 85 | + }); |
| 86 | + |
| 87 | + const result = await actionProvider.getMarkets(mockWallet, {}); |
| 88 | + expect(result).toContain("Error fetching markets"); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe("instantBorrow", () => { |
| 93 | + it("calls the correct API with auth headers", async () => { |
| 94 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 95 | + ok: true, |
| 96 | + json: async () => ({ loanId: "42" }), |
| 97 | + }); |
| 98 | + |
| 99 | + const result = await actionProvider.instantBorrow(mockWallet, { |
| 100 | + borrowAmount: "1000", |
| 101 | + collateralAmount: "10000", |
| 102 | + maxInterestRateBps: "800", |
| 103 | + duration: "1209600", |
| 104 | + }); |
| 105 | + |
| 106 | + expect(global.fetch).toHaveBeenCalledWith( |
| 107 | + "https://mock-api.test/v1/credit/instant-borrow", |
| 108 | + expect.objectContaining({ method: "POST" }), |
| 109 | + ); |
| 110 | + expect(result).toContain("Loan Created"); |
| 111 | + expect(result).toContain("1000 USDC"); |
| 112 | + expect(result).toContain("10000 USDC"); |
| 113 | + }); |
| 114 | + |
| 115 | + it("handles no liquidity", async () => { |
| 116 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 117 | + ok: false, |
| 118 | + status: 404, |
| 119 | + json: async () => ({ error: "no_liquidity" }), |
| 120 | + }); |
| 121 | + |
| 122 | + const result = await actionProvider.instantBorrow(mockWallet, { |
| 123 | + borrowAmount: "1000", |
| 124 | + collateralAmount: "10000", |
| 125 | + maxInterestRateBps: "800", |
| 126 | + duration: "1209600", |
| 127 | + }); |
| 128 | + |
| 129 | + expect(result).toContain("No lenders available"); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe("grantDelegation", () => { |
| 134 | + it("encodes setOperator correctly and sends transaction", async () => { |
| 135 | + const result = await actionProvider.grantDelegation(mockWallet, { |
| 136 | + facilitatorAddress: "0x58EDdE022FFDAD3Fb0Fb0E7D51eb05AaF66a31f1", |
| 137 | + borrowLimit: "10000", |
| 138 | + maxRateBps: "1500", |
| 139 | + expiryDays: "90", |
| 140 | + }); |
| 141 | + |
| 142 | + expect(mockWallet.sendTransaction).toHaveBeenCalledWith( |
| 143 | + expect.objectContaining({ |
| 144 | + to: LENDING_MATCHER_ADDRESSES["base-mainnet"], |
| 145 | + }), |
| 146 | + ); |
| 147 | + expect(result).toContain("Credit Delegation Granted"); |
| 148 | + expect(result).toContain("10000 USDC"); |
| 149 | + expect(result).toContain("15.00% APR"); |
| 150 | + expect(result).toContain("90 days"); |
| 151 | + }); |
| 152 | + |
| 153 | + it("rejects unsupported networks", async () => { |
| 154 | + mockWallet.getNetwork.mockReturnValue({ |
| 155 | + protocolFamily: "evm", |
| 156 | + networkId: "ethereum-mainnet", |
| 157 | + } as Network); |
| 158 | + |
| 159 | + const result = await actionProvider.grantDelegation(mockWallet, { |
| 160 | + facilitatorAddress: "0x58EDdE022FFDAD3Fb0Fb0E7D51eb05AaF66a31f1", |
| 161 | + borrowLimit: "10000", |
| 162 | + maxRateBps: "1500", |
| 163 | + expiryDays: "90", |
| 164 | + }); |
| 165 | + |
| 166 | + expect(result).toContain("not supported"); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + describe("checkStatus", () => { |
| 171 | + it("returns formatted loan status", async () => { |
| 172 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 173 | + ok: true, |
| 174 | + json: async () => ({ |
| 175 | + remainingPrincipal: "500000000", |
| 176 | + accruedInterest: "12500000", |
| 177 | + interestRateBps: 800, |
| 178 | + status: "active", |
| 179 | + timeToExpiry: "10 days", |
| 180 | + }), |
| 181 | + }); |
| 182 | + |
| 183 | + const result = await actionProvider.checkStatus(mockWallet, { loanId: "42" }); |
| 184 | + expect(result).toContain("Loan #42 Status"); |
| 185 | + expect(result).toContain("8.00% APR"); |
| 186 | + expect(result).toContain("active"); |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + describe("x402Fetch", () => { |
| 191 | + it("calls proxy endpoint with auth headers", async () => { |
| 192 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 193 | + ok: true, |
| 194 | + status: 200, |
| 195 | + headers: new Map([["payment-response", "0xtxhash"]]), |
| 196 | + text: async () => '{"data": "premium content"}', |
| 197 | + }); |
| 198 | + |
| 199 | + const result = await actionProvider.x402Fetch(mockWallet, { |
| 200 | + url: "https://api.example.com/data", |
| 201 | + }); |
| 202 | + |
| 203 | + expect(global.fetch).toHaveBeenCalledWith( |
| 204 | + "https://mock-api.test/v1/proxy/fetch", |
| 205 | + expect.objectContaining({ method: "POST" }), |
| 206 | + ); |
| 207 | + expect(result).toContain("x402 Response"); |
| 208 | + }); |
| 209 | + |
| 210 | + it("handles insufficient balance", async () => { |
| 211 | + (global.fetch as jest.Mock).mockResolvedValueOnce({ |
| 212 | + ok: false, |
| 213 | + status: 402, |
| 214 | + json: async () => ({ error: "insufficient_balance", available: "100", required: "500" }), |
| 215 | + }); |
| 216 | + |
| 217 | + const result = await actionProvider.x402Fetch(mockWallet, { |
| 218 | + url: "https://api.example.com/data", |
| 219 | + }); |
| 220 | + |
| 221 | + expect(result).toContain("Insufficient credit balance"); |
| 222 | + }); |
| 223 | + }); |
| 224 | +}); |
0 commit comments