Skip to content
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 @@ -181,7 +181,7 @@ const ContractInteraction = memo(() => {
for (const method of methods) {
const parameters = method.parameters

if (method.name === 'approve' && parameters?.value) {
if (method.name === 'approve' || method.name === 'setApprovalForAll') {
return {
isNativeTokenInteraction: false,
typeName: request.formatterTransaction?.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function ConsoleContent(props: ConsoleContentProps) {
const { value: balance = '0' } = useBalance()
const { value: blockNumber = 0 } = useBlockNumber()
const { value: blockTimestamp = 0 } = useBlockTimestamp()

const onTransferCallback = useCallback(() => {
if (!NATIVE_TOKEN_ADDRESS) return
return connection.transferFungibleToken(
Expand All @@ -70,6 +71,26 @@ export function ConsoleContent(props: ConsoleContentProps) {
)
}, [connection])

const onApproveFungibleTokenCallback = useCallback(() => {
if (pluginID !== NetworkPluginID.PLUGIN_EVM) return
if (chainId !== ChainId.Mainnet) return
return connection.approveFungibleToken(
'0x6B175474E89094C44Da98b954EedeAC495271d0F',
'0x31f42841c2db5173425b5223809cf3a38fede360',
'1',
)
}, [pluginID, connection])

const onApproveNonFungibleTokenCallback = useCallback(() => {
if (pluginID !== NetworkPluginID.PLUGIN_EVM) return
if (chainId !== ChainId.Mainnet) return
return connection.approveNonFungibleToken(
'0xd945f759d422ae30a6166838317b937de08380e3',
'0x31f42841c2db5173425b5223809cf3a38fede360',
'71050',
)
}, [pluginID, connection])

const onSignMessage = useCallback(
async (type?: string) => {
const message = 'Hello World'
Expand Down Expand Up @@ -210,6 +231,30 @@ export function ConsoleContent(props: ConsoleContentProps) {
</Button>
</TableCell>
</TableRow>
<TableRow>
<TableCell>
<Typography variant="body2" whiteSpace="nowrap">
Approve Fungible Token
</Typography>
</TableCell>
<TableCell>
<Button size="small" onClick={onApproveFungibleTokenCallback}>
Approve
</Button>
</TableCell>
</TableRow>
<TableRow>
<TableCell>
<Typography variant="body2" whiteSpace="nowrap">
Approve Non-Fungible Token
</Typography>
</TableCell>
<TableCell>
<Button size="small" onClick={onApproveNonFungibleTokenCallback}>
Approve
</Button>
</TableCell>
</TableRow>
<TableRow>
<TableCell>
<Typography variant="body2" whiteSpace="nowrap">
Expand Down
34 changes: 19 additions & 15 deletions packages/plugins/EVM/src/state/Connection/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,26 +337,30 @@ class Connection implements EVM_Connection {
const ERC721_ENUMERABLE_INTERFACE_ID = '0x780e9d63'
const ERC1155_ENUMERABLE_INTERFACE_ID = '0xd9b67a26'

const erc165Contract = await this.getWeb3Contract<ERC165>(address, ERC165ABI as AbiItem[], options)
try {
const erc165Contract = await this.getWeb3Contract<ERC165>(address, ERC165ABI as AbiItem[], options)

const isERC165 = await erc165Contract?.methods
.supportsInterface(ERC165_INTERFACE_ID)
.call({ from: options.account })
const isERC165 = await erc165Contract?.methods
.supportsInterface(ERC165_INTERFACE_ID)
.call({ from: options.account })

const isERC721 = await erc165Contract?.methods
.supportsInterface(ERC721_ENUMERABLE_INTERFACE_ID)
.call({ from: options.account })
if (isERC165 && isERC721) return SchemaType.ERC721
const isERC721 = await erc165Contract?.methods
.supportsInterface(ERC721_ENUMERABLE_INTERFACE_ID)
.call({ from: options.account })
if (isERC165 && isERC721) return SchemaType.ERC721

const isERC1155 = await erc165Contract?.methods
.supportsInterface(ERC1155_ENUMERABLE_INTERFACE_ID)
.call({ from: options.account })
if (isERC165 && isERC1155) return SchemaType.ERC1155
const isERC1155 = await erc165Contract?.methods
.supportsInterface(ERC1155_ENUMERABLE_INTERFACE_ID)
.call({ from: options.account })
if (isERC165 && isERC1155) return SchemaType.ERC1155

const isERC20 = (await this.getCode(address, options)) !== '0x'
if (isERC20) return SchemaType.ERC20
const isERC20 = (await this.getCode(address, options)) !== '0x'
if (isERC20) return SchemaType.ERC20

return
return
} catch {
return
}
}
async getNonFungibleToken(
address: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class ERC20Descriptor implements TransactionDescriptor {
})
for (const method of context.methods) {
const parameters = method.parameters

switch (method.name) {
case 'approve':
if (parameters?.spender === undefined || parameters?.value === undefined) break
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
import type { TransactionContext } from '@masknet/web3-shared-base'
import type { ChainId, TransactionParameter } from '@masknet/web3-shared-evm'
import { Web3StateSettings } from '../../../settings'
import type { TransactionDescriptor } from '../types'

export class ERC721Descriptor implements TransactionDescriptor {
async getContractSymbol(chainId: ChainId, address: string) {
const connection = await Web3StateSettings.value.Connection?.getConnection?.({
chainId: chainId,
})
const contract = await connection?.getNonFungibleTokenContract(address)
return contract?.symbol
}

async compute(context: TransactionContext<ChainId, TransactionParameter>) {
if (!context.methods?.length) return

for (const method of context.methods) {
const parameters = method.parameters

switch (method.name) {
case 'setApprovalForAll':
case 'approve': {
if (parameters?.to === undefined || parameters?.tokenId === undefined) break

const symbol = await this.getContractSymbol(context.chainId, context.to)

return {
chainId: context.chainId,
title: `Unlock ${symbol ?? 'token'} contract`,
description: `Unlock ${symbol ?? 'token'} contract`,
successfulDescription: `${symbol ?? 'token'} is unlocked successfully.`,
}
}
case 'setApprovalForAll': {
if (parameters?.operator === undefined || parameters?.approved === undefined) break

const action = parameters?.approved === false ? 'Revoke' : 'Unlock'
const symbol = await this.getContractSymbol(context.chainId, context.to)

return {
chainId: context.chainId,
title: parameters?.approved === false ? 'Revoke' : 'Unlock',
description: `${
parameters?.approved === false ? 'Revoke the approval for' : 'Unlock'
} the token.`,
successfulDescription: 'Revoke the approval successfully.',
title: `${action} ${symbol ?? 'token'} contract`,
description: `${action} ${symbol ?? 'token'} contract`,
successfulDescription: `${action} the approval successfully.`,
}
}

default:
return
Expand Down