Skip to content

Commit 1fe2b6f

Browse files
committed
feat: test for vaultDeploy and refactor other tests
1 parent afd196d commit 1fe2b6f

File tree

5 files changed

+78
-39
lines changed

5 files changed

+78
-39
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"test:react": "vitest --project react",
2525
"test:withdraw": "vitest --project client packages/client/src/actions/withdraw.test.ts",
2626
"test:borrow": "vitest --project client packages/client/src/actions/borrow.test.ts",
27+
"test:vaultDeploy": "vitest --project client packages/client/src/actions/vaultDeploy.test.ts",
2728
"test": "vitest"
2829
},
2930
"license": "MIT",

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

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import {
1111
createNewWallet,
1212
DEFAULT_MARKET_ADDRESS,
1313
ETHEREUM_FORK_ID,
14+
fetchReserve,
1415
fundErc20Address,
16+
wait,
1517
WETH_ADDRESS,
1618
} from '../test-utils';
1719
import { sendWith } from '../viem';
1820
import { market } from './markets';
19-
import { borrow, collateralToggle, supply } from './transactions';
21+
import { borrow, supply } from './transactions';
2022
import { userBorrows, userSupplies } from './user';
2123

2224
async function supplyAndCheck(
@@ -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,7 +56,6 @@ async function supplyAndCheck(
5356

5457
describe('Given an Aave Market', () => {
5558
let marketInfo: Market;
56-
let initialPosition: MarketUserReserveSupplyPosition | undefined;
5759

5860
const wallet: WalletClient = createNewWallet();
5961

@@ -72,7 +74,8 @@ describe('Given an Aave Market', () => {
7274
evmAddress(wallet.account!.address),
7375
bigDecimal('0.011'),
7476
);
75-
const supplyResult = await supplyAndCheck(wallet, {
77+
78+
await supplyAndCheck(wallet, {
7679
market: marketInfo.address,
7780
chainId: marketInfo.chain.chainId,
7881
supplier: evmAddress(wallet.account!.address),
@@ -83,56 +86,27 @@ describe('Given an Aave Market', () => {
8386
},
8487
},
8588
});
86-
initialPosition = supplyResult[0];
8789
});
8890

8991
describe('And a user with a supply position', () => {
9092
describe('When user set the supply as collateral', async () => {
9193
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-
11994
// Borrow from the reserve
120-
const borrowReserve = marketInfo.borrowReserves.find(
121-
(reserve) => reserve.underlyingToken.symbol === 'USDC',
122-
)!;
95+
const borrowReserve = await fetchReserve(WETH_ADDRESS, evmAddress(wallet.account!.address));
12396
const borrowResult = await borrow(client, {
12497
market: marketInfo.address,
12598
chainId: marketInfo.chain.chainId,
12699
borrower: evmAddress(wallet.account!.address),
127100
amount: {
128101
erc20: {
129102
currency: borrowReserve.underlyingToken.address,
130-
value: '10',
103+
value: borrowReserve.userState!.borrowable.amount.value,
131104
},
132105
},
133106
})
134107
.andThen(sendWith(wallet))
135108
.andTee((tx) => console.log(`tx to borrow: ${tx}`))
109+
.andTee(() => wait(5000))
136110
.andThen(() =>
137111
userBorrows(client, {
138112
markets: [
@@ -145,7 +119,7 @@ describe('Given an Aave Market', () => {
145119
}),
146120
);
147121
assertOk(borrowResult);
148-
});
122+
}, 25_000);
149123
});
150124
});
151125
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { assertOk, bigDecimal, evmAddress } from '@aave/types';
2+
import { beforeAll, describe, expect, it } from 'vitest';
3+
import {
4+
client,
5+
DEFAULT_MARKET_ADDRESS,
6+
ETHEREUM_FORK_ID,
7+
fetchReserve,
8+
WETH_ADDRESS,
9+
createNewWallet,
10+
wait,
11+
fundErc20Address,
12+
} from '../test-utils';
13+
import { sendWith } from '../viem';
14+
import { vaultDeploy } from './transactions';
15+
import { vaults } from './vaults';
16+
17+
describe('Given the Aave Protocol v3', () => {
18+
describe('When user deploys a vault for wETH on default market', () => {
19+
const wallet = createNewWallet();
20+
21+
beforeAll(async () => {
22+
await fundErc20Address(WETH_ADDRESS, evmAddress(wallet.account!.address), bigDecimal('1'));
23+
});
24+
25+
it('Then vault is created', async () => {
26+
const reserve = await fetchReserve(WETH_ADDRESS);
27+
const result = await vaultDeploy(client, {
28+
chainId: reserve.market.chain.chainId,
29+
market: reserve.market.address,
30+
deployer: evmAddress(wallet.account!.address),
31+
owner: evmAddress(wallet.account!.address),
32+
initialFee: bigDecimal('3'),
33+
initialLockDeposit: bigDecimal('1'),
34+
shareName: 'Aave WETH Vault Shares',
35+
shareSymbol: 'avWETH',
36+
underlyingToken: reserve.underlyingToken.address,
37+
})
38+
.andTee((result) => console.log(`result: ${JSON.stringify(result, null, 2)}`))
39+
.andThen(sendWith(wallet))
40+
.andTee((tx) => console.log(`transaction: ${tx}`));
41+
assertOk(result);
42+
await wait(5000);
43+
const vaultInfo = await vaults(client, {
44+
criteria: {
45+
ownedBy: [evmAddress(wallet.account!.address)],
46+
},
47+
});
48+
assertOk(vaultInfo);
49+
expect(vaultInfo.value.items.length).toBe(1);
50+
});
51+
});
52+
});

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
fetchReserve,
1010
fundErc20Address,
1111
fundNativeAddress,
12+
wait,
1213
WETH_ADDRESS,
1314
} from '../test-utils';
1415
import { sendWith } from '../viem';
@@ -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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export const WETH_ADDRESS = evmAddress(
4343
export const USDC_ADDRESS = evmAddress(
4444
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
4545
);
46+
export const DAI_ADDRESS = evmAddress('0x6B175474E89094C44Da98b954EedeAC495271d0F');
4647
export const DEFAULT_MARKET_ADDRESS = evmAddress(
4748
'0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2',
4849
);
@@ -162,11 +163,12 @@ export function fundErc20Address(
162163
);
163164
}
164165

165-
export async function fetchReserve(tokenAddress: EvmAddress): Promise<Reserve> {
166+
export async function fetchReserve(tokenAddress: EvmAddress, user?: EvmAddress): Promise<Reserve> {
166167
const result = await reserve(client, {
167168
chainId: ETHEREUM_FORK_ID,
168169
market: DEFAULT_MARKET_ADDRESS,
169170
underlyingToken: tokenAddress,
171+
user: user,
170172
}).map(nonNullable);
171173
assertOk(result);
172174
return result.value;
@@ -205,3 +207,7 @@ export function assertTypedDocumentSatisfies<
205207
) {
206208
expect(validate(schema, document, rules)).toEqual([]);
207209
}
210+
211+
export function wait(ms: number) {
212+
return new Promise((resolve) => setTimeout(resolve, ms));
213+
}

0 commit comments

Comments
 (0)