Migrated project to Viem, removing Ethers completelly. Not finished tests.
This commit is contained in:
@@ -1,60 +1,81 @@
|
||||
import { getContract, getProvider } from "./provider";
|
||||
import { getContract, getPublicClient, getWalletClient } from "./provider";
|
||||
import { getTokenAddress, getP2PixAddress } from "./addresses";
|
||||
|
||||
import { encodeBytes32String, Contract, parseEther } from "ethers";
|
||||
import { parseEther, toHex } from "viem";
|
||||
|
||||
import mockToken from "../utils/smart_contract_files/MockToken.json";
|
||||
import { useEtherStore } from "@/store/ether";
|
||||
import { useViemStore } from "@/store/viem";
|
||||
import { createParticipant } from "@/utils/bbPay";
|
||||
import type { Participant } from "@/utils/bbPay";
|
||||
|
||||
const approveTokens = async (participant: Participant): Promise<any> => {
|
||||
const provider = getProvider();
|
||||
const signer = await provider?.getSigner();
|
||||
const etherStore = useEtherStore();
|
||||
|
||||
etherStore.setSeller(participant);
|
||||
const tokenContract = new Contract(
|
||||
getTokenAddress(etherStore.selectedToken),
|
||||
mockToken.abi,
|
||||
signer
|
||||
);
|
||||
|
||||
const viemStore = useViemStore();
|
||||
const publicClient = getPublicClient();
|
||||
const walletClient = getWalletClient();
|
||||
|
||||
if (!publicClient || !walletClient) {
|
||||
throw new Error("Clients not initialized");
|
||||
}
|
||||
|
||||
viemStore.setSeller(participant);
|
||||
const [account] = await walletClient.getAddresses();
|
||||
|
||||
// Get token address
|
||||
const tokenAddress = getTokenAddress(viemStore.selectedToken);
|
||||
|
||||
// Check if the token is already approved
|
||||
const approved = await tokenContract.allowance(
|
||||
await signer?.getAddress(),
|
||||
getP2PixAddress()
|
||||
);
|
||||
if (approved < parseEther(participant.offer)) {
|
||||
const allowance = await publicClient.readContract({
|
||||
address: tokenAddress,
|
||||
abi: mockToken.abi,
|
||||
functionName: 'allowance',
|
||||
args: [account, getP2PixAddress()]
|
||||
});
|
||||
|
||||
if (allowance < parseEther(participant.offer)) {
|
||||
// Approve tokens
|
||||
const apprv = await tokenContract.approve(
|
||||
getP2PixAddress(),
|
||||
parseEther(participant.offer)
|
||||
);
|
||||
await apprv.wait();
|
||||
const hash = await walletClient.writeContract({
|
||||
address: tokenAddress,
|
||||
abi: mockToken.abi,
|
||||
functionName: 'approve',
|
||||
args: [getP2PixAddress(), parseEther(participant.offer)],
|
||||
account
|
||||
});
|
||||
|
||||
await publicClient.waitForTransactionReceipt({ hash });
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
const addDeposit = async (): Promise<any> => {
|
||||
const p2pContract = await getContract();
|
||||
const etherStore = useEtherStore();
|
||||
|
||||
const sellerId = await createParticipant(etherStore.seller);
|
||||
etherStore.setSellerId(sellerId.id);
|
||||
|
||||
const deposit = await p2pContract.deposit(
|
||||
sellerId,
|
||||
encodeBytes32String(""),
|
||||
getTokenAddress(etherStore.selectedToken),
|
||||
parseEther(etherStore.seller.offer),
|
||||
true
|
||||
);
|
||||
|
||||
await deposit.wait();
|
||||
|
||||
return deposit;
|
||||
const { address, abi, client } = await getContract();
|
||||
const walletClient = getWalletClient();
|
||||
const viemStore = useViemStore();
|
||||
|
||||
if (!walletClient) {
|
||||
throw new Error("Wallet client not initialized");
|
||||
}
|
||||
|
||||
const [account] = await walletClient.getAddresses();
|
||||
|
||||
const sellerId = await createParticipant(viemStore.seller);
|
||||
viemStore.setSellerId(sellerId.id);
|
||||
|
||||
const hash = await walletClient.writeContract({
|
||||
address,
|
||||
abi,
|
||||
functionName: 'deposit',
|
||||
args: [
|
||||
sellerId.id,
|
||||
toHex("", { size: 32 }),
|
||||
getTokenAddress(viemStore.selectedToken),
|
||||
parseEther(viemStore.seller.offer),
|
||||
true
|
||||
],
|
||||
account
|
||||
});
|
||||
|
||||
const receipt = await client.waitForTransactionReceipt({ hash });
|
||||
return receipt;
|
||||
};
|
||||
|
||||
export { approveTokens, addDeposit };
|
||||
|
||||
Reference in New Issue
Block a user