@render-foundation/utils 0.0.169 → 0.0.170
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/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/lib/cjs/backfiller.js +0 -855
- package/lib/cjs/backfiller.js.map +0 -1
- package/lib/cjs/client/onchain.js +0 -132
- package/lib/cjs/client/onchain.js.map +0 -1
- package/lib/cjs/client/pg/base.js +0 -198
- package/lib/cjs/client/pg/base.js.map +0 -1
- package/lib/cjs/client/pg/v1.js +0 -623
- package/lib/cjs/client/pg/v1.js.map +0 -1
- package/lib/cjs/client/pg/v2.js +0 -840
- package/lib/cjs/client/pg/v2.js.map +0 -1
- package/lib/cjs/client/pg/v2Client.js +0 -1436
- package/lib/cjs/client/pg/v2Client.js.map +0 -1
- package/lib/cjs/client/pg/viewClient.js +0 -107
- package/lib/cjs/client/pg/viewClient.js.map +0 -1
- package/lib/cjs/client/price.js +0 -63
- package/lib/cjs/client/price.js.map +0 -1
- package/lib/cjs/client/render.js +0 -172
- package/lib/cjs/client/render.js.map +0 -1
- package/lib/cjs/client/squads.js +0 -204
- package/lib/cjs/client/squads.js.map +0 -1
- package/lib/cjs/client/trade.js +0 -231
- package/lib/cjs/client/trade.js.map +0 -1
- package/lib/cjs/client/wormhole.js +0 -69
- package/lib/cjs/client/wormhole.js.map +0 -1
- package/lib/cjs/constants.js +0 -46
- package/lib/cjs/constants.js.map +0 -1
- package/lib/cjs/dbTypes.js +0 -3
- package/lib/cjs/dbTypes.js.map +0 -1
- package/lib/cjs/dbTypesV2.js +0 -3
- package/lib/cjs/dbTypesV2.js.map +0 -1
- package/lib/cjs/dbViewTypes.js +0 -3
- package/lib/cjs/dbViewTypes.js.map +0 -1
- package/lib/cjs/errorReporter.js +0 -126
- package/lib/cjs/errorReporter.js.map +0 -1
- package/lib/cjs/index.js +0 -45
- package/lib/cjs/index.js.map +0 -1
- package/lib/cjs/logger.js +0 -21
- package/lib/cjs/logger.js.map +0 -1
- package/lib/esm/src/backfiller.js +0 -810
- package/lib/esm/src/backfiller.js.map +0 -1
- package/lib/esm/src/client/onchain.js +0 -122
- package/lib/esm/src/client/onchain.js.map +0 -1
- package/lib/esm/src/client/pg/base.js +0 -162
- package/lib/esm/src/client/pg/base.js.map +0 -1
- package/lib/esm/src/client/pg/v1.js +0 -559
- package/lib/esm/src/client/pg/v1.js.map +0 -1
- package/lib/esm/src/client/pg/v2.js +0 -774
- package/lib/esm/src/client/pg/v2.js.map +0 -1
- package/lib/esm/src/client/pg/v2Client.js +0 -1327
- package/lib/esm/src/client/pg/v2Client.js.map +0 -1
- package/lib/esm/src/client/pg/viewClient.js +0 -91
- package/lib/esm/src/client/pg/viewClient.js.map +0 -1
- package/lib/esm/src/client/price.js +0 -41
- package/lib/esm/src/client/price.js.map +0 -1
- package/lib/esm/src/client/render.js +0 -146
- package/lib/esm/src/client/render.js.map +0 -1
- package/lib/esm/src/client/squads.js +0 -163
- package/lib/esm/src/client/squads.js.map +0 -1
- package/lib/esm/src/client/trade.js +0 -215
- package/lib/esm/src/client/trade.js.map +0 -1
- package/lib/esm/src/client/wormhole.js +0 -33
- package/lib/esm/src/client/wormhole.js.map +0 -1
- package/lib/esm/src/constants.js +0 -43
- package/lib/esm/src/constants.js.map +0 -1
- package/lib/esm/src/dbTypes.js +0 -2
- package/lib/esm/src/dbTypes.js.map +0 -1
- package/lib/esm/src/dbTypesV2.js +0 -2
- package/lib/esm/src/dbTypesV2.js.map +0 -1
- package/lib/esm/src/dbViewTypes.js +0 -2
- package/lib/esm/src/dbViewTypes.js.map +0 -1
- package/lib/esm/src/errorReporter.js +0 -117
- package/lib/esm/src/errorReporter.js.map +0 -1
- package/lib/esm/src/index.js +0 -16
- package/lib/esm/src/index.js.map +0 -1
- package/lib/esm/src/logger.js +0 -18
- package/lib/esm/src/logger.js.map +0 -1
- package/lib/types/src/backfiller.d.ts +0 -62
- package/lib/types/src/backfiller.d.ts.map +0 -1
- package/lib/types/src/client/onchain.d.ts +0 -24
- package/lib/types/src/client/onchain.d.ts.map +0 -1
- package/lib/types/src/client/pg/base.d.ts +0 -65
- package/lib/types/src/client/pg/base.d.ts.map +0 -1
- package/lib/types/src/client/pg/v1.d.ts +0 -137
- package/lib/types/src/client/pg/v1.d.ts.map +0 -1
- package/lib/types/src/client/pg/v2.d.ts +0 -36
- package/lib/types/src/client/pg/v2.d.ts.map +0 -1
- package/lib/types/src/client/pg/v2Client.d.ts +0 -273
- package/lib/types/src/client/pg/v2Client.d.ts.map +0 -1
- package/lib/types/src/client/pg/viewClient.d.ts +0 -55
- package/lib/types/src/client/pg/viewClient.d.ts.map +0 -1
- package/lib/types/src/client/price.d.ts +0 -12
- package/lib/types/src/client/price.d.ts.map +0 -1
- package/lib/types/src/client/render.d.ts +0 -23
- package/lib/types/src/client/render.d.ts.map +0 -1
- package/lib/types/src/client/squads.d.ts +0 -17
- package/lib/types/src/client/squads.d.ts.map +0 -1
- package/lib/types/src/client/trade.d.ts +0 -24
- package/lib/types/src/client/trade.d.ts.map +0 -1
- package/lib/types/src/client/wormhole.d.ts +0 -9
- package/lib/types/src/client/wormhole.d.ts.map +0 -1
- package/lib/types/src/constants.d.ts +0 -41
- package/lib/types/src/constants.d.ts.map +0 -1
- package/lib/types/src/dbTypes.d.ts +0 -123
- package/lib/types/src/dbTypes.d.ts.map +0 -1
- package/lib/types/src/dbTypesV2.d.ts +0 -163
- package/lib/types/src/dbTypesV2.d.ts.map +0 -1
- package/lib/types/src/dbViewTypes.d.ts +0 -35
- package/lib/types/src/dbViewTypes.d.ts.map +0 -1
- package/lib/types/src/errorReporter.d.ts +0 -13
- package/lib/types/src/errorReporter.d.ts.map +0 -1
- package/lib/types/src/index.d.ts +0 -16
- package/lib/types/src/index.d.ts.map +0 -1
- package/lib/types/src/logger.d.ts +0 -16
- package/lib/types/src/logger.d.ts.map +0 -1
|
@@ -1,1436 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
-
};
|
|
37
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
-
exports.pgClient = exports.batchFinishedAtMedian = exports.POLYGON_UPGRADE_TABLE = exports.ENTITY_EPOCH_INFO_TABLE = exports.BURN_ADJUSTMENT_TABLE = exports.NETWORK_REVENUE_TABLE = exports.BRIDGE_TRANSFER_TABLE = exports.BURN_TABLE = exports.JOB_TABLE = exports.JOB_ID_TABLE = exports.LIABILITY_TABLE = exports.SOL_TRANSFER_TABLE = exports.ENTITY_TABLE = exports.MANUAL_BURN_TABLE = exports.EPOCH_TABLE = exports.SOL_TX_TABLE = void 0;
|
|
39
|
-
const kysely_1 = require("kysely");
|
|
40
|
-
const logger_1 = require("../../logger");
|
|
41
|
-
const pg_1 = require("pg");
|
|
42
|
-
const pg = __importStar(require("pg"));
|
|
43
|
-
const base_1 = require("./base");
|
|
44
|
-
exports.SOL_TX_TABLE = 'sol_tx';
|
|
45
|
-
exports.EPOCH_TABLE = 'epoch';
|
|
46
|
-
exports.MANUAL_BURN_TABLE = 'manual_burn';
|
|
47
|
-
exports.ENTITY_TABLE = 'entity';
|
|
48
|
-
exports.SOL_TRANSFER_TABLE = 'sol_transfer1';
|
|
49
|
-
exports.LIABILITY_TABLE = 'liability';
|
|
50
|
-
exports.JOB_ID_TABLE = 'job_id';
|
|
51
|
-
exports.JOB_TABLE = 'job';
|
|
52
|
-
exports.BURN_TABLE = 'burn';
|
|
53
|
-
exports.BRIDGE_TRANSFER_TABLE = 'bridge_transfer';
|
|
54
|
-
exports.NETWORK_REVENUE_TABLE = 'network_revenue';
|
|
55
|
-
exports.BURN_ADJUSTMENT_TABLE = 'burn_adjustment';
|
|
56
|
-
exports.ENTITY_EPOCH_INFO_TABLE = 'entity_epoch_info';
|
|
57
|
-
exports.POLYGON_UPGRADE_TABLE = 'polygon_upgrade';
|
|
58
|
-
BigInt.prototype.toJSON = function () {
|
|
59
|
-
return this.toString();
|
|
60
|
-
};
|
|
61
|
-
const base_2 = require("./base");
|
|
62
|
-
const moment_1 = __importDefault(require("moment"));
|
|
63
|
-
pg.types.setTypeParser(1114, (str) => moment_1.default.utc(str).toDate());
|
|
64
|
-
pg.defaults.parseInputDatesAsUTC = true;
|
|
65
|
-
/* ;(Date.prototype as any).toPostgres = function (prepareValue: (v: any) => any) {
|
|
66
|
-
return this.toISOString()
|
|
67
|
-
}
|
|
68
|
-
*/
|
|
69
|
-
const batchFinishedAtMedian = (jobs) => {
|
|
70
|
-
const pricedAt = jobs.length % 2 == 0
|
|
71
|
-
? new Date(Math.trunc((Date.parse(jobs[jobs.length / 2].finishedAt) +
|
|
72
|
-
Date.parse(jobs[jobs.length / 2 - 1].finishedAt)) /
|
|
73
|
-
2))
|
|
74
|
-
: new Date(Date.parse(jobs[Math.floor(jobs.length / 2)].finishedAt));
|
|
75
|
-
return pricedAt;
|
|
76
|
-
};
|
|
77
|
-
exports.batchFinishedAtMedian = batchFinishedAtMedian;
|
|
78
|
-
const pgClient = (config) => {
|
|
79
|
-
// Single pg pool that we can also use for ad-hoc performance tests outside Kysely
|
|
80
|
-
const pgPool = new pg_1.Pool(config);
|
|
81
|
-
pgPool.on('connect', (c) => console.log('new pg socket created'));
|
|
82
|
-
pgPool.on('error', (err) => console.log('idle pg client died and was replaced:', err));
|
|
83
|
-
const dialect = new kysely_1.PostgresDialect({ pool: pgPool });
|
|
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' || p.channel == 'availability'
|
|
153
|
-
? new Date()
|
|
154
|
-
: undefined,
|
|
155
|
-
})
|
|
156
|
-
.onConflict((oc) => oc.columns(['ui', 'channel']).doNothing())
|
|
157
|
-
.returning((eb) => ['id as id'])
|
|
158
|
-
.executeTakeFirst());
|
|
159
|
-
log.debug(`inserted epoch ${id}`);
|
|
160
|
-
return id;
|
|
161
|
-
});
|
|
162
|
-
const updateTotalRndr = (db, p, totalRndr, savedAt, log) => __awaiter(void 0, void 0, void 0, function* () {
|
|
163
|
-
const res = (yield db
|
|
164
|
-
.updateTable(exports.EPOCH_TABLE)
|
|
165
|
-
.set({
|
|
166
|
-
total_rndr: totalRndr,
|
|
167
|
-
saved_at: savedAt,
|
|
168
|
-
})
|
|
169
|
-
.where('channel', '=', p.channel)
|
|
170
|
-
.where('ui', '=', String(p.uiEpochId))
|
|
171
|
-
.executeTakeFirst());
|
|
172
|
-
log.info(`set ${res.numUpdatedRows} rows. channel: ${p.channel}, ui: ${p.uiEpochId} -> totalRndr ${totalRndr} `);
|
|
173
|
-
});
|
|
174
|
-
const fetchJobs1 = (db, f, log = logger_1.consoleLogger) => __awaiter(void 0, void 0, void 0, function* () {
|
|
175
|
-
let q = db.selectFrom('job').selectAll();
|
|
176
|
-
//.where('job.burn_id', 'is not', null)
|
|
177
|
-
if (f.completedAtAfter) {
|
|
178
|
-
q = q.where('completed_at', '>=', f.completedAtAfter);
|
|
179
|
-
}
|
|
180
|
-
if (f.completedAtBefore) {
|
|
181
|
-
q = q.where('completed_at', '<=', f.completedAtBefore);
|
|
182
|
-
}
|
|
183
|
-
if (f.ids) {
|
|
184
|
-
q = q.where('id', 'in', f.ids);
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
if (f.burned == false) {
|
|
188
|
-
q = q.where('burn_id', 'is', null);
|
|
189
|
-
}
|
|
190
|
-
else if (f.burned == true) {
|
|
191
|
-
q = q.where('burn_id', 'is not', null);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
q = q.orderBy('completed_at asc');
|
|
195
|
-
log.info(`fetchJobs q ${q.compile().sql} ${JSON.stringify(f)}`);
|
|
196
|
-
const rows = yield q.execute();
|
|
197
|
-
return rows.map((r) => ({
|
|
198
|
-
id: Number(r.id),
|
|
199
|
-
completedAt: r.completed_at,
|
|
200
|
-
rndrUsed: BigInt(r.render_amt),
|
|
201
|
-
insertedAt: r.inserted_at,
|
|
202
|
-
}));
|
|
203
|
-
});
|
|
204
|
-
const createBridgeTransfers = (db, bts, log = logger_1.consoleLogger) => __awaiter(void 0, void 0, void 0, function* () {
|
|
205
|
-
const sequenceNumbers = [];
|
|
206
|
-
const dbEpochIds = [];
|
|
207
|
-
for (const bt of bts) {
|
|
208
|
-
let { solKey: toSolKey, seq, createdAt, amount, upgradeBracket, priorSupply, points, epochId, } = bt;
|
|
209
|
-
let entityId;
|
|
210
|
-
if (toSolKey && toSolKey != '') {
|
|
211
|
-
entityId = yield getOrInsertEntity(db, { solKey: toSolKey });
|
|
212
|
-
}
|
|
213
|
-
const dbEpochId = yield getOrInsertEpoch(db, {
|
|
214
|
-
channel: 'upgrade',
|
|
215
|
-
uiEpochId: epochId,
|
|
216
|
-
}, log);
|
|
217
|
-
dbEpochIds.push(BigInt(dbEpochId));
|
|
218
|
-
const q = db
|
|
219
|
-
.insertInto(exports.BRIDGE_TRANSFER_TABLE)
|
|
220
|
-
.values({
|
|
221
|
-
entity_id: entityId,
|
|
222
|
-
sequence: seq,
|
|
223
|
-
created_at: createdAt,
|
|
224
|
-
amount: amount,
|
|
225
|
-
upgrade_bracket: upgradeBracket,
|
|
226
|
-
prior_supply: priorSupply,
|
|
227
|
-
points: points,
|
|
228
|
-
epoch_id: dbEpochId,
|
|
229
|
-
})
|
|
230
|
-
.onConflict((oc) => oc.column('sequence').doUpdateSet({
|
|
231
|
-
created_at: createdAt,
|
|
232
|
-
amount: amount,
|
|
233
|
-
upgrade_bracket: upgradeBracket,
|
|
234
|
-
prior_supply: priorSupply,
|
|
235
|
-
points: points,
|
|
236
|
-
epoch_id: dbEpochId,
|
|
237
|
-
entity_id: entityId,
|
|
238
|
-
}))
|
|
239
|
-
.returning('sequence');
|
|
240
|
-
const c = q.compile();
|
|
241
|
-
log.debug(`inserting bridge transfer ${JSON.stringify(bt)} -> ${c.sql} ps ${c.parameters}`);
|
|
242
|
-
const { sequence: seqNum } = yield q.executeTakeFirst();
|
|
243
|
-
log.debug(`inserted bridge transfer sequence ${seq}`);
|
|
244
|
-
sequenceNumbers.push(BigInt(seqNum));
|
|
245
|
-
}
|
|
246
|
-
return { sequenceNumbers: sequenceNumbers };
|
|
247
|
-
});
|
|
248
|
-
const createPayments = (db, ps, log = logger_1.consoleLogger) => __awaiter(void 0, void 0, void 0, function* () {
|
|
249
|
-
const ids = [];
|
|
250
|
-
const epochDbIds = [];
|
|
251
|
-
for (const p of ps) {
|
|
252
|
-
let liabilityId;
|
|
253
|
-
let transferId;
|
|
254
|
-
if (p.liability) {
|
|
255
|
-
let { id: dbId, solKey, channel, epochId, createdAt, amountDue, deviceId, points, obhUsed, } = p.liability;
|
|
256
|
-
try {
|
|
257
|
-
let entityId;
|
|
258
|
-
if (solKey && solKey != '') {
|
|
259
|
-
entityId = yield getOrInsertEntity(db, { solKey });
|
|
260
|
-
}
|
|
261
|
-
const dbEpochId = yield getOrInsertEpoch(db, {
|
|
262
|
-
channel,
|
|
263
|
-
uiEpochId: epochId,
|
|
264
|
-
}, log);
|
|
265
|
-
epochDbIds.push(BigInt(dbEpochId));
|
|
266
|
-
log.debug(`inserting liability ${JSON.stringify(p.liability)}`);
|
|
267
|
-
const { id } = yield db
|
|
268
|
-
.insertInto(exports.LIABILITY_TABLE)
|
|
269
|
-
.values({
|
|
270
|
-
id: dbId,
|
|
271
|
-
entity_id: entityId,
|
|
272
|
-
epoch_id: dbEpochId,
|
|
273
|
-
channel,
|
|
274
|
-
created_at: createdAt !== null && createdAt !== void 0 ? createdAt : new Date(),
|
|
275
|
-
amount_due: amountDue,
|
|
276
|
-
device_id: deviceId,
|
|
277
|
-
points,
|
|
278
|
-
obh_used: obhUsed,
|
|
279
|
-
})
|
|
280
|
-
.returning((eb) => ['id as id'])
|
|
281
|
-
.executeTakeFirstOrThrow();
|
|
282
|
-
log.debug(`inserted liability ${id}`);
|
|
283
|
-
liabilityId = BigInt(id);
|
|
284
|
-
}
|
|
285
|
-
catch (e) {
|
|
286
|
-
throw new Error(`failed inserting solKey ${solKey} epochId ${epochId} ${JSON.stringify(p.liability)}: ${e}`);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
if (p.transfer) {
|
|
290
|
-
let { executedAt, symbol, amountPayed, solTx, channel, fromSolKey, toSolKey, id: dbId, } = p.transfer;
|
|
291
|
-
if (!fromSolKey || !toSolKey || !solTx) {
|
|
292
|
-
log.debug(`skipping transfer. one of undefined: fromKey ${fromSolKey} toKey ${toSolKey} tx ${solTx}`);
|
|
293
|
-
continue;
|
|
294
|
-
}
|
|
295
|
-
log.debug(`inserting transfer ${JSON.stringify(p.transfer)}`);
|
|
296
|
-
const fromEntityId = yield getOrInsertEntity(db, { solKey: fromSolKey });
|
|
297
|
-
const toEntityId = yield getOrInsertEntity(db, { solKey: toSolKey });
|
|
298
|
-
const solTxId = yield getOrInsertTx(db, {
|
|
299
|
-
sig: solTx,
|
|
300
|
-
executedAt,
|
|
301
|
-
}, log);
|
|
302
|
-
const { id } = yield db
|
|
303
|
-
.insertInto(exports.SOL_TRANSFER_TABLE)
|
|
304
|
-
.values({
|
|
305
|
-
id: dbId,
|
|
306
|
-
sol_tx_id: solTxId,
|
|
307
|
-
from_entity_id: fromEntityId,
|
|
308
|
-
to_entity_id: toEntityId,
|
|
309
|
-
channel,
|
|
310
|
-
symbol,
|
|
311
|
-
amount_payed: amountPayed,
|
|
312
|
-
})
|
|
313
|
-
.returning((eb) => ['id as id'])
|
|
314
|
-
.executeTakeFirstOrThrow();
|
|
315
|
-
transferId = BigInt(id);
|
|
316
|
-
}
|
|
317
|
-
ids.push({ transferId, liabilityId });
|
|
318
|
-
}
|
|
319
|
-
return { epochDbIds, inserted: ids };
|
|
320
|
-
});
|
|
321
|
-
return {
|
|
322
|
-
db: {
|
|
323
|
-
commit: () => { },
|
|
324
|
-
rollback: () => { },
|
|
325
|
-
trx: db,
|
|
326
|
-
},
|
|
327
|
-
startTransaction(log = logger_1.consoleLogger) {
|
|
328
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
329
|
-
return (0, base_2.begin)(db, log);
|
|
330
|
-
});
|
|
331
|
-
},
|
|
332
|
-
createPayments(db, ps, log = logger_1.consoleLogger) {
|
|
333
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
334
|
-
return createPayments(db.trx, ps, log);
|
|
335
|
-
});
|
|
336
|
-
},
|
|
337
|
-
fetchTransfers(db, f, log = logger_1.consoleLogger) {
|
|
338
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
339
|
-
let lastId = undefined;
|
|
340
|
-
if (f.cursor) {
|
|
341
|
-
lastId = atob(f.cursor);
|
|
342
|
-
}
|
|
343
|
-
let q = db.trx
|
|
344
|
-
.selectFrom(exports.SOL_TRANSFER_TABLE)
|
|
345
|
-
.select([
|
|
346
|
-
'sol_transfer1.id',
|
|
347
|
-
'sol_transfer1.channel',
|
|
348
|
-
'sol_transfer1.symbol',
|
|
349
|
-
'sol_transfer1.amount_payed',
|
|
350
|
-
])
|
|
351
|
-
.innerJoin('sol_tx', 'sol_tx.id', 'sol_transfer1.sol_tx_id')
|
|
352
|
-
.select(['sol_tx.sig', 'sol_tx.executed_at'])
|
|
353
|
-
.innerJoin('entity as f', 'f.id', 'sol_transfer1.from_entity_id')
|
|
354
|
-
.select(['f.sol_key as from_sol_key'])
|
|
355
|
-
.innerJoin('entity as t', 't.id', 'sol_transfer1.to_entity_id')
|
|
356
|
-
.select(['t.sol_key as to_sol_key']);
|
|
357
|
-
if (f.executedAfter) {
|
|
358
|
-
q = q.where('sol_tx.executed_at', '>=', f.executedAfter);
|
|
359
|
-
}
|
|
360
|
-
if (f.executedBefore) {
|
|
361
|
-
q = q.where('sol_tx.executed_at', '<=', f.executedBefore);
|
|
362
|
-
}
|
|
363
|
-
if (f.channels) {
|
|
364
|
-
q = q.where('sol_transfer1.channel', 'in', f.channels);
|
|
365
|
-
}
|
|
366
|
-
if (lastId) {
|
|
367
|
-
q = q.where('sol_transfer1.id', '>', lastId);
|
|
368
|
-
}
|
|
369
|
-
let limit = 10000;
|
|
370
|
-
if (f.limit) {
|
|
371
|
-
limit = f.limit;
|
|
372
|
-
}
|
|
373
|
-
q = q.limit(limit).orderBy(`${exports.SOL_TRANSFER_TABLE}.id`);
|
|
374
|
-
const comp = q.compile();
|
|
375
|
-
const { rows } = yield (0, base_1.timedQuery)(pgPool, comp.sql, comp.parameters, 'fetchTransfers', log);
|
|
376
|
-
const trs = rows.map((r) => ({
|
|
377
|
-
id: BigInt(r.id),
|
|
378
|
-
channel: r.channel,
|
|
379
|
-
symbol: r.symbol,
|
|
380
|
-
amountPayed: BigInt(r.amount_payed),
|
|
381
|
-
solTx: r.sig,
|
|
382
|
-
executedAt: r.executed_at,
|
|
383
|
-
fromSolKey: r.from_sol_key,
|
|
384
|
-
toSolKey: r.to_sol_key,
|
|
385
|
-
}));
|
|
386
|
-
return {
|
|
387
|
-
transfers: trs,
|
|
388
|
-
cursor: trs.length === limit
|
|
389
|
-
? btoa(trs[trs.length - 1].id.toString())
|
|
390
|
-
: undefined,
|
|
391
|
-
};
|
|
392
|
-
});
|
|
393
|
-
},
|
|
394
|
-
fetchEpochs(db, f, log = logger_1.consoleLogger) {
|
|
395
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
396
|
-
let q = db.trx.selectFrom(exports.EPOCH_TABLE).selectAll();
|
|
397
|
-
if (f.channels) {
|
|
398
|
-
q = q.where('channel', 'in', f.channels);
|
|
399
|
-
}
|
|
400
|
-
if (f.startTimeAfter) {
|
|
401
|
-
q = q.where('epoch.start_time', '>', f.startTimeAfter);
|
|
402
|
-
}
|
|
403
|
-
if (f.endTimeBefore) {
|
|
404
|
-
q = q.where('epoch.end_time', '<', f.endTimeBefore);
|
|
405
|
-
}
|
|
406
|
-
q = q.orderBy('epoch.start_time');
|
|
407
|
-
const comp = q.compile();
|
|
408
|
-
const { rows } = yield (0, base_1.timedQuery)(pgPool, comp.sql, comp.parameters, 'fetchEpochs', log);
|
|
409
|
-
return rows.map((e) => ({
|
|
410
|
-
channel: e.channel,
|
|
411
|
-
startTime: e.start_time,
|
|
412
|
-
endTime: e.end_time,
|
|
413
|
-
ui: Number(e.ui),
|
|
414
|
-
totalRndr: Number(e.total_rndr),
|
|
415
|
-
}));
|
|
416
|
-
});
|
|
417
|
-
},
|
|
418
|
-
fetchLiabilities(db, f, log = logger_1.consoleLogger) {
|
|
419
|
-
var _a;
|
|
420
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
421
|
-
let offset = 0;
|
|
422
|
-
let orderBy = (_a = f.orderBy) !== null && _a !== void 0 ? _a : 'epoch.ui,liability.id';
|
|
423
|
-
if (f.cursor) {
|
|
424
|
-
const c = JSON.parse(atob(f.cursor));
|
|
425
|
-
if (c.offset) {
|
|
426
|
-
offset = c.offset;
|
|
427
|
-
}
|
|
428
|
-
if (c.orderBy) {
|
|
429
|
-
orderBy = c.orderBy;
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
let q = db.trx
|
|
433
|
-
.selectFrom(exports.LIABILITY_TABLE)
|
|
434
|
-
.selectAll()
|
|
435
|
-
.innerJoin(exports.ENTITY_TABLE, `${exports.LIABILITY_TABLE}.entity_id`, 'entity.id')
|
|
436
|
-
.select(['entity.sol_key'])
|
|
437
|
-
.innerJoin(exports.EPOCH_TABLE, `${exports.EPOCH_TABLE}.id`, 'liability.epoch_id')
|
|
438
|
-
.select(['epoch.ui', 'epoch.start_time', 'epoch.end_time']);
|
|
439
|
-
if (f.epochEndedAtAfter) {
|
|
440
|
-
q = q.where('epoch.end_time', '>=', f.epochEndedAtAfter);
|
|
441
|
-
}
|
|
442
|
-
if (f.epochEndedAtBefore) {
|
|
443
|
-
q = q.where('epoch.end_time', '<=', f.epochEndedAtBefore);
|
|
444
|
-
}
|
|
445
|
-
if (f.epochId) {
|
|
446
|
-
q = q.where('epoch.ui', '=', f.epochId.toString());
|
|
447
|
-
}
|
|
448
|
-
if (f.channels) {
|
|
449
|
-
q = q.where('liability.channel', 'in', f.channels);
|
|
450
|
-
}
|
|
451
|
-
let limit = 10000;
|
|
452
|
-
if (f.limit) {
|
|
453
|
-
limit = f.limit;
|
|
454
|
-
}
|
|
455
|
-
// @ts-ignore
|
|
456
|
-
q = q.limit(limit).orderBy(orderBy.split(','));
|
|
457
|
-
if (offset) {
|
|
458
|
-
q = q.offset(offset);
|
|
459
|
-
}
|
|
460
|
-
const comp = q.compile();
|
|
461
|
-
const { rows } = yield (0, base_1.timedQuery)(pgPool, comp.sql, comp.parameters, 'fetchLiabilities', log);
|
|
462
|
-
const ls = [];
|
|
463
|
-
for (const l of rows) {
|
|
464
|
-
ls.push({
|
|
465
|
-
id: BigInt(l.id),
|
|
466
|
-
solKey: l.sol_key,
|
|
467
|
-
epochId: Number(l.ui),
|
|
468
|
-
channel: l.channel,
|
|
469
|
-
createdAt: l.created_at,
|
|
470
|
-
amountDue: BigInt(l.amount_due),
|
|
471
|
-
deviceId: l.device_id,
|
|
472
|
-
obhUsed: l.obh_used,
|
|
473
|
-
points: l.points ? BigInt(l.points) : undefined,
|
|
474
|
-
startTime: l.start_time,
|
|
475
|
-
endTime: l.end_time,
|
|
476
|
-
ui: Number(l.ui),
|
|
477
|
-
});
|
|
478
|
-
}
|
|
479
|
-
return {
|
|
480
|
-
liabilities: ls,
|
|
481
|
-
cursor: ls.length == limit
|
|
482
|
-
? btoa(JSON.stringify({ offset: ls.length + offset, orderBy }))
|
|
483
|
-
: undefined,
|
|
484
|
-
};
|
|
485
|
-
});
|
|
486
|
-
},
|
|
487
|
-
getLiabilitiesAndTransfers(f, log = logger_1.consoleLogger) {
|
|
488
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
489
|
-
let lq = db
|
|
490
|
-
.selectFrom(exports.LIABILITY_TABLE)
|
|
491
|
-
.selectAll()
|
|
492
|
-
.innerJoin(exports.ENTITY_TABLE, `${exports.LIABILITY_TABLE}.entity_id`, 'entity.id')
|
|
493
|
-
.select(['entity.sol_key'])
|
|
494
|
-
.innerJoin(exports.EPOCH_TABLE, `${exports.EPOCH_TABLE}.id`, 'liability.epoch_id')
|
|
495
|
-
.select(['epoch.ui', 'epoch.start_time', 'epoch.end_time'])
|
|
496
|
-
.where('entity.sol_key', '=', f.user);
|
|
497
|
-
let tq = db
|
|
498
|
-
.selectFrom(exports.SOL_TRANSFER_TABLE)
|
|
499
|
-
.selectAll()
|
|
500
|
-
.innerJoin(exports.SOL_TX_TABLE, 'sol_tx.id', 'sol_transfer1.sol_tx_id')
|
|
501
|
-
.select(['sol_tx.sig', 'sol_tx.executed_at'])
|
|
502
|
-
.innerJoin(`${exports.ENTITY_TABLE} as t`, `${exports.SOL_TRANSFER_TABLE}.to_entity_id`, 't.id')
|
|
503
|
-
.select(['t.sol_key as to_sol_key'])
|
|
504
|
-
.innerJoin(`${exports.ENTITY_TABLE} as f`, `${exports.SOL_TRANSFER_TABLE}.from_entity_id`, 'f.id')
|
|
505
|
-
.select(['f.sol_key as from_sol_key'])
|
|
506
|
-
.where('t.sol_key', '=', f.user);
|
|
507
|
-
log.info(`fetch liabilities ${lq.compile().sql} ps ${JSON.stringify(f)}`);
|
|
508
|
-
log.info(`fetch transfers ${tq.compile().sql} ps ${JSON.stringify(f)}`);
|
|
509
|
-
const lc = lq.compile();
|
|
510
|
-
const tc = tq.compile();
|
|
511
|
-
const [liabRes, transRes] = yield Promise.all([
|
|
512
|
-
(0, base_1.timedQuery)(pgPool, lc.sql, lc.parameters, 'getLiabilitiesAndTransfers->liabilities', log),
|
|
513
|
-
(0, base_1.timedQuery)(pgPool, tc.sql, tc.parameters, 'getLiabilitiesAndTransfers->transfers', log),
|
|
514
|
-
]);
|
|
515
|
-
const rows = [liabRes.rows, transRes.rows];
|
|
516
|
-
const epochs = {};
|
|
517
|
-
const liabilities = [];
|
|
518
|
-
for (const l of rows[0]) {
|
|
519
|
-
epochs[`${l.ui}-${l.channel}`] = {
|
|
520
|
-
startTime: l.start_time,
|
|
521
|
-
endTime: l.end_time,
|
|
522
|
-
channel: l.channel,
|
|
523
|
-
ui: Number(l.ui),
|
|
524
|
-
};
|
|
525
|
-
liabilities.push({
|
|
526
|
-
id: BigInt(l.id),
|
|
527
|
-
solKey: l.sol_key,
|
|
528
|
-
epochId: Number(l.ui),
|
|
529
|
-
channel: l.channel,
|
|
530
|
-
createdAt: l.created_at,
|
|
531
|
-
amountDue: BigInt(l.amount_due),
|
|
532
|
-
deviceId: l.device_id,
|
|
533
|
-
points: l.points ? BigInt(l.points) : undefined,
|
|
534
|
-
});
|
|
535
|
-
}
|
|
536
|
-
const transfers = rows[1].map((r) => ({
|
|
537
|
-
id: BigInt(r.id),
|
|
538
|
-
fromSolKey: r.from_sol_key,
|
|
539
|
-
toSolKey: r.to_sol_key,
|
|
540
|
-
symbol: r.symbol,
|
|
541
|
-
channel: r.channel,
|
|
542
|
-
solTx: r.sig,
|
|
543
|
-
executedAt: r.executed_at,
|
|
544
|
-
amountPayed: BigInt(r.amount_payed),
|
|
545
|
-
}));
|
|
546
|
-
return { transfers, liabilities, epochs: Object.values(epochs) };
|
|
547
|
-
});
|
|
548
|
-
},
|
|
549
|
-
getOutstanding(f, log = logger_1.consoleLogger) {
|
|
550
|
-
var _a, _b;
|
|
551
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
552
|
-
let epochUi;
|
|
553
|
-
let epochEndTime;
|
|
554
|
-
if (f.startEpochId.id) {
|
|
555
|
-
if (((_a = f.channels) === null || _a === void 0 ? void 0 : _a.includes('node_operator')) ||
|
|
556
|
-
((_b = f.channels) === null || _b === void 0 ? void 0 : _b.includes('availability'))) {
|
|
557
|
-
let qb = db
|
|
558
|
-
.selectFrom(exports.EPOCH_TABLE)
|
|
559
|
-
.selectAll(exports.EPOCH_TABLE)
|
|
560
|
-
.where('ui', '>=', f.startEpochId.id.toString())
|
|
561
|
-
.where('channel', 'in', f.channels);
|
|
562
|
-
if (f.startEpochId.savedAtNotNull) {
|
|
563
|
-
qb = qb.where('saved_at', 'is not', null);
|
|
564
|
-
}
|
|
565
|
-
const e = yield qb
|
|
566
|
-
.orderBy('ui', 'desc')
|
|
567
|
-
.limit(1)
|
|
568
|
-
.executeTakeFirstOrThrow();
|
|
569
|
-
epochEndTime = e.end_time;
|
|
570
|
-
epochUi = e.ui;
|
|
571
|
-
}
|
|
572
|
-
else {
|
|
573
|
-
epochUi = f.startEpochId.id.toString();
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
let q = db
|
|
577
|
-
.selectFrom((eb) => eb
|
|
578
|
-
.selectFrom((eb) => {
|
|
579
|
-
let b = eb
|
|
580
|
-
.selectFrom(exports.SOL_TRANSFER_TABLE)
|
|
581
|
-
.select(({ fn }) => fn.sum(`amount_payed`).as('payed'))
|
|
582
|
-
.innerJoin(exports.ENTITY_TABLE, `${exports.SOL_TRANSFER_TABLE}.to_entity_id`, 'entity.id')
|
|
583
|
-
.select(['entity.sol_key'])
|
|
584
|
-
.groupBy('entity.sol_key');
|
|
585
|
-
if (f.banned) {
|
|
586
|
-
b = b.where('entity.banned', '=', true);
|
|
587
|
-
}
|
|
588
|
-
else {
|
|
589
|
-
b = b.where('entity.banned', '=', false);
|
|
590
|
-
}
|
|
591
|
-
if (epochEndTime) {
|
|
592
|
-
b = b
|
|
593
|
-
.innerJoin(exports.SOL_TX_TABLE, `${exports.SOL_TX_TABLE}.id`, `${exports.SOL_TRANSFER_TABLE}.sol_tx_id`)
|
|
594
|
-
.where(`${exports.SOL_TX_TABLE}.executed_at`, '>', epochEndTime);
|
|
595
|
-
}
|
|
596
|
-
if (f.channels && f.channels.length > 0) {
|
|
597
|
-
b = b.where((eb) => eb.or(f.channels.map((c) => eb('sol_transfer1.channel', '=', c))));
|
|
598
|
-
}
|
|
599
|
-
else {
|
|
600
|
-
b = b.where((eb) => eb.or([
|
|
601
|
-
eb('sol_transfer1.channel', '=', 'node_operator'),
|
|
602
|
-
eb('sol_transfer1.channel', '=', 'availability'),
|
|
603
|
-
]));
|
|
604
|
-
}
|
|
605
|
-
return b.as('y');
|
|
606
|
-
})
|
|
607
|
-
.fullJoin((eb) => {
|
|
608
|
-
let b = eb
|
|
609
|
-
.selectFrom(exports.LIABILITY_TABLE)
|
|
610
|
-
.select(({ fn }) => fn.sum(`amount_due`).as('due'))
|
|
611
|
-
.innerJoin(exports.ENTITY_TABLE, `${exports.LIABILITY_TABLE}.entity_id`, 'entity.id')
|
|
612
|
-
.select(['entity.sol_key'])
|
|
613
|
-
.groupBy('entity.sol_key');
|
|
614
|
-
if (f.banned) {
|
|
615
|
-
b = b.where('entity.banned', '=', true);
|
|
616
|
-
}
|
|
617
|
-
else {
|
|
618
|
-
b = b.where('entity.banned', '=', false);
|
|
619
|
-
}
|
|
620
|
-
if (epochUi) {
|
|
621
|
-
b = b
|
|
622
|
-
.innerJoin(exports.EPOCH_TABLE, `${exports.LIABILITY_TABLE}.epoch_id`, `${exports.EPOCH_TABLE}.id`)
|
|
623
|
-
.where(`${exports.EPOCH_TABLE}.ui`, '>=', epochUi);
|
|
624
|
-
}
|
|
625
|
-
if (f.channels && f.channels.length > 0) {
|
|
626
|
-
b = b.where((eb) => eb.or(f.channels.map((c) => eb('liability.channel', '=', c))));
|
|
627
|
-
}
|
|
628
|
-
else {
|
|
629
|
-
b = b.where((eb) => eb.or([
|
|
630
|
-
eb('liability.channel', '=', 'node_operator'),
|
|
631
|
-
eb('liability.channel', '=', 'availability'),
|
|
632
|
-
]));
|
|
633
|
-
}
|
|
634
|
-
return b.as('x');
|
|
635
|
-
}, (join) => join.onRef('y.sol_key', '=', 'x.sol_key'))
|
|
636
|
-
.select(({ fn }) => [
|
|
637
|
-
fn.coalesce('y.sol_key', 'x.sol_key').as('sol_key'),
|
|
638
|
-
(0, kysely_1.sql) `${fn.coalesce(`x.due`, (0, kysely_1.sql) `0`)} -
|
|
639
|
-
${fn.coalesce(`y.payed`, (0, kysely_1.sql) `0`)}`.as('out'),
|
|
640
|
-
])
|
|
641
|
-
.as('z'))
|
|
642
|
-
.selectAll();
|
|
643
|
-
if (f.due == 'overpayed') {
|
|
644
|
-
q = q.where('out', '<', '0');
|
|
645
|
-
}
|
|
646
|
-
else if (f.due == 'underpayed') {
|
|
647
|
-
q = q.where('out', '>', '0');
|
|
648
|
-
}
|
|
649
|
-
log.info(`${q.compile().sql} ps ${JSON.stringify(Object.assign(Object.assign({}, f), { epochEndTime,
|
|
650
|
-
epochUi }))}`);
|
|
651
|
-
const res = yield q.execute();
|
|
652
|
-
const out = {};
|
|
653
|
-
for (const r of res) {
|
|
654
|
-
out[r.sol_key] = BigInt(r.out);
|
|
655
|
-
}
|
|
656
|
-
return out;
|
|
657
|
-
});
|
|
658
|
-
},
|
|
659
|
-
fetchNodeOperatorEpoch(epochId, log = logger_1.consoleLogger) {
|
|
660
|
-
var _a;
|
|
661
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
662
|
-
const q = db
|
|
663
|
-
.selectFrom(exports.JOB_ID_TABLE)
|
|
664
|
-
.innerJoin(exports.EPOCH_TABLE, 'epoch.id', 'job_id.epoch_id')
|
|
665
|
-
.leftJoin(exports.MANUAL_BURN_TABLE, 'manual_burn.epoch_id', 'epoch.id')
|
|
666
|
-
.where('epoch.ui', '=', epochId.toString())
|
|
667
|
-
.where('epoch.channel', '=', 'node_operator')
|
|
668
|
-
.select([
|
|
669
|
-
'job_id.id',
|
|
670
|
-
'epoch.total_rndr',
|
|
671
|
-
'epoch.start_time',
|
|
672
|
-
'epoch.end_time',
|
|
673
|
-
'manual_burn.burned',
|
|
674
|
-
'manual_burn.tx_sig',
|
|
675
|
-
])
|
|
676
|
-
.orderBy('job_id.id asc');
|
|
677
|
-
log.debug(`fetch epoch info q ${q.compile().sql} epochId ${epochId}`);
|
|
678
|
-
const res = yield q.execute();
|
|
679
|
-
if (res.length == 0) {
|
|
680
|
-
throw new Error(`epoch ${epochId} not found`);
|
|
681
|
-
}
|
|
682
|
-
log.debug(`fetchNodeOperatorEpoch -> fetching liablities`);
|
|
683
|
-
const ks = yield db
|
|
684
|
-
.selectFrom(exports.LIABILITY_TABLE)
|
|
685
|
-
.innerJoin(exports.ENTITY_TABLE, 'liability.entity_id', 'entity.id')
|
|
686
|
-
.select(({ fn }) => [
|
|
687
|
-
(0, kysely_1.sql) `distinct
|
|
688
|
-
(entity.sol_key)`.as('sol_key'),
|
|
689
|
-
])
|
|
690
|
-
.innerJoin(exports.EPOCH_TABLE, 'liability.epoch_id', 'epoch.id')
|
|
691
|
-
.where('epoch.ui', '=', epochId.toString())
|
|
692
|
-
.where('epoch.channel', '=', 'node_operator')
|
|
693
|
-
.where('liability.channel', '=', 'node_operator')
|
|
694
|
-
.execute();
|
|
695
|
-
log.debug(`fetchNodeOperatorEpoch -> fetched liablities`);
|
|
696
|
-
const solKeys = ks.reduce((r, k) => {
|
|
697
|
-
r[k.sol_key] = {};
|
|
698
|
-
return r;
|
|
699
|
-
}, {});
|
|
700
|
-
return {
|
|
701
|
-
jobIds: res.map((r) => Number(r.id)),
|
|
702
|
-
totalRndr: BigInt(res[0].total_rndr),
|
|
703
|
-
startTime: res[0].start_time,
|
|
704
|
-
endTime: res[0].end_time,
|
|
705
|
-
burned: BigInt((_a = res[0].burned) !== null && _a !== void 0 ? _a : 0),
|
|
706
|
-
burnTx: res[0].tx_sig,
|
|
707
|
-
solKeys,
|
|
708
|
-
};
|
|
709
|
-
});
|
|
710
|
-
},
|
|
711
|
-
insertEpoch(db, epoch, log = logger_1.consoleLogger) {
|
|
712
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
713
|
-
yield getOrInsertEpoch(db.trx, Object.assign(Object.assign({}, epoch), { uiEpochId: epoch.ui }), log);
|
|
714
|
-
});
|
|
715
|
-
},
|
|
716
|
-
getEpoch(f, log = logger_1.consoleLogger) {
|
|
717
|
-
var _a;
|
|
718
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
719
|
-
const now = new Date(Date.now());
|
|
720
|
-
try {
|
|
721
|
-
let q = db
|
|
722
|
-
.selectFrom(exports.EPOCH_TABLE)
|
|
723
|
-
.selectAll()
|
|
724
|
-
.where('channel', '=', f.channel);
|
|
725
|
-
if (f.epochId) {
|
|
726
|
-
q = q.where('ui', '=', f.epochId.toString());
|
|
727
|
-
}
|
|
728
|
-
else {
|
|
729
|
-
q = q.where('end_time', '<=', now);
|
|
730
|
-
if (!f.savedAtNotNull) {
|
|
731
|
-
q = q.where('saved_at', 'is', null);
|
|
732
|
-
}
|
|
733
|
-
else {
|
|
734
|
-
q = q.where('saved_at', 'is not', null);
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
q = q.orderBy('end_time desc').limit(1);
|
|
738
|
-
const res = yield q.executeTakeFirstOrThrow();
|
|
739
|
-
return {
|
|
740
|
-
ui: Number(res.ui),
|
|
741
|
-
channel: f.channel,
|
|
742
|
-
startTime: res.start_time,
|
|
743
|
-
endTime: res.end_time,
|
|
744
|
-
savedAt: (_a = res.saved_at) !== null && _a !== void 0 ? _a : undefined,
|
|
745
|
-
};
|
|
746
|
-
}
|
|
747
|
-
catch (e) {
|
|
748
|
-
if (e instanceof kysely_1.NoResultError) {
|
|
749
|
-
log.info(`no node operator epoch found`);
|
|
750
|
-
return undefined;
|
|
751
|
-
}
|
|
752
|
-
throw e;
|
|
753
|
-
}
|
|
754
|
-
});
|
|
755
|
-
},
|
|
756
|
-
getLatestNodeOperatorEpoch(f, log = logger_1.consoleLogger) {
|
|
757
|
-
var _a;
|
|
758
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
759
|
-
const now = new Date(Date.now());
|
|
760
|
-
try {
|
|
761
|
-
let q = db
|
|
762
|
-
.selectFrom(exports.EPOCH_TABLE)
|
|
763
|
-
.selectAll()
|
|
764
|
-
.where('channel', '=', 'node_operator')
|
|
765
|
-
.where('end_time', '<=', now);
|
|
766
|
-
if (!f.latestDistributed) {
|
|
767
|
-
q = q.where('saved_at', 'is', null).orderBy('end_time asc');
|
|
768
|
-
}
|
|
769
|
-
else {
|
|
770
|
-
q = q.orderBy('end_time desc');
|
|
771
|
-
}
|
|
772
|
-
q = q.limit(1);
|
|
773
|
-
const res = yield q.executeTakeFirstOrThrow();
|
|
774
|
-
return {
|
|
775
|
-
epochId: BigInt(res.ui),
|
|
776
|
-
startedAt: res.start_time,
|
|
777
|
-
endedAt: res.end_time,
|
|
778
|
-
savedAt: (_a = res.saved_at) !== null && _a !== void 0 ? _a : undefined,
|
|
779
|
-
};
|
|
780
|
-
}
|
|
781
|
-
catch (e) {
|
|
782
|
-
if (e instanceof kysely_1.NoResultError) {
|
|
783
|
-
log.info(`no node operator epoch found`);
|
|
784
|
-
return undefined;
|
|
785
|
-
}
|
|
786
|
-
throw e;
|
|
787
|
-
}
|
|
788
|
-
});
|
|
789
|
-
},
|
|
790
|
-
insertEpochEntityInfo(db, eeis, log) {
|
|
791
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
792
|
-
for (const eei of eeis) {
|
|
793
|
-
yield db.trx.insertInto(exports.ENTITY_EPOCH_INFO_TABLE).values(eei).execute();
|
|
794
|
-
log.debug(`inserted eei ${JSON.stringify(eei)}`);
|
|
795
|
-
}
|
|
796
|
-
});
|
|
797
|
-
},
|
|
798
|
-
saveNodeOperatorEpoch(epochId, liabilities, jobIds, totalRndr, log = logger_1.consoleLogger) {
|
|
799
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
800
|
-
yield db
|
|
801
|
-
.transaction()
|
|
802
|
-
.setIsolationLevel('repeatable read')
|
|
803
|
-
.execute((trx) => __awaiter(this, void 0, void 0, function* () {
|
|
804
|
-
const { epochDbIds } = yield createPayments(trx, liabilities.map((l) => ({
|
|
805
|
-
liability: l,
|
|
806
|
-
})), log);
|
|
807
|
-
log.info(`inserted liabilities`);
|
|
808
|
-
for (const id of jobIds) {
|
|
809
|
-
yield trx
|
|
810
|
-
.insertInto(exports.JOB_ID_TABLE)
|
|
811
|
-
.values({
|
|
812
|
-
id: BigInt(id),
|
|
813
|
-
epoch_id: epochDbIds[0],
|
|
814
|
-
})
|
|
815
|
-
.executeTakeFirstOrThrow();
|
|
816
|
-
}
|
|
817
|
-
const now = new Date();
|
|
818
|
-
yield updateTotalRndr(db, { uiEpochId: epochId, channel: 'node_operator' }, totalRndr, now, log);
|
|
819
|
-
log.debug(`saved epoch`);
|
|
820
|
-
/*const now = new Date(Date.now())
|
|
821
|
-
const res = await trx
|
|
822
|
-
.updateTable(EPOCH_TABLE)
|
|
823
|
-
.set({
|
|
824
|
-
saved_at: now,
|
|
825
|
-
total_rndr: totalRndr,
|
|
826
|
-
})
|
|
827
|
-
.where('channel')
|
|
828
|
-
.where('ui', '=', epochId.toString())
|
|
829
|
-
.where('', '=')
|
|
830
|
-
.executeTakeFirstOrThrow()*/
|
|
831
|
-
}));
|
|
832
|
-
return;
|
|
833
|
-
});
|
|
834
|
-
},
|
|
835
|
-
produceBurnedJobs(jobs, log = logger_1.consoleLogger) {
|
|
836
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
837
|
-
yield db.transaction().execute((trx) => __awaiter(this, void 0, void 0, function* () {
|
|
838
|
-
var _a;
|
|
839
|
-
let num = BigInt(0);
|
|
840
|
-
for (const { amount, obhUsed, completedAt, jobId } of jobs) {
|
|
841
|
-
const res = yield trx
|
|
842
|
-
.insertInto(exports.JOB_TABLE)
|
|
843
|
-
.values({
|
|
844
|
-
id: jobId,
|
|
845
|
-
render_amt: amount,
|
|
846
|
-
completed_at: completedAt,
|
|
847
|
-
inserted_at: new Date(Date.now()),
|
|
848
|
-
obh_used: obhUsed,
|
|
849
|
-
})
|
|
850
|
-
.onConflict((oc) => oc.column('id').doNothing())
|
|
851
|
-
.executeTakeFirstOrThrow();
|
|
852
|
-
num += (_a = res.numInsertedOrUpdatedRows) !== null && _a !== void 0 ? _a : BigInt(0);
|
|
853
|
-
}
|
|
854
|
-
log.debug(`inserted ${num} of ${jobs.length} burn jobs`);
|
|
855
|
-
}));
|
|
856
|
-
});
|
|
857
|
-
},
|
|
858
|
-
markJobsBurnt(trx, p, log = logger_1.consoleLogger) {
|
|
859
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
860
|
-
let pricedAt = p.pricedAt;
|
|
861
|
-
if (!pricedAt) {
|
|
862
|
-
const jobs = yield fetchJobs1(trx, { ids: p.jobsIds.map(String) }, log);
|
|
863
|
-
pricedAt = (0, exports.batchFinishedAtMedian)(jobs.map((j) => ({
|
|
864
|
-
finishedAt: j.completedAt.toISOString(),
|
|
865
|
-
jobId: j.id,
|
|
866
|
-
rndrUsed: Number(j.rndrUsed.toString()),
|
|
867
|
-
})));
|
|
868
|
-
}
|
|
869
|
-
const solTxId = yield getOrInsertTx(trx, { sig: p.solTx, executedAt: p.executedAt }, log);
|
|
870
|
-
const { id } = yield trx
|
|
871
|
-
.insertInto(exports.BURN_TABLE)
|
|
872
|
-
.values({
|
|
873
|
-
sol_tx_id: solTxId,
|
|
874
|
-
priced_at: pricedAt,
|
|
875
|
-
eur_to_usdc: p.eurToUsdc,
|
|
876
|
-
render_to_usdc: p.renderToUsdc,
|
|
877
|
-
usdc_spent: p.usdcSpent,
|
|
878
|
-
quote_amt: p.quoteAmt,
|
|
879
|
-
burned: p.burned,
|
|
880
|
-
tags: p.tags.join(','),
|
|
881
|
-
burn_adjustment_id: p.burnAdjustmentId,
|
|
882
|
-
to_burn: p.toBurn,
|
|
883
|
-
})
|
|
884
|
-
//.onConflict((oc) => oc.column('sol_tx').doNothing())
|
|
885
|
-
.returning((eb) => ['id as id'])
|
|
886
|
-
.executeTakeFirstOrThrow();
|
|
887
|
-
const jobIdsStr = p.jobsIds.map(String);
|
|
888
|
-
const res = yield trx
|
|
889
|
-
.updateTable(exports.JOB_TABLE)
|
|
890
|
-
.where('id', 'in', jobIdsStr)
|
|
891
|
-
.set('burn_id', id)
|
|
892
|
-
.executeTakeFirstOrThrow();
|
|
893
|
-
log.debug(`set burn_id on ${res.numUpdatedRows} jobs`);
|
|
894
|
-
return;
|
|
895
|
-
});
|
|
896
|
-
},
|
|
897
|
-
markJobsBurned(p, log = logger_1.consoleLogger) {
|
|
898
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
899
|
-
let { markedAt, solTx } = p;
|
|
900
|
-
let res = yield db
|
|
901
|
-
.updateTable(exports.BURN_TABLE)
|
|
902
|
-
.set({ marked_burned_at: markedAt })
|
|
903
|
-
.from(exports.SOL_TX_TABLE)
|
|
904
|
-
.whereRef('burn.sol_tx_id', '=', 'sol_tx.id')
|
|
905
|
-
.where('sol_tx.sig', '=', solTx)
|
|
906
|
-
.executeTakeFirstOrThrow();
|
|
907
|
-
log.debug(`set ${res.numUpdatedRows} ${solTx} -> marked_burned_at ${markedAt.toISOString()}`);
|
|
908
|
-
});
|
|
909
|
-
},
|
|
910
|
-
consumeAndLockBurnJobs(limit, lowerRndrThreshold, upperRndrThreshold, log = logger_1.consoleLogger) {
|
|
911
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
912
|
-
const trx = yield (0, base_2.begin)(db, log);
|
|
913
|
-
const q = trx.trx
|
|
914
|
-
.selectFrom(trx.trx
|
|
915
|
-
.selectFrom(exports.JOB_TABLE)
|
|
916
|
-
.select(({ fn, val, ref }) => [
|
|
917
|
-
'id',
|
|
918
|
-
fn
|
|
919
|
-
.sum('render_amt')
|
|
920
|
-
.over((ob) => ob.orderBy(`completed_at`, `asc`))
|
|
921
|
-
.as('total'),
|
|
922
|
-
])
|
|
923
|
-
.where('burn_id', 'is', null)
|
|
924
|
-
.as('t'))
|
|
925
|
-
.select('id')
|
|
926
|
-
.where('total', '>=', lowerRndrThreshold)
|
|
927
|
-
.where('total', '<=', upperRndrThreshold);
|
|
928
|
-
log.debug(`outstanding jobs q: ${q.compile().sql} ${JSON.stringify({
|
|
929
|
-
limit,
|
|
930
|
-
lowerRndrThreshold,
|
|
931
|
-
upperRndrThreshold,
|
|
932
|
-
})}`);
|
|
933
|
-
const idRs = yield q.execute();
|
|
934
|
-
const ids = idRs.map((x) => x.id);
|
|
935
|
-
log.debug(`${ids.length} candidate jobs for burn. locking up to ${limit} jobs`);
|
|
936
|
-
if (ids.length == 0) {
|
|
937
|
-
return { trx, jobs: [] };
|
|
938
|
-
}
|
|
939
|
-
const rows = yield trx.trx
|
|
940
|
-
.selectFrom(exports.JOB_TABLE)
|
|
941
|
-
.selectAll()
|
|
942
|
-
.where('id', 'in', ids)
|
|
943
|
-
.limit(limit)
|
|
944
|
-
.forUpdate()
|
|
945
|
-
.skipLocked()
|
|
946
|
-
.execute();
|
|
947
|
-
log.debug(`locked ${rows.length} jobs`);
|
|
948
|
-
const jobs = [];
|
|
949
|
-
for (const r of rows) {
|
|
950
|
-
jobs.push({
|
|
951
|
-
id: Number(r.id),
|
|
952
|
-
completedAt: r.completed_at,
|
|
953
|
-
rndrUsed: BigInt(r.render_amt),
|
|
954
|
-
});
|
|
955
|
-
}
|
|
956
|
-
return { trx, jobs };
|
|
957
|
-
});
|
|
958
|
-
},
|
|
959
|
-
paymentMetrics(log) {
|
|
960
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
961
|
-
// Get the latest epochs for each channel
|
|
962
|
-
const latestEpochs = yield db
|
|
963
|
-
.selectFrom(exports.EPOCH_TABLE)
|
|
964
|
-
.select(['id', 'ui', 'channel', 'start_time', 'end_time'])
|
|
965
|
-
.where('channel', 'in', [
|
|
966
|
-
'availability',
|
|
967
|
-
'node_operator',
|
|
968
|
-
'render_compute_network',
|
|
969
|
-
])
|
|
970
|
-
.distinctOn('channel')
|
|
971
|
-
.where('saved_at', 'is not', null)
|
|
972
|
-
.orderBy('channel')
|
|
973
|
-
.orderBy('ui', 'desc')
|
|
974
|
-
.execute();
|
|
975
|
-
// Calculate distributed rewards for each channel
|
|
976
|
-
let availabilityRewards = 0;
|
|
977
|
-
let nodeOperatorRewards = 0;
|
|
978
|
-
let renderComputeNetworkRewards = 0;
|
|
979
|
-
let latestEpochId = 0;
|
|
980
|
-
for (const epoch of latestEpochs) {
|
|
981
|
-
if (!epoch.end_time) {
|
|
982
|
-
log === null || log === void 0 ? void 0 : log.info(`Skipping epoch ${epoch.ui} for channel ${epoch.channel} - no end_time`);
|
|
983
|
-
continue;
|
|
984
|
-
}
|
|
985
|
-
// Get total distributed rewards for this channel after epoch ended
|
|
986
|
-
const transfersResult = yield db
|
|
987
|
-
.selectFrom(exports.SOL_TRANSFER_TABLE)
|
|
988
|
-
.innerJoin('sol_tx', 'sol_tx.id', `${exports.SOL_TRANSFER_TABLE}.sol_tx_id`)
|
|
989
|
-
.select([(eb) => eb.fn.sum('amount_payed').as('total_distributed')])
|
|
990
|
-
.where(`${exports.SOL_TRANSFER_TABLE}.channel`, '=', epoch.channel)
|
|
991
|
-
.where('sol_tx.executed_at', '>', epoch.end_time)
|
|
992
|
-
.executeTakeFirst();
|
|
993
|
-
const totalDistributed = Number((transfersResult === null || transfersResult === void 0 ? void 0 : transfersResult.total_distributed) || 0);
|
|
994
|
-
switch (epoch.channel) {
|
|
995
|
-
case 'availability':
|
|
996
|
-
availabilityRewards = totalDistributed;
|
|
997
|
-
break;
|
|
998
|
-
case 'node_operator':
|
|
999
|
-
nodeOperatorRewards = totalDistributed;
|
|
1000
|
-
latestEpochId = Number(epoch.ui);
|
|
1001
|
-
break;
|
|
1002
|
-
case 'render_compute_network':
|
|
1003
|
-
renderComputeNetworkRewards = totalDistributed;
|
|
1004
|
-
break;
|
|
1005
|
-
}
|
|
1006
|
-
log === null || log === void 0 ? void 0 : log.info(`Channel ${epoch.channel}: ${totalDistributed} distributed after epoch ${epoch.ui}`);
|
|
1007
|
-
}
|
|
1008
|
-
return {
|
|
1009
|
-
latestEpochId,
|
|
1010
|
-
availabilityRewards,
|
|
1011
|
-
nodeOperatorRewards,
|
|
1012
|
-
renderComputeNetworkRewards,
|
|
1013
|
-
};
|
|
1014
|
-
});
|
|
1015
|
-
},
|
|
1016
|
-
incrBuyBurnMetrics(log = logger_1.consoleLogger) {
|
|
1017
|
-
var _a;
|
|
1018
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1019
|
-
const res = yield db
|
|
1020
|
-
.selectFrom(exports.JOB_TABLE)
|
|
1021
|
-
.select('inserted_at')
|
|
1022
|
-
.orderBy('completed_at asc')
|
|
1023
|
-
.where('burn_id', 'is', null)
|
|
1024
|
-
.execute();
|
|
1025
|
-
if (res.length == 0) {
|
|
1026
|
-
return { outstandingJobs: 0 };
|
|
1027
|
-
}
|
|
1028
|
-
return {
|
|
1029
|
-
outstandingJobs: res.length,
|
|
1030
|
-
oldestJobInsertedAt: (_a = res[0].inserted_at) !== null && _a !== void 0 ? _a : undefined,
|
|
1031
|
-
};
|
|
1032
|
-
});
|
|
1033
|
-
},
|
|
1034
|
-
fetchBurns(db, f, log = logger_1.consoleLogger) {
|
|
1035
|
-
var _a, _b;
|
|
1036
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1037
|
-
let q = db.trx
|
|
1038
|
-
.selectFrom('burn')
|
|
1039
|
-
.selectAll('burn')
|
|
1040
|
-
.innerJoin('sol_tx', 'sol_tx.id', 'burn.sol_tx_id')
|
|
1041
|
-
.select(['sol_tx.executed_at', 'sol_tx.sig']);
|
|
1042
|
-
if (!f.inclManual) {
|
|
1043
|
-
q = q.where('burn.usdc_spent', '>', String(0));
|
|
1044
|
-
}
|
|
1045
|
-
if (f.executedAfter) {
|
|
1046
|
-
q = q.where('sol_tx.executed_at', '>=', f.executedAfter);
|
|
1047
|
-
}
|
|
1048
|
-
if (f.executedBefore) {
|
|
1049
|
-
q = q.where('sol_tx.executed_at', '<=', f.executedBefore);
|
|
1050
|
-
}
|
|
1051
|
-
if (f.pricedAtBefore) {
|
|
1052
|
-
q = q.where('burn.priced_at', '<=', f.pricedAtBefore);
|
|
1053
|
-
}
|
|
1054
|
-
if (f.pricedAtAfter) {
|
|
1055
|
-
q = q.where('burn.priced_at', '>=', f.pricedAtAfter);
|
|
1056
|
-
}
|
|
1057
|
-
if (f.orderBy == 'pricedAt') {
|
|
1058
|
-
q = q.orderBy(['burn.priced_at']);
|
|
1059
|
-
}
|
|
1060
|
-
else if (f.orderBy == 'executedAt') {
|
|
1061
|
-
q = q.orderBy(['sol_tx.executed_at']);
|
|
1062
|
-
}
|
|
1063
|
-
log.info(`fetchBurns q ${q.compile().sql} ${JSON.stringify(f)}`);
|
|
1064
|
-
const burns = [];
|
|
1065
|
-
const burnIdToIdx = {};
|
|
1066
|
-
const comp = q.compile();
|
|
1067
|
-
const { rows } = yield (0, base_1.timedQuery)(pgPool, comp.sql, comp.parameters, 'fetchBurns', log);
|
|
1068
|
-
log.info(`fetchBurns len ${rows.length}`);
|
|
1069
|
-
for (const r of rows) {
|
|
1070
|
-
burns.push({
|
|
1071
|
-
jobs: [],
|
|
1072
|
-
id: Number(r.id),
|
|
1073
|
-
renderToUsdc: r.render_to_usdc,
|
|
1074
|
-
solTx: r.sig,
|
|
1075
|
-
executedAt: r.executed_at,
|
|
1076
|
-
pricedAt: r.priced_at,
|
|
1077
|
-
usdcSpent: BigInt((_a = r.usdc_spent) !== null && _a !== void 0 ? _a : 0),
|
|
1078
|
-
quoteAmt: BigInt((_b = r.quote_amt) !== null && _b !== void 0 ? _b : 0),
|
|
1079
|
-
burned: BigInt(r.burned),
|
|
1080
|
-
eurToUsdc: r.eur_to_usdc,
|
|
1081
|
-
markedBurnedAt: r.marked_burned_at,
|
|
1082
|
-
tags: r.tags ? r.tags.split(',') : [],
|
|
1083
|
-
});
|
|
1084
|
-
burnIdToIdx[r.id] = burns.length - 1;
|
|
1085
|
-
}
|
|
1086
|
-
if (f.inclJobs) {
|
|
1087
|
-
const jobRows = yield db.trx
|
|
1088
|
-
.selectFrom('job')
|
|
1089
|
-
.selectAll('job')
|
|
1090
|
-
.where('burn_id', 'in', rows.map((r) => r.id))
|
|
1091
|
-
.execute();
|
|
1092
|
-
log.info(`fetchBurns jobRows len ${jobRows.length}`);
|
|
1093
|
-
for (const r of jobRows) {
|
|
1094
|
-
burns[burnIdToIdx[r.burn_id]].jobs.push({
|
|
1095
|
-
id: Number(r.id),
|
|
1096
|
-
completedAt: r.completed_at,
|
|
1097
|
-
rndrUsed: BigInt(r.render_amt),
|
|
1098
|
-
obhUsed: r.obh_used ? Number(r.obh_used) : undefined,
|
|
1099
|
-
});
|
|
1100
|
-
}
|
|
1101
|
-
}
|
|
1102
|
-
return burns;
|
|
1103
|
-
});
|
|
1104
|
-
},
|
|
1105
|
-
createBridgeTransfers(db, bts, log = logger_1.consoleLogger) {
|
|
1106
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1107
|
-
return createBridgeTransfers(db.trx, bts, log);
|
|
1108
|
-
});
|
|
1109
|
-
},
|
|
1110
|
-
getBridgeTransfers({ solKey, page, }, log = logger_1.consoleLogger) {
|
|
1111
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1112
|
-
let q = db
|
|
1113
|
-
.selectFrom(exports.BRIDGE_TRANSFER_TABLE)
|
|
1114
|
-
.innerJoin(exports.EPOCH_TABLE, 'epoch.id', 'bridge_transfer.epoch_id')
|
|
1115
|
-
.innerJoin(exports.ENTITY_TABLE, 'entity.id', 'bridge_transfer.entity_id')
|
|
1116
|
-
.select([
|
|
1117
|
-
'entity.sol_key',
|
|
1118
|
-
'bridge_transfer.sequence',
|
|
1119
|
-
'bridge_transfer.created_at',
|
|
1120
|
-
'bridge_transfer.amount',
|
|
1121
|
-
'bridge_transfer.upgrade_bracket',
|
|
1122
|
-
'bridge_transfer.prior_supply',
|
|
1123
|
-
'bridge_transfer.points',
|
|
1124
|
-
'epoch.ui',
|
|
1125
|
-
]);
|
|
1126
|
-
let limit = 10000;
|
|
1127
|
-
if (page && page.limit) {
|
|
1128
|
-
limit = page.limit;
|
|
1129
|
-
}
|
|
1130
|
-
q = q.limit(page.limit).orderBy(`${exports.BRIDGE_TRANSFER_TABLE}.id`);
|
|
1131
|
-
let offset = 0;
|
|
1132
|
-
if (page && page.cursor) {
|
|
1133
|
-
offset = Number(atob(page.cursor));
|
|
1134
|
-
q = q.offset(offset);
|
|
1135
|
-
}
|
|
1136
|
-
if (solKey) {
|
|
1137
|
-
q = q.where('entity.sol_key', '=', solKey);
|
|
1138
|
-
}
|
|
1139
|
-
const c = q.compile();
|
|
1140
|
-
log.debug(`fetch bridge transfers ${c.sql} params ${c.parameters}`);
|
|
1141
|
-
const res = yield q.execute();
|
|
1142
|
-
log.info(`bridge transfers ${res.length}`);
|
|
1143
|
-
return {
|
|
1144
|
-
bts: res.map((row) => {
|
|
1145
|
-
return {
|
|
1146
|
-
solKey: row.sol_key,
|
|
1147
|
-
seq: BigInt(row.sequence),
|
|
1148
|
-
createdAt: row.created_at,
|
|
1149
|
-
amount: BigInt(row.amount),
|
|
1150
|
-
upgradeBracket: row.upgrade_bracket
|
|
1151
|
-
? Number(row.upgrade_bracket)
|
|
1152
|
-
: undefined,
|
|
1153
|
-
priorSupply: BigInt(row.prior_supply),
|
|
1154
|
-
points: row.points ? BigInt(row.points) : undefined,
|
|
1155
|
-
epochId: Number(row.ui),
|
|
1156
|
-
};
|
|
1157
|
-
}),
|
|
1158
|
-
cursor: res.length == limit ? btoa(`${res.length + offset}`) : undefined,
|
|
1159
|
-
};
|
|
1160
|
-
});
|
|
1161
|
-
},
|
|
1162
|
-
getLiabilitySolKeys(f) {
|
|
1163
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1164
|
-
let q = db
|
|
1165
|
-
.selectFrom(exports.LIABILITY_TABLE)
|
|
1166
|
-
.innerJoin(exports.ENTITY_TABLE, `${exports.LIABILITY_TABLE}.entity_id`, 'entity.id')
|
|
1167
|
-
.select(({ fn }) => [
|
|
1168
|
-
(0, kysely_1.sql) `distinct
|
|
1169
|
-
(entity.sol_key)`.as('sol_key'),
|
|
1170
|
-
])
|
|
1171
|
-
.where(`${exports.LIABILITY_TABLE}.channel`, '=', f.channel);
|
|
1172
|
-
if (f.epoch) {
|
|
1173
|
-
q = q.where('liability.epoch_id', '=', db
|
|
1174
|
-
.selectFrom('epoch')
|
|
1175
|
-
.select('id')
|
|
1176
|
-
.where('channel', '=', f.channel)
|
|
1177
|
-
.where('ui', '=', String(f.epoch)));
|
|
1178
|
-
}
|
|
1179
|
-
const ks = yield q.execute();
|
|
1180
|
-
const solKeys = {};
|
|
1181
|
-
for (const k of ks) {
|
|
1182
|
-
solKeys[k.sol_key] = {};
|
|
1183
|
-
}
|
|
1184
|
-
return solKeys;
|
|
1185
|
-
});
|
|
1186
|
-
},
|
|
1187
|
-
getTotalBurned(db) {
|
|
1188
|
-
var _a, _b;
|
|
1189
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1190
|
-
const total = yield db.trx
|
|
1191
|
-
.selectFrom('burn')
|
|
1192
|
-
.select(({ fn }) => fn.sum('burned').as('total_burned'))
|
|
1193
|
-
.execute();
|
|
1194
|
-
return BigInt((_b = (_a = total[0]) === null || _a === void 0 ? void 0 : _a.total_burned) !== null && _b !== void 0 ? _b : 0);
|
|
1195
|
-
});
|
|
1196
|
-
},
|
|
1197
|
-
insertBurn(p, log = logger_1.consoleLogger) {
|
|
1198
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1199
|
-
yield db.transaction().execute((trx) => __awaiter(this, void 0, void 0, function* () {
|
|
1200
|
-
var _a;
|
|
1201
|
-
const solTxId = yield getOrInsertTx(trx, { sig: p.solTx, executedAt: p.executedAt }, log);
|
|
1202
|
-
const { id } = yield trx
|
|
1203
|
-
.insertInto(exports.BURN_TABLE)
|
|
1204
|
-
.values({
|
|
1205
|
-
sol_tx_id: solTxId,
|
|
1206
|
-
priced_at: p.pricedAt,
|
|
1207
|
-
usdc_spent: p.usdcSpent,
|
|
1208
|
-
quote_amt: p.quoteAmt,
|
|
1209
|
-
burned: p.burned,
|
|
1210
|
-
marked_burned_at: p.markedBurnedAt,
|
|
1211
|
-
})
|
|
1212
|
-
//.onConflict((oc) => oc.column('sol_tx').doNothing())
|
|
1213
|
-
.returning((eb) => ['id as id'])
|
|
1214
|
-
.executeTakeFirstOrThrow();
|
|
1215
|
-
let num = BigInt(0);
|
|
1216
|
-
for (const { rndrUsed, completedAt, id: jobId, insertedAt } of p.jobs) {
|
|
1217
|
-
const res = yield trx
|
|
1218
|
-
.insertInto(exports.JOB_TABLE)
|
|
1219
|
-
.values({
|
|
1220
|
-
id: jobId,
|
|
1221
|
-
render_amt: rndrUsed,
|
|
1222
|
-
burn_id: id,
|
|
1223
|
-
completed_at: completedAt,
|
|
1224
|
-
inserted_at: insertedAt !== null && insertedAt !== void 0 ? insertedAt : new Date(),
|
|
1225
|
-
})
|
|
1226
|
-
.onConflict((oc) => oc.column('id').doNothing())
|
|
1227
|
-
.executeTakeFirstOrThrow();
|
|
1228
|
-
num += (_a = res.numInsertedOrUpdatedRows) !== null && _a !== void 0 ? _a : BigInt(0);
|
|
1229
|
-
}
|
|
1230
|
-
log.debug(`burn id ${id} inserted ${num} of ${p.jobs.length} burn jobs`);
|
|
1231
|
-
}));
|
|
1232
|
-
});
|
|
1233
|
-
},
|
|
1234
|
-
fetchJobs(db, f, log = logger_1.consoleLogger) {
|
|
1235
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1236
|
-
return yield fetchJobs1(db.trx, f, log);
|
|
1237
|
-
});
|
|
1238
|
-
},
|
|
1239
|
-
fetchLiabilityEpochs(db, log = logger_1.consoleLogger) {
|
|
1240
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1241
|
-
const q = db.trx
|
|
1242
|
-
.selectFrom(exports.LIABILITY_TABLE)
|
|
1243
|
-
.select(({ fn }) => [
|
|
1244
|
-
`${exports.LIABILITY_TABLE}.channel`,
|
|
1245
|
-
fn.sum(`${exports.LIABILITY_TABLE}.amount_due`).as('amount_due'),
|
|
1246
|
-
])
|
|
1247
|
-
.innerJoin(exports.EPOCH_TABLE, `${exports.EPOCH_TABLE}.id`, `${exports.LIABILITY_TABLE}.epoch_id`)
|
|
1248
|
-
.select(['ui', 'start_time', 'end_time'])
|
|
1249
|
-
.groupBy([
|
|
1250
|
-
'liability.channel',
|
|
1251
|
-
'epoch.ui',
|
|
1252
|
-
'epoch.start_time',
|
|
1253
|
-
'epoch.end_time',
|
|
1254
|
-
])
|
|
1255
|
-
.orderBy(['epoch.start_time desc']);
|
|
1256
|
-
// Fetch node_operator SOL key metadata separately
|
|
1257
|
-
const nodeOpMetadataQuery = db.trx
|
|
1258
|
-
.selectFrom(exports.LIABILITY_TABLE)
|
|
1259
|
-
.select([`${exports.LIABILITY_TABLE}.obh_used`])
|
|
1260
|
-
.innerJoin(exports.EPOCH_TABLE, `${exports.EPOCH_TABLE}.id`, `${exports.LIABILITY_TABLE}.epoch_id`)
|
|
1261
|
-
.select([`${exports.EPOCH_TABLE}.ui as epoch_ui`])
|
|
1262
|
-
.innerJoin('entity', 'entity.id', `${exports.LIABILITY_TABLE}.entity_id`)
|
|
1263
|
-
.select(['entity.sol_key'])
|
|
1264
|
-
.where(`${exports.LIABILITY_TABLE}.channel`, '=', 'node_operator')
|
|
1265
|
-
.where('entity.sol_key', 'is not', null)
|
|
1266
|
-
.where(`${exports.LIABILITY_TABLE}.obh_used`, 'is not', null);
|
|
1267
|
-
log.debug(`fetchLiabilityEpochs ${q.compile().sql}`);
|
|
1268
|
-
log.debug(`nodeOpMetadata ${nodeOpMetadataQuery.compile().sql}`);
|
|
1269
|
-
const [rs, nodeOpMetadata] = yield Promise.all([
|
|
1270
|
-
q.execute(),
|
|
1271
|
-
nodeOpMetadataQuery.execute(),
|
|
1272
|
-
]);
|
|
1273
|
-
// Group node operator metadata by epoch
|
|
1274
|
-
const metadataByEpoch = {};
|
|
1275
|
-
nodeOpMetadata.forEach((row) => {
|
|
1276
|
-
const epochId = Number(row.epoch_ui);
|
|
1277
|
-
if (!metadataByEpoch[epochId]) {
|
|
1278
|
-
metadataByEpoch[epochId] = {};
|
|
1279
|
-
}
|
|
1280
|
-
if (row.sol_key && row.obh_used !== null) {
|
|
1281
|
-
metadataByEpoch[epochId][row.sol_key] = {
|
|
1282
|
-
obhUsed: Number(row.obh_used),
|
|
1283
|
-
};
|
|
1284
|
-
}
|
|
1285
|
-
});
|
|
1286
|
-
return rs.map((r) => ({
|
|
1287
|
-
epochId: Number(r.ui),
|
|
1288
|
-
channel: r.channel,
|
|
1289
|
-
start: r.start_time,
|
|
1290
|
-
end: r.end_time,
|
|
1291
|
-
amountDue: Number(r.amount_due),
|
|
1292
|
-
metadataBySolKey: r.channel === 'node_operator'
|
|
1293
|
-
? metadataByEpoch[Number(r.ui)] || {}
|
|
1294
|
-
: {},
|
|
1295
|
-
}));
|
|
1296
|
-
});
|
|
1297
|
-
},
|
|
1298
|
-
getBurnAdjustments(trx, ps, log = logger_1.consoleLogger) {
|
|
1299
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1300
|
-
if (ps.toFill) {
|
|
1301
|
-
const q = trx
|
|
1302
|
-
.selectFrom(exports.BURN_ADJUSTMENT_TABLE)
|
|
1303
|
-
.selectAll(exports.BURN_ADJUSTMENT_TABLE)
|
|
1304
|
-
.innerJoin(exports.JOB_TABLE, `${exports.JOB_TABLE}.id`, `${exports.BURN_ADJUSTMENT_TABLE}.job_id`)
|
|
1305
|
-
.select([`${exports.JOB_TABLE}.completed_at`, `${exports.JOB_TABLE}.render_amt`])
|
|
1306
|
-
.leftJoin(exports.BURN_TABLE, `${exports.BURN_TABLE}.burn_adjustment_id`, `${exports.BURN_ADJUSTMENT_TABLE}.id`)
|
|
1307
|
-
.select(({ fn, lit }) => [
|
|
1308
|
-
fn
|
|
1309
|
-
.sum(fn.coalesce(`${exports.BURN_TABLE}.to_burn`, lit(0)))
|
|
1310
|
-
.as('tot_to_burn'),
|
|
1311
|
-
fn
|
|
1312
|
-
.sum(fn.coalesce(`${exports.BURN_TABLE}.burned`, lit(0)))
|
|
1313
|
-
.as('tot_burned'),
|
|
1314
|
-
])
|
|
1315
|
-
.groupBy([
|
|
1316
|
-
`${exports.BURN_ADJUSTMENT_TABLE}.id`,
|
|
1317
|
-
`${exports.JOB_TABLE}.completed_at`,
|
|
1318
|
-
`${exports.JOB_TABLE}.render_amt`,
|
|
1319
|
-
])
|
|
1320
|
-
.havingRef((0, kysely_1.sql) `SUM(COALESCE("burn"."to_burn", 0)) - SUM(COALESCE("burn"."burned", 0))`, '<', `${exports.BURN_ADJUSTMENT_TABLE}.down_adj_to_burn`);
|
|
1321
|
-
log.info(`getBurnAdjustments ${q.compile().sql}`);
|
|
1322
|
-
const res = yield q.execute();
|
|
1323
|
-
return res.map((r) => ({
|
|
1324
|
-
id: Number(r.id),
|
|
1325
|
-
createdAt: r.created_at,
|
|
1326
|
-
jobId: Number(r.job_id),
|
|
1327
|
-
downAdjRndrUsed: Number(r.down_adj_rndr_used),
|
|
1328
|
-
downAdjToBurn: Number(r.down_adj_to_burn),
|
|
1329
|
-
adjusted: Number(r.tot_to_burn) - Number(r.tot_burned),
|
|
1330
|
-
job: {
|
|
1331
|
-
id: Number(r.job_id),
|
|
1332
|
-
completedAt: r.completed_at,
|
|
1333
|
-
rndrUsed: BigInt(r.render_amt),
|
|
1334
|
-
},
|
|
1335
|
-
}));
|
|
1336
|
-
}
|
|
1337
|
-
return [];
|
|
1338
|
-
});
|
|
1339
|
-
},
|
|
1340
|
-
insertPolygonUpgrade(db, p, log = logger_1.consoleLogger) {
|
|
1341
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1342
|
-
const solTxId = yield getOrInsertTx(db.trx, {
|
|
1343
|
-
sig: p.solTx.sig,
|
|
1344
|
-
executedAt: p.solTx.executedAt,
|
|
1345
|
-
});
|
|
1346
|
-
yield db.trx
|
|
1347
|
-
.insertInto(exports.POLYGON_UPGRADE_TABLE)
|
|
1348
|
-
.values({
|
|
1349
|
-
eth_addr: p.ethAddr,
|
|
1350
|
-
sol_addr: p.solAddr,
|
|
1351
|
-
amount: p.amount,
|
|
1352
|
-
sol_tx_id: solTxId,
|
|
1353
|
-
})
|
|
1354
|
-
.execute();
|
|
1355
|
-
log.info(`insertPolygonUpgrade ${JSON.stringify(p)}`);
|
|
1356
|
-
return;
|
|
1357
|
-
});
|
|
1358
|
-
},
|
|
1359
|
-
fetchPolygonUpgrades(db, f, log = logger_1.consoleLogger) {
|
|
1360
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1361
|
-
let query = db.trx
|
|
1362
|
-
.selectFrom('polygon_upgrade')
|
|
1363
|
-
.selectAll('polygon_upgrade')
|
|
1364
|
-
.innerJoin('sol_tx', 'sol_tx.id', 'polygon_upgrade.sol_tx_id')
|
|
1365
|
-
.select(['sol_tx.sig', 'sol_tx.executed_at'])
|
|
1366
|
-
.orderBy('sol_tx.executed_at', 'desc');
|
|
1367
|
-
if (f.executedAfter) {
|
|
1368
|
-
query = query.where('sol_tx.executed_at', '>=', f.executedAfter);
|
|
1369
|
-
}
|
|
1370
|
-
if (f.executedBefore) {
|
|
1371
|
-
query = query.where('sol_tx.executed_at', '<=', f.executedBefore);
|
|
1372
|
-
}
|
|
1373
|
-
const results = yield query.execute();
|
|
1374
|
-
log.info(`fetchPolygonUpgrades returned ${results.length} records`);
|
|
1375
|
-
return results.map((row) => ({
|
|
1376
|
-
ethAddr: row.eth_addr,
|
|
1377
|
-
solAddr: row.sol_addr,
|
|
1378
|
-
amount: BigInt(row.amount.toString()),
|
|
1379
|
-
solTx: {
|
|
1380
|
-
sig: row.sig,
|
|
1381
|
-
executedAt: row.executed_at,
|
|
1382
|
-
},
|
|
1383
|
-
}));
|
|
1384
|
-
});
|
|
1385
|
-
},
|
|
1386
|
-
deleteAll(log = logger_1.consoleLogger) {
|
|
1387
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1388
|
-
if (config.database.includes('render')) {
|
|
1389
|
-
throw new Error('cannot nuke from deployed database. call from tests only');
|
|
1390
|
-
}
|
|
1391
|
-
const tx = yield (0, base_2.begin)(db, log);
|
|
1392
|
-
try {
|
|
1393
|
-
const res = yield Promise.all([
|
|
1394
|
-
tx.trx
|
|
1395
|
-
.deleteFrom(exports.SOL_TX_TABLE)
|
|
1396
|
-
.where('id', 'is not', null)
|
|
1397
|
-
.executeTakeFirst(),
|
|
1398
|
-
tx.trx.deleteFrom(exports.LIABILITY_TABLE).executeTakeFirstOrThrow(),
|
|
1399
|
-
tx.trx.deleteFrom(exports.SOL_TRANSFER_TABLE).executeTakeFirstOrThrow(),
|
|
1400
|
-
tx.trx
|
|
1401
|
-
.deleteFrom(exports.EPOCH_TABLE)
|
|
1402
|
-
.where('id', 'is not', null)
|
|
1403
|
-
.executeTakeFirst(),
|
|
1404
|
-
tx.trx
|
|
1405
|
-
.deleteFrom(exports.ENTITY_TABLE)
|
|
1406
|
-
.where('id', 'is not', null)
|
|
1407
|
-
.executeTakeFirst(),
|
|
1408
|
-
tx.trx
|
|
1409
|
-
.deleteFrom(exports.MANUAL_BURN_TABLE)
|
|
1410
|
-
.where('epoch_id', 'is not', null)
|
|
1411
|
-
.executeTakeFirst(),
|
|
1412
|
-
tx.trx
|
|
1413
|
-
.deleteFrom(exports.JOB_ID_TABLE)
|
|
1414
|
-
.where('id', 'is not', null)
|
|
1415
|
-
.executeTakeFirst(),
|
|
1416
|
-
tx.trx
|
|
1417
|
-
.deleteFrom(exports.JOB_TABLE)
|
|
1418
|
-
.where('id', 'is not', null)
|
|
1419
|
-
.executeTakeFirst(),
|
|
1420
|
-
tx.trx
|
|
1421
|
-
.deleteFrom(exports.BURN_TABLE)
|
|
1422
|
-
.where('id', 'is not', null)
|
|
1423
|
-
.executeTakeFirst(),
|
|
1424
|
-
]);
|
|
1425
|
-
tx.commit();
|
|
1426
|
-
}
|
|
1427
|
-
catch (e) {
|
|
1428
|
-
log.error(`caught ${e}`);
|
|
1429
|
-
tx.rollback();
|
|
1430
|
-
}
|
|
1431
|
-
});
|
|
1432
|
-
},
|
|
1433
|
-
};
|
|
1434
|
-
};
|
|
1435
|
-
exports.pgClient = pgClient;
|
|
1436
|
-
//# sourceMappingURL=v2Client.js.map
|