Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update network #137

Merged
merged 2 commits into from
Jul 3, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 51 additions & 20 deletions manta-js/examples/extension-example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -37,7 +42,7 @@ let isConnecting = false;
export default function App() {
const [isInjected] = useState(!!injectedWeb3);
const [injected, setInjected] = useState<Injected | null>(null);
const [network, setNetwork] = useState<string>('');
const [network, setNetwork] = useState<Network | undefined>(undefined);
const [api, setApi] = useState<ApiPromise | null>(null);
const [publicAddress, setPublicAddress] = useState<string | null>(null);
const [zkAddress, setZkAddress] = useState<string | null>(null);
Expand Down Expand Up @@ -67,6 +72,9 @@ export default function App() {
}, [setInjected]);

const currentNetwork = useMemo(() => {
if (!network) {
return null;
}
return networkConfigs[network];
}, [network]);

Expand All @@ -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({
Expand All @@ -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);
Expand Down Expand Up @@ -145,6 +153,9 @@ export default function App() {
setResult('publicAddress is empty');
return;
}
if (!network) {
return;
}
try {
setOperating(true);
setResult('Signing');
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -268,6 +288,9 @@ export default function App() {
]);

const multiSbtPostBuildTransition = useCallback(async () => {
if (!network) {
return;
}
sendTransaction(async () => {
return injected?.privateWallet.multiSbtPostBuild({
sbtInfoList: [
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -353,7 +384,7 @@ export default function App() {
return;
}
updateBalance();
}, [updateBalance, api]);
}, [updateBalance, api, stateInfo?.isWalletReady]);

// auto connect wallet
useEffect(() => {
Expand Down Expand Up @@ -401,10 +432,10 @@ export default function App() {
<fieldset>
<legend>Balance</legend>
<p>
{currentNetwork.assetName}: <strong>{publicBalance}</strong>
{currentNetwork?.assetName}: <strong>{publicBalance}</strong>
</p>
<p>
zk{currentNetwork.assetName}: <strong>{balance}</strong>
zk{currentNetwork?.assetName}: <strong>{balance}</strong>
</p>
</fieldset>
<fieldset>
Expand All @@ -417,7 +448,7 @@ export default function App() {
setToPrivateAmount(e.target.value);
}}
/>
<i>{currentNetwork.assetName}</i>
<i>{currentNetwork?.assetName}</i>
</span>
<button
type="button"
Expand All @@ -435,7 +466,7 @@ export default function App() {
setPrivateTransferAmount(e.target.value);
}}
/>
<i>zk{currentNetwork.assetName}</i>
<i>zk{currentNetwork?.assetName}</i>
</span>
<button
type="button"
Expand All @@ -462,7 +493,7 @@ export default function App() {
setToPublicAmount(e.target.value);
}}
/>
<i>zk{currentNetwork.assetName}</i>
<i>zk{currentNetwork?.assetName}</i>
</span>
<button
type="button"
Expand Down