Skip to content

Commit

Permalink
refactor: erc1155
Browse files Browse the repository at this point in the history
  • Loading branch information
guanbinrui committed Jun 10, 2022
1 parent ed5defd commit 55c1327
Show file tree
Hide file tree
Showing 35 changed files with 900 additions and 274 deletions.
2 changes: 2 additions & 0 deletions packages/mask/src/plugin-infra/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export function createSharedContext(pluginID: string, signal: AbortSignal): Plug

send: WalletRPC.sendPayload,

fetch: Services.Helper.r2d2Fetch,

openPopupWindow: Services.Helper.openPopupWindow,
closePopupWindow: Services.Helper.removePopupWindow,

Expand Down
6 changes: 4 additions & 2 deletions packages/mask/src/plugins/Avatar/Application/NFTListPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useImageChecker } from '@masknet/shared'
import { makeStyles } from '@masknet/theme'
import type { NonFungibleToken } from '@masknet/web3-shared-base'
import { createNonFungibleToken, NonFungibleToken } from '@masknet/web3-shared-base'
import { ChainId, createERC721Token, SchemaType } from '@masknet/web3-shared-evm'
import { Box, Skeleton } from '@mui/material'
import { useState } from 'react'
Expand Down Expand Up @@ -54,7 +54,9 @@ export function NFTListPage(props: NFTListPageProps) {
const { classes } = useStyles()
const { onSelect, chainId, tokenInfo, tokens, children } = props
const [selectedToken, setSelectedToken] = useState<NonFungibleToken<ChainId, SchemaType> | undefined>(
tokenInfo ? createERC721Token(chainId, tokenInfo.address, tokenInfo.tokenId) : undefined,
tokenInfo
? createNonFungibleToken(chainId, tokenInfo.address, SchemaType.ERC721, tokenInfo.tokenId)
: undefined,
)

const onChange = (token: NonFungibleToken<ChainId, SchemaType>) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/mask/src/plugins/Avatar/SNSAdaptor/AddNFT.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ export function AddNFT(props: AddNFTProps) {
return
}

const token = await connection?.getNonFungibleToken(address, tokenId, { chainId, account })
const token = await connection?.getNonFungibleToken(address, tokenId, undefined, { chainId, account })
if (token) {
if (chainId && token && token.contract?.chainId !== chainId) {
setMessage('chain does not match.')
return
}
if (!token || !isSameAddress(token?.metadata?.owner, account ?? _account)) {
if (!token || !isSameAddress(token?.contract?.owner, account ?? _account)) {
setMessage(t('nft_owner_hint'))
return
}
Expand Down
8 changes: 4 additions & 4 deletions packages/mask/src/plugins/Avatar/hooks/useTokenOwner.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { useAsyncRetry } from 'react-use'
import type { ChainId } from '@masknet/web3-shared-evm'
import { isSameAddress, NetworkPluginID } from '@masknet/web3-shared-base'
import { useWeb3Connection, useAccount } from '@masknet/plugin-infra/web3'
import { activatedSocialNetworkUI } from '../../../social-network'
import { PluginNFTAvatarRPC } from '../messages'
import { usePersonas } from './usePersonas'
import { useWeb3Connection, useAccount } from '@masknet/plugin-infra/web3'
import type { ChainId } from '@masknet/web3-shared-evm'

export function useTokenOwner(address: string, tokenId: string, chainId?: ChainId) {
const connection = useWeb3Connection(NetworkPluginID.PLUGIN_EVM, { chainId })
const account = useAccount(NetworkPluginID.PLUGIN_EVM)
return useAsyncRetry(async () => {
if (!address || !tokenId || !connection) return
const token = await connection.getNonFungibleToken(address, tokenId, { chainId, account })
return { owner: token?.metadata?.owner, name: token?.contract?.name, symbol: token?.contract?.symbol }
const token = await connection.getNonFungibleToken(address, tokenId, undefined, { chainId, account })
return { owner: token?.contract?.owner, name: token?.contract?.name, symbol: token?.contract?.symbol }
}, [connection, tokenId, address, account, chainId])
}

Expand Down
11 changes: 10 additions & 1 deletion packages/mask/src/plugins/MaskBox/hooks/useContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { omit, clamp, first, uniq } from 'lodash-unified'
import BigNumber from 'bignumber.js'
import { createContainer } from 'unstated-next'
import { unreachable } from '@dimensiondev/kit'
import { ChainId, useTokenConstants, useMaskBoxConstants, ZERO_ADDRESS, isZeroAddress } from '@masknet/web3-shared-evm'
import {
ChainId,
useTokenConstants,
useMaskBoxConstants,
ZERO_ADDRESS,
isZeroAddress,
SchemaType,
} from '@masknet/web3-shared-evm'
import type { NonPayableTx } from '@masknet/web3-contracts/types/types'
import { BoxInfo, BoxState } from '../type'
import { useMaskBoxInfo } from './useMaskBoxInfo'
Expand Down Expand Up @@ -239,6 +246,8 @@ function useContext(initialState?: { boxId: string; hashRoot: string }) {
const { value: contractDetailed } = useNonFungibleTokenContract(
NetworkPluginID.PLUGIN_EVM,
maskBoxInfo?.nft_address ?? '',
undefined,
SchemaType.ERC721,
{ account },
)
// #endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ export const NftRedPacketHistoryItem: FC<NftRedPacketHistoryItemProps> = memo(
const { value: contractDetailed } = useNonFungibleTokenContract(
NetworkPluginID.PLUGIN_EVM,
history.token_contract.address,
undefined,
undefined,
{ account },
)
const { closeDialog: closeApplicationBoardDialog } = useRemoteControlledDialog(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,16 @@ export function SelectNftTokenDialog(props: SelectNftTokenDialogProps) {
// #region fetch token detail
const onSearch = useCallback(async () => {
setLoadingToken(true)
const _tokenDetailed = (await connection?.getNonFungibleToken(contract?.address ?? '', tokenId, {
account,
chainId,
})) as NonFungibleToken<ChainId, SchemaType.ERC721>
setTokenDetailed(_tokenDetailed?.metadata?.owner ? { ..._tokenDetailed, index: 0 } : undefined)
const _tokenDetailed = (await connection?.getNonFungibleToken(
contract?.address ?? '',
tokenId,
SchemaType.ERC721,
{
account,
chainId,
},
)) as NonFungibleToken<ChainId, SchemaType.ERC721>
setTokenDetailed(_tokenDetailed?.contract?.owner ? { ..._tokenDetailed, index: 0 } : undefined)
setSearched(true)
setLoadingToken(false)
}, [connection, contract, tokenId, chainId, account])
Expand All @@ -420,7 +425,7 @@ export function SelectNftTokenDialog(props: SelectNftTokenDialogProps) {
if (tokenDetailedOwnerList.length > 0) setTokenDetailed(undefined)
}, [tokenDetailedOwnerList.length])

const isOwner = isSameAddress(account, tokenDetailed?.metadata?.owner) || tokenDetailedSelectedList.length > 0
const isOwner = isSameAddress(account, tokenDetailed?.contract?.owner) || tokenDetailedSelectedList.length > 0
const isAdded = existTokenDetailedList.map((t) => t.tokenId).includes(tokenDetailed?.tokenId ?? '')
// #endregion

Expand Down
15 changes: 10 additions & 5 deletions packages/mask/src/plugins/Tips/components/AddDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useERC721TokenContract } from '@masknet/plugin-infra/web3-evm'
import { ImageIcon, InjectedDialog, InjectedDialogProps } from '@masknet/shared'
import { makeStyles } from '@masknet/theme'
import { isSameAddress, NetworkPluginID, NonFungibleToken } from '@masknet/web3-shared-base'
import type { ChainId, SchemaType } from '@masknet/web3-shared-evm'
import { ChainId, SchemaType } from '@masknet/web3-shared-evm'
import { Button, DialogContent, FormControl, TextField, Typography } from '@mui/material'
import { FC, useCallback, useEffect, useMemo, useState } from 'react'
import { useAsyncFn } from 'react-use'
Expand Down Expand Up @@ -89,10 +89,15 @@ export const AddDialog: FC<Props> = ({ onAdd, onClose, ...rest }) => {
setMessage(t.tip_add_collectibles_error())
return
}
const erc721TokenDetailed = await web3Connection?.getNonFungibleToken(contractAddress, tokenId, {
chainId,
account,
})
const erc721TokenDetailed = await web3Connection?.getNonFungibleToken(
contractAddress,
tokenId,
SchemaType.ERC721,
{
chainId,
account,
},
)

if (!erc721TokenDetailed) {
setMessage(t.tip_add_collectibles_error())
Expand Down
14 changes: 10 additions & 4 deletions packages/mask/src/plugins/Tips/contexts/Tip/TipTaskProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useChainId, useFungibleToken, useNonFungibleTokenContract, useAccount } from '@masknet/plugin-infra/web3'
import { NetworkPluginID } from '@masknet/web3-shared-base'
import type { GasConfig } from '@masknet/web3-shared-evm'
import { GasConfig, SchemaType } from '@masknet/web3-shared-evm'
import { FC, useContext, useEffect, useMemo, useState } from 'react'
import { useSubscription } from 'use-subscription'
import { getStorage } from '../../storage'
Expand Down Expand Up @@ -29,9 +29,15 @@ export const TipTaskProvider: FC<React.PropsWithChildren<Props>> = ({ children,
const [erc721TokenId, setErc721TokenId] = useState<ContextOptions['erc721TokenId']>(null)
const storedTokens = useSubscription(getStorage().addedTokens.subscription)

const { value: erc721Contract } = useNonFungibleTokenContract(NetworkPluginID.PLUGIN_EVM, erc721Address, {
account,
})
const { value: erc721Contract } = useNonFungibleTokenContract(
NetworkPluginID.PLUGIN_EVM,
erc721Address,
undefined,
SchemaType.ERC721,
{
account,
},
)

useEffect(() => {
setTipType(TipType.Token)
Expand Down
2 changes: 1 addition & 1 deletion packages/mask/src/plugins/Tips/contexts/Tip/useNftTip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function useNftTip(recipient: string, tokenId: string | null, contractAdd
const sendTip = useCallback(async () => {
if (!tokenId || !contractAddress) return
const hash = await transferCallback(tokenId, recipient)
const tokenDetailed = await connection?.getNonFungibleToken(contractAddress ?? '', tokenId, {
const tokenDetailed = await connection?.getNonFungibleToken(contractAddress ?? '', tokenId, undefined, {
chainId,
account,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ function SearchResultBox(props: SearchResultBoxProps) {
const { value: contractDetailed = null, loading } = useNonFungibleTokenContract(
NetworkPluginID.PLUGIN_EVM,
keyword,
undefined,
undefined,
{ account },
)
return (
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-infra/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ export namespace Plugin.Shared {
},
): Promise<JsonRpcResponse>

fetch: (input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>

/** Open popup window */
openPopupWindow(route?: PopupRoutes, params?: Record<string, any>): Promise<void>
/** Close popup window */
Expand Down
21 changes: 13 additions & 8 deletions packages/plugin-infra/src/web3-state/Connection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Subscription } from 'use-subscription'
import type { Connection, ConnectionOptions, ConnectionState as Web3ConnectionState } from '@masknet/web3-shared-base'
import type { Plugin } from '../types'

export class ConnectionState<
ChainId,
Expand Down Expand Up @@ -35,10 +36,14 @@ export class ConnectionState<
>
{
constructor(
private context: Plugin.Shared.SharedContext,
protected createConnection: (
chainId?: ChainId,
account?: string,
providerType?: ProviderType,
context: Plugin.Shared.SharedContext,
options?: {
chainId?: ChainId
account?: string
providerType?: ProviderType
},
) => Connection<
ChainId,
SchemaType,
Expand Down Expand Up @@ -71,10 +76,10 @@ export class ConnectionState<
}

async getConnection(options?: Web3ConnectionOptions) {
return this.createConnection(
options?.chainId ?? this.subscription.chainId?.getCurrentValue(),
options?.account ?? this.subscription.account?.getCurrentValue(),
options?.providerType ?? this.subscription.providerType?.getCurrentValue(),
)
return this.createConnection(this.context, {
chainId: options?.chainId ?? this.subscription.chainId?.getCurrentValue(),
account: options?.account ?? this.subscription.account?.getCurrentValue(),
providerType: options?.providerType ?? this.subscription.providerType?.getCurrentValue(),
})
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ChainId, SchemaType } from '@masknet/web3-shared-evm'
import { useAsyncRetry } from 'react-use'
import { useRef } from 'react'
import { useAsyncRetry } from 'react-use'
import { ChainId, SchemaType } from '@masknet/web3-shared-evm'
import { NonFungibleToken, NetworkPluginID } from '@masknet/web3-shared-base'
import { useWeb3Connection } from '../useWeb3Connection'

Expand Down Expand Up @@ -49,7 +49,7 @@ export function useNonFungibleOwnerTokens(
allListRef.current = (
await Promise.all(
listOfPairs?.map((x) =>
connection.getNonFungibleToken(x[0], x[1], {
connection.getNonFungibleToken(x[0], x[1], SchemaType.ERC721, {
account: ownerAccount,
}),
) ?? [],
Expand Down
9 changes: 5 additions & 4 deletions packages/plugin-infra/src/web3/useNonFungibleToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import { useAccount } from './useAccount'
export function useNonFungibleToken<S extends 'all' | void = void, T extends NetworkPluginID = NetworkPluginID>(
pluginID?: T,
address?: string,
id?: string,
tokenId?: string,
schemaType?: Web3Helper.SchemaTypeScope<S, T>,
options?: Web3Helper.Web3ConnectionOptionsScope<S, T>,
) {
const chainId = useChainId(pluginID, options?.chainId)
const account = useAccount(pluginID, options?.account)
const connection = useWeb3Connection(pluginID, options)

return useAsyncRetry<Web3Helper.NonFungibleTokenScope<S, T> | undefined>(async () => {
if (!address || !id || !connection) return
return connection.getNonFungibleToken(address, id, {
if (!address || !tokenId || !connection) return
return connection.getNonFungibleToken(address, tokenId, schemaType, {
...options,
account: options?.account ?? account,
chainId: options?.chainId ?? chainId,
})
}, [address, id, connection, chainId, account])
}, [address, tokenId, schemaType, connection, chainId, account])
}
8 changes: 5 additions & 3 deletions packages/plugin-infra/src/web3/useNonFungibleTokenContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import { useWeb3Connection } from './useWeb3Connection'
export function useNonFungibleTokenContract<S extends 'all' | void = void, T extends NetworkPluginID = NetworkPluginID>(
pluginID: T,
address: string,
options: Web3Helper.Web3ConnectionOptionsScope<S, T>,
tokenId?: string,
schemaType?: Web3Helper.SchemaTypeScope<S, T>,
options?: Web3Helper.Web3ConnectionOptionsScope<S, T>,
) {
const connection = useWeb3Connection(pluginID, options)

return useAsyncRetry<
NonFungibleTokenContract<Web3Helper.ChainIdScope<S, T>, Web3Helper.SchemaTypeScope<S, T>> | undefined
>(async () => {
if (!connection || !address || !options) return
return connection.getNonFungibleTokenContract?.(address, options)
}, [address, connection, JSON.stringify(options)])
return connection.getNonFungibleTokenContract?.(address, tokenId, schemaType, options)
}, [address, tokenId, schemaType, connection, JSON.stringify(options)])
}
4 changes: 3 additions & 1 deletion packages/plugin-infra/src/web3/useNonFungibleTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export function useNonFungibleTokens<S extends 'all' | void = void, T extends Ne

return useAsyncRetry<Array<Web3Helper.NonFungibleTokenScope<S, T>>>(async () => {
if (!connection) return EMPTY_LIST
return Promise.all(listOfPairs?.map((x) => connection.getNonFungibleToken(x[0], x[1], options)) ?? [])
return Promise.all(
listOfPairs?.map((x) => connection.getNonFungibleToken(x[0], x[1], undefined, options)) ?? [],
)
}, [connection, listOfPairs?.join()])
}
4 changes: 2 additions & 2 deletions packages/plugins/EVM/src/state/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export class Connection extends ConnectionState<
Web3Provider
> {
constructor(
private context: Plugin.Shared.SharedContext,
context: Plugin.Shared.SharedContext,
subscription: {
account?: Subscription<string>
chainId?: Subscription<ChainId>
providerType?: Subscription<ProviderType>
},
) {
super(createConnection, subscription)
super(context, createConnection, subscription)
}
}
Loading

0 comments on commit 55c1327

Please sign in to comment.