Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unwrap native token in borrow/withdraw operations #2313

Merged
merged 20 commits into from
Mar 15, 2024

Conversation

gleiser-oliveira
Copy link
Contributor

@gleiser-oliveira gleiser-oliveira commented Mar 7, 2024

Jira ticket(s)

VEN-2391

Changes

  • Wired up BorrowForm with borrowAndUnwrap
  • Wired up WithdrawForm with redeemUnderlyingAndUnwrap and redeemAndUnwrap
  • Updated addresses for the NativeTokenGateway in all testnet chains

Copy link

changeset-bot bot commented Mar 7, 2024

🦋 Changeset detected

Latest commit: 9e5727e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@venusprotocol/evm Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Mar 7, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
mainnet ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 15, 2024 3:49pm
preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 15, 2024 3:49pm
testnet ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 15, 2024 3:49pm

@gleiser-oliveira gleiser-oliveira changed the title feat: unwrap native token in borrowwithdraw operations feat: unwrap native token in borrow/withdraw operations Mar 7, 2024
Copy link
Contributor

github-actions bot commented Mar 7, 2024

Coverage Report for ./apps/evm

Status Category Percentage Covered / Total
🔵 Lines 79.14% 46467 / 58712
🔵 Statements 79.14% 46467 / 58712
🔵 Functions 71.27% 876 / 1229
🔵 Branches 82.76% 3646 / 4405
File Coverage
File Stmts % Branch % Funcs % Lines Uncovered Lines
Changed Files
apps/evm/src/clients/api/index.ts 0% 0% 0% 0% 1-406
apps/evm/src/clients/api/mutations/borrowAndUnwrap/index.ts 100% 100% 100% 100%
apps/evm/src/clients/api/mutations/borrowAndUnwrap/useBorrowAndUnwrap.ts 0% 0% 0% 0% 1-60
apps/evm/src/clients/api/mutations/redeemAndUnwrap/index.ts 100% 100% 100% 100%
apps/evm/src/clients/api/mutations/redeemAndUnwrap/useRedeemAndUnwrap.ts 0% 0% 0% 0% 1-60
apps/evm/src/clients/api/mutations/redeemUnderlyingAndUnwrap/index.ts 100% 100% 100% 100%
apps/evm/src/clients/api/mutations/redeemUnderlyingAndUnwrap/useRedeemUnderlyingAndUnwrap.ts 0% 0% 0% 0% 1-65
apps/evm/src/constants/functionKey.ts 100% 100% 100% 100%
apps/evm/src/hooks/useDelegateApproval/index.ts 100% 71.42% 100% 100%
apps/evm/src/hooks/useOperationModal/Modal/BorrowForm/index.tsx 98.02% 96.42% 71.42% 98.02% 62-66, 105
apps/evm/src/hooks/useOperationModal/Modal/RepayForm/index.tsx 99.17% 90.69% 100% 99.17% 186-187, 411, 546-547
apps/evm/src/hooks/useOperationModal/Modal/RepayForm/useForm/index.tsx 97.35% 85.71% 100% 97.35% 73-74, 92-93
apps/evm/src/hooks/useOperationModal/Modal/RepayForm/useForm/useFormValidation.ts 100% 100% 100% 100%
apps/evm/src/hooks/useOperationModal/Modal/SupplyForm/index.tsx 96.91% 90.69% 100% 96.91% 201-202, 244-258, 464, 595-596
apps/evm/src/hooks/useOperationModal/Modal/SupplyForm/useForm/index.tsx 95.6% 60% 100% 95.6% 63-64, 80-81
apps/evm/src/hooks/useOperationModal/Modal/SupplyForm/useForm/useFormValidation.ts 100% 100% 100% 100%
apps/evm/src/hooks/useOperationModal/Modal/WithdrawForm/index.tsx 93.95% 86.11% 66.66% 93.95% 69-73, 101-102, 121, 150-151, 302-306, 338-344
apps/evm/src/libs/contracts/config/index.ts 0% 0% 0% 0% 1-414
apps/evm/src/libs/tokens/infos/commonTokens/bscMainnet.ts 100% 100% 100% 100%
apps/evm/src/libs/tokens/infos/commonTokens/opBnbMainnet.ts 100% 100% 100% 100%
apps/evm/src/utilities/index.ts 100% 100% 100% 100%
apps/evm/src/utilities/getUniqueTokenBalances/index.ts 100% 100% 100% 100%
Generated in workflow #7735

