Files
P2Pix-Front-End/src/blockchain/buyerMethods.ts
2025-06-22 01:17:10 -03:00

87 lines
2.2 KiB
TypeScript

import { getContract } from "./provider";
import { getTokenAddress } from "./addresses";
import { parseEther } from "viem";
import type { TokenEnum } from "@/model/NetworkEnum";
export const addLock = async (
sellerAddress: string,
tokenAddress: string,
amount: number
): Promise<string> => {
const { address, abi, wallet, client, account } = await getContract();
const parsedAmount = parseEther(amount.toString());
const { request } = await client.simulateContract({
address,
abi,
functionName: "lock",
args: [sellerAddress, tokenAddress, parsedAmount, [], []],
account,
});
console.log(wallet);
if (!wallet) {
throw new Error("Wallet is undefined");
}
const hash = await wallet.writeContract(request);
const receipt = await client.waitForTransactionReceipt({ hash });
if (!receipt.status)
throw new Error("Transaction failed: " + receipt.transactionHash);
const topic = receipt.logs[0]?.topics[2];
if (!topic) {
throw new Error("Topic is undefined");
}
return topic;
};
export const withdrawDeposit = async (
amount: string,
token: TokenEnum
): Promise<boolean> => {
const { address, abi, wallet, client, account } = await getContract();
const tokenAddress = getTokenAddress(token);
const { request } = await client.simulateContract({
address,
abi,
functionName: "withdraw",
args: [tokenAddress, parseEther(amount), []],
account
});
if (!wallet) {
throw new Error("Wallet is undefined");
}
const hash = await wallet.writeContract(request);
const receipt = await client.waitForTransactionReceipt({ hash });
if ( receipt.status == "success"){
return true;
} else {
return false;
}
};
export const releaseLock = async (solicitation: any): Promise<any> => {
const { address, abi, wallet, client, account } = await getContract();
const { request } = await client.simulateContract({
address,
abi,
functionName: "release",
args: [solicitation.lockId, solicitation.e2eId],
account
});
if (!wallet) {
throw new Error("Wallet is undefined");
}
const hash = await wallet.writeContract(request);
return client.waitForTransactionReceipt({ hash });
};