Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"test:client:local": "ENVIRONMENT=local vitest --project client",
"test:react": "vitest --project react",
"test:borrow": "vitest --project client packages/client/src/actions/borrow.test.ts",
"test:vaultDeploy": "vitest --project client packages/client/src/actions/vaultDeploy.test.ts",
"test:856": "vitest --project client packages/client/src/actions/eMode.test.ts -t 'Then they should be able to disable it at any time'",
"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\"",
"test:withdraw": "vitest --project client packages/client/src/actions/withdraw.test.ts",
Expand Down
94 changes: 50 additions & 44 deletions packages/client/src/actions/borrow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
createNewWallet,
DEFAULT_MARKET_ADDRESS,
ETHEREUM_FORK_ID,
fetchReserve,
fundErc20Address,
WETH_ADDRESS,
wait,
} from '../test-utils';
import { sendWith } from '../viem';
import { market } from './markets';
Expand Down Expand Up @@ -45,6 +47,7 @@ async function supplyAndCheck(
}),
}),
// Check if the position can be used as collateral
isCollateral: true,
canBeCollateral: true,
}),
]);
Expand All @@ -53,8 +56,6 @@ async function supplyAndCheck(

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

const wallet: WalletClient = createNewWallet();

beforeAll(async () => {
Expand All @@ -72,67 +73,71 @@ describe('Given an Aave Market', () => {
evmAddress(wallet.account!.address),
bigDecimal('0.011'),
);
const supplyResult = await supplyAndCheck(wallet, {
market: marketInfo.address,
chainId: marketInfo.chain.chainId,
supplier: evmAddress(wallet.account!.address),
amount: {
erc20: {
currency: WETH_ADDRESS,
value: '0.01',
},
},
});
initialPosition = supplyResult[0];
});

describe('And a user with a supply position', () => {
describe('When user set the supply as collateral', async () => {
it('Then it should be possible to borrow from the reserve', async () => {
// Enable collateral
const result = await collateralToggle(client, {
market: initialPosition!.market.address,
underlyingToken: initialPosition!.currency.address,
chainId: initialPosition!.market.chain.chainId,
user: evmAddress(wallet.account!.address),
})
.andThen(sendWith(wallet))
.andTee((tx) => console.log(`tx to enable collateral: ${tx}`))
.andThen(() => {
return userSupplies(client, {
markets: [
{
address: initialPosition!.market.address,
chainId: initialPosition!.market.chain.chainId,
},
],
user: evmAddress(wallet.account!.address),
const supplyResult = await supplyAndCheck(wallet, {
market: marketInfo.address,
chainId: marketInfo.chain.chainId,
supplier: evmAddress(wallet.account!.address),
amount: {
erc20: {
currency: WETH_ADDRESS,
value: '0.01',
},
},
});

if (!supplyResult[0]!.isCollateral) {
// Enable collateral
const result = await collateralToggle(client, {
market: marketInfo.address,
underlyingToken: WETH_ADDRESS,
chainId: marketInfo.chain.chainId,
user: evmAddress(wallet.account!.address),
})
.andThen(sendWith(wallet))
.andTee((tx) => console.log(`tx to enable collateral: ${tx}`))
.andThen(() => {
return userSupplies(client, {
markets: [
{
address: marketInfo.address,
chainId: marketInfo.chain.chainId,
},
],
user: evmAddress(wallet.account!.address),
});
});
});
assertOk(result);
expect(result.value).toEqual([
expect.objectContaining({
isCollateral: true,
}),
]);
assertOk(result);
expect(result.value).toEqual([
expect.objectContaining({
isCollateral: true,
}),
]);
}

// Borrow from the reserve
const borrowReserve = marketInfo.borrowReserves.find(
(reserve) => reserve.underlyingToken.symbol === 'USDC',
)!;
const borrowReserve = await fetchReserve(
WETH_ADDRESS,
evmAddress(wallet.account!.address),
);
const borrowResult = await borrow(client, {
market: marketInfo.address,
chainId: marketInfo.chain.chainId,
borrower: evmAddress(wallet.account!.address),
amount: {
erc20: {
currency: borrowReserve.underlyingToken.address,
value: '10',
value: borrowReserve.userState!.borrowable.amount.value,
},
},
})
.andThen(sendWith(wallet))
.andTee((tx) => console.log(`tx to borrow: ${tx}`))
.andTee(() => wait(5000))
.andThen(() =>
userBorrows(client, {
markets: [
Expand All @@ -145,7 +150,8 @@ describe('Given an Aave Market', () => {
}),
);
assertOk(borrowResult);
});
expect(borrowResult.value.length).toBe(1);
}, 25_000);
});
});
});
56 changes: 56 additions & 0 deletions packages/client/src/actions/vaultDeploy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { assertOk, bigDecimal, evmAddress } from '@aave/types';
import { beforeAll, describe, expect, it } from 'vitest';
import {
client,
createNewWallet,
fetchReserve,
fundErc20Address,
WETH_ADDRESS,
wait,
} from '../test-utils';
import { sendWith } from '../viem';
import { vaultDeploy } from './transactions';
import { vaults } from './vaults';

describe('Given the Aave Protocol v3', () => {
describe('When user deploys a vault for wETH on default market', () => {
const wallet = createNewWallet();

beforeAll(async () => {
await fundErc20Address(
WETH_ADDRESS,
evmAddress(wallet.account!.address),
bigDecimal('1'),
);
});

it('Then vault is created', async () => {
const reserve = await fetchReserve(WETH_ADDRESS);
const result = await vaultDeploy(client, {
chainId: reserve.market.chain.chainId,
market: reserve.market.address,
deployer: evmAddress(wallet.account!.address),
owner: evmAddress(wallet.account!.address),
initialFee: bigDecimal('3'),
initialLockDeposit: bigDecimal('1'),
shareName: 'Aave WETH Vault Shares',
shareSymbol: 'avWETH',
underlyingToken: reserve.underlyingToken.address,
})
.andTee((result) =>
console.log(`result: ${JSON.stringify(result, null, 2)}`),
)
.andThen(sendWith(wallet))
.andTee((tx) => console.log(`transaction: ${tx}`));
assertOk(result);
await wait(5000);
const vaultInfo = await vaults(client, {
criteria: {
ownedBy: [evmAddress(wallet.account!.address)],
},
});
assertOk(vaultInfo);
expect(vaultInfo.value.items.length).toBe(1);
}, 25_000);
});
});
10 changes: 8 additions & 2 deletions packages/client/src/actions/withdraw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
fundErc20Address,
fundNativeAddress,
WETH_ADDRESS,
wait,
} from '../test-utils';
import { sendWith } from '../viem';
import { supply, withdraw } from './transactions';
Expand Down Expand Up @@ -90,6 +91,9 @@ describe('Given an Aave Market', () => {
chainId: reserveInfo.market.chain.chainId,
})
.andThen(sendWith(wallet))
.andTee((tx) => console.log(`tx to withdraw: ${tx}`))
// Wait for the transaction to be mined
.andTee(() => wait(5000))
.andThen(() =>
userSupplies(client, {
markets: [
Expand All @@ -111,7 +115,7 @@ describe('Given an Aave Market', () => {
}),
}),
]);
});
}, 25_000);
});
});

Expand Down Expand Up @@ -153,6 +157,8 @@ describe('Given an Aave Market', () => {
chainId: reserveInfo.market.chain.chainId,
})
.andThen(sendWith(wallet))
.andTee((tx) => console.log(`tx to withdraw: ${tx}`))
.andTee(() => wait(5000))
.andThen(() =>
userSupplies(client, {
markets: [
Expand All @@ -178,7 +184,7 @@ describe('Given an Aave Market', () => {
address: evmAddress(wallet.account!.address),
});
expect(balanceAfter).toBeGreaterThan(balanceBefore);
});
}, 25_000);
});
});
});
13 changes: 12 additions & 1 deletion packages/client/src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export const WETH_ADDRESS = evmAddress(
export const USDC_ADDRESS = evmAddress(
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
);
export const DAI_ADDRESS = evmAddress(
'0x6B175474E89094C44Da98b954EedeAC495271d0F',
);
export const DEFAULT_MARKET_ADDRESS = evmAddress(
'0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2',
);
Expand Down Expand Up @@ -163,11 +166,15 @@ export function fundErc20Address(
);
}

export async function fetchReserve(tokenAddress: EvmAddress): Promise<Reserve> {
export async function fetchReserve(
tokenAddress: EvmAddress,
user?: EvmAddress,
): Promise<Reserve> {
const result = await reserve(client, {
chainId: ETHEREUM_FORK_ID,
market: DEFAULT_MARKET_ADDRESS,
underlyingToken: tokenAddress,
user: user,
}).map(nonNullable);
assertOk(result);
return result.value;
Expand Down Expand Up @@ -206,3 +213,7 @@ export function assertTypedDocumentSatisfies<
) {
expect(validate(schema, document, rules)).toEqual([]);
}

export function wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}