P2Pix-Front-End/src/blockchain/sellerMethods.ts
2025-10-11 22:07:32 -03:00

92 lines
2.6 KiB
TypeScript

import { getContract, getPublicClient, getWalletClient } from "./provider";
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();
const publicClient = getPublicClient();
const walletClient = getWalletClient();
if (!publicClient || !walletClient) {
throw new Error("Clients not initialized");
}
user.setSeller(participant);
const [account] = await walletClient.getAddresses();
// Get token address
const tokenAddress = user.network.value.tokens[user.selectedToken.value].address;
// Check if the token is already approved
const allowance = await publicClient.readContract({
address: tokenAddress,
abi: mockTokenAbi,
functionName: "allowance",
args: [account, getP2PixAddress()],
});
if ( allowance < parseEther(participant.offer.toString()) ) {
// Approve tokens
const chain = user.network.value;
const hash = await walletClient.writeContract({
address: tokenAddress,
abi: mockTokenAbi,
functionName: "approve",
args: [getP2PixAddress(), parseEther(participant.offer.toString())],
account,
chain,
});
await publicClient.waitForTransactionReceipt({ hash });
return true;
}
return true;
};
const addDeposit = async (): Promise<any> => {
const { address, abi, client } = await getContract();
const walletClient = getWalletClient();
const user = useUser();
if (!walletClient) {
throw new Error("Wallet client not initialized");
}
const [account] = await walletClient.getAddresses();
const sellerId = await createParticipant(user.seller.value);
user.setSellerId(sellerId.id);
if (!sellerId.id) {
throw new Error("Failed to create participant");
}
const chain = user.network.value;
const hash = await walletClient.writeContract({
address,
abi,
functionName: "deposit",
args: [
user.network.value.id + "-" + sellerId.id,
toHex("", { size: 32 }),
user.network.value.tokens[user.selectedToken.value].address,
parseEther(user.seller.value.offer.toString()),
true,
],
account,
chain,
});
const receipt = await client.waitForTransactionReceipt({ hash });
return receipt;
};
export { approveTokens, addDeposit };