|
| 1 | +import type { Account, Address, Chain, Hex, WriteContractParameters } from 'viem'; |
| 2 | + |
| 3 | +import { queryClient } from 'clients/api'; |
| 4 | +import FunctionKey from 'constants/functionKey'; |
| 5 | +import { useGetContractAddress } from 'hooks/useGetContractAddress'; |
| 6 | +import { type UseSendTransactionOptions, useSendTransaction } from 'hooks/useSendTransaction'; |
| 7 | +import { leverageManagerAbi } from 'libs/contracts'; |
| 8 | +import { VError } from 'libs/errors'; |
| 9 | +import type { ExactInSwapQuote, VToken } from 'types'; |
| 10 | + |
| 11 | +type OpenLeveragedPositionWithSwapInput = { |
| 12 | + swapQuote: ExactInSwapQuote; |
| 13 | + borrowedVToken: VToken; |
| 14 | + suppliedVToken: VToken; |
| 15 | +}; |
| 16 | + |
| 17 | +type OpenLeveragedPositionWithSingleAssetInput = { |
| 18 | + vToken: VToken; |
| 19 | + amountMantissa: bigint; |
| 20 | +}; |
| 21 | + |
| 22 | +type OpenLeveragedPositionInput = |
| 23 | + | OpenLeveragedPositionWithSwapInput |
| 24 | + | OpenLeveragedPositionWithSingleAssetInput; |
| 25 | + |
| 26 | +type Options = UseSendTransactionOptions<OpenLeveragedPositionInput>; |
| 27 | + |
| 28 | +// TODO: add tests |
| 29 | + |
| 30 | +export const useOpenLeveragedPosition = (options?: Partial<Options>) => { |
| 31 | + const { address: leverageManagerContractAddress } = useGetContractAddress({ |
| 32 | + name: 'LeverageManager', |
| 33 | + }); |
| 34 | + |
| 35 | + return useSendTransaction({ |
| 36 | + fn: (input: OpenLeveragedPositionInput) => { |
| 37 | + if (!leverageManagerContractAddress) { |
| 38 | + throw new VError({ type: 'unexpected', code: 'somethingWentWrong' }); |
| 39 | + } |
| 40 | + |
| 41 | + if ('swapQuote' in input) { |
| 42 | + console.log('Open leveraged position with swap', [ |
| 43 | + input.suppliedVToken.address, |
| 44 | + input.borrowedVToken.address, |
| 45 | + 0n, |
| 46 | + input.swapQuote.fromTokenAmountSoldMantissa, |
| 47 | + input.swapQuote.minimumToTokenAmountReceivedMantissa, |
| 48 | + input.swapQuote.callData, |
| 49 | + ]); |
| 50 | + |
| 51 | + return { |
| 52 | + abi: leverageManagerAbi, |
| 53 | + address: leverageManagerContractAddress, |
| 54 | + functionName: 'enterLeverageFromBorrow', |
| 55 | + args: [ |
| 56 | + input.suppliedVToken.address, |
| 57 | + input.borrowedVToken.address, |
| 58 | + 0n, |
| 59 | + input.swapQuote.fromTokenAmountSoldMantissa, |
| 60 | + input.swapQuote.minimumToTokenAmountReceivedMantissa, |
| 61 | + input.swapQuote.callData, |
| 62 | + ], |
| 63 | + } as WriteContractParameters< |
| 64 | + typeof leverageManagerAbi, |
| 65 | + 'enterLeverageFromBorrow', |
| 66 | + readonly [Address, Address, bigint, bigint, bigint, Hex], |
| 67 | + Chain, |
| 68 | + Account |
| 69 | + >; |
| 70 | + } |
| 71 | + |
| 72 | + console.log('Open leveraged position without swap', [input.vToken, 0n, input.amountMantissa]); |
| 73 | + |
| 74 | + return { |
| 75 | + abi: leverageManagerAbi, |
| 76 | + address: leverageManagerContractAddress, |
| 77 | + functionName: 'enterSingleAssetLeverage', |
| 78 | + args: [input.vToken.address, 0n, input.amountMantissa], |
| 79 | + } as WriteContractParameters< |
| 80 | + typeof leverageManagerAbi, |
| 81 | + 'enterSingleAssetLeverage', |
| 82 | + readonly [Address, bigint, bigint], |
| 83 | + Chain, |
| 84 | + Account |
| 85 | + >; |
| 86 | + }, |
| 87 | + onConfirmed: () => { |
| 88 | + // TODO: send analytic event |
| 89 | + |
| 90 | + queryClient.invalidateQueries({ |
| 91 | + queryKey: [FunctionKey.GET_POOLS], |
| 92 | + }); |
| 93 | + }, |
| 94 | + options, |
| 95 | + }); |
| 96 | +}; |
0 commit comments