@render-foundation/utils 0.0.233-beta.0 → 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/burn/burnCalculations.js +140 -0
- package/lib/cjs/burn/burnCalculations.js.map +1 -0
- package/lib/cjs/client/pg/v2Client.js +47 -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/esm/src/burn/burnCalculations.js +124 -0
- package/lib/esm/src/burn/burnCalculations.js.map +1 -0
- package/lib/esm/src/client/pg/v2Client.js +43 -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/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/src/burn/burnCalculations.d.ts +52 -0
- package/lib/types/src/burn/burnCalculations.d.ts.map +1 -0
- package/lib/types/src/client/pg/v2Client.d.ts +19 -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/dbTypesV2.d.ts +10 -16
- package/lib/types/src/dbTypesV2.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.calculateBurnAmounts = exports.batchFinishedAtMedian = void 0;
|
|
15
|
+
const spl_utils_1 = require("@render-foundation/spl-utils");
|
|
16
|
+
const logger_1 = require("../logger");
|
|
17
|
+
// Utility function copied from cron-server
|
|
18
|
+
const batchFinishedAtMedian = (jobs) => {
|
|
19
|
+
const pricedAt = jobs.length % 2 == 0
|
|
20
|
+
? new Date(Math.trunc((Date.parse(jobs[jobs.length / 2].finishedAt) +
|
|
21
|
+
Date.parse(jobs[jobs.length / 2 - 1].finishedAt)) /
|
|
22
|
+
2))
|
|
23
|
+
: new Date(Date.parse(jobs[Math.floor(jobs.length / 2)].finishedAt));
|
|
24
|
+
return pricedAt;
|
|
25
|
+
};
|
|
26
|
+
exports.batchFinishedAtMedian = batchFinishedAtMedian;
|
|
27
|
+
/**
|
|
28
|
+
* Pure function that calculates burn amounts without side effects
|
|
29
|
+
* This is the core logic extracted from consumeIncrBuyAndBurnRenderPG
|
|
30
|
+
*/
|
|
31
|
+
const calculateBurnAmounts = (input, opts = {
|
|
32
|
+
perpetual: true,
|
|
33
|
+
}, log = logger_1.consoleLogger) => {
|
|
34
|
+
const { jobs, userGrantSpends, burnAdjustments, params, decimals, priceAtDate, eurToUSDC, } = input;
|
|
35
|
+
// Apply grant/buy split logic to jobs
|
|
36
|
+
const processedJobs = jobs.map((j) => {
|
|
37
|
+
const { buyRenderAmt, grantRenderAmt } = j, rest = __rest(j, ["buyRenderAmt", "grantRenderAmt"]);
|
|
38
|
+
return rest;
|
|
39
|
+
}); // Deep copy to avoid mutations
|
|
40
|
+
for (const j of processedJobs) {
|
|
41
|
+
const userGrantSpend = userGrantSpends[j.userId];
|
|
42
|
+
if (userGrantSpend) {
|
|
43
|
+
if (opts.perpetual ||
|
|
44
|
+
userGrantSpend.userGrantSpend.perpetual ||
|
|
45
|
+
j.rndrUsed <= userGrantSpend.currAllot) {
|
|
46
|
+
j.grantRenderAmt = Number(j.rndrUsed);
|
|
47
|
+
log.debug(`job ${j.id} grant split: ${j.rndrUsed}`);
|
|
48
|
+
userGrantSpend.currAllot -= BigInt(j.rndrUsed);
|
|
49
|
+
}
|
|
50
|
+
else if (userGrantSpend.currAllot <= BigInt(0)) {
|
|
51
|
+
log.debug(`job ${j.id} buy split: ${j.rndrUsed}`);
|
|
52
|
+
j.buyRenderAmt = Number(j.rndrUsed);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
log.debug(`job ${j.id} buy + grant split: ${j.rndrUsed} grant: ${Number(userGrantSpend.currAllot)} buy: ${Number(j.rndrUsed) - Number(userGrantSpend.currAllot)}`);
|
|
56
|
+
j.grantRenderAmt = Number(userGrantSpend.currAllot);
|
|
57
|
+
j.buyRenderAmt = Number(j.rndrUsed) - Number(userGrantSpend.currAllot);
|
|
58
|
+
userGrantSpend.currAllot = BigInt(0);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
log.debug(`job ${j.id} no grant split: ${j.rndrUsed}`);
|
|
63
|
+
j.buyRenderAmt = Number(j.rndrUsed);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const totalRenderUsed = processedJobs.reduce((tot, job) => tot + Number(job.rndrUsed.toString()), 0);
|
|
67
|
+
log.debug(`jobs buy + grant split: ${JSON.stringify(processedJobs)}`);
|
|
68
|
+
// Check minimum threshold
|
|
69
|
+
if (totalRenderUsed < (0, spl_utils_1.toBN)(params.minTotalRndrThreshold, decimals).toNumber()) {
|
|
70
|
+
throw new Error(`totalRenderUsed ${totalRenderUsed / Math.pow(10, decimals)} < total threshold ${params.minTotalRndrThreshold}`);
|
|
71
|
+
}
|
|
72
|
+
const totalRenderBuyAndBurn = processedJobs.reduce((tot, job) => { var _a; return tot + Number((_a = job.buyRenderAmt) !== null && _a !== void 0 ? _a : 0); }, 0);
|
|
73
|
+
const totalRender = processedJobs.reduce((tot, job) => tot + Number(job.rndrUsed.toString()), 0);
|
|
74
|
+
log.debug(`Burn calculation inputs:`);
|
|
75
|
+
log.debug(` totalRenderUsed: ${totalRenderUsed}`);
|
|
76
|
+
log.debug(` totalRenderBuyAndBurn: ${totalRenderBuyAndBurn}`);
|
|
77
|
+
log.debug(` totalRender: ${totalRender}`);
|
|
78
|
+
log.debug(` priceAtDate: ${priceAtDate}`);
|
|
79
|
+
log.debug(` eurToUSDC: ${eurToUSDC}`);
|
|
80
|
+
const pricedAt = (0, exports.batchFinishedAtMedian)(processedJobs.map((job) => ({
|
|
81
|
+
finishedAt: job.completedAt.toISOString(),
|
|
82
|
+
jobId: job.id,
|
|
83
|
+
rndrUsed: Number(job.rndrUsed.toString()),
|
|
84
|
+
})));
|
|
85
|
+
// Core burn calculation logic
|
|
86
|
+
const toBurn = Math.trunc((totalRender / priceAtDate / 4) * 0.95 * eurToUSDC);
|
|
87
|
+
let renderToBuyAndBurn = Math.trunc((totalRenderBuyAndBurn / totalRender) * toBurn);
|
|
88
|
+
const origRenderToBuyAndBurn = renderToBuyAndBurn;
|
|
89
|
+
log.debug(`Core burn calculation:`);
|
|
90
|
+
log.debug(` toBurn: ${toBurn}`);
|
|
91
|
+
log.debug(` renderToBuyAndBurn: ${renderToBuyAndBurn}`);
|
|
92
|
+
log.debug(` origRenderToBuyAndBurn: ${origRenderToBuyAndBurn}`);
|
|
93
|
+
let burnAdjustmentId = undefined;
|
|
94
|
+
// Apply burn adjustments
|
|
95
|
+
if (burnAdjustments.length > 0) {
|
|
96
|
+
const adjustment = burnAdjustments[0];
|
|
97
|
+
burnAdjustmentId = adjustment.id;
|
|
98
|
+
const adjLeft = adjustment.downAdjToBurn - adjustment.adjusted;
|
|
99
|
+
const minBurnRndrThreshold = (0, spl_utils_1.toBN)(params.minBurnRndrThreshold, decimals).toNumber();
|
|
100
|
+
const available = renderToBuyAndBurn - minBurnRndrThreshold;
|
|
101
|
+
if (adjLeft > available) {
|
|
102
|
+
renderToBuyAndBurn = minBurnRndrThreshold;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
renderToBuyAndBurn -= adjLeft;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// Validate burn thresholds
|
|
109
|
+
const toBuyAndBurnUI = renderToBuyAndBurn / Math.pow(10, decimals);
|
|
110
|
+
if (renderToBuyAndBurn != 0) {
|
|
111
|
+
if (toBuyAndBurnUI > params.maxBurnRndrThreshold) {
|
|
112
|
+
throw new Error(`buy and burn RENDER ${toBuyAndBurnUI} too high`);
|
|
113
|
+
}
|
|
114
|
+
if (toBuyAndBurnUI < params.minBurnRndrThreshold) {
|
|
115
|
+
throw new Error(`buy and burn RENDER ${toBuyAndBurnUI} too low < ${params.minBurnRndrThreshold}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const renderToBurnFromEmissions = toBurn - origRenderToBuyAndBurn;
|
|
119
|
+
const totalToBurn = renderToBuyAndBurn + renderToBurnFromEmissions;
|
|
120
|
+
log.debug(`Final burn calculation results:`);
|
|
121
|
+
log.debug(` renderToBuyAndBurn: ${renderToBuyAndBurn}`);
|
|
122
|
+
log.debug(` renderToBurnFromEmissions: ${renderToBurnFromEmissions}`);
|
|
123
|
+
log.debug(` totalToBurn: ${totalToBurn}`);
|
|
124
|
+
log.debug(` toBurn: ${toBurn}`);
|
|
125
|
+
return {
|
|
126
|
+
totalRenderUsed,
|
|
127
|
+
totalRenderBuyAndBurn,
|
|
128
|
+
totalRender,
|
|
129
|
+
renderToBuyAndBurn,
|
|
130
|
+
renderToBurnFromEmissions,
|
|
131
|
+
totalToBurn,
|
|
132
|
+
toBurn,
|
|
133
|
+
origRenderToBuyAndBurn,
|
|
134
|
+
burnAdjustmentId,
|
|
135
|
+
pricedAt,
|
|
136
|
+
processedJobs,
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
exports.calculateBurnAmounts = calculateBurnAmounts;
|
|
140
|
+
//# sourceMappingURL=burnCalculations.js.map
|
|
@@ -0,0 +1 @@
|
|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B,WAAW,EAAE,CAAC,CAAA;IAC1C,GAAG,CAAC,KAAK,CAAC,aAAa,MAAM,EAAE,CAAC,CAAA;IAEhC,OAAO;QACL,eAAe;QACf,qBAAqB;QACrB,WAAW;QACX,kBAAkB;QAClB,yBAAyB;QACzB,WAAW;QACX,MAAM;QACN,sBAAsB;QACtB,gBAAgB;QAChB,QAAQ;QACR,aAAa;KACd,CAAA;AACH,CAAC,CAAA;AAxKY,QAAA,oBAAoB,wBAwKhC"}
|
|
@@ -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.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.
|
|
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,8 +54,7 @@ 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';
|
|
@@ -1125,7 +1124,7 @@ const pgClient = (config) => {
|
|
|
1125
1124
|
let buyBurnId;
|
|
1126
1125
|
let grantBurnId;
|
|
1127
1126
|
if (p.buyBurn) {
|
|
1128
|
-
const { burned, usdcSpent, eurToUsdc, renderToUsdc, quoteAmt, tags,
|
|
1127
|
+
const { burned, usdcSpent, eurToUsdc, renderToUsdc, quoteAmt, tags, downCorrectionId, escrowCorrectionId, toBurn, fromEscrowUsdc, } = p.buyBurn;
|
|
1129
1128
|
let pricedAt = p.buyBurn.pricedAt;
|
|
1130
1129
|
if (!pricedAt) {
|
|
1131
1130
|
const jobs = yield fetchJobs1(trx.trx, { ids: p.jobs.map((j) => String(j.id)) }, log);
|
|
@@ -1144,8 +1143,8 @@ const pgClient = (config) => {
|
|
|
1144
1143
|
quote_amt: quoteAmt,
|
|
1145
1144
|
burned: burned,
|
|
1146
1145
|
tags: tags.join(','),
|
|
1147
|
-
|
|
1148
|
-
|
|
1146
|
+
down_correction_id: downCorrectionId,
|
|
1147
|
+
escrow_correction_id: escrowCorrectionId,
|
|
1149
1148
|
to_burn: toBurn,
|
|
1150
1149
|
from_escrow_usdc: fromEscrowUsdc !== null && fromEscrowUsdc !== void 0 ? fromEscrowUsdc : 0,
|
|
1151
1150
|
};
|
|
@@ -1847,90 +1846,79 @@ const pgClient = (config) => {
|
|
|
1847
1846
|
}));
|
|
1848
1847
|
});
|
|
1849
1848
|
},
|
|
1850
|
-
|
|
1849
|
+
createBurnCorrection(trx, p, log = logger_1.consoleLogger) {
|
|
1851
1850
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1852
|
-
//
|
|
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.
|
|
1853
1859
|
const row = yield trx.trx
|
|
1854
|
-
.insertInto(exports.
|
|
1860
|
+
.insertInto(exports.BURN_CORRECTION_TABLE)
|
|
1855
1861
|
.values({
|
|
1862
|
+
kind: p.kind,
|
|
1863
|
+
job_id: p.jobId,
|
|
1856
1864
|
sol_tx_id: p.solTxId,
|
|
1857
|
-
|
|
1865
|
+
amount_render: p.amountRender,
|
|
1866
|
+
amount_usdc: p.amountUsdc,
|
|
1858
1867
|
description: p.description,
|
|
1859
1868
|
})
|
|
1860
|
-
.onConflict((oc) => oc.
|
|
1869
|
+
.onConflict((oc) => oc.doNothing())
|
|
1861
1870
|
.returning('id')
|
|
1862
1871
|
.executeTakeFirst();
|
|
1863
1872
|
if (!row) {
|
|
1864
|
-
log.info(`
|
|
1873
|
+
log.info(`burn_correction ${p.kind} for ${p.jobId != null ? `job ${p.jobId}` : `sol_tx ${p.solTxId}`} already exists — skipped`);
|
|
1865
1874
|
return undefined;
|
|
1866
1875
|
}
|
|
1867
|
-
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`}`);
|
|
1868
1877
|
return Number(row.id);
|
|
1869
1878
|
});
|
|
1870
1879
|
},
|
|
1871
|
-
|
|
1880
|
+
getBurnCorrections(trx, ps, log = logger_1.consoleLogger) {
|
|
1872
1881
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1873
1882
|
if (!ps.toFill)
|
|
1874
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';
|
|
1875
1887
|
const q = trx.trx
|
|
1876
|
-
.selectFrom(exports.
|
|
1877
|
-
.selectAll(exports.
|
|
1878
|
-
.
|
|
1879
|
-
.select([`${exports.JOB_TABLE}.completed_at`, `${exports.JOB_TABLE}.render_amt`])
|
|
1880
|
-
.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`)
|
|
1881
1891
|
.select(({ fn, lit }) => [
|
|
1882
1892
|
fn.sum(fn.coalesce(`${exports.BURN_TABLE}.to_burn`, lit(0))).as('tot_to_burn'),
|
|
1883
1893
|
fn.sum(fn.coalesce(`${exports.BURN_TABLE}.burned`, lit(0))).as('tot_burned'),
|
|
1884
|
-
])
|
|
1885
|
-
.groupBy([
|
|
1886
|
-
`${exports.JOB_BURN_ADJUSTMENT_TABLE}.id`,
|
|
1887
|
-
`${exports.JOB_TABLE}.completed_at`,
|
|
1888
|
-
`${exports.JOB_TABLE}.render_amt`,
|
|
1889
|
-
]);
|
|
1890
|
-
log.info(`getJobBurnAdjustments ${q.compile().sql}`);
|
|
1891
|
-
const res = yield q.execute();
|
|
1892
|
-
return res
|
|
1893
|
-
.map((r) => ({
|
|
1894
|
-
id: Number(r.id),
|
|
1895
|
-
createdAt: r.created_at,
|
|
1896
|
-
jobId: Number(r.job_id),
|
|
1897
|
-
downAdjRndrUsed: Number(r.down_adj_rndr_used),
|
|
1898
|
-
downAdjToBurn: Number(r.down_adj_to_burn),
|
|
1899
|
-
adjusted: Number(r.tot_to_burn) - Number(r.tot_burned),
|
|
1900
|
-
job: {
|
|
1901
|
-
id: Number(r.job_id),
|
|
1902
|
-
completedAt: r.completed_at,
|
|
1903
|
-
rndrUsed: BigInt(r.render_amt),
|
|
1904
|
-
},
|
|
1905
|
-
}))
|
|
1906
|
-
.filter((a) => a.adjusted < a.downAdjToBurn);
|
|
1907
|
-
});
|
|
1908
|
-
},
|
|
1909
|
-
getTxBurnAdjustments(trx, ps, log = logger_1.consoleLogger) {
|
|
1910
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1911
|
-
if (!ps.toFill)
|
|
1912
|
-
return [];
|
|
1913
|
-
const q = trx.trx
|
|
1914
|
-
.selectFrom(exports.TX_BURN_ADJUSTMENT_TABLE)
|
|
1915
|
-
.selectAll(exports.TX_BURN_ADJUSTMENT_TABLE)
|
|
1916
|
-
.leftJoin(exports.BURN_TABLE, `${exports.BURN_TABLE}.tx_burn_adjustment_id`, `${exports.TX_BURN_ADJUSTMENT_TABLE}.id`)
|
|
1917
|
-
.select(({ fn, lit }) => [
|
|
1918
1894
|
fn
|
|
1919
1895
|
.sum(fn.coalesce(`${exports.BURN_TABLE}.from_escrow_usdc`, lit(0)))
|
|
1920
1896
|
.as('tot_from_escrow'),
|
|
1921
1897
|
])
|
|
1922
|
-
.
|
|
1923
|
-
|
|
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}`);
|
|
1924
1902
|
const res = yield q.execute();
|
|
1925
1903
|
return res
|
|
1926
1904
|
.map((r) => ({
|
|
1927
1905
|
id: Number(r.id),
|
|
1928
1906
|
createdAt: r.created_at,
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
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),
|
|
1932
1915
|
}))
|
|
1933
|
-
.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
|
+
});
|
|
1934
1922
|
});
|
|
1935
1923
|
},
|
|
1936
1924
|
insertPolygonUpgrade(db, p, log = logger_1.consoleLogger) {
|