Skip to content

Commit 23885c8

Browse files
juangmcesarenaldi
andauthored
test: repay scenarios and missing borrow with native token (#19)
Co-authored-by: Cesare Naldi <[email protected]>
1 parent f190e71 commit 23885c8

File tree

5 files changed

+335
-71
lines changed

5 files changed

+335
-71
lines changed

packages/client/src/actions/borrow.test.ts

Lines changed: 87 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import {
1313
ETHEREUM_MARKET_ADDRESS,
1414
fetchReserve,
1515
fundErc20Address,
16+
fundNativeAddress,
1617
WETH_ADDRESS,
1718
wait,
1819
} from '../test-utils';
1920
import { sendWith } from '../viem';
2021
import { market } from './markets';
21-
import { borrow, collateralToggle, supply } from './transactions';
22+
import { borrow, supply } from './transactions';
2223
import { userBorrows, userSupplies } from './user';
2324

2425
async function supplyAndCheck(
@@ -55,30 +56,31 @@ async function supplyAndCheck(
5556
}
5657

5758
describe('Given an Aave Market', () => {
58-
let marketInfo: Market;
59-
const wallet: WalletClient = createNewWallet();
59+
describe('And a user with a supply position', () => {
60+
describe('When the user set the supply as collateral', async () => {
61+
let marketInfo: Market;
62+
const wallet: WalletClient = createNewWallet();
6063

61-
beforeAll(async () => {
62-
// Set up market info first
63-
const result = await market(client, {
64-
address: ETHEREUM_MARKET_ADDRESS,
65-
chainId: ETHEREUM_FORK_ID,
66-
});
67-
assertOk(result);
68-
marketInfo = result.value!;
64+
beforeAll(async () => {
65+
// Set up market info first
66+
const result = await market(client, {
67+
address: ETHEREUM_MARKET_ADDRESS,
68+
chainId: ETHEREUM_FORK_ID,
69+
});
70+
assertOk(result);
71+
marketInfo = result.value!;
6972

70-
// Set up wallet and supply position
71-
await fundErc20Address(
72-
WETH_ADDRESS,
73-
evmAddress(wallet.account!.address),
74-
bigDecimal('0.011'),
75-
);
76-
});
73+
// Set up wallet and supply position
74+
await fundErc20Address(
75+
WETH_ADDRESS,
76+
evmAddress(wallet.account!.address),
77+
bigDecimal('0.011'),
78+
);
79+
});
7780

78-
describe('And a user with a supply position', () => {
79-
describe('When user set the supply as collateral', async () => {
80-
it('Then it should be possible to borrow from the reserve', async () => {
81-
const supplyResult = await supplyAndCheck(wallet, {
81+
it('Then it should be possible to borrow ERC20 from the reserve', async () => {
82+
// NOTE: first time supply is set as collateral automatically
83+
await supplyAndCheck(wallet, {
8284
market: marketInfo.address,
8385
chainId: marketInfo.chain.chainId,
8486
supplier: evmAddress(wallet.account!.address),
@@ -90,35 +92,6 @@ describe('Given an Aave Market', () => {
9092
},
9193
});
9294

93-
if (!supplyResult[0]!.isCollateral) {
94-
// Enable collateral
95-
const result = await collateralToggle(client, {
96-
market: marketInfo.address,
97-
underlyingToken: WETH_ADDRESS,
98-
chainId: marketInfo.chain.chainId,
99-
user: evmAddress(wallet.account!.address),
100-
})
101-
.andThen(sendWith(wallet))
102-
.andTee((tx) => console.log(`tx to enable collateral: ${tx}`))
103-
.andThen(() => {
104-
return userSupplies(client, {
105-
markets: [
106-
{
107-
address: marketInfo.address,
108-
chainId: marketInfo.chain.chainId,
109-
},
110-
],
111-
user: evmAddress(wallet.account!.address),
112-
});
113-
});
114-
assertOk(result);
115-
expect(result.value).toEqual([
116-
expect.objectContaining({
117-
isCollateral: true,
118-
}),
119-
]);
120-
}
121-
12295
// Borrow from the reserve
12396
const borrowReserve = await fetchReserve(
12497
WETH_ADDRESS,
@@ -153,5 +126,68 @@ describe('Given an Aave Market', () => {
153126
expect(borrowResult.value.length).toBe(1);
154127
}, 25_000);
155128
});
129+
130+
describe('When the user set the supply as collateral', async () => {
131+
let marketInfo: Market;
132+
const wallet: WalletClient = createNewWallet();
133+
134+
beforeAll(async () => {
135+
// Set up market info first
136+
const result = await market(client, {
137+
address: ETHEREUM_MARKET_ADDRESS,
138+
chainId: ETHEREUM_FORK_ID,
139+
});
140+
assertOk(result);
141+
marketInfo = result.value!;
142+
143+
// Set up wallet and supply position
144+
await fundNativeAddress(
145+
evmAddress(wallet.account!.address),
146+
bigDecimal('0.2'),
147+
);
148+
});
149+
150+
it('Then it should be possible to borrow native from the reserve', async () => {
151+
// NOTE: first time supply is set as collateral automatically
152+
await supplyAndCheck(wallet, {
153+
market: marketInfo.address,
154+
chainId: marketInfo.chain.chainId,
155+
supplier: evmAddress(wallet.account!.address),
156+
amount: {
157+
native: '0.1',
158+
},
159+
});
160+
161+
// Borrow from the reserve
162+
const borrowReserve = await fetchReserve(
163+
WETH_ADDRESS,
164+
evmAddress(wallet.account!.address),
165+
);
166+
const borrowResult = await borrow(client, {
167+
market: marketInfo.address,
168+
chainId: marketInfo.chain.chainId,
169+
borrower: evmAddress(wallet.account!.address),
170+
amount: {
171+
native: borrowReserve.userState!.borrowable.amount.value,
172+
},
173+
})
174+
.andThen(sendWith(wallet))
175+
.andTee((tx) => console.log(`tx to borrow: ${tx}`))
176+
.andTee(() => wait(5000))
177+
.andThen(() =>
178+
userBorrows(client, {
179+
markets: [
180+
{
181+
address: marketInfo.address,
182+
chainId: marketInfo.chain.chainId,
183+
},
184+
],
185+
user: evmAddress(wallet.account!.address),
186+
}),
187+
);
188+
assertOk(borrowResult);
189+
expect(borrowResult.value.length).toBe(1);
190+
}, 25_000);
191+
});
156192
});
157193
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)