|
| 1 | +import { |
| 2 | + assertOk, |
| 3 | + OrderDirection, |
| 4 | + ReservesRequestFilter, |
| 5 | +} from '@aave/client-next'; |
| 6 | +import { reserves } from '@aave/client-next/actions'; |
| 7 | +import { |
| 8 | + client, |
| 9 | + ETHEREUM_FORK_ID, |
| 10 | + ETHEREUM_HUB_CORE_ADDRESS, |
| 11 | + ETHEREUM_SPOKE_CORE_ADDRESS, |
| 12 | + ETHEREUM_USDC_ADDRESS, |
| 13 | + ETHEREUM_WETH_ADDRESS, |
| 14 | +} from '@aave/client-next/test-utils'; |
| 15 | +import { describe, expect, it } from 'vitest'; |
| 16 | +import { assertNonEmptyArray } from '../test-utils'; |
| 17 | + |
| 18 | +describe('Aave V4 Reserve Scenario', () => { |
| 19 | + describe('Given a user who wants to fetch reserves', () => { |
| 20 | + describe('When fetching reserves for a specific hub token', () => { |
| 21 | + it('Then it should return the reserves for the specific token', async () => { |
| 22 | + const listReserves = await reserves(client, { |
| 23 | + query: { |
| 24 | + hubToken: { |
| 25 | + token: ETHEREUM_USDC_ADDRESS, |
| 26 | + hub: ETHEREUM_HUB_CORE_ADDRESS, |
| 27 | + chainId: ETHEREUM_FORK_ID, |
| 28 | + }, |
| 29 | + }, |
| 30 | + }); |
| 31 | + assertOk(listReserves); |
| 32 | + assertNonEmptyArray(listReserves.value); |
| 33 | + |
| 34 | + listReserves.value.forEach((elem) => { |
| 35 | + expect(elem.asset.hub.address).toEqual(ETHEREUM_HUB_CORE_ADDRESS); |
| 36 | + expect(elem.asset.underlying.address).toEqual(ETHEREUM_USDC_ADDRESS); |
| 37 | + }); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('When fetching reserves for a specific spoke token', () => { |
| 42 | + it('Then it should return the reserves for the specific token', async () => { |
| 43 | + const listReserves = await reserves(client, { |
| 44 | + query: { |
| 45 | + spokeToken: { |
| 46 | + token: ETHEREUM_USDC_ADDRESS, |
| 47 | + spoke: ETHEREUM_SPOKE_CORE_ADDRESS, |
| 48 | + chainId: ETHEREUM_FORK_ID, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }); |
| 52 | + assertOk(listReserves); |
| 53 | + assertNonEmptyArray(listReserves.value); |
| 54 | + |
| 55 | + listReserves.value.forEach((elem) => { |
| 56 | + expect(elem.spoke.address).toEqual(ETHEREUM_SPOKE_CORE_ADDRESS); |
| 57 | + expect(elem.asset.underlying.address).toEqual(ETHEREUM_USDC_ADDRESS); |
| 58 | + }); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('When fetching reserves for a specific spoke', () => { |
| 63 | + it('Then it should return the reserves for that specific spoke', async () => { |
| 64 | + const listReserves = await reserves(client, { |
| 65 | + query: { |
| 66 | + spoke: { |
| 67 | + chainId: ETHEREUM_FORK_ID, |
| 68 | + address: ETHEREUM_SPOKE_CORE_ADDRESS, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }); |
| 72 | + assertOk(listReserves); |
| 73 | + assertNonEmptyArray(listReserves.value); |
| 74 | + |
| 75 | + listReserves.value.forEach((elem) => { |
| 76 | + expect(elem.spoke.address).toEqual(ETHEREUM_SPOKE_CORE_ADDRESS); |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('When fetching reserves for a specific tokens', () => { |
| 82 | + it('Then it should return the reserves for the specific tokens', async () => { |
| 83 | + const tokens = [ETHEREUM_USDC_ADDRESS, ETHEREUM_WETH_ADDRESS]; |
| 84 | + const listReserves = await reserves(client, { |
| 85 | + query: { |
| 86 | + tokens: tokens.map((token) => ({ |
| 87 | + chainId: ETHEREUM_FORK_ID, |
| 88 | + address: token, |
| 89 | + })), |
| 90 | + }, |
| 91 | + }); |
| 92 | + assertOk(listReserves); |
| 93 | + assertNonEmptyArray(listReserves.value); |
| 94 | + |
| 95 | + listReserves.value.forEach((elem) => { |
| 96 | + expect(tokens.includes(elem.asset.underlying.address)).toBe(true); |
| 97 | + }); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('When fetching reserves for a specific chainId', () => { |
| 102 | + it('Then it should return the reserves for the specific chainId', async () => { |
| 103 | + const listReserves = await reserves(client, { |
| 104 | + query: { |
| 105 | + chainIds: [ETHEREUM_FORK_ID], |
| 106 | + }, |
| 107 | + }); |
| 108 | + assertOk(listReserves); |
| 109 | + assertNonEmptyArray(listReserves.value); |
| 110 | + |
| 111 | + listReserves.value.forEach((elem) => { |
| 112 | + expect(elem.chain.chainId).toEqual(ETHEREUM_FORK_ID); |
| 113 | + }); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('When fetching reserves for a specific hub', () => { |
| 118 | + it('Then it should return the reserves for the specific hub', async () => { |
| 119 | + const listReserves = await reserves(client, { |
| 120 | + query: { |
| 121 | + hub: { |
| 122 | + chainId: ETHEREUM_FORK_ID, |
| 123 | + address: ETHEREUM_HUB_CORE_ADDRESS, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }); |
| 127 | + assertOk(listReserves); |
| 128 | + assertNonEmptyArray(listReserves.value); |
| 129 | + |
| 130 | + listReserves.value.forEach((elem) => { |
| 131 | + expect(elem.asset.hub.address).toEqual(ETHEREUM_HUB_CORE_ADDRESS); |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('When fetching reserves ordered by', () => { |
| 137 | + // TODO: Code is not implemented in the backend yet |
| 138 | + it.skip('Then it should return reserves ordered by assetName', async () => { |
| 139 | + let listReserves = await reserves(client, { |
| 140 | + query: { |
| 141 | + chainIds: [ETHEREUM_FORK_ID], |
| 142 | + }, |
| 143 | + orderBy: { assetName: OrderDirection.Asc }, |
| 144 | + }); |
| 145 | + assertOk(listReserves); |
| 146 | + assertNonEmptyArray(listReserves.value); |
| 147 | + |
| 148 | + const listNamesAsc = listReserves.value.map( |
| 149 | + (elem) => elem.asset.underlying.info.name, |
| 150 | + ); |
| 151 | + expect(listNamesAsc).toEqual(listNamesAsc.sort()); |
| 152 | + |
| 153 | + listReserves = await reserves(client, { |
| 154 | + query: { |
| 155 | + chainIds: [ETHEREUM_FORK_ID], |
| 156 | + }, |
| 157 | + orderBy: { assetName: OrderDirection.Desc }, |
| 158 | + }); |
| 159 | + assertOk(listReserves); |
| 160 | + assertNonEmptyArray(listReserves.value); |
| 161 | + const listNamesDesc = listReserves.value.map( |
| 162 | + (elem) => elem.asset.underlying.info.name, |
| 163 | + ); |
| 164 | + expect(listNamesDesc).toEqual(listNamesDesc.sort().reverse()); |
| 165 | + }); |
| 166 | + |
| 167 | + it.todo('Then it should return reserves ordered by borrowApy'); |
| 168 | + it.todo('Then it should return reserves ordered by supplyApy'); |
| 169 | + it.todo('Then it should return reserves ordered by collateralFactor'); |
| 170 | + it.todo('Then it should return reserves ordered by userBalance'); |
| 171 | + }); |
| 172 | + |
| 173 | + describe('When fetching reserves filtered by', () => { |
| 174 | + const status = Object.values(ReservesRequestFilter); |
| 175 | + it.each(status)( |
| 176 | + 'Then it should return only reserves with a status: %s', |
| 177 | + async (status) => { |
| 178 | + const listReserves = await reserves(client, { |
| 179 | + query: { |
| 180 | + chainIds: [ETHEREUM_FORK_ID], |
| 181 | + }, |
| 182 | + filter: status, |
| 183 | + }); |
| 184 | + assertOk(listReserves); |
| 185 | + assertNonEmptyArray(listReserves.value); |
| 186 | + |
| 187 | + listReserves.value.forEach((elem) => { |
| 188 | + if (status === ReservesRequestFilter.Borrow) { |
| 189 | + expect(elem.canBorrow).toBeTrue(); |
| 190 | + } |
| 191 | + if (status === ReservesRequestFilter.Supply) { |
| 192 | + expect(elem.canSupply).toBeTrue(); |
| 193 | + } |
| 194 | + }); |
| 195 | + }, |
| 196 | + ); |
| 197 | + }); |
| 198 | + }); |
| 199 | +}); |
0 commit comments