-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2313 from VenusProtocol/feat/unwrap-native-token-…
…borrow-withdraw feat: unwrap native token in borrow/withdraw operations
- Loading branch information
Showing
33 changed files
with
753 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@venusprotocol/evm": minor | ||
--- | ||
|
||
unwrap native token in borrow/withdraw operations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
apps/evm/src/clients/api/mutations/borrowAndUnwrap/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import fakeContractTransaction from '__mocks__/models/contractTransaction'; | ||
|
||
import type { NativeTokenGateway } from 'libs/contracts'; | ||
|
||
import borrowAndUnwrap from '.'; | ||
|
||
const fakeAmountMantissa = new BigNumber('10000000000000000'); | ||
|
||
vi.mock('libs/contracts'); | ||
|
||
describe('borrowAndUnwrap', () => { | ||
it('returns transaction when request succeeds', async () => { | ||
const borrowAndUnwrapMock = vi.fn(() => fakeContractTransaction); | ||
|
||
const fakeNativeTokenGatewayContract = { | ||
borrowAndUnwrap: borrowAndUnwrapMock, | ||
} as unknown as NativeTokenGateway; | ||
|
||
const response = await borrowAndUnwrap({ | ||
nativeTokenGatewayContract: fakeNativeTokenGatewayContract, | ||
amountMantissa: fakeAmountMantissa, | ||
}); | ||
|
||
expect(response).toBe(fakeContractTransaction); | ||
|
||
expect(borrowAndUnwrapMock).toHaveBeenCalledTimes(1); | ||
expect(borrowAndUnwrapMock).toHaveBeenCalledWith(fakeAmountMantissa.toFixed()); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
apps/evm/src/clients/api/mutations/borrowAndUnwrap/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type BigNumber from 'bignumber.js'; | ||
import type { ContractTransaction } from 'ethers'; | ||
|
||
import type { NativeTokenGateway } from 'libs/contracts'; | ||
|
||
export interface BorrowAndUnwrapInput { | ||
nativeTokenGatewayContract: NativeTokenGateway; | ||
amountMantissa: BigNumber; | ||
} | ||
|
||
export type BorrowAndUnwrapOutput = ContractTransaction; | ||
|
||
const borrowAndUnwrap = async ({ | ||
nativeTokenGatewayContract, | ||
amountMantissa, | ||
}: BorrowAndUnwrapInput) => nativeTokenGatewayContract.borrowAndUnwrap(amountMantissa.toFixed()); | ||
|
||
export default borrowAndUnwrap; |
60 changes: 60 additions & 0 deletions
60
apps/evm/src/clients/api/mutations/borrowAndUnwrap/useBorrowAndUnwrap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import borrowAndUnwrap, { type BorrowAndUnwrapInput } from 'clients/api/mutations/borrowAndUnwrap'; | ||
import queryClient from 'clients/api/queryClient'; | ||
import FunctionKey from 'constants/functionKey'; | ||
import { type UseSendTransactionOptions, useSendTransaction } from 'hooks/useSendTransaction'; | ||
import { useGetNativeTokenGatewayContract } from 'libs/contracts'; | ||
import { useChainId } from 'libs/wallet'; | ||
import type { VToken } from 'types'; | ||
import { callOrThrow } from 'utilities'; | ||
|
||
type TrimmedBorrowAndUnwrapInput = Omit<BorrowAndUnwrapInput, 'nativeTokenGatewayContract'>; | ||
type Options = UseSendTransactionOptions<TrimmedBorrowAndUnwrapInput>; | ||
|
||
const useBorrowAndUnwrap = ( | ||
{ vToken, poolComptrollerAddress }: { vToken: VToken; poolComptrollerAddress: string }, | ||
options?: Options, | ||
) => { | ||
const { chainId } = useChainId(); | ||
const nativeToken = vToken.underlyingToken.tokenWrapped; | ||
const nativeTokenGatewayContract = useGetNativeTokenGatewayContract({ | ||
passSigner: true, | ||
comptrollerContractAddress: poolComptrollerAddress, | ||
}); | ||
|
||
return useSendTransaction({ | ||
fnKey: FunctionKey.BORROW_AND_UNWRAP, | ||
fn: (input: TrimmedBorrowAndUnwrapInput) => | ||
callOrThrow({ nativeTokenGatewayContract }, params => | ||
borrowAndUnwrap({ | ||
...input, | ||
...params, | ||
}), | ||
), | ||
onConfirmed: async () => { | ||
const accountAddress = await nativeTokenGatewayContract?.signer.getAddress(); | ||
|
||
queryClient.invalidateQueries(FunctionKey.GET_V_TOKEN_BALANCES_ALL); | ||
queryClient.invalidateQueries([ | ||
FunctionKey.GET_BALANCE_OF, | ||
{ | ||
chainId, | ||
accountAddress, | ||
vTokenAddress: vToken.address, | ||
}, | ||
]); | ||
queryClient.invalidateQueries([ | ||
FunctionKey.GET_BALANCE_OF, | ||
{ | ||
chainId, | ||
accountAddress, | ||
tokenAddress: nativeToken?.address, | ||
}, | ||
]); | ||
queryClient.invalidateQueries(FunctionKey.GET_MAIN_MARKETS); | ||
queryClient.invalidateQueries(FunctionKey.GET_ISOLATED_POOLS); | ||
}, | ||
options, | ||
}); | ||
}; | ||
|
||
export default useBorrowAndUnwrap; |
31 changes: 31 additions & 0 deletions
31
apps/evm/src/clients/api/mutations/redeemAndUnwrap/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import fakeContractTransaction from '__mocks__/models/contractTransaction'; | ||
|
||
import type { NativeTokenGateway } from 'libs/contracts'; | ||
|
||
import redeemAndUnwrap from '.'; | ||
|
||
const fakeAmountMantissa = new BigNumber('10000000000000000'); | ||
|
||
vi.mock('libs/contracts'); | ||
|
||
describe('redeemAndUnwrap', () => { | ||
it('returns transaction when request succeeds', async () => { | ||
const redeemAndUnwrapMock = vi.fn(() => fakeContractTransaction); | ||
|
||
const fakeNativeTokenGatewayContract = { | ||
redeemAndUnwrap: redeemAndUnwrapMock, | ||
} as unknown as NativeTokenGateway; | ||
|
||
const response = await redeemAndUnwrap({ | ||
nativeTokenGatewayContract: fakeNativeTokenGatewayContract, | ||
amountMantissa: fakeAmountMantissa, | ||
}); | ||
|
||
expect(response).toBe(fakeContractTransaction); | ||
|
||
expect(redeemAndUnwrapMock).toHaveBeenCalledTimes(1); | ||
expect(redeemAndUnwrapMock).toHaveBeenCalledWith(fakeAmountMantissa.toFixed()); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
apps/evm/src/clients/api/mutations/redeemAndUnwrap/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type BigNumber from 'bignumber.js'; | ||
import type { ContractTransaction } from 'ethers'; | ||
|
||
import type { NativeTokenGateway } from 'libs/contracts'; | ||
|
||
export interface RedeemAndUnwrapInput { | ||
nativeTokenGatewayContract: NativeTokenGateway; | ||
amountMantissa: BigNumber; | ||
} | ||
|
||
export type RedeemAndUnwrapOutput = ContractTransaction; | ||
|
||
const redeemAndUnwrap = async ({ | ||
nativeTokenGatewayContract, | ||
amountMantissa, | ||
}: RedeemAndUnwrapInput) => nativeTokenGatewayContract.redeemAndUnwrap(amountMantissa.toFixed()); | ||
|
||
export default redeemAndUnwrap; |
60 changes: 60 additions & 0 deletions
60
apps/evm/src/clients/api/mutations/redeemAndUnwrap/useRedeemAndUnwrap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import redeemAndUnwrap, { type RedeemAndUnwrapInput } from 'clients/api/mutations/redeemAndUnwrap'; | ||
import queryClient from 'clients/api/queryClient'; | ||
import FunctionKey from 'constants/functionKey'; | ||
import { type UseSendTransactionOptions, useSendTransaction } from 'hooks/useSendTransaction'; | ||
import { useGetNativeTokenGatewayContract } from 'libs/contracts'; | ||
import { useChainId } from 'libs/wallet'; | ||
import type { VToken } from 'types'; | ||
import { callOrThrow } from 'utilities'; | ||
|
||
type TrimmedRedeemAndUnwrapInput = Omit<RedeemAndUnwrapInput, 'nativeTokenGatewayContract'>; | ||
type Options = UseSendTransactionOptions<TrimmedRedeemAndUnwrapInput>; | ||
|
||
const useRedeemAndUnwrap = ( | ||
{ vToken, poolComptrollerAddress }: { vToken: VToken; poolComptrollerAddress: string }, | ||
options?: Options, | ||
) => { | ||
const { chainId } = useChainId(); | ||
const nativeToken = vToken.underlyingToken.tokenWrapped; | ||
const nativeTokenGatewayContract = useGetNativeTokenGatewayContract({ | ||
passSigner: true, | ||
comptrollerContractAddress: poolComptrollerAddress, | ||
}); | ||
|
||
return useSendTransaction({ | ||
fnKey: FunctionKey.REDEEM_AND_UNWRAP, | ||
fn: (input: TrimmedRedeemAndUnwrapInput) => | ||
callOrThrow({ nativeTokenGatewayContract }, params => | ||
redeemAndUnwrap({ | ||
...input, | ||
...params, | ||
}), | ||
), | ||
onConfirmed: async () => { | ||
const accountAddress = await nativeTokenGatewayContract?.signer.getAddress(); | ||
|
||
queryClient.invalidateQueries(FunctionKey.GET_V_TOKEN_BALANCES_ALL); | ||
queryClient.invalidateQueries([ | ||
FunctionKey.GET_BALANCE_OF, | ||
{ | ||
chainId, | ||
accountAddress, | ||
vTokenAddress: vToken.address, | ||
}, | ||
]); | ||
queryClient.invalidateQueries([ | ||
FunctionKey.GET_BALANCE_OF, | ||
{ | ||
chainId, | ||
accountAddress, | ||
tokenAddress: nativeToken?.address, | ||
}, | ||
]); | ||
queryClient.invalidateQueries(FunctionKey.GET_MAIN_MARKETS); | ||
queryClient.invalidateQueries(FunctionKey.GET_ISOLATED_POOLS); | ||
}, | ||
options, | ||
}); | ||
}; | ||
|
||
export default useRedeemAndUnwrap; |
31 changes: 31 additions & 0 deletions
31
apps/evm/src/clients/api/mutations/redeemUnderlyingAndUnwrap/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import fakeContractTransaction from '__mocks__/models/contractTransaction'; | ||
|
||
import type { NativeTokenGateway } from 'libs/contracts'; | ||
|
||
import redeemAndUnwrap from '.'; | ||
|
||
const fakeAmountMantissa = new BigNumber('10000000000000000'); | ||
|
||
vi.mock('libs/contracts'); | ||
|
||
describe('redeemUnderlyingAndUnwrap', () => { | ||
it('returns transaction when request succeeds', async () => { | ||
const redeemUnderlyingAndUnwrapMock = vi.fn(() => fakeContractTransaction); | ||
|
||
const fakeNativeTokenGatewayContract = { | ||
redeemUnderlyingAndUnwrap: redeemUnderlyingAndUnwrapMock, | ||
} as unknown as NativeTokenGateway; | ||
|
||
const response = await redeemAndUnwrap({ | ||
nativeTokenGatewayContract: fakeNativeTokenGatewayContract, | ||
amountMantissa: fakeAmountMantissa, | ||
}); | ||
|
||
expect(response).toBe(fakeContractTransaction); | ||
|
||
expect(redeemUnderlyingAndUnwrapMock).toHaveBeenCalledTimes(1); | ||
expect(redeemUnderlyingAndUnwrapMock).toHaveBeenCalledWith(fakeAmountMantissa.toFixed()); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
apps/evm/src/clients/api/mutations/redeemUnderlyingAndUnwrap/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type BigNumber from 'bignumber.js'; | ||
import type { ContractTransaction } from 'ethers'; | ||
|
||
import type { NativeTokenGateway } from 'libs/contracts'; | ||
|
||
export interface RedeemUnderlyingAndUnwrapInput { | ||
nativeTokenGatewayContract: NativeTokenGateway; | ||
amountMantissa: BigNumber; | ||
} | ||
|
||
export type RedeemUnderlyingAndUnwrapOutput = ContractTransaction; | ||
|
||
const redeemUnderlyingAndUnwrap = async ({ | ||
nativeTokenGatewayContract, | ||
amountMantissa, | ||
}: RedeemUnderlyingAndUnwrapInput) => | ||
nativeTokenGatewayContract.redeemUnderlyingAndUnwrap(amountMantissa.toFixed()); | ||
|
||
export default redeemUnderlyingAndUnwrap; |
65 changes: 65 additions & 0 deletions
65
apps/evm/src/clients/api/mutations/redeemUnderlyingAndUnwrap/useRedeemUnderlyingAndUnwrap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import redeemUnderlyingAndUnwrap, { | ||
type RedeemUnderlyingAndUnwrapInput, | ||
} from 'clients/api/mutations/redeemUnderlyingAndUnwrap'; | ||
import queryClient from 'clients/api/queryClient'; | ||
import FunctionKey from 'constants/functionKey'; | ||
import { type UseSendTransactionOptions, useSendTransaction } from 'hooks/useSendTransaction'; | ||
import { useGetNativeTokenGatewayContract } from 'libs/contracts'; | ||
import { useChainId } from 'libs/wallet'; | ||
import type { VToken } from 'types'; | ||
import { callOrThrow } from 'utilities'; | ||
|
||
type TrimmedRedeemUnderlyingAndUnwrapInput = Omit< | ||
RedeemUnderlyingAndUnwrapInput, | ||
'nativeTokenGatewayContract' | ||
>; | ||
type Options = UseSendTransactionOptions<TrimmedRedeemUnderlyingAndUnwrapInput>; | ||
|
||
const useRedeemUnderlyingAndUnwrap = ( | ||
{ vToken, poolComptrollerAddress }: { vToken: VToken; poolComptrollerAddress: string }, | ||
options?: Options, | ||
) => { | ||
const { chainId } = useChainId(); | ||
const nativeToken = vToken.underlyingToken.tokenWrapped; | ||
const nativeTokenGatewayContract = useGetNativeTokenGatewayContract({ | ||
passSigner: true, | ||
comptrollerContractAddress: poolComptrollerAddress, | ||
}); | ||
|
||
return useSendTransaction({ | ||
fnKey: FunctionKey.REDEEM_AND_UNWRAP, | ||
fn: (input: TrimmedRedeemUnderlyingAndUnwrapInput) => | ||
callOrThrow({ nativeTokenGatewayContract }, params => | ||
redeemUnderlyingAndUnwrap({ | ||
...input, | ||
...params, | ||
}), | ||
), | ||
onConfirmed: async () => { | ||
const accountAddress = await nativeTokenGatewayContract?.signer.getAddress(); | ||
|
||
queryClient.invalidateQueries(FunctionKey.GET_V_TOKEN_BALANCES_ALL); | ||
queryClient.invalidateQueries([ | ||
FunctionKey.GET_BALANCE_OF, | ||
{ | ||
chainId, | ||
accountAddress, | ||
vTokenAddress: vToken.address, | ||
}, | ||
]); | ||
queryClient.invalidateQueries([ | ||
FunctionKey.GET_BALANCE_OF, | ||
{ | ||
chainId, | ||
accountAddress, | ||
tokenAddress: nativeToken?.address, | ||
}, | ||
]); | ||
queryClient.invalidateQueries(FunctionKey.GET_MAIN_MARKETS); | ||
queryClient.invalidateQueries(FunctionKey.GET_ISOLATED_POOLS); | ||
}, | ||
options, | ||
}); | ||
}; | ||
|
||
export default useRedeemUnderlyingAndUnwrap; |
Oops, something went wrong.