-
Notifications
You must be signed in to change notification settings - Fork 316
feat: add transaction dialog in saving plugin #6076
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,29 +1,31 @@ | ||||||
| import BigNumber from 'bignumber.js' | ||||||
| import { Typography } from '@mui/material' | ||||||
| import { useState, useMemo, useCallback } from 'react' | ||||||
| import { useCallback, useEffect, useMemo, useState } from 'react' | ||||||
| import { useAsync, useAsyncFn } from 'react-use' | ||||||
| import { unreachable } from '@dimensiondev/kit' | ||||||
| import { isLessThan, rightShift } from '@masknet/web3-shared-base' | ||||||
| import { | ||||||
| createContract, | ||||||
| createERC20Token, | ||||||
| EthereumTokenType, | ||||||
| useFungibleTokenBalance, | ||||||
| useWeb3, | ||||||
| useAccount, | ||||||
| formatCurrency, | ||||||
| formatBalance, | ||||||
| formatCurrency, | ||||||
| getAaveConstants, | ||||||
| isSameAddress, | ||||||
| TransactionState, | ||||||
| TransactionStateType, | ||||||
| useAccount, | ||||||
| useFungibleTokenBalance, | ||||||
| useTokenConstants, | ||||||
| createERC20Token, | ||||||
| getAaveConstants, | ||||||
| useWeb3, | ||||||
| ZERO_ADDRESS, | ||||||
| createContract, | ||||||
| } from '@masknet/web3-shared-evm' | ||||||
| import { TokenAmountPanel, FormattedCurrency, LoadingAnimation, TokenIcon } from '@masknet/shared' | ||||||
| import { FormattedCurrency, LoadingAnimation, TokenAmountPanel, TokenIcon } from '@masknet/shared' | ||||||
| import { useRemoteControlledDialog } from '@masknet/shared-base-ui' | ||||||
| import { useTokenPrice } from '../../Wallet/hooks/useTokenPrice' | ||||||
| import { useI18N } from '../../../utils' | ||||||
| import { useStyles } from './SavingsFormStyles' | ||||||
| import { TabType, ProtocolType, SavingsProtocol } from '../types' | ||||||
| import { ProtocolType, SavingsProtocol, TabType } from '../types' | ||||||
| import { EthereumWalletConnectedBoundary } from '../../../web3/UI/EthereumWalletConnectedBoundary' | ||||||
| import { EthereumChainBoundary } from '../../../web3/UI/EthereumChainBoundary' | ||||||
| import { ActionButtonPromise } from '../../../extension/options-page/DashboardComponents/ActionButton' | ||||||
|
|
@@ -33,6 +35,10 @@ import { EthereumERC20TokenApprovedBoundary } from '../../../web3/UI/EthereumERC | |||||
| import type { AaveLendingPoolAddressProvider } from '@masknet/web3-contracts/types/AaveLendingPoolAddressProvider' | ||||||
| import AaveLendingPoolAddressProviderABI from '@masknet/web3-contracts/abis/AaveLendingPoolAddressProvider.json' | ||||||
| import type { AbiItem } from 'web3-utils' | ||||||
| import { WalletMessages } from '../../Wallet/messages' | ||||||
| import { isTwitter } from '../../../social-network-adaptor/twitter.com/base' | ||||||
| import { activatedSocialNetworkUI } from '../../../social-network' | ||||||
| import { isFacebook } from '../../../social-network-adaptor/facebook.com/base' | ||||||
|
|
||||||
| export interface SavingsFormProps { | ||||||
| chainId: number | ||||||
|
|
@@ -41,6 +47,11 @@ export interface SavingsFormProps { | |||||
| onClose?: () => void | ||||||
| } | ||||||
|
|
||||||
| const ProtocolName = { | ||||||
| [ProtocolType.Lido]: 'lido', | ||||||
| [ProtocolType.AAVE]: 'aave', | ||||||
| } | ||||||
|
|
||||||
| export function SavingsForm({ chainId, protocol, tab, onClose }: SavingsFormProps) { | ||||||
| const { t } = useI18N() | ||||||
| const { classes } = useStyles() | ||||||
|
|
@@ -50,6 +61,10 @@ export function SavingsForm({ chainId, protocol, tab, onClose }: SavingsFormProp | |||||
| const { NATIVE_TOKEN_ADDRESS } = useTokenConstants() | ||||||
| const [inputAmount, setInputAmount] = useState('') | ||||||
| const [estimatedGas, setEstimatedGas] = useState<BigNumber.Value>(new BigNumber('0')) | ||||||
| const [tradeState, setTradeState] = useState<TransactionState>({ | ||||||
| type: TransactionStateType.UNKNOWN, | ||||||
| }) | ||||||
| const [isOpen, setIsOpen] = useState(false) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| const { value: nativeTokenBalance } = useFungibleTokenBalance(EthereumTokenType.Native, '', chainId) | ||||||
|
|
||||||
|
|
@@ -151,10 +166,34 @@ export function SavingsForm({ chainId, protocol, tab, onClose }: SavingsFormProp | |||||
| } | ||||||
| }, [protocol.bareToken, inputAmount, chainId]) | ||||||
|
|
||||||
| const { setDialog: setTransactionDialog } = useRemoteControlledDialog( | ||||||
| WalletMessages.events.transactionDialogUpdated, | ||||||
| (ev) => { | ||||||
| if (ev.open) return | ||||||
| setTradeState({ | ||||||
| type: TransactionStateType.UNKNOWN, | ||||||
| }) | ||||||
| }, | ||||||
| ) | ||||||
|
|
||||||
| const [, executor] = useAsyncFn(async () => { | ||||||
| switch (tab) { | ||||||
| case TabType.Deposit: | ||||||
| if (!(await protocol.deposit(account, chainId, web3, tokenAmount))) { | ||||||
| setTradeState({ | ||||||
| type: TransactionStateType.WAIT_FOR_CONFIRMING, | ||||||
| }) | ||||||
| if ( | ||||||
| !(await protocol.deposit(account, chainId, web3, tokenAmount, (state) => { | ||||||
| setTradeState((prev) => { | ||||||
| if ( | ||||||
| prev.type === TransactionStateType.UNKNOWN && | ||||||
| state.type === TransactionStateType.CONFIRMED | ||||||
| ) | ||||||
| return prev | ||||||
| return state | ||||||
| }) | ||||||
| })) | ||||||
| ) { | ||||||
| throw new Error('Failed to deposit token.') | ||||||
| } else { | ||||||
| await protocol.updateBalance(chainId, web3, account) | ||||||
|
|
@@ -179,6 +218,24 @@ export function SavingsForm({ chainId, protocol, tab, onClose }: SavingsFormProp | |||||
| } | ||||||
| }, [tab, protocol, account, chainId, web3, tokenAmount]) | ||||||
|
|
||||||
| useEffect(() => { | ||||||
| if (tradeState.type === TransactionStateType.UNKNOWN) return | ||||||
| setTransactionDialog({ | ||||||
| open: true, | ||||||
| state: tradeState, | ||||||
| shareText: [ | ||||||
| `I just deposit ${inputAmount} ${protocol.bareToken.symbol} with ${ProtocolName[protocol.type]}. ${ | ||||||
| isTwitter(activatedSocialNetworkUI) || isFacebook(activatedSocialNetworkUI) | ||||||
| ? `Follow @${ | ||||||
| isTwitter(activatedSocialNetworkUI) ? t('twitter_account') : t('facebook_account') | ||||||
| } (mask.io) to deposit.` | ||||||
| : '' | ||||||
| }`, | ||||||
| '#mask_io', | ||||||
| ].join('\n'), | ||||||
| }) | ||||||
| }, [tradeState]) | ||||||
|
|
||||||
| const needsSwap = protocol.type === ProtocolType.Lido && tab === TabType.Withdraw | ||||||
|
|
||||||
| const buttonDom = useMemo(() => { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,7 +72,7 @@ export function SearchableList<T extends {}>({ | |
| shouldSort: true, | ||
| threshold: 0.45, | ||
| minMatchCharLength: 1, | ||
| keys: searchKey ?? data.length > 0 ? Object.keys(data[0]) : [], | ||
| keys: searchKey ?? Object.keys(data.length > 0 ? data[0] : []), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, they are different, right?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, they will cause #6073
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, it's about the operator precedence. |
||
| }), | ||
| [data, searchKey], | ||
| ) | ||
|
|
||
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.
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.
Recommend to use
createLookupTableResolver, you will never lost newProtocolTypes.