refactored network selection
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
import { useUser } from "@/composables/useUser";
|
||||
import { NetworkEnum, TokenEnum } from "@/model/NetworkEnum";
|
||||
import { createPublicClient, http, type Address } from "viem";
|
||||
import { getContractAddress, getRpcUrl, getNetworkConfig, getViemChain } from "@/config/networks";
|
||||
|
||||
export const getTokenByAddress = (address: Address) => {
|
||||
const user = useUser();
|
||||
const networkConfig = getNetworkConfig(user.networkName.value);
|
||||
|
||||
for (const [token, tokenAddress] of Object.entries(networkConfig.contracts.tokenAddresses)) {
|
||||
if (tokenAddress.toLowerCase() === address.toLowerCase()) {
|
||||
return token;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
export const getTokenAddress = (
|
||||
token: TokenEnum,
|
||||
network?: NetworkEnum
|
||||
): Address => {
|
||||
const user = useUser();
|
||||
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 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 networkId = network || user.networkName.value;
|
||||
|
||||
return getRpcUrl(networkId);
|
||||
};
|
||||
|
||||
export const getProviderByNetwork = (network: NetworkEnum) => {
|
||||
const chain = getViemChain(network);
|
||||
return createPublicClient({
|
||||
chain,
|
||||
transport: http(getProviderUrl(network)),
|
||||
});
|
||||
};
|
||||
|
||||
export const isPossibleNetwork = (networkChain: NetworkEnum): boolean => {
|
||||
return Number(networkChain) in NetworkEnum;
|
||||
};
|
||||
@@ -1,11 +1,10 @@
|
||||
import { getContract } from "./provider";
|
||||
import { getTokenAddress } from "./addresses";
|
||||
import { ChainContract } from "viem";
|
||||
import {
|
||||
parseEther,
|
||||
type Address,
|
||||
type TransactionReceipt,
|
||||
} from "viem";
|
||||
import type { TokenEnum } from "@/model/NetworkEnum";
|
||||
|
||||
export const addLock = async (
|
||||
sellerAddress: Address,
|
||||
@@ -37,7 +36,7 @@ export const addLock = async (
|
||||
|
||||
export const withdrawDeposit = async (
|
||||
amount: string,
|
||||
token: TokenEnum
|
||||
token: Address
|
||||
): Promise<boolean> => {
|
||||
const { address, abi, wallet, client, account } = await getContract();
|
||||
|
||||
@@ -45,13 +44,11 @@ export const withdrawDeposit = async (
|
||||
throw new Error("Wallet not connected");
|
||||
}
|
||||
|
||||
const tokenAddress = getTokenAddress(token);
|
||||
|
||||
const { request } = await client.simulateContract({
|
||||
address,
|
||||
abi,
|
||||
functionName: "withdraw",
|
||||
args: [tokenAddress, parseEther(amount), []],
|
||||
args: [token, parseEther(amount), []],
|
||||
account
|
||||
});
|
||||
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import { useUser } from "@/composables/useUser";
|
||||
import { formatEther, toHex, stringToHex } from "viem";
|
||||
import type { PublicClient, Address } from "viem";
|
||||
|
||||
import { Networks } from "@/config/networks";
|
||||
import { getContract } from "./provider";
|
||||
import { getP2PixAddress, getTokenAddress } from "./addresses";
|
||||
import { p2PixAbi } from "./abi"
|
||||
import type { ValidDeposit } from "@/model/ValidDeposit";
|
||||
import { getNetworkSubgraphURL, NetworkEnum, TokenEnum } from "@/model/NetworkEnum";
|
||||
import type { NetworkConfig } from "@/model/NetworkEnum";
|
||||
import type { UnreleasedLock } from "@/model/UnreleasedLock";
|
||||
import type { LockStatus } from "@/model/LockStatus"
|
||||
import { ChainContract } from "viem";
|
||||
|
||||
const getNetworksLiquidity = async (): Promise<void> => {
|
||||
const user = useUser();
|
||||
@@ -16,12 +15,10 @@ const getNetworksLiquidity = async (): Promise<void> => {
|
||||
|
||||
const depositLists: ValidDeposit[][] = [];
|
||||
|
||||
for (const network of Object.values(NetworkEnum).filter(
|
||||
(v) => !isNaN(Number(v))
|
||||
)) {
|
||||
for (const network of Object.values(Networks)) {
|
||||
const deposits = await getValidDeposits(
|
||||
getTokenAddress(user.selectedToken.value),
|
||||
Number(network)
|
||||
user.network.value.tokens[user.selectedToken.value].address,
|
||||
network
|
||||
);
|
||||
if (deposits) depositLists.push(deposits);
|
||||
}
|
||||
@@ -62,7 +59,7 @@ const getParticipantID = async (
|
||||
|
||||
const getValidDeposits = async (
|
||||
token: Address,
|
||||
network: NetworkEnum,
|
||||
network: NetworkConfig,
|
||||
contractInfo?: { client: PublicClient; address: Address }
|
||||
): Promise<ValidDeposit[]> => {
|
||||
let client: PublicClient, abi;
|
||||
@@ -74,9 +71,6 @@ const getValidDeposits = async (
|
||||
({ abi, client } = await getContract(true));
|
||||
}
|
||||
|
||||
// TODO: Remove this once we have a subgraph for rootstock
|
||||
if (network === NetworkEnum.rootstock) return [];
|
||||
|
||||
const body = {
|
||||
query: `
|
||||
{
|
||||
@@ -90,7 +84,7 @@ const getValidDeposits = async (
|
||||
`,
|
||||
};
|
||||
|
||||
const depositLogs = await fetch(getNetworkSubgraphURL(network), {
|
||||
const depositLogs = await fetch( network.subgraphUrls[0], {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
@@ -119,7 +113,7 @@ const getValidDeposits = async (
|
||||
const sellersList = Object.keys(uniqueSellers) as Address[];
|
||||
// Use multicall to batch all getBalance requests
|
||||
const balanceCalls = sellersList.map((seller) => ({
|
||||
address: getP2PixAddress(network),
|
||||
address: (network.contracts?.p2pix as ChainContract).address,
|
||||
abi,
|
||||
functionName: "getBalance",
|
||||
args: [seller, token],
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { p2PixAbi } from "./abi";
|
||||
import { updateWalletStatus } from "./wallet";
|
||||
import { getProviderUrl, getP2PixAddress } from "./addresses";
|
||||
import {
|
||||
createPublicClient,
|
||||
createWalletClient,
|
||||
@@ -10,15 +9,16 @@ import {
|
||||
WalletClient,
|
||||
} from "viem";
|
||||
import { useUser } from "@/composables/useUser";
|
||||
import { getViemChain } from "@/config/networks";
|
||||
import type { NetworkConfig } from "@/model/NetworkEnum";
|
||||
import type { ChainContract } from "viem";
|
||||
|
||||
let walletClient: WalletClient | null = null;
|
||||
|
||||
const getPublicClient = (): PublicClient => {
|
||||
const user = useUser();
|
||||
const rpcUrl = getProviderUrl();
|
||||
const chain = getViemChain(user.networkName.value);
|
||||
|
||||
const rpcUrl = (user.network.value as NetworkConfig).rpcUrls.default.http[0];
|
||||
const chain = user.network.value;
|
||||
|
||||
return createPublicClient({
|
||||
chain,
|
||||
transport: http(rpcUrl),
|
||||
@@ -31,7 +31,8 @@ const getWalletClient = (): WalletClient | null => {
|
||||
|
||||
const getContract = async (onlyRpcProvider = false) => {
|
||||
const client = getPublicClient();
|
||||
const address = getP2PixAddress();
|
||||
const user = useUser();
|
||||
const address = (user.network.value.contracts?.p2pix as ChainContract).address;
|
||||
const abi = p2PixAbi;
|
||||
const wallet = onlyRpcProvider ? null : getWalletClient();
|
||||
|
||||
@@ -46,7 +47,7 @@ const getContract = async (onlyRpcProvider = false) => {
|
||||
|
||||
const connectProvider = async (p: any): Promise<void> => {
|
||||
const user = useUser();
|
||||
const chain = getViemChain(user.networkName.value);
|
||||
const chain = user.network.value;
|
||||
|
||||
const [account] = await p!.request({ method: "eth_requestAccounts" });
|
||||
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import { getContract, getPublicClient, getWalletClient } from "./provider";
|
||||
import { getTokenAddress, getP2PixAddress } from "./addresses";
|
||||
import { parseEther, toHex } from "viem";
|
||||
import { getViemChain } from "@/config/networks";
|
||||
|
||||
import { parseEther, toHex, ChainContract } from "viem";
|
||||
import { mockTokenAbi } from "./abi";
|
||||
import { useUser } from "@/composables/useUser";
|
||||
import { createParticipant } from "@/utils/bbPay";
|
||||
import type { Participant } from "@/utils/bbPay";
|
||||
import type { Address } from "viem";
|
||||
|
||||
const getP2PixAddress = (): Address => {
|
||||
const user = useUser();
|
||||
return (user.network.value.contracts?.p2pix as ChainContract).address;
|
||||
};
|
||||
|
||||
const approveTokens = async (participant: Participant): Promise<any> => {
|
||||
const user = useUser();
|
||||
@@ -21,7 +24,7 @@ const approveTokens = async (participant: Participant): Promise<any> => {
|
||||
const [account] = await walletClient.getAddresses();
|
||||
|
||||
// Get token address
|
||||
const tokenAddress = getTokenAddress(user.selectedToken.value);
|
||||
const tokenAddress = user.network.value.tokens[user.selectedToken.value].address;
|
||||
|
||||
// Check if the token is already approved
|
||||
const allowance = await publicClient.readContract({
|
||||
@@ -33,7 +36,7 @@ const approveTokens = async (participant: Participant): Promise<any> => {
|
||||
|
||||
if ( allowance < parseEther(participant.offer.toString()) ) {
|
||||
// Approve tokens
|
||||
const chain = getViemChain(user.networkName.value);
|
||||
const chain = user.network.value;
|
||||
const hash = await walletClient.writeContract({
|
||||
address: tokenAddress,
|
||||
abi: mockTokenAbi,
|
||||
@@ -65,15 +68,15 @@ const addDeposit = async (): Promise<any> => {
|
||||
if (!sellerId.id) {
|
||||
throw new Error("Failed to create participant");
|
||||
}
|
||||
const chain = getViemChain(user.networkName.value);
|
||||
const chain = user.network.value;
|
||||
const hash = await walletClient.writeContract({
|
||||
address,
|
||||
abi,
|
||||
functionName: "deposit",
|
||||
args: [
|
||||
user.networkId.value + "-" + sellerId.id,
|
||||
user.network.value.id + "-" + sellerId.id,
|
||||
toHex("", { size: 32 }),
|
||||
getTokenAddress(user.selectedToken.value),
|
||||
user.network.value.tokens[user.selectedToken.value].address,
|
||||
parseEther(user.seller.value.offer.toString()),
|
||||
true,
|
||||
],
|
||||
|
||||
@@ -2,7 +2,6 @@ import { formatEther, type Address } from "viem";
|
||||
import { useUser } from "@/composables/useUser";
|
||||
|
||||
import { getPublicClient, getWalletClient, getContract } from "./provider";
|
||||
import { getTokenAddress } from "./addresses";
|
||||
|
||||
import { getValidDeposits, getUnreleasedLockById } from "./events";
|
||||
|
||||
@@ -10,7 +9,6 @@ import type { ValidDeposit } from "@/model/ValidDeposit";
|
||||
import type { WalletTransaction } from "@/model/WalletTransaction";
|
||||
import type { UnreleasedLock } from "@/model/UnreleasedLock";
|
||||
import { LockStatus } from "@/model/LockStatus";
|
||||
import { getNetworkSubgraphURL } from "@/model/NetworkEnum";
|
||||
|
||||
export const updateWalletStatus = async (): Promise<void> => {
|
||||
const user = useUser();
|
||||
@@ -36,8 +34,8 @@ export const listValidDepositTransactionsByWalletAddress = async (
|
||||
): Promise<ValidDeposit[]> => {
|
||||
const user = useUser();
|
||||
const walletDeposits = await getValidDeposits(
|
||||
getTokenAddress(user.selectedToken.value),
|
||||
user.networkName.value
|
||||
user.network.value.tokens[user.selectedToken.value].address,
|
||||
user.network.value
|
||||
);
|
||||
if (walletDeposits) {
|
||||
return walletDeposits
|
||||
@@ -67,7 +65,7 @@ export const listAllTransactionByWalletAddress = async (
|
||||
const user = useUser();
|
||||
|
||||
// Get the current network for the subgraph URL
|
||||
const network = user.networkName.value;
|
||||
const network = user.network.value;
|
||||
|
||||
// Query subgraph for all relevant transactions
|
||||
const subgraphQuery = {
|
||||
@@ -110,7 +108,7 @@ export const listAllTransactionByWalletAddress = async (
|
||||
`,
|
||||
};
|
||||
|
||||
const response = await fetch(getNetworkSubgraphURL(network), {
|
||||
const response = await fetch(network.subgraphUrls[0], {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
@@ -200,7 +198,7 @@ export const listReleaseTransactionByWalletAddress = async (
|
||||
walletAddress: Address
|
||||
) => {
|
||||
const user = useUser();
|
||||
const network = user.networkName.value;
|
||||
const network = user.network.value;
|
||||
|
||||
// Query subgraph for release transactions
|
||||
const subgraphQuery = {
|
||||
@@ -219,7 +217,7 @@ export const listReleaseTransactionByWalletAddress = async (
|
||||
};
|
||||
|
||||
// Fetch data from subgraph
|
||||
const response = await fetch(getNetworkSubgraphURL(network), {
|
||||
const response = await fetch(network.subgraphUrls[0], {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
@@ -263,7 +261,7 @@ export const listReleaseTransactionByWalletAddress = async (
|
||||
|
||||
const listLockTransactionByWalletAddress = async (walletAddress: Address) => {
|
||||
const user = useUser();
|
||||
const network = user.networkName.value;
|
||||
const network = user.network.value;
|
||||
|
||||
// Query subgraph for lock added transactions
|
||||
const subgraphQuery = {
|
||||
@@ -284,7 +282,7 @@ const listLockTransactionByWalletAddress = async (walletAddress: Address) => {
|
||||
|
||||
try {
|
||||
// Fetch data from subgraph
|
||||
const response = await fetch(getNetworkSubgraphURL(network), {
|
||||
const response = await fetch(network.subgraphUrls[0], {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
@@ -332,7 +330,7 @@ const listLockTransactionByWalletAddress = async (walletAddress: Address) => {
|
||||
|
||||
const listLockTransactionBySellerAddress = async (sellerAddress: Address) => {
|
||||
const user = useUser();
|
||||
const network = user.networkName.value;
|
||||
const network = user.network.value;
|
||||
|
||||
// Query subgraph for lock added transactions where seller matches
|
||||
const subgraphQuery = {
|
||||
@@ -354,7 +352,7 @@ const listLockTransactionBySellerAddress = async (sellerAddress: Address) => {
|
||||
|
||||
try {
|
||||
// Fetch data from subgraph
|
||||
const response = await fetch(getNetworkSubgraphURL(network), {
|
||||
const response = await fetch(network.subgraphUrls[0], {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
|
||||
Reference in New Issue
Block a user