From 96f81d81f90c30668ad8febc831e0002129b96ba Mon Sep 17 00:00:00 2001 From: rovast Date: Wed, 16 Aug 2023 14:39:01 +0800 Subject: [PATCH 1/2] Add opbnb net Signed-off-by: rovast --- src/config/chain.ts | 52 +++++++++++++++++++++++++++++++++++--- src/utils/contract/web3.ts | 11 +++++--- src/utils/switch-chain.ts | 43 +++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 src/utils/switch-chain.ts diff --git a/src/config/chain.ts b/src/config/chain.ts index 8aa3bff..ae07029 100644 --- a/src/config/chain.ts +++ b/src/config/chain.ts @@ -1,12 +1,20 @@ // 合约相关的配置 -type ChainName = "Optimism Goerli Testnet" | "Sepolia"; +type ChainName = "Optimism Goerli Testnet" | "Sepolia" | "opBNB Testnet"; -type Chain = { +export type Chain = { chainId: string; chainName: ChainName; - chainlistUrl: string; + chainlistUrl?: string; + nativeCurrency?: { + name: string; + symbol: string; + decimals: number; + }; + rpcUrls?: string[]; + blockExplorerUrls?: string[]; + iconUrls?: string[]; oracle: string; @@ -85,11 +93,47 @@ const chains: Chain[] = [ stcMarketContract: "0x6051273DB68F2af68617589f30F8a91f859e5b82", stcMarketTokenContract: "0x8b321Dde4CAe93848f756895fdb34E889A6c831b", + userHubContract: "0xE87a986fDc35170c66C3a2449bC4Aee6350cc1F6" + }, + { + chainId: "0x15eb", + chainName: "opBNB Testnet", + // chainlistUrl: "https://chainlist.org/chain/420", + nativeCurrency: { + name: "tBNB", + symbol: "tBNB", + decimals: 18 + }, + rpcUrls: ["https://opbnb-testnet-rpc.bnbchain.org/"], + blockExplorerUrls: ["https://mainnet.opbnbscan.com/"], + iconUrls: ["https://docs.bnbchain.org/opbnb-docs/img/logo.svg"], + + oracle: "0x141Ee019dE7bBd96441a6287C506B35C9718094f", // + + podContract: "0xed809AF6f37889c7914Bf9f10a210c49EFF9011F", // + podJobId: "780d3dd1933a4a9d839f4c78d92ab595", // + + deploymentContract: "0xCe76ab89Cac76d67Bd43Ed901AE6544b03103a63", // + deploymentJobId: "68c1dc5cd63841459ff2395a931f042c", // + + nodeContract: "0x962E121f6067B0dA6F90D07d565ba21004922B9d", // + nodeJobId: "aa7198c0190f4cc29d4e4470c08f6391", + nodeOracle: "0x248E10ec1C54CB570F7A15933286BAa1D59B70c0", + + zeroTrustContract: "0xd72E21A51594f1Cacf5F1231bA432c392323Fd8c", // + + meshContract: "0x73Cb12189FCAcDE3d742E2ad0ABD2068011F56Ac", // + + codContract: "0x0A873038d29bf16871232f59D589deA2B849C846", // + + stcMarketContract: "0xBd0F68775aA77288Ea2083D7d7776D2D0C3bf0Ee", // + stcMarketTokenContract: "0x5d3c43875589f4881E769f4b46cFf6257dC5Ad1C", // + userHubContract: "0xE87a986fDc35170c66C3a2449bC4Aee6350cc1F6" } ]; -let currentChain: ChainName = "Optimism Goerli Testnet"; +let currentChain: ChainName = "opBNB Testnet"; export function setCurrentChain(name: ChainName) { currentChain = name; } diff --git a/src/utils/contract/web3.ts b/src/utils/contract/web3.ts index b5f58b8..a1871d5 100644 --- a/src/utils/contract/web3.ts +++ b/src/utils/contract/web3.ts @@ -5,6 +5,7 @@ import { generatePrivateKey } from "nostr-tools"; import { storageSession } from "@pureadmin/utils"; import { md5 } from "../shared"; import { getCurrentChain } from "@/config/chain"; +import { switchToChain } from "../switch-chain"; type Instance = { provider: BrowserProvider; @@ -106,10 +107,12 @@ function switchNetworkIfNeed() { window.ethereum.request({ method: "eth_chainId" }).then(currentChainId => { if (currentChainId === targetChainId) return; - window.ethereum.request({ - method: "wallet_switchEthereumChain", - params: [{ chainId: targetChainId }] - }); + switchToChain(getCurrentChain()); + + // window.ethereum.request({ + // method: "wallet_switchEthereumChain", + // params: [{ chainId: targetChainId }] + // }); }); } diff --git a/src/utils/switch-chain.ts b/src/utils/switch-chain.ts new file mode 100644 index 0000000..de08a12 --- /dev/null +++ b/src/utils/switch-chain.ts @@ -0,0 +1,43 @@ +import { chains, type Chain } from "@/config/chain"; + +export const switchToChain = async (chain: Chain) => { + try { + await (window.ethereum as any).request({ + method: "wallet_switchEthereumChain", + params: [{ chainId: chain.chainId }] + }); + console.log("链切换成功"); + } catch (error) { + if (error.code === 4902) { + console.log("链未添加,开始添加链..."); + await addChain(chain); + console.log("链添加成功"); + } else { + console.error("链切换失败:", error); + } + } +}; + +export const addChain = async (chain: Chain) => { + try { + await (window.ethereum as any).request({ + method: "wallet_addEthereumChain", + params: [ + { + chainId: chain.chainId, + chainName: chain.chainName, + nativeCurrency: { + name: chain.nativeCurrency.name, + symbol: chain.nativeCurrency.symbol, + decimals: chain.nativeCurrency.decimals + }, + rpcUrls: chain.rpcUrls, + blockExplorerUrls: chain.blockExplorerUrls, + iconUrls: chain.iconUrls + } + ] + }); + } catch (error) { + console.error("链添加失败:", error); + } +}; From a273507eb17964e92ac83e5b7393bccad6316925 Mon Sep 17 00:00:00 2001 From: rovast Date: Wed, 16 Aug 2023 14:48:55 +0800 Subject: [PATCH 2/2] Update contract Signed-off-by: rovast --- src/config/chain.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/config/chain.ts b/src/config/chain.ts index ae07029..aa9bbc3 100644 --- a/src/config/chain.ts +++ b/src/config/chain.ts @@ -110,10 +110,10 @@ const chains: Chain[] = [ oracle: "0x141Ee019dE7bBd96441a6287C506B35C9718094f", // - podContract: "0xed809AF6f37889c7914Bf9f10a210c49EFF9011F", // + podContract: "0xc07f40758d3bb0f8B8E4C4d146f79265484ba6Fe", // podJobId: "780d3dd1933a4a9d839f4c78d92ab595", // - deploymentContract: "0xCe76ab89Cac76d67Bd43Ed901AE6544b03103a63", // + deploymentContract: "0x67F47fC6cdCcD13bbe6ee88B3E7eb71726225e97", // deploymentJobId: "68c1dc5cd63841459ff2395a931f042c", // nodeContract: "0x962E121f6067B0dA6F90D07d565ba21004922B9d", // @@ -129,7 +129,7 @@ const chains: Chain[] = [ stcMarketContract: "0xBd0F68775aA77288Ea2083D7d7776D2D0C3bf0Ee", // stcMarketTokenContract: "0x5d3c43875589f4881E769f4b46cFf6257dC5Ad1C", // - userHubContract: "0xE87a986fDc35170c66C3a2449bC4Aee6350cc1F6" + userHubContract: "0xD436429Cf172a79A5E4D8F672c698A2E98315dc0" // } ];