-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetRigsToPark.ts
117 lines (107 loc) · 2.96 KB
/
getRigsToPark.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { network, rigsDeployment } from "hardhat";
import { Database } from "@tableland/sdk";
const db = new Database();
type Rig = {
id: string;
permalink: string;
bundle_item_number: string | null;
listing_timestamp: string;
expiration_timestamp: string | null;
seller_address: string;
auction_type: string | null;
quantity: number;
quantity_remaining: number;
price: number;
marketplace_id: string;
order_hash: string | null;
collection_id: string;
nft_id: string; // Token ID as suffix: `ethereum.0x8eaa9ae1ac89b1c8c8a8104d08c045f78aadb42d.2359`
payment_token: {
payment_token_id: string;
name: string;
symbol: string;
address: string | null;
decimals: number;
};
is_private: boolean;
};
type Res = {
next_cursor: string | null;
next: string | null;
previous: string | null;
listings: Rig[];
};
async function getInFlight(rigIds: number[]): Promise<number[]> {
// Get the sessions where end_time is null, there should only ever be one of these
const res = await db
.prepare(
`SELECT rig_id FROM ${
rigsDeployment.pilotSessionsTable
} WHERE rig_id in (${rigIds.join(",")}) AND end_time is null;`
)
.all<{ rig_id: number }>();
const ids: number[] = [];
(res && res.results ? res.results : []).forEach((i) => {
ids.push(i.rig_id);
});
return ids;
}
async function getListedRigs(nextCursor?: string): Promise<Res> {
const params = new URLSearchParams({
limit: "50",
});
if (nextCursor) {
params.append("cursor", nextCursor);
}
const req = await fetch(
`https://api.simplehash.com/api/v0/nfts/listings/ethereum/${rigsDeployment.contractAddress}?` +
params,
{
headers: {
accept: "application/json",
"X-API-KEY": process.env.SIMPLEHASH_API_KEY!,
},
}
);
const res: Res = await req.json();
return res;
}
async function main() {
console.log(
`\nGetting listed rigs that are in-flight on '${network.name}'...`
);
// Get contract address
if (rigsDeployment.contractAddress === "") {
throw Error(`no contractAddress entry for '${network.name}'`);
}
console.log(`Using address '${rigsDeployment.contractAddress}'`);
let listed: Rig[] = [];
let nextCursor: string | null = null;
do {
let res: Res;
if (!nextCursor) {
res = await getListedRigs();
} else {
res = await getListedRigs(nextCursor);
}
listed = [...listed, ...(res.listings || [])];
nextCursor = res.next_cursor;
} while (nextCursor);
// Check if Rigs are in-flight
const ids = listed.map((l) => {
// Parse tokenId from nft_id (e.g. `2359` from `ethereum.<contract>.2359`)
const tokenId = l.nft_id.split(".")[2];
return parseInt(tokenId);
});
const toPark = await getInFlight(ids);
console.log(
`----\nForce park Rigs:\n`,
`IDs: ${toPark.length > 0 ? toPark.join(",") : "none"}\n`
);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});