Skip to content

Commit 9ee8f46

Browse files
authored
feat: test for vaultDeploy and refactor other tests (#13)
1 parent a6b19cd commit 9ee8f46

File tree

5 files changed

+127
-47
lines changed

5 files changed

+127
-47
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"test:client:local": "ENVIRONMENT=local vitest --project client",
2424
"test:react": "vitest --project react",
2525
"test:borrow": "vitest --project client packages/client/src/actions/borrow.test.ts",
26+
"test:vaultDeploy": "vitest --project client packages/client/src/actions/vaultDeploy.test.ts",
2627
"test:856": "vitest --project client packages/client/src/actions/eMode.test.ts -t 'Then they should be able to disable it at any time'",
2728
"test:857": "vitest --project client packages/client/src/actions/eMode.test.ts -t \"Then the market's reserves should have user state that reflects the selected E-Mode category settings\"",
2829
"test:withdraw": "vitest --project client packages/client/src/actions/withdraw.test.ts",

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

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import {
1111
createNewWallet,
1212
DEFAULT_MARKET_ADDRESS,
1313
ETHEREUM_FORK_ID,
14+
fetchReserve,
1415
fundErc20Address,
1516
WETH_ADDRESS,
17+
wait,
1618
} from '../test-utils';
1719
import { sendWith } from '../viem';
1820
import { market } from './markets';
@@ -45,6 +47,7 @@ async function supplyAndCheck(
4547
}),
4648
}),
4749
// Check if the position can be used as collateral
50+
isCollateral: true,
4851
canBeCollateral: true,
4952
}),
5053
]);
@@ -53,8 +56,6 @@ async function supplyAndCheck(
5356

5457
describe('Given an Aave Market', () => {
5558
let marketInfo: Market;
56-
let initialPosition: MarketUserReserveSupplyPosition | undefined;
57-
5859
const wallet: WalletClient = createNewWallet();
5960

6061
beforeAll(async () => {
@@ -72,67 +73,71 @@ describe('Given an Aave Market', () => {
7273
evmAddress(wallet.account!.address),
7374
bigDecimal('0.011'),
7475
);
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];
8776
});
8877

8978
describe('And a user with a supply position', () => {
9079
describe('When user set the supply as collateral', async () => {
9180
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),
81+
const supplyResult = await supplyAndCheck(wallet, {
82+
market: marketInfo.address,
83+
chainId: marketInfo.chain.chainId,
84+
supplier: evmAddress(wallet.account!.address),
85+
amount: {
86+
erc20: {
87+
currency: WETH_ADDRESS,
88+
value: '0.01',
89+
},
90+
},
91+
});
92+
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+
});
110113
});
111-
});
112-
assertOk(result);
113-
expect(result.value).toEqual([
114-
expect.objectContaining({
115-
isCollateral: true,
116-
}),
117-
]);
114+
assertOk(result);
115+
expect(result.value).toEqual([
116+
expect.objectContaining({
117+
isCollateral: true,
118+
}),
119+
]);
120+
}
118121

