Skip to content

Commit

Permalink
fix: mf-6133 get token balance on Conflux (#11524) (#11526)
Browse files Browse the repository at this point in the history
Co-authored-by: Wukong Sun <158803171+swkatmask@users.noreply.github.com>
  • Loading branch information
guanbinrui and swkatmask committed Mar 27, 2024
1 parent ecda511 commit 58d57f1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
Expand Up @@ -8,7 +8,7 @@ import {
type RedPacketJSONPayloadFromChain,
} from '@masknet/web3-providers/types'
import { formatBalance, minus, type FungibleToken } from '@masknet/web3-shared-base'
import { type ChainId, type SchemaType } from '@masknet/web3-shared-evm'
import { ChainId, type SchemaType, NETWORK_DESCRIPTORS } from '@masknet/web3-shared-evm'
import { Box, ListItem, Typography, useMediaQuery, type Theme } from '@mui/material'
import { intervalToDuration, nextDay } from 'date-fns'
import { memo, useCallback, useMemo } from 'react'
Expand All @@ -19,6 +19,7 @@ import { useCreateRedPacketReceipt } from './hooks/useCreateRedPacketReceipt.js'
import { useRefundCallback } from './hooks/useRefundCallback.js'
import { dateTimeFormat } from './utils/formatDate.js'

const DEFAULT_BACKGROUND = NETWORK_DESCRIPTORS.find((x) => x.chainId === ChainId.Mainnet)!.backgroundGradient!
const useStyles = makeStyles<{ listItemBackground?: string; listItemBackgroundIcon?: string }>()((
theme,
{ listItemBackground, listItemBackgroundIcon },
Expand Down Expand Up @@ -49,7 +50,7 @@ const useStyles = makeStyles<{ listItemBackground?: string; listItemBackgroundIc
position: 'static !important' as any,
height: 'auto !important',
padding: theme.spacing(1.5),
background: listItemBackground ?? theme.palette.background.default,
background: listItemBackground || DEFAULT_BACKGROUND,
[smallQuery]: {
padding: theme.spacing(2, 1.5),
},
Expand Down Expand Up @@ -179,6 +180,7 @@ interface RedPacketInHistoryListProps {
history: RedPacketJSONPayload | RedPacketJSONPayloadFromChain
onSelect: (payload: RedPacketJSONPayload) => void
}

export const RedPacketInHistoryList = memo(function RedPacketInHistoryList(props: RedPacketInHistoryListProps) {
const { history, onSelect } = props
const t = useRedPacketTrans()
Expand Down
1 change: 1 addition & 0 deletions packages/web3-constants/evm/ethereum.json
Expand Up @@ -18,6 +18,7 @@
"Celo": "0x8e28F1d64ceD52b9A09aB1AA3071Aa3c05802d1F",
"Fantom": "0xc119574d5fb333f5ac018658d4d8b5035e16bf39",
"Aurora": "0xC119574D5Fb333F5AC018658D4d8b5035E16bf39",
"Conflux": "0x93e0b87a0ad0c991dc1b5176ddcd850c9a78aabb",
"Astar": "0xf5056B96ab242C566002852d0b98ce0BcDf1af51",
"Scroll": "0xbC7d98985966f56A66B0cB5F23d865676dc2ac84",
"Metis": "0xC119574D5Fb333F5AC018658D4d8b5035E16bf39",
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-providers/src/DeBank/types.ts
Expand Up @@ -7,12 +7,11 @@ export enum DebankTransactionDirection {
* Collect from https://docs.cloud.debank.com/en/readme/api-pro-reference/chain#returns-1
*/
export type DebankChains =
| 'base'
| 'eth'
| 'arb'
| 'astar'
| 'aurora'
| 'avax'
| 'base'
| 'boba'
| 'brise'
| 'bsc'
Expand All @@ -23,6 +22,7 @@ export type DebankChains =
| 'cro'
| 'dfk'
| 'doge'
| 'eth'
| 'evmos'
| 'ftm'
| 'fuse'
Expand All @@ -44,14 +44,14 @@ export type DebankChains =
| 'pls'
| 'rsk'
| 'sbch'
| 'scrl'
| 'sdn'
| 'sgb'
| 'step'
| 'swm'
| 'tlos'
| 'wan'
| 'xdai'
| 'scrl'

export interface DictItem {
name: string
Expand Down
24 changes: 21 additions & 3 deletions packages/web3-providers/src/Web3/EVM/apis/ConnectionReadonlyAPI.ts
Expand Up @@ -531,17 +531,26 @@ export class EVMConnectionReadonlyAPI

const options = this.ConnectionOptions.fill(initial)
const NATIVE_TOKEN_ADDRESS = getTokenConstant(options.chainId, 'NATIVE_TOKEN_ADDRESS')
const BALANCE_CHECKER_ADDRESS = getEthereumConstant(options.chainId, 'BALANCE_CHECKER_ADDRESS')
const entities: Array<[string, string]> = []

if (listOfAddress.some(isNativeTokenAddress)) {
entities.push([NATIVE_TOKEN_ADDRESS ?? '', await this.getBalance(options.account, options)])
}
const BALANCE_CHECKER_ADDRESS = getEthereumConstant(options.chainId, 'BALANCE_CHECKER_ADDRESS')
if (!BALANCE_CHECKER_ADDRESS) {
if (process.env.NODE_ENV === 'development') {
console.error(
`BALANCE_CHECKER_ADDRESS for chain ${options.chainId} is not provided, do you forget to update packages/web3-constants/evm/ethereum.json ?`,
BALANCE_CHECKER_ADDRESS,
)
}
return Object.fromEntries(entities)
}

const listOfNonNativeAddress = listOfAddress.filter((x) => !isNativeTokenAddress(x))

if (listOfNonNativeAddress.length) {
const contract = this.Contract.getBalanceCheckerContract(BALANCE_CHECKER_ADDRESS ?? '', options)
const contract = this.Contract.getBalanceCheckerContract(BALANCE_CHECKER_ADDRESS, options)
const balances = await contract?.methods.balances([options.account], listOfNonNativeAddress).call({
// cannot check the sender's balance in the same contract
from: undefined,
Expand All @@ -563,7 +572,16 @@ export class EVMConnectionReadonlyAPI

const options = this.ConnectionOptions.fill(initial)
const BALANCE_CHECKER_ADDRESS = getEthereumConstant(options.chainId, 'BALANCE_CHECKER_ADDRESS')
const contract = this.Contract.getBalanceCheckerContract(BALANCE_CHECKER_ADDRESS ?? '', options)
if (!BALANCE_CHECKER_ADDRESS) {
if (process.env.NODE_ENV === 'development') {
console.error(
`BALANCE_CHECKER_ADDRESS for chain ${options.chainId} is not provided, do you forget to update packages/web3-constants/evm/ethereum.json ?`,
BALANCE_CHECKER_ADDRESS,
)
}
return {}
}
const contract = this.Contract.getBalanceCheckerContract(BALANCE_CHECKER_ADDRESS, options)
const result = await contract?.methods.balances([options.account], listOfAddress).call({
// cannot check the sender's balance in the same contract
from: undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/web3-shared/evm/src/constants/descriptors.ts
Expand Up @@ -244,6 +244,7 @@ export const NETWORK_DESCRIPTORS: ReadonlyArray<NetworkDescriptor<ChainId, Netwo
icon: new URL('../assets/conflux.png', import.meta.url).href,
iconColor: 'rgb(112, 212, 74)',
averageBlockDelay: 10,
backgroundGradient: 'linear-gradient(180deg, rgba(72, 168, 166, 0.15) 0%, rgba(72, 168, 166, 0.05) 100%)',
isMainnet: true,
},
{
Expand Down

0 comments on commit 58d57f1

Please sign in to comment.