2022-08-08 09:32:24 +00:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
ReactNode,
|
|
|
|
useContext,
|
|
|
|
useEffect,
|
|
|
|
useState,
|
|
|
|
} from "react";
|
2022-03-17 15:56:54 +00:00
|
|
|
import { SolanaChainData } from "../chains/solana";
|
2022-08-11 09:43:12 +00:00
|
|
|
import { PolkadotChainData } from "../chains/polkadot";
|
2023-05-30 07:13:49 +00:00
|
|
|
import { MultiversxChainData } from "../chains/multiversx";
|
2023-02-22 17:01:39 +00:00
|
|
|
import { TronChainData } from "../chains/tron";
|
2022-03-17 15:56:54 +00:00
|
|
|
|
2023-02-22 17:01:39 +00:00
|
|
|
import { ChainNamespaces, ChainsMap, getAllChainNamespaces } from "../helpers";
|
2022-10-18 10:31:08 +00:00
|
|
|
import { NearChainData } from "../chains/near";
|
2023-02-22 17:01:39 +00:00
|
|
|
import { CosmosChainData } from "../chains/cosmos";
|
|
|
|
import { EIP155ChainData } from "../chains/eip155";
|
2023-03-31 08:35:53 +00:00
|
|
|
import { TezosChainData } from "../chains/tezos";
|
2022-03-17 15:56:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Types
|
|
|
|
*/
|
|
|
|
interface IContext {
|
|
|
|
chainData: ChainNamespaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Context
|
|
|
|
*/
|
|
|
|
export const ChainDataContext = createContext<IContext>({} as IContext);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provider
|
|
|
|
*/
|
2022-08-08 09:32:24 +00:00
|
|
|
export function ChainDataContextProvider({
|
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: ReactNode | ReactNode[];
|
|
|
|
}) {
|
2022-03-17 15:56:54 +00:00
|
|
|
const [chainData, setChainData] = useState<ChainNamespaces>({});
|
|
|
|
|
|
|
|
const loadChainData = async () => {
|
|
|
|
const namespaces = getAllChainNamespaces();
|
|
|
|
const chainData: ChainNamespaces = {};
|
|
|
|
await Promise.all(
|
2022-08-08 09:32:24 +00:00
|
|
|
namespaces.map(async (namespace) => {
|
2022-03-17 15:56:54 +00:00
|
|
|
let chains: ChainsMap | undefined;
|
2023-02-22 17:01:39 +00:00
|
|
|
switch (namespace) {
|
|
|
|
case "solana":
|
2022-03-17 15:56:54 +00:00
|
|
|
chains = SolanaChainData;
|
2023-02-22 17:01:39 +00:00
|
|
|
break;
|
|
|
|
case "polkadot":
|
2022-08-11 09:43:12 +00:00
|
|
|
chains = PolkadotChainData;
|
2023-02-22 17:01:39 +00:00
|
|
|
break;
|
|
|
|
case "near":
|
2022-10-18 10:31:08 +00:00
|
|
|
chains = NearChainData;
|
2023-02-22 17:01:39 +00:00
|
|
|
break;
|
2023-05-30 07:13:49 +00:00
|
|
|
case "mvx":
|
|
|
|
chains = MultiversxChainData;
|
2023-02-22 17:01:39 +00:00
|
|
|
break;
|
|
|
|
case "tron":
|
2022-12-21 12:32:39 +00:00
|
|
|
chains = TronChainData;
|
2023-02-22 17:01:39 +00:00
|
|
|
break;
|
|
|
|
case "cosmos":
|
|
|
|
chains = CosmosChainData;
|
|
|
|
break;
|
|
|
|
case "eip155":
|
|
|
|
chains = EIP155ChainData;
|
|
|
|
break;
|
2023-03-31 08:35:53 +00:00
|
|
|
case "tezos":
|
|
|
|
chains = TezosChainData;
|
|
|
|
break;
|
2023-02-22 17:01:39 +00:00
|
|
|
default:
|
|
|
|
console.error("Unknown chain namespace: ", namespace);
|
2022-03-17 15:56:54 +00:00
|
|
|
}
|
2023-02-22 17:01:39 +00:00
|
|
|
|
2022-03-17 15:56:54 +00:00
|
|
|
if (typeof chains !== "undefined") {
|
|
|
|
chainData[namespace] = chains;
|
|
|
|
}
|
2022-08-08 09:32:24 +00:00
|
|
|
})
|
2022-03-17 15:56:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
setChainData(chainData);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
loadChainData();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ChainDataContext.Provider
|
|
|
|
value={{
|
|
|
|
chainData,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</ChainDataContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useChainData() {
|
|
|
|
const context = useContext(ChainDataContext);
|
|
|
|
if (context === undefined) {
|
2022-08-08 09:32:24 +00:00
|
|
|
throw new Error(
|
|
|
|
"useChainData must be used within a ChainDataContextProvider"
|
|
|
|
);
|
2022-03-17 15:56:54 +00:00
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|