@render-foundation/utils 0.0.232 → 0.0.233
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/client/dispersed.js +123 -0
- package/lib/cjs/client/dispersed.js.map +1 -0
- package/lib/cjs/client/pg/v2Client.js +115 -59
- package/lib/cjs/client/pg/v2Client.js.map +1 -1
- package/lib/cjs/client/price.js +41 -17
- package/lib/cjs/client/price.js.map +1 -1
- package/lib/cjs/client/trade.js +2 -0
- package/lib/cjs/client/trade.js.map +1 -1
- package/lib/cjs/index.js +5 -2
- package/lib/cjs/index.js.map +1 -1
- package/lib/esm/src/client/dispersed.js +108 -0
- package/lib/esm/src/client/dispersed.js.map +1 -0
- package/lib/esm/src/client/pg/v2Client.js +99 -56
- package/lib/esm/src/client/pg/v2Client.js.map +1 -1
- package/lib/esm/src/client/price.js +40 -18
- package/lib/esm/src/client/price.js.map +1 -1
- package/lib/esm/src/client/trade.js +2 -0
- package/lib/esm/src/client/trade.js.map +1 -1
- package/lib/esm/src/index.js +5 -2
- package/lib/esm/src/index.js.map +1 -1
- package/lib/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/src/client/dispersed.d.ts +91 -0
- package/lib/types/src/client/dispersed.d.ts.map +1 -0
- package/lib/types/src/client/pg/v2Client.d.ts +45 -24
- package/lib/types/src/client/pg/v2Client.d.ts.map +1 -1
- package/lib/types/src/client/price.d.ts.map +1 -1
- package/lib/types/src/client/trade.d.ts +2 -0
- package/lib/types/src/client/trade.d.ts.map +1 -1
- package/lib/types/src/dbTypesV2.d.ts +24 -16
- package/lib/types/src/dbTypesV2.d.ts.map +1 -1
- package/lib/types/src/index.d.ts +2 -2
- package/lib/types/src/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/lib/cjs/burn/burnAnalysis.js +0 -101
- package/lib/cjs/burn/burnAnalysis.js.map +0 -1
- package/lib/esm/src/burn/burnAnalysis.js +0 -90
- package/lib/esm/src/burn/burnAnalysis.js.map +0 -1
- package/lib/types/src/burn/burnAnalysis.d.ts +0 -51
- package/lib/types/src/burn/burnAnalysis.d.ts.map +0 -1
|
@@ -0,0 +1,123 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.dispersedClient = exports.DispersedConflictError = void 0;
|
|
16
|
+
const logger_1 = require("../logger");
|
|
17
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
18
|
+
/**
|
|
19
|
+
* Dispersed Token Burn API client (M2M OAuth2 client_credentials).
|
|
20
|
+
*
|
|
21
|
+
* Thin transport only: it mints/caches the bearer token and posts the two burn
|
|
22
|
+
* endpoints (claim, settle). It does NOT price RENDER, sum obligations, or do
|
|
23
|
+
* any burn math — the consumer owns that (incl. the `settled_amount == Σ
|
|
24
|
+
* obligation amounts` invariant). Amounts are passed through as decimal strings.
|
|
25
|
+
*
|
|
26
|
+
* Contract: see docs/dispersed-burn.md. Authoritative spec at
|
|
27
|
+
* `{apiBaseUrl}/openapi.json` (tag "Burn Ledger").
|
|
28
|
+
*/
|
|
29
|
+
// Both scopes, space-delimited, requested on every token mint.
|
|
30
|
+
const SCOPES = 'burn_ledger_claims.create.all burn_ledger_settlements.create.all';
|
|
31
|
+
// Re-mint a little before the token actually expires.
|
|
32
|
+
const TOKEN_REFRESH_MARGIN_MS = 60000;
|
|
33
|
+
/**
|
|
34
|
+
* Thrown on a 409 from settle: the claim is missing/expired/released/already
|
|
35
|
+
* settled, an obligation mismatch, an amount mismatch, or a tx_signature reused
|
|
36
|
+
* with a different payload. Nothing was written. Do NOT re-burn — reconcile.
|
|
37
|
+
*/
|
|
38
|
+
class DispersedConflictError extends Error {
|
|
39
|
+
constructor(body) {
|
|
40
|
+
super(`dispersed 409 conflict: ${body}`);
|
|
41
|
+
this.body = body;
|
|
42
|
+
this.status = 409;
|
|
43
|
+
this.name = 'DispersedConflictError';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.DispersedConflictError = DispersedConflictError;
|
|
47
|
+
const dispersedClient = (cfg, fetchImpl = node_fetch_1.default) => {
|
|
48
|
+
let cached;
|
|
49
|
+
const mintToken = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
+
const res = yield fetchImpl(`${cfg.authBaseUrl}/token`, {
|
|
51
|
+
method: 'POST',
|
|
52
|
+
headers: { 'Content-Type': 'application/json' },
|
|
53
|
+
body: JSON.stringify({
|
|
54
|
+
grant_type: 'client_credentials',
|
|
55
|
+
client_id: cfg.clientId,
|
|
56
|
+
client_secret: cfg.clientSecret,
|
|
57
|
+
scope: SCOPES,
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
60
|
+
if (res.status !== 200) {
|
|
61
|
+
throw new Error(`dispersed token mint failed. code: ${res.status} body: ${yield res.text()}`);
|
|
62
|
+
}
|
|
63
|
+
const json = (yield res.json());
|
|
64
|
+
cached = {
|
|
65
|
+
token: json.access_token,
|
|
66
|
+
expiresAt: Date.now() + json.expires_in * 1000,
|
|
67
|
+
};
|
|
68
|
+
return cached.token;
|
|
69
|
+
});
|
|
70
|
+
const getToken = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
71
|
+
if (cached && Date.now() < cached.expiresAt - TOKEN_REFRESH_MARGIN_MS) {
|
|
72
|
+
return cached.token;
|
|
73
|
+
}
|
|
74
|
+
return mintToken();
|
|
75
|
+
});
|
|
76
|
+
// POST with bearer; one transparent re-mint+retry on 401 (a dropped daily run
|
|
77
|
+
// is expensive, so it's worth recovering from a token that died mid-flight).
|
|
78
|
+
const authedPost = (path, body) => __awaiter(void 0, void 0, void 0, function* () {
|
|
79
|
+
const doPost = (token) => fetchImpl(`${cfg.apiBaseUrl}${path}`, {
|
|
80
|
+
method: 'POST',
|
|
81
|
+
headers: {
|
|
82
|
+
'Content-Type': 'application/json',
|
|
83
|
+
Authorization: `Bearer ${token}`,
|
|
84
|
+
},
|
|
85
|
+
body: JSON.stringify(body),
|
|
86
|
+
});
|
|
87
|
+
let res = yield doPost(yield getToken());
|
|
88
|
+
if (res.status === 401) {
|
|
89
|
+
cached = undefined;
|
|
90
|
+
res = yield doPost(yield mintToken());
|
|
91
|
+
}
|
|
92
|
+
return res;
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
claim({ from, to, limit }, log = logger_1.consoleLogger) {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
const res = yield authedPost('/v1/burn-ledger-claims', Object.assign({ executor_id: cfg.executorId, from: from.toISOString(), to: to.toISOString() }, (limit !== undefined ? { limit } : {})));
|
|
98
|
+
if (res.status !== 201) {
|
|
99
|
+
throw new Error(`dispersed claim failed. code: ${res.status} body: ${JSON.stringify(yield res.text())}`);
|
|
100
|
+
}
|
|
101
|
+
const result = (yield res.json());
|
|
102
|
+
log.info(`dispersed claim ${result.claim.uuid}: ${result.claimed_count} obligations, expires ${result.claim.claim_expires_at}`);
|
|
103
|
+
return result;
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
settle(req, log = logger_1.consoleLogger) {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
const res = yield authedPost('/v1/burn-ledger-settlements', req);
|
|
109
|
+
if (res.status === 409) {
|
|
110
|
+
throw new DispersedConflictError(yield res.text());
|
|
111
|
+
}
|
|
112
|
+
if (res.status !== 201 && res.status !== 200) {
|
|
113
|
+
throw new Error(`dispersed settle failed. code: ${res.status} body: ${JSON.stringify(yield res.text())}`);
|
|
114
|
+
}
|
|
115
|
+
const result = (yield res.json());
|
|
116
|
+
log.info(`dispersed settle ${result.settlement.uuid} for tx ${req.tx_signature} (replay=${result.idempotent_replay})`);
|
|
117
|
+
return result;
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
exports.dispersedClient = dispersedClient;
|
|
123
|
+
//# sourceMappingURL=dispersed.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispersed.js","sourceRoot":"","sources":["../../../src/client/dispersed.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,sCAAuD;AACvD,4DAA8B;AAE9B;;;;;;;;;;GAUG;AAEH,+DAA+D;AAC/D,MAAM,MAAM,GACV,kEAAkE,CAAA;AAEpE,sDAAsD;AACtD,MAAM,uBAAuB,GAAG,KAAM,CAAA;AA6EtC;;;;GAIG;AACH,MAAa,sBAAuB,SAAQ,KAAK;IAE/C,YAA4B,IAAY;QACtC,KAAK,CAAC,2BAA2B,IAAI,EAAE,CAAC,CAAA;QADd,SAAI,GAAJ,IAAI,CAAQ;QAD/B,WAAM,GAAG,GAAG,CAAA;QAGnB,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAA;IACtC,CAAC;CACF;AAND,wDAMC;AAUM,MAAM,eAAe,GAAG,CAC7B,GAAoB,EACpB,YAAqB,oBAA2B,EAC/B,EAAE;IACnB,IAAI,MAAwD,CAAA;IAE5D,MAAM,SAAS,GAAG,GAA0B,EAAE;QAC5C,MAAM,GAAG,GAAG,MAAM,SAAS,CAAC,GAAG,GAAG,CAAC,WAAW,QAAQ,EAAE;YACtD,MAAM,E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BAAa;;gBACnC,MAAM,GAAG,GAAG,MAAM,UAAU,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAA;gBAChE,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE;oBACtB,MAAM,IAAI,sBAAsB,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC,CAAA;iBACnD;gBACD,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE;oBAC5C,MAAM,IAAI,KAAK,CACb,kCAAkC,GAAG,CAAC,MAAM,UAAU,IAAI,CAAC,SAAS,CAClE,MAAM,GAAG,CAAC,IAAI,EAAE,CACjB,EAAE,CACJ,CAAA;iBACF;gBACD,MAAM,MAAM,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAiB,CAAA;gBACjD,GAAG,CAAC,IAAI,CACN,oBAAoB,MAAM,CAAC,UAAU,CAAC,IAAI,WAAW,GAAG,CAAC,YAAY,YAAY,MAAM,CAAC,iBAAiB,GAAG,CAC7G,CAAA;gBACD,OAAO,MAAM,CAAA;YACf,CAAC;SAAA;KACF,CAAA;AACH,CAAC,CAAA;AAxGY,QAAA,eAAe,mBAwG3B"}
|
|
@@ -35,7 +35,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
35
35
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
36
|
};
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
-
exports.pgClient = exports.batchFinishedAtMedian = exports.GRANT_BURN_TABLE = exports.GRANT_BURN_ID_TABLE = exports.USER_GRANT_SPEND_TABLE = exports.CURRENT_USER_GRANT_SPEND_TABLE = exports.POLYGON_UPGRADE_TABLE = exports.ENTITY_EPOCH_INFO_TABLE = exports.
|
|
38
|
+
exports.pgClient = exports.batchFinishedAtMedian = exports.DISPERSED_SETTLEMENT_TABLE = exports.GRANT_BURN_TABLE = exports.GRANT_BURN_ID_TABLE = exports.USER_GRANT_SPEND_TABLE = exports.CURRENT_USER_GRANT_SPEND_TABLE = exports.POLYGON_UPGRADE_TABLE = exports.ENTITY_EPOCH_INFO_TABLE = exports.BURN_CORRECTION_TABLE = exports.NETWORK_REVENUE_TABLE = exports.BRIDGE_TRANSFER_TABLE = exports.BURN_TABLE = exports.JOB_TABLE = exports.JOB_ID_TABLE = exports.LIABILITY_ADJUSTMENT_BATCH_TABLE = exports.LIABILITY_ADJUSTMENT_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
39
|
const kysely_1 = require("kysely");
|
|
40
40
|
const logger_1 = require("../../logger");
|
|
41
41
|
const pg_1 = require("pg");
|
|
@@ -54,14 +54,14 @@ exports.JOB_TABLE = 'job';
|
|
|
54
54
|
exports.BURN_TABLE = 'burn';
|
|
55
55
|
exports.BRIDGE_TRANSFER_TABLE = 'bridge_transfer';
|
|
56
56
|
exports.NETWORK_REVENUE_TABLE = 'network_revenue';
|
|
57
|
-
exports.
|
|
58
|
-
exports.TX_BURN_ADJUSTMENT_TABLE = 'tx_burn_adjustment';
|
|
57
|
+
exports.BURN_CORRECTION_TABLE = 'burn_correction';
|
|
59
58
|
exports.ENTITY_EPOCH_INFO_TABLE = 'entity_epoch_info';
|
|
60
59
|
exports.POLYGON_UPGRADE_TABLE = 'polygon_upgrade';
|
|
61
60
|
exports.CURRENT_USER_GRANT_SPEND_TABLE = 'current_user_grant_spend';
|
|
62
61
|
exports.USER_GRANT_SPEND_TABLE = 'user_grant_spend';
|
|
63
62
|
exports.GRANT_BURN_ID_TABLE = 'grant_burn_id';
|
|
64
63
|
exports.GRANT_BURN_TABLE = 'grant_burn';
|
|
64
|
+
exports.DISPERSED_SETTLEMENT_TABLE = 'dispersed_settlement';
|
|
65
65
|
BigInt.prototype.toJSON = function () {
|
|
66
66
|
return this.toString();
|
|
67
67
|
};
|
|
@@ -1124,7 +1124,7 @@ const pgClient = (config) => {
|
|
|
1124
1124
|
let buyBurnId;
|
|
1125
1125
|
let grantBurnId;
|
|
1126
1126
|
if (p.buyBurn) {
|
|
1127
|
-
const { burned, usdcSpent, eurToUsdc, renderToUsdc, quoteAmt, tags,
|
|
1127
|
+
const { burned, usdcSpent, eurToUsdc, renderToUsdc, quoteAmt, tags, downCorrectionId, escrowCorrectionId, toBurn, fromEscrowUsdc, } = p.buyBurn;
|
|
1128
1128
|
let pricedAt = p.buyBurn.pricedAt;
|
|
1129
1129
|
if (!pricedAt) {
|
|
1130
1130
|
const jobs = yield fetchJobs1(trx.trx, { ids: p.jobs.map((j) => String(j.id)) }, log);
|
|
@@ -1143,8 +1143,8 @@ const pgClient = (config) => {
|
|
|
1143
1143
|
quote_amt: quoteAmt,
|
|
1144
1144
|
burned: burned,
|
|
1145
1145
|
tags: tags.join(','),
|
|
1146
|
-
|
|
1147
|
-
|
|
1146
|
+
down_correction_id: downCorrectionId,
|
|
1147
|
+
escrow_correction_id: escrowCorrectionId,
|
|
1148
1148
|
to_burn: toBurn,
|
|
1149
1149
|
from_escrow_usdc: fromEscrowUsdc !== null && fromEscrowUsdc !== void 0 ? fromEscrowUsdc : 0,
|
|
1150
1150
|
};
|
|
@@ -1710,6 +1710,73 @@ const pgClient = (config) => {
|
|
|
1710
1710
|
}));
|
|
1711
1711
|
});
|
|
1712
1712
|
},
|
|
1713
|
+
insertDispersedOutbox(p, log = logger_1.consoleLogger) {
|
|
1714
|
+
var _a, _b;
|
|
1715
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1716
|
+
const res = yield db
|
|
1717
|
+
.insertInto(exports.DISPERSED_SETTLEMENT_TABLE)
|
|
1718
|
+
.values({
|
|
1719
|
+
tx_signature: p.txSignature,
|
|
1720
|
+
claim_uuid: p.claimUuid,
|
|
1721
|
+
obligation_uuids: p.obligationUuids,
|
|
1722
|
+
settled_amount: p.settledAmount,
|
|
1723
|
+
burned_amount: p.burnedAmount,
|
|
1724
|
+
executed_at: p.executedAt,
|
|
1725
|
+
eur_render: (_a = p.eurRender) !== null && _a !== void 0 ? _a : null,
|
|
1726
|
+
last_valid_block_height: (_b = p.lastValidBlockHeight) !== null && _b !== void 0 ? _b : null,
|
|
1727
|
+
})
|
|
1728
|
+
.onConflict((oc) => oc.column('tx_signature').doNothing())
|
|
1729
|
+
.executeTakeFirst();
|
|
1730
|
+
log.debug(`dispersed outbox tx ${p.txSignature}: ${res.numInsertedOrUpdatedRows} row(s)`);
|
|
1731
|
+
});
|
|
1732
|
+
},
|
|
1733
|
+
markDispersedSettled(txSignature, p, log = logger_1.consoleLogger) {
|
|
1734
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1735
|
+
yield db
|
|
1736
|
+
.updateTable(exports.DISPERSED_SETTLEMENT_TABLE)
|
|
1737
|
+
.set({ settled_at: new Date(), idempotent_replay: p.idempotentReplay })
|
|
1738
|
+
.where('tx_signature', '=', txSignature)
|
|
1739
|
+
.execute();
|
|
1740
|
+
log.debug(`dispersed settled tx ${txSignature}`);
|
|
1741
|
+
});
|
|
1742
|
+
},
|
|
1743
|
+
getUnsettledDispersed(log = logger_1.consoleLogger) {
|
|
1744
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1745
|
+
const rows = yield db
|
|
1746
|
+
.selectFrom(exports.DISPERSED_SETTLEMENT_TABLE)
|
|
1747
|
+
.selectAll()
|
|
1748
|
+
.where('settled_at', 'is', null)
|
|
1749
|
+
.execute();
|
|
1750
|
+
log.debug(`dispersed outbox: ${rows.length} unsettled`);
|
|
1751
|
+
return rows.map((r) => {
|
|
1752
|
+
var _a;
|
|
1753
|
+
return ({
|
|
1754
|
+
txSignature: r.tx_signature,
|
|
1755
|
+
claimUuid: r.claim_uuid,
|
|
1756
|
+
obligationUuids: r.obligation_uuids,
|
|
1757
|
+
settledAmount: r.settled_amount,
|
|
1758
|
+
burnedAmount: r.burned_amount,
|
|
1759
|
+
executedAt: r.executed_at,
|
|
1760
|
+
eurRender: (_a = r.eur_render) !== null && _a !== void 0 ? _a : undefined,
|
|
1761
|
+
// int8 comes back as a string from pg; block heights are well within JS safe-integer range.
|
|
1762
|
+
lastValidBlockHeight: r.last_valid_block_height != null
|
|
1763
|
+
? Number(r.last_valid_block_height)
|
|
1764
|
+
: undefined,
|
|
1765
|
+
});
|
|
1766
|
+
});
|
|
1767
|
+
});
|
|
1768
|
+
},
|
|
1769
|
+
voidDispersedOutbox(txSignature, log = logger_1.consoleLogger) {
|
|
1770
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1771
|
+
const res = yield db
|
|
1772
|
+
.deleteFrom(exports.DISPERSED_SETTLEMENT_TABLE)
|
|
1773
|
+
.where('tx_signature', '=', txSignature)
|
|
1774
|
+
// Never delete a row we already settled — void only applies to pending (un-landed) rows.
|
|
1775
|
+
.where('settled_at', 'is', null)
|
|
1776
|
+
.executeTakeFirst();
|
|
1777
|
+
log.debug(`dispersed outbox void tx ${txSignature}: ${res.numDeletedRows} row(s)`);
|
|
1778
|
+
});
|
|
1779
|
+
},
|
|
1713
1780
|
fetchJobs(db, f, log = logger_1.consoleLogger) {
|
|
1714
1781
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1715
1782
|
return yield fetchJobs1(db.trx, f, log);
|
|
@@ -1779,90 +1846,79 @@ const pgClient = (config) => {
|
|
|
1779
1846
|
}));
|
|
1780
1847
|
});
|
|
1781
1848
|
},
|
|
1782
|
-
|
|
1849
|
+
createBurnCorrection(trx, p, log = logger_1.consoleLogger) {
|
|
1783
1850
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1784
|
-
//
|
|
1851
|
+
// Anchor + unit invariants are also DB CHECKs; validate here for a clearer error.
|
|
1852
|
+
if ((p.jobId == null) === (p.solTxId == null))
|
|
1853
|
+
throw new Error('burn_correction needs exactly one anchor (jobId XOR solTxId)');
|
|
1854
|
+
if (p.kind === 'down_adjust' && p.amountRender == null)
|
|
1855
|
+
throw new Error('down_adjust needs amountRender');
|
|
1856
|
+
if (p.kind === 'escrow_credit' && p.amountUsdc == null)
|
|
1857
|
+
throw new Error('escrow_credit needs amountUsdc');
|
|
1858
|
+
// Idempotent per (anchor, kind) — a re-run for the same bad job/tx is a no-op.
|
|
1785
1859
|
const row = yield trx.trx
|
|
1786
|
-
.insertInto(exports.
|
|
1860
|
+
.insertInto(exports.BURN_CORRECTION_TABLE)
|
|
1787
1861
|
.values({
|
|
1862
|
+
kind: p.kind,
|
|
1863
|
+
job_id: p.jobId,
|
|
1788
1864
|
sol_tx_id: p.solTxId,
|
|
1789
|
-
|
|
1865
|
+
amount_render: p.amountRender,
|
|
1866
|
+
amount_usdc: p.amountUsdc,
|
|
1790
1867
|
description: p.description,
|
|
1791
1868
|
})
|
|
1792
|
-
.onConflict((oc) => oc.
|
|
1869
|
+
.onConflict((oc) => oc.doNothing())
|
|
1793
1870
|
.returning('id')
|
|
1794
1871
|
.executeTakeFirst();
|
|
1795
1872
|
if (!row) {
|
|
1796
|
-
log.info(`
|
|
1873
|
+
log.info(`burn_correction ${p.kind} for ${p.jobId != null ? `job ${p.jobId}` : `sol_tx ${p.solTxId}`} already exists — skipped`);
|
|
1797
1874
|
return undefined;
|
|
1798
1875
|
}
|
|
1799
|
-
log.info(`created
|
|
1876
|
+
log.info(`created burn_correction ${row.id} (${p.kind}, ${p.jobId != null ? `job ${p.jobId}` : `sol_tx ${p.solTxId}`}): ${p.kind === 'down_adjust' ? `${p.amountRender} RENDER` : `${p.amountUsdc} USDC`}`);
|
|
1800
1877
|
return Number(row.id);
|
|
1801
1878
|
});
|
|
1802
1879
|
},
|
|
1803
|
-
|
|
1880
|
+
getBurnCorrections(trx, ps, log = logger_1.consoleLogger) {
|
|
1804
1881
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1805
1882
|
if (!ps.toFill)
|
|
1806
1883
|
return [];
|
|
1884
|
+
// fill column depends on kind: down_adjust consumes (to_burn - burned); escrow_credit consumes
|
|
1885
|
+
// from_escrow_usdc. Each links via its own burn FK so one burn can carry one of each.
|
|
1886
|
+
const linkCol = ps.kind === 'down_adjust' ? 'down_correction_id' : 'escrow_correction_id';
|
|
1807
1887
|
const q = trx.trx
|
|
1808
|
-
.selectFrom(exports.
|
|
1809
|
-
.selectAll(exports.
|
|
1810
|
-
.
|
|
1811
|
-
.select([`${exports.JOB_TABLE}.completed_at`, `${exports.JOB_TABLE}.render_amt`])
|
|
1812
|
-
.leftJoin(exports.BURN_TABLE, `${exports.BURN_TABLE}.job_burn_adjustment_id`, `${exports.JOB_BURN_ADJUSTMENT_TABLE}.id`)
|
|
1888
|
+
.selectFrom(exports.BURN_CORRECTION_TABLE)
|
|
1889
|
+
.selectAll(exports.BURN_CORRECTION_TABLE)
|
|
1890
|
+
.leftJoin(exports.BURN_TABLE, `${exports.BURN_TABLE}.${linkCol}`, `${exports.BURN_CORRECTION_TABLE}.id`)
|
|
1813
1891
|
.select(({ fn, lit }) => [
|
|
1814
1892
|
fn.sum(fn.coalesce(`${exports.BURN_TABLE}.to_burn`, lit(0))).as('tot_to_burn'),
|
|
1815
1893
|
fn.sum(fn.coalesce(`${exports.BURN_TABLE}.burned`, lit(0))).as('tot_burned'),
|
|
1816
|
-
])
|
|
1817
|
-
.groupBy([
|
|
1818
|
-
`${exports.JOB_BURN_ADJUSTMENT_TABLE}.id`,
|
|
1819
|
-
`${exports.JOB_TABLE}.completed_at`,
|
|
1820
|
-
`${exports.JOB_TABLE}.render_amt`,
|
|
1821
|
-
]);
|
|
1822
|
-
log.info(`getJobBurnAdjustments ${q.compile().sql}`);
|
|
1823
|
-
const res = yield q.execute();
|
|
1824
|
-
return res
|
|
1825
|
-
.map((r) => ({
|
|
1826
|
-
id: Number(r.id),
|
|
1827
|
-
createdAt: r.created_at,
|
|
1828
|
-
jobId: Number(r.job_id),
|
|
1829
|
-
downAdjRndrUsed: Number(r.down_adj_rndr_used),
|
|
1830
|
-
downAdjToBurn: Number(r.down_adj_to_burn),
|
|
1831
|
-
adjusted: Number(r.tot_to_burn) - Number(r.tot_burned),
|
|
1832
|
-
job: {
|
|
1833
|
-
id: Number(r.job_id),
|
|
1834
|
-
completedAt: r.completed_at,
|
|
1835
|
-
rndrUsed: BigInt(r.render_amt),
|
|
1836
|
-
},
|
|
1837
|
-
}))
|
|
1838
|
-
.filter((a) => a.adjusted < a.downAdjToBurn);
|
|
1839
|
-
});
|
|
1840
|
-
},
|
|
1841
|
-
getTxBurnAdjustments(trx, ps, log = logger_1.consoleLogger) {
|
|
1842
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1843
|
-
if (!ps.toFill)
|
|
1844
|
-
return [];
|
|
1845
|
-
const q = trx.trx
|
|
1846
|
-
.selectFrom(exports.TX_BURN_ADJUSTMENT_TABLE)
|
|
1847
|
-
.selectAll(exports.TX_BURN_ADJUSTMENT_TABLE)
|
|
1848
|
-
.leftJoin(exports.BURN_TABLE, `${exports.BURN_TABLE}.tx_burn_adjustment_id`, `${exports.TX_BURN_ADJUSTMENT_TABLE}.id`)
|
|
1849
|
-
.select(({ fn, lit }) => [
|
|
1850
1894
|
fn
|
|
1851
1895
|
.sum(fn.coalesce(`${exports.BURN_TABLE}.from_escrow_usdc`, lit(0)))
|
|
1852
1896
|
.as('tot_from_escrow'),
|
|
1853
1897
|
])
|
|
1854
|
-
.
|
|
1855
|
-
|
|
1898
|
+
.where(`${exports.BURN_CORRECTION_TABLE}.kind`, '=', ps.kind)
|
|
1899
|
+
.groupBy(`${exports.BURN_CORRECTION_TABLE}.id`)
|
|
1900
|
+
.orderBy(`${exports.BURN_CORRECTION_TABLE}.created_at`, 'asc');
|
|
1901
|
+
log.info(`getBurnCorrections(${ps.kind}) ${q.compile().sql}`);
|
|
1856
1902
|
const res = yield q.execute();
|
|
1857
1903
|
return res
|
|
1858
1904
|
.map((r) => ({
|
|
1859
1905
|
id: Number(r.id),
|
|
1860
1906
|
createdAt: r.created_at,
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1907
|
+
jobId: r.job_id != null ? Number(r.job_id) : undefined,
|
|
1908
|
+
solTxId: r.sol_tx_id != null ? Number(r.sol_tx_id) : undefined,
|
|
1909
|
+
kind: r.kind,
|
|
1910
|
+
amountRender: r.amount_render != null ? Number(r.amount_render) : undefined,
|
|
1911
|
+
amountUsdc: r.amount_usdc != null ? Number(r.amount_usdc) : undefined,
|
|
1912
|
+
consumed: ps.kind === 'down_adjust'
|
|
1913
|
+
? Number(r.tot_to_burn) - Number(r.tot_burned)
|
|
1914
|
+
: Number(r.tot_from_escrow),
|
|
1864
1915
|
}))
|
|
1865
|
-
.filter((
|
|
1916
|
+
.filter((c) => {
|
|
1917
|
+
var _a, _b;
|
|
1918
|
+
return c.kind === 'down_adjust'
|
|
1919
|
+
? c.consumed < ((_a = c.amountRender) !== null && _a !== void 0 ? _a : 0)
|
|
1920
|
+
: c.consumed < ((_b = c.amountUsdc) !== null && _b !== void 0 ? _b : 0);
|
|
1921
|
+
});
|
|
1866
1922
|
});
|
|
1867
1923
|
},
|
|
1868
1924
|
insertPolygonUpgrade(db, p, log = logger_1.consoleLogger) {
|