-
Notifications
You must be signed in to change notification settings - Fork 491
feat: let users cap their repay approval, and show the amount needed #3090
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
Open
sammdec
wants to merge
3
commits into
main
Choose a base branch
from
feat/repay-exact-approval-amount
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
113 changes: 113 additions & 0 deletions
113
src/components/transactions/Repay/RepayAllowanceControl.tsx
This file contains hidden or 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,113 @@ | ||
| import { CheckIcon } from '@heroicons/react/outline'; | ||
| import { CogIcon } from '@heroicons/react/solid'; | ||
| import { Trans } from '@lingui/macro'; | ||
| import { | ||
| Box, | ||
| ListItemIcon, | ||
| ListItemText, | ||
| Menu, | ||
| MenuItem, | ||
| SvgIcon, | ||
| Typography, | ||
| } from '@mui/material'; | ||
| import * as React from 'react'; | ||
|
|
||
| export enum RepayAllowance { | ||
| EXACT = 'exact', | ||
| UNLIMITED = 'unlimited', | ||
| } | ||
|
|
||
| interface RepayAllowanceControlProps { | ||
| allowance: RepayAllowance; | ||
| setAllowance: (allowance: RepayAllowance) => void; | ||
| /** Full-precision amount that will be approved when EXACT is selected. */ | ||
| exactAmount: string; | ||
| symbol: string; | ||
| } | ||
|
|
||
| /** | ||
| * Repay's own allowance picker, rather than an option on the shared | ||
| * ApprovalMethodToggleButton. Being Repay-specific is the point: it knows the amount, so | ||
| * it can show the figure the approval gate is asking for. That number is otherwise | ||
| * invisible, which is what left users re-approving an allowance they thought was ample. | ||
| */ | ||
| export const RepayAllowanceControl = ({ | ||
| allowance, | ||
| setAllowance, | ||
| exactAmount, | ||
| symbol, | ||
| }: RepayAllowanceControlProps) => { | ||
| const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null); | ||
| const isExact = allowance === RepayAllowance.EXACT; | ||
|
|
||
| const select = (next: RepayAllowance) => { | ||
| setAllowance(next); | ||
| setAnchorEl(null); | ||
| }; | ||
|
|
||
| return ( | ||
| <Box sx={{ display: 'inline-flex', alignItems: 'center', mb: 2 }}> | ||
| <Typography variant="subheader2" color="text.secondary"> | ||
| <Trans>Allowance</Trans> | ||
| </Typography> | ||
| <Box | ||
| onClick={(event: React.MouseEvent<HTMLDivElement>) => setAnchorEl(event.currentTarget)} | ||
| sx={{ display: 'flex', alignItems: 'center', cursor: 'pointer' }} | ||
| data-cy="repayAllowanceChange" | ||
| > | ||
| <Typography variant="subheader2" color="info.main" component="span"> | ||
| {isExact ? `${exactAmount} ${symbol}` : <Trans>Unlimited</Trans>} | ||
| </Typography> | ||
| <SvgIcon sx={{ fontSize: 16, ml: 1, color: 'info.main' }}> | ||
| <CogIcon /> | ||
| </SvgIcon> | ||
| </Box> | ||
|
|
||
| <Menu | ||
| anchorEl={anchorEl} | ||
| open={Boolean(anchorEl)} | ||
| onClose={() => setAnchorEl(null)} | ||
| keepMounted={true} | ||
| data-cy={`repayAllowanceMenu_${allowance}`} | ||
| > | ||
| <MenuItem | ||
| data-cy="repayAllowanceOption_exact" | ||
| selected={isExact} | ||
| onClick={() => select(RepayAllowance.EXACT)} | ||
| > | ||
| <ListItemText | ||
| primaryTypographyProps={{ variant: 'subheader1' }} | ||
| secondaryTypographyProps={{ variant: 'caption' }} | ||
| secondary={ | ||
| <Trans> | ||
| {exactAmount} {symbol} — covers interest accruing before the transaction lands | ||
| </Trans> | ||
| } | ||
| > | ||
| <Trans>Exact amount</Trans> | ||
| </ListItemText> | ||
| <ListItemIcon> | ||
| <SvgIcon>{isExact && <CheckIcon />}</SvgIcon> | ||
| </ListItemIcon> | ||
| </MenuItem> | ||
|
|
||
| <MenuItem | ||
| data-cy="repayAllowanceOption_unlimited" | ||
| selected={!isExact} | ||
| onClick={() => select(RepayAllowance.UNLIMITED)} | ||
| > | ||
| <ListItemText | ||
| primaryTypographyProps={{ variant: 'subheader1' }} | ||
| secondaryTypographyProps={{ variant: 'caption' }} | ||
| secondary={<Trans>No further approvals needed for future repays</Trans>} | ||
| > | ||
| <Trans>Unlimited</Trans> | ||
| </ListItemText> | ||
| <ListItemIcon> | ||
| <SvgIcon>{!isExact && <CheckIcon />}</SvgIcon> | ||
| </ListItemIcon> | ||
| </MenuItem> | ||
| </Menu> | ||
| </Box> | ||
| ); | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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,103 @@ | ||
| import { BigNumber } from 'bignumber.js'; | ||
| import { parseUnits } from 'ethers/lib/utils'; | ||
|
|
||
| import { checkRequiresApproval, getRepayAmountToApprove, getSafeAmountToRepayAll } from '../utils'; | ||
|
|
||
| /** | ||
| * Numbers taken from the cbBTC support report on Aave V3 Ethereum | ||
| * (wallet 0xCA686974913389D42F3C5F61010503DAccDb487a, block 25703709). | ||
| * The user approved 32.7 by hand, which the UI never accepted. | ||
| */ | ||
| const DEBT = '32.68074616'; | ||
| const HAND_SET_APPROVAL = '32.7'; | ||
| const DECIMALS = 8; | ||
|
|
||
| const gateRequires = (debt: string) => getSafeAmountToRepayAll(debt, DECIMALS).toString(10); | ||
|
|
||
| /** Does an allowance of `approved` let the user past the approval gate for this debt? */ | ||
| const passesGate = (approved: string, debt: string) => | ||
| !checkRequiresApproval({ | ||
| approvedAmount: approved, | ||
| amount: gateRequires(debt), | ||
| signedAmount: '0', | ||
| }); | ||
|
|
||
| const approvalForMaxRepay = (debt: string) => | ||
| getRepayAmountToApprove({ | ||
| amountRequiringApproval: gateRequires(debt), | ||
| isMaxRepay: true, | ||
| decimals: DECIMALS, | ||
| }); | ||
|
|
||
| describe('getSafeAmountToRepayAll', () => { | ||
| it('applies the full-repay buffer on top of the debt', () => { | ||
| expect(gateRequires(DEBT)).toBe('32.76244803'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getRepayAmountToApprove', () => { | ||
| it('leaves a partial repay amount untouched', () => { | ||
| expect( | ||
| getRepayAmountToApprove({ | ||
| amountRequiringApproval: '10.5', | ||
| isMaxRepay: false, | ||
| decimals: DECIMALS, | ||
| }) | ||
| ).toBe('10.5'); | ||
| }); | ||
|
|
||
| it('trims a typed amount to the token decimals so parseUnits cannot throw', () => { | ||
| // The repay input has no decimalScale, so 7dp on a 6dp token is reachable by typing. | ||
| const approved = getRepayAmountToApprove({ | ||
| amountRequiringApproval: '1.1234567', | ||
| isMaxRepay: false, | ||
| decimals: 6, | ||
| }); | ||
|
|
||
| expect(approved).toBe('1.123457'); | ||
| expect(() => parseUnits(approved, 6)).not.toThrow(); | ||
| }); | ||
|
|
||
| it('rounds a typed amount up, so the trimmed approval still clears the gate', () => { | ||
| const typed = '1.1234567'; | ||
| const approved = getRepayAmountToApprove({ | ||
| amountRequiringApproval: typed, | ||
| isMaxRepay: false, | ||
| decimals: 6, | ||
| }); | ||
|
|
||
| expect(new BigNumber(approved).isGreaterThanOrEqualTo(typed)).toBe(true); | ||
| expect( | ||
| checkRequiresApproval({ approvedAmount: approved, amount: typed, signedAmount: '0' }) | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| it('reproduces the reported bug: a hand-set approval above the debt still fails the gate', () => { | ||
| expect(new BigNumber(HAND_SET_APPROVAL).isGreaterThan(DEBT)).toBe(true); | ||
| expect(passesGate(HAND_SET_APPROVAL, DEBT)).toBe(false); | ||
| }); | ||
|
|
||
| it('approves an amount that satisfies the gate for a full repay', () => { | ||
| expect(passesGate(approvalForMaxRepay(DEBT), DEBT)).toBe(true); | ||
| }); | ||
|
|
||
| it('still satisfies the gate after the debt accrues while the approval is in flight', () => { | ||
| const approved = approvalForMaxRepay(DEBT); | ||
| // Debt keeps growing, so the gate's target creeps up after we build the approval. | ||
| const accruedDebt = new BigNumber(DEBT).multipliedBy('1.002').toString(10); | ||
|
|
||
| // Approving exactly what the gate asked for at t0 would now fall short - | ||
| // this is what the margin exists to absorb. | ||
| expect(passesGate(gateRequires(DEBT), accruedDebt)).toBe(false); | ||
| expect(passesGate(approved, accruedDebt)).toBe(true); | ||
| }); | ||
|
|
||
| it('never returns more decimals than the token supports', () => { | ||
| const approved = approvalForMaxRepay(DEBT); | ||
| expect(new BigNumber(approved).decimalPlaces()).toBeLessThanOrEqual(DECIMALS); | ||
| }); | ||
|
|
||
| it('does not return exponential notation for dust-sized debts', () => { | ||
| expect(approvalForMaxRepay('0.00000001')).not.toMatch(/e/i); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This runs during render, and
getRepayAmountToApprovereturns the typed string untouched on the partial branch. The repay input has nodecimalScale(AssetInput.tsx:39), so USDC (6dp) +1.1234567+ "Exact amount" throwsfractional component exceeds decimalsand takes the modal down. The existingparseUnitscalls are insideaction()'s try/catch, so today the same input just fails the tx..decimalPlaces(decimals, BigNumber.ROUND_UP).toString(10)on both branches fixes it, and stops the footer showing more decimals than the token has.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed -
NumberFormatCustomhas nodecimalScale, so 7dp on a 6dp token is reachable by typing, and this one runs at render rather than insideaction().getRepayAmountToApprovenow rounds on both branches (.decimalPlaces(decimals, ROUND_UP).toString(10)), soparseUnitscannot throw and the footer stops showing decimals the token does not have. Rounding up rather than down keeps the approval at or above the gate target, so no approve loop. Two tests added inutils.test.tsfor the trim and for the gate still passing afterwards.