-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-requests.lua
156 lines (128 loc) · 3.92 KB
/
data-requests.lua
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
-- currently unused
local _0RBIT= "WSXUI2JjYUldJ7CKq9wE1MGwXs-ldzlUlHOQszwQe0s"
function simpleTemplate(template, data)
return (template:gsub('($%b{})', function(w) return data[w:sub(3, -2)] or w end))
end
function requestTransactions(first)
-- Ensure minHeight is set to one more than the current max height in Transactions to fetch new transactions
local minHeight = 1
if #Transactions > 0 then
minHeight = Transactions[#Transactions].height
end
local maxHeight = 2147483647 -- max int 32
first = first or 100
local sortParam = 'HEIGHT_ASC' --sortAscending and 'HEIGHT_ASC' or 'HEIGHT_DESC'
local queryTemplate = [[
query {
transactions(tags: [{name: "Action", values: ["Order-Confirmation"]}, {name: "From-Process", values: ["${AMM}"]}], sort: ${sortParam}, first: ${first}, block: {min: ${minHeight}, max: ${maxHeight}}) {
edges {
node {
id
owner {
address
}
block {
height
id
}
tags {
name
value
}
}
}
}
}
]]
local templatedQuery = simpleTemplate(queryTemplate, {
first = tonumber(first),
sortParam = sortParam,
minHeight = minHeight,
maxHeight = maxHeight,
AMM = AMM
})
local message = {
query = templatedQuery,
variables = {}
}
ao.send({
Target = _0RBIT,
Action = "Post-Real-Data",
Url = "https://arweave.net/graphql",
Body = json.encode(message)
})
return templatedQuery
end
function requestBlocks()
local blockIdsSet = {}
local blockIds = {}
-- Collect unique block IDs from transactions where blockTimestamp is nil
for _, transaction in ipairs(Transactions) do
if transaction.blockId and transaction.blockTimestamp == nil and not blockIdsSet[transaction.blockId] then
blockIdsSet[transaction.blockId] = true -- Mark this ID as collected
table.insert(blockIds, '"' .. transaction.blockId .. '"')
-- Stop collecting once we have 100 unique block IDs
if #blockIds >= 100 then
break
end
end
end
-- If there are no block IDs to request, return early
if #blockIds == 0 then
return
end
local queryTemplate = [[
query {
blocks(ids: [${blockIds}]) {
edges {
node {
id
height
timestamp
}
}
}
}
]]
local templatedQuery = simpleTemplate(queryTemplate, {
blockIds = table.concat(blockIds, ', ')
})
local message = {
query = templatedQuery,
variables = {}
}
ao.send({
Target = _0RBIT,
Action = "Post-Real-Data",
Url = "https://arweave.net/graphql",
Body = json.encode(message)
})
end
function updateTransactions(data)
local dataToInsert = {}
for _, edge in ipairs(data) do
local node = edge.node
local nodeFlattened = {
Id = node.id,
['Block-Height'] = node.block.height,
From = node.owner.address,
['Block-Id'] = node.block.id,
Timestamp = nil,
Tags = node.Tags
}
table.insert(dataToInsert, nodeFlattened)
end
insertManyTransactions(dataToInsert)
end
function updateBlockTimestamps(blockData)
for _, edge in ipairs(blockData) do
local blockId = edge.node.id
local blockTimestamp = edge.node.timestamp
-- Iterate over Transactions to find matching blockId and update blockTimestamp
for _, transaction in ipairs(Transactions) do
if transaction.blockId == blockId then
transaction.blockTimestamp = blockTimestamp
end
end
end
end