119122
// Borrow from the reserve
120-
const borrowReserve = marketInfo.borrowReserves.find(
121-
(reserve) => reserve.underlyingToken.symbol === 'USDC',
122-
)!;
123+
const borrowReserve = await fetchReserve(
124+
WETH_ADDRESS,
125+
evmAddress(wallet.account!.address),
126+
);
123127
const borrowResult = await borrow(client, {
124128
market: marketInfo.address,
125129
chainId: marketInfo.chain.chainId,
126130
borrower: evmAddress(wallet.account!.address),
127131
amount: {
128132
erc20: {
129133
currency: borrowReserve.underlyingToken.address,
130-
value: '10',
134+
value: borrowReserve.userState!.borrowable.amount.value,
131135
},
132136
},
133137
})
134138
.andThen(sendWith(wallet))
135139
.andTee((tx) => console.log(`tx to borrow: ${tx}`))
140+
.andTee(() => wait(5000))
136141
.andThen(() =>
137142
userBorrows(client, {
138143
markets: [
@@ -145,7 +150,8 @@ describe('Given an Aave Market', () => {
145150
}),
146151
);
147152
assertOk(borrowResult);
148-
});
153+
expect(borrowResult.value.length).toBe(1);
154+
}, 25_000);
149155
});
150156
});
151157
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { assertOk, bigDecimal, evmAddress } from '@aave/types';
2+
import { beforeAll, describe, expect, it } from 'vitest';
3+
import {
4+
client,
5+
createNewWallet,
6+
fetchReserve,
7+
fundErc20Address,
8+
WETH_ADDRESS,
9+
wait,
10+
} from '../test-utils';
11+
import { sendWith } from '../viem';
12+
import { vaultDeploy } from './transactions';
13+
import { vaults } from './vaults';
14+
15+
describe('Given the Aave Protocol v3', () => {
16+
describe('When user deploys a vault for wETH on default market', () => {
17+
const wallet = createNewWallet();
18+
19+
beforeAll(async () => {
20+
await fundErc20Address(
21+
WETH_ADDRESS,
22+
evmAddress(wallet.account!.address),
23+
bigDecimal('1'),
24+
);
25+
});
26+
27+
it('Then vault is created', async () => {
28+
const reserve = await fetchReserve(WETH_ADDRESS);
29+
const result = await vaultDeploy(client, {
30+
chainId: reserve.market.chain.chainId,
31+
market: reserve.market.address,
32+
deployer: evmAddress(wallet.account!.address),
33+
owner: evmAddress(wallet.account!.address),
34+
initialFee: bigDecimal('3'),
35+
initialLockDeposit: bigDecimal('1'),
36+
shareName: 'Aave WETH Vault Shares',
37+
shareSymbol: 'avWETH',
38+
underlyingToken: reserve.underlyingToken.address,
39+
})
40+
.andTee((result) =>
41+
console.log(`result: ${JSON.stringify(result, null, 2)}`),
42+
)
43+
.andThen(sendWith(wallet))
44+
.andTee((tx) => console.log(`transaction: ${tx}`));
45+
assertOk(result);
46+
await wait(5000);
47+
const vaultInfo = await vaults(client, {
48+
criteria: {
49+
ownedBy: [evmAddress(wallet.account!.address)],
50+
},
51+
});
52+
assertOk(vaultInfo);
53+
expect(vaultInfo.value.items.length).toBe(1);
54+
}, 25_000);
55+
});
56+
});

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
fundErc20Address,
1111
fundNativeAddress,
1212
WETH_ADDRESS,
13+
wait,
1314
} from '../test-utils';
1415
import { sendWith } from '../viem';
1516
import { supply, withdraw } from './transactions';
@@ -90,6 +91,9 @@ describe('Given an Aave Market', () => {
9091
chainId: reserveInfo.market.chain.chainId,
9192
})
9293
.andThen(sendWith(wallet))
94+
.andTee((tx) => console.log(`tx to withdraw: ${tx}`))
95+
// Wait for the transaction to be mined
96+
.andTee(() => wait(5000))
9397
.andThen(() =>
9498
userSupplies(client, {
9599
markets: [
@@ -111,7 +115,7 @@ describe('Given an Aave Market', () => {
111115
}),
112116
}),
113117
]);
114-
});
118+
}, 25_000);
115119
});
116120
});
117121

@@ -153,6 +157,8 @@ describe('Given an Aave Market', () => {
153157
chainId: reserveInfo.market.chain.chainId,
154158
})
155159
.andThen(sendWith(wallet))
160+
.andTee((tx) => console.log(`tx to withdraw: ${tx}`))
161+
.andTee(() => wait(5000))
156162
.andThen(() =>
157163
userSupplies(client, {
158164
markets: [
@@ -178,7 +184,7 @@ describe('Given an Aave Market', () => {
178184
address: evmAddress(wallet.account!.address),
179185
});
180186
expect(balanceAfter).toBeGreaterThan(balanceBefore);
181-
});
187+
}, 25_000);
182188
});
183189
});
184190
});

packages/client/src/test-utils.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ export const WETH_ADDRESS = evmAddress(
4343
export const USDC_ADDRESS = evmAddress(
4444
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
4545
);
46+
export const DAI_ADDRESS = evmAddress(
47+
'0x6B175474E89094C44Da98b954EedeAC495271d0F',
48+
);
4649
export const DEFAULT_MARKET_ADDRESS = evmAddress(
4750
'0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2',
4851
);
@@ -163,11 +166,15 @@ export function fundErc20Address(
163166
);
164167
}
165168

166-
export async function fetchReserve(tokenAddress: EvmAddress): Promise<Reserve> {
169+
export async function fetchReserve(
170+
tokenAddress: EvmAddress,
171+
user?: EvmAddress,
172+
): Promise<Reserve> {
167173
const result = await reserve(client, {
168174
chainId: ETHEREUM_FORK_ID,
169175
market: DEFAULT_MARKET_ADDRESS,
170176
underlyingToken: tokenAddress,
177+
user: user,
171178
}).map(nonNullable);
172179
assertOk(result);
173180
return result.value;
@@ -206,3 +213,7 @@ export function assertTypedDocumentSatisfies<
206213
) {
207214
expect(validate(schema, document, rules)).toEqual([]);
208215
}
216+
217+
export function wait(ms: number) {
218+
return new Promise((resolve) => setTimeout(resolve, ms));
219+
}

0 commit comments

Comments
 (0)