From 9d9ed7a3ddb32d01369fd2ce35d5891b0c4164f4 Mon Sep 17 00:00:00 2001 From: hueso Date: Sun, 22 Jun 2025 01:17:10 -0300 Subject: [PATCH] fix type errors --- src/blockchain/addresses.ts | 8 ++--- src/blockchain/buyerMethods.ts | 36 ++++++++++++++++--- src/blockchain/events.ts | 28 +++++++++++++-- src/blockchain/provider.ts | 15 ++++---- src/blockchain/sellerMethods.ts | 11 +++--- src/blockchain/wallet.ts | 20 +++++------ .../ListingComponent/ListingComponent.vue | 4 +-- 7 files changed, 86 insertions(+), 36 deletions(-) diff --git a/src/blockchain/addresses.ts b/src/blockchain/addresses.ts index 8a85409..51fd8f0 100644 --- a/src/blockchain/addresses.ts +++ b/src/blockchain/addresses.ts @@ -3,7 +3,7 @@ import { NetworkEnum, TokenEnum } from "@/model/NetworkEnum"; import { createPublicClient, http } from "viem"; import { sepolia, rootstock } from "viem/chains"; -const Tokens: { [key in NetworkEnum]: { [key in TokenEnum]: string } } = { +const Tokens: { [key in NetworkEnum]: { [key in TokenEnum]: `0x${string}` } } = { [NetworkEnum.sepolia]: { BRZ: "0x3eBE67A2C7bdB2081CBd34ba3281E90377462289", // BRX: "0x3eBE67A2C7bdB2081CBd34ba3281E90377462289", @@ -28,14 +28,14 @@ export const getTokenByAddress = (address: string) => { export const getTokenAddress = ( token: TokenEnum, network?: NetworkEnum -): string => { +): `0x${string}` => { const user = useUser(); return Tokens[network ? network : user.networkName.value][token]; }; -export const getP2PixAddress = (network?: NetworkEnum): string => { +export const getP2PixAddress = (network?: NetworkEnum): `0x${string}` => { const user = useUser(); - const possibleP2PixAddresses: { [key in NetworkEnum]: string } = { + const possibleP2PixAddresses: { [key in NetworkEnum]: `0x${string}` } = { [NetworkEnum.sepolia]: "0xb7cD135F5eFD9760981e02E2a898790b688939fe", [NetworkEnum.rootstock]: "0x98ba35eb14b38D6Aa709338283af3e922476dE34", }; diff --git a/src/blockchain/buyerMethods.ts b/src/blockchain/buyerMethods.ts index 2e445b0..57f671a 100644 --- a/src/blockchain/buyerMethods.ts +++ b/src/blockchain/buyerMethods.ts @@ -8,7 +8,7 @@ export const addLock = async ( tokenAddress: string, amount: number ): Promise => { - const { address, abi, wallet, client } = await getContract(); + const { address, abi, wallet, client, account } = await getContract(); const parsedAmount = parseEther(amount.toString()); const { request } = await client.simulateContract({ @@ -16,19 +16,31 @@ export const addLock = async ( 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 }); - return receipt.status ? receipt.logs[0].topics[2] : ""; + 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 => { - const { address, abi, wallet, client } = await getContract(); + const { address, abi, wallet, client, account } = await getContract(); const tokenAddress = getTokenAddress(token); @@ -37,24 +49,38 @@ export const withdrawDeposit = async ( 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 }); - return receipt.status; + if ( receipt.status == "success"){ + return true; + } else { + return false; + } }; export const releaseLock = async (solicitation: any): Promise => { - const { address, abi, wallet, client } = await getContract(); + 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 }); }; diff --git a/src/blockchain/events.ts b/src/blockchain/events.ts index ce739bc..34117b5 100644 --- a/src/blockchain/events.ts +++ b/src/blockchain/events.ts @@ -1,5 +1,5 @@ import { useUser } from "@/composables/useUser"; -import { formatEther, toHex, type PublicClient } from "viem"; +import { formatEther, toHex, type PublicClient, type ByteArray } from "viem"; import p2pix from "@/utils/smart_contract_files/P2PIX.json"; import { getContract } from "./provider"; @@ -9,6 +9,28 @@ import { getNetworkSubgraphURL, NetworkEnum } from "@/model/NetworkEnum"; import type { UnreleasedLock } from "@/model/UnreleasedLock"; import type { Pix } from "@/model/Pix"; +export interface Lock { // from DataTypes.sol + counter: bigint; + expirationBlock: bigint; + pixTarget: string; + amount: bigint; + token: `0x${string}`; + buyerAddress: `0x${string}`; + seller: `0x${string}`; +} + +export enum LockStatus { + Inexistent = 0, // Uninitialized Lock + Active = 1, // Valid Lock + Expired = 2, // Expired Lock + Released = 3 // Already released Lock +} + +export interface GetLocksStatusResponse { + sortedIDs: number[]; + status: LockStatus[]; +} + const getNetworksLiquidity = async (): Promise => { const user = useUser(); user.setLoadingNetworkLiquidity(true); @@ -38,7 +60,7 @@ const getPixKey = async (seller: string, token: string): Promise => { abi, functionName: "getPixTarget", args: [seller, token], - }); + }) as ByteArray; // Remove '0x' prefix and convert hex to UTF-8 string const hexString = @@ -156,7 +178,7 @@ const getUnreleasedLockById = async ( abi, functionName: "mapLocks", args: [BigInt(lockID)], - }); + }) as Lock; const pixTarget = lock.pixTarget; const amount = formatEther(lock.amount); diff --git a/src/blockchain/provider.ts b/src/blockchain/provider.ts index ab813d4..38ffdf6 100644 --- a/src/blockchain/provider.ts +++ b/src/blockchain/provider.ts @@ -11,12 +11,10 @@ import { } from "viem"; import { sepolia, rootstock } from "viem/chains"; import { useUser } from "@/composables/useUser"; - let publicClient: PublicClient | null = null; let walletClient: WalletClient | null = null; -const getPublicClient: PublicClient | null = (onlyRpcProvider = false) => { - if (onlyRpcProvider) { +const getPublicClient = (): PublicClient => { const user = useUser(); const rpcUrl = getProviderUrl(); return createPublicClient({ @@ -24,21 +22,22 @@ const getPublicClient: PublicClient | null = (onlyRpcProvider = false) => { Number(user.networkName.value) === sepolia.id ? sepolia : rootstock, transport: http(rpcUrl), }); - } - return publicClient; }; -const getWalletClient: WalletClient | null = () => { +const getWalletClient = (): WalletClient | null => { return walletClient; }; const getContract = async (onlyRpcProvider = false) => { - const client = getPublicClient(onlyRpcProvider); + const client = getPublicClient(); + if (!client) { + throw new Error("Public client is not initialized"); + } const address = getP2PixAddress(); const abi = p2pix.abi; const wallet = getWalletClient(); - const [account] = wallet ? await wallet.getAddresses() : [""]; + const [account] = wallet ? await wallet.getAddresses() : [null]; return { address, abi, client, wallet, account }; }; diff --git a/src/blockchain/sellerMethods.ts b/src/blockchain/sellerMethods.ts index dcd3aa8..91f12e8 100644 --- a/src/blockchain/sellerMethods.ts +++ b/src/blockchain/sellerMethods.ts @@ -28,18 +28,19 @@ const approveTokens = async (participant: Participant): Promise => { abi: mockToken.abi, functionName: "allowance", args: [account, getP2PixAddress()], - }); + }) as bigint; if (allowance < parseEther(participant.offer.toString())) { // Approve tokens - const hash = await walletClient.writeContract({ + const { result } = await publicClient.simulateContract({ address: tokenAddress, abi: mockToken.abi, functionName: "approve", args: [getP2PixAddress(), parseEther(participant.offer.toString())], - account, + account }); + const hash = await walletClient.writeContract(result); await publicClient.waitForTransactionReceipt({ hash }); return true; } @@ -48,6 +49,7 @@ const approveTokens = async (participant: Participant): Promise => { const addDeposit = async (): Promise => { const { address, abi, client } = await getContract(); + const publicClient = getPublicClient(); const walletClient = getWalletClient(); const user = useUser(); @@ -60,7 +62,7 @@ const addDeposit = async (): Promise => { const sellerId = await createParticipant(user.seller.value); user.setSellerId(sellerId.id); - const hash = await walletClient.writeContract({ + const { result } = await publicClient.simulateContract({ address, abi, functionName: "deposit", @@ -74,6 +76,7 @@ const addDeposit = async (): Promise => { account, }); + const hash = await walletClient.writeContract(result); const receipt = await client.waitForTransactionReceipt({ hash }); return receipt; }; diff --git a/src/blockchain/wallet.ts b/src/blockchain/wallet.ts index 338dd5d..1236e8e 100644 --- a/src/blockchain/wallet.ts +++ b/src/blockchain/wallet.ts @@ -6,7 +6,7 @@ import { getTokenAddress } from "./addresses"; import p2pix from "@/utils/smart_contract_files/P2PIX.json"; -import { getValidDeposits } from "./events"; +import { getValidDeposits, Lock, LockStatus, GetLocksStatusResponse } from "./events"; import type { ValidDeposit } from "@/model/ValidDeposit"; import type { WalletTransaction } from "@/model/WalletTransaction"; @@ -52,15 +52,15 @@ export const listValidDepositTransactionsByWalletAddress = async ( return []; }; -const getLockStatus = async (id: bigint): Promise => { +const getLockStatus = async (id: bigint): Promise => { const { address, abi, client } = await getContract(); - const result = await client.readContract({ + const { sortedIDs , status } = await client.readContract({ address, abi, functionName: "getLocksStatus", args: [[id]], - }); - return result[1][0]; + }) as GetLocksStatusResponse; + return status[0]; }; const filterLockStatus = async ( @@ -469,12 +469,12 @@ export const checkUnreleasedLock = async ( const lockIds = addedLocks.map((lock: any) => lock.args.lockID); - const lockStatus = await client.readContract({ + const { sortedIDs, status: lockStatus } = await client.readContract({ address, abi, functionName: "getLocksStatus", args: [lockIds], - }); + }) as GetLocksStatusResponse; const unreleasedLockId = lockStatus[1].findIndex( (status: number) => status == 1 @@ -488,7 +488,7 @@ export const checkUnreleasedLock = async ( abi, functionName: "mapLocks", args: [lockID], - }); + }) as Lock; const pixTarget = lock.pixTarget; const amount = formatEther(lock.amount); @@ -512,12 +512,12 @@ export const getActiveLockAmount = async ( const lockIds = lockSeller.map((lock: any) => lock.args.lockID); - const lockStatus = await client.readContract({ + const { sortedIDs, status: lockStatus } = await client.readContract({ address, abi, functionName: "getLocksStatus", args: [lockIds], - }); + }) as GetLocksStatusResponse; const mapLocksRequests = lockStatus[0].map((id: bigint) => client.readContract({ diff --git a/src/components/ListingComponent/ListingComponent.vue b/src/components/ListingComponent/ListingComponent.vue index 754d400..9a5e21f 100644 --- a/src/components/ListingComponent/ListingComponent.vue +++ b/src/components/ListingComponent/ListingComponent.vue @@ -174,12 +174,12 @@ showInitialItems(); Saldo disponível

- {{ getRemaining() }} {{ etherStore.selectedToken }} + {{ getRemaining() }} {{ user.networkName.value }}

{{ `com ${activeLockAmount.toFixed(2)} ${ - etherStore.selectedToken + user.networkName.value } em lock` }}