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

fix: erc20 allowance comparison #11033

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ export function RedPacketERC20Form(props: RedPacketFormProps) {
actualPluginID={pluginID}>
<EthereumERC20TokenApprovedBoundary
amount={totalAmount.toFixed()}
balance={balance}
classes={{ container: classes.unlockContainer }}
ActionButtonProps={{
size: 'medium',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useCallback } from 'react'
import React, { useCallback, useMemo } from 'react'
import { unreachable } from '@masknet/kit'
import { TokenIcon } from '@masknet/shared'
import { NetworkPluginID } from '@masknet/shared-base'
import { ActionButton, ShadowRootTooltip, makeStyles, type ActionButtonProps } from '@masknet/theme'
import { useChainContext, useFungibleTokenSpenders } from '@masknet/web3-hooks-base'
import { useChainContext, useFungibleTokenBalance, useFungibleTokenSpenders } from '@masknet/web3-hooks-base'
import { ApproveStateType, useERC20TokenApproveCallback } from '@masknet/web3-hooks-evm'
import { isGte, isSameAddress, type FungibleToken } from '@masknet/web3-shared-base'
import { isGte, isSameAddress, type FungibleToken, rightShift } from '@masknet/web3-shared-base'
import type { ChainId, SchemaType } from '@masknet/web3-shared-evm'
import { HelpOutline } from '@mui/icons-material'
import { useSharedTrans } from '../../../locales/index.js'
Expand All @@ -29,6 +29,7 @@ const useStyles = makeStyles<void, 'icon'>()((theme, _, refs) => ({

export interface EthereumERC20TokenApprovedBoundaryProps extends withClasses<'button' | 'container'> {
amount: string
balance?: string
spender?: string
token?: FungibleToken<ChainId, SchemaType>
fallback?: React.ReactNode
Expand All @@ -45,6 +46,7 @@ export function EthereumERC20TokenApprovedBoundary(props: EthereumERC20TokenAppr
const {
children = null,
amount,
balance,
spender,
token,
infiniteUnlockContent,
Expand All @@ -57,6 +59,9 @@ export function EthereumERC20TokenApprovedBoundary(props: EthereumERC20TokenAppr
const t = useSharedTrans()
const { classes } = useStyles(undefined, { props })
const { account, chainId } = useChainContext<NetworkPluginID.PLUGIN_EVM>({ chainId: token?.chainId })
const { data: tokenBalance } = useFungibleTokenBalance(NetworkPluginID.PLUGIN_EVM, token?.address, {
chainId,
})

const {
data: spenders,
Expand All @@ -68,20 +73,28 @@ export function EthereumERC20TokenApprovedBoundary(props: EthereumERC20TokenAppr
account,
})

const approveAmount = balance || tokenBalance || amount
const [{ type: approveStateType, allowance }, transactionState, approveCallback] = useERC20TokenApproveCallback(
token?.address ?? '',
amount,
approveAmount,
spender ?? '',
() => {
callback?.()
refetch()
},
token?.chainId,
)

const approved =
isGte(allowance, amount) ||
spenders?.some((x) => isSameAddress(x.tokenInfo.address, token?.address) && isSameAddress(x.address, spender))
const approved = useMemo(() => {
if (isGte(allowance, amount)) return true
if (!token?.address || !spenders?.length) return false
return spenders.some((x) => {
return (
isSameAddress(x.tokenInfo.address, token.address) &&
isSameAddress(x.address, spender) &&
isGte(rightShift(x.amount || 0, x.tokenInfo.decimals || token.decimals), amount)
)
})
}, [allowance, amount, spenders, token?.address, token?.decimals, spender])

const loading =
spendersLoading ||
Expand Down
4 changes: 3 additions & 1 deletion packages/web3-shared/base/src/specs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,9 @@ export interface NonFungibleAsset<ChainId, SchemaType> extends NonFungibleToken<
* Authorization about a fungible token.
*/
export interface FungibleTokenSpender<ChainId, SchemaType> {
tokenInfo: Pick<FungibleToken<ChainId, SchemaType>, 'address' | 'logoURL' | 'symbol' | 'name'>
tokenInfo: Pick<FungibleToken<ChainId, SchemaType>, 'address' | 'logoURL' | 'symbol' | 'name'> & {
decimals?: number
}
/** spender address */
address: string
/** spender name */
Expand Down
52 changes: 26 additions & 26 deletions packages/web3-shared/evm/src/constants/chains.json
Original file line number Diff line number Diff line change
Expand Up @@ -413,25 +413,27 @@
]
},
{
"chainId": 250,
"type": "Fantom",
"name": "Fantom",
"fullName": "Fantom Opera",
"chainId": 534352,
"type": "Scroll",
"name": "Scroll",
"network": "mainnet",
"shortName": "ftm",
"fullName": "Scroll",
"shortName": "Scroll",
"rpc": ["https://rpc.scroll.io"],
"faucets": [],
"features": [],
"nativeCurrency": {
"chainId": 250,
"name": "Fantom",
"symbol": "FTM",
"chainId": 534352,
"name": "Ether",
"symbol": "ETH",
"decimals": 18,
"logoURL": "https://imagedelivery.net/PCnTHRkdRhGodr0AWBAvMA/Assets/blockchains/fantom/info/logo.png/quality=85"
"logoURL": "https://imagedelivery.net/PCnTHRkdRhGodr0AWBAvMA/Assets/blockchains/ethereum/info/logo.png/quality=85"
},
"infoURL": "https://fantom.foundation",
"infoURL": "https://scrollscan.com/",
"explorers": [
{
"name": "ftmscan",
"url": "https://ftmscan.com",
"name": "blockscout",
"url": "https://scrollscan.com/",
"standard": "EIP3091"
}
]
Expand Down Expand Up @@ -649,27 +651,25 @@
]
},
{
"chainId": 534352,
"type": "Scroll",
"name": "Scroll",
"chainId": 250,
"type": "Fantom",
"name": "Fantom",
"fullName": "Fantom Opera",
"network": "mainnet",
"fullName": "Scroll",
"shortName": "Scroll",
"rpc": ["https://rpc.scroll.io"],
"faucets": [],
"shortName": "ftm",
"features": [],
"nativeCurrency": {
"chainId": 534352,
"name": "Ether",
"symbol": "ETH",
"chainId": 250,
"name": "Fantom",
"symbol": "FTM",
"decimals": 18,
"logoURL": "https://imagedelivery.net/PCnTHRkdRhGodr0AWBAvMA/Assets/blockchains/ethereum/info/logo.png/quality=85"
"logoURL": "https://imagedelivery.net/PCnTHRkdRhGodr0AWBAvMA/Assets/blockchains/fantom/info/logo.png/quality=85"
},
"infoURL": "https://scrollscan.com/",
"infoURL": "https://fantom.foundation",
"explorers": [
{
"name": "blockscout",
"url": "https://scrollscan.com/",
"name": "ftmscan",
"url": "https://ftmscan.com",
"standard": "EIP3091"
}
]
Expand Down
34 changes: 17 additions & 17 deletions packages/web3-shared/evm/src/constants/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,16 @@ export const NETWORK_DESCRIPTORS: ReadonlyArray<NetworkDescriptor<ChainId, Netwo
isMainnet: false,
},
{
ID: `${PLUGIN_ID}_fantom`,
ID: `${PLUGIN_ID}_scroll`,
networkSupporterPluginID: PLUGIN_ID,
chainId: ChainId.Fantom,
type: NetworkType.Fantom,
name: 'Fantom',
icon: new URL('../assets/fantom.png', import.meta.url).href,
iconColor: 'rgb(73, 169, 166)',
averageBlockDelay: 10,
backgroundGradient: 'linear-gradient(180deg, rgba(24, 94, 255, 0.15) 0%, rgba(24, 94, 255, 0.05) 100%)',
chainId: ChainId.Scroll,
type: NetworkType.Scroll,
name: 'Scroll',
icon: new URL('../assets/scroll.svg', import.meta.url).href,
backgroundGradient:
'linear-gradient(180deg, rgba(98, 126, 234, 0.15) 0%, rgba(98, 126, 234, 0.05) 100%), rgba(255, 255, 255, 0.2)',
iconColor: 'rgb(255, 248, 243)',
averageBlockDelay: 9,
isMainnet: true,
},
{
Expand Down Expand Up @@ -256,16 +257,15 @@ export const NETWORK_DESCRIPTORS: ReadonlyArray<NetworkDescriptor<ChainId, Netwo
isMainnet: true,
},
{
ID: `${PLUGIN_ID}_scroll`,
ID: `${PLUGIN_ID}_fantom`,
networkSupporterPluginID: PLUGIN_ID,
chainId: ChainId.Scroll,
type: NetworkType.Scroll,
name: 'Scroll',
icon: new URL('../assets/scroll.svg', import.meta.url).href,
backgroundGradient:
'linear-gradient(180deg, rgba(98, 126, 234, 0.15) 0%, rgba(98, 126, 234, 0.05) 100%), rgba(255, 255, 255, 0.2)',
iconColor: 'rgb(255, 248, 243)',
averageBlockDelay: 9,
chainId: ChainId.Fantom,
type: NetworkType.Fantom,
name: 'Fantom',
icon: new URL('../assets/fantom.png', import.meta.url).href,
iconColor: 'rgb(73, 169, 166)',
averageBlockDelay: 10,
backgroundGradient: 'linear-gradient(180deg, rgba(24, 94, 255, 0.15) 0%, rgba(24, 94, 255, 0.05) 100%)',
isMainnet: true,
},
{
Expand Down