Skip to content

Commit

Permalink
fix: wallet issues (#10369)
Browse files Browse the repository at this point in the history
* fix: connect status

* fix: mf-4885 distinguish assets among multiple accounts

* fix: mf-4917 boba token

* fix: mf-4862 transaction handling
  • Loading branch information
UncleBill committed Aug 12, 2023
1 parent d198a9c commit 2559bcd
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export const GasSettingDialog = memo<GasSettingDialogProps>(function GasSettingM
</Box>
<Box>
<Typography className={classes.title}>
{t('popups_wallet_gas_fee_settings_gas_limit')}
{replaceType ? t('nonce') : t('popups_wallet_gas_fee_settings_gas_limit')}
</Typography>
<TextField
value={toFixed(replaceType ? nonce : config.gas, 0)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,27 +302,28 @@ export const RecentActivityItem = memo<RecentActivityItemProps>(function RecentA
) : null}
{recipient}
</Typography>
{/* TODO actions for pending transitions */}
<Box className={classes.operations}>
<button
type="button"
className={cx(classes.button, classes.speedupButton)}
onClick={(e) => {
e.stopPropagation()
onSpeedup?.(transaction)
}}>
{t('speed_up')}
</button>
<button
type="button"
className={cx(classes.button, classes.cancelButton)}
onClick={(e) => {
e.stopPropagation()
onCancel?.(transaction)
}}>
{t('cancel')}
</button>
</Box>
{transaction.status === 1 ? (
<Box className={classes.operations}>
<button
type="button"
className={cx(classes.button, classes.speedupButton)}
onClick={(e) => {
e.stopPropagation()
onSpeedup?.(transaction)
}}>
{t('speed_up')}
</button>
<button
type="button"
className={cx(classes.button, classes.cancelButton)}
onClick={(e) => {
e.stopPropagation()
onCancel?.(transaction)
}}>
{t('cancel')}
</button>
</Box>
) : null}
</Box>
}>
{/* TODO specify cateType */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function useTransactions() {
const networks = useNetworks()

const { Transaction } = useWeb3State()
// TODO invalidQueries after sending transitions
const queries = useQueries({
queries: networks.map((network) => {
return {
Expand All @@ -42,7 +43,7 @@ export function useTransactions() {

// Some are already in debank history
const localeTxes = useMemo(() => {
return allLocaleTxes.filter((tx) => !transactions.find((x) => x.hash === tx.id))
return allLocaleTxes.filter((tx) => !transactions.find((x) => x.id === tx.id))
}, [allLocaleTxes, transactions])

return { ...result, data: transactions, localeTxes }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ export const WalletHeaderUI = memo<WalletHeaderUIProps>(function WalletHeaderUI(
/>
) : null}
</Box>
{isLoading ? (
<ProgressiveText className={classes.connected} loading={isLoading}>
{isLoading ? null : (
<ProgressiveText className={classes.connected} loading={isLoading} skeletonWidth={50}>
<span
className={cx(
classes.dot,
Expand All @@ -181,7 +181,7 @@ export const WalletHeaderUI = memo<WalletHeaderUIProps>(function WalletHeaderUI(
})}
</span>
</ProgressiveText>
) : null}
)}
</Box>
</div>
<div
Expand Down
8 changes: 5 additions & 3 deletions packages/mask/src/extension/popups/pages/Wallet/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Web3 } from '@masknet/web3-providers'
import type { RecentTransaction } from '@masknet/web3-shared-base'
import {
ProviderType,
formatWeiToGwei,
type ChainId,
type Transaction as EvmTransaction,
formatWeiToGwei,
} from '@masknet/web3-shared-evm'
import { mapKeys } from 'lodash-es'
import { toHex } from 'web3-utils'
import { GasSettingModal } from '../../modals/modals.js'
import { ReplaceType } from './type.js'

Expand All @@ -17,7 +19,7 @@ export async function modifyTransaction(
if (!candidate) return
const oldGasSettings = {
gas: candidate.gas!,
gasPrice: candidate.gasPrice,
gasPrice: candidate.gasPrice ? formatWeiToGwei(candidate.gasPrice).toFixed() : undefined,
maxFeePerGas: candidate.maxFeePerGas ? formatWeiToGwei(candidate.maxFeePerGas).toFixed() : undefined,
maxPriorityFeePerGas: candidate.maxPriorityFeePerGas
? formatWeiToGwei(candidate.maxPriorityFeePerGas).toFixed()
Expand All @@ -33,7 +35,7 @@ export async function modifyTransaction(
const newConfig = {
...candidate,
...oldGasSettings,
...gasSettings,
...mapKeys(gasSettings, (value) => (typeof value === 'undefined' ? value : toHex(value))),
}
if (replaceType === ReplaceType.CANCEL) {
await Web3.cancelTransaction(transaction.id, newConfig, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,20 @@ export const AssetsProvider = memo<Props>(function AssetsProvider({ children, bl
const listingMap: Record<string, AssetsState> = { ...assetsMap }
let updated = false
for (const id in assetsMap) {
const originalAssets = assetsMap[id].assets
const storeId = `${account}.${id}`
const originalAssets = assetsMap[storeId].assets
const newAssets = originalAssets.filter((x) => {
const assetId = `${x.chainId}.${x.address}.${x.tokenId}`.toLowerCase()
return !blockedIds.includes(assetId)
})
if (newAssets.length !== originalAssets.length) {
listingMap[id] = { ...listingMap[id], assets: newAssets }
listingMap[storeId] = { ...listingMap[storeId], assets: newAssets }
updated = true
}
}
// Update accordingly
return updated ? listingMap : assetsMap
}, [assetsMap, blockedIds])
}, [assetsMap, account, blockedIds])

const assetsMapRef = useRef<AssetsReducerState['assetsMap']>({})
const listingAssetsMapRef = useRef<AssetsReducerState['assetsMap']>({})
Expand Down Expand Up @@ -120,15 +121,16 @@ export const AssetsProvider = memo<Props>(function AssetsProvider({ children, bl
if (!collection.id) return

const { id, chainId } = collection
const storeId = `${account}.${id}`
const stateKey = `${id}.${chainId}`
const realId = collectionId ?? id
const assetsState = assetsMapRef.current[id]
const assetsState = assetsMapRef.current[storeId]

// Fetch less in collection list, and more every time in expanded collection.
// Also expand size if for id chunk, since there might be more assets than chunk size
const size = assetsState?.assets.length || collectionId ? 20 : 4
const indicator = (!collectionId && indicatorMapRef.current.get(id)) || createIndicator()
dispatch({ type: 'SET_LOADING_STATUS', id: stateKey, loading: true })
dispatch({ type: 'SET_LOADING_STATUS', account, id: stateKey, loading: true })
const pageable = await Hub.getNonFungibleAssetsByCollectionAndOwner(realId, account, {
indicator,
size,
Expand All @@ -144,11 +146,11 @@ export const AssetsProvider = memo<Props>(function AssetsProvider({ children, bl
if (pageable.nextIndicator) {
indicatorMapRef.current.set(id, pageable.nextIndicator as PageIndicator)
}
dispatch({ type: 'APPEND_ASSETS', id: stateKey, assets: pageable.data })
dispatch({ type: 'APPEND_ASSETS', id: stateKey, account, assets: pageable.data })
// If collectionId is set, that means we are loading part of a merged collection.
// And we will let the merged collection's iterator decide if it has ended
const finished = !collectionId && !pageable.nextIndicator
dispatch({ type: 'SET_LOADING_STATUS', id: stateKey, finished, loading: false })
dispatch({ type: 'SET_LOADING_STATUS', id: stateKey, account, finished, loading: false })
return pageable.data
},
[Hub, account],
Expand Down Expand Up @@ -179,7 +181,7 @@ export const AssetsProvider = memo<Props>(function AssetsProvider({ children, bl
assetsLoaderIterators.current.set(stateKey, iterator)
const result = await iterator.next()
if (result.done) {
dispatch({ type: 'SET_LOADING_STATUS', id: stateKey, finished: true, loading: false })
dispatch({ type: 'SET_LOADING_STATUS', id: stateKey, account, finished: true, loading: false })
return
}
assets = result.value
Expand Down Expand Up @@ -207,10 +209,10 @@ export const AssetsProvider = memo<Props>(function AssetsProvider({ children, bl

const getAssets = useCallback(
(collection: Web3Helper.NonFungibleCollectionAll) => {
const key = `${collection.id}.${collection.chainId}`
const key = `${account}.${collection.id}.${collection.chainId}`
return listingAssetsMap[key] ?? createAssetsState()
},
[listingAssetsMap],
[listingAssetsMap, account],
)
const getBLockedTokenIds = useCallback(
(collection: Web3Helper.NonFungibleCollectionAll) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ export const CollectionList = memo(function CollectionList({
const handleInitialRender = useCallback(
(collection: Web3Helper.NonFungibleCollectionAll) => {
const id = collection.id!
const assetsState = assetsMapRef.current[id]
const assetsState = assetsMapRef.current[`${account}.${id}`]
// To reduce requests, check if has been initialized
if (assetsState?.assets.length || assetsState?.loading) return
loadVerifiedBy(id)
loadAssets(collection)
},
[loadAssets, loadVerifiedBy],
[loadAssets, loadVerifiedBy, account],
)

const sidebar = disableSidebar ? null : (
Expand Down
30 changes: 19 additions & 11 deletions packages/shared/src/UI/components/AssetsManagement/assetsReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ export interface AssetsReducerState {
verifiedMap: Record<string, string[]>
}

type Action =
export type AssetsAction =
| {
type: 'SET_LOADING_STATUS'
id: string
account: string
loading?: boolean
finished?: boolean
}
| {
type: 'APPEND_ASSETS'
id: string
account: string
assets: Web3Helper.NonFungibleAssetScope[]
}
| {
Expand All @@ -42,26 +44,32 @@ export function createAssetsState() {

export const initialAssetsState: AssetsReducerState = { assetsMap: {}, verifiedMap: {} }

export function assetsReducer(state: AssetsReducerState, action: Action): AssetsReducerState {
/**
* To distinguish assets among multiple accounts, we combine account and collection id as store-id.
*
*/
export function assetsReducer(state: AssetsReducerState, action: AssetsAction): AssetsReducerState {
switch (action.type) {
case 'SET_LOADING_STATUS':
return produce(state, (draft) => {
const { id, loading, finished } = action
if (!draft.assetsMap[id]) draft.assetsMap[id] = createAssetsState()
const { loading, finished, id, account } = action
const storeId = `${account}.${id}`
if (!draft.assetsMap[storeId]) draft.assetsMap[storeId] = createAssetsState()
if (typeof loading !== 'undefined') {
draft.assetsMap[id].loading = loading
draft.assetsMap[storeId].loading = loading
}
if (typeof finished !== 'undefined') {
draft.assetsMap[id].finished = finished
draft.assetsMap[storeId].finished = finished
}
})
case 'APPEND_ASSETS':
return produce(state, (draft) => {
const { id, assets } = action
if (!draft.assetsMap[id]) draft.assetsMap[id] = createAssetsState()
draft.assetsMap[id].assets = assets.length
? uniqBy([...draft.assetsMap[id].assets, ...assets], (x) => `${x.id}.${x.tokenId}`)
: draft.assetsMap[id].assets ?? EMPTY_LIST
const { id, account, assets } = action
const storeId = `${account}.${id}`
if (!draft.assetsMap[storeId]) draft.assetsMap[storeId] = createAssetsState()
draft.assetsMap[storeId].assets = assets.length
? uniqBy([...draft.assetsMap[storeId].assets, ...assets], (x) => `${x.id}.${x.tokenId}`)
: draft.assetsMap[storeId].assets ?? EMPTY_LIST
})
case 'SET_VERIFIED':
return produce(state, (draft) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/web3-constants/evm/coingecko.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"Aurora": "aurora",
"Aurora_Testnet": "",
"Conflux": "",
"Astar": "astar"
"Astar": "astar",
"Boba": "boba"
},
"COIN_ID": {
"Mainnet": "ethereum",
Expand Down Expand Up @@ -54,6 +55,7 @@
"Harmony": "harmony",
"Moonriver": "moonriver",
"Cronos": "crypto-com-chain",
"BitTorrent": "bittorrent"
"BitTorrent": "bittorrent",
"Boba": "boba-network"
}
}
1 change: 0 additions & 1 deletion packages/web3-hooks/base/src/useFungibleToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export function useFungibleToken<S extends 'all' | void = void, T extends Networ
if (pluginID !== NetworkPluginID.PLUGIN_EVM || !isNativeTokenAddress(address) || !chainId)
return
const network = networks.find((x) => x.chainId === chainId)
console.log('nativeCurrency', network?.nativeCurrency, chainId)
return network?.nativeCurrency
},
async () => {
Expand Down

0 comments on commit 2559bcd

Please sign in to comment.