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
5 changes: 5 additions & 0 deletions .changeset/twenty-sites-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@venusprotocol/evm": minor
---

add base Boost tab
18 changes: 18 additions & 0 deletions apps/evm/src/clients/api/__mocks__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,16 @@ export const useGetAccountTransactionHistory = vi.fn(() =>
}),
);

export const getSwapQuote = vi.fn(async () => ({
swapQuote: undefined,
}));
export const useGetSwapQuote = vi.fn(() =>
useQuery({
queryKey: [FunctionKey.GET_SWAP_QUOTE],
queryFn: getSwapQuote,
}),
);

// Mutations
export const useApproveToken = vi.fn((_variables: never, options?: MutationObserverOptions) =>
useMutation({
Expand Down Expand Up @@ -677,6 +687,14 @@ export const useBorrow = vi.fn((_variables: never, options?: MutationObserverOpt
}),
);

export const useOpenLeveragedPosition = vi.fn(
(_variables: never, options?: MutationObserverOptions) =>
useMutation({
mutationFn: vi.fn(),
...options,
}),
);

export const withdrawXvs = vi.fn();
export const useWithdrawXvs = (options?: MutationObserverOptions) =>
useMutation({
Expand Down
4 changes: 4 additions & 0 deletions apps/evm/src/clients/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export * from './mutations/useSwapTokens';
export * from './mutations/useWithdraw';
export * from './mutations/useImportSupplyPosition';
export * from './mutations/useSetEModeGroup';
export * from './mutations/useOpenLeveragedPosition';

// Queries
export * from './queries/getVaiTreasuryPercentage';
Expand Down Expand Up @@ -226,3 +227,6 @@ export * from './queries/getAccountTransactionHistory/useGetAccountTransactionHi

export * from './queries/getSimulatedPool';
export * from './queries/getSimulatedPool/useGetSimulatedPool';

export * from './queries/getSwapQuote';
export * from './queries/getSwapQuote/useGetSwapQuote';
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`useOpenLeveragedPosition > calls useSendTransaction with correct parameters 'with single asset' 1`] = `
{
"abi": Any<Array>,
"address": "0xfakeLeverageManagerContractAddress",
"args": [
"0xD5C4C2e2facBEB59D0216D0595d63FcDc6F9A1a7",
0n,
100000000n,
],
"functionName": "enterSingleAssetLeverage",
}
`;

exports[`useOpenLeveragedPosition > calls useSendTransaction with correct parameters 'with single asset' 2`] = `
[
[
{
"queryKey": [
"GET_POOLS",
],
},
],
]
`;

exports[`useOpenLeveragedPosition > calls useSendTransaction with correct parameters 'with swapQuote' 1`] = `
{
"abi": Any<Array>,
"address": "0xfakeLeverageManagerContractAddress",
"args": [
"0x170d3b2da05cc2124334240fB34ad1359e34C562",
0n,
"0xD5C4C2e2facBEB59D0216D0595d63FcDc6F9A1a7",
100000000n,
100000000n,
"0x",
],
"functionName": "enterLeverage",
}
`;

exports[`useOpenLeveragedPosition > calls useSendTransaction with correct parameters 'with swapQuote' 2`] = `
[
[
{
"queryKey": [
"GET_POOLS",
],
},
],
]
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { lisUsd, usdc } from '__mocks__/models/tokens';
import { vLisUSD, vUsdc } from '__mocks__/models/vTokens';
import { queryClient } from 'clients/api';
import { useGetContractAddress } from 'hooks/useGetContractAddress';
import { useSendTransaction } from 'hooks/useSendTransaction';
import { renderHook } from 'testUtils/render';
import type { ExactInSwapQuote } from 'types';
import type { Mock } from 'vitest';
import { useOpenLeveragedPosition } from '..';

const fakeSwapQuote: ExactInSwapQuote = {
fromToken: usdc,
toToken: lisUsd,
direction: 'exact-in',
priceImpactPercentage: 0.1,
fromTokenAmountSoldMantissa: 100000000n,
expectedToTokenAmountReceivedMantissa: 100000000n,
minimumToTokenAmountReceivedMantissa: 100000000n,
callData: '0x',
};

vi.mock('libs/contracts');

describe('useOpenLeveragedPosition', () => {
it.each([
{
label: 'with swapQuote',
input: {
borrowedVToken: vUsdc,
suppliedVToken: vLisUSD,
swapQuote: fakeSwapQuote,
},
},
{
label: 'with single asset',
input: {
vToken: vUsdc,
amountMantissa: 100000000n,
},
},
])('calls useSendTransaction with correct parameters $label', async ({ input }) => {
renderHook(() => useOpenLeveragedPosition());

expect(useSendTransaction).toHaveBeenCalledWith({
fn: expect.any(Function),
onConfirmed: expect.any(Function),
options: undefined,
});

const { fn } = (useSendTransaction as jest.Mock).mock.calls[0][0];

expect(await fn(input)).toMatchSnapshot({
abi: expect.any(Array),
});

const { onConfirmed } = (useSendTransaction as jest.Mock).mock.calls[0][0];
await onConfirmed();

expect((queryClient.invalidateQueries as Mock).mock.calls).toMatchSnapshot();
});

it('throws error when LeverageManager contract address is not found', async () => {
(useGetContractAddress as Mock).mockImplementation(() => ({ address: undefined }));

renderHook(() => useOpenLeveragedPosition());

const { fn } = (useSendTransaction as Mock).mock.calls[0][0];

await expect(async () =>
fn({
vToken: vUsdc,
amountMantissa: 100000000n,
}),
).rejects.toThrow('somethingWentWrong');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type { Account, Address, Chain, Hex, WriteContractParameters } from 'viem';

import { queryClient } from 'clients/api';
import FunctionKey from 'constants/functionKey';
import { useGetContractAddress } from 'hooks/useGetContractAddress';
import { type UseSendTransactionOptions, useSendTransaction } from 'hooks/useSendTransaction';
import { leverageManagerAbi } from 'libs/contracts';
import { VError } from 'libs/errors';
import type { ExactInSwapQuote, VToken } from 'types';

type OpenLeveragedPositionWithSwapInput = {
swapQuote: ExactInSwapQuote;
borrowedVToken: VToken;
suppliedVToken: VToken;
};

type OpenLeveragedPositionWithSingleAssetInput = {
vToken: VToken;
amountMantissa: bigint;
};

type OpenLeveragedPositionInput =
| OpenLeveragedPositionWithSwapInput
| OpenLeveragedPositionWithSingleAssetInput;

type Options = UseSendTransactionOptions<OpenLeveragedPositionInput>;

export const useOpenLeveragedPosition = (options?: Partial<Options>) => {
const { address: leverageManagerContractAddress } = useGetContractAddress({
name: 'LeverageManager',
});

return useSendTransaction({
fn: (input: OpenLeveragedPositionInput) => {
if (!leverageManagerContractAddress) {
throw new VError({ type: 'unexpected', code: 'somethingWentWrong' });
}

if ('swapQuote' in input) {
return {
abi: leverageManagerAbi,
address: leverageManagerContractAddress,
functionName: 'enterLeverage',
args: [
input.suppliedVToken.address,
0n,
input.borrowedVToken.address,
input.swapQuote.fromTokenAmountSoldMantissa,
input.swapQuote.minimumToTokenAmountReceivedMantissa,
input.swapQuote.callData,
],
} as WriteContractParameters<
typeof leverageManagerAbi,
'enterLeverage',
readonly [Address, bigint, Address, bigint, bigint, Hex],
Chain,
Account
>;
}

return {
abi: leverageManagerAbi,
address: leverageManagerContractAddress,
functionName: 'enterSingleAssetLeverage',
args: [input.vToken.address, 0n, input.amountMantissa],
} as WriteContractParameters<
typeof leverageManagerAbi,
'enterSingleAssetLeverage',
readonly [Address, bigint, bigint],
Chain,
Account
>;
},
onConfirmed: () => {
// TODO: send analytic event

queryClient.invalidateQueries({
queryKey: [FunctionKey.GET_POOLS],
});
},
options,
});
};
Loading