|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import { createMockLendProvider } from '@/test/MockLendProvider.js' |
| 4 | +import { getRandomAddress } from '@/test/utils.js' |
| 5 | +import type { LendProvider } from '@/types/lend.js' |
| 6 | + |
| 7 | +import { WalletLendNamespace } from './WalletLendNamespace.js' |
| 8 | + |
| 9 | +describe('WalletLendNamespace', () => { |
| 10 | + const mockWalletAddress = getRandomAddress() |
| 11 | + let mockProvider: LendProvider |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + mockProvider = createMockLendProvider() |
| 15 | + }) |
| 16 | + |
| 17 | + it('should create an instance with a lend provider and wallet address', () => { |
| 18 | + const namespace = new WalletLendNamespace(mockProvider, mockWalletAddress) |
| 19 | + |
| 20 | + expect(namespace).toBeInstanceOf(WalletLendNamespace) |
| 21 | + }) |
| 22 | + |
| 23 | + it('should inherit read operations from VerbsLendNamespace', async () => { |
| 24 | + const namespace = new WalletLendNamespace(mockProvider, mockWalletAddress) |
| 25 | + const mockVaults = [ |
| 26 | + { |
| 27 | + chainId: 130, |
| 28 | + address: getRandomAddress(), |
| 29 | + name: 'Test Vault', |
| 30 | + asset: getRandomAddress(), |
| 31 | + totalAssets: BigInt('1000000'), |
| 32 | + totalShares: BigInt('1000000'), |
| 33 | + apy: 0.05, |
| 34 | + apyBreakdown: { |
| 35 | + nativeApy: 0.04, |
| 36 | + totalRewardsApr: 0.01, |
| 37 | + performanceFee: 0.0, |
| 38 | + netApy: 0.05, |
| 39 | + }, |
| 40 | + owner: getRandomAddress(), |
| 41 | + curator: getRandomAddress(), |
| 42 | + fee: 0.1, |
| 43 | + lastUpdate: Date.now(), |
| 44 | + }, |
| 45 | + ] |
| 46 | + |
| 47 | + vi.mocked(mockProvider.getVaults).mockResolvedValue(mockVaults) |
| 48 | + |
| 49 | + const result = await namespace.getVaults() |
| 50 | + |
| 51 | + expect(mockProvider.getVaults).toHaveBeenCalled() |
| 52 | + expect(result).toBe(mockVaults) |
| 53 | + }) |
| 54 | + |
| 55 | + describe('lendExecute', () => { |
| 56 | + it('should call provider lend with wallet address as receiver', async () => { |
| 57 | + const namespace = new WalletLendNamespace(mockProvider, mockWalletAddress) |
| 58 | + const asset = getRandomAddress() |
| 59 | + const amount = BigInt('1000000') |
| 60 | + const marketId = 'test-market' |
| 61 | + const mockTransaction = { |
| 62 | + amount, |
| 63 | + asset, |
| 64 | + marketId, |
| 65 | + apy: 0.05, |
| 66 | + timestamp: Date.now(), |
| 67 | + transactionData: { |
| 68 | + deposit: { |
| 69 | + to: asset, |
| 70 | + value: 0n, |
| 71 | + data: '0x' as const, |
| 72 | + }, |
| 73 | + }, |
| 74 | + slippage: 50, |
| 75 | + } |
| 76 | + |
| 77 | + vi.mocked(mockProvider.lend).mockResolvedValue(mockTransaction) |
| 78 | + |
| 79 | + const result = await namespace.lendExecute(asset, amount, marketId) |
| 80 | + |
| 81 | + expect(mockProvider.lend).toHaveBeenCalledWith(asset, amount, marketId, { |
| 82 | + receiver: mockWalletAddress, |
| 83 | + }) |
| 84 | + expect(result).toBe(mockTransaction) |
| 85 | + }) |
| 86 | + |
| 87 | + it('should preserve custom receiver in options', async () => { |
| 88 | + const namespace = new WalletLendNamespace(mockProvider, mockWalletAddress) |
| 89 | + const asset = getRandomAddress() |
| 90 | + const amount = BigInt('1000000') |
| 91 | + const customReceiver = getRandomAddress() |
| 92 | + const options = { receiver: customReceiver, slippage: 100 } |
| 93 | + |
| 94 | + await namespace.lendExecute(asset, amount, undefined, options) |
| 95 | + |
| 96 | + expect(mockProvider.lend).toHaveBeenCalledWith( |
| 97 | + asset, |
| 98 | + amount, |
| 99 | + undefined, |
| 100 | + options, |
| 101 | + ) |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + describe('deposit', () => { |
| 106 | + it('should delegate to lendExecute', async () => { |
| 107 | + const namespace = new WalletLendNamespace(mockProvider, mockWalletAddress) |
| 108 | + const asset = getRandomAddress() |
| 109 | + const amount = BigInt('1000000') |
| 110 | + const marketId = 'test-market' |
| 111 | + const options = { slippage: 75 } |
| 112 | + |
| 113 | + const lendExecuteSpy = vi.spyOn(namespace, 'lendExecute') |
| 114 | + const mockTransaction = { |
| 115 | + amount, |
| 116 | + asset, |
| 117 | + marketId, |
| 118 | + apy: 0.05, |
| 119 | + timestamp: Date.now(), |
| 120 | + transactionData: { |
| 121 | + deposit: { |
| 122 | + to: asset, |
| 123 | + value: 0n, |
| 124 | + data: '0x' as const, |
| 125 | + }, |
| 126 | + }, |
| 127 | + slippage: 75, |
| 128 | + } |
| 129 | + lendExecuteSpy.mockResolvedValue(mockTransaction) |
| 130 | + |
| 131 | + const result = await namespace.deposit(asset, amount, marketId, options) |
| 132 | + |
| 133 | + expect(lendExecuteSpy).toHaveBeenCalledWith( |
| 134 | + asset, |
| 135 | + amount, |
| 136 | + marketId, |
| 137 | + options, |
| 138 | + ) |
| 139 | + expect(result).toBe(mockTransaction) |
| 140 | + }) |
| 141 | + }) |
| 142 | + |
| 143 | + describe('withdraw', () => { |
| 144 | + it('should call provider withdraw with wallet address as receiver', async () => { |
| 145 | + const namespace = new WalletLendNamespace(mockProvider, mockWalletAddress) |
| 146 | + const asset = getRandomAddress() |
| 147 | + const amount = BigInt('500000') |
| 148 | + const marketId = 'test-market' |
| 149 | + const mockTransaction = { |
| 150 | + amount, |
| 151 | + asset, |
| 152 | + marketId, |
| 153 | + apy: 0.05, |
| 154 | + timestamp: Date.now(), |
| 155 | + transactionData: { |
| 156 | + deposit: { |
| 157 | + to: asset, |
| 158 | + value: 0n, |
| 159 | + data: '0x' as const, |
| 160 | + }, |
| 161 | + }, |
| 162 | + slippage: 50, |
| 163 | + } |
| 164 | + |
| 165 | + vi.mocked(mockProvider.withdraw).mockResolvedValue(mockTransaction) |
| 166 | + |
| 167 | + const result = await namespace.withdraw(asset, amount, marketId) |
| 168 | + |
| 169 | + expect(mockProvider.withdraw).toHaveBeenCalledWith( |
| 170 | + asset, |
| 171 | + amount, |
| 172 | + marketId, |
| 173 | + { |
| 174 | + receiver: mockWalletAddress, |
| 175 | + }, |
| 176 | + ) |
| 177 | + expect(result).toBe(mockTransaction) |
| 178 | + }) |
| 179 | + |
| 180 | + it('should preserve custom receiver in options', async () => { |
| 181 | + const namespace = new WalletLendNamespace(mockProvider, mockWalletAddress) |
| 182 | + const asset = getRandomAddress() |
| 183 | + const amount = BigInt('500000') |
| 184 | + const customReceiver = getRandomAddress() |
| 185 | + const options = { receiver: customReceiver, slippage: 200 } |
| 186 | + |
| 187 | + await namespace.withdraw(asset, amount, undefined, options) |
| 188 | + |
| 189 | + expect(mockProvider.withdraw).toHaveBeenCalledWith( |
| 190 | + asset, |
| 191 | + amount, |
| 192 | + undefined, |
| 193 | + options, |
| 194 | + ) |
| 195 | + }) |
| 196 | + }) |
| 197 | + |
| 198 | + it('should store the wallet address', () => { |
| 199 | + const namespace = new WalletLendNamespace(mockProvider, mockWalletAddress) |
| 200 | + |
| 201 | + expect(namespace['address']).toBe(mockWalletAddress) |
| 202 | + }) |
| 203 | +}) |
0 commit comments