@render-foundation/utils 0.0.78 → 0.0.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/client/onchain.js +109 -0
- package/lib/cjs/client/onchain.js.map +1 -0
- package/lib/cjs/client/pg/v1.js +623 -0
- package/lib/cjs/client/pg/v1.js.map +1 -0
- package/lib/cjs/client/pg/v2.js +840 -0
- package/lib/cjs/client/pg/v2.js.map +1 -0
- package/lib/cjs/client/pg/v2Client.js +807 -0
- package/lib/cjs/client/pg/v2Client.js.map +1 -0
- package/lib/cjs/client/price.js +46 -0
- package/lib/cjs/client/price.js.map +1 -0
- package/lib/cjs/client/render.js +164 -0
- package/lib/cjs/client/render.js.map +1 -0
- package/lib/cjs/client/squads.js +204 -0
- package/lib/cjs/client/squads.js.map +1 -0
- package/lib/cjs/client/trade.js +127 -0
- package/lib/cjs/client/trade.js.map +1 -0
- package/lib/cjs/client/wormhole.js +69 -0
- package/lib/cjs/client/wormhole.js.map +1 -0
- package/lib/cjs/constants.js +37 -0
- package/lib/cjs/constants.js.map +1 -0
- package/lib/cjs/dbTypes.js +3 -0
- package/lib/cjs/dbTypes.js.map +1 -0
- package/lib/cjs/dbTypesV2.js +3 -0
- package/lib/cjs/dbTypesV2.js.map +1 -0
- package/lib/cjs/errorReporter.js +77 -0
- package/lib/cjs/errorReporter.js.map +1 -0
- package/lib/cjs/index.js +42 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/logger.js +21 -0
- package/lib/cjs/logger.js.map +1 -0
- package/lib/esm/src/client/onchain.js +100 -0
- package/lib/esm/src/client/onchain.js.map +1 -0
- package/lib/esm/src/client/pg/v1.js +559 -0
- package/lib/esm/src/client/pg/v1.js.map +1 -0
- package/lib/esm/src/client/pg/v2.js +774 -0
- package/lib/esm/src/client/pg/v2.js.map +1 -0
- package/lib/esm/src/client/pg/v2Client.js +744 -0
- package/lib/esm/src/client/pg/v2Client.js.map +1 -0
- package/lib/esm/src/client/price.js +32 -0
- package/lib/esm/src/client/price.js.map +1 -0
- package/lib/esm/src/client/render.js +138 -0
- package/lib/esm/src/client/render.js.map +1 -0
- package/lib/esm/src/client/squads.js +163 -0
- package/lib/esm/src/client/squads.js.map +1 -0
- package/lib/esm/src/client/trade.js +114 -0
- package/lib/esm/src/client/trade.js.map +1 -0
- package/lib/esm/src/client/wormhole.js +33 -0
- package/lib/esm/src/client/wormhole.js.map +1 -0
- package/lib/esm/src/constants.js +34 -0
- package/lib/esm/src/constants.js.map +1 -0
- package/lib/esm/src/dbTypes.js +2 -0
- package/lib/esm/src/dbTypes.js.map +1 -0
- package/lib/esm/src/dbTypesV2.js +2 -0
- package/lib/esm/src/dbTypesV2.js.map +1 -0
- package/lib/esm/src/errorReporter.js +68 -0
- package/lib/esm/src/errorReporter.js.map +1 -0
- package/lib/esm/src/index.js +13 -0
- package/lib/esm/src/index.js.map +1 -0
- package/lib/esm/src/logger.js +18 -0
- package/lib/esm/src/logger.js.map +1 -0
- package/lib/esm/tsconfig.esm.tsbuildinfo +1 -0
- package/lib/types/src/client/onchain.d.ts +19 -0
- package/lib/types/src/client/onchain.d.ts.map +1 -0
- package/lib/types/src/client/pg/v1.d.ts +163 -0
- package/lib/types/src/client/pg/v1.d.ts.map +1 -0
- package/lib/types/src/client/pg/v2.d.ts +35 -0
- package/lib/types/src/client/pg/v2.d.ts.map +1 -0
- package/lib/types/src/client/pg/v2Client.d.ts +147 -0
- package/lib/types/src/client/pg/v2Client.d.ts.map +1 -0
- package/lib/types/src/client/price.d.ts +11 -0
- package/lib/types/src/client/price.d.ts.map +1 -0
- package/lib/types/src/client/render.d.ts +19 -0
- package/lib/types/src/client/render.d.ts.map +1 -0
- package/lib/types/src/client/squads.d.ts +17 -0
- package/lib/types/src/client/squads.d.ts.map +1 -0
- package/lib/types/src/client/trade.d.ts +15 -0
- package/lib/types/src/client/trade.d.ts.map +1 -0
- package/lib/types/src/client/wormhole.d.ts +9 -0
- package/lib/types/src/client/wormhole.d.ts.map +1 -0
- package/lib/types/src/constants.d.ts +32 -0
- package/lib/types/src/constants.d.ts.map +1 -0
- package/lib/types/src/dbTypes.d.ts +123 -0
- package/lib/types/src/dbTypes.d.ts.map +1 -0
- package/lib/types/src/dbTypesV2.d.ts +109 -0
- package/lib/types/src/dbTypesV2.d.ts.map +1 -0
- package/lib/types/src/errorReporter.d.ts +12 -0
- package/lib/types/src/errorReporter.d.ts.map +1 -0
- package/lib/types/src/index.d.ts +13 -0
- package/lib/types/src/index.d.ts.map +1 -0
- package/lib/types/src/logger.d.ts +16 -0
- package/lib/types/src/logger.d.ts.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,840 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
12
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
13
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
14
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
15
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
16
|
+
};
|
|
17
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
18
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
19
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
20
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
21
|
+
};
|
|
22
|
+
var _Deferred_promise, _Deferred_resolve, _Deferred_reject;
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.Deferred = exports.pgClient = exports.BURN_TABLE = exports.JOB_TABLE = exports.JOB_ID_TABLE = exports.SOL_TRANSFER_INFO_TABLE = exports.SOL_TRANSFER_TABLE = exports.ENTITY_TABLE = exports.MANUAL_BURN_TABLE = exports.EPOCH_TABLE = exports.SOL_TX_TABLE = void 0;
|
|
25
|
+
const pg_1 = require("pg");
|
|
26
|
+
const kysely_1 = require("kysely");
|
|
27
|
+
const logger_1 = require("../../logger");
|
|
28
|
+
const v1_1 = require("./v1");
|
|
29
|
+
const transferFromDBV2 = (o) => {
|
|
30
|
+
return {
|
|
31
|
+
id: o.id,
|
|
32
|
+
solKey: o.sol_key,
|
|
33
|
+
solTx: o.sig,
|
|
34
|
+
sqTx: o.sq_tx,
|
|
35
|
+
createdAt: o.created_at,
|
|
36
|
+
payedAt: o.executed_at,
|
|
37
|
+
amountDue: BigInt(o.amount_due),
|
|
38
|
+
amountPayed: o.amount_payed ? BigInt(o.amount_payed) : undefined,
|
|
39
|
+
fromSolKey: o.from_sol_key,
|
|
40
|
+
channel: o.channel,
|
|
41
|
+
symbol: o.symbol,
|
|
42
|
+
operatorRewards: o.channel == 'node_operator'
|
|
43
|
+
? {
|
|
44
|
+
epochId: o.ui,
|
|
45
|
+
startTime: o.start_time,
|
|
46
|
+
endTime: o.end_time,
|
|
47
|
+
}
|
|
48
|
+
: undefined,
|
|
49
|
+
computeClientRewards: o.channel == 'open_compute'
|
|
50
|
+
? {
|
|
51
|
+
epochId: o.ui,
|
|
52
|
+
deviceId: o.device_id,
|
|
53
|
+
}
|
|
54
|
+
: undefined,
|
|
55
|
+
upgradeRewards: o.channel == 'upgrade'
|
|
56
|
+
? {
|
|
57
|
+
epochId: o.ui,
|
|
58
|
+
epochStartTime: o.start_time,
|
|
59
|
+
points: BigInt(o.points),
|
|
60
|
+
}
|
|
61
|
+
: undefined,
|
|
62
|
+
availabilityRewards: o.channel == 'availability'
|
|
63
|
+
? {
|
|
64
|
+
epochId: o.ui,
|
|
65
|
+
deviceId: o.device_id,
|
|
66
|
+
}
|
|
67
|
+
: undefined,
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
exports.SOL_TX_TABLE = 'sol_tx';
|
|
71
|
+
exports.EPOCH_TABLE = 'epoch';
|
|
72
|
+
exports.MANUAL_BURN_TABLE = 'manual_burn';
|
|
73
|
+
exports.ENTITY_TABLE = 'entity';
|
|
74
|
+
exports.SOL_TRANSFER_TABLE = 'sol_transfer';
|
|
75
|
+
exports.SOL_TRANSFER_INFO_TABLE = 'sol_transfer_info';
|
|
76
|
+
exports.JOB_ID_TABLE = 'job_id';
|
|
77
|
+
exports.JOB_TABLE = 'job';
|
|
78
|
+
exports.BURN_TABLE = 'burn';
|
|
79
|
+
const pgClient = (config) => {
|
|
80
|
+
var _a, _b;
|
|
81
|
+
const dialect = new kysely_1.PostgresDialect({
|
|
82
|
+
pool: new pg_1.Pool(Object.assign(Object.assign({}, config), { port: (_a = config.port) !== null && _a !== void 0 ? _a : 5432, max: (_b = config.max) !== null && _b !== void 0 ? _b : 30 })),
|
|
83
|
+
});
|
|
84
|
+
// Database interface is passed to Kysely's constructor, and from now on, Kysely
|
|
85
|
+
// knows your database structure.
|
|
86
|
+
// Dialect is passed to Kysely's constructor, and from now on, Kysely knows how
|
|
87
|
+
// to communicate with your database.
|
|
88
|
+
const db = new kysely_1.Kysely({
|
|
89
|
+
dialect,
|
|
90
|
+
});
|
|
91
|
+
const getOrInsertEntity = (db, p, log = logger_1.consoleLogger) => __awaiter(void 0, void 0, void 0, function* () {
|
|
92
|
+
const res = (yield db
|
|
93
|
+
.selectFrom(exports.ENTITY_TABLE)
|
|
94
|
+
.select('id')
|
|
95
|
+
.where('sol_key', '=', p.solKey)
|
|
96
|
+
.executeTakeFirst());
|
|
97
|
+
if (res) {
|
|
98
|
+
log.debug(`already found entity ${res.id}`);
|
|
99
|
+
return res.id;
|
|
100
|
+
}
|
|
101
|
+
const { id } = (yield db
|
|
102
|
+
.insertInto(exports.ENTITY_TABLE)
|
|
103
|
+
.values({
|
|
104
|
+
sol_key: p.solKey,
|
|
105
|
+
})
|
|
106
|
+
.onConflict((oc) => oc.column('sol_key').doNothing())
|
|
107
|
+
.returning((eb) => ['id as id'])
|
|
108
|
+
.executeTakeFirst());
|
|
109
|
+
log.debug(`inserted entity ${id}`);
|
|
110
|
+
return id;
|
|
111
|
+
});
|
|
112
|
+
const getOrInsertTx = (db, p, log = logger_1.consoleLogger) => __awaiter(void 0, void 0, void 0, function* () {
|
|
113
|
+
const res = yield db
|
|
114
|
+
.selectFrom(exports.SOL_TX_TABLE)
|
|
115
|
+
.select('id')
|
|
116
|
+
.where('sig', '=', p.sig)
|
|
117
|
+
.executeTakeFirst();
|
|
118
|
+
if (res) {
|
|
119
|
+
log.debug(`already found tx ${res.id}`);
|
|
120
|
+
return res.id;
|
|
121
|
+
}
|
|
122
|
+
const { id } = (yield db
|
|
123
|
+
.insertInto(exports.SOL_TX_TABLE)
|
|
124
|
+
.values({
|
|
125
|
+
sig: p.sig,
|
|
126
|
+
executed_at: p.executedAt,
|
|
127
|
+
})
|
|
128
|
+
.onConflict((oc) => oc.column('sig').doNothing())
|
|
129
|
+
.returning((eb) => ['id as id'])
|
|
130
|
+
.executeTakeFirst());
|
|
131
|
+
log.debug(`inserted tx ${id}`);
|
|
132
|
+
return id;
|
|
133
|
+
});
|
|
134
|
+
const getOrInsertEpoch = (db, p, log = logger_1.consoleLogger) => __awaiter(void 0, void 0, void 0, function* () {
|
|
135
|
+
const res = yield db
|
|
136
|
+
.selectFrom(exports.EPOCH_TABLE)
|
|
137
|
+
.select('id')
|
|
138
|
+
.where('ui', '=', String(p.uiEpochId))
|
|
139
|
+
.where('channel', '=', p.channel)
|
|
140
|
+
.executeTakeFirst();
|
|
141
|
+
if (res) {
|
|
142
|
+
log.debug(`already found epoch ${res.id}`);
|
|
143
|
+
return res.id;
|
|
144
|
+
}
|
|
145
|
+
const { id } = (yield db
|
|
146
|
+
.insertInto(exports.EPOCH_TABLE)
|
|
147
|
+
.values({
|
|
148
|
+
ui: p.uiEpochId,
|
|
149
|
+
channel: p.channel,
|
|
150
|
+
start_time: p.startTime,
|
|
151
|
+
end_time: p.endTime,
|
|
152
|
+
saved_at: p.channel == 'node_operator' ? new Date() : undefined,
|
|
153
|
+
})
|
|
154
|
+
.onConflict((oc) => oc.columns(['ui', 'channel']).doNothing())
|
|
155
|
+
.returning((eb) => ['id as id'])
|
|
156
|
+
.executeTakeFirst());
|
|
157
|
+
log.debug(`inserted epoch ${id}`);
|
|
158
|
+
return id;
|
|
159
|
+
});
|
|
160
|
+
const updateTotalRndr = (db, p, totalRndr, savedAt, log) => __awaiter(void 0, void 0, void 0, function* () {
|
|
161
|
+
const res = (yield db
|
|
162
|
+
.updateTable(exports.EPOCH_TABLE)
|
|
163
|
+
.set({
|
|
164
|
+
total_rndr: totalRndr,
|
|
165
|
+
saved_at: savedAt,
|
|
166
|
+
})
|
|
167
|
+
.where('channel', '=', p.channel)
|
|
168
|
+
.where('ui', '=', String(p.uiEpochId))
|
|
169
|
+
.executeTakeFirst());
|
|
170
|
+
log.info(`set ${res.numUpdatedRows} rows. channel: ${p.channel}, ui: ${p.uiEpochId} -> totalRndr ${totalRndr} `);
|
|
171
|
+
});
|
|
172
|
+
const createTransfers = (db, transfers, log = logger_1.consoleLogger) => __awaiter(void 0, void 0, void 0, function* () {
|
|
173
|
+
var _c;
|
|
174
|
+
const epochIds = [];
|
|
175
|
+
for (const t of transfers) {
|
|
176
|
+
if (!t.computeClientRewards &&
|
|
177
|
+
!t.upgradeRewards &&
|
|
178
|
+
!t.operatorRewards &&
|
|
179
|
+
!t.availabilityRewards &&
|
|
180
|
+
!t.channel) {
|
|
181
|
+
log.warn(`malformed transfer doesnt have type ${JSON.stringify(t, (key, value) => typeof value === 'bigint' ? value.toString() : value)}`);
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
let solTxId;
|
|
185
|
+
if (t.solTx) {
|
|
186
|
+
solTxId = yield getOrInsertTx(db, {
|
|
187
|
+
sig: t.solTx,
|
|
188
|
+
executedAt: t.payedAt,
|
|
189
|
+
}, log);
|
|
190
|
+
}
|
|
191
|
+
let entityId;
|
|
192
|
+
if (t.solKey) {
|
|
193
|
+
entityId = yield getOrInsertEntity(db, { solKey: t.solKey }, log);
|
|
194
|
+
}
|
|
195
|
+
log.info(`inserting transfer ${JSON.stringify(t, (key, value) => typeof value === 'bigint' ? value.toString() : value)}`);
|
|
196
|
+
let fromEntityId;
|
|
197
|
+
if (t.fromSolKey) {
|
|
198
|
+
fromEntityId = yield getOrInsertEntity(db, { solKey: t.fromSolKey }, log);
|
|
199
|
+
}
|
|
200
|
+
const { id: transferId } = (yield db
|
|
201
|
+
.insertInto(exports.SOL_TRANSFER_TABLE)
|
|
202
|
+
.values({
|
|
203
|
+
sol_tx_id: solTxId,
|
|
204
|
+
from_entity_id: fromEntityId,
|
|
205
|
+
to_entity_id: entityId,
|
|
206
|
+
channel: t.channel
|
|
207
|
+
? t.channel
|
|
208
|
+
: (0, v1_1.transferTypToChan)((() => {
|
|
209
|
+
if (t.operatorRewards) {
|
|
210
|
+
return 'node_operator_rewards';
|
|
211
|
+
}
|
|
212
|
+
else if (t.upgradeRewards) {
|
|
213
|
+
return 'upgrade_rewards';
|
|
214
|
+
}
|
|
215
|
+
else if (t.availabilityRewards) {
|
|
216
|
+
return 'availability_rewards';
|
|
217
|
+
}
|
|
218
|
+
return 'open_compute_rewards';
|
|
219
|
+
})()),
|
|
220
|
+
symbol: (_c = t.symbol) !== null && _c !== void 0 ? _c : 'RENDER',
|
|
221
|
+
created_at: t.createdAt,
|
|
222
|
+
amount_due: BigInt(t.amountDue),
|
|
223
|
+
amount_payed: t.amountPayed ? BigInt(t.amountPayed) : 0,
|
|
224
|
+
})
|
|
225
|
+
.returning((eb) => ['id as id'])
|
|
226
|
+
.executeTakeFirst());
|
|
227
|
+
let epochId;
|
|
228
|
+
let deviceId;
|
|
229
|
+
let points;
|
|
230
|
+
let startTime;
|
|
231
|
+
let endTime;
|
|
232
|
+
let channel;
|
|
233
|
+
if (t.operatorRewards) {
|
|
234
|
+
log.info(`inserting operator rewards ${transferId}`);
|
|
235
|
+
epochId = t.operatorRewards.epochId;
|
|
236
|
+
startTime = t.operatorRewards.startTime;
|
|
237
|
+
endTime = t.operatorRewards.endTime;
|
|
238
|
+
channel = 'node_operator';
|
|
239
|
+
}
|
|
240
|
+
else if (t.computeClientRewards) {
|
|
241
|
+
log.info(`inserting compute client rewards ${transferId}`);
|
|
242
|
+
epochId = t.computeClientRewards.epochId;
|
|
243
|
+
deviceId = t.computeClientRewards.deviceId;
|
|
244
|
+
channel = 'open_compute';
|
|
245
|
+
}
|
|
246
|
+
else if (t.upgradeRewards) {
|
|
247
|
+
log.info(`inserting upgrade rewards ${transferId}`);
|
|
248
|
+
channel = 'upgrade';
|
|
249
|
+
epochId = t.upgradeRewards.epochId;
|
|
250
|
+
points = t.upgradeRewards.points;
|
|
251
|
+
}
|
|
252
|
+
else if (t.availabilityRewards) {
|
|
253
|
+
log.info(`inserting availability rewards ${transferId}`);
|
|
254
|
+
channel = 'availability';
|
|
255
|
+
epochId = t.availabilityRewards.epochId;
|
|
256
|
+
deviceId = t.availabilityRewards.deviceId;
|
|
257
|
+
}
|
|
258
|
+
else if (t.channel) {
|
|
259
|
+
channel = t.channel;
|
|
260
|
+
}
|
|
261
|
+
let epochIdDb;
|
|
262
|
+
if (epochId != null) {
|
|
263
|
+
epochIdDb = yield getOrInsertEpoch(db, {
|
|
264
|
+
channel,
|
|
265
|
+
startTime,
|
|
266
|
+
endTime,
|
|
267
|
+
uiEpochId: epochId,
|
|
268
|
+
}, log);
|
|
269
|
+
}
|
|
270
|
+
if (epochIdDb || deviceId || points) {
|
|
271
|
+
yield db
|
|
272
|
+
.insertInto(exports.SOL_TRANSFER_INFO_TABLE)
|
|
273
|
+
.values({
|
|
274
|
+
transfer_id: transferId,
|
|
275
|
+
device_id: deviceId,
|
|
276
|
+
points,
|
|
277
|
+
epoch_id: epochIdDb ? BigInt(epochIdDb) : null,
|
|
278
|
+
})
|
|
279
|
+
.executeTakeFirst();
|
|
280
|
+
}
|
|
281
|
+
epochIds.push({ epochDbId: epochIdDb });
|
|
282
|
+
}
|
|
283
|
+
return epochIds;
|
|
284
|
+
});
|
|
285
|
+
return {
|
|
286
|
+
db,
|
|
287
|
+
saveNodeOperatorEpoch(epochId, transfers, jobIds, totalRndr, log = logger_1.consoleLogger) {
|
|
288
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
289
|
+
yield db
|
|
290
|
+
.transaction()
|
|
291
|
+
.setIsolationLevel('repeatable read')
|
|
292
|
+
.execute((trx) => __awaiter(this, void 0, void 0, function* () {
|
|
293
|
+
const epochDbIds = yield createTransfers(trx, transfers, log);
|
|
294
|
+
log.info(`inserted transfers`);
|
|
295
|
+
for (const id of jobIds) {
|
|
296
|
+
yield trx
|
|
297
|
+
.insertInto(exports.JOB_ID_TABLE)
|
|
298
|
+
.values({
|
|
299
|
+
id: BigInt(id),
|
|
300
|
+
epoch_id: epochDbIds[0].epochDbId,
|
|
301
|
+
})
|
|
302
|
+
.executeTakeFirstOrThrow();
|
|
303
|
+
}
|
|
304
|
+
const now = new Date();
|
|
305
|
+
yield updateTotalRndr(db, { uiEpochId: epochId, channel: 'node_operator' }, totalRndr, now, log);
|
|
306
|
+
log.debug(`saved epoch`);
|
|
307
|
+
/*const now = new Date(Date.now())
|
|
308
|
+
const res = await trx
|
|
309
|
+
.updateTable(EPOCH_TABLE)
|
|
310
|
+
.set({
|
|
311
|
+
saved_at: now,
|
|
312
|
+
total_rndr: totalRndr,
|
|
313
|
+
})
|
|
314
|
+
.where('channel')
|
|
315
|
+
.where('ui', '=', epochId.toString())
|
|
316
|
+
.where('', '=')
|
|
317
|
+
.executeTakeFirstOrThrow()*/
|
|
318
|
+
}));
|
|
319
|
+
return;
|
|
320
|
+
});
|
|
321
|
+
},
|
|
322
|
+
getLatestNodeOperatorEpoch(log = logger_1.consoleLogger) {
|
|
323
|
+
var _a;
|
|
324
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
325
|
+
const now = new Date(Date.now());
|
|
326
|
+
try {
|
|
327
|
+
const res = yield db
|
|
328
|
+
.selectFrom(exports.EPOCH_TABLE)
|
|
329
|
+
.selectAll()
|
|
330
|
+
.where('channel', '=', 'node_operator')
|
|
331
|
+
.where('end_time', '<=', now)
|
|
332
|
+
.where('saved_at', 'is', null)
|
|
333
|
+
.orderBy('end_time asc')
|
|
334
|
+
.limit(1)
|
|
335
|
+
.executeTakeFirstOrThrow();
|
|
336
|
+
return {
|
|
337
|
+
epochId: BigInt(res.ui),
|
|
338
|
+
startedAt: res.start_time,
|
|
339
|
+
endedAt: res.end_time,
|
|
340
|
+
savedAt: (_a = res.saved_at) !== null && _a !== void 0 ? _a : undefined,
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
catch (e) {
|
|
344
|
+
if (e instanceof kysely_1.NoResultError) {
|
|
345
|
+
log.info(`no node operator epoch found`);
|
|
346
|
+
return undefined;
|
|
347
|
+
}
|
|
348
|
+
throw e;
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
},
|
|
352
|
+
getOutstanding(f, log = logger_1.consoleLogger) {
|
|
353
|
+
var _a;
|
|
354
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
355
|
+
const { typ, nullKey, due } = f;
|
|
356
|
+
//select sol_key, sum(amount_due)-sum(amount_payed) from transfer group by sol_key;
|
|
357
|
+
let q = db
|
|
358
|
+
.selectFrom(exports.SOL_TRANSFER_TABLE)
|
|
359
|
+
.innerJoin(exports.ENTITY_TABLE, 'sol_transfer.to_entity_id', 'entity.id')
|
|
360
|
+
.select(({ fn, val, ref }) => [
|
|
361
|
+
'entity.sol_key',
|
|
362
|
+
(0, kysely_1.sql) `${fn.sum('sol_transfer.amount_due')} -
|
|
363
|
+
${fn.sum('sol_transfer.amount_payed')}`.as('due'),
|
|
364
|
+
])
|
|
365
|
+
.where('sol_transfer.channel', '=', (0, v1_1.transferTypToChan)(typ))
|
|
366
|
+
.groupBy(['entity.sol_key']);
|
|
367
|
+
if (!nullKey) {
|
|
368
|
+
q = q.where('entity.sol_key', 'is not', null);
|
|
369
|
+
}
|
|
370
|
+
if (due) {
|
|
371
|
+
q = q.having(({ fn, ref }) => (0, kysely_1.sql) `${fn.sum('sol_transfer.amount_due')} -
|
|
372
|
+
${fn.sum('sol_transfer.amount_payed')}
|
|
373
|
+
>
|
|
374
|
+
0`);
|
|
375
|
+
}
|
|
376
|
+
const res = yield q.execute();
|
|
377
|
+
log.info(`outstanding query ${q.compile().sql} params ${JSON.stringify(f)} ${res.length} rows`);
|
|
378
|
+
const out = {};
|
|
379
|
+
for (const r of res) {
|
|
380
|
+
out[(_a = r.sol_key) !== null && _a !== void 0 ? _a : ''] = BigInt(r.due);
|
|
381
|
+
}
|
|
382
|
+
return out;
|
|
383
|
+
});
|
|
384
|
+
},
|
|
385
|
+
fetchTransfers(f, log = logger_1.consoleLogger) {
|
|
386
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
387
|
+
let base = db
|
|
388
|
+
.selectFrom(exports.SOL_TRANSFER_TABLE)
|
|
389
|
+
.selectAll(exports.SOL_TRANSFER_TABLE)
|
|
390
|
+
.leftJoin(exports.SOL_TRANSFER_INFO_TABLE, 'sol_transfer_info.transfer_id', 'sol_transfer.id')
|
|
391
|
+
.select(['device_id', 'points'])
|
|
392
|
+
.leftJoin(exports.SOL_TX_TABLE, 'sol_tx.id', 'sol_transfer.sol_tx_id')
|
|
393
|
+
.select(['sol_tx.sig', 'sol_tx.executed_at'])
|
|
394
|
+
.leftJoin(exports.ENTITY_TABLE, 'entity.id', 'sol_transfer.to_entity_id')
|
|
395
|
+
.select(['entity.sol_key'])
|
|
396
|
+
.leftJoin(`${exports.ENTITY_TABLE} as fe`, 'fe.id', 'sol_transfer.from_entity_id')
|
|
397
|
+
.select(['fe.sol_key as from_sol_key'])
|
|
398
|
+
.leftJoin(exports.EPOCH_TABLE, 'epoch.id', 'sol_transfer_info.epoch_id')
|
|
399
|
+
.select(['epoch.ui', 'epoch.start_time', 'epoch.end_time']);
|
|
400
|
+
if (f.typ) {
|
|
401
|
+
base = base.where('sol_transfer.channel', '=', (0, v1_1.transferTypToChan)(f.typ));
|
|
402
|
+
}
|
|
403
|
+
if (f.start) {
|
|
404
|
+
base = base.where('sol_tx.executed_at', '>=', f.start);
|
|
405
|
+
}
|
|
406
|
+
if (f.end) {
|
|
407
|
+
base = base.where('sol_tx.executed_at', '<=', f.end);
|
|
408
|
+
}
|
|
409
|
+
if (f.solKey) {
|
|
410
|
+
base = base.where('entity.sol_key', '=', f.solKey);
|
|
411
|
+
}
|
|
412
|
+
if (f.payed != undefined) {
|
|
413
|
+
base = base.where('sol_transfer.sol_tx_id', f.payed ? 'is not' : 'is', null);
|
|
414
|
+
}
|
|
415
|
+
if (f.createdBefore != undefined) {
|
|
416
|
+
base = base.where('sol_transfer.created_at', '<=', f.createdBefore);
|
|
417
|
+
}
|
|
418
|
+
if (f.epochId) {
|
|
419
|
+
base = base.where('epoch.ui', '=', String(f.epochId));
|
|
420
|
+
}
|
|
421
|
+
if (f.sort) {
|
|
422
|
+
base = base.orderBy('entity.sol_key asc');
|
|
423
|
+
}
|
|
424
|
+
log.info(`fetching ${base.compile().sql} params ${JSON.stringify(f)}`);
|
|
425
|
+
const res = yield base.execute();
|
|
426
|
+
log.info(`fetched ${res.length} transfers`);
|
|
427
|
+
return res.map((r) => transferFromDBV2(r));
|
|
428
|
+
});
|
|
429
|
+
},
|
|
430
|
+
payoutTransfers(idToAmt, payedAt, solTx, log = logger_1.consoleLogger) {
|
|
431
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
432
|
+
yield db
|
|
433
|
+
.transaction()
|
|
434
|
+
.setIsolationLevel('repeatable read')
|
|
435
|
+
.execute((tx) => __awaiter(this, void 0, void 0, function* () {
|
|
436
|
+
const alreadyUpdatedIds = (yield tx
|
|
437
|
+
.selectFrom(exports.SOL_TRANSFER_TABLE)
|
|
438
|
+
.innerJoin(exports.SOL_TX_TABLE, 'sol_transfer.sol_tx_id', 'sol_tx.id')
|
|
439
|
+
.select(['sol_transfer.id'])
|
|
440
|
+
.where('sol_transfer.id', 'in', Object.keys(idToAmt))
|
|
441
|
+
.where('sol_tx.sig', 'is not', null)
|
|
442
|
+
.execute()).map(({ id }) => id);
|
|
443
|
+
for (const [id, amt] of Object.entries(idToAmt)) {
|
|
444
|
+
if (alreadyUpdatedIds.includes(id)) {
|
|
445
|
+
log.info(`transfer ${id} already marked payed out. skipping`);
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
const solTxId = yield getOrInsertTx(tx, { sig: solTx, executedAt: payedAt }, log);
|
|
449
|
+
const res = yield tx
|
|
450
|
+
.updateTable(exports.SOL_TRANSFER_TABLE)
|
|
451
|
+
.set({
|
|
452
|
+
sol_tx_id: solTxId,
|
|
453
|
+
amount_payed: amt,
|
|
454
|
+
})
|
|
455
|
+
.where('id', '=', id.toString())
|
|
456
|
+
.executeTakeFirstOrThrow();
|
|
457
|
+
log.info(`marked ${res.numUpdatedRows} transfer payed out`);
|
|
458
|
+
}
|
|
459
|
+
}));
|
|
460
|
+
});
|
|
461
|
+
},
|
|
462
|
+
payoutTransfer(id, payedAt, solTx, amtPayed, log = logger_1.consoleLogger) {
|
|
463
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
464
|
+
yield db
|
|
465
|
+
.transaction()
|
|
466
|
+
.setIsolationLevel('repeatable read')
|
|
467
|
+
.execute((tx) => __awaiter(this, void 0, void 0, function* () {
|
|
468
|
+
const solTxId = yield getOrInsertTx(tx, { sig: solTx, executedAt: payedAt }, log);
|
|
469
|
+
const res = yield tx
|
|
470
|
+
.updateTable(exports.SOL_TRANSFER_TABLE)
|
|
471
|
+
.set({
|
|
472
|
+
sol_tx_id: solTxId,
|
|
473
|
+
amount_payed: amtPayed,
|
|
474
|
+
})
|
|
475
|
+
.where('id', '=', id.toString())
|
|
476
|
+
.executeTakeFirst();
|
|
477
|
+
log.info(`marked ${res.numUpdatedRows} transfer payed out`);
|
|
478
|
+
}));
|
|
479
|
+
});
|
|
480
|
+
},
|
|
481
|
+
createTransfers(transfers, log = logger_1.consoleLogger) {
|
|
482
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
483
|
+
yield db
|
|
484
|
+
.transaction()
|
|
485
|
+
.setIsolationLevel('repeatable read')
|
|
486
|
+
.execute((trx) => __awaiter(this, void 0, void 0, function* () {
|
|
487
|
+
yield createTransfers(trx, transfers, log);
|
|
488
|
+
}));
|
|
489
|
+
});
|
|
490
|
+
},
|
|
491
|
+
fetchInfoForEpoch(epochId, log = logger_1.consoleLogger) {
|
|
492
|
+
var _a;
|
|
493
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
494
|
+
const q = db
|
|
495
|
+
.selectFrom(exports.JOB_ID_TABLE)
|
|
496
|
+
.innerJoin(exports.EPOCH_TABLE, 'epoch.id', 'job_id.epoch_id')
|
|
497
|
+
.leftJoin(exports.MANUAL_BURN_TABLE, 'manual_burn.epoch_id', 'epoch.id')
|
|
498
|
+
.where('epoch.ui', '=', String(epochId))
|
|
499
|
+
.where('epoch.channel', '=', 'node_operator')
|
|
500
|
+
.select([
|
|
501
|
+
'job_id.id',
|
|
502
|
+
'epoch.total_rndr',
|
|
503
|
+
'epoch.start_time',
|
|
504
|
+
'epoch.end_time',
|
|
505
|
+
'manual_burn.burned',
|
|
506
|
+
'manual_burn.tx_sig',
|
|
507
|
+
])
|
|
508
|
+
.orderBy('job_id.id asc');
|
|
509
|
+
log.debug(`fetch epoch info q ${q.compile().sql} epochId ${epochId}`);
|
|
510
|
+
const res = yield q.execute();
|
|
511
|
+
if (res.length == 0) {
|
|
512
|
+
throw new Error(`epoch ${epochId} not found`);
|
|
513
|
+
}
|
|
514
|
+
return {
|
|
515
|
+
jobIds: res.map((r) => Number(r.id)),
|
|
516
|
+
totalRndr: BigInt(res[0].total_rndr),
|
|
517
|
+
startTime: res[0].start_time,
|
|
518
|
+
endTime: res[0].end_time,
|
|
519
|
+
burned: BigInt((_a = res[0].burned) !== null && _a !== void 0 ? _a : 0),
|
|
520
|
+
burnTx: res[0].tx_sig,
|
|
521
|
+
};
|
|
522
|
+
});
|
|
523
|
+
},
|
|
524
|
+
updateComputeClientSolKey(deviceId, solKey, log = logger_1.consoleLogger) {
|
|
525
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
526
|
+
const q = db
|
|
527
|
+
.updateTable(`${exports.ENTITY_TABLE} as e`)
|
|
528
|
+
.set({
|
|
529
|
+
sol_key: solKey,
|
|
530
|
+
})
|
|
531
|
+
.from(`${exports.SOL_TRANSFER_TABLE} as t`)
|
|
532
|
+
.innerJoin(exports.SOL_TRANSFER_INFO_TABLE, 'sol_transfer_info.transfer_id', 't.id')
|
|
533
|
+
//.whereRef('t.id', '=', 'o.transfer_id')
|
|
534
|
+
.where('e.sol_key', 'is', null)
|
|
535
|
+
.where('sol_transfer_info.device_id', '=', deviceId);
|
|
536
|
+
const sql = yield q.compile();
|
|
537
|
+
log.info(`sql ${sql.sql} params ${sql.parameters}`);
|
|
538
|
+
const res = yield q.executeTakeFirstOrThrow();
|
|
539
|
+
log.info(`updated ${res.numUpdatedRows} device-id sol_keys`);
|
|
540
|
+
});
|
|
541
|
+
},
|
|
542
|
+
markJobsBurnt(trx, p, log = logger_1.consoleLogger) {
|
|
543
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
544
|
+
const solTxId = yield getOrInsertTx(trx, { sig: p.solTx, executedAt: p.executedAt }, log);
|
|
545
|
+
const { id } = yield trx
|
|
546
|
+
.insertInto(exports.BURN_TABLE)
|
|
547
|
+
.values({
|
|
548
|
+
sol_tx_id: solTxId,
|
|
549
|
+
priced_at: p.pricedAt,
|
|
550
|
+
usdc_spent: p.usdcSpent,
|
|
551
|
+
quote_amt: p.quoteAmt,
|
|
552
|
+
burned: p.burned,
|
|
553
|
+
})
|
|
554
|
+
//.onConflict((oc) => oc.column('sol_tx').doNothing())
|
|
555
|
+
.returning((eb) => ['id as id'])
|
|
556
|
+
.executeTakeFirstOrThrow();
|
|
557
|
+
const jobIdsStr = p.jobsIds.map(String);
|
|
558
|
+
const res = yield trx
|
|
559
|
+
.updateTable(exports.JOB_TABLE)
|
|
560
|
+
.where('id', 'in', jobIdsStr)
|
|
561
|
+
.set('burn_id', id)
|
|
562
|
+
.executeTakeFirstOrThrow();
|
|
563
|
+
log.debug(`set burn_id on ${res.numUpdatedRows} jobs`);
|
|
564
|
+
return;
|
|
565
|
+
});
|
|
566
|
+
},
|
|
567
|
+
fetchBurns(f, log = logger_1.consoleLogger) {
|
|
568
|
+
var _a, _b;
|
|
569
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
570
|
+
let q = db
|
|
571
|
+
.selectFrom('burn')
|
|
572
|
+
.selectAll('burn')
|
|
573
|
+
.innerJoin('sol_tx', 'sol_tx.id', 'burn.sol_tx_id')
|
|
574
|
+
.select(['sol_tx.executed_at', 'sol_tx.sig']);
|
|
575
|
+
if (!f.inclManual) {
|
|
576
|
+
q = q.where('burn.usdc_spent', '>', String(0));
|
|
577
|
+
}
|
|
578
|
+
if (f.start) {
|
|
579
|
+
q = q.where('sol_tx.executed_at', '>=', f.start);
|
|
580
|
+
}
|
|
581
|
+
if (f.end) {
|
|
582
|
+
q = q.where('sol_tx.executed_at', '<=', f.end);
|
|
583
|
+
}
|
|
584
|
+
log.info(`fetchBurns q ${q.compile().sql}`);
|
|
585
|
+
const burns = [];
|
|
586
|
+
const rows = yield q.execute();
|
|
587
|
+
log.info(`fetchBurns len ${rows.length}`);
|
|
588
|
+
for (const r of rows) {
|
|
589
|
+
burns.push({
|
|
590
|
+
jobs: [],
|
|
591
|
+
id: Number(r.id),
|
|
592
|
+
solTx: r.sig,
|
|
593
|
+
executedAt: r.executed_at,
|
|
594
|
+
pricedAt: r.priced_at,
|
|
595
|
+
usdcSpent: BigInt((_a = r.usdc_spent) !== null && _a !== void 0 ? _a : 0),
|
|
596
|
+
quoteAmt: BigInt((_b = r.quote_amt) !== null && _b !== void 0 ? _b : 0),
|
|
597
|
+
burned: BigInt(r.burned),
|
|
598
|
+
markedBurnedAt: r.marked_burned_at,
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
return burns;
|
|
602
|
+
});
|
|
603
|
+
},
|
|
604
|
+
backfillBurn(p, log = logger_1.consoleLogger) {
|
|
605
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
606
|
+
yield db.transaction().execute((trx) => __awaiter(this, void 0, void 0, function* () {
|
|
607
|
+
var _a;
|
|
608
|
+
const solTxId = yield getOrInsertTx(trx, { sig: p.solTx, executedAt: p.executedAt }, log);
|
|
609
|
+
const { id } = yield trx
|
|
610
|
+
.insertInto(exports.BURN_TABLE)
|
|
611
|
+
.values({
|
|
612
|
+
sol_tx_id: solTxId,
|
|
613
|
+
priced_at: p.pricedAt,
|
|
614
|
+
usdc_spent: p.usdcSpent,
|
|
615
|
+
quote_amt: p.quoteAmt,
|
|
616
|
+
burned: p.burned,
|
|
617
|
+
marked_burned_at: p.markedBurnedAt,
|
|
618
|
+
})
|
|
619
|
+
//.onConflict((oc) => oc.column('sol_tx').doNothing())
|
|
620
|
+
.returning((eb) => ['id as id'])
|
|
621
|
+
.executeTakeFirstOrThrow();
|
|
622
|
+
let num = BigInt(0);
|
|
623
|
+
for (const { rndrUsed, completedAt, id: jobId, insertedAt } of p.jobs) {
|
|
624
|
+
const res = yield trx
|
|
625
|
+
.insertInto(exports.JOB_TABLE)
|
|
626
|
+
.values({
|
|
627
|
+
id: jobId,
|
|
628
|
+
render_amt: rndrUsed,
|
|
629
|
+
burn_id: id,
|
|
630
|
+
completed_at: completedAt,
|
|
631
|
+
inserted_at: insertedAt !== null && insertedAt !== void 0 ? insertedAt : new Date(),
|
|
632
|
+
})
|
|
633
|
+
.onConflict((oc) => oc.column('id').doNothing())
|
|
634
|
+
.executeTakeFirstOrThrow();
|
|
635
|
+
num += (_a = res.numInsertedOrUpdatedRows) !== null && _a !== void 0 ? _a : BigInt(0);
|
|
636
|
+
}
|
|
637
|
+
log.debug(`burn id ${id} inserted ${num} of ${p.jobs.length} burn jobs`);
|
|
638
|
+
}));
|
|
639
|
+
});
|
|
640
|
+
},
|
|
641
|
+
produceBurnedJobs(jobs, log = logger_1.consoleLogger) {
|
|
642
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
643
|
+
yield db.transaction().execute((trx) => __awaiter(this, void 0, void 0, function* () {
|
|
644
|
+
var _a;
|
|
645
|
+
let num = BigInt(0);
|
|
646
|
+
for (const { amount, completedAt, jobId } of jobs) {
|
|
647
|
+
const res = yield trx
|
|
648
|
+
.insertInto(exports.JOB_TABLE)
|
|
649
|
+
.values({
|
|
650
|
+
id: jobId,
|
|
651
|
+
render_amt: amount,
|
|
652
|
+
completed_at: completedAt,
|
|
653
|
+
inserted_at: new Date(Date.now()),
|
|
654
|
+
})
|
|
655
|
+
.onConflict((oc) => oc.column('id').doNothing())
|
|
656
|
+
.executeTakeFirstOrThrow();
|
|
657
|
+
num += (_a = res.numInsertedOrUpdatedRows) !== null && _a !== void 0 ? _a : BigInt(0);
|
|
658
|
+
}
|
|
659
|
+
log.debug(`inserted ${num} of ${jobs.length} burn jobs`);
|
|
660
|
+
}));
|
|
661
|
+
});
|
|
662
|
+
},
|
|
663
|
+
markJobsBurned(p, log = logger_1.consoleLogger) {
|
|
664
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
665
|
+
let { markedAt, solTx } = p;
|
|
666
|
+
let res = yield db
|
|
667
|
+
.updateTable(exports.BURN_TABLE)
|
|
668
|
+
.set({ marked_burned_at: markedAt })
|
|
669
|
+
.from(exports.SOL_TX_TABLE)
|
|
670
|
+
.whereRef('burn.sol_tx_id', '=', 'sol_tx.id')
|
|
671
|
+
.where('sol_tx.sig', '=', solTx)
|
|
672
|
+
.executeTakeFirstOrThrow();
|
|
673
|
+
log.debug(`set ${res.numUpdatedRows} ${solTx} -> marked_burned_at ${markedAt.toISOString()}`);
|
|
674
|
+
});
|
|
675
|
+
},
|
|
676
|
+
consumeAndLockBurnJobs(limit, lowerRndrThreshold, upperRndrThreshold, log = logger_1.consoleLogger) {
|
|
677
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
678
|
+
const trx = yield begin(db);
|
|
679
|
+
const q = trx.trx
|
|
680
|
+
.selectFrom(trx.trx
|
|
681
|
+
.selectFrom(exports.JOB_TABLE)
|
|
682
|
+
.select(({ fn, val, ref }) => [
|
|
683
|
+
'id',
|
|
684
|
+
fn
|
|
685
|
+
.sum('render_amt')
|
|
686
|
+
.over((ob) => ob.orderBy(`completed_at`, `asc`))
|
|
687
|
+
.as('total'),
|
|
688
|
+
])
|
|
689
|
+
.where('burn_id', 'is', null)
|
|
690
|
+
.as('t'))
|
|
691
|
+
.select('id')
|
|
692
|
+
.where('total', '>', lowerRndrThreshold)
|
|
693
|
+
.where('total', '<', upperRndrThreshold);
|
|
694
|
+
log.debug(`outstanding jobs q: ${q.compile().sql} ${JSON.stringify({
|
|
695
|
+
limit,
|
|
696
|
+
lowerRndrThreshold,
|
|
697
|
+
upperRndrThreshold,
|
|
698
|
+
})}`);
|
|
699
|
+
const idRs = yield q.execute();
|
|
700
|
+
const ids = idRs.map((x) => x.id);
|
|
701
|
+
log.debug(`${ids.length} candidate jobs for burn. locking up to ${limit} jobs`);
|
|
702
|
+
if (ids.length == 0) {
|
|
703
|
+
return { trx, jobs: [] };
|
|
704
|
+
}
|
|
705
|
+
const rows = yield trx.trx
|
|
706
|
+
.selectFrom(exports.JOB_TABLE)
|
|
707
|
+
.selectAll()
|
|
708
|
+
.where('id', 'in', ids)
|
|
709
|
+
.limit(limit)
|
|
710
|
+
.forUpdate()
|
|
711
|
+
.skipLocked()
|
|
712
|
+
.execute();
|
|
713
|
+
log.debug(`locked ${rows.length} jobs`);
|
|
714
|
+
const jobs = [];
|
|
715
|
+
for (const r of rows) {
|
|
716
|
+
jobs.push({
|
|
717
|
+
id: Number(r.id),
|
|
718
|
+
completedAt: r.completed_at,
|
|
719
|
+
rndrUsed: BigInt(r.render_amt),
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
return { trx, jobs };
|
|
723
|
+
});
|
|
724
|
+
},
|
|
725
|
+
incrBuyBurnMetrics(log = logger_1.consoleLogger) {
|
|
726
|
+
var _a;
|
|
727
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
728
|
+
const res = yield db
|
|
729
|
+
.selectFrom(exports.JOB_TABLE)
|
|
730
|
+
.select('completed_at')
|
|
731
|
+
.orderBy('completed_at asc')
|
|
732
|
+
.where('burn_id', 'is', null)
|
|
733
|
+
.execute();
|
|
734
|
+
if (res.length == 0) {
|
|
735
|
+
return { outstandingJobs: 0 };
|
|
736
|
+
}
|
|
737
|
+
return {
|
|
738
|
+
outstandingJobs: res.length,
|
|
739
|
+
oldestJobFinishedAt: (_a = res[0].completed_at) !== null && _a !== void 0 ? _a : undefined,
|
|
740
|
+
};
|
|
741
|
+
});
|
|
742
|
+
},
|
|
743
|
+
deleteAll() {
|
|
744
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
745
|
+
if (config.database.includes('render')) {
|
|
746
|
+
throw new Error('cannot nuke from deployed database. call from tests only');
|
|
747
|
+
}
|
|
748
|
+
yield Promise.all([
|
|
749
|
+
db
|
|
750
|
+
.deleteFrom(exports.SOL_TX_TABLE)
|
|
751
|
+
.where('id', 'is not', null)
|
|
752
|
+
.executeTakeFirst(),
|
|
753
|
+
db
|
|
754
|
+
.deleteFrom(exports.SOL_TRANSFER_TABLE)
|
|
755
|
+
.where('id', 'is not', null)
|
|
756
|
+
.executeTakeFirst(),
|
|
757
|
+
db
|
|
758
|
+
.deleteFrom(exports.SOL_TRANSFER_INFO_TABLE)
|
|
759
|
+
.where('transfer_id', 'is not', null)
|
|
760
|
+
.executeTakeFirst(),
|
|
761
|
+
db
|
|
762
|
+
.deleteFrom(exports.EPOCH_TABLE)
|
|
763
|
+
.where('id', 'is not', null)
|
|
764
|
+
.executeTakeFirst(),
|
|
765
|
+
db
|
|
766
|
+
.deleteFrom(exports.ENTITY_TABLE)
|
|
767
|
+
.where('id', 'is not', null)
|
|
768
|
+
.executeTakeFirst(),
|
|
769
|
+
db
|
|
770
|
+
.deleteFrom(exports.MANUAL_BURN_TABLE)
|
|
771
|
+
.where('epoch_id', 'is not', null)
|
|
772
|
+
.executeTakeFirst(),
|
|
773
|
+
db
|
|
774
|
+
.deleteFrom(exports.JOB_ID_TABLE)
|
|
775
|
+
.where('id', 'is not', null)
|
|
776
|
+
.executeTakeFirst(),
|
|
777
|
+
db.deleteFrom(exports.JOB_TABLE).where('id', 'is not', null).executeTakeFirst(),
|
|
778
|
+
db
|
|
779
|
+
.deleteFrom(exports.BURN_TABLE)
|
|
780
|
+
.where('id', 'is not', null)
|
|
781
|
+
.executeTakeFirst(),
|
|
782
|
+
]);
|
|
783
|
+
});
|
|
784
|
+
},
|
|
785
|
+
};
|
|
786
|
+
};
|
|
787
|
+
exports.pgClient = pgClient;
|
|
788
|
+
function begin(db) {
|
|
789
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
790
|
+
const connection = new Deferred();
|
|
791
|
+
const result = new Deferred();
|
|
792
|
+
// Do NOT await this line.
|
|
793
|
+
db.transaction()
|
|
794
|
+
.setIsolationLevel('repeatable read')
|
|
795
|
+
.execute((trx) => {
|
|
796
|
+
connection.resolve(trx);
|
|
797
|
+
return result.promise;
|
|
798
|
+
})
|
|
799
|
+
.catch((err) => {
|
|
800
|
+
// Don't do anything here. Just swallow the exception.
|
|
801
|
+
});
|
|
802
|
+
const trx = yield connection.promise;
|
|
803
|
+
return {
|
|
804
|
+
trx,
|
|
805
|
+
commit() {
|
|
806
|
+
result.resolve(null);
|
|
807
|
+
},
|
|
808
|
+
rollback() {
|
|
809
|
+
result.reject(new Error('rollback'));
|
|
810
|
+
},
|
|
811
|
+
};
|
|
812
|
+
});
|
|
813
|
+
}
|
|
814
|
+
class Deferred {
|
|
815
|
+
constructor() {
|
|
816
|
+
_Deferred_promise.set(this, void 0);
|
|
817
|
+
_Deferred_resolve.set(this, void 0);
|
|
818
|
+
_Deferred_reject.set(this, void 0);
|
|
819
|
+
this.resolve = (value) => {
|
|
820
|
+
if (__classPrivateFieldGet(this, _Deferred_resolve, "f")) {
|
|
821
|
+
__classPrivateFieldGet(this, _Deferred_resolve, "f").call(this, value);
|
|
822
|
+
}
|
|
823
|
+
};
|
|
824
|
+
this.reject = (reason) => {
|
|
825
|
+
if (__classPrivateFieldGet(this, _Deferred_reject, "f")) {
|
|
826
|
+
__classPrivateFieldGet(this, _Deferred_reject, "f").call(this, reason);
|
|
827
|
+
}
|
|
828
|
+
};
|
|
829
|
+
__classPrivateFieldSet(this, _Deferred_promise, new Promise((resolve, reject) => {
|
|
830
|
+
__classPrivateFieldSet(this, _Deferred_reject, reject, "f");
|
|
831
|
+
__classPrivateFieldSet(this, _Deferred_resolve, resolve, "f");
|
|
832
|
+
}), "f");
|
|
833
|
+
}
|
|
834
|
+
get promise() {
|
|
835
|
+
return __classPrivateFieldGet(this, _Deferred_promise, "f");
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
exports.Deferred = Deferred;
|
|
839
|
+
_Deferred_promise = new WeakMap(), _Deferred_resolve = new WeakMap(), _Deferred_reject = new WeakMap();
|
|
840
|
+
//# sourceMappingURL=v2.js.map
|