-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_unit_values.ts
99 lines (86 loc) · 2.57 KB
/
check_unit_values.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* This script checks the units of all fractions in the database against the units on-chain.
*/
import * as dotenv from "dotenv";
dotenv.config();
import { createClient } from "@supabase/supabase-js";
import { Database } from "@/types/database.types";
import { createPublicClient, http } from "viem";
import { sepolia } from "viem/chains";
const main = async () => {
const supabase = createClient<Database>(
process.env.SUPABASE_CACHING_DB_URL!,
process.env.SUPABASE_CACHING_SERVICE_API_KEY!,
);
const { count } = await supabase
.from("fractions")
.select("id", { count: "exact", head: true });
console.log(count);
if (!count) {
throw new Error("No fractions found in the database");
}
const pageSize = 100;
for (let i = 0; i < count; i += pageSize) {
const { data: fractions } = await supabase
.from("fractions")
.select("token_id::text, units::text")
.order("creation_block_timestamp", { ascending: true })
.range(i, i + pageSize - 1);
if (!fractions) {
throw new Error("No fractions found in the database");
}
console.log(fractions);
const client = createPublicClient({
chain: sepolia,
transport: http(
`https://eth-sepolia.g.alchemy.com/v2/${process.env["ALCHEMY_API_KEY"]}`,
{
timeout: 20_000,
},
),
batch: {
multicall: {
wait: 32,
},
},
});
await Promise.all(
fractions.map(async (fraction) => {
const units = await client.readContract({
address: "0xa16DFb32Eb140a6f3F2AC68f41dAd8c7e83C4941",
abi: [
{
inputs: [
{
internalType: "uint256",
name: "tokenID",
type: "uint256",
},
],
name: "unitsOf",
outputs: [
{
internalType: "uint256",
name: "units",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
],
functionName: "unitsOf",
args: [fraction.token_id],
});
if (units.toString() !== fraction.units.toString()) {
throw new Error(
`Units of token ${fraction.token_id} in the database do not match the units on-chain. DB: ${fraction.units}, Chain: ${units}. Range: ${i} - ${i + pageSize - 1}`,
);
}
}),
);
}
console.log("🚀 Units indexed correctly!");
process.exit();
};
main();