From 01096a494cb9900a83cac7a40010788812a7bbfd Mon Sep 17 00:00:00 2001 From: Eason Smith Date: Tue, 3 Dec 2024 10:24:52 +0800 Subject: [PATCH] test keplr ethereum provider --- pages/ethers/index.tsx | 83 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 8 deletions(-) diff --git a/pages/ethers/index.tsx b/pages/ethers/index.tsx index 23b1d96..8262dd5 100644 --- a/pages/ethers/index.tsx +++ b/pages/ethers/index.tsx @@ -1,7 +1,12 @@ import { Box, Button, Text } from "@interchain-ui/react"; import { ethers } from "ethers"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import abi from './abi.json' +import { IEthereumProvider } from "@keplr-wallet/types"; +import { MetaMaskInpageProvider } from "@metamask/providers"; + +type EthereumProvider = MetaMaskInpageProvider | IEthereumProvider | undefined +type EthereumProviderType = 'keplr' | 'metamask' export default function Index() { const verifyingContract = '0xf67a42D581eB7d83135De8Dfe2fCccE58e5259bc' @@ -9,31 +14,42 @@ export default function Index() { const [balance, setBalance] = useState('--') const [result, setResult] = useState(null) + const [ethereum, setEthereum] = useState() + const [selectedWallet, setSelectedWallet] = useState('metamask') + + useEffect(()=>{ + let ethereum:EthereumProvider = window.ethereum + if (selectedWallet === 'keplr') { + ethereum = window.keplr?.ethereum + } + setEthereum(ethereum) + }, [selectedWallet]) const send = async () => { setResult(null) - if (!window.ethereum) { + if (!ethereum) { alert('Please install MetaMask') return } - const provider = new ethers.BrowserProvider(window.ethereum) + const provider = new ethers.BrowserProvider(ethereum) const wallet = await provider.getSigner() // wallet.signTypedData() const tx = await wallet.sendTransaction({ to: addr0, value: '1' }) + console.log('tx', tx) const res = await tx.wait(); setResult(res) getBalance() } const getBalance = async () => { - if (!window.ethereum) { + if (!ethereum) { alert('Please install MetaMask') return } - const provider = new ethers.BrowserProvider(window.ethereum) + const provider = new ethers.BrowserProvider(ethereum) const wallet = await provider.getSigner() const address = await wallet.getAddress() console.log('address', address) @@ -42,13 +58,13 @@ export default function Index() { } const signTypedDataTest = async () => { - if (!window.ethereum) { + if (!ethereum) { alert('Please install MetaMask') return } try { - const provider = new ethers.BrowserProvider(window.ethereum); + const provider = new ethers.BrowserProvider(ethereum); await provider.send("eth_requestAccounts", []); const wallet = await provider.getSigner(); @@ -96,13 +112,64 @@ export default function Index() { } } - return + async function addEthereumChain() { + if (ethereum) { + try { + await ethereum.request({ + method: "wallet_addEthereumChain", + params: [ + { + chainId: '0x61', + chainName: "BSC Test", + nativeCurrency: { + name: "tBNB", + symbol: "tBNB", + decimals: 18, + }, + rpcUrls: ["https://data-seed-prebsc-1-s1.binance.org:8545"], + blockExplorerUrls: ["https://testnet.bscscan.com"] + }, + ], + }); + console.log("added"); + } catch (error) { + console.error("error wallet_addEthereumChain", error); + } + } else { + console.error("metamask not installed"); + } + } + + return + + + + Balance: {balance} + {result && {JSON.stringify(result, null, 2)}} } \ No newline at end of file