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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"test:client": "vitest --project client",
"test:client:local": "ENVIRONMENT=local vitest --project client",
"test:react": "vitest --project react",
"test:withdraw": "vitest --project client packages/client/src/actions/withdraw.test.ts",
"test:borrow": "vitest --project client packages/client/src/actions/borrow.test.ts",
"test": "vitest"
},
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ exports[`Given the Aave Protocol v3 > When fetching user market state > Then it
"__typename": "MarketUserState",
"availableBorrowsBase": Any<String>,
"currentLiquidationThreshold": Any<String>,
"eModeEnabled": Any<Boolean>,
"eModeEnabled": false,
"healthFactor": Any<String>,
"isInIsolationMode": Any<Boolean>,
"isInIsolationMode": false,
"ltv": Any<String>,
"netAPY": Any<String>,
"netWorth": Any<String>,
Expand Down
151 changes: 151 additions & 0 deletions packages/client/src/actions/borrow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import type {
Market,
MarketUserReserveSupplyPosition,
SupplyRequest,
} from '@aave/graphql';
import { assertOk, bigDecimal, evmAddress } from '@aave/types';
import type { WalletClient } from 'viem';
import { beforeAll, describe, expect, it } from 'vitest';
import {
client,
createNewWallet,
DEFAULT_MARKET_ADDRESS,
ETHEREUM_FORK_ID,
fundErc20Address,
WETH_ADDRESS,
} from '../test-utils';
import { sendWith } from '../viem';
import { market } from './markets';
import { borrow, collateralToggle, supply } from './transactions';
import { userBorrows, userSupplies } from './user';

async function supplyAndCheck(
wallet: WalletClient,
request: SupplyRequest,
): Promise<MarketUserReserveSupplyPosition[]> {
const userAddress = evmAddress(wallet.account!.address);
const result = await supply(client, request)
.andThen(sendWith(wallet))
.andThen(() =>
userSupplies(client, {
markets: [{ address: request.market, chainId: request.chainId }],
user: userAddress,
}),
);
assertOk(result);
expect(result.value).toEqual([
expect.objectContaining({
balance: expect.objectContaining({
amount: expect.objectContaining({
value: expect.toBeBigDecimalCloseTo(
'erc20' in request.amount
? request.amount.erc20.value
: request.amount.native,
),
}),
}),
// Check if the position can be used as collateral
canBeCollateral: true,
}),
]);
return result.value!;
}

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

const wallet: WalletClient = createNewWallet();

beforeAll(async () => {
// Set up market info first
const result = await market(client, {
address: DEFAULT_MARKET_ADDRESS,
chainId: ETHEREUM_FORK_ID,
});
assertOk(result);
marketInfo = result.value!;

// Set up wallet and supply position
await fundErc20Address(
WETH_ADDRESS,
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),
});
});
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 borrowResult = await borrow(client, {
market: marketInfo.address,
chainId: marketInfo.chain.chainId,
borrower: evmAddress(wallet.account!.address),
amount: {
erc20: {
currency: borrowReserve.underlyingToken.address,
value: '10',
},
},
})
.andThen(sendWith(wallet))
.andTee((tx) => console.log(`tx to borrow: ${tx}`))
.andThen(() =>
userBorrows(client, {
markets: [
{
address: marketInfo.address,
chainId: marketInfo.chain.chainId,
},
],
user: evmAddress(wallet.account!.address),
}),
);
assertOk(borrowResult);
});
});
});
});
13 changes: 0 additions & 13 deletions packages/client/src/actions/borrow.todo.ts

This file was deleted.

13 changes: 13 additions & 0 deletions packages/client/src/actions/eMode.todo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, expect, it } from 'vitest';

// THIS IS A VERY WIP

describe('Given an Aave Market', () => {
describe('And a user with a supply position', () => {
describe('When the user enables e-mode for that market', () => {
it('Then it should be possible to borrow from the reserve', () => {
expect(true).toBe(true);
});
});
});
});
2 changes: 0 additions & 2 deletions packages/client/src/actions/markets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ describe('Given the Aave Protocol v3', () => {
expect(result.value).toMatchSnapshot({
availableBorrowsBase: expect.any(String),
currentLiquidationThreshold: expect.any(String),
eModeEnabled: expect.any(Boolean),
healthFactor: expect.any(String),
isInIsolationMode: expect.any(Boolean),
ltv: expect.any(String),
netAPY: expect.any(String),
netWorth: expect.any(String),
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"gql:download:staging": "gql-tada generate schema 'https://api.v3.staging.aave.com/graphql' --output './schema.graphql'",
"gql:download:local": "gql-tada generate schema 'http://localhost:3011/graphql' --output './schema.graphql'",
"gql:generate": "gql-tada generate output",
"gql:turbo": "gql-tada turbo"
"gql:turbo": "gql-tada turbo",
"prebuild": "pnpm run gql:turbo"
},
"dependencies": {
"@aave/types": "workspace:*",
Expand Down