From 3e8018adccab29eb9abc210d3048c7e939b9eeca Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 28 Jun 2023 23:00:12 +0800 Subject: [PATCH 1/2] chore: update network --- .../examples/extension-example/src/App.tsx | 71 +++++++++++++------ 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/manta-js/examples/extension-example/src/App.tsx b/manta-js/examples/extension-example/src/App.tsx index 0cf15e9..65982a3 100644 --- a/manta-js/examples/extension-example/src/App.tsx +++ b/manta-js/examples/extension-example/src/App.tsx @@ -2,27 +2,32 @@ import { ApiPromise, HttpProvider, WsProvider } from '@polkadot/api'; import BigNumber from 'bignumber.js'; import { useCallback, useEffect, useMemo, useState } from 'react'; -import { Injected, InjectedWeb3, PrivateWalletStateInfo } from './interfaces'; +import { + Injected, + InjectedWeb3, + PrivateWalletStateInfo, + Network, +} from './interfaces'; const networkConfigs: Record< string, { rpc: string | string[]; assetName: string; decimals: number } > = { Manta: { - rpc: ['https://c1.manta.seabird.systems/rpc'], + rpc: ['https://ws.manta.systems'], assetName: 'MANTA', - decimals: 18 - }, - Dolphin: { - rpc: ['https://calamari.seabird.systems/rpc'], - assetName: 'DOL', - decimals: 12, + decimals: 18, }, Calamari: { - rpc: ['https://calamari.systems/rpc'], + rpc: ['https://ws.calamari.systems'], assetName: 'KMA', decimals: 12, }, + Dolphin: { + rpc: ['https://ws.calamari.seabird.systems'], + assetName: 'DOL', + decimals: 12, + }, }; const assetId = '1'; @@ -37,7 +42,7 @@ let isConnecting = false; export default function App() { const [isInjected] = useState(!!injectedWeb3); const [injected, setInjected] = useState(null); - const [network, setNetwork] = useState(''); + const [network, setNetwork] = useState(undefined); const [api, setApi] = useState(null); const [publicAddress, setPublicAddress] = useState(null); const [zkAddress, setZkAddress] = useState(null); @@ -67,6 +72,9 @@ export default function App() { }, [setInjected]); const currentNetwork = useMemo(() => { + if (!network) { + return null; + } return networkConfigs[network]; }, [network]); @@ -75,7 +83,7 @@ export default function App() { }, [injected]); const fetchBalance = useCallback(async () => { - if (!stateInfo?.isWalletReady) { + if (!stateInfo?.isWalletReady || !network || !currentNetwork) { return null; } const balance = await injected?.privateWallet.getZkBalance({ @@ -88,10 +96,10 @@ export default function App() { return new BigNumber(balance) .div(new BigNumber(10).pow(currentNetwork.decimals)) .toFixed(); - }, [injected, network]); + }, [injected, network, stateInfo?.isWalletReady]); const fetchPublicBalance = useCallback(async () => { - if (!api) { + if (!api || !currentNetwork) { return; } const accountInfo = await api.query.system.account(publicAddress); @@ -145,6 +153,9 @@ export default function App() { setResult('publicAddress is empty'); return; } + if (!network) { + return; + } try { setOperating(true); setResult('Signing'); @@ -200,6 +211,9 @@ export default function App() { ); const toPrivateTransaction = useCallback(async () => { + if (!currentNetwork || !network) { + return; + } sendTransaction(async () => { const response = await injected?.privateWallet.toPrivateBuild({ assetId, @@ -222,6 +236,9 @@ export default function App() { ]); const privateTransferTransaction = useCallback(async () => { + if (!currentNetwork || !network) { + return; + } sendTransaction(async () => { const response = await injected?.privateWallet.privateTransferBuild({ assetId, @@ -246,6 +263,9 @@ export default function App() { ]); const toPublicTransaction = useCallback(async () => { + if (!currentNetwork || !network) { + return; + } sendTransaction(async () => { const response = await injected?.privateWallet.toPublicBuild({ assetId, @@ -268,6 +288,9 @@ export default function App() { ]); const multiSbtPostBuildTransition = useCallback(async () => { + if (!network) { + return; + } sendTransaction(async () => { return injected?.privateWallet.multiSbtPostBuild({ sbtInfoList: [ @@ -280,6 +303,9 @@ export default function App() { }, [startAssetId, injected, sendTransaction, network]); const getSbtIdentityProof = useCallback(async () => { + if (!network) { + return; + } await sendTransaction(async () => { return injected?.privateWallet.getSbtIdentityProof({ virtualAsset, @@ -338,7 +364,12 @@ export default function App() { : currentNetwork.rpc; const provider = new HttpProvider(rpcUrl); // const provider = new WsProvider(rpcUrl); - setApi(null); + setApi((api) => { + if (api) { + api.disconnect(); + } + return null; + }); const api = await ApiPromise.create({ provider }); console.log(`[connected rpcUrl]: ${rpcUrl}`); api.setSigner(injected.signer); @@ -353,7 +384,7 @@ export default function App() { return; } updateBalance(); - }, [updateBalance, api]); + }, [updateBalance, api, stateInfo?.isWalletReady]); // auto connect wallet useEffect(() => { @@ -401,10 +432,10 @@ export default function App() {
Balance

- {currentNetwork.assetName}: {publicBalance} + {currentNetwork?.assetName}: {publicBalance}

- zk{currentNetwork.assetName}: {balance} + zk{currentNetwork?.assetName}: {balance}

@@ -417,7 +448,7 @@ export default function App() { setToPrivateAmount(e.target.value); }} /> - {currentNetwork.assetName} + {currentNetwork?.assetName}
- Wallet State + Extension State +

+ isActive: {String(isExtensionActive)} +

+
+
+ Private Wallet State {stateInfo && Object.keys(stateInfo).map((key: string) => (

@@ -431,6 +445,9 @@ export default function App() {

Balance +

+ Network: {network} +

{currentNetwork?.assetName}: {publicBalance}

diff --git a/manta-js/examples/extension-example/src/interfaces.ts b/manta-js/examples/extension-example/src/interfaces.ts index 77f42d5..f692a0f 100644 --- a/manta-js/examples/extension-example/src/interfaces.ts +++ b/manta-js/examples/extension-example/src/interfaces.ts @@ -14,6 +14,7 @@ declare global { export interface Injected extends PolkadotInjected { privateWallet: InjectedPrivateWallet; + on(eventName: string | symbol, listener: (...args: any[]) => void): this; } export type InjectedWeb3 = {