forked from Jonny-Ringo/the_eye_of_AO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocesses.js
More file actions
306 lines (274 loc) · 10.6 KB
/
processes.js
File metadata and controls
306 lines (274 loc) · 10.6 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* Process definitions and query generation for Eye of AO dashboard
*/
// Define processes with their query parameters
export const PROCESSES = {
permaswap: {
description: "Permaswap Order Notice Processes",
protocol: "ao",
action: "Order-Notice",
spawnerProcess: "5G5_ftQT6f2OsmJ8EZ4-84eRcIMNEmUyH9aQSD85f9I",
defaultAddresses: [
"xZwIYa2DapmKmOpqOn9iMN0YQnYV4hgtwKadiKBpbt8",
"SMKH5JnFE7c0MjsURMVRZn7kUerg1yMwcnVbWJJBEDU",
"tnzfEWXA9CRxr9lBGZbZfVEZux44lZj3pqMJCK5cHgc",
"dBbZhQoV4Lq9Bzbm0vlTrHmOZT7NchC_Dillbmqx0tM",
"vJY-ed1Aoa0pGgQ30BcpO9ehGBu1PfNHUlwV9W8_n5A",
"-9lYCEgMbASuQMr76ddhnaT3H996UFjMPc5jOs3kiAk",
"qhMOXu9ANdOmOE38fHC3PnJuRsAQ6JzGFNq09oBSmpM",
"7AOIMfTZVpX52-XYBDS7VHsXdqEYYsGdYND_MoEVEwg",
]
},
botega: {
description: "Botega Order Confirmation Processes",
protocol: "ao",
action: "Order-Confirmation",
spawnerProcess: "3XBGLrygs11K63F_7mldWz4veNx6Llg6hI2yZs8LKHo",
defaultAddresses: []
},
wARTransfer: {
description: "wAR Token Transfer",
fromProcess: ["xU9zFkq3X2ZQ6olwNVvr1vUWIjc3kXTWr7xKQD6dh10"],
action: "Credit-Notice",
displayName: "wAR Transfers"
},
wARweeklyTransfer: {
description: "wAR Weekly Token Transfer",
fromProcess: ["xU9zFkq3X2ZQ6olwNVvr1vUWIjc3kXTWr7xKQD6dh10"],
action: "Credit-Notice",
displayName: "wAR Weekly Transfers"
},
wUSDCTransfer: {
description: "wUSDC Token Transfer",
fromProcess: ["7zH9dlMNoxprab9loshv3Y7WG45DOny_Vrq9KrXObdQ"],
action: "Credit-Notice",
displayName: "wUSDC Transfers"
},
USDATransfer: {
description: "USDA Token Transfer",
fromProcess: ["o2eqRo_mwJrvToPyeZf7igiJOJz-efODYq3m6ufb4HM"],
action: "Credit-Notice",
displayName: "USDA Transfers"
},
AOTransfer: {
description: "AO Token Transfer",
fromProcess: ["0syT13r0s0tgPmIed95bJnuSqaD29HQNN8D3ElLSrsc"],
action: "Credit-Notice",
displayName: "AO Transfers"
},
wARTotalSupply: {
description: "wAR Total Supply",
wARProcess: "Bi6bSPz-IyOCX9ZNedmLzv7Z6yxsrj9nHE1TnZzm_ks",
action: "SupplyHistory"
},
llamaLand: {
description: "LlamaLand Login Info",
fromProcess: ["2dFSGGlc5xJb0sWinAnEFHM-62tQEbhDzi1v5ldWX5k"],
action: "Login-Info",
message: "No Reward",
displayName: "LlamaLand"
}
};
// Cache for process addresses to avoid repeated fetching
const processAddressCache = new Map();
/**
* Fetches process addresses from the network
* @param {string} spawnerProcess - The spawner process ID
* @param {Array} defaultAddresses - Default addresses to use if fetch fails
* @returns {Promise<Array>} Array of process addresses
*/
async function fetchProcessAddresses(spawnerProcess, defaultAddresses = []) {
// Check cache first
const cacheKey = `process-addresses-${spawnerProcess}`;
if (processAddressCache.has(cacheKey)) {
return processAddressCache.get(cacheKey);
}
const buildQuery = (cursor) => `
query {
transactions(
block: {min: 0}
tags: [
{ name: "From-Process", values: "${spawnerProcess}" }
{ name: "Action", values: "Spawned" }
],
first: 100,
after: ${cursor ? `"${cursor}"` : null}
) {
edges {
node {
id
tags {
name
value
}
}
cursor
}
pageInfo {
hasNextPage
}
}
}
`;
try {
const processAddresses = new Set();
let hasNextPage = true;
let cursor = null;
let pageCount = 0;
const MAX_PAGES = 10; // Limit pages to avoid excessive queries
console.log(`Fetching process addresses for spawner ${spawnerProcess}...`);
// Loop through pages until no more results or max pages reached
while (hasNextPage && pageCount < MAX_PAGES) {
pageCount++;
const response = await fetch('https://arweave-search.goldsky.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: buildQuery(cursor) }),
});
if (!response.ok) {
throw new Error(`Network error: ${response.status}`);
}
const result = await response.json();
if (result.errors) {
console.error('GraphQL errors:', result.errors);
break;
}
const transactions = result.data.transactions;
// Process current page
transactions.edges.forEach((edge) => {
if (!edge.node.tags) return;
const processTags = edge.node.tags.filter((tag) => tag.name === "Process");
processTags.forEach((processTag) => {
processAddresses.add(processTag.value);
});
});
// Check if there's another page
hasNextPage = transactions.pageInfo.hasNextPage;
if (hasNextPage && transactions.edges.length > 0) {
cursor = transactions.edges[transactions.edges.length - 1].cursor;
} else {
hasNextPage = false;
}
}
// Add default addresses
defaultAddresses.forEach((address) => processAddresses.add(address));
const processList = Array.from(processAddresses);
// Cache the result for future use
processAddressCache.set(cacheKey, processList);
console.log(`Found ${processList.length} processes for spawner ${spawnerProcess}`);
return processList;
} catch (error) {
console.error(`Error fetching process addresses for ${spawnerProcess}:`, error);
// Return default addresses on error
console.log(`Using ${defaultAddresses.length} default addresses for ${spawnerProcess}`);
processAddressCache.set(cacheKey, defaultAddresses);
return defaultAddresses;
}
}
/**
* Generates a GraphQL query for a specific process and block range
* @param {string} processType - The process type
* @param {number} startHeight - The starting block height
* @param {number} endHeight - The ending block height
* @param {number} currentHeight - The current block height
* @returns {Promise<string>} The generated GraphQL query
*/
export async function generateQuery(processType, startHeight, endHeight, currentHeight) {
const process = PROCESSES[processType];
if (!process) {
throw new Error(`Unknown process type: ${processType}`);
}
// If endHeight is current block height, only use min for live data
const blockRange = endHeight === currentHeight
? `block: { min: ${startHeight} }`
: `block: { min: ${startHeight}, max: ${endHeight} }`;
switch(processType) {
case 'permaswap': {
// Fetch addresses for Permaswap
const addresses = await fetchProcessAddresses(
process.spawnerProcess,
process.defaultAddresses
);
return `query {
transactions (
${blockRange}
tags: [
{ name: "Data-Protocol", values: ["${process.protocol}"] }
{ name: "Action", values: ["${process.action}"] }
{ name: "From-Process", values: ${JSON.stringify(addresses)} }
]
) {
count
}
}`;
}
case 'botega': {
// Fetch addresses for Botega
const addresses = await fetchProcessAddresses(
process.spawnerProcess,
process.defaultAddresses
);
return `query {
transactions (
${blockRange}
tags: [
{ name: "Data-Protocol", values: ["${process.protocol}"] }
{ name: "Action", values: ["${process.action}"] }
{ name: "From-Process", values: ${JSON.stringify(addresses)} }
]
) {
count
}
}`;
}
case 'wARTransfer':
case 'wARweeklyTransfer':
case 'wUSDCTransfer':
case 'USDATransfer':
case 'AOTransfer':
return `query {
transactions (
${blockRange}
tags:[
{ name:"Action", values: ["${process.action}"]},
{ name: "From-Process", values: ${JSON.stringify(process.fromProcess)}},
]
) {
count
}
}`;
case 'llamaLand':
return `query {
transactions(
${blockRange}
tags: [
{ name: "From-Process", values: ${JSON.stringify(process.fromProcess)}}
{ name: "Action", values: "${process.action}" },
{ name: "Message", values: "${process.message}" },
],
sort: HEIGHT_DESC
) {
count
}
}`;
default:
throw new Error(`Query template not found for process type: ${processType}`);
}
}
/**
* Gets the display name for a process
* @param {string} processName - The process name
* @returns {string} The display name for the process
*/
export function getProcessDisplayName(processName) {
const process = PROCESSES[processName];
return process?.displayName || processName;
}
/**
* Clears the process address cache
*/
export function clearProcessAddressCache() {
processAddressCache.clear();
}