|
| 1 | +import type { |
| 2 | + Market, |
| 3 | + MarketUserReserveSupplyPosition, |
| 4 | + SupplyRequest, |
| 5 | +} from '@aave/graphql'; |
| 6 | +import { assertOk, bigDecimal, evmAddress } from '@aave/types'; |
| 7 | +import type { WalletClient } from 'viem'; |
| 8 | +import { beforeAll, describe, expect, it } from 'vitest'; |
| 9 | +import { |
| 10 | + client, |
| 11 | + createNewWallet, |
| 12 | + DEFAULT_MARKET_ADDRESS, |
| 13 | + ETHEREUM_FORK_ID, |
| 14 | + fundErc20Address, |
| 15 | + WETH_ADDRESS, |
| 16 | +} from '../test-utils'; |
| 17 | +import { sendWith } from '../viem'; |
| 18 | +import { market } from './markets'; |
| 19 | +import { borrow, collateralToggle, supply } from './transactions'; |
| 20 | +import { userBorrows, userSupplies } from './user'; |
| 21 | + |
| 22 | +async function supplyAndCheck( |
| 23 | + wallet: WalletClient, |
| 24 | + request: SupplyRequest, |
| 25 | +): Promise<MarketUserReserveSupplyPosition[]> { |
| 26 | + const userAddress = evmAddress(wallet.account!.address); |
| 27 | + const result = await supply(client, request) |
| 28 | + .andThen(sendWith(wallet)) |
| 29 | + .andThen(() => |
| 30 | + userSupplies(client, { |
| 31 | + markets: [{ address: request.market, chainId: request.chainId }], |
| 32 | + user: userAddress, |
| 33 | + }), |
| 34 | + ); |
| 35 | + assertOk(result); |
| 36 | + expect(result.value).toEqual([ |
| 37 | + expect.objectContaining({ |
| 38 | + balance: expect.objectContaining({ |
| 39 | + amount: expect.objectContaining({ |
| 40 | + value: expect.toBeBigDecimalCloseTo( |
| 41 | + 'erc20' in request.amount |
| 42 | + ? request.amount.erc20.value |
| 43 | + : request.amount.native, |
| 44 | + ), |
| 45 | + }), |
| 46 | + }), |
| 47 | + // Check if the position can be used as collateral |
| 48 | + canBeCollateral: true, |
| 49 | + }), |
| 50 | + ]); |
| 51 | + return result.value!; |
| 52 | +} |
| 53 | + |
| 54 | +describe('Given an Aave Market', () => { |
| 55 | + let marketInfo: Market; |
| 56 | + let initialPosition: MarketUserReserveSupplyPosition | undefined; |
| 57 | + |
| 58 | + const wallet: WalletClient = createNewWallet(); |
| 59 | + |
| 60 | + beforeAll(async () => { |
| 61 | + // Set up market info first |
| 62 | + const result = await market(client, { |
| 63 | + address: DEFAULT_MARKET_ADDRESS, |
| 64 | + chainId: ETHEREUM_FORK_ID, |
| 65 | + }); |
| 66 | + assertOk(result); |
| 67 | + marketInfo = result.value!; |
| 68 | + |
| 69 | + // Set up wallet and supply position |
| 70 | + await fundErc20Address( |
| 71 | + WETH_ADDRESS, |
| 72 | + evmAddress(wallet.account!.address), |
| 73 | + bigDecimal('0.011'), |
| 74 | + ); |
| 75 | + const supplyResult = await supplyAndCheck(wallet, { |
| 76 | + market: marketInfo.address, |
| 77 | + chainId: marketInfo.chain.chainId, |
| 78 | + supplier: evmAddress(wallet.account!.address), |
| 79 | + amount: { |
| 80 | + erc20: { |
| 81 | + currency: WETH_ADDRESS, |
| 82 | + value: '0.01', |
| 83 | + }, |
| 84 | + }, |
| 85 | + }); |
| 86 | + initialPosition = supplyResult[0]; |
| 87 | + }); |
| 88 | + |
| 89 | + describe('And a user with a supply position', () => { |
| 90 | + describe('When user set the supply as collateral', async () => { |
| 91 | + it('Then it should be possible to borrow from the reserve', async () => { |
| 92 | + // Enable collateral |
| 93 | + const result = await collateralToggle(client, { |
| 94 | + market: initialPosition!.market.address, |
| 95 | + underlyingToken: initialPosition!.currency.address, |
| 96 | + chainId: initialPosition!.market.chain.chainId, |
| 97 | + user: evmAddress(wallet.account!.address), |
| 98 | + }) |
| 99 | + .andThen(sendWith(wallet)) |
| 100 | + .andTee((tx) => console.log(`tx to enable collateral: ${tx}`)) |
| 101 | + .andThen(() => { |
| 102 | + return userSupplies(client, { |
| 103 | + markets: [ |
| 104 | + { |
| 105 | + address: initialPosition!.market.address, |
| 106 | + chainId: initialPosition!.market.chain.chainId, |
| 107 | + }, |
| 108 | + ], |
| 109 | + user: evmAddress(wallet.account!.address), |
| 110 | + }); |
| 111 | + }); |
| 112 | + assertOk(result); |
| 113 | + expect(result.value).toEqual([ |
| 114 | + expect.objectContaining({ |
| 115 | + isCollateral: true, |
| 116 | + }), |
| 117 | + ]); |
| 118 | + |
| 119 | + // Borrow from the reserve |
| 120 | + const borrowReserve = marketInfo.borrowReserves.find( |
| 121 | + (reserve) => reserve.underlyingToken.symbol === 'USDC', |
| 122 | + )!; |
| 123 | + const borrowResult = await borrow(client, { |
| 124 | + market: marketInfo.address, |
| 125 | + chainId: marketInfo.chain.chainId, |
| 126 | + borrower: evmAddress(wallet.account!.address), |
| 127 | + amount: { |
| 128 | + erc20: { |
| 129 | + currency: borrowReserve.underlyingToken.address, |
| 130 | + value: '10', |
| 131 | + }, |
| 132 | + }, |
| 133 | + }) |
| 134 | + .andThen(sendWith(wallet)) |
| 135 | + .andTee((tx) => console.log(`tx to borrow: ${tx}`)) |
| 136 | + .andThen(() => |
| 137 | + userBorrows(client, { |
| 138 | + markets: [ |
| 139 | + { |
| 140 | + address: marketInfo.address, |
| 141 | + chainId: marketInfo.chain.chainId, |
| 142 | + }, |
| 143 | + ], |
| 144 | + user: evmAddress(wallet.account!.address), |
| 145 | + }), |
| 146 | + ); |
| 147 | + assertOk(borrowResult); |
| 148 | + }); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments