Fix issues with locking and solicitation.

This commit is contained in:
Filipe Soccol
2025-06-27 15:59:34 -03:00
parent 73ba77ca4f
commit cf61f5ecfd
16 changed files with 202 additions and 189 deletions

View File

@@ -1,17 +1,14 @@
import { decodeEventLog, formatEther, type Log } from "viem";
import { formatEther } from "viem";
import { useUser } from "@/composables/useUser";
import { getPublicClient, getWalletClient, getContract } from "./provider";
import { getTokenAddress } from "./addresses";
import p2pix from "@/utils/smart_contract_files/P2PIX.json";
import { getValidDeposits } from "./events";
import type { ValidDeposit } from "@/model/ValidDeposit";
import type { WalletTransaction } from "@/model/WalletTransaction";
import type { UnreleasedLock } from "@/model/UnreleasedLock";
import type { Pix } from "@/model/Pix";
import { getNetworkSubgraphURL } from "@/model/NetworkEnum";
export const updateWalletStatus = async (): Promise<void> => {
@@ -55,54 +52,12 @@ export const listValidDepositTransactionsByWalletAddress = async (
const getLockStatus = async (id: bigint): Promise<number> => {
const { address, abi, client } = await getContract();
const result = await client.readContract({
address,
address: address as `0x${string}`,
abi,
functionName: "getLocksStatus",
args: [[id]],
});
return result[1][0];
};
const filterLockStatus = async (
transactions: Log[]
): Promise<WalletTransaction[]> => {
const txs: WalletTransaction[] = [];
for (const transaction of transactions) {
try {
const decoded = decodeEventLog({
abi: p2pix.abi,
data: transaction.data,
topics: transaction.topics,
});
if (!decoded || !decoded.args) continue;
// Type assertion to handle the args safely
const args = decoded.args as Record<string, any>;
const tx: WalletTransaction = {
token: args.token ? String(args.token) : "",
blockNumber: Number(transaction.blockNumber),
amount: args.amount ? Number(formatEther(args.amount)) : -1,
seller: args.seller ? String(args.seller) : "",
buyer: args.buyer ? String(args.buyer) : "",
event: decoded.eventName || "",
lockStatus:
decoded.eventName == "LockAdded" && args.lockID
? await getLockStatus(args.lockID)
: -1,
transactionHash: transaction.transactionHash
? transaction.transactionHash
: "",
transactionID: args.lockID ? args.lockID.toString() : "",
};
txs.push(tx);
} catch (error) {
console.error("Error decoding log", error);
}
}
return txs;
return (result as any)[1][0] as number;
};
export const listAllTransactionByWalletAddress = async (
@@ -118,6 +73,7 @@ export const listAllTransactionByWalletAddress = async (
query: `
{
depositAddeds(where: {seller: "${walletAddress.toLowerCase()}"}) {
id
seller
token
amount
@@ -129,7 +85,6 @@ export const listAllTransactionByWalletAddress = async (
buyer
lockID
seller
token
amount
blockTimestamp
blockNumber
@@ -138,7 +93,6 @@ export const listAllTransactionByWalletAddress = async (
lockReleaseds(where: {buyer: "${walletAddress.toLowerCase()}"}) {
buyer
lockId
e2eId
blockTimestamp
blockNumber
transactionHash
@@ -155,7 +109,6 @@ export const listAllTransactionByWalletAddress = async (
`,
};
console.log("Fetching transactions from subgraph");
const response = await fetch(getNetworkSubgraphURL(network), {
method: "POST",
headers: {
@@ -165,14 +118,12 @@ export const listAllTransactionByWalletAddress = async (
});
const data = await response.json();
console.log("Subgraph data fetched:", data);
console.log("Subgraph data:", data);
// Convert all transactions to common WalletTransaction format
const transactions: WalletTransaction[] = [];
// Process deposit added events
if (data.data?.depositAddeds) {
console.log("Processing deposit events");
for (const deposit of data.data.depositAddeds) {
transactions.push({
token: deposit.token,
@@ -189,7 +140,6 @@ export const listAllTransactionByWalletAddress = async (
// Process lock added events
if (data.data?.lockAddeds) {
console.log("Processing lock events");
for (const lock of data.data.lockAddeds) {
// Get lock status from the contract
const lockStatus = await getLockStatus(BigInt(lock.lockID));
@@ -210,7 +160,6 @@ export const listAllTransactionByWalletAddress = async (
// Process lock released events
if (data.data?.lockReleaseds) {
console.log("Processing release events");
for (const release of data.data.lockReleaseds) {
transactions.push({
token: "", // Subgraph doesn't provide token in this event, we could enhance this later
@@ -228,7 +177,6 @@ export const listAllTransactionByWalletAddress = async (
// Process deposit withdrawn events
if (data.data?.depositWithdrawns) {
console.log("Processing withdrawal events");
for (const withdrawal of data.data.depositWithdrawns) {
transactions.push({
token: withdrawal.token,
@@ -325,7 +273,6 @@ const listLockTransactionByWalletAddress = async (walletAddress: string) => {
buyer
lockID
seller
token
amount
blockTimestamp
blockNumber
@@ -386,7 +333,6 @@ const listLockTransactionByWalletAddress = async (walletAddress: string) => {
const listLockTransactionBySellerAddress = async (sellerAddress: string) => {
const user = useUser();
const network = user.networkName.value;
console.log("Will get locks as seller", sellerAddress);
// Query subgraph for lock added transactions where seller matches
const subgraphQuery = {
@@ -459,10 +405,6 @@ export const checkUnreleasedLock = async (
walletAddress: string
): Promise<UnreleasedLock | undefined> => {
const { address, abi, client } = await getContract();
const pixData: Pix = {
pixKey: "",
};
const addedLocks = await listLockTransactionByWalletAddress(walletAddress);
if (!addedLocks.length) return undefined;
@@ -470,34 +412,43 @@ export const checkUnreleasedLock = async (
const lockIds = addedLocks.map((lock: any) => lock.args.lockID);
const lockStatus = await client.readContract({
address,
address: address as `0x${string}`,
abi,
functionName: "getLocksStatus",
args: [lockIds],
});
const unreleasedLockId = lockStatus[1].findIndex(
const lockStatusResult = lockStatus as [bigint[], number[]];
const unreleasedLockId = lockStatusResult[1].findIndex(
(status: number) => status == 1
);
if (unreleasedLockId !== -1) {
const lockID = lockStatus[0][unreleasedLockId];
const lockID = lockStatusResult[0][unreleasedLockId];
const lock = await client.readContract({
address,
address: address as `0x${string}`,
abi,
functionName: "mapLocks",
args: [lockID],
});
const pixTarget = lock.pixTarget;
const amount = formatEther(lock.amount);
pixData.pixKey = pixTarget;
pixData.value = Number(amount);
const lockData = lock as [
bigint,
string,
string,
bigint,
string,
string,
string
];
const amount = formatEther(lockData[0]);
return {
lockID,
pix: pixData,
lockID: lockID.toString(),
amount: Number(amount),
sellerAddress: lockData[1] as `0x${string}`,
tokenAddress: lockData[4] as `0x${string}`,
};
}
};
@@ -513,15 +464,16 @@ export const getActiveLockAmount = async (
const lockIds = lockSeller.map((lock: any) => lock.args.lockID);
const lockStatus = await client.readContract({
address,
address: address as `0x${string}`,
abi,
functionName: "getLocksStatus",
args: [lockIds],
});
const mapLocksRequests = lockStatus[0].map((id: bigint) =>
const lockStatusResult = lockStatus as [bigint[], number[]];
const mapLocksRequests = lockStatusResult[0].map((id: bigint) =>
client.readContract({
address,
address: address as `0x${string}`,
abi,
functionName: "mapLocks",
args: [id],
@@ -533,9 +485,24 @@ export const getActiveLockAmount = async (
});
return mapLocksResults.reduce((total: number, lock: any, index: number) => {
if (lockStatus[1][index] === 1) {
if (lockStatusResult[1][index] === 1) {
return total + Number(formatEther(lock.amount));
}
return total;
}, 0);
};
export const getSellerParticipantId = async (
sellerAddress: string,
tokenAddress: string
): Promise<string> => {
const { address, abi, client } = await getContract();
const participantId = await client.readContract({
address: address as `0x${string}`,
abi,
functionName: "getPixTarget",
args: [sellerAddress, tokenAddress],
});
return participantId as string;
};