-
Notifications
You must be signed in to change notification settings - Fork 1
/
atomic-sales-tracehandler.js
343 lines (296 loc) · 14.4 KB
/
atomic-sales-tracehandler.js
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const {Api, JsonRpc, Serialize} = require('eosjs');
const fetch = require('node-fetch');
const { TextDecoder, TextEncoder } = require('text-encoding');
class TraceHandler {
constructor({config}) {
this.config = config;
this.notify = [];
this.eos_rpc = new JsonRpc(config.eos.endpoint, {fetch});
this.eos_api = new Api({ rpc: this.eos_rpc, signatureProvider:null, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });
}
add_sale_notify(handler){
this.notify.push(handler);
}
async get_atomic_data(owner, asset_id) {
const url = `${this.config.atomic_endpoint}/atomicassets/v1/assets?owner=${owner}&ids=${asset_id}&page=1&limit=1`;
const res = await fetch(url);
const json = await res.json();
if (!json.data){
console.error(`Failed to get data for ${asset_id}`, json);
return;
}
// console.log(json);
return json.data[0];
}
async get_simple_data(owner, asset_id) {
const res = await this.eos_rpc.get_table_rows({code: 'simpleassets', scope: owner, table: 'sassets', lower_bound: asset_id, upper_bound: asset_id, limit: 1});
if (res.rows.length){
const asset = res.rows[0];
let mdata = {}, idata = {};
if (asset.mdata){
mdata = JSON.parse(asset.mdata);
}
if (asset.idata){
idata = JSON.parse(asset.idata);
}
asset.data = Object.assign(mdata, idata);
return asset;
}
return null;
}
async process_atomic(buyer, seller, quantity, asset_id, block_num, block_timestamp, retries = 0) {
if (retries >= 10){
console.log(`Giving up on ${asset_id}`);
return;
}
// console.log(`${buyer} paid ${quantity} to ${seller} for ${asset_id}`);
const asset = await this.get_atomic_data(buyer, asset_id);
// console.log(asset);
if (!asset){
retries++;
// console.log(`Couldnt get asset data`);
setTimeout(() => {
this.process_atomic(buyer, seller, quantity, asset_id, block_num, block_timestamp, retries);
}, 3000);
return;
}
this.notify.forEach((n) => {
n.sale('atomic', buyer, seller, quantity, asset, block_num, block_timestamp);
});
}
async process_myth(asset, block_num, block_timestamp){
// console.log(asset);
const asset_data = JSON.parse(asset.mdata);
asset_data.asset_id = asset.assetid;
asset_data.category = asset.category;
asset_data.author = asset.author;
// console.log(`${asset.buyer} paid ${asset.price} to ${asset.seller} for ${asset.assetid}`);
this.notify.forEach((n) => {
n.sale('myth', asset.buyer, asset.seller, asset.price, asset_data, block_num, block_timestamp);
});
}
async process_simple(data, block_num, block_timestamp){
// console.log(data);
const buyer = data.from;
const sale_data = JSON.parse(data.assets_seller);
// console.log(sale_data);
for (const seller in sale_data){
for (let i=0; i < sale_data[seller].length; i++){
const asset_id = sale_data[seller][i][0];
const quantity = sale_data[seller][i][1];
const asset = await this.get_simple_data(buyer, asset_id);
if (!asset){
console.log(`Failed to parse simpleasset ${asset_id}, owned by ${buyer}`);
continue;
}
const asset_data = asset.data;
// console.log(`${buyer} paid ${quantity} to ${seller} for ${asset.id}`);
asset_data.asset_id = asset_id;
asset_data.category = asset.category;
asset_data.author = asset.author;
this.notify.forEach((n) => {
n.sale('simple', buyer, seller, quantity, asset_data, block_num, block_timestamp);
});
}
}
}
async process_waxstash(data, block_num, block_timestamp){
const asset = await this.get_simple_data(data.buyer, data.asset_id);
const asset_data = asset.data;
console.log(`${data.buyer} paid ${data.quantity} to ${data.seller} for ${data.asset_id}`);
asset_data.asset_id = data.asset_id;
asset_data.category = asset.category;
asset_data.author = asset.author;
this.notify.forEach((n) => {
n.sale('waxstash', data.buyer, data.seller, data.quantity, asset_data, block_num, block_timestamp);
});
}
async queueTrace(block_num, traces, block_timestamp) {
const atomic_sale_traces = [], atomic_drop_traces = [], myth_sale_traces = [], collectables_sale_traces = [], simple_sale_traces = [], waxstash_sale_traces = [];
for (const trace of traces) {
switch (trace[0]) {
case 'transaction_trace_v0':
const trx = trace[1];
// console.log(trx)
for (let action of trx.action_traces) {
//console.log(action)
switch (action[0]) {
case 'action_trace_v0':
if (action[1].act.account === 'atomicmarket' && action[1].act.name === 'purchasesale'){
atomic_sale_traces.push(trx);
continue;
}
else if (action[1].act.account === 'atomicdropsx' && action[1].act.name === 'assertdrop'){
atomic_drop_traces.push(trx);
continue;
}
else if (action[1].act.account === 'market.myth' && action[1].act.name === 'logsale' && action[1].receiver === 'market.myth'){
myth_sale_traces.push(action[1].act);
continue;
}
else if (action[1].act.account === 'simplemarket' && action[1].act.name === 'buylog'){
simple_sale_traces.push(action[1].act);
continue;
}
else if (action[1].act.account === 'eosio.token' && action[1].act.name === 'transfer'){
const sb = new Serialize.SerialBuffer({
textEncoder: new TextEncoder,
textDecoder: new TextDecoder,
array: action[1].act.data
});
const from = sb.getName();
const to = sb.getName();
if (to === 'waxstashsale'){
waxstash_sale_traces.push(trx);
}
continue;
}
break;
}
}
break;
}
}
if (atomic_sale_traces.length){
atomic_sale_traces.forEach(async st => {
let payment_action = st.action_traces[0][1];
if (payment_action.act.account === 'res.pink'){
payment_action = st.action_traces[1][1];
}
if (payment_action.act.account === 'eosio.token'){
// console.log(`Found sale`, payment_action);
const sb = new Serialize.SerialBuffer({
textEncoder: new TextEncoder,
textDecoder: new TextDecoder,
array: payment_action.act.data
});
const buyer = sb.getName();
sb.getName();
const quantity = sb.getAsset();
// get seller and asset id
let seller, asset_id;
for (let action of st.action_traces) {
switch (action[0]) {
case 'action_trace_v0':
if (action[1].act.account === 'eosio.token' && action[1].act.name === 'transfer'){
const sb_withdraw = new Serialize.SerialBuffer({
textEncoder: new TextEncoder,
textDecoder: new TextDecoder,
array: action[1].act.data
});
sb_withdraw.getName(); // from
const potential_seller = sb_withdraw.getName();
sb_withdraw.getAsset(); // quantity
const memo = sb_withdraw.getString();
if (memo.indexOf('Payout') > -1){
seller = potential_seller;
}
}
else if (action[1].act.account === 'atomicassets' && action[1].act.name === 'transfer'){
const sb_transfer = new Serialize.SerialBuffer({
textEncoder: new TextEncoder,
textDecoder: new TextDecoder,
array: action[1].act.data
});
sb_transfer.getName();
sb_transfer.getName();
sb_transfer.get(); // vector length (assumed to be 1)
asset_id = sb_transfer.getUint64AsNumber();
}
break;
}
}
await this.process_atomic(buyer, seller, quantity, asset_id, block_num, block_timestamp);
}
else {
console.error(`First action wasnt transfer in ${st.id}`);
}
});
// process.exit(0)
}
if (atomic_drop_traces.length){
let quantity, asset_id, buyer, seller;
for (let d = 0; d < atomic_drop_traces.length; d++){
const dt = atomic_drop_traces[d];
// console.log(dt);
for (let a = 0; a < dt.action_traces.length; a++){
const act = dt.action_traces[a];
// console.log('drop action', act[1].act);
if (act[1].act.name === 'assertdrop' && act[1].act.account === 'atomicdropsx'){
const deser_acts = await this.eos_api.deserializeActions([act[1].act]);
// console.log('assertdrop action', deser_acts[0]);
quantity = deser_acts[0].data.listing_price_to_assert;
if (quantity === '0 NULL'){
quantity = 'FREE';
}
}
else if (act[1].act.name === 'logmint' && act[1].act.account === 'atomicassets'){
const deser_acts = await this.eos_api.deserializeActions([act[1].act]);
asset_id = deser_acts[0].data.asset_id;
buyer = deser_acts[0].data.new_asset_owner;
seller = deser_acts[0].data.collection_name;
}
}
}
// process.exit(0)
await this.process_atomic(buyer, seller, quantity, asset_id, block_num, block_timestamp);
}
if (myth_sale_traces.length){
myth_sale_traces.forEach(ms => {
const act = this.eos_api.deserializeActions([ms]).then(act => {
// console.log(act[0]);
this.process_myth(act[0].data, block_num, block_timestamp);
});
});
}
if (simple_sale_traces.length){
simple_sale_traces.forEach(ms => {
const act = this.eos_api.deserializeActions([ms]).then(act => {
// console.log(act[0].data);
this.process_simple(act[0].data, block_num, block_timestamp);
});
});
}
if (waxstash_sale_traces.length){
waxstash_sale_traces.forEach(trx => {
// console.log(trx)
let seller, buyer, asset_id, quantity;
trx.action_traces.forEach(a => {
if (a[1].act.account === 'eosio.token' && a[1].act.name === 'transfer'){
const sb = new Serialize.SerialBuffer({
textEncoder: new TextEncoder,
textDecoder: new TextDecoder,
array: a[1].act.data
});
const from = sb.getName();
const to = sb.getName();
const qty = sb.getAsset();
const memo = sb.getString();
if (to === 'waxstashsale' && memo.indexOf(' Author:') > -1){
buyer = from;
quantity = qty;
const [asset_str] = memo.split(' ');
asset_id = asset_str.replace('Id:', '');
}
else if (memo.indexOf('SOLD ASSET') > -1){
seller = to;
}
}
});
if (seller && buyer && asset_id && quantity){
this.process_waxstash({ seller, buyer, asset_id, quantity }, block_num, block_timestamp);
}
});
}
/*if (collectables_sale_traces.length){
collectables_sale_traces.forEach(trx => {
console.log(trx);
});
}*/
}
async processTrace(block_num, traces, block_timestamp) {
// console.log(`Process block ${block_num}`)
return this.queueTrace(block_num, traces, block_timestamp);
}
}
module.exports = { TraceHandler }