-
Notifications
You must be signed in to change notification settings - Fork 17
feat(ng): evm token/nft approvals #408
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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
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
52 changes: 52 additions & 0 deletions
52
apps/next/src/pages/Approve/ExtensionActionApprovalScreen.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,52 @@ | ||
| import { useCallback } from 'react'; | ||
| import { Typography } from '@avalabs/k2-alpine'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import { ActionStatus } from '@core/types'; | ||
| import { useApproveAction, useGetRequestId } from '@core/ui'; | ||
|
|
||
| import { | ||
| ActionDrawer, | ||
| ApprovalScreenTitle, | ||
| LoadingScreen, | ||
| Styled, | ||
| } from './components'; | ||
|
|
||
| export const ExtensionActionApprovalScreen = () => { | ||
| const { t } = useTranslation(); | ||
|
|
||
| const requestId = useGetRequestId(); | ||
| const { action, updateAction, cancelHandler } = useApproveAction(requestId); | ||
|
|
||
| const approve = useCallback(async () => { | ||
| updateAction({ | ||
| status: ActionStatus.SUBMITTING, | ||
| id: requestId, | ||
| }); | ||
| }, [updateAction, requestId]); | ||
|
|
||
| if (!action) { | ||
| return <LoadingScreen />; | ||
| } | ||
|
|
||
| return ( | ||
| <Styled.ApprovalScreenPage> | ||
| <Styled.NoScrollStack> | ||
| <ApprovalScreenTitle | ||
| title={t('🚧 Placeholder - needs implementation 🚧')} | ||
| /> | ||
| <Typography variant="body3"> | ||
| {t( | ||
| 'This is a placeholder screen just to allow us to reject/approve some wallet-specific interactions (i.e. chain switching).', | ||
| )} | ||
| </Typography> | ||
| </Styled.NoScrollStack> | ||
| <ActionDrawer | ||
| open | ||
| approve={approve} | ||
| reject={cancelHandler} | ||
| action={action} | ||
| /> | ||
| </Styled.ApprovalScreenPage> | ||
| ); | ||
| }; |
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
66 changes: 66 additions & 0 deletions
66
...xt/src/pages/Approve/components/ActionDetails/evm/EvmTokenApprovals/EvmTokenApprovals.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,66 @@ | ||
| import { Stack } from '@avalabs/k2-alpine'; | ||
| import { FC, useRef } from 'react'; | ||
| import { DisplayData } from '@avalabs/vm-module-types'; | ||
|
|
||
| import { Action, EnsureDefined } from '@core/types'; | ||
| import { useBalancesContext } from '@core/ui'; | ||
|
|
||
| import { DetailsSection } from '../../generic/DetailsSection'; | ||
| import { getApprovalValue, isUnlimitedApproval } from './lib'; | ||
| import { CustomApprovalLimit, TokenSpendLimitCard } from './components'; | ||
|
|
||
| type EvmTokenApprovalsProps = { | ||
| action: Action<EnsureDefined<DisplayData, 'tokenApprovals'>>; | ||
| }; | ||
|
|
||
| export const EvmTokenApprovals: FC<EvmTokenApprovalsProps> = ({ action }) => { | ||
| const { getTokenPrice } = useBalancesContext(); | ||
|
|
||
| const { tokenApprovals } = action.displayData; | ||
|
|
||
| const requestedApprovalRef = useRef(tokenApprovals.approvals[0]); | ||
| const hasOnlyOneEditableApproval = | ||
| tokenApprovals.approvals.length === 1 && tokenApprovals.isEditable; | ||
|
|
||
| const currentApprovalValue = getApprovalValue( | ||
| tokenApprovals.approvals[0]!, | ||
gergelylovas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| getTokenPrice, | ||
| ); | ||
| const requestedApproval = requestedApprovalRef.current; | ||
|
|
||
| if (!currentApprovalValue || !requestedApproval) { | ||
| return null; | ||
| } | ||
|
|
||
| const requestedApprovalValue = getApprovalValue( | ||
| requestedApproval, | ||
| getTokenPrice, | ||
| ); | ||
|
|
||
| if (!requestedApprovalValue) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <Stack width="100%" gap={1}> | ||
| <DetailsSection sx={{ py: 2 }}> | ||
| {tokenApprovals.approvals.map((approval) => ( | ||
| <TokenSpendLimitCard | ||
| key={approval.token.address} | ||
| approval={approval} | ||
| approvalValue={currentApprovalValue} | ||
| /> | ||
| ))} | ||
| </DetailsSection> | ||
| {hasOnlyOneEditableApproval && requestedApproval && ( | ||
| <CustomApprovalLimit | ||
| action={action} | ||
| requestedValue={requestedApprovalValue} | ||
| approval={tokenApprovals.approvals[0]!} | ||
gergelylovas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| approvalValue={currentApprovalValue} | ||
| isUnlimitedRequested={isUnlimitedApproval(requestedApproval)} | ||
| /> | ||
| )} | ||
| </Stack> | ||
| ); | ||
| }; | ||
19 changes: 19 additions & 0 deletions
19
...c/pages/Approve/components/ActionDetails/evm/EvmTokenApprovals/components/AmountInput.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,19 @@ | ||
| import { styled } from '@avalabs/k2-alpine'; | ||
|
|
||
| export const AmountInput = styled('input')(({ theme }) => ({ | ||
| background: 'transparent', | ||
| border: 0, | ||
| paddingInline: theme.spacing(0), | ||
| lineHeight: 1, | ||
| outline: 'none', | ||
| color: theme.palette.text.primary, | ||
| textAlign: 'end', | ||
| textOverflow: 'ellipsis', | ||
| '&::-webkit-outer-spin-button, &::-webkit-inner-spin-button': { | ||
| '-webkit-appearance': 'none', | ||
| margin: 0, | ||
| }, | ||
| 'input[type=number]': { | ||
| '-moz-appearance': 'textfield', | ||
| }, | ||
| })); |
91 changes: 91 additions & 0 deletions
91
...Approve/components/ActionDetails/evm/EvmTokenApprovals/components/CustomApprovalLimit.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,91 @@ | ||
| import { MaxUint256 } from 'ethers'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { FC, useCallback, useState } from 'react'; | ||
| import { DisplayData, TokenApproval } from '@avalabs/vm-module-types'; | ||
|
|
||
| import { useConnectionContext } from '@core/ui'; | ||
| import type { UpdateActionTxDataHandler } from '@core/service-worker'; | ||
| import { Action, EnsureDefined, ExtensionRequest } from '@core/types'; | ||
|
|
||
| import { DetailsSection } from '../../../generic/DetailsSection'; | ||
| import { TxDetailsRow } from '../../../generic/DetailsItem/items/DetailRow'; | ||
|
|
||
| import { ApprovalValue, SpendLimit } from '../types'; | ||
| import { CustomLimitTrigger } from './CustomLimitTrigger'; | ||
| import { CustomApprovalLimitOverlay } from './CustomApprovalLimitOverlay'; | ||
|
|
||
| type CustomApprovalLimitProps = { | ||
| action: Action<EnsureDefined<DisplayData, 'tokenApprovals'>>; | ||
| approval: TokenApproval; | ||
| requestedValue: ApprovalValue; | ||
| approvalValue: ApprovalValue; | ||
| isUnlimitedRequested: boolean; | ||
| }; | ||
|
|
||
| export const CustomApprovalLimit: FC<CustomApprovalLimitProps> = ({ | ||
| action, | ||
| approval, | ||
| requestedValue, | ||
| approvalValue, | ||
| isUnlimitedRequested, | ||
| }) => { | ||
| const { t } = useTranslation(); | ||
| const { request } = useConnectionContext(); | ||
| const [isDialogOpen, setIsDialogOpen] = useState(false); | ||
|
|
||
| const { tokenValue } = approvalValue; | ||
|
|
||
| const [spendLimit, setSpendLimit] = useState<SpendLimit>({ | ||
| type: isUnlimitedRequested ? 'unlimited' : 'requested', | ||
| value: tokenValue?.toSubUnit() ?? 0n, | ||
| }); | ||
|
|
||
| const updateApprovalLimit = useCallback(() => { | ||
| if (!action.actionId) return; | ||
|
|
||
| const limitAmount = | ||
| spendLimit.type === 'unlimited' ? MaxUint256 : (spendLimit.value ?? 0n); | ||
|
|
||
| request<UpdateActionTxDataHandler>({ | ||
| method: ExtensionRequest.ACTION_UPDATE_TX_DATA, | ||
| params: [ | ||
| action.actionId, | ||
| { approvalLimit: `0x${limitAmount.toString(16)}` }, | ||
| ], | ||
| }); | ||
| }, [request, action.actionId, spendLimit]); | ||
|
|
||
| const handleSave = () => { | ||
| setIsDialogOpen(false); | ||
| updateApprovalLimit(); | ||
| }; | ||
|
|
||
| if (!tokenValue) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <DetailsSection> | ||
| <TxDetailsRow label={t('Spend limit')}> | ||
| <CustomLimitTrigger | ||
| approval={approval} | ||
| approvalValue={approvalValue} | ||
| spendLimit={spendLimit} | ||
| onClick={() => setIsDialogOpen(true)} | ||
| /> | ||
| </TxDetailsRow> | ||
| <CustomApprovalLimitOverlay | ||
| isDialogOpen={isDialogOpen} | ||
| onClose={() => setIsDialogOpen(false)} | ||
| spendLimit={spendLimit} | ||
| setSpendLimit={setSpendLimit} | ||
| approval={approval} | ||
| requestedValue={requestedValue} | ||
| isUnlimitedRequested={isUnlimitedRequested} | ||
| onSave={handleSave} | ||
| /> | ||
| </DetailsSection> | ||
| </> | ||
| ); | ||
| }; |
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.
Uh oh!
There was an error while loading. Please reload this page.