Files
P2Pix-Front-End/src/blockchain/sellerMethods.ts
Arthur Abeilice d63cb8c6d3 refactor: clean up code formatting and improve readability across multiple components
- Standardized the use of quotes and spacing in various files.
- Removed unnecessary line breaks and trailing spaces in components.
- Improved the structure of computed properties and methods for better clarity.
- Enhanced the consistency of prop definitions and emit events in Vue components.
- Updated the GraphQL composable to streamline error handling and data processing.
- Refactored network configuration files for better organization and readability.
- Cleaned up model files by removing redundant lines and ensuring consistent formatting.
- Adjusted router configuration for improved readability.
- Enhanced utility functions for better maintainability and clarity.
2026-06-02 01:41:01 +00:00

93 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 };