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

Multiple rpcs #54

Merged
merged 6 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions src/components/Modals/AddAddress/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ export const AddAddressModal = ({ open, onClose }: AddAddressModalProps) => {
</MenuItem>
))}
</TextField>
{networkId !== undefined && (
<div>
<FormHelperText
sx={{
width: '100%',
textAlign: 'right',
margin: 0,
}}
>
<span>{`Ss58 prefix: ${networks[networkId].ss58Prefix}`}</span>
</FormHelperText>
</div>
)}
</FormControl>
<FormControl className='form-item'>
<FormLabel>Address</FormLabel>
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/addressbook/metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": {
"hash": "0x4ab87202c8d94c5fd2bb598ea73ce826b93351f47e0a2e9ca61f9a4c7591b129",
"hash": "0x8dc8d21880cc15012552148eb6551379525ee975554f1f87b61470a487333135",
"language": "ink! 4.2.0",
"compiler": "rustc 1.68.0-nightly",
"build_info": {
Expand All @@ -14,7 +14,7 @@
}
},
"contract": {
"name": "address-book",
"name": "address_book",
"version": "0.1.0",
"authors": [
"Master Union <masteruniondoo@gmail.com>"
Expand Down
42 changes: 26 additions & 16 deletions src/contracts/identity/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ const IdentityContractProvider = ({ children }: Props) => {
const [identityNo, setIdentityNo] = useState<IdentityNo>(null);
const [networks, setNetworks] = useState<Networks>({});
const [addresses, setAddresses] = useState<Array<Address>>([]);
const [loading, setLoading] = useState(false);
const [loadingIdentityNo, setLoadingIdentityNo] = useState(false);
const [loadingNetworks, setLoadingNetworks] = useState(false);
const { toastError } = useToast();

const fetchIdentityNo = useCallback(async () => {
if (!api || !contract || !activeAccount) {
setIdentityNo(null);
return;
}
setLoadingIdentityNo(true);
try {
const result = await contractQuery(api, '', contract, 'identity_of', {}, [
activeAccount.address,
Expand All @@ -87,6 +89,7 @@ const IdentityContractProvider = ({ children }: Props) => {
} catch (e) {
setIdentityNo(null);
}
setLoadingIdentityNo(false);
}, [activeAccount, api, contract]);

const fetchNetworks = useCallback(async () => {
Expand All @@ -96,28 +99,36 @@ const IdentityContractProvider = ({ children }: Props) => {
}

const getChainInfo = async (
rpcUrl: string
rpcUrls: string[]
): Promise<NetworkConsts | null> => {
const count = rpcUrls.length;
const rpcIndex = Math.min(Math.floor(Math.random() * count), count - 1);
const rpc = rpcUrls[rpcIndex];
try {
const provider = new WsProvider(rpcUrl);
const provider = new WsProvider(rpc);
const api = new ApiPromise({ provider, rpc: jsonrpc });

await api.isReady;

const ss58Prefix: number =
api.consts.system.ss58Prefix.toPrimitive() as number;
const name = (await api.rpc.system.chain()).toString();
const paraId = (
await api.query.parachainInfo.parachainId()
).toPrimitive() as number;

return {
name,
ss58Prefix,
paraId,
};
} catch (e) {
toastError && toastError(`Failed to get chain info for ${rpcUrl}`);
toastError && toastError(`Failed to get chain info for ${rpc}`);
return null;
}
};

setLoadingNetworks(true);
try {
const result = await contractQuery(
api,
Expand All @@ -137,11 +148,11 @@ const IdentityContractProvider = ({ children }: Props) => {

for await (const item of output) {
const networkId = Number(item[0]);
const { accountType, rpcUrl } = item[1];
const info = await getChainInfo(rpcUrl);
const { accountType, rpcUrls } = item[1];
const info = await getChainInfo(rpcUrls);
if (info)
_networks[networkId] = {
rpcUrl,
rpcUrls,
accountType,
...info,
};
Expand All @@ -150,6 +161,7 @@ const IdentityContractProvider = ({ children }: Props) => {
} catch (e: any) {
toastError(e.toString());
}
setLoadingNetworks(false);
}, [api, contract, toastError]);

const fetchAddresses = useCallback(async () => {
Expand Down Expand Up @@ -189,14 +201,12 @@ const IdentityContractProvider = ({ children }: Props) => {
}, [api, contract, identityNo, fetchAddresses]);

useEffect(() => {
const init = async () => {
setLoading(true);
await fetchIdentityNo();
await fetchNetworks();
setLoading(false);
};
init();
}, [fetchIdentityNo, fetchNetworks]);
fetchIdentityNo();
}, [api, contract, activeAccount]);

useEffect(() => {
fetchNetworks();
}, [api?.isReady, contract?.address]);

return (
<IdentityContext.Provider
Expand All @@ -207,7 +217,7 @@ const IdentityContractProvider = ({ children }: Props) => {
networks,
fetchAddresses,
fetchIdentityNo,
loading,
loading: loadingIdentityNo || loadingNetworks,
}}
>
{children}
Expand Down
Loading
Loading