Skip to content

Commit bfd0d98

Browse files
committed
feat(log-indexing): implement script to update log index values in sales table
- Added a new script to fetch sales with null log indices and update them based on transaction receipts. - Integrated Supabase client to interact with the sales table and ensure log indices are populated. - Created a migration to enforce non-null constraints on the log_index column in the sales table.
1 parent 6a2bb38 commit bfd0d98

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

scripts/update_log_index_values.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import "dotenv/config";
2+
3+
import {
4+
HypercertExchangeAbi,
5+
getHypercertTokenId,
6+
parseClaimOrFractionId,
7+
} from "@hypercerts-org/sdk";
8+
import { createClient } from "@supabase/supabase-js";
9+
import { Chain, erc20Abi, getAddress, parseEventLogs, zeroAddress } from "viem";
10+
import {
11+
arbitrum,
12+
arbitrumSepolia,
13+
base,
14+
baseSepolia,
15+
celo,
16+
filecoin,
17+
filecoinCalibration,
18+
optimism,
19+
sepolia,
20+
} from "viem/chains";
21+
import { EvmClientFactory } from "../src/clients/evmClient.js";
22+
import { TakerBid } from "../src/storage/storeTakerBid.js";
23+
import { getDeployment } from "../src/utils/getDeployment.js";
24+
25+
const getChain = (chainId: number) => {
26+
const chains: Record<number, Chain> = {
27+
10: optimism,
28+
314: filecoin,
29+
8453: base,
30+
42161: arbitrum,
31+
42220: celo,
32+
84532: baseSepolia,
33+
314159: filecoinCalibration,
34+
421614: arbitrumSepolia,
35+
11155111: sepolia,
36+
};
37+
38+
const chain = chains[chainId];
39+
if (!chain) throw new Error(`Unsupported chain ID: ${chainId}`);
40+
return chain;
41+
};
42+
43+
const main = async () => {
44+
console.log("update_log_index_values");
45+
// Get all sales rows
46+
// Create supabase client
47+
const supabase = createClient(
48+
process.env.SUPABASE_CACHING_DB_URL!,
49+
process.env.SUPABASE_CACHING_SERVICE_API_KEY!,
50+
);
51+
const salesResponse = await supabase
52+
.from("sales")
53+
.select("*")
54+
.filter("log_index", "is", null);
55+
const sales = salesResponse.data;
56+
57+
if (!sales) {
58+
console.log("No sales found");
59+
return;
60+
}
61+
62+
const results: {
63+
id: string;
64+
log_index: number;
65+
transaction_hash: string;
66+
chain_id: number;
67+
}[] = [];
68+
for (const sale of sales) {
69+
const chainId = parseClaimOrFractionId(sale.hypercert_id).chainId;
70+
71+
if (!chainId) {
72+
throw new Error(
73+
`No chainId found for sale ${sale.transaction_hash} ${sale.hypercert_id}`,
74+
);
75+
}
76+
77+
// Get transaction and parse logs using viem
78+
const client = EvmClientFactory.createClient(Number(chainId));
79+
const { addresses } = getDeployment(Number(chainId));
80+
81+
try {
82+
const transactionReceipt = await client.getTransactionReceipt({
83+
hash: sale.transaction_hash as `0x${string}`,
84+
});
85+
const exchangeLogs = transactionReceipt.logs.filter(
86+
(log) =>
87+
log.address.toLowerCase() ===
88+
addresses?.HypercertExchange?.toLowerCase(),
89+
);
90+
91+
const parsedExchangeLog = parseEventLogs({
92+
abi: HypercertExchangeAbi,
93+
logs: exchangeLogs,
94+
// @ts-expect-error eventName is missing in the type
95+
}).find((log) => log.eventName === "TakerBid");
96+
97+
if (parsedExchangeLog?.logIndex === undefined) {
98+
throw new Error(
99+
`No log index found for sale ${sale.transaction_hash} ${sale.hypercert_id}`,
100+
);
101+
}
102+
results.push({
103+
id: sale.id,
104+
log_index: parsedExchangeLog?.logIndex,
105+
transaction_hash: sale.transaction_hash,
106+
chain_id: chainId,
107+
});
108+
} catch (e) {
109+
console.log("Error parsing transaction", JSON.stringify(sale, null, 2));
110+
console.log(e);
111+
continue;
112+
}
113+
}
114+
115+
console.log("Results");
116+
console.log(JSON.stringify(results, null, 2));
117+
118+
for (const result of results) {
119+
const res = await supabase
120+
.from("sales")
121+
.update({
122+
log_index: result.log_index,
123+
})
124+
.eq("id", result.id);
125+
console.log("--------------------------------");
126+
console.log("Updating log index for sale", result.id);
127+
console.log(res);
128+
}
129+
};
130+
131+
main();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
alter table sales alter column log_index set not null;

0 commit comments

Comments
 (0)