@render-foundation/utils 0.0.76 → 0.0.78
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/package.json +1 -1
- package/lib/cjs/client/onchain.js +0 -109
- package/lib/cjs/client/onchain.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 -769
- package/lib/cjs/client/pg/v2Client.js.map +0 -1
- package/lib/cjs/client/price.js +0 -46
- package/lib/cjs/client/price.js.map +0 -1
- package/lib/cjs/client/render.js +0 -164
- 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 -127
- 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/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/errorReporter.js +0 -77
- package/lib/cjs/errorReporter.js.map +0 -1
- package/lib/cjs/index.js +0 -41
- 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/client/onchain.js +0 -100
- package/lib/esm/src/client/onchain.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 -709
- package/lib/esm/src/client/pg/v2Client.js.map +0 -1
- package/lib/esm/src/client/price.js +0 -32
- package/lib/esm/src/client/price.js.map +0 -1
- package/lib/esm/src/client/render.js +0 -138
- 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 -114
- 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/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/errorReporter.js +0 -68
- package/lib/esm/src/errorReporter.js.map +0 -1
- package/lib/esm/src/index.js +0 -12
- 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/esm/tsconfig.esm.tsbuildinfo +0 -1
- package/lib/types/src/client/onchain.d.ts +0 -19
- package/lib/types/src/client/onchain.d.ts.map +0 -1
- package/lib/types/src/client/pg/v1.d.ts +0 -163
- package/lib/types/src/client/pg/v1.d.ts.map +0 -1
- package/lib/types/src/client/pg/v2.d.ts +0 -35
- package/lib/types/src/client/pg/v2.d.ts.map +0 -1
- package/lib/types/src/client/pg/v2Client.d.ts +0 -142
- package/lib/types/src/client/pg/v2Client.d.ts.map +0 -1
- package/lib/types/src/client/price.d.ts +0 -11
- package/lib/types/src/client/price.d.ts.map +0 -1
- package/lib/types/src/client/render.d.ts +0 -19
- 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 -15
- 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/dbTypes.d.ts +0 -123
- package/lib/types/src/dbTypes.d.ts.map +0 -1
- package/lib/types/src/dbTypesV2.d.ts +0 -109
- package/lib/types/src/dbTypesV2.d.ts.map +0 -1
- package/lib/types/src/errorReporter.d.ts +0 -12
- package/lib/types/src/errorReporter.d.ts.map +0 -1
- package/lib/types/src/index.d.ts +0 -12
- 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,769 +0,0 @@
|
|
|
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.LIABILITY_TABLE = exports.SOL_TRANSFER_TABLE = exports.ENTITY_TABLE = exports.MANUAL_BURN_TABLE = exports.EPOCH_TABLE = exports.SOL_TX_TABLE = void 0;
|
|
25
|
-
const kysely_1 = require("kysely");
|
|
26
|
-
const logger_1 = require("../../logger");
|
|
27
|
-
const pg_1 = require("pg");
|
|
28
|
-
exports.SOL_TX_TABLE = 'sol_tx';
|
|
29
|
-
exports.EPOCH_TABLE = 'epoch';
|
|
30
|
-
exports.MANUAL_BURN_TABLE = 'manual_burn';
|
|
31
|
-
exports.ENTITY_TABLE = 'entity';
|
|
32
|
-
exports.SOL_TRANSFER_TABLE = 'sol_transfer1';
|
|
33
|
-
exports.LIABILITY_TABLE = 'liability';
|
|
34
|
-
exports.JOB_ID_TABLE = 'job_id';
|
|
35
|
-
exports.JOB_TABLE = 'job';
|
|
36
|
-
exports.BURN_TABLE = 'burn';
|
|
37
|
-
BigInt.prototype.toJSON = function () {
|
|
38
|
-
return this.toString();
|
|
39
|
-
};
|
|
40
|
-
const pgClient = (config) => {
|
|
41
|
-
var _a, _b;
|
|
42
|
-
const dialect = new kysely_1.PostgresDialect({
|
|
43
|
-
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 : 10 })),
|
|
44
|
-
});
|
|
45
|
-
// Database interface is passed to Kysely's constructor, and from now on, Kysely
|
|
46
|
-
// knows your database structure.
|
|
47
|
-
// Dialect is passed to Kysely's constructor, and from now on, Kysely knows how
|
|
48
|
-
// to communicate with your database.
|
|
49
|
-
const db = new kysely_1.Kysely({
|
|
50
|
-
dialect,
|
|
51
|
-
});
|
|
52
|
-
const getOrInsertEntity = (db, p, log = logger_1.consoleLogger) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
-
const res = (yield db
|
|
54
|
-
.selectFrom(exports.ENTITY_TABLE)
|
|
55
|
-
.select('id')
|
|
56
|
-
.where('sol_key', '=', p.solKey)
|
|
57
|
-
.executeTakeFirst());
|
|
58
|
-
if (res) {
|
|
59
|
-
log.debug(`already found entity ${res.id}`);
|
|
60
|
-
return res.id;
|
|
61
|
-
}
|
|
62
|
-
const { id } = (yield db
|
|
63
|
-
.insertInto(exports.ENTITY_TABLE)
|
|
64
|
-
.values({
|
|
65
|
-
sol_key: p.solKey,
|
|
66
|
-
})
|
|
67
|
-
.onConflict((oc) => oc.column('sol_key').doNothing())
|
|
68
|
-
.returning((eb) => ['id as id'])
|
|
69
|
-
.executeTakeFirst());
|
|
70
|
-
log.debug(`inserted entity ${id}`);
|
|
71
|
-
return id;
|
|
72
|
-
});
|
|
73
|
-
const getOrInsertTx = (db, p, log = logger_1.consoleLogger) => __awaiter(void 0, void 0, void 0, function* () {
|
|
74
|
-
const res = yield db
|
|
75
|
-
.selectFrom(exports.SOL_TX_TABLE)
|
|
76
|
-
.select('id')
|
|
77
|
-
.where('sig', '=', p.sig)
|
|
78
|
-
.executeTakeFirst();
|
|
79
|
-
if (res) {
|
|
80
|
-
log.debug(`already found tx ${res.id}`);
|
|
81
|
-
return res.id;
|
|
82
|
-
}
|
|
83
|
-
const { id } = (yield db
|
|
84
|
-
.insertInto(exports.SOL_TX_TABLE)
|
|
85
|
-
.values({
|
|
86
|
-
sig: p.sig,
|
|
87
|
-
executed_at: p.executedAt,
|
|
88
|
-
})
|
|
89
|
-
.onConflict((oc) => oc.column('sig').doNothing())
|
|
90
|
-
.returning((eb) => ['id as id'])
|
|
91
|
-
.executeTakeFirst());
|
|
92
|
-
log.debug(`inserted tx ${id}`);
|
|
93
|
-
return id;
|
|
94
|
-
});
|
|
95
|
-
const getOrInsertEpoch = (db, p, log = logger_1.consoleLogger) => __awaiter(void 0, void 0, void 0, function* () {
|
|
96
|
-
const res = yield db
|
|
97
|
-
.selectFrom(exports.EPOCH_TABLE)
|
|
98
|
-
.select('id')
|
|
99
|
-
.where('ui', '=', String(p.uiEpochId))
|
|
100
|
-
.where('channel', '=', p.channel)
|
|
101
|
-
.executeTakeFirst();
|
|
102
|
-
if (res) {
|
|
103
|
-
log.debug(`already found epoch ${res.id}`);
|
|
104
|
-
return res.id;
|
|
105
|
-
}
|
|
106
|
-
const { id } = (yield db
|
|
107
|
-
.insertInto(exports.EPOCH_TABLE)
|
|
108
|
-
.values({
|
|
109
|
-
ui: p.uiEpochId,
|
|
110
|
-
channel: p.channel,
|
|
111
|
-
start_time: p.startTime,
|
|
112
|
-
end_time: p.endTime,
|
|
113
|
-
saved_at: p.channel == 'node_operator' ? new Date() : undefined,
|
|
114
|
-
})
|
|
115
|
-
.onConflict((oc) => oc.columns(['ui', 'channel']).doNothing())
|
|
116
|
-
.returning((eb) => ['id as id'])
|
|
117
|
-
.executeTakeFirst());
|
|
118
|
-
log.debug(`inserted epoch ${id}`);
|
|
119
|
-
return id;
|
|
120
|
-
});
|
|
121
|
-
const updateTotalRndr = (db, p, totalRndr, savedAt, log) => __awaiter(void 0, void 0, void 0, function* () {
|
|
122
|
-
const res = (yield db
|
|
123
|
-
.updateTable(exports.EPOCH_TABLE)
|
|
124
|
-
.set({
|
|
125
|
-
total_rndr: totalRndr,
|
|
126
|
-
saved_at: savedAt,
|
|
127
|
-
})
|
|
128
|
-
.where('channel', '=', p.channel)
|
|
129
|
-
.where('ui', '=', String(p.uiEpochId))
|
|
130
|
-
.executeTakeFirst());
|
|
131
|
-
log.info(`set ${res.numUpdatedRows} rows. channel: ${p.channel}, ui: ${p.uiEpochId} -> totalRndr ${totalRndr} `);
|
|
132
|
-
});
|
|
133
|
-
const createPayments = (db, ps, log = logger_1.consoleLogger) => __awaiter(void 0, void 0, void 0, function* () {
|
|
134
|
-
const ids = [];
|
|
135
|
-
const epochDbIds = [];
|
|
136
|
-
for (const p of ps) {
|
|
137
|
-
let liabilityId;
|
|
138
|
-
let transferId;
|
|
139
|
-
if (p.liability) {
|
|
140
|
-
let { id: dbId, solKey, channel, epochId, createdAt, amountDue, deviceId, points, } = p.liability;
|
|
141
|
-
try {
|
|
142
|
-
let entityId;
|
|
143
|
-
if (solKey && solKey != '') {
|
|
144
|
-
entityId = yield getOrInsertEntity(db, { solKey });
|
|
145
|
-
}
|
|
146
|
-
const dbEpochId = yield getOrInsertEpoch(db, {
|
|
147
|
-
channel,
|
|
148
|
-
uiEpochId: epochId,
|
|
149
|
-
}, log);
|
|
150
|
-
epochDbIds.push(BigInt(dbEpochId));
|
|
151
|
-
log.debug(`inserting liability ${JSON.stringify(p.liability)}`);
|
|
152
|
-
const { id } = yield db
|
|
153
|
-
.insertInto(exports.LIABILITY_TABLE)
|
|
154
|
-
.values({
|
|
155
|
-
id: dbId,
|
|
156
|
-
entity_id: entityId,
|
|
157
|
-
epoch_id: dbEpochId,
|
|
158
|
-
channel,
|
|
159
|
-
created_at: createdAt !== null && createdAt !== void 0 ? createdAt : new Date(),
|
|
160
|
-
amount_due: amountDue,
|
|
161
|
-
device_id: deviceId,
|
|
162
|
-
points,
|
|
163
|
-
})
|
|
164
|
-
.returning((eb) => ['id as id'])
|
|
165
|
-
.executeTakeFirstOrThrow();
|
|
166
|
-
log.debug(`inserted liability ${id}`);
|
|
167
|
-
liabilityId = BigInt(id);
|
|
168
|
-
}
|
|
169
|
-
catch (e) {
|
|
170
|
-
throw new Error(`failed inserting solKey ${solKey} epochId ${epochId} ${JSON.stringify(p.liability)}: ${e}`);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
if (p.transfer) {
|
|
174
|
-
let { executedAt, symbol, amountPayed, solTx, channel, fromSolKey, toSolKey, id: dbId, } = p.transfer;
|
|
175
|
-
if (!fromSolKey || !toSolKey || !solTx) {
|
|
176
|
-
log.debug(`skipping transfer. one of undefined: fromKey ${fromSolKey} toKey ${toSolKey} tx ${solTx}`);
|
|
177
|
-
continue;
|
|
178
|
-
}
|
|
179
|
-
log.debug(`inserting transfer ${JSON.stringify(p.transfer)}`);
|
|
180
|
-
const fromEntityId = yield getOrInsertEntity(db, { solKey: fromSolKey });
|
|
181
|
-
const toEntityId = yield getOrInsertEntity(db, { solKey: toSolKey });
|
|
182
|
-
const solTxId = yield getOrInsertTx(db, {
|
|
183
|
-
sig: solTx,
|
|
184
|
-
executedAt,
|
|
185
|
-
}, log);
|
|
186
|
-
const { id } = yield db
|
|
187
|
-
.insertInto(exports.SOL_TRANSFER_TABLE)
|
|
188
|
-
.values({
|
|
189
|
-
id: dbId,
|
|
190
|
-
sol_tx_id: solTxId,
|
|
191
|
-
from_entity_id: fromEntityId,
|
|
192
|
-
to_entity_id: toEntityId,
|
|
193
|
-
channel,
|
|
194
|
-
symbol,
|
|
195
|
-
amount_payed: amountPayed,
|
|
196
|
-
})
|
|
197
|
-
.returning((eb) => ['id as id'])
|
|
198
|
-
.executeTakeFirstOrThrow();
|
|
199
|
-
transferId = BigInt(id);
|
|
200
|
-
}
|
|
201
|
-
ids.push({ transferId, liabilityId });
|
|
202
|
-
}
|
|
203
|
-
return { epochDbIds, inserted: ids };
|
|
204
|
-
});
|
|
205
|
-
return {
|
|
206
|
-
db: {
|
|
207
|
-
commit: () => { },
|
|
208
|
-
rollback: () => { },
|
|
209
|
-
trx: db,
|
|
210
|
-
},
|
|
211
|
-
startTransaction() {
|
|
212
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
213
|
-
return begin(db);
|
|
214
|
-
});
|
|
215
|
-
},
|
|
216
|
-
createPayments(db, ps, log = logger_1.consoleLogger) {
|
|
217
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
218
|
-
return createPayments(db.trx, ps, log);
|
|
219
|
-
});
|
|
220
|
-
},
|
|
221
|
-
fetchTransfers(db, f) {
|
|
222
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
223
|
-
let offset = 0;
|
|
224
|
-
if (f.cursor) {
|
|
225
|
-
offset = Number(atob(f.cursor));
|
|
226
|
-
}
|
|
227
|
-
let q = db.trx
|
|
228
|
-
.selectFrom(exports.SOL_TRANSFER_TABLE)
|
|
229
|
-
.selectAll()
|
|
230
|
-
.innerJoin('sol_tx', 'sol_tx.id', 'sol_transfer1.sol_tx_id')
|
|
231
|
-
.select(['sol_tx.sig', 'sol_tx.executed_at'])
|
|
232
|
-
.innerJoin('entity as f', 'f.id', 'sol_transfer1.from_entity_id')
|
|
233
|
-
.select(['f.sol_key as from_sol_key'])
|
|
234
|
-
.innerJoin('entity as t', 't.id', 'sol_transfer1.to_entity_id')
|
|
235
|
-
.select(['t.sol_key as to_sol_key']);
|
|
236
|
-
if (f.executedAfter) {
|
|
237
|
-
q = q.where('sol_tx.executed_at', '>=', f.executedAfter);
|
|
238
|
-
}
|
|
239
|
-
if (f.executedBefore) {
|
|
240
|
-
q = q.where('sol_tx.executed_at', '<=', f.executedBefore);
|
|
241
|
-
}
|
|
242
|
-
if (f.channel) {
|
|
243
|
-
q = q.where('sol_transfer1.channel', '=', f.channel);
|
|
244
|
-
}
|
|
245
|
-
if (f.limit) {
|
|
246
|
-
q = q.limit(f.limit);
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
q = q.limit(5000);
|
|
250
|
-
}
|
|
251
|
-
q = q.orderBy(`${exports.SOL_TRANSFER_TABLE}.id`);
|
|
252
|
-
if (offset) {
|
|
253
|
-
q = q.offset(offset);
|
|
254
|
-
}
|
|
255
|
-
console.log(`fetching transactions ${q.compile().sql} ps ${JSON.stringify(f)}`);
|
|
256
|
-
const rows = yield q.execute();
|
|
257
|
-
const trs = [];
|
|
258
|
-
for (const r of rows) {
|
|
259
|
-
trs.push({
|
|
260
|
-
id: BigInt(r.id),
|
|
261
|
-
fromSolKey: r.from_sol_key,
|
|
262
|
-
toSolKey: r.to_sol_key,
|
|
263
|
-
symbol: r.symbol,
|
|
264
|
-
channel: r.channel,
|
|
265
|
-
solTx: r.sig,
|
|
266
|
-
executedAt: r.executed_at,
|
|
267
|
-
amountPayed: BigInt(r.amount_payed),
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
return { transfers: trs, cursor: btoa(`${trs.length + offset}`) };
|
|
271
|
-
});
|
|
272
|
-
},
|
|
273
|
-
getLiabilitiesAndTransfers(f, log = logger_1.consoleLogger) {
|
|
274
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
275
|
-
let lq = db
|
|
276
|
-
.selectFrom(exports.LIABILITY_TABLE)
|
|
277
|
-
.selectAll()
|
|
278
|
-
.innerJoin(exports.ENTITY_TABLE, `${exports.LIABILITY_TABLE}.entity_id`, 'entity.id')
|
|
279
|
-
.select(['entity.sol_key'])
|
|
280
|
-
.innerJoin(exports.EPOCH_TABLE, `${exports.EPOCH_TABLE}.id`, 'liability.epoch_id')
|
|
281
|
-
.select(['epoch.ui', 'epoch.start_time', 'epoch.end_time'])
|
|
282
|
-
.where('entity.sol_key', '=', f.user);
|
|
283
|
-
let tq = db
|
|
284
|
-
.selectFrom(exports.SOL_TRANSFER_TABLE)
|
|
285
|
-
.selectAll()
|
|
286
|
-
.innerJoin(exports.SOL_TX_TABLE, 'sol_tx.id', 'sol_transfer1.sol_tx_id')
|
|
287
|
-
.select(['sol_tx.sig', 'sol_tx.executed_at'])
|
|
288
|
-
.innerJoin(`${exports.ENTITY_TABLE} as t`, `${exports.SOL_TRANSFER_TABLE}.to_entity_id`, 't.id')
|
|
289
|
-
.select(['t.sol_key as to_sol_key'])
|
|
290
|
-
.innerJoin(`${exports.ENTITY_TABLE} as f`, `${exports.SOL_TRANSFER_TABLE}.from_entity_id`, 'f.id')
|
|
291
|
-
.select(['f.sol_key as from_sol_key'])
|
|
292
|
-
.where('t.sol_key', '=', f.user);
|
|
293
|
-
log.info(`fetch liabilities ${lq.compile().sql} ps ${JSON.stringify(f)}`);
|
|
294
|
-
log.info(`fetch transfers ${tq.compile().sql} ps ${JSON.stringify(f)}`);
|
|
295
|
-
const rows = yield Promise.all([lq.execute(), tq.execute()]);
|
|
296
|
-
const epochs = {};
|
|
297
|
-
const liabilities = [];
|
|
298
|
-
for (const l of rows[0]) {
|
|
299
|
-
epochs[`${l.ui}-${l.channel}`] = {
|
|
300
|
-
startTime: l.start_time,
|
|
301
|
-
endTime: l.end_time,
|
|
302
|
-
channel: l.channel,
|
|
303
|
-
ui: Number(l.ui),
|
|
304
|
-
};
|
|
305
|
-
liabilities.push({
|
|
306
|
-
id: BigInt(l.id),
|
|
307
|
-
solKey: l.sol_key,
|
|
308
|
-
epochId: Number(l.ui),
|
|
309
|
-
channel: l.channel,
|
|
310
|
-
createdAt: l.created_at,
|
|
311
|
-
amountDue: BigInt(l.amount_due),
|
|
312
|
-
deviceId: l.device_id,
|
|
313
|
-
points: l.points ? BigInt(l.points) : undefined,
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
const transfers = rows[1].map((r) => ({
|
|
317
|
-
id: BigInt(r.id),
|
|
318
|
-
fromSolKey: r.from_sol_key,
|
|
319
|
-
toSolKey: r.to_sol_key,
|
|
320
|
-
symbol: r.symbol,
|
|
321
|
-
channel: r.channel,
|
|
322
|
-
solTx: r.sig,
|
|
323
|
-
executedAt: r.executed_at,
|
|
324
|
-
amountPayed: BigInt(r.amount_payed),
|
|
325
|
-
}));
|
|
326
|
-
return { transfers, liabilities, epochs: Object.values(epochs) };
|
|
327
|
-
});
|
|
328
|
-
},
|
|
329
|
-
getOutstanding(f, log = logger_1.consoleLogger) {
|
|
330
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
331
|
-
let q = db
|
|
332
|
-
.selectFrom((eb) => eb
|
|
333
|
-
.selectFrom((eb) => {
|
|
334
|
-
let b = eb
|
|
335
|
-
.selectFrom(exports.SOL_TRANSFER_TABLE)
|
|
336
|
-
.select(({ fn }) => fn.sum(`amount_payed`).as('payed'))
|
|
337
|
-
.innerJoin(exports.ENTITY_TABLE, `${exports.SOL_TRANSFER_TABLE}.to_entity_id`, 'entity.id')
|
|
338
|
-
.select(['entity.sol_key'])
|
|
339
|
-
.groupBy('entity.sol_key');
|
|
340
|
-
if (f.banned) {
|
|
341
|
-
b = b.where('entity.banned', '=', true);
|
|
342
|
-
}
|
|
343
|
-
else {
|
|
344
|
-
b = b.where('entity.banned', '=', false);
|
|
345
|
-
}
|
|
346
|
-
if (f.channels && f.channels.length > 0) {
|
|
347
|
-
b = b.where((eb) => eb.or(f.channels.map((c) => eb('sol_transfer1.channel', '=', c))));
|
|
348
|
-
}
|
|
349
|
-
else {
|
|
350
|
-
b = b.where((eb) => eb.or([
|
|
351
|
-
eb('sol_transfer1.channel', '=', 'node_operator'),
|
|
352
|
-
eb('sol_transfer1.channel', '=', 'availability'),
|
|
353
|
-
]));
|
|
354
|
-
}
|
|
355
|
-
return b.as('y');
|
|
356
|
-
})
|
|
357
|
-
.fullJoin((eb) => {
|
|
358
|
-
let b = eb
|
|
359
|
-
.selectFrom(exports.LIABILITY_TABLE)
|
|
360
|
-
.select(({ fn }) => fn.sum(`amount_due`).as('due'))
|
|
361
|
-
.innerJoin(exports.ENTITY_TABLE, `${exports.LIABILITY_TABLE}.entity_id`, 'entity.id')
|
|
362
|
-
.select(['entity.sol_key'])
|
|
363
|
-
.groupBy('entity.sol_key');
|
|
364
|
-
if (f.banned) {
|
|
365
|
-
b = b.where('entity.banned', '=', true);
|
|
366
|
-
}
|
|
367
|
-
else {
|
|
368
|
-
b = b.where('entity.banned', '=', false);
|
|
369
|
-
}
|
|
370
|
-
if (f.channels && f.channels.length > 0) {
|
|
371
|
-
b = b.where((eb) => eb.or(f.channels.map((c) => eb('liability.channel', '=', c))));
|
|
372
|
-
}
|
|
373
|
-
else {
|
|
374
|
-
b = b.where((eb) => eb.or([
|
|
375
|
-
eb('liability.channel', '=', 'node_operator'),
|
|
376
|
-
eb('liability.channel', '=', 'availability'),
|
|
377
|
-
]));
|
|
378
|
-
}
|
|
379
|
-
return b.as('x');
|
|
380
|
-
}, (join) => join.onRef('y.sol_key', '=', 'x.sol_key'))
|
|
381
|
-
.select(({ fn }) => [
|
|
382
|
-
fn.coalesce('y.sol_key', 'x.sol_key').as('sol_key'),
|
|
383
|
-
(0, kysely_1.sql) `${fn.coalesce(`x.due`, (0, kysely_1.sql) `0`)} - ${fn.coalesce(`y.payed`, (0, kysely_1.sql) `0`)}`.as('out'),
|
|
384
|
-
])
|
|
385
|
-
.as('z'))
|
|
386
|
-
.selectAll();
|
|
387
|
-
if (f.due == 'overpayed') {
|
|
388
|
-
q = q.where('out', '<', '0');
|
|
389
|
-
}
|
|
390
|
-
else if (f.due == 'underpayed') {
|
|
391
|
-
q = q.where('out', '>', '0');
|
|
392
|
-
}
|
|
393
|
-
log.info(`${q.compile().sql} ps ${JSON.stringify(f)}`);
|
|
394
|
-
const res = yield q.execute();
|
|
395
|
-
const out = {};
|
|
396
|
-
for (const r of res) {
|
|
397
|
-
out[r.sol_key] = BigInt(r.out);
|
|
398
|
-
}
|
|
399
|
-
return out;
|
|
400
|
-
});
|
|
401
|
-
},
|
|
402
|
-
fetchNodeOperatorEpoch(epochId, log = logger_1.consoleLogger) {
|
|
403
|
-
var _a;
|
|
404
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
405
|
-
const q = db
|
|
406
|
-
.selectFrom(exports.JOB_ID_TABLE)
|
|
407
|
-
.innerJoin(exports.EPOCH_TABLE, 'epoch.id', 'job_id.epoch_id')
|
|
408
|
-
.leftJoin(exports.MANUAL_BURN_TABLE, 'manual_burn.epoch_id', 'epoch.id')
|
|
409
|
-
.where('epoch.ui', '=', epochId.toString())
|
|
410
|
-
.where('epoch.channel', '=', 'node_operator')
|
|
411
|
-
.select([
|
|
412
|
-
'job_id.id',
|
|
413
|
-
'epoch.total_rndr',
|
|
414
|
-
'epoch.start_time',
|
|
415
|
-
'epoch.end_time',
|
|
416
|
-
'manual_burn.burned',
|
|
417
|
-
'manual_burn.tx_sig',
|
|
418
|
-
])
|
|
419
|
-
.orderBy('job_id.id asc');
|
|
420
|
-
log.debug(`fetch epoch info q ${q.compile().sql} epochId ${epochId}`);
|
|
421
|
-
const res = yield q.execute();
|
|
422
|
-
if (res.length == 0) {
|
|
423
|
-
throw new Error(`epoch ${epochId} not found`);
|
|
424
|
-
}
|
|
425
|
-
log.debug(`fetchNodeOperatorEpoch -> fetching liablities`);
|
|
426
|
-
const ks = yield db
|
|
427
|
-
.selectFrom(exports.LIABILITY_TABLE)
|
|
428
|
-
.innerJoin(exports.ENTITY_TABLE, 'liability.entity_id', 'entity.id')
|
|
429
|
-
.select(({ fn }) => [(0, kysely_1.sql) `distinct(entity.sol_key)`.as('sol_key')])
|
|
430
|
-
.innerJoin(exports.EPOCH_TABLE, 'liability.epoch_id', 'epoch.id')
|
|
431
|
-
.where('epoch.ui', '=', epochId.toString())
|
|
432
|
-
.where('epoch.channel', '=', 'node_operator')
|
|
433
|
-
.where('liability.channel', '=', 'node_operator')
|
|
434
|
-
.execute();
|
|
435
|
-
log.debug(`fetchNodeOperatorEpoch -> fetched liablities`);
|
|
436
|
-
const solKeys = ks.reduce((r, k) => {
|
|
437
|
-
r[k.sol_key] = {};
|
|
438
|
-
return r;
|
|
439
|
-
}, {});
|
|
440
|
-
return {
|
|
441
|
-
jobIds: res.map((r) => Number(r.id)),
|
|
442
|
-
totalRndr: BigInt(res[0].total_rndr),
|
|
443
|
-
startTime: res[0].start_time,
|
|
444
|
-
endTime: res[0].end_time,
|
|
445
|
-
burned: BigInt((_a = res[0].burned) !== null && _a !== void 0 ? _a : 0),
|
|
446
|
-
burnTx: res[0].tx_sig,
|
|
447
|
-
solKeys,
|
|
448
|
-
};
|
|
449
|
-
});
|
|
450
|
-
},
|
|
451
|
-
getLatestNodeOperatorEpoch(log = logger_1.consoleLogger) {
|
|
452
|
-
var _a;
|
|
453
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
454
|
-
const now = new Date(Date.now());
|
|
455
|
-
try {
|
|
456
|
-
const res = yield db
|
|
457
|
-
.selectFrom(exports.EPOCH_TABLE)
|
|
458
|
-
.selectAll()
|
|
459
|
-
.where('channel', '=', 'node_operator')
|
|
460
|
-
.where('end_time', '<=', now)
|
|
461
|
-
.where('saved_at', 'is', null)
|
|
462
|
-
.orderBy('end_time asc')
|
|
463
|
-
.limit(1)
|
|
464
|
-
.executeTakeFirstOrThrow();
|
|
465
|
-
return {
|
|
466
|
-
epochId: BigInt(res.ui),
|
|
467
|
-
startedAt: res.start_time,
|
|
468
|
-
endedAt: res.end_time,
|
|
469
|
-
savedAt: (_a = res.saved_at) !== null && _a !== void 0 ? _a : undefined,
|
|
470
|
-
};
|
|
471
|
-
}
|
|
472
|
-
catch (e) {
|
|
473
|
-
if (e instanceof kysely_1.NoResultError) {
|
|
474
|
-
log.info(`no node operator epoch found`);
|
|
475
|
-
return undefined;
|
|
476
|
-
}
|
|
477
|
-
throw e;
|
|
478
|
-
}
|
|
479
|
-
});
|
|
480
|
-
},
|
|
481
|
-
saveNodeOperatorEpoch(epochId, liabilities, jobIds, totalRndr, 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
|
-
const { epochDbIds } = yield createPayments(trx, liabilities.map((l) => ({
|
|
488
|
-
liability: l,
|
|
489
|
-
})), log);
|
|
490
|
-
log.info(`inserted liabilities`);
|
|
491
|
-
for (const id of jobIds) {
|
|
492
|
-
yield trx
|
|
493
|
-
.insertInto(exports.JOB_ID_TABLE)
|
|
494
|
-
.values({
|
|
495
|
-
id: BigInt(id),
|
|
496
|
-
epoch_id: epochDbIds[0],
|
|
497
|
-
})
|
|
498
|
-
.executeTakeFirstOrThrow();
|
|
499
|
-
}
|
|
500
|
-
const now = new Date();
|
|
501
|
-
yield updateTotalRndr(db, { uiEpochId: epochId, channel: 'node_operator' }, totalRndr, now, log);
|
|
502
|
-
log.debug(`saved epoch`);
|
|
503
|
-
/*const now = new Date(Date.now())
|
|
504
|
-
const res = await trx
|
|
505
|
-
.updateTable(EPOCH_TABLE)
|
|
506
|
-
.set({
|
|
507
|
-
saved_at: now,
|
|
508
|
-
total_rndr: totalRndr,
|
|
509
|
-
})
|
|
510
|
-
.where('channel')
|
|
511
|
-
.where('ui', '=', epochId.toString())
|
|
512
|
-
.where('', '=')
|
|
513
|
-
.executeTakeFirstOrThrow()*/
|
|
514
|
-
}));
|
|
515
|
-
return;
|
|
516
|
-
});
|
|
517
|
-
},
|
|
518
|
-
produceBurnedJobs(jobs, log = logger_1.consoleLogger) {
|
|
519
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
520
|
-
yield db.transaction().execute((trx) => __awaiter(this, void 0, void 0, function* () {
|
|
521
|
-
var _a;
|
|
522
|
-
let num = BigInt(0);
|
|
523
|
-
for (const { amount, completedAt, jobId } of jobs) {
|
|
524
|
-
const res = yield trx
|
|
525
|
-
.insertInto(exports.JOB_TABLE)
|
|
526
|
-
.values({
|
|
527
|
-
id: jobId,
|
|
528
|
-
render_amt: amount,
|
|
529
|
-
completed_at: completedAt,
|
|
530
|
-
inserted_at: new Date(Date.now()),
|
|
531
|
-
})
|
|
532
|
-
.onConflict((oc) => oc.column('id').doNothing())
|
|
533
|
-
.executeTakeFirstOrThrow();
|
|
534
|
-
num += (_a = res.numInsertedOrUpdatedRows) !== null && _a !== void 0 ? _a : BigInt(0);
|
|
535
|
-
}
|
|
536
|
-
log.debug(`inserted ${num} of ${jobs.length} burn jobs`);
|
|
537
|
-
}));
|
|
538
|
-
});
|
|
539
|
-
},
|
|
540
|
-
markJobsBurnt(trx, p, log = logger_1.consoleLogger) {
|
|
541
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
542
|
-
const solTxId = yield getOrInsertTx(trx, { sig: p.solTx, executedAt: p.executedAt }, log);
|
|
543
|
-
const { id } = yield trx
|
|
544
|
-
.insertInto(exports.BURN_TABLE)
|
|
545
|
-
.values({
|
|
546
|
-
sol_tx_id: solTxId,
|
|
547
|
-
priced_at: p.pricedAt,
|
|
548
|
-
usdc_spent: p.usdcSpent,
|
|
549
|
-
quote_amt: p.quoteAmt,
|
|
550
|
-
burned: p.burned,
|
|
551
|
-
})
|
|
552
|
-
//.onConflict((oc) => oc.column('sol_tx').doNothing())
|
|
553
|
-
.returning((eb) => ['id as id'])
|
|
554
|
-
.executeTakeFirstOrThrow();
|
|
555
|
-
const jobIdsStr = p.jobsIds.map(String);
|
|
556
|
-
const res = yield trx
|
|
557
|
-
.updateTable(exports.JOB_TABLE)
|
|
558
|
-
.where('id', 'in', jobIdsStr)
|
|
559
|
-
.set('burn_id', id)
|
|
560
|
-
.executeTakeFirstOrThrow();
|
|
561
|
-
log.debug(`set burn_id on ${res.numUpdatedRows} jobs`);
|
|
562
|
-
return;
|
|
563
|
-
});
|
|
564
|
-
},
|
|
565
|
-
markJobsBurned(p, log = logger_1.consoleLogger) {
|
|
566
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
567
|
-
let { markedAt, solTx } = p;
|
|
568
|
-
let res = yield db
|
|
569
|
-
.updateTable(exports.BURN_TABLE)
|
|
570
|
-
.set({ marked_burned_at: markedAt })
|
|
571
|
-
.from(exports.SOL_TX_TABLE)
|
|
572
|
-
.whereRef('burn.sol_tx_id', '=', 'sol_tx.id')
|
|
573
|
-
.where('sol_tx.sig', '=', solTx)
|
|
574
|
-
.executeTakeFirstOrThrow();
|
|
575
|
-
log.debug(`set ${res.numUpdatedRows} ${solTx} -> marked_burned_at ${markedAt.toISOString()}`);
|
|
576
|
-
});
|
|
577
|
-
},
|
|
578
|
-
consumeAndLockBurnJobs(limit, lowerRndrThreshold, upperRndrThreshold, log = logger_1.consoleLogger) {
|
|
579
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
580
|
-
const trx = yield begin(db);
|
|
581
|
-
const q = trx.trx
|
|
582
|
-
.selectFrom(trx.trx
|
|
583
|
-
.selectFrom(exports.JOB_TABLE)
|
|
584
|
-
.select(({ fn, val, ref }) => [
|
|
585
|
-
'id',
|
|
586
|
-
fn
|
|
587
|
-
.sum('render_amt')
|
|
588
|
-
.over((ob) => ob.orderBy(`completed_at`, `asc`))
|
|
589
|
-
.as('total'),
|
|
590
|
-
])
|
|
591
|
-
.where('burn_id', 'is', null)
|
|
592
|
-
.as('t'))
|
|
593
|
-
.select('id')
|
|
594
|
-
.where('total', '>', lowerRndrThreshold)
|
|
595
|
-
.where('total', '<', upperRndrThreshold);
|
|
596
|
-
log.debug(`outstanding jobs q: ${q.compile().sql} ${JSON.stringify({
|
|
597
|
-
limit,
|
|
598
|
-
lowerRndrThreshold,
|
|
599
|
-
upperRndrThreshold,
|
|
600
|
-
})}`);
|
|
601
|
-
const idRs = yield q.execute();
|
|
602
|
-
const ids = idRs.map((x) => x.id);
|
|
603
|
-
log.debug(`${ids.length} candidate jobs for burn. locking up to ${limit} jobs`);
|
|
604
|
-
if (ids.length == 0) {
|
|
605
|
-
return { trx, jobs: [] };
|
|
606
|
-
}
|
|
607
|
-
const rows = yield trx.trx
|
|
608
|
-
.selectFrom(exports.JOB_TABLE)
|
|
609
|
-
.selectAll()
|
|
610
|
-
.where('id', 'in', ids)
|
|
611
|
-
.limit(limit)
|
|
612
|
-
.forUpdate()
|
|
613
|
-
.skipLocked()
|
|
614
|
-
.execute();
|
|
615
|
-
log.debug(`locked ${rows.length} jobs`);
|
|
616
|
-
const jobs = [];
|
|
617
|
-
for (const r of rows) {
|
|
618
|
-
jobs.push({
|
|
619
|
-
id: Number(r.id),
|
|
620
|
-
completedAt: r.completed_at,
|
|
621
|
-
rndrUsed: BigInt(r.render_amt),
|
|
622
|
-
});
|
|
623
|
-
}
|
|
624
|
-
return { trx, jobs };
|
|
625
|
-
});
|
|
626
|
-
},
|
|
627
|
-
incrBuyBurnMetrics(log = logger_1.consoleLogger) {
|
|
628
|
-
var _a;
|
|
629
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
630
|
-
const res = yield db
|
|
631
|
-
.selectFrom(exports.JOB_TABLE)
|
|
632
|
-
.select('completed_at')
|
|
633
|
-
.orderBy('completed_at asc')
|
|
634
|
-
.where('burn_id', 'is', null)
|
|
635
|
-
.execute();
|
|
636
|
-
if (res.length == 0) {
|
|
637
|
-
return { outstandingJobs: 0 };
|
|
638
|
-
}
|
|
639
|
-
return {
|
|
640
|
-
outstandingJobs: res.length,
|
|
641
|
-
oldestJobFinishedAt: (_a = res[0].completed_at) !== null && _a !== void 0 ? _a : undefined,
|
|
642
|
-
};
|
|
643
|
-
});
|
|
644
|
-
},
|
|
645
|
-
getLiabilitySolKeys(f) {
|
|
646
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
647
|
-
let q = db
|
|
648
|
-
.selectFrom(exports.LIABILITY_TABLE)
|
|
649
|
-
.innerJoin(exports.ENTITY_TABLE, `${exports.LIABILITY_TABLE}.entity_id`, 'entity.id')
|
|
650
|
-
.select(({ fn }) => [(0, kysely_1.sql) `distinct(entity.sol_key)`.as('sol_key')])
|
|
651
|
-
.where(`${exports.LIABILITY_TABLE}.channel`, '=', f.channel);
|
|
652
|
-
if (f.epoch) {
|
|
653
|
-
q = q.where('liability.epoch_id', '=', db
|
|
654
|
-
.selectFrom('epoch')
|
|
655
|
-
.select('id')
|
|
656
|
-
.where('channel', '=', f.channel)
|
|
657
|
-
.where('ui', '=', String(f.epoch)));
|
|
658
|
-
}
|
|
659
|
-
const ks = yield q.execute();
|
|
660
|
-
const solKeys = {};
|
|
661
|
-
for (const k of ks) {
|
|
662
|
-
solKeys[k.sol_key] = {};
|
|
663
|
-
}
|
|
664
|
-
return solKeys;
|
|
665
|
-
});
|
|
666
|
-
},
|
|
667
|
-
deleteAll(log = logger_1.consoleLogger) {
|
|
668
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
669
|
-
if (config.database.includes('render')) {
|
|
670
|
-
throw new Error('cannot nuke from deployed database. call from tests only');
|
|
671
|
-
}
|
|
672
|
-
const tx = yield begin(db);
|
|
673
|
-
try {
|
|
674
|
-
const res = yield Promise.all([
|
|
675
|
-
tx.trx
|
|
676
|
-
.deleteFrom(exports.SOL_TX_TABLE)
|
|
677
|
-
.where('id', 'is not', null)
|
|
678
|
-
.executeTakeFirst(),
|
|
679
|
-
tx.trx.deleteFrom(exports.LIABILITY_TABLE).executeTakeFirstOrThrow(),
|
|
680
|
-
tx.trx.deleteFrom(exports.SOL_TRANSFER_TABLE).executeTakeFirstOrThrow(),
|
|
681
|
-
tx.trx
|
|
682
|
-
.deleteFrom(exports.EPOCH_TABLE)
|
|
683
|
-
.where('id', 'is not', null)
|
|
684
|
-
.executeTakeFirst(),
|
|
685
|
-
tx.trx
|
|
686
|
-
.deleteFrom(exports.ENTITY_TABLE)
|
|
687
|
-
.where('id', 'is not', null)
|
|
688
|
-
.executeTakeFirst(),
|
|
689
|
-
tx.trx
|
|
690
|
-
.deleteFrom(exports.MANUAL_BURN_TABLE)
|
|
691
|
-
.where('epoch_id', 'is not', null)
|
|
692
|
-
.executeTakeFirst(),
|
|
693
|
-
tx.trx
|
|
694
|
-
.deleteFrom(exports.JOB_ID_TABLE)
|
|
695
|
-
.where('id', 'is not', null)
|
|
696
|
-
.executeTakeFirst(),
|
|
697
|
-
tx.trx
|
|
698
|
-
.deleteFrom(exports.JOB_TABLE)
|
|
699
|
-
.where('id', 'is not', null)
|
|
700
|
-
.executeTakeFirst(),
|
|
701
|
-
tx.trx
|
|
702
|
-
.deleteFrom(exports.BURN_TABLE)
|
|
703
|
-
.where('id', 'is not', null)
|
|
704
|
-
.executeTakeFirst(),
|
|
705
|
-
]);
|
|
706
|
-
tx.commit();
|
|
707
|
-
}
|
|
708
|
-
catch (e) {
|
|
709
|
-
log.error(`caught ${e}`);
|
|
710
|
-
tx.rollback();
|
|
711
|
-
}
|
|
712
|
-
});
|
|
713
|
-
},
|
|
714
|
-
};
|
|
715
|
-
};
|
|
716
|
-
exports.pgClient = pgClient;
|
|
717
|
-
function begin(db) {
|
|
718
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
719
|
-
const connection = new Deferred();
|
|
720
|
-
const result = new Deferred();
|
|
721
|
-
// Do NOT await this line.
|
|
722
|
-
db.transaction()
|
|
723
|
-
.setIsolationLevel('repeatable read')
|
|
724
|
-
.execute((trx) => {
|
|
725
|
-
connection.resolve(trx);
|
|
726
|
-
return result.promise;
|
|
727
|
-
})
|
|
728
|
-
.catch((err) => {
|
|
729
|
-
// Don't do anything here. Just swallow the exception.
|
|
730
|
-
});
|
|
731
|
-
const trx = yield connection.promise;
|
|
732
|
-
return {
|
|
733
|
-
trx,
|
|
734
|
-
commit() {
|
|
735
|
-
result.resolve(null);
|
|
736
|
-
},
|
|
737
|
-
rollback() {
|
|
738
|
-
result.reject(new Error('rollback'));
|
|
739
|
-
},
|
|
740
|
-
};
|
|
741
|
-
});
|
|
742
|
-
}
|
|
743
|
-
class Deferred {
|
|
744
|
-
constructor() {
|
|
745
|
-
_Deferred_promise.set(this, void 0);
|
|
746
|
-
_Deferred_resolve.set(this, void 0);
|
|
747
|
-
_Deferred_reject.set(this, void 0);
|
|
748
|
-
this.resolve = (value) => {
|
|
749
|
-
if (__classPrivateFieldGet(this, _Deferred_resolve, "f")) {
|
|
750
|
-
__classPrivateFieldGet(this, _Deferred_resolve, "f").call(this, value);
|
|
751
|
-
}
|
|
752
|
-
};
|
|
753
|
-
this.reject = (reason) => {
|
|
754
|
-
if (__classPrivateFieldGet(this, _Deferred_reject, "f")) {
|
|
755
|
-
__classPrivateFieldGet(this, _Deferred_reject, "f").call(this, reason);
|
|
756
|
-
}
|
|
757
|
-
};
|
|
758
|
-
__classPrivateFieldSet(this, _Deferred_promise, new Promise((resolve, reject) => {
|
|
759
|
-
__classPrivateFieldSet(this, _Deferred_reject, reject, "f");
|
|
760
|
-
__classPrivateFieldSet(this, _Deferred_resolve, resolve, "f");
|
|
761
|
-
}), "f");
|
|
762
|
-
}
|
|
763
|
-
get promise() {
|
|
764
|
-
return __classPrivateFieldGet(this, _Deferred_promise, "f");
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
exports.Deferred = Deferred;
|
|
768
|
-
_Deferred_promise = new WeakMap(), _Deferred_resolve = new WeakMap(), _Deferred_reject = new WeakMap();
|
|
769
|
-
//# sourceMappingURL=v2Client.js.map
|