Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions scripts/update_log_index_values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import "dotenv/config";

import {
HypercertExchangeAbi,
getHypercertTokenId,
parseClaimOrFractionId,
} from "@hypercerts-org/sdk";
import { createClient } from "@supabase/supabase-js";
import { Chain, erc20Abi, getAddress, parseEventLogs, zeroAddress } from "viem";
import {
arbitrum,
arbitrumSepolia,
base,
baseSepolia,
celo,
filecoin,
filecoinCalibration,
optimism,
sepolia,
} from "viem/chains";
import { EvmClientFactory } from "../src/clients/evmClient.js";
import { TakerBid } from "../src/storage/storeTakerBid.js";
import { getDeployment } from "../src/utils/getDeployment.js";

const getChain = (chainId: number) => {
const chains: Record<number, Chain> = {
10: optimism,
314: filecoin,
8453: base,
42161: arbitrum,
42220: celo,
84532: baseSepolia,
314159: filecoinCalibration,
421614: arbitrumSepolia,
11155111: sepolia,
};

const chain = chains[chainId];
if (!chain) throw new Error(`Unsupported chain ID: ${chainId}`);
return chain;
};

const main = async () => {
console.log("update_log_index_values");
// Get all sales rows
// Create supabase client
const supabase = createClient(
process.env.SUPABASE_CACHING_DB_URL!,
process.env.SUPABASE_CACHING_SERVICE_API_KEY!,
);
const salesResponse = await supabase
.from("sales")
.select("*")
.filter("log_index", "is", null);
const sales = salesResponse.data;

if (!sales) {
console.log("No sales found");
return;
}

const results: {
id: string;
log_index: number;
transaction_hash: string;
chain_id: number;
}[] = [];
for (const sale of sales) {
const chainId = parseClaimOrFractionId(sale.hypercert_id).chainId;

if (!chainId) {
throw new Error(
`No chainId found for sale ${sale.transaction_hash} ${sale.hypercert_id}`,
);
}

// Get transaction and parse logs using viem
const client = EvmClientFactory.createClient(Number(chainId));
const { addresses } = getDeployment(Number(chainId));

try {
const transactionReceipt = await client.getTransactionReceipt({
hash: sale.transaction_hash as `0x${string}`,
});
const exchangeLogs = transactionReceipt.logs.filter(
(log) =>
log.address.toLowerCase() ===
addresses?.HypercertExchange?.toLowerCase(),
);

const parsedExchangeLog = parseEventLogs({
abi: HypercertExchangeAbi,
logs: exchangeLogs,
// @ts-expect-error eventName is missing in the type
}).find((log) => log.eventName === "TakerBid");

if (parsedExchangeLog?.logIndex === undefined) {
throw new Error(
`No log index found for sale ${sale.transaction_hash} ${sale.hypercert_id}`,
);
}
results.push({
id: sale.id,
log_index: parsedExchangeLog?.logIndex,
transaction_hash: sale.transaction_hash,
chain_id: chainId,
});
} catch (e) {
console.log("Error parsing transaction", JSON.stringify(sale, null, 2));
console.log(e);
continue;
}
}

console.log("Results");
console.log(JSON.stringify(results, null, 2));

for (const result of results) {
const res = await supabase
.from("sales")
.update({
log_index: result.log_index,
})
.eq("id", result.id);
console.log("--------------------------------");
console.log("Updating log index for sale", result.id);
console.log(res);
}
};

main();
50 changes: 11 additions & 39 deletions src/parsing/parseTakerBidEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { ParserMethod } from "@/indexer/LogParser.js";
import { TakerBid } from "@/storage/storeTakerBid.js";
import { getDeployment } from "@/utils/getDeployment.js";
import { messages } from "@/utils/validation.js";
import { HypercertExchangeAbi, HypercertMinterAbi, getHypercertTokenId } from "@hypercerts-org/sdk";
import {
HypercertExchangeAbi,
HypercertMinterAbi,
getHypercertTokenId,
} from "@hypercerts-org/sdk";
import {
erc20Abi,
getAddress,
Expand Down Expand Up @@ -75,6 +79,7 @@ export const TakerBidEventSchema = z.object({
}),
blockNumber: z.coerce.bigint(),
transactionHash: z.string(),
logIndex: z.number().int(),
});

export const parseTakerBidEvent: ParserMethod<TakerBid> = async ({
Expand All @@ -83,7 +88,6 @@ export const parseTakerBidEvent: ParserMethod<TakerBid> = async ({
}) => {
const { addresses } = getDeployment(Number(chain_id));
const client = getEvmClient(Number(chain_id));

try {
const bid = TakerBidEventSchema.parse(event);

Expand Down Expand Up @@ -114,19 +118,7 @@ export const parseTakerBidEvent: ParserMethod<TakerBid> = async ({
(log) => log.eventName === "TransferSingle",
);

// Get the claim ID from either event type
let claimId;
// @ts-expect-error args is missing in the type
if (batchValueTransferLog?.args?.claimIDs?.[0]) {
// @ts-expect-error args is missing in the type
claimId = batchValueTransferLog.args.claimIDs[0];
// @ts-expect-error args is missing in the type
} else if (transferSingleLog?.args?.id) {
// In this case, the ID from the transferSingleLog is a fraction token ID
// We need to get the claim ID from the fraction token ID
// @ts-expect-error args is missing in the type
claimId = getHypercertTokenId(transferSingleLog.args.id);
}
const claimId = getHypercertTokenId(bid.params.itemIds[0]);

if (!claimId) {
throw new Error(
Expand All @@ -136,30 +128,9 @@ export const parseTakerBidEvent: ParserMethod<TakerBid> = async ({

const hypercertId = `${chain_id}-${getAddress(bid.params?.collection)}-${claimId}`;

let currencyAmount = 0n;
const currency = getAddress(bid.params.currency);
if (currency === zeroAddress) {
// Get value of the transaction
const transaction = await client.getTransaction({
hash: bid.transactionHash as `0x${string}`,
});
currencyAmount = transaction.value;
} else {
const currencyLogs = transactionReceipt.logs.filter(
(log) => log.address.toLowerCase() === currency.toLowerCase(),
);
const parsedCurrencyLogs = parseEventLogs({
abi: erc20Abi,
logs: currencyLogs,
});
const transferLogs = parsedCurrencyLogs.filter(
(log) => log.eventName === "Transfer",
);
currencyAmount = transferLogs.reduce(
(acc, transferLog) => acc + (transferLog?.args?.value ?? 0n),
0n,
);
}
const currencyAmount = bid.params.feeAmounts.reduce((acc, amount) => {
return acc + amount;
}, 0n);

const exchangeLogs = transactionReceipt.logs.filter(
(log) =>
Expand Down Expand Up @@ -192,6 +163,7 @@ export const parseTakerBidEvent: ParserMethod<TakerBid> = async ({
currency_amount: currencyAmount,
fee_amounts: fee_amounts,
fee_recipients: fee_recipients,
log_index: bid.logIndex,
}),
];
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions src/storage/storeTakerBid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const TakerBid = z.object({
fee_recipients: z.array(
z.string().refine(isAddress, { message: "Invalid fee recipient address" }),
),
log_index: z.number().int(),
});

export type TakerBid = z.infer<typeof TakerBid>;
Expand Down
Loading
Loading