@gleiser-oliveira gleiser-oliveira force-pushed the feat/unwrap-native-token-borrow-withdraw branch from 653a124 to cd8212e Compare March 7, 2024 16:56
@gleiser-oliveira gleiser-oliveira force-pushed the feat/unwrap-native-token-borrow-withdraw branch from cd8212e to 224d516 Compare March 11, 2024 16:46
@gleiser-oliveira gleiser-oliveira force-pushed the feat/unwrap-native-token-borrow-withdraw branch from 224d516 to 5435382 Compare March 11, 2024 18:58
@gleiser-oliveira gleiser-oliveira force-pushed the feat/unwrap-native-token-borrow-withdraw branch from 5435382 to 7bf9d31 Compare March 12, 2024 04:56
@gleiser-oliveira gleiser-oliveira force-pushed the feat/unwrap-native-token-borrow-withdraw branch from 7bf9d31 to 2a0d2e1 Compare March 13, 2024 20:01
Comment on lines 404 to 405
const shouldSelectNativeToken =
canWrapNativeToken && nativeTokenBalanceTokens?.gte(asset.userWalletBalanceTokens);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should ever select the token being wraped by default. The UX can be confusing for users this way, as they'd be looking at a certain market (say WETH), but the form would directly propose them to repay their balance with another token (ETH). For someone who isn't familiar with the UI, they might think they can't repay using WETH.

After checking I realized we have this logic on the Supply form too. Let me know what you think, personally I'd remove the logic from both forms.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a request from product, to select either the native token or the wrapped version, based on the balance the user has

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Then my only suggestion would be to change this line to use gt instead gte, so that the underlying token is still favored over over the token being wrapped. What do you think?

Comment on lines 457 to 458
const shouldSelectNativeToken =
canWrapNativeToken && nativeTokenBalanceTokens?.gte(asset.userWalletBalanceTokens);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as on the Repay form: I wouldn't change the token selected by default.

Copy link
Member

@therealemjy therealemjy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While testing the UX with the new wrap/unwrap features, I realized we currently cannot repay an entire WETH loan using ETH. The way we currently do this for other markets is by passing MAX_UINT256 to the repayBorrow function of the corresponding VToken contract, but as far as I can understand the NativeTokenGateway contract does not allow to do the same.

I've asked the contract team to confirm whether my assumption is correct and if the contract can be updated to permit this behavior ((here)[https://github.com/VenusProtocol/isolated-pools/pull/361#discussion_r1526043355]).

Comment on lines 330 to +335
if (vTokenBalanceMantissa) {
if (formValues.receiveNativeToken) {
return redeemAndUnwrap({
amountMantissa: vTokenBalanceMantissa,
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use nested IF statements, it tends to make the logic harder to read.

A nice way to do that could be to merge all the redeem mutation functions into one withdraw function under the API client (similarly to how the repay function handles multiple use cases), which would determine what contract to call based on parameters passed to it. What do you think?
We can do this in a separate PR and I'm happy to assist. I'd follow this logic for all four actions: supply, borrow, withdraw, repay, this way we might simplify the forms a little bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good! I agree, we can refactor it in a separate PR, this way the form only has to reach out a single mutation and keep this logic out of it.

Comment on lines +6 to +12
if (tokenBalance.token.address in acc) {
return acc;
}
return {
...acc,
[tokenBalance.token.address]: tokenBalance,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: you could use the areAddressesEqual utility function to prevent cases where the same token is returned twice with the same address but using a different casing.

@@ -387,6 +392,27 @@ const RepayForm: React.FC<RepayFormProps> = ({ asset, pool, onCloseModal }) => {
[isWrapUnwrapNativeTokenEnabled, asset.vToken.underlyingToken.tokenWrapped],
);

const nativeTokenBalanceTokens = useMemo(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep prefixing variables to make it easy to understand what balance we are defining here: userWalletNativeTokenBalanceTokens.

I'd follow this logic on other forms as well (Supply/Borrow/Withdraw).

fromToken: asset.vToken.underlyingToken,
fixedRepayPercentage: undefined,
});
const { data: nativeTokenBalanceData } = useGetBalanceOf(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think we should keep prefixing variables to make it easy to understand what balance we are defining here: userWalletNativeTokenBalanceData.

@gleiser-oliveira gleiser-oliveira merged commit 5ac89b2 into main Mar 15, 2024
6 checks passed
@gleiser-oliveira gleiser-oliveira deleted the feat/unwrap-native-token-borrow-withdraw branch March 15, 2024 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants