import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
const client = createPublicClient({
chain: mainnet,
transport: http(), //http()是公共client,也可以使用 wallet client换成 custom(window.ethereum!)
})
// 获取当前区块高度
const blockNumber = await client.getBlockNumber()
使用WalletConnect 的 web3modal
npm i @cocos-labs/web3modal-ethers5
import { createWeb3Modal, useWeb3Modal } from '@cocos-labs/web3modal-ethers5'
// 1. Get projectId from https://cloud.walletconnect.com
const projectId = 'xxxxxxxxxxxxxxxxxxx'
// 2. Set chains
const mainnet = {
chainId: 1,
name: 'Ethereum',
currency: 'ETH',
explorerUrl: 'https://etherscan.io',
rpcUrl: 'https://rpc.ankr.com/eth'
}
const bnbchain = {
chainId: 56,
name: 'BNB Smart Chain Mainnet',
currency: 'BNB',
explorerUrl: 'https://bscscan.com/',
rpcUrl: 'https://bsc-dataseed4.ninicoin.io'
}
// 3. Create your application's metadata object
const metadata = {
name: 'My Website',
description: 'My Website description',
url: 'https://mywebsite.com', // url must match your domain & subdomain
icons: ['https://avatars.mywebsite.com/']
}
// 4. Create Ethers config
const ethersConfig = defaultConfig({
/*Required*/
metadata,
/*Optional*/
enableEIP6963: true, // true by default
enableInjected: true, // true by default
defaultChainId: 56
})
// 5. Create a AppKit instance
this.evmConnect = createWeb3Modal({
ethersConfig,
chains: [mainnet, bnbchain],
projectId,
enableAnalytics: false // Optional - defaults to your Cloud configuration
})
// 6. Open the modal
this.evmConnect.open({ view: 'Connect' })