@render-foundation/utils 0.0.200 → 0.0.231
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/burnAnalysis.js +101 -0
- package/lib/cjs/burn/burnAnalysis.js.map +1 -0
- package/lib/cjs/burn/burnCalculations.js +140 -0
- package/lib/cjs/burn/burnCalculations.js.map +1 -0
- package/lib/cjs/client/pg/v2Client.js +222 -106
- package/lib/cjs/client/pg/v2Client.js.map +1 -1
- package/lib/cjs/index.js +2 -5
- package/lib/cjs/index.js.map +1 -1
- package/lib/esm/src/burn/burnAnalysis.js +90 -0
- package/lib/esm/src/burn/burnAnalysis.js.map +1 -0
- 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 +214 -95
- package/lib/esm/src/client/pg/v2Client.js.map +1 -1
- package/lib/esm/src/index.js +2 -5
- package/lib/esm/src/index.js.map +1 -1
- package/lib/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/src/burn/burnAnalysis.d.ts +51 -0
- package/lib/types/src/burn/burnAnalysis.d.ts.map +1 -0
- 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 +41 -27
- package/lib/types/src/client/pg/v2Client.d.ts.map +1 -1
- package/lib/types/src/dbTypesV2.d.ts +40 -15
- 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/client/dispersed.js +0 -123
- package/lib/cjs/client/dispersed.js.map +0 -1
- package/lib/esm/src/client/dispersed.js +0 -108
- package/lib/esm/src/client/dispersed.js.map +0 -1
- package/lib/types/src/client/dispersed.d.ts +0 -91
- package/lib/types/src/client/dispersed.d.ts.map +0 -1
|
@@ -9,19 +9,21 @@ export const MANUAL_BURN_TABLE = 'manual_burn';
|
|
|
9
9
|
export const ENTITY_TABLE = 'entity';
|
|
10
10
|
export const SOL_TRANSFER_TABLE = 'sol_transfer1';
|
|
11
11
|
export const LIABILITY_TABLE = 'liability';
|
|
12
|
+
export const LIABILITY_ADJUSTMENT_TABLE = 'liability_adjustment';
|
|
13
|
+
export const LIABILITY_ADJUSTMENT_BATCH_TABLE = 'liability_adjustment_batch';
|
|
12
14
|
export const JOB_ID_TABLE = 'job_id';
|
|
13
15
|
export const JOB_TABLE = 'job';
|
|
14
16
|
export const BURN_TABLE = 'burn';
|
|
15
17
|
export const BRIDGE_TRANSFER_TABLE = 'bridge_transfer';
|
|
16
18
|
export const NETWORK_REVENUE_TABLE = 'network_revenue';
|
|
17
|
-
export const
|
|
19
|
+
export const JOB_BURN_ADJUSTMENT_TABLE = 'job_burn_adjustment';
|
|
20
|
+
export const TX_BURN_ADJUSTMENT_TABLE = 'tx_burn_adjustment';
|
|
18
21
|
export const ENTITY_EPOCH_INFO_TABLE = 'entity_epoch_info';
|
|
19
22
|
export const POLYGON_UPGRADE_TABLE = 'polygon_upgrade';
|
|
20
23
|
export const CURRENT_USER_GRANT_SPEND_TABLE = 'current_user_grant_spend';
|
|
21
24
|
export const USER_GRANT_SPEND_TABLE = 'user_grant_spend';
|
|
22
25
|
export const GRANT_BURN_ID_TABLE = 'grant_burn_id';
|
|
23
26
|
export const GRANT_BURN_TABLE = 'grant_burn';
|
|
24
|
-
export const DISPERSED_SETTLEMENT_TABLE = 'dispersed_settlement';
|
|
25
27
|
BigInt.prototype.toJSON = function () {
|
|
26
28
|
return this.toString();
|
|
27
29
|
};
|
|
@@ -313,7 +315,11 @@ export const pgClient = (config) => {
|
|
|
313
315
|
sig: solTx,
|
|
314
316
|
executedAt,
|
|
315
317
|
}, log);
|
|
316
|
-
|
|
318
|
+
// sol_transfer1 already has UNIQUE(sol_tx_id, from_entity_id,
|
|
319
|
+
// to_entity_id, symbol). Without ON CONFLICT a retry of the record step
|
|
320
|
+
// throws a unique violation and wedges the BullMQ job; make it an
|
|
321
|
+
// idempotent no-op and re-fetch the existing id instead.
|
|
322
|
+
let transferRow = await db
|
|
317
323
|
.insertInto(SOL_TRANSFER_TABLE)
|
|
318
324
|
.values({
|
|
319
325
|
id: dbId,
|
|
@@ -324,9 +330,23 @@ export const pgClient = (config) => {
|
|
|
324
330
|
symbol,
|
|
325
331
|
amount_payed: amountPayed,
|
|
326
332
|
})
|
|
333
|
+
.onConflict((oc) => oc
|
|
334
|
+
.columns(['sol_tx_id', 'from_entity_id', 'to_entity_id', 'symbol'])
|
|
335
|
+
.doNothing())
|
|
327
336
|
.returning((eb) => ['id as id'])
|
|
328
|
-
.
|
|
329
|
-
|
|
337
|
+
.executeTakeFirst();
|
|
338
|
+
if (!transferRow) {
|
|
339
|
+
log.warn(`sol_transfer for sol_tx_id ${solTxId} ${fromEntityId}->${toEntityId} ${symbol} already exists; reusing existing id (idempotent retry)`);
|
|
340
|
+
transferRow = await db
|
|
341
|
+
.selectFrom(SOL_TRANSFER_TABLE)
|
|
342
|
+
.select((eb) => ['id as id'])
|
|
343
|
+
.where('sol_tx_id', '=', solTxId)
|
|
344
|
+
.where('from_entity_id', '=', fromEntityId)
|
|
345
|
+
.where('to_entity_id', '=', toEntityId)
|
|
346
|
+
.where('symbol', '=', symbol)
|
|
347
|
+
.executeTakeFirstOrThrow();
|
|
348
|
+
}
|
|
349
|
+
transferId = BigInt(transferRow.id);
|
|
330
350
|
}
|
|
331
351
|
ids.push({ transferId, liabilityId });
|
|
332
352
|
}
|
|
@@ -633,9 +653,36 @@ export const pgClient = (config) => {
|
|
|
633
653
|
}
|
|
634
654
|
return b.as('x');
|
|
635
655
|
}, (join) => join.onRef('y.sol_key', '=', 'x.sol_key'))
|
|
656
|
+
// Standing per-wallet/channel corrections (migration 36). NO epoch filter — an adjustment is
|
|
657
|
+
// a permanent delta on outstanding, not tied to a payout window; payments net it via `payed`.
|
|
658
|
+
.fullJoin((eb) => {
|
|
659
|
+
let b = eb
|
|
660
|
+
.selectFrom(LIABILITY_ADJUSTMENT_TABLE)
|
|
661
|
+
.select(({ fn }) => fn.sum(`amount_adjustment`).as('adj'))
|
|
662
|
+
.innerJoin(ENTITY_TABLE, `${LIABILITY_ADJUSTMENT_TABLE}.entity_id`, 'entity.id')
|
|
663
|
+
.select(['entity.sol_key'])
|
|
664
|
+
.groupBy('entity.sol_key');
|
|
665
|
+
if (f.banned) {
|
|
666
|
+
b = b.where('entity.banned', '=', true);
|
|
667
|
+
}
|
|
668
|
+
else {
|
|
669
|
+
b = b.where('entity.banned', '=', false);
|
|
670
|
+
}
|
|
671
|
+
if (f.channels && f.channels.length > 0) {
|
|
672
|
+
b = b.where((eb) => eb.or(f.channels.map((c) => eb('liability_adjustment.channel', '=', c))));
|
|
673
|
+
}
|
|
674
|
+
else {
|
|
675
|
+
b = b.where((eb) => eb.or([
|
|
676
|
+
eb('liability_adjustment.channel', '=', 'node_operator'),
|
|
677
|
+
eb('liability_adjustment.channel', '=', 'availability'),
|
|
678
|
+
]));
|
|
679
|
+
}
|
|
680
|
+
return b.as('a');
|
|
681
|
+
}, (join) => join.on(sql `"a"."sol_key" = coalesce("y"."sol_key", "x"."sol_key")`))
|
|
636
682
|
.select(({ fn }) => [
|
|
637
|
-
fn.coalesce('y.sol_key', 'x.sol_key').as('sol_key'),
|
|
638
|
-
sql `${fn.coalesce(`x.due`, sql `0`)}
|
|
683
|
+
fn.coalesce('y.sol_key', 'x.sol_key', 'a.sol_key').as('sol_key'),
|
|
684
|
+
sql `${fn.coalesce(`x.due`, sql `0`)} +
|
|
685
|
+
${fn.coalesce(`a.adj`, sql `0`)} -
|
|
639
686
|
${fn.coalesce(`y.payed`, sql `0`)}`.as('out'),
|
|
640
687
|
])
|
|
641
688
|
.as('z'))
|
|
@@ -658,6 +705,56 @@ export const pgClient = (config) => {
|
|
|
658
705
|
}
|
|
659
706
|
return out;
|
|
660
707
|
},
|
|
708
|
+
async addLiabilityAdjustments(batch, rows, log = consoleLogger) {
|
|
709
|
+
return await db.transaction().execute(async (trx) => {
|
|
710
|
+
// Upsert the batch by its label (idempotent re-runs; label is the run identity).
|
|
711
|
+
const b = await trx
|
|
712
|
+
.insertInto(LIABILITY_ADJUSTMENT_BATCH_TABLE)
|
|
713
|
+
.values({
|
|
714
|
+
label: batch.label,
|
|
715
|
+
description: batch.description,
|
|
716
|
+
created_by: batch.createdBy,
|
|
717
|
+
})
|
|
718
|
+
.onConflict((oc) => oc.column('label').doUpdateSet({
|
|
719
|
+
description: batch.description,
|
|
720
|
+
created_by: batch.createdBy,
|
|
721
|
+
}))
|
|
722
|
+
.returning('id')
|
|
723
|
+
.executeTakeFirstOrThrow();
|
|
724
|
+
const batchId = BigInt(b.id);
|
|
725
|
+
let inserted = 0;
|
|
726
|
+
let skipped = 0;
|
|
727
|
+
let net = BigInt(0);
|
|
728
|
+
let positive = BigInt(0);
|
|
729
|
+
let negative = BigInt(0);
|
|
730
|
+
for (const r of rows) {
|
|
731
|
+
const entityId = await getOrInsertEntity(trx, { solKey: r.solKey }, log);
|
|
732
|
+
const res = await trx
|
|
733
|
+
.insertInto(LIABILITY_ADJUSTMENT_TABLE)
|
|
734
|
+
.values({
|
|
735
|
+
entity_id: entityId,
|
|
736
|
+
channel: r.channel,
|
|
737
|
+
amount_adjustment: r.amountAdjustment,
|
|
738
|
+
batch_id: batchId,
|
|
739
|
+
})
|
|
740
|
+
// unique (entity_id, channel, batch_id) → a re-run of the same batch is a no-op
|
|
741
|
+
.onConflict((oc) => oc.columns(['entity_id', 'channel', 'batch_id']).doNothing())
|
|
742
|
+
.executeTakeFirst();
|
|
743
|
+
if ((res.numInsertedOrUpdatedRows ?? BigInt(0)) > BigInt(0))
|
|
744
|
+
inserted++;
|
|
745
|
+
else
|
|
746
|
+
skipped++;
|
|
747
|
+
net += r.amountAdjustment;
|
|
748
|
+
if (r.amountAdjustment > BigInt(0))
|
|
749
|
+
positive += r.amountAdjustment;
|
|
750
|
+
else
|
|
751
|
+
negative += r.amountAdjustment;
|
|
752
|
+
}
|
|
753
|
+
log.info(`liability_adjustment batch '${batch.label}' (id ${batchId}): ` +
|
|
754
|
+
`inserted ${inserted}, skipped ${skipped}, net ${net} (+${positive} / ${negative})`);
|
|
755
|
+
return { batchId, inserted, skipped, net, positive, negative };
|
|
756
|
+
});
|
|
757
|
+
},
|
|
661
758
|
async fetchNodeOperatorEpoch(epochId, log = consoleLogger) {
|
|
662
759
|
const q = db
|
|
663
760
|
.selectFrom(JOB_ID_TABLE)
|
|
@@ -942,7 +1039,7 @@ export const pgClient = (config) => {
|
|
|
942
1039
|
let buyBurnId;
|
|
943
1040
|
let grantBurnId;
|
|
944
1041
|
if (p.buyBurn) {
|
|
945
|
-
const { burned, usdcSpent, eurToUsdc, renderToUsdc, quoteAmt, tags,
|
|
1042
|
+
const { burned, usdcSpent, eurToUsdc, renderToUsdc, quoteAmt, tags, jobBurnAdjustmentId, txBurnAdjustmentId, toBurn, fromEscrowCredit, } = p.buyBurn;
|
|
946
1043
|
let pricedAt = p.buyBurn.pricedAt;
|
|
947
1044
|
if (!pricedAt) {
|
|
948
1045
|
const jobs = await fetchJobs1(trx.trx, { ids: p.jobs.map((j) => String(j.id)) }, log);
|
|
@@ -961,17 +1058,30 @@ export const pgClient = (config) => {
|
|
|
961
1058
|
quote_amt: quoteAmt,
|
|
962
1059
|
burned: burned,
|
|
963
1060
|
tags: tags.join(','),
|
|
964
|
-
|
|
1061
|
+
job_burn_adjustment_id: jobBurnAdjustmentId,
|
|
1062
|
+
tx_burn_adjustment_id: txBurnAdjustmentId,
|
|
965
1063
|
to_burn: toBurn,
|
|
1064
|
+
from_escrow_credit: fromEscrowCredit ?? 0,
|
|
966
1065
|
};
|
|
967
1066
|
log.debug(`inserting burn ${JSON.stringify(v)}`);
|
|
968
|
-
|
|
1067
|
+
// Idempotent on sol_tx_id (uniq_burn_sol_tx_id, migration 27): a retry of
|
|
1068
|
+
// the record step for the same on-chain burn is a safe no-op. On conflict
|
|
1069
|
+
// executeTakeFirst() returns undefined, so re-fetch the existing id.
|
|
1070
|
+
let burnRow = await trx.trx
|
|
969
1071
|
.insertInto(BURN_TABLE)
|
|
970
1072
|
.values(v)
|
|
971
|
-
|
|
1073
|
+
.onConflict((oc) => oc.column('sol_tx_id').doNothing())
|
|
972
1074
|
.returning((eb) => ['id as id'])
|
|
973
|
-
.
|
|
974
|
-
|
|
1075
|
+
.executeTakeFirst();
|
|
1076
|
+
if (!burnRow) {
|
|
1077
|
+
log.warn(`burn for sol_tx_id ${solTxId} already exists; reusing existing id (idempotent retry)`);
|
|
1078
|
+
burnRow = await trx.trx
|
|
1079
|
+
.selectFrom(BURN_TABLE)
|
|
1080
|
+
.select((eb) => ['id as id'])
|
|
1081
|
+
.where('sol_tx_id', '=', solTxId)
|
|
1082
|
+
.executeTakeFirstOrThrow();
|
|
1083
|
+
}
|
|
1084
|
+
buyBurnId = Number(burnRow.id);
|
|
975
1085
|
}
|
|
976
1086
|
if (p.grantBurn) {
|
|
977
1087
|
const { burned, tags } = p.grantBurn;
|
|
@@ -981,12 +1091,22 @@ export const pgClient = (config) => {
|
|
|
981
1091
|
tags: tags?.join(','),
|
|
982
1092
|
};
|
|
983
1093
|
log.debug(`inserting grant burn ${JSON.stringify(v)}`);
|
|
984
|
-
|
|
1094
|
+
// Idempotent on sol_tx_id (uniq_grant_burn_sol_tx_id, migration 27).
|
|
1095
|
+
let grantBurnRow = await trx.trx
|
|
985
1096
|
.insertInto(GRANT_BURN_TABLE)
|
|
986
1097
|
.values(v)
|
|
1098
|
+
.onConflict((oc) => oc.column('sol_tx_id').doNothing())
|
|
987
1099
|
.returning((eb) => ['id as id'])
|
|
988
|
-
.
|
|
989
|
-
|
|
1100
|
+
.executeTakeFirst();
|
|
1101
|
+
if (!grantBurnRow) {
|
|
1102
|
+
log.warn(`grant_burn for sol_tx_id ${solTxId} already exists; reusing existing id (idempotent retry)`);
|
|
1103
|
+
grantBurnRow = await trx.trx
|
|
1104
|
+
.selectFrom(GRANT_BURN_TABLE)
|
|
1105
|
+
.select((eb) => ['id as id'])
|
|
1106
|
+
.where('sol_tx_id', '=', solTxId)
|
|
1107
|
+
.executeTakeFirstOrThrow();
|
|
1108
|
+
}
|
|
1109
|
+
grantBurnId = Number(grantBurnRow.id);
|
|
990
1110
|
}
|
|
991
1111
|
const burnId = buyBurnId;
|
|
992
1112
|
const grantId = grantBurnId ?? buyBurnId;
|
|
@@ -1477,47 +1597,6 @@ export const pgClient = (config) => {
|
|
|
1477
1597
|
log.debug(`burn id ${id} inserted ${num} of ${p.jobs.length} burn jobs`);
|
|
1478
1598
|
});
|
|
1479
1599
|
},
|
|
1480
|
-
async insertDispersedOutbox(p, log = consoleLogger) {
|
|
1481
|
-
const res = await db
|
|
1482
|
-
.insertInto(DISPERSED_SETTLEMENT_TABLE)
|
|
1483
|
-
.values({
|
|
1484
|
-
tx_signature: p.txSignature,
|
|
1485
|
-
claim_uuid: p.claimUuid,
|
|
1486
|
-
obligation_uuids: p.obligationUuids,
|
|
1487
|
-
settled_amount: p.settledAmount,
|
|
1488
|
-
burned_amount: p.burnedAmount,
|
|
1489
|
-
executed_at: p.executedAt,
|
|
1490
|
-
eur_render: p.eurRender ?? null,
|
|
1491
|
-
})
|
|
1492
|
-
.onConflict((oc) => oc.column('tx_signature').doNothing())
|
|
1493
|
-
.executeTakeFirst();
|
|
1494
|
-
log.debug(`dispersed outbox tx ${p.txSignature}: ${res.numInsertedOrUpdatedRows} row(s)`);
|
|
1495
|
-
},
|
|
1496
|
-
async markDispersedSettled(txSignature, p, log = consoleLogger) {
|
|
1497
|
-
await db
|
|
1498
|
-
.updateTable(DISPERSED_SETTLEMENT_TABLE)
|
|
1499
|
-
.set({ settled_at: new Date(), idempotent_replay: p.idempotentReplay })
|
|
1500
|
-
.where('tx_signature', '=', txSignature)
|
|
1501
|
-
.execute();
|
|
1502
|
-
log.debug(`dispersed settled tx ${txSignature}`);
|
|
1503
|
-
},
|
|
1504
|
-
async getUnsettledDispersed(log = consoleLogger) {
|
|
1505
|
-
const rows = await db
|
|
1506
|
-
.selectFrom(DISPERSED_SETTLEMENT_TABLE)
|
|
1507
|
-
.selectAll()
|
|
1508
|
-
.where('settled_at', 'is', null)
|
|
1509
|
-
.execute();
|
|
1510
|
-
log.debug(`dispersed outbox: ${rows.length} unsettled`);
|
|
1511
|
-
return rows.map((r) => ({
|
|
1512
|
-
txSignature: r.tx_signature,
|
|
1513
|
-
claimUuid: r.claim_uuid,
|
|
1514
|
-
obligationUuids: r.obligation_uuids,
|
|
1515
|
-
settledAmount: r.settled_amount,
|
|
1516
|
-
burnedAmount: r.burned_amount,
|
|
1517
|
-
executedAt: r.executed_at,
|
|
1518
|
-
eurRender: r.eur_render ?? undefined,
|
|
1519
|
-
}));
|
|
1520
|
-
},
|
|
1521
1600
|
async fetchJobs(db, f, log = consoleLogger) {
|
|
1522
1601
|
return await fetchJobs1(db.trx, f, log);
|
|
1523
1602
|
},
|
|
@@ -1581,45 +1660,85 @@ export const pgClient = (config) => {
|
|
|
1581
1660
|
: {},
|
|
1582
1661
|
}));
|
|
1583
1662
|
},
|
|
1584
|
-
async
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
])
|
|
1600
|
-
.groupBy([
|
|
1601
|
-
`${BURN_ADJUSTMENT_TABLE}.id`,
|
|
1602
|
-
`${JOB_TABLE}.completed_at`,
|
|
1603
|
-
`${JOB_TABLE}.render_amt`,
|
|
1604
|
-
])
|
|
1605
|
-
.havingRef(sql `SUM(COALESCE("burn"."to_burn", 0)) - SUM(COALESCE("burn"."burned", 0))`, '<', `${BURN_ADJUSTMENT_TABLE}.down_adj_to_burn`);
|
|
1606
|
-
log.info(`getBurnAdjustments ${q.compile().sql}`);
|
|
1607
|
-
const res = await q.execute();
|
|
1608
|
-
return res.map((r) => ({
|
|
1609
|
-
id: Number(r.id),
|
|
1610
|
-
createdAt: r.created_at,
|
|
1611
|
-
jobId: Number(r.job_id),
|
|
1612
|
-
downAdjRndrUsed: Number(r.down_adj_rndr_used),
|
|
1613
|
-
downAdjToBurn: Number(r.down_adj_to_burn),
|
|
1614
|
-
adjusted: Number(r.tot_to_burn) - Number(r.tot_burned),
|
|
1615
|
-
job: {
|
|
1616
|
-
id: Number(r.job_id),
|
|
1617
|
-
completedAt: r.completed_at,
|
|
1618
|
-
rndrUsed: BigInt(r.render_amt),
|
|
1619
|
-
},
|
|
1620
|
-
}));
|
|
1663
|
+
async createTxBurnAdjustment(trx, p, log = consoleLogger) {
|
|
1664
|
+
// Idempotent per sol_tx_id (unique) — a re-run for the same bad tx is a no-op.
|
|
1665
|
+
const row = await trx.trx
|
|
1666
|
+
.insertInto(TX_BURN_ADJUSTMENT_TABLE)
|
|
1667
|
+
.values({
|
|
1668
|
+
sol_tx_id: p.solTxId,
|
|
1669
|
+
surplus_render: p.surplusRender,
|
|
1670
|
+
description: p.description,
|
|
1671
|
+
})
|
|
1672
|
+
.onConflict((oc) => oc.column('sol_tx_id').doNothing())
|
|
1673
|
+
.returning('id')
|
|
1674
|
+
.executeTakeFirst();
|
|
1675
|
+
if (!row) {
|
|
1676
|
+
log.info(`tx_burn_adjustment for sol_tx ${p.solTxId} already exists — skipped`);
|
|
1677
|
+
return undefined;
|
|
1621
1678
|
}
|
|
1622
|
-
|
|
1679
|
+
log.info(`created tx_burn_adjustment ${row.id}: ${p.surplusRender} RENDER surplus (sol_tx ${p.solTxId})`);
|
|
1680
|
+
return Number(row.id);
|
|
1681
|
+
},
|
|
1682
|
+
async getJobBurnAdjustments(trx, ps, log = consoleLogger) {
|
|
1683
|
+
if (!ps.toFill)
|
|
1684
|
+
return [];
|
|
1685
|
+
const q = trx.trx
|
|
1686
|
+
.selectFrom(JOB_BURN_ADJUSTMENT_TABLE)
|
|
1687
|
+
.selectAll(JOB_BURN_ADJUSTMENT_TABLE)
|
|
1688
|
+
.innerJoin(JOB_TABLE, `${JOB_TABLE}.id`, `${JOB_BURN_ADJUSTMENT_TABLE}.job_id`)
|
|
1689
|
+
.select([`${JOB_TABLE}.completed_at`, `${JOB_TABLE}.render_amt`])
|
|
1690
|
+
.leftJoin(BURN_TABLE, `${BURN_TABLE}.job_burn_adjustment_id`, `${JOB_BURN_ADJUSTMENT_TABLE}.id`)
|
|
1691
|
+
.select(({ fn, lit }) => [
|
|
1692
|
+
fn.sum(fn.coalesce(`${BURN_TABLE}.to_burn`, lit(0))).as('tot_to_burn'),
|
|
1693
|
+
fn.sum(fn.coalesce(`${BURN_TABLE}.burned`, lit(0))).as('tot_burned'),
|
|
1694
|
+
])
|
|
1695
|
+
.groupBy([
|
|
1696
|
+
`${JOB_BURN_ADJUSTMENT_TABLE}.id`,
|
|
1697
|
+
`${JOB_TABLE}.completed_at`,
|
|
1698
|
+
`${JOB_TABLE}.render_amt`,
|
|
1699
|
+
]);
|
|
1700
|
+
log.info(`getJobBurnAdjustments ${q.compile().sql}`);
|
|
1701
|
+
const res = await q.execute();
|
|
1702
|
+
return res
|
|
1703
|
+
.map((r) => ({
|
|
1704
|
+
id: Number(r.id),
|
|
1705
|
+
createdAt: r.created_at,
|
|
1706
|
+
jobId: Number(r.job_id),
|
|
1707
|
+
downAdjRndrUsed: Number(r.down_adj_rndr_used),
|
|
1708
|
+
downAdjToBurn: Number(r.down_adj_to_burn),
|
|
1709
|
+
adjusted: Number(r.tot_to_burn) - Number(r.tot_burned),
|
|
1710
|
+
job: {
|
|
1711
|
+
id: Number(r.job_id),
|
|
1712
|
+
completedAt: r.completed_at,
|
|
1713
|
+
rndrUsed: BigInt(r.render_amt),
|
|
1714
|
+
},
|
|
1715
|
+
}))
|
|
1716
|
+
.filter((a) => a.adjusted < a.downAdjToBurn);
|
|
1717
|
+
},
|
|
1718
|
+
async getTxBurnAdjustments(trx, ps, log = consoleLogger) {
|
|
1719
|
+
if (!ps.toFill)
|
|
1720
|
+
return [];
|
|
1721
|
+
const q = trx.trx
|
|
1722
|
+
.selectFrom(TX_BURN_ADJUSTMENT_TABLE)
|
|
1723
|
+
.selectAll(TX_BURN_ADJUSTMENT_TABLE)
|
|
1724
|
+
.leftJoin(BURN_TABLE, `${BURN_TABLE}.tx_burn_adjustment_id`, `${TX_BURN_ADJUSTMENT_TABLE}.id`)
|
|
1725
|
+
.select(({ fn, lit }) => [
|
|
1726
|
+
fn
|
|
1727
|
+
.sum(fn.coalesce(`${BURN_TABLE}.from_escrow_credit`, lit(0)))
|
|
1728
|
+
.as('tot_from_escrow'),
|
|
1729
|
+
])
|
|
1730
|
+
.groupBy(`${TX_BURN_ADJUSTMENT_TABLE}.id`);
|
|
1731
|
+
log.info(`getTxBurnAdjustments ${q.compile().sql}`);
|
|
1732
|
+
const res = await q.execute();
|
|
1733
|
+
return res
|
|
1734
|
+
.map((r) => ({
|
|
1735
|
+
id: Number(r.id),
|
|
1736
|
+
createdAt: r.created_at,
|
|
1737
|
+
solTxId: Number(r.sol_tx_id),
|
|
1738
|
+
surplusRender: Number(r.surplus_render),
|
|
1739
|
+
consumed: Number(r.tot_from_escrow),
|
|
1740
|
+
}))
|
|
1741
|
+
.filter((a) => a.consumed < a.surplusRender);
|
|
1623
1742
|
},
|
|
1624
1743
|
async insertPolygonUpgrade(db, p, log = consoleLogger) {
|
|
1625
1744
|
const solTxId = await getOrInsertTx(db.trx, {
|