This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processAndUpload.ts
227 lines (211 loc) · 6.85 KB
/
processAndUpload.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
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
import { getEvents } from './fetch/getEvents'
import { getBalance } from './bundlr/utils/getBalance'
import { getTransactions } from './prisma/getTransactions'
import { prisma } from './prisma/prismaClient'
import {
Transactions,
Node,
} from './interfaces/transactionInterfaces'
import { DatabaseLog } from './types'
import { apolloClient } from './apollo/apolloClient'
import { NEW_TRANSACTIONS_QUERY } from './gql'
async function main() {
await getBalance()
await getTransactions()
const result = await getEvents()
const { cleanedLogs } = result
const { data } = await apolloClient.query({
query: NEW_TRANSACTIONS_QUERY,
variables: { owner: process.env.OWNER },
})
const processedTransactions = processTransactions(data.transactions)
for (const transaction of processedTransactions) {
if (transaction) {
// console.log(`transaction: ${JSON.stringify(transaction, replacer, 2)}`);
await prisma.transaction
.create({
data: {
id: transaction.id,
address: transaction.address,
eventType: transaction.eventType,
tags: transaction.tags,
},
})
.catch((e: Error) =>
console.error('Error upserting transaction:', e.message),
)
}
}
await processCleanedLogs(cleanedLogs)
console.log('Finished processing cleaned logs')
}
function processTransactions(transactions: Transactions) {
const eventTypes = [
'SetupAP721',
'DataStored',
'DataOverwritten',
'DataRemoved',
'LogicUpdated',
'RendererUpdated',
]
return transactions.edges.map((edge) => {
const eventTag = edge.node.tags.find(
(tag) => tag.name === `Database Events - Chain: ${process.env.CHAIN_ID} v0.1`,
)
if (!eventTag || !eventTypes.includes(eventTag.value)) {
return null
}
return shapeData(edge.node)
})
}
function shapeData(node: Node) {
const tags = node.tags.reduce((acc, tag) => {
acc[tag.name] = tag.value
return acc
}, {} as Record<string, string>)
return {
id: node.id,
address: node.address,
eventType: tags[`Database Events - Chain: ${process.env.CHAIN_ID} v0.1`],
tags,
}
}
export async function processCleanedLogs(cleanedLogs: DatabaseLog[]) {
for (const log of cleanedLogs) {
if (!log.args) {
console.log(`Skipping log due to missing args: ${JSON.stringify(log)}`)
continue
}
console.log(`Processing log with event name: ${log.eventName}`)
try {
switch (log.eventName) {
case 'SetupAP721':
console.log('Checking conditions for SetupAP721...')
if (
log.args.ap721 &&
log.args.sender &&
log.args.initialOwner &&
log.args.logic &&
log.args.renderer &&
log.args.factory !== undefined
) {
console.log(
'Conditions met for SetupAP721. Attempting to populate pressSettings table...',
)
await prisma.pressSettings.create({
data: {
sender: log.args.sender,
initialOwner: log.args.initialOwner,
logic: log.args.logic,
renderer: log.args.renderer,
factory: log.args.factory,
eventType: log.eventName,
},
})
console.log('Successfully populated pressSettings table.')
} else {
console.log(
'Conditions for populating pressSettings table not met.',
)
}
break
case 'LogicUpdated':
console.log('Checking conditions for LogicUpdated...')
if (log.args.target && log.args.logic !== undefined) {
console.log(
'Conditions met for LogicUpdated. Attempting to update pressSettings table...',
)
await prisma.pressSettings.create({
data: {
logic: log.args.logic,
target: log.args.target,
eventType: log.eventName,
},
})
console.log(
'Successfully updated pressSettings table for LogicUpdated.',
)
} else {
console.log(
'Conditions for updating pressSettings table not met for LogicUpdated.',
)
}
break
case 'RendererUpdated':
console.log('Checking conditions for RendererUpdated...')
if (log.args.target && log.args.renderer !== undefined) {
console.log(
'Conditions met for RendererUpdated. Attempting to update pressSettings table...',
)
await prisma.pressSettings.create({
data: {
renderer: log.args.renderer,
target: log.args.target,
eventType: log.eventName,
},
})
console.log(
'Successfully updated pressSettings table for RendererUpdated.',
)
} else {
console.log(
'Conditions for updating pressSettings table not met for RendererUpdated.',
)
}
break
case 'DataStored':
case 'DataOverwritten':
console.log('Checking conditions for DataStored/DataOverwritten...')
if (
log.args.target &&
log.args.sender &&
log.args.tokenId &&
log.args.pointer
) {
console.log(
'Conditions met for DataStored/DataOverwritten. Attempting to populate tokenStorage table...',
)
await prisma.tokenStorage.upsert({
where: {
target_sender_tokenId: {
target: log.args.target,
sender: log.args.sender,
tokenId: log.args.tokenId,
},
},
update: {
target: log.args.target,
sender: log.args.sender,
pointer: log.args.pointer,
eventType: log.eventName,
},
create: {
target: log.args.target,
sender: log.args.sender,
tokenId: log.args.tokenId,
pointer: log.args.pointer,
eventType: log.eventName,
},
})
console.log('Successfully populated tokenStorage table.')
} else {
console.log('Conditions for populating tokenStorage table not met.')
}
break
default:
console.log(`Unknown event type: ${log.eventName}`)
}
} catch (e) {
console.error(`Error processing event ${log.eventName}:`, e)
}
}
}
main()
.catch((e) => {
console.error('Error running main: ', e)
throw e
})
.finally(async () => {
console.log('Finished all Prisma operations, disconnecting...')
await prisma.$disconnect()
})