diff --git a/manta-js/examples/extension-example/src/App.tsx b/manta-js/examples/extension-example/src/App.tsx index 0cf15e9..3b21053 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'; @@ -36,8 +41,9 @@ let isConnecting = false; export default function App() { const [isInjected] = useState(!!injectedWeb3); + const [isExtensionActive, setIsExtensionActive] = useState(false); 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); @@ -63,10 +69,20 @@ export default function App() { if (!injected) { return; } + setIsExtensionActive(true); setInjected(injected); + injected.on('connect', () => { + setIsExtensionActive(true); + }); + injected.on('disconnect', () => { + setIsExtensionActive(false); + }); }, [setInjected]); const currentNetwork = useMemo(() => { + if (!network) { + return null; + } return networkConfigs[network]; }, [network]); @@ -75,7 +91,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 +104,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 +161,9 @@ export default function App() { setResult('publicAddress is empty'); return; } + if (!network) { + return; + } try { setOperating(true); setResult('Signing'); @@ -200,6 +219,9 @@ export default function App() { ); const toPrivateTransaction = useCallback(async () => { + if (!currentNetwork || !network) { + return; + } sendTransaction(async () => { const response = await injected?.privateWallet.toPrivateBuild({ assetId, @@ -222,6 +244,9 @@ export default function App() { ]); const privateTransferTransaction = useCallback(async () => { + if (!currentNetwork || !network) { + return; + } sendTransaction(async () => { const response = await injected?.privateWallet.privateTransferBuild({ assetId, @@ -246,6 +271,9 @@ export default function App() { ]); const toPublicTransaction = useCallback(async () => { + if (!currentNetwork || !network) { + return; + } sendTransaction(async () => { const response = await injected?.privateWallet.toPublicBuild({ assetId, @@ -268,6 +296,9 @@ export default function App() { ]); const multiSbtPostBuildTransition = useCallback(async () => { + if (!network) { + return; + } sendTransaction(async () => { return injected?.privateWallet.multiSbtPostBuild({ sbtInfoList: [ @@ -280,6 +311,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 +372,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 +392,7 @@ export default function App() { return; } updateBalance(); - }, [updateBalance, api]); + }, [updateBalance, api, stateInfo?.isWalletReady]); // auto connect wallet useEffect(() => { @@ -385,11 +424,17 @@ export default function App() { Address)

- {zkAddress}(zKAddress) + {zkAddress}(zkAddress) +

+ +
+ Extension State +

+ isActive: {String(isExtensionActive)}

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

@@ -400,11 +445,14 @@ export default function App() {

Balance +

+ Network: {network} +

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

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

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