|
| 1 | +import { assertOk, bigDecimal, evmAddress } from '@aave/types'; |
| 2 | +import { beforeAll, describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { |
| 5 | + client, |
| 6 | + createNewWallet, |
| 7 | + ETHEREUM_FORK_ID, |
| 8 | + ETHEREUM_MARKET_ADDRESS, |
| 9 | + fundErc20Address, |
| 10 | + WETH_ADDRESS, |
| 11 | + wait, |
| 12 | +} from '../test-utils'; |
| 13 | +import { sendWith } from '../viem'; |
| 14 | +import { collateralToggle, supply } from './transactions'; |
| 15 | +import { userSupplies } from './user'; |
| 16 | + |
| 17 | +describe('Given Aave Market', () => { |
| 18 | + describe('And a user with a supply position', () => { |
| 19 | + describe('When the user toggles the position as collateral', () => { |
| 20 | + const wallet = createNewWallet(); |
| 21 | + |
| 22 | + beforeAll(async () => { |
| 23 | + await fundErc20Address( |
| 24 | + WETH_ADDRESS, |
| 25 | + evmAddress(wallet.account!.address), |
| 26 | + bigDecimal('0.02'), |
| 27 | + ); |
| 28 | + |
| 29 | + const result = await supply(client, { |
| 30 | + market: ETHEREUM_MARKET_ADDRESS, |
| 31 | + chainId: ETHEREUM_FORK_ID, |
| 32 | + supplier: evmAddress(wallet.account!.address), |
| 33 | + amount: { erc20: { currency: WETH_ADDRESS, value: '0.01' } }, |
| 34 | + }).andThen(sendWith(wallet)); |
| 35 | + assertOk(result); |
| 36 | + }); |
| 37 | + |
| 38 | + it('Then it should be reflected in the user supply positions', async () => { |
| 39 | + const userSuppliesBefore = await userSupplies(client, { |
| 40 | + markets: [ |
| 41 | + { address: ETHEREUM_MARKET_ADDRESS, chainId: ETHEREUM_FORK_ID }, |
| 42 | + ], |
| 43 | + user: evmAddress(wallet.account!.address), |
| 44 | + }); |
| 45 | + assertOk(userSuppliesBefore); |
| 46 | + expect(userSuppliesBefore.value.length).toBe(1); |
| 47 | + |
| 48 | + // Toggle collateral |
| 49 | + const result = await collateralToggle(client, { |
| 50 | + market: ETHEREUM_MARKET_ADDRESS, |
| 51 | + chainId: ETHEREUM_FORK_ID, |
| 52 | + user: evmAddress(wallet.account!.address), |
| 53 | + underlyingToken: WETH_ADDRESS, |
| 54 | + }) |
| 55 | + .andThen(sendWith(wallet)) |
| 56 | + .andTee((tx) => console.log(`tx to toggle collateral: ${tx}`)) |
| 57 | + .andTee(() => wait(1000)); |
| 58 | + assertOk(result); |
| 59 | + |
| 60 | + const userSuppliesAfter = await userSupplies(client, { |
| 61 | + markets: [ |
| 62 | + { address: ETHEREUM_MARKET_ADDRESS, chainId: ETHEREUM_FORK_ID }, |
| 63 | + ], |
| 64 | + user: evmAddress(wallet.account!.address), |
| 65 | + }); |
| 66 | + assertOk(userSuppliesAfter); |
| 67 | + expect(userSuppliesAfter.value.length).toBe(1); |
| 68 | + |
| 69 | + expect(userSuppliesAfter.value[0]?.isCollateral).toEqual( |
| 70 | + !userSuppliesBefore.value[0]?.isCollateral, |
| 71 | + ); |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments