|
| 1 | +import { assertOk, chainId, evmAddress } from '@aave/client'; |
| 2 | +import { TimeWindow } from '@aave/graphql'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { |
| 5 | + client, |
| 6 | + createNewWallet, |
| 7 | + ETHEREUM_FORK_ID, |
| 8 | + ETHEREUM_MARKET_ADDRESS, |
| 9 | + WETH_ADDRESS, |
| 10 | +} from '../test-utils'; |
| 11 | +import { borrowAPYHistory, reserve, supplyAPYHistory } from './reserve'; |
| 12 | + |
| 13 | +function windowToDate(window: TimeWindow): Date { |
| 14 | + switch (window) { |
| 15 | + case TimeWindow.LastDay: |
| 16 | + return new Date(Date.now() - 1000 * 60 * 60 * 24); |
| 17 | + case TimeWindow.LastWeek: |
| 18 | + return new Date(Date.now() - 1000 * 60 * 60 * 24 * 7); |
| 19 | + case TimeWindow.LastMonth: |
| 20 | + return new Date(Date.now() - 1000 * 60 * 60 * 24 * 30); |
| 21 | + case TimeWindow.LastYear: |
| 22 | + return new Date(Date.now() - 1000 * 60 * 60 * 24 * 365); |
| 23 | + default: |
| 24 | + throw new Error(`Unknown window: ${window}`); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +describe('Given an Aave Market reserve', () => { |
| 29 | + const wallet = createNewWallet(); |
| 30 | + const windowEnum = Object.values(TimeWindow); |
| 31 | + |
| 32 | + describe('When fetching the reserve data', () => { |
| 33 | + it('Then it should return the expected reserve details', async () => { |
| 34 | + const result = await reserve(client, { |
| 35 | + market: ETHEREUM_MARKET_ADDRESS, |
| 36 | + chainId: ETHEREUM_FORK_ID, |
| 37 | + underlyingToken: WETH_ADDRESS, |
| 38 | + user: evmAddress(wallet.account!.address), |
| 39 | + }); |
| 40 | + assertOk(result); |
| 41 | + expect(result.value).toMatchSnapshot({ |
| 42 | + aToken: expect.any(Object), |
| 43 | + underlyingToken: { |
| 44 | + address: WETH_ADDRESS, |
| 45 | + }, |
| 46 | + vToken: expect.any(Object), |
| 47 | + supplyInfo: expect.any(Object), |
| 48 | + borrowInfo: expect.any(Object), |
| 49 | + eModeInfo: expect.any(Array), |
| 50 | + market: expect.any(Object), |
| 51 | + acceptsNative: expect.any(Object), |
| 52 | + size: expect.any(Object), |
| 53 | + userState: expect.any(Object), |
| 54 | + }); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('When fetching the borrow APY history for it', () => { |
| 59 | + it.each(windowEnum)( |
| 60 | + 'Then it should return a time series for the specified window %s', |
| 61 | + async (window) => { |
| 62 | + const result = await borrowAPYHistory(client, { |
| 63 | + market: ETHEREUM_MARKET_ADDRESS, |
| 64 | + chainId: chainId(1), |
| 65 | + underlyingToken: WETH_ADDRESS, |
| 66 | + window, |
| 67 | + }); |
| 68 | + assertOk(result); |
| 69 | + expect(result.value?.length).toBeGreaterThan(0); |
| 70 | + expect(result.value).toEqual( |
| 71 | + result.value?.map(() => |
| 72 | + expect.objectContaining({ |
| 73 | + avgRate: expect.any(Object), |
| 74 | + date: expect.toBeBetween(windowToDate(window), new Date()), |
| 75 | + }), |
| 76 | + ), |
| 77 | + ); |
| 78 | + }, |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('When fetching the supply APY history for it', () => { |
| 83 | + it.each(windowEnum)( |
| 84 | + 'Then it should return a time series for the specified window %s', |
| 85 | + async (window) => { |
| 86 | + const result = await supplyAPYHistory(client, { |
| 87 | + market: ETHEREUM_MARKET_ADDRESS, |
| 88 | + chainId: chainId(1), |
| 89 | + underlyingToken: WETH_ADDRESS, |
| 90 | + window, |
| 91 | + }); |
| 92 | + assertOk(result); |
| 93 | + expect(result.value?.length).toBeGreaterThan(0); |
| 94 | + expect(result.value).toEqual( |
| 95 | + result.value?.map(() => |
| 96 | + expect.objectContaining({ |
| 97 | + avgRate: expect.any(Object), |
| 98 | + date: expect.toBeBetween(windowToDate(window), new Date()), |
| 99 | + }), |
| 100 | + ), |
| 101 | + ); |
| 102 | + }, |
| 103 | + ); |
| 104 | + }); |
| 105 | +}); |
0 commit comments