refactored network and token selection
This commit is contained in:
@@ -4,8 +4,6 @@ import {
|
||||
getP2PixAddress,
|
||||
getProviderUrl,
|
||||
isPossibleNetwork,
|
||||
possibleChains,
|
||||
network2Chain,
|
||||
} from "../addresses";
|
||||
|
||||
import { setActivePinia, createPinia } from "pinia";
|
||||
@@ -18,9 +16,6 @@ describe("addresses.ts types", () => {
|
||||
expectTypeOf(getP2PixAddress).toBeFunction();
|
||||
expectTypeOf(getProviderUrl).toBeFunction();
|
||||
expectTypeOf(isPossibleNetwork).toBeFunction();
|
||||
|
||||
expectTypeOf(possibleChains).toBeObject();
|
||||
expectTypeOf(network2Chain).toBeObject();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -115,13 +110,12 @@ describe("addresses.ts functions", () => {
|
||||
it("isPossibleNetwork Returns", () => {
|
||||
const etherStore = useEtherStore();
|
||||
etherStore.setNetworkName(NetworkEnum.ethereum);
|
||||
expect(isPossibleNetwork("0x5")).toBe(true);
|
||||
expect(isPossibleNetwork("5")).toBe(true);
|
||||
expect(isPossibleNetwork("0x13881")).toBe(true);
|
||||
expect(isPossibleNetwork("80001")).toBe(true);
|
||||
expect(isPossibleNetwork(0x5)).toBe(true);
|
||||
expect(isPossibleNetwork(5)).toBe(true);
|
||||
expect(isPossibleNetwork(0x13881)).toBe(true);
|
||||
expect(isPossibleNetwork(80001)).toBe(true);
|
||||
|
||||
expect(isPossibleNetwork("")).toBe(false);
|
||||
expect(isPossibleNetwork(" ")).toBe(false);
|
||||
expect(isPossibleNetwork("0x55")).toBe(false);
|
||||
expect(isPossibleNetwork(NaN)).toBe(false);
|
||||
expect(isPossibleNetwork(0x55)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,62 +1,44 @@
|
||||
import { useEtherStore } from "@/store/ether";
|
||||
import { NetworkEnum, TokenEnum } from "@/model/NetworkEnum";
|
||||
|
||||
const ethereumTokens: { [key in TokenEnum]: string } = {
|
||||
BRZ: "0x3eBE67A2C7bdB2081CBd34ba3281E90377462289",
|
||||
BRX: "0x3eBE67A2C7bdB2081CBd34ba3281E90377462289",
|
||||
}
|
||||
|
||||
const polygonTokens: { [key in TokenEnum]: string } = {
|
||||
BRZ: "0xC86042E9F2977C62Da8c9dDF7F9c40fde4796A29",
|
||||
BRX: "0xC86042E9F2977C62Da8c9dDF7F9c40fde4796A29",
|
||||
}
|
||||
|
||||
const rootstockTokens: { [key in TokenEnum]: string } = {
|
||||
BRZ: "0xfE841c74250e57640390f46d914C88d22C51e82e",
|
||||
BRX: "0xfE841c74250e57640390f46d914C88d22C51e82e",
|
||||
}
|
||||
const Tokens: { [key in NetworkEnum]: {[key in TokenEnum] :string} } = {
|
||||
[NetworkEnum.ethereum]: {
|
||||
BRZ: "0x3eBE67A2C7bdB2081CBd34ba3281E90377462289",
|
||||
BRX: "0x3eBE67A2C7bdB2081CBd34ba3281E90377462289",
|
||||
},
|
||||
[NetworkEnum.polygon]: {
|
||||
BRZ: "0xC86042E9F2977C62Da8c9dDF7F9c40fde4796A29",
|
||||
BRX: "0xC86042E9F2977C62Da8c9dDF7F9c40fde4796A29",
|
||||
},
|
||||
[NetworkEnum.rootstock]: {
|
||||
BRZ: "0xfE841c74250e57640390f46d914C88d22C51e82e",
|
||||
BRX: "0xfE841c74250e57640390f46d914C88d22C51e82e",
|
||||
}
|
||||
};
|
||||
|
||||
export const getTokenByAddress = (address: string) => {
|
||||
for (const token of Object.keys(ethereumTokens)) {
|
||||
if (address === ethereumTokens[token as TokenEnum]) {
|
||||
return token as TokenEnum;
|
||||
}
|
||||
}
|
||||
|
||||
for (const token of Object.keys(polygonTokens)) {
|
||||
if (address === ethereumTokens[token as TokenEnum]) {
|
||||
return token as TokenEnum;
|
||||
}
|
||||
}
|
||||
|
||||
for (const token of Object.keys(rootstockTokens)) {
|
||||
if (address === ethereumTokens[token as TokenEnum]) {
|
||||
return token as TokenEnum;
|
||||
for ( let network in NetworkEnum ) {
|
||||
for (const token of Object.keys(Tokens[network])) {
|
||||
if (address === Tokens[network][token as TokenEnum]) {
|
||||
return token as TokenEnum;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const getTokenAddress = (token: TokenEnum, network?: NetworkEnum): string => {
|
||||
const etherStore = useEtherStore();
|
||||
|
||||
const possibleTokenAddresses: { [key: string]: { [key in TokenEnum]: string } } = {
|
||||
Ethereum: ethereumTokens,
|
||||
Polygon: polygonTokens,
|
||||
Rootstock: rootstockTokens,
|
||||
};
|
||||
|
||||
return possibleTokenAddresses[network ? network : etherStore.networkName][token];
|
||||
return Tokens[network ? network : etherStore.networkName][token];
|
||||
};
|
||||
|
||||
const getP2PixAddress = (network?: NetworkEnum): string => {
|
||||
const etherStore = useEtherStore();
|
||||
|
||||
const possibleP2PixAddresses: { [key: string]: string } = {
|
||||
Ethereum: "0xb7cD135F5eFD9760981e02E2a898790b688939fe",
|
||||
Polygon: "0x4A2886EAEc931e04297ed336Cc55c4eb7C75BA00",
|
||||
Rootstock: "0x98ba35eb14b38D6Aa709338283af3e922476dE34",
|
||||
const possibleP2PixAddresses: { [key in NetworkEnum]: string } = {
|
||||
[NetworkEnum.ethereum]: "0xb7cD135F5eFD9760981e02E2a898790b688939fe",
|
||||
[NetworkEnum.polygon]: "0x4A2886EAEc931e04297ed336Cc55c4eb7C75BA00",
|
||||
[NetworkEnum.rootstock]: "0x98ba35eb14b38D6Aa709338283af3e922476dE34",
|
||||
};
|
||||
|
||||
return possibleP2PixAddresses[network ? network : etherStore.networkName];
|
||||
@@ -64,40 +46,22 @@ const getP2PixAddress = (network?: NetworkEnum): string => {
|
||||
|
||||
const getProviderUrl = (): string => {
|
||||
const etherStore = useEtherStore();
|
||||
const possibleProvidersUrls: { [key: string]: string } = {
|
||||
Ethereum: import.meta.env.VITE_SEPOLIA_API_URL,
|
||||
Polygon: import.meta.env.VITE_MUMBAI_API_URL,
|
||||
Rootstock: import.meta.env.VITE_RSK_API_URL,
|
||||
const possibleProvidersUrls: { [key in NetworkEnum]: string } = {
|
||||
[NetworkEnum.ethereum]: import.meta.env.VITE_SEPOLIA_API_URL,
|
||||
[NetworkEnum.polygon]: import.meta.env.VITE_MUMBAI_API_URL,
|
||||
[NetworkEnum.rootstock]: import.meta.env.VITE_RSK_API_URL,
|
||||
};
|
||||
|
||||
return possibleProvidersUrls[etherStore.networkName];
|
||||
};
|
||||
|
||||
const possibleChains: { [key: string]: NetworkEnum } = {
|
||||
"11155111": NetworkEnum.ethereum,
|
||||
"80001": NetworkEnum.polygon,
|
||||
"31": NetworkEnum.rootstock,
|
||||
};
|
||||
|
||||
const network2Chain: { [key: string]: string } = {
|
||||
Ethereum: "0xAA36A7",
|
||||
Polygon: "0x13881",
|
||||
Localhost: "0x7a69",
|
||||
Rootstock: "0x1f",
|
||||
};
|
||||
|
||||
const isPossibleNetwork = (networkChain: string): boolean => {
|
||||
if (Object.keys(possibleChains).includes(networkChain.toString())) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
const isPossibleNetwork = (networkChain: NetworkEnum): boolean => {
|
||||
return (Number(networkChain) in NetworkEnum);
|
||||
};
|
||||
|
||||
export {
|
||||
getTokenAddress,
|
||||
getProviderUrl,
|
||||
possibleChains,
|
||||
network2Chain,
|
||||
isPossibleNetwork,
|
||||
getP2PixAddress,
|
||||
};
|
||||
|
||||
@@ -6,11 +6,12 @@ import { updateWalletStatus } from "./wallet";
|
||||
import {
|
||||
getProviderUrl,
|
||||
isPossibleNetwork,
|
||||
possibleChains,
|
||||
network2Chain,
|
||||
getP2PixAddress,
|
||||
} from "./addresses";
|
||||
|
||||
import type { NetworkEnum } from "@/model/NetworkEnum";
|
||||
import { Networks } from "@/model/Networks";
|
||||
|
||||
import { ethers } from "ethers";
|
||||
|
||||
const getProvider = (
|
||||
@@ -57,11 +58,11 @@ const listenToWalletChange = (connection: any): void => {
|
||||
const listenToNetworkChange = (connection: any) => {
|
||||
const etherStore = useEtherStore();
|
||||
|
||||
connection.on("chainChanged", (networkChain: string) => {
|
||||
connection.on("chainChanged", (networkChain: NetworkEnum) => {
|
||||
console.log("Changed network!");
|
||||
|
||||
if (isPossibleNetwork(networkChain)) {
|
||||
etherStore.setNetworkName(possibleChains[networkChain]);
|
||||
etherStore.setNetworkName(networkChain);
|
||||
updateWalletStatus();
|
||||
} else {
|
||||
window.alert("Invalid chain!");
|
||||
@@ -69,7 +70,7 @@ const listenToNetworkChange = (connection: any) => {
|
||||
});
|
||||
};
|
||||
|
||||
const requestNetworkChange = async (network: string): Promise<boolean> => {
|
||||
const requestNetworkChange = async (network: NetworkEnum): Promise<boolean> => {
|
||||
const etherStore = useEtherStore();
|
||||
if (!etherStore.walletAddress) return true;
|
||||
|
||||
@@ -77,7 +78,7 @@ const requestNetworkChange = async (network: string): Promise<boolean> => {
|
||||
const window_ = window as any;
|
||||
await window_.ethereum.request({
|
||||
method: "wallet_switchEthereumChain",
|
||||
params: [{ chainId: network2Chain[network] }], // chainId must be in hexadecimal numbers
|
||||
params: [{ chainId: Networks[network].chainId }], // chainId must be in hexadecimal numbers
|
||||
});
|
||||
} catch {
|
||||
return false;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useEtherStore } from "@/store/ether";
|
||||
|
||||
import { getContract, getProvider } from "./provider";
|
||||
import { getTokenAddress, possibleChains, isPossibleNetwork } from "./addresses";
|
||||
import { getTokenAddress, isPossibleNetwork } from "./addresses";
|
||||
|
||||
import mockToken from "@/utils/smart_contract_files/MockToken.json";
|
||||
|
||||
@@ -21,11 +21,11 @@ const updateWalletStatus = async (): Promise<void> => {
|
||||
const signer = provider.getSigner();
|
||||
|
||||
const { chainId } = await provider.getNetwork();
|
||||
if (!isPossibleNetwork(chainId.toString())) {
|
||||
if (!isPossibleNetwork(chainId)) {
|
||||
window.alert("Invalid chain!:" + chainId);
|
||||
return;
|
||||
}
|
||||
etherStore.setNetworkName(possibleChains[chainId]);
|
||||
etherStore.setNetworkName(chainId);
|
||||
|
||||
const mockTokenContract = new ethers.Contract(
|
||||
getTokenAddress(etherStore.selectedToken),
|
||||
|
||||
Reference in New Issue
Block a user