|
| 1 | +import { queryClient } from 'clients/api'; |
| 2 | +import FunctionKey from 'constants/functionKey'; |
| 3 | +import { useGetContractAddress } from 'hooks/useGetContractAddress'; |
| 4 | +import { type UseSendTransactionOptions, useSendTransaction } from 'hooks/useSendTransaction'; |
| 5 | +import { governorBravoDelegateAbi } from 'libs/contracts'; |
| 6 | +import { VError } from 'libs/errors'; |
| 7 | + |
| 8 | +type OpenLeveragedPositionInput = { |
| 9 | + proposalId: number; |
| 10 | +}; |
| 11 | + |
| 12 | +type Options = UseSendTransactionOptions<OpenLeveragedPositionInput>; |
| 13 | + |
| 14 | +// TODO: add tests |
| 15 | + |
| 16 | +export const useOpenLeveragedPositions = (options?: Partial<Options>) => { |
| 17 | + const { address: governorBravoDelegateContractAddress } = useGetContractAddress({ |
| 18 | + name: 'GovernorBravoDelegate', |
| 19 | + }); |
| 20 | + |
| 21 | + return useSendTransaction({ |
| 22 | + fn: ({ proposalId }: OpenLeveragedPositionInput) => { |
| 23 | + if (!governorBravoDelegateContractAddress) { |
| 24 | + throw new VError({ type: 'unexpected', code: 'somethingWentWrong' }); |
| 25 | + } |
| 26 | + |
| 27 | + return { |
| 28 | + abi: governorBravoDelegateAbi, |
| 29 | + address: governorBravoDelegateContractAddress, |
| 30 | + functionName: 'queue', |
| 31 | + args: [BigInt(proposalId)], |
| 32 | + }; |
| 33 | + }, |
| 34 | + onConfirmed: ({ input }) => { |
| 35 | + queryClient.invalidateQueries({ |
| 36 | + queryKey: [FunctionKey.GET_PROPOSALS], |
| 37 | + }); |
| 38 | + |
| 39 | + queryClient.invalidateQueries({ |
| 40 | + queryKey: [ |
| 41 | + FunctionKey.GET_PROPOSAL, |
| 42 | + { |
| 43 | + id: input.proposalId, |
| 44 | + }, |
| 45 | + ], |
| 46 | + }); |
| 47 | + }, |
| 48 | + options, |
| 49 | + }); |
| 50 | +}; |
0 commit comments