refactor: dynamize network config
This commit is contained in:
@@ -1,23 +1,13 @@
|
||||
import { useUser } from "@/composables/useUser";
|
||||
import { NetworkEnum, TokenEnum } from "@/model/NetworkEnum";
|
||||
import { createPublicClient, http, type Address } from "viem";
|
||||
import { sepolia, rootstockTestnet } from "viem/chains";
|
||||
|
||||
const Tokens: { [key in NetworkEnum]: { [key in TokenEnum]: Address } } = {
|
||||
[NetworkEnum.sepolia]: {
|
||||
BRZ: "0x3eBE67A2C7bdB2081CBd34ba3281E90377462289",
|
||||
// BRX: "0x3eBE67A2C7bdB2081CBd34ba3281E90377462289",
|
||||
},
|
||||
[NetworkEnum.rootstock]: {
|
||||
BRZ: "0xfE841c74250e57640390f46d914C88d22C51e82e",
|
||||
// BRX: "0xfE841c74250e57640390f46d914C88d22C51e82e",
|
||||
},
|
||||
};
|
||||
import { getContractAddress, getRpcUrl, getNetworkConfig, getViemChain } from "@/config/networks";
|
||||
|
||||
export const getTokenByAddress = (address: Address) => {
|
||||
const user = useUser();
|
||||
const networksTokens = Tokens[user.networkName.value];
|
||||
for (const [token, tokenAddress] of Object.entries(networksTokens)) {
|
||||
const networkConfig = getNetworkConfig(user.networkName.value);
|
||||
|
||||
for (const [token, tokenAddress] of Object.entries(networkConfig.contracts.tokenAddresses)) {
|
||||
if (tokenAddress.toLowerCase() === address.toLowerCase()) {
|
||||
return token;
|
||||
}
|
||||
@@ -30,35 +20,29 @@ export const getTokenAddress = (
|
||||
network?: NetworkEnum
|
||||
): Address => {
|
||||
const user = useUser();
|
||||
return Tokens[network ? network : user.networkName.value][
|
||||
token
|
||||
];
|
||||
const networkId = network ? network : user.networkName.value;
|
||||
|
||||
const tokenAddress = getContractAddress(networkId, token);
|
||||
return tokenAddress as Address;
|
||||
};
|
||||
|
||||
export const getP2PixAddress = (network?: NetworkEnum): Address => {
|
||||
const user = useUser();
|
||||
const possibleP2PixAddresses: { [key in NetworkEnum]: Address } = {
|
||||
[NetworkEnum.sepolia]: "0xb7cD135F5eFD9760981e02E2a898790b688939fe",
|
||||
[NetworkEnum.rootstock]: "0x57Dcba05980761169508886eEdc6f5E7EC0411Dc",
|
||||
};
|
||||
|
||||
return possibleP2PixAddresses[
|
||||
network ? network : user.networkName.value
|
||||
];
|
||||
const networkId = network ? network : user.networkName.value;
|
||||
|
||||
const p2pixAddress = getContractAddress(networkId, 'p2pix');
|
||||
return p2pixAddress as Address;
|
||||
};
|
||||
|
||||
export const getProviderUrl = (network?: NetworkEnum): string => {
|
||||
const user = useUser();
|
||||
const possibleProvidersUrls: { [key in NetworkEnum]: string } = {
|
||||
[NetworkEnum.sepolia]: import.meta.env.VITE_SEPOLIA_API_URL,
|
||||
[NetworkEnum.rootstock]: import.meta.env.VITE_RSK_API_URL,
|
||||
};
|
||||
|
||||
return possibleProvidersUrls[network || user.networkName.value];
|
||||
const networkId = network || user.networkName.value;
|
||||
|
||||
return getRpcUrl(networkId);
|
||||
};
|
||||
|
||||
export const getProviderByNetwork = (network: NetworkEnum) => {
|
||||
const chain = network === NetworkEnum.sepolia ? sepolia : rootstockTestnet;
|
||||
const chain = getViemChain(network);
|
||||
return createPublicClient({
|
||||
chain,
|
||||
transport: http(getProviderUrl(network)),
|
||||
|
||||
@@ -9,17 +9,18 @@ import {
|
||||
PublicClient,
|
||||
WalletClient,
|
||||
} from "viem";
|
||||
import { sepolia, rootstockTestnet } from "viem/chains";
|
||||
import { useUser } from "@/composables/useUser";
|
||||
import { getViemChain } from "@/config/networks";
|
||||
|
||||
let walletClient: WalletClient | null = null;
|
||||
|
||||
const getPublicClient = (): PublicClient => {
|
||||
const user = useUser();
|
||||
const rpcUrl = getProviderUrl();
|
||||
const chain = getViemChain(user.networkName.value);
|
||||
|
||||
return createPublicClient({
|
||||
chain:
|
||||
Number(user.networkName.value) === sepolia.id ? sepolia : rootstockTestnet,
|
||||
chain,
|
||||
transport: http(rpcUrl),
|
||||
});
|
||||
};
|
||||
@@ -45,8 +46,7 @@ const getContract = async (onlyRpcProvider = false) => {
|
||||
|
||||
const connectProvider = async (p: any): Promise<void> => {
|
||||
const user = useUser();
|
||||
const chain =
|
||||
Number(user.networkName.value) === sepolia.id ? sepolia : rootstockTestnet;
|
||||
const chain = getViemChain(user.networkName.value);
|
||||
|
||||
const [account] = await p!.request({ method: "eth_requestAccounts" });
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { getContract, getPublicClient, getWalletClient } from "./provider";
|
||||
import { getTokenAddress, getP2PixAddress } from "./addresses";
|
||||
import { parseEther, toHex } from "viem";
|
||||
import { sepolia, rootstockTestnet } from "viem/chains";
|
||||
import { getViemChain } from "@/config/networks";
|
||||
|
||||
import { mockTokenAbi } from "./abi";
|
||||
import { useUser } from "@/composables/useUser";
|
||||
@@ -33,7 +33,7 @@ const approveTokens = async (participant: Participant): Promise<any> => {
|
||||
|
||||
if ( allowance < parseEther(participant.offer.toString()) ) {
|
||||
// Approve tokens
|
||||
const chain = user.networkId.value === sepolia.id ? sepolia : rootstockTestnet;
|
||||
const chain = getViemChain(user.networkName.value);
|
||||
const hash = await walletClient.writeContract({
|
||||
address: tokenAddress,
|
||||
abi: mockTokenAbi,
|
||||
@@ -65,7 +65,7 @@ const addDeposit = async (): Promise<any> => {
|
||||
if (!sellerId.id) {
|
||||
throw new Error("Failed to create participant");
|
||||
}
|
||||
const chain = user.networkId.value === sepolia.id ? sepolia : rootstockTestnet;
|
||||
const chain = getViemChain(user.networkName.value);
|
||||
const hash = await walletClient.writeContract({
|
||||
address,
|
||||
abi,
|
||||
|
||||
Reference in New Issue
Block a user