@render-foundation/utils 0.0.49 → 0.0.51
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/onchain.js +109 -0
- package/lib/cjs/client/onchain.js.map +1 -0
- package/lib/cjs/{pgClient.js → client/pg.js} +144 -102
- package/lib/cjs/client/pg.js.map +1 -0
- package/lib/cjs/{priceClient.js → client/price.js} +3 -3
- package/lib/cjs/client/price.js.map +1 -0
- package/lib/cjs/{renderClient.js → client/render.js} +23 -23
- package/lib/cjs/client/render.js.map +1 -0
- package/lib/cjs/{squads.js → client/squads.js} +64 -34
- package/lib/cjs/client/squads.js.map +1 -0
- package/lib/cjs/{tradeClient.js → client/trade.js} +16 -16
- package/lib/cjs/client/trade.js.map +1 -0
- package/lib/cjs/{wormhole.js → client/wormhole.js} +8 -7
- package/lib/cjs/client/wormhole.js.map +1 -0
- package/lib/cjs/errorReporter.js +11 -11
- package/lib/cjs/errorReporter.js.map +1 -1
- package/lib/cjs/index.js +7 -6
- package/lib/cjs/index.js.map +1 -1
- package/lib/esm/src/client/onchain.js +100 -0
- package/lib/esm/src/client/onchain.js.map +1 -0
- package/lib/esm/src/{pgClient.js → client/pg.js} +149 -107
- package/lib/esm/src/client/pg.js.map +1 -0
- package/lib/esm/src/{priceClient.js → client/price.js} +6 -6
- package/lib/esm/src/client/price.js.map +1 -0
- package/lib/esm/src/{renderClient.js → client/render.js} +23 -23
- package/lib/esm/src/client/render.js.map +1 -0
- package/lib/esm/src/client/squads.js +152 -0
- package/lib/esm/src/client/squads.js.map +1 -0
- package/lib/esm/src/{tradeClient.js → client/trade.js} +17 -17
- package/lib/esm/src/client/trade.js.map +1 -0
- package/lib/esm/src/{wormhole.js → client/wormhole.js} +9 -8
- package/lib/esm/src/client/wormhole.js.map +1 -0
- package/lib/esm/src/errorReporter.js +14 -14
- package/lib/esm/src/errorReporter.js.map +1 -1
- package/lib/esm/src/index.js +9 -8
- package/lib/esm/src/index.js.map +1 -1
- package/lib/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/src/client/onchain.d.ts +19 -0
- package/lib/types/src/client/onchain.d.ts.map +1 -0
- package/lib/types/src/{pgClient.d.ts → client/pg.d.ts} +13 -11
- package/lib/types/src/client/pg.d.ts.map +1 -0
- package/lib/types/src/{priceClient.d.ts → client/price.d.ts} +3 -3
- package/lib/types/src/client/price.d.ts.map +1 -0
- package/lib/types/src/{renderClient.d.ts → client/render.d.ts} +4 -4
- package/lib/types/src/client/render.d.ts.map +1 -0
- package/lib/types/src/{squads.d.ts → client/squads.d.ts} +6 -5
- package/lib/types/src/client/squads.d.ts.map +1 -0
- package/lib/types/src/{tradeClient.d.ts → client/trade.d.ts} +5 -5
- package/lib/types/src/client/trade.d.ts.map +1 -0
- package/lib/types/src/client/wormhole.d.ts +9 -0
- package/lib/types/src/client/wormhole.d.ts.map +1 -0
- package/lib/types/src/errorReporter.d.ts +1 -1
- package/lib/types/src/errorReporter.d.ts.map +1 -1
- package/lib/types/src/index.d.ts +9 -8
- package/lib/types/src/index.d.ts.map +1 -1
- package/package.json +5 -3
- package/lib/cjs/pgClient.js.map +0 -1
- package/lib/cjs/priceClient.js.map +0 -1
- package/lib/cjs/renderClient.js.map +0 -1
- package/lib/cjs/squads.js.map +0 -1
- package/lib/cjs/tradeClient.js.map +0 -1
- package/lib/cjs/wormhole.js.map +0 -1
- package/lib/esm/src/pgClient.js.map +0 -1
- package/lib/esm/src/priceClient.js.map +0 -1
- package/lib/esm/src/renderClient.js.map +0 -1
- package/lib/esm/src/squads.js +0 -122
- package/lib/esm/src/squads.js.map +0 -1
- package/lib/esm/src/tradeClient.js.map +0 -1
- package/lib/esm/src/wormhole.js.map +0 -1
- package/lib/types/src/pgClient.d.ts.map +0 -1
- package/lib/types/src/priceClient.d.ts.map +0 -1
- package/lib/types/src/renderClient.d.ts.map +0 -1
- package/lib/types/src/squads.d.ts.map +0 -1
- package/lib/types/src/tradeClient.d.ts.map +0 -1
- package/lib/types/src/wormhole.d.ts +0 -9
- package/lib/types/src/wormhole.d.ts.map +0 -1
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { fetchEthInfo } from './wormhole';
|
|
2
|
+
import { consoleLogger } from '../logger';
|
|
3
|
+
export const fetchTransfers = async (bridgeProgram,
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
erc20Contract, ethTokenBridge, fromBlock, f, log = consoleLogger) => {
|
|
6
|
+
const { ethKey, solKey } = f;
|
|
7
|
+
const transfers = [];
|
|
8
|
+
if (ethKey) {
|
|
9
|
+
const events = await erc20Contract.getPastEvents('Transfer', {
|
|
10
|
+
filter: {
|
|
11
|
+
from: ethKey,
|
|
12
|
+
to: ethTokenBridge.toLowerCase(),
|
|
13
|
+
},
|
|
14
|
+
fromBlock,
|
|
15
|
+
}, (err, events) => {
|
|
16
|
+
throw err;
|
|
17
|
+
});
|
|
18
|
+
const accts = await bridgeProgram.account.transferV2.all([
|
|
19
|
+
{
|
|
20
|
+
memcmp: {
|
|
21
|
+
offset: 8,
|
|
22
|
+
bytes: solKey,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
]);
|
|
26
|
+
log.info(`ethKey ${ethKey} -> ${events.length} eth burns`);
|
|
27
|
+
log.info(`solKey ${solKey} -> ${accts.length} sol transfer pdas`);
|
|
28
|
+
const fetches = [];
|
|
29
|
+
const acctsBySequence = {};
|
|
30
|
+
for (const acct of accts) {
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
log.info(`transfer ${acct.publicKey.toString()} pda ${JSON.stringify({
|
|
33
|
+
...acct.account,
|
|
34
|
+
createdAt: acct.account.createdAt,
|
|
35
|
+
incentiveEpoch: acct.account.epochId,
|
|
36
|
+
})}`);
|
|
37
|
+
// @ts-ignore
|
|
38
|
+
if (!acct.account.sequence) {
|
|
39
|
+
// legacy transfers
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
const seq = acct.account.sequence.toNumber();
|
|
44
|
+
fetches.push(fetchEthInfo(seq, ethTokenBridge));
|
|
45
|
+
acctsBySequence[seq] = acct;
|
|
46
|
+
}
|
|
47
|
+
const reses = await Promise.all(fetches);
|
|
48
|
+
const transferPDAByEthTx = {};
|
|
49
|
+
for (const [seq, { originTx: { txHash }, },] of reses) {
|
|
50
|
+
transferPDAByEthTx[txHash] = acctsBySequence[seq];
|
|
51
|
+
}
|
|
52
|
+
for (const event of events.reverse()) {
|
|
53
|
+
const pda = transferPDAByEthTx[event.transactionHash];
|
|
54
|
+
console.log(`ethTx ${event.transactionHash} -> ${JSON.stringify(pda)}`);
|
|
55
|
+
transfers.push({
|
|
56
|
+
ethTx: event.transactionHash,
|
|
57
|
+
blockNo: event.blockNumber,
|
|
58
|
+
pda: !pda
|
|
59
|
+
? undefined
|
|
60
|
+
: {
|
|
61
|
+
key: pda.publicKey.toString(),
|
|
62
|
+
createdAt: pda.account.createdAt.toNumber(),
|
|
63
|
+
amount: pda.account.amount.toNumber(),
|
|
64
|
+
incentiveRound: pda.account.epochId
|
|
65
|
+
? pda.account.epochId.toNumber()
|
|
66
|
+
: undefined,
|
|
67
|
+
points: pda.account.points
|
|
68
|
+
? pda.account.points.toString()
|
|
69
|
+
: undefined,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const accts = await bridgeProgram.account.transferV2.all([
|
|
76
|
+
{
|
|
77
|
+
memcmp: {
|
|
78
|
+
offset: 8,
|
|
79
|
+
bytes: solKey,
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
]);
|
|
83
|
+
for (const { account, publicKey } of accts) {
|
|
84
|
+
transfers.push({
|
|
85
|
+
pda: {
|
|
86
|
+
key: publicKey.toString(),
|
|
87
|
+
createdAt: account.createdAt.toNumber(),
|
|
88
|
+
amount: account.amount.toNumber(),
|
|
89
|
+
incentiveRound: account.epochId
|
|
90
|
+
? account.epochId.toNumber()
|
|
91
|
+
: undefined,
|
|
92
|
+
points: account.points ? account.points.toString() : undefined,
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
log.debug(`transfers ${JSON.stringify(transfers)}`);
|
|
98
|
+
return transfers;
|
|
99
|
+
};
|
|
100
|
+
//# sourceMappingURL=onchain.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"onchain.js","sourceRoot":"","sources":["../../../../src/client/onchain.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAW,MAAM,YAAY,CAAA;AAClD,OAAO,EAAE,aAAa,EAAgB,MAAM,WAAW,CAAA;AAiBvD,MAAM,CAAC,MAAM,cAAc,GAAG,KAAK,EACjC,aAA8B;AAC9B,aAAa;AACb,aAAuB,EACvB,cAAsB,EACtB,SAAiB,EACjB,CAAsC,EACtC,MAAoB,aAAa,EACL,EAAE;IAC9B,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,CAAA;IAE5B,MAAM,SAAS,GAAU,EAAE,CAAA;IAE3B,IAAI,MAAM,EAAE;QACV,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,aAAa,CAC9C,UAAU,EACV;YACE,MAAM,EAAE;gBACN,IAAI,EAAE,MAAgB;gBACtB,EAAE,EAAE,cAAc,CAAC,WAAW,EAAE;aACjC;YACD,SAAS;SACV,EACD,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE;YACd,MAAM,GAAG,CAAA;QACX,CAAC,CACF,CAAA;QAED,MAAM,KAAK,GAAG,MAAM,aAAa,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC;YACvD;gBACE,MAAM,EAAE;oBACN,MAAM,EAAE,CAAC;oBACT,KAAK,EAAE,MAAgB;iBACxB;aACF;SACF,CAAC,CAAA;QAEF,GAAG,CAAC,IAAI,CAAC,UAAU,MAAM,OAAO,MAAM,CAAC,MAAM,YAAY,CAAC,CAAA;QAC1D,GAAG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EAAE;oBACjC,cAAc,EAAE,OAAO,CAAC,OAAO;wBAC7B,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE;wBAC5B,CAAC,CAAC,SAAS;oBACb,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,SAAS;iBAC/D;aACF,CAAC,CAAA;SACH;KACF;IAED,GAAG,CAAC,KAAK,CAAC,aAAa,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;IACnD,OAAO,SAAS,CAAA;AAClB,CAAC,CAAA"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Pool } from
|
|
2
|
-
import { Kysely, NoResultError, PostgresDialect, sql } from
|
|
3
|
-
import { consoleLogger } from
|
|
1
|
+
import { Pool } from 'pg';
|
|
2
|
+
import { Kysely, NoResultError, PostgresDialect, sql, } from 'kysely';
|
|
3
|
+
import { consoleLogger } from '../logger';
|
|
4
4
|
const transferFromDB = (o) => {
|
|
5
5
|
return {
|
|
6
6
|
id: o.id,
|
|
@@ -12,20 +12,20 @@ const transferFromDB = (o) => {
|
|
|
12
12
|
payedAt: o.payed_at,
|
|
13
13
|
amountDue: BigInt(o.amount_due),
|
|
14
14
|
amountPayed: o.amount_payed ? BigInt(o.amount_payed) : undefined,
|
|
15
|
-
operatorRewards: o.transfer_type ==
|
|
15
|
+
operatorRewards: o.transfer_type == 'node_operator_rewards'
|
|
16
16
|
? {
|
|
17
17
|
epochId: o.epoch_id,
|
|
18
18
|
startTime: o.start_time,
|
|
19
19
|
endTime: o.end_time,
|
|
20
20
|
}
|
|
21
21
|
: undefined,
|
|
22
|
-
computeClientRewards: o.transfer_type ==
|
|
22
|
+
computeClientRewards: o.transfer_type == 'open_compute_rewards'
|
|
23
23
|
? {
|
|
24
24
|
epochId: o.epoch_id,
|
|
25
25
|
deviceId: o.device_id,
|
|
26
26
|
}
|
|
27
27
|
: undefined,
|
|
28
|
-
upgradeRewards: o.transfer_type ==
|
|
28
|
+
upgradeRewards: o.transfer_type == 'upgrade_rewards' && !!o.epoch_id
|
|
29
29
|
? {
|
|
30
30
|
epochId: o.epoch_id,
|
|
31
31
|
epochStartTime: o.epoch_start_time,
|
|
@@ -42,23 +42,26 @@ const txHandle = (ps, log = consoleLogger) => {
|
|
|
42
42
|
rollback,
|
|
43
43
|
async releaseLock(f) {
|
|
44
44
|
const now = new Date(Date.now());
|
|
45
|
-
const res = await trx
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
const res = await trx
|
|
46
|
+
.updateTable(LOCK_TABLE)
|
|
47
|
+
.set({
|
|
48
|
+
...(f.newCursor ? { cursor: f.newCursor } : {}),
|
|
49
|
+
released_at: now,
|
|
50
|
+
})
|
|
51
|
+
.where('description', '=', f.lock)
|
|
49
52
|
.executeTakeFirstOrThrow();
|
|
50
53
|
log.debug(`updated ${res.numUpdatedRows} lock`);
|
|
51
54
|
commit();
|
|
52
55
|
},
|
|
53
56
|
};
|
|
54
57
|
};
|
|
55
|
-
export const TRANSFER_TABLE =
|
|
56
|
-
export const NODE_OPERATOR_REWARDS_TRANSFER_TABLE =
|
|
57
|
-
export const OPEN_COMPUTE_REWARDS_TRANSFER_TABLE =
|
|
58
|
-
export const UPGRADE_REWARDS_TRANSFER_TABLE =
|
|
59
|
-
export const SQUADS_TX_TABLE =
|
|
60
|
-
export const NODE_OPERATOR_EPOCH_TABLE =
|
|
61
|
-
export const JOB_ID_TABLE =
|
|
58
|
+
export const TRANSFER_TABLE = 'transfer';
|
|
59
|
+
export const NODE_OPERATOR_REWARDS_TRANSFER_TABLE = 'node_operator_rewards_transfer';
|
|
60
|
+
export const OPEN_COMPUTE_REWARDS_TRANSFER_TABLE = 'open_compute_rewards_transfer';
|
|
61
|
+
export const UPGRADE_REWARDS_TRANSFER_TABLE = 'upgrade_rewards_transfer';
|
|
62
|
+
export const SQUADS_TX_TABLE = 'squads_tx';
|
|
63
|
+
export const NODE_OPERATOR_EPOCH_TABLE = 'node_operator_epoch';
|
|
64
|
+
export const JOB_ID_TABLE = 'job_id';
|
|
62
65
|
export const JOB_TABLE = 'job';
|
|
63
66
|
export const BURN_TABLE = 'burn';
|
|
64
67
|
export const LOCK_TABLE = 'lock';
|
|
@@ -90,20 +93,20 @@ export const pgClient = (config) => {
|
|
|
90
93
|
sol_tx: t.solTx,
|
|
91
94
|
transfer_type: (() => {
|
|
92
95
|
if (t.operatorRewards) {
|
|
93
|
-
return
|
|
96
|
+
return 'node_operator_rewards';
|
|
94
97
|
}
|
|
95
98
|
else if (t.upgradeRewards) {
|
|
96
|
-
return
|
|
99
|
+
return 'upgrade_rewards';
|
|
97
100
|
}
|
|
98
101
|
else if (t.computeClientRewards) {
|
|
99
|
-
return
|
|
102
|
+
return 'open_compute_rewards';
|
|
100
103
|
}
|
|
101
|
-
throw new Error(
|
|
104
|
+
throw new Error('wtf');
|
|
102
105
|
})(),
|
|
103
106
|
})
|
|
104
|
-
.returning((eb) => [
|
|
107
|
+
.returning((eb) => ['id as id'])
|
|
105
108
|
.executeTakeFirst());
|
|
106
|
-
log.info(`inserted transfer ${JSON.stringify(t, (key, value) => typeof value ===
|
|
109
|
+
log.info(`inserted transfer ${JSON.stringify(t, (key, value) => typeof value === 'bigint' ? value.toString() : value)}`);
|
|
107
110
|
if (t.operatorRewards) {
|
|
108
111
|
log.info(`inserting operator rewards ${transferId}`);
|
|
109
112
|
const x = (await db
|
|
@@ -145,24 +148,30 @@ export const pgClient = (config) => {
|
|
|
145
148
|
return {
|
|
146
149
|
db,
|
|
147
150
|
async getLock(f) {
|
|
148
|
-
let res = await db
|
|
151
|
+
let res = await db
|
|
152
|
+
.selectFrom(LOCK_TABLE)
|
|
153
|
+
.selectAll()
|
|
154
|
+
.where('description', '=', f.lock)
|
|
155
|
+
.executeTakeFirstOrThrow();
|
|
149
156
|
let r = res[0];
|
|
150
157
|
return {
|
|
151
158
|
cursor: r.cursor,
|
|
152
159
|
acquiredAt: r.acquired_at,
|
|
153
|
-
releasedAt: r.released_at
|
|
160
|
+
releasedAt: r.released_at,
|
|
154
161
|
};
|
|
155
162
|
},
|
|
156
163
|
async acquireLock({ lock = 'incremental_burn' }, log = consoleLogger) {
|
|
157
164
|
const { trx, commit, rollback } = await begin(db);
|
|
158
165
|
const res = await sql `select txid_current()`.execute(trx);
|
|
159
|
-
let r = await trx
|
|
166
|
+
let r = await trx
|
|
167
|
+
.selectFrom(LOCK_TABLE)
|
|
160
168
|
.selectAll()
|
|
161
169
|
.where('description', '=', lock)
|
|
162
170
|
.forUpdate()
|
|
163
171
|
.executeTakeFirstOrThrow();
|
|
164
172
|
const acquiredAt = Date.now();
|
|
165
|
-
await trx
|
|
173
|
+
await trx
|
|
174
|
+
.updateTable(LOCK_TABLE)
|
|
166
175
|
.set({ acquired_at: new Date(acquiredAt) })
|
|
167
176
|
.where('id', '=', r.id)
|
|
168
177
|
.executeTakeFirstOrThrow();
|
|
@@ -178,9 +187,9 @@ export const pgClient = (config) => {
|
|
|
178
187
|
.set({
|
|
179
188
|
burned,
|
|
180
189
|
description,
|
|
181
|
-
burn_tx: burnTx
|
|
190
|
+
burn_tx: burnTx,
|
|
182
191
|
})
|
|
183
|
-
.where(
|
|
192
|
+
.where('id', '=', epochId.toString())
|
|
184
193
|
.executeTakeFirstOrThrow();
|
|
185
194
|
log.info(`saved node operator epoch: ${res.numUpdatedRows}`);
|
|
186
195
|
},
|
|
@@ -205,7 +214,7 @@ export const pgClient = (config) => {
|
|
|
205
214
|
saved_at: now,
|
|
206
215
|
total_rndr: totalRndr,
|
|
207
216
|
})
|
|
208
|
-
.where(
|
|
217
|
+
.where('id', '=', epochId.toString())
|
|
209
218
|
.executeTakeFirstOrThrow();
|
|
210
219
|
log.info(`saved node operator epoch: ${res.numUpdatedRows}`);
|
|
211
220
|
});
|
|
@@ -217,8 +226,9 @@ export const pgClient = (config) => {
|
|
|
217
226
|
const res = await db
|
|
218
227
|
.selectFrom(NODE_OPERATOR_EPOCH_TABLE)
|
|
219
228
|
.selectAll()
|
|
220
|
-
.where(
|
|
221
|
-
.
|
|
229
|
+
.where('end_time', '<=', now)
|
|
230
|
+
.where('saved_at', 'is', null)
|
|
231
|
+
.orderBy('end_time asc')
|
|
222
232
|
.limit(1)
|
|
223
233
|
.executeTakeFirstOrThrow();
|
|
224
234
|
return {
|
|
@@ -236,21 +246,32 @@ export const pgClient = (config) => {
|
|
|
236
246
|
throw e;
|
|
237
247
|
}
|
|
238
248
|
},
|
|
239
|
-
async getOutstanding(f) {
|
|
249
|
+
async getOutstanding(f, log = consoleLogger) {
|
|
250
|
+
const { typ, nullKey, due } = f;
|
|
240
251
|
//select sol_key, sum(amount_due)-sum(amount_payed) from transfer group by sol_key;
|
|
241
|
-
|
|
252
|
+
let q = db
|
|
242
253
|
.selectFrom(TRANSFER_TABLE)
|
|
243
254
|
.select(({ fn, val, ref }) => [
|
|
244
|
-
|
|
245
|
-
sql `${fn.sum(
|
|
246
|
-
${fn.sum(
|
|
255
|
+
'sol_key',
|
|
256
|
+
sql `${fn.sum('amount_due')} -
|
|
257
|
+
${fn.sum('amount_payed')}`.as('due'),
|
|
247
258
|
])
|
|
248
|
-
.where(
|
|
249
|
-
.groupBy([
|
|
250
|
-
|
|
259
|
+
.where('transfer_type', '=', typ)
|
|
260
|
+
.groupBy(['sol_key']);
|
|
261
|
+
if (!nullKey) {
|
|
262
|
+
q = q.where('sol_key', 'is not', null);
|
|
263
|
+
}
|
|
264
|
+
if (due) {
|
|
265
|
+
q = q.having(({ fn, ref }) => sql `${fn.sum('amount_due')} -
|
|
266
|
+
${fn.sum('amount_payed')}
|
|
267
|
+
>
|
|
268
|
+
0`);
|
|
269
|
+
}
|
|
270
|
+
const res = await q.execute();
|
|
271
|
+
log.info(`outstanding query ${q.compile().sql} params ${JSON.stringify(f)} ${res.length} rows`);
|
|
251
272
|
const out = {};
|
|
252
273
|
for (const r of res) {
|
|
253
|
-
out[r.sol_key ??
|
|
274
|
+
out[r.sol_key ?? ''] = BigInt(r.due);
|
|
254
275
|
}
|
|
255
276
|
return out;
|
|
256
277
|
},
|
|
@@ -258,20 +279,20 @@ export const pgClient = (config) => {
|
|
|
258
279
|
const res = await db
|
|
259
280
|
.updateTable(SQUADS_TX_TABLE)
|
|
260
281
|
.set({ tx_status: status })
|
|
261
|
-
.where(
|
|
282
|
+
.where('sq_tx', '=', tx)
|
|
262
283
|
.execute();
|
|
263
284
|
log.info(`updated ${tx} -> ${status}`);
|
|
264
285
|
},
|
|
265
286
|
async setSqTx(transferIds, sqTx, log = consoleLogger) {
|
|
266
287
|
const { id } = (await db
|
|
267
288
|
.insertInto(SQUADS_TX_TABLE)
|
|
268
|
-
.values({ sq_tx: sqTx, tx_status:
|
|
269
|
-
.returning((eb) => [
|
|
289
|
+
.values({ sq_tx: sqTx, tx_status: 'created' })
|
|
290
|
+
.returning((eb) => ['id as id'])
|
|
270
291
|
.executeTakeFirst());
|
|
271
292
|
const res = await db
|
|
272
293
|
.updateTable(TRANSFER_TABLE)
|
|
273
294
|
.set({ sq_tx_id: id })
|
|
274
|
-
.where(
|
|
295
|
+
.where('id', 'in', transferIds.map((id) => id.toString()))
|
|
275
296
|
.execute();
|
|
276
297
|
log.info(`set sqTx ${sqTx} on ${transferIds.length} transfers`);
|
|
277
298
|
},
|
|
@@ -280,46 +301,54 @@ export const pgClient = (config) => {
|
|
|
280
301
|
.selectFrom(TRANSFER_TABLE)
|
|
281
302
|
// @ts-ignore
|
|
282
303
|
.selectAll(TRANSFER_TABLE)
|
|
283
|
-
.leftJoin(SQUADS_TX_TABLE,
|
|
284
|
-
.select([
|
|
285
|
-
.where(
|
|
304
|
+
.leftJoin(SQUADS_TX_TABLE, 'squads_tx.id', 'transfer.sq_tx_id')
|
|
305
|
+
.select(['squads_tx.tx_status', 'squads_tx.sq_tx'])
|
|
306
|
+
.where('transfer.transfer_type', '=', f.typ);
|
|
286
307
|
if (f.status) {
|
|
287
|
-
if (f.status ==
|
|
288
|
-
base = base.where(
|
|
308
|
+
if (f.status == 'notx') {
|
|
309
|
+
base = base.where('squads_tx.tx_status', 'is', null);
|
|
289
310
|
}
|
|
290
311
|
else {
|
|
291
|
-
base = base.where(
|
|
312
|
+
base = base.where('squads_tx.tx_status', '=', f.status);
|
|
292
313
|
}
|
|
293
314
|
}
|
|
294
315
|
if (f.payed != undefined) {
|
|
295
|
-
base = base.where(
|
|
316
|
+
base = base.where('transfer.payed_at', f.payed ? 'is not' : 'is', null);
|
|
296
317
|
}
|
|
297
318
|
if (f.createdBefore != undefined) {
|
|
298
|
-
base = base.where(
|
|
319
|
+
base = base.where('transfer.created_at', '<=', f.createdBefore);
|
|
299
320
|
}
|
|
300
|
-
if (f.typ ==
|
|
301
|
-
base = base
|
|
321
|
+
if (f.typ == 'upgrade_rewards') {
|
|
322
|
+
base = base
|
|
323
|
+
.innerJoin(UPGRADE_REWARDS_TRANSFER_TABLE, 'upgrade_rewards_transfer.transfer_id', 'transfer.id')
|
|
324
|
+
.selectAll(UPGRADE_REWARDS_TRANSFER_TABLE);
|
|
302
325
|
if (f.epochId) {
|
|
303
326
|
// @ts-ignore
|
|
304
|
-
base = base.where(
|
|
327
|
+
base = base.where('upgrade_rewards_transfer.epoch_id', '=', f.epochId);
|
|
305
328
|
}
|
|
306
329
|
}
|
|
307
|
-
if (f.typ ==
|
|
308
|
-
base = base
|
|
330
|
+
if (f.typ == 'open_compute_rewards') {
|
|
331
|
+
base = base
|
|
332
|
+
.innerJoin(OPEN_COMPUTE_REWARDS_TRANSFER_TABLE, 'open_compute_rewards_transfer.transfer_id', 'transfer.id')
|
|
333
|
+
.selectAll(OPEN_COMPUTE_REWARDS_TRANSFER_TABLE);
|
|
309
334
|
if (f.epochId) {
|
|
335
|
+
base = base.where(
|
|
310
336
|
// @ts-ignore
|
|
311
|
-
|
|
337
|
+
'open_compute_rewards_transfer.epoch_id', '=', f.epochId);
|
|
312
338
|
}
|
|
313
339
|
}
|
|
314
|
-
if (f.typ ==
|
|
315
|
-
base = base
|
|
340
|
+
if (f.typ == 'node_operator_rewards') {
|
|
341
|
+
base = base
|
|
342
|
+
.innerJoin(NODE_OPERATOR_REWARDS_TRANSFER_TABLE, 'node_operator_rewards_transfer.transfer_id', 'transfer.id')
|
|
343
|
+
.selectAll(NODE_OPERATOR_REWARDS_TRANSFER_TABLE);
|
|
316
344
|
if (f.epochId) {
|
|
345
|
+
base = base.where(
|
|
317
346
|
// @ts-ignore
|
|
318
|
-
|
|
347
|
+
'node_operator_rewards_transfer.epoch_id', '=', f.epochId);
|
|
319
348
|
}
|
|
320
349
|
}
|
|
321
350
|
if (f.sort) {
|
|
322
|
-
base = base.orderBy(
|
|
351
|
+
base = base.orderBy('transfer.sol_key asc');
|
|
323
352
|
}
|
|
324
353
|
const res = await base.execute();
|
|
325
354
|
log.info(`fetched ${base.compile().sql} params ${JSON.stringify(f)} ${res.length} transfers`);
|
|
@@ -328,8 +357,8 @@ export const pgClient = (config) => {
|
|
|
328
357
|
async payoutTransfersSquads(ts, payedAt, solTx, sqTx, log = consoleLogger) {
|
|
329
358
|
const { id: sqTxId } = (await db
|
|
330
359
|
.insertInto(SQUADS_TX_TABLE)
|
|
331
|
-
.values({ sq_tx: sqTx, tx_status:
|
|
332
|
-
.returning((eb) => [
|
|
360
|
+
.values({ sq_tx: sqTx, tx_status: 'executed' })
|
|
361
|
+
.returning((eb) => ['id as id'])
|
|
333
362
|
.executeTakeFirstOrThrow());
|
|
334
363
|
let numUpdated = BigInt(0);
|
|
335
364
|
for (const [id, amtPayed] of Object.entries(ts)) {
|
|
@@ -341,7 +370,7 @@ export const pgClient = (config) => {
|
|
|
341
370
|
amount_payed: amtPayed,
|
|
342
371
|
sq_tx_id: sqTxId,
|
|
343
372
|
})
|
|
344
|
-
.where(
|
|
373
|
+
.where('id', '=', id)
|
|
345
374
|
.executeTakeFirstOrThrow();
|
|
346
375
|
numUpdated += res.numUpdatedRows;
|
|
347
376
|
}
|
|
@@ -350,7 +379,9 @@ export const pgClient = (config) => {
|
|
|
350
379
|
},
|
|
351
380
|
async payoutTransfers(idToAmt, payedAt, solTx, log = consoleLogger) {
|
|
352
381
|
const res = await db.transaction().execute(async (tx) => {
|
|
353
|
-
const alreadyUpdatedIds = (await tx
|
|
382
|
+
const alreadyUpdatedIds = (await tx
|
|
383
|
+
.selectFrom(TRANSFER_TABLE)
|
|
384
|
+
.select(['id'])
|
|
354
385
|
.where('id', 'in', Object.keys(idToAmt))
|
|
355
386
|
.where('sol_tx', 'is not', null)
|
|
356
387
|
.execute()).map(({ id }) => id);
|
|
@@ -366,7 +397,7 @@ export const pgClient = (config) => {
|
|
|
366
397
|
sol_tx: solTx,
|
|
367
398
|
amount_payed: amt,
|
|
368
399
|
})
|
|
369
|
-
.where(
|
|
400
|
+
.where('id', '=', id.toString())
|
|
370
401
|
.executeTakeFirstOrThrow();
|
|
371
402
|
log.info(`marked ${res.numUpdatedRows} transfer payed out`);
|
|
372
403
|
}
|
|
@@ -380,7 +411,7 @@ export const pgClient = (config) => {
|
|
|
380
411
|
sol_tx: solTx,
|
|
381
412
|
amount_payed: amtPayed,
|
|
382
413
|
})
|
|
383
|
-
.where(
|
|
414
|
+
.where('id', '=', id.toString())
|
|
384
415
|
.executeTakeFirst();
|
|
385
416
|
log.info(`marked ${res.numUpdatedRows} transfer payed out`);
|
|
386
417
|
return;
|
|
@@ -388,9 +419,9 @@ export const pgClient = (config) => {
|
|
|
388
419
|
async payoutTransferSq(id, payedAt, solTx, amtPayed, sqTx, log = consoleLogger) {
|
|
389
420
|
const { id: sqTxId } = (await db
|
|
390
421
|
.insertInto(SQUADS_TX_TABLE)
|
|
391
|
-
.values({ sq_tx: sqTx, tx_status:
|
|
392
|
-
.onConflict((oc) => oc.column(
|
|
393
|
-
.returning((eb) => [
|
|
422
|
+
.values({ sq_tx: sqTx, tx_status: 'executed' })
|
|
423
|
+
.onConflict((oc) => oc.column('sq_tx').doNothing())
|
|
424
|
+
.returning((eb) => ['id as id'])
|
|
394
425
|
.executeTakeFirst());
|
|
395
426
|
const res = await db
|
|
396
427
|
.updateTable(TRANSFER_TABLE)
|
|
@@ -400,7 +431,7 @@ export const pgClient = (config) => {
|
|
|
400
431
|
amount_payed: amtPayed,
|
|
401
432
|
sq_tx_id: sqTxId,
|
|
402
433
|
})
|
|
403
|
-
.where(
|
|
434
|
+
.where('id', '=', id.toString())
|
|
404
435
|
.executeTakeFirst();
|
|
405
436
|
log.info(`marked ${res.numUpdatedRows} transfer payed out`);
|
|
406
437
|
return;
|
|
@@ -411,15 +442,15 @@ export const pgClient = (config) => {
|
|
|
411
442
|
async fetchInfoForEpoch(epochId) {
|
|
412
443
|
const res = await db
|
|
413
444
|
.selectFrom(JOB_ID_TABLE)
|
|
414
|
-
.innerJoin(NODE_OPERATOR_EPOCH_TABLE,
|
|
415
|
-
.where(
|
|
445
|
+
.innerJoin(NODE_OPERATOR_EPOCH_TABLE, 'node_operator_epoch.id', 'job_id.epoch_id')
|
|
446
|
+
.where('job_id.epoch_id', '=', epochId.toString())
|
|
416
447
|
.select([
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
448
|
+
'job_id.id',
|
|
449
|
+
'node_operator_epoch.total_rndr',
|
|
450
|
+
'node_operator_epoch.start_time',
|
|
451
|
+
'node_operator_epoch.end_time',
|
|
452
|
+
'node_operator_epoch.burned',
|
|
453
|
+
'node_operator_epoch.burn_tx',
|
|
423
454
|
])
|
|
424
455
|
.execute();
|
|
425
456
|
if (res.length == 0) {
|
|
@@ -431,38 +462,43 @@ export const pgClient = (config) => {
|
|
|
431
462
|
startTime: res[0].start_time,
|
|
432
463
|
endTime: res[0].end_time,
|
|
433
464
|
burned: BigInt(res[0].burned),
|
|
434
|
-
burnTx: res[0].burn_tx
|
|
465
|
+
burnTx: res[0].burn_tx,
|
|
435
466
|
};
|
|
436
467
|
},
|
|
437
468
|
async fetchTransfersByWallet(f) {
|
|
438
469
|
let base = db
|
|
439
470
|
.selectFrom(TRANSFER_TABLE)
|
|
440
471
|
.selectAll(TRANSFER_TABLE)
|
|
441
|
-
.where(
|
|
472
|
+
.where('sol_key', '=', f.address);
|
|
442
473
|
if (f.payed != undefined) {
|
|
443
|
-
base = base.where(
|
|
474
|
+
base = base.where('payed_at', f.payed ? 'is not' : 'is', null);
|
|
444
475
|
}
|
|
445
476
|
const res = await base.execute();
|
|
446
|
-
if (f.typ ===
|
|
447
|
-
base = base.innerJoin(NODE_OPERATOR_REWARDS_TRANSFER_TABLE,
|
|
477
|
+
if (f.typ === 'node_operator_rewards') {
|
|
478
|
+
base = base.innerJoin(NODE_OPERATOR_REWARDS_TRANSFER_TABLE, 'node_operator_rewards_transfer.transfer_id', 'transfer.id');
|
|
448
479
|
if (f.epochId) {
|
|
480
|
+
base = base.where(
|
|
449
481
|
// @ts-ignore
|
|
450
|
-
|
|
482
|
+
'node_operator_rewards_transfer.epoch_id', '=', BigInt(f.epochId));
|
|
451
483
|
}
|
|
452
484
|
}
|
|
453
|
-
if (f.typ ===
|
|
454
|
-
base = base.innerJoin(OPEN_COMPUTE_REWARDS_TRANSFER_TABLE,
|
|
485
|
+
if (f.typ === 'open_compute_rewards') {
|
|
486
|
+
base = base.innerJoin(OPEN_COMPUTE_REWARDS_TRANSFER_TABLE, 'open_compute_rewards_transfer.transfer_id', 'transfer.id');
|
|
455
487
|
if (f.epochId) {
|
|
488
|
+
base = base.where(
|
|
456
489
|
// @ts-ignore
|
|
457
|
-
|
|
490
|
+
'open_compute_rewards_transfer.epoch_id', '=', BigInt(f.epochId));
|
|
458
491
|
}
|
|
459
492
|
}
|
|
460
493
|
return res.map((r) => transferFromDB(r));
|
|
461
494
|
},
|
|
462
495
|
async updateComputeClientSolKey(deviceId, solKey, log = consoleLogger) {
|
|
463
|
-
const q = db
|
|
464
|
-
|
|
465
|
-
|
|
496
|
+
const q = db
|
|
497
|
+
.updateTable(`${TRANSFER_TABLE} as t`)
|
|
498
|
+
.set({
|
|
499
|
+
sol_key: solKey,
|
|
500
|
+
})
|
|
501
|
+
.from(`${OPEN_COMPUTE_REWARDS_TRANSFER_TABLE} as o`)
|
|
466
502
|
.whereRef('t.id', '=', 'o.transfer_id')
|
|
467
503
|
.where('t.sol_key', 'is', null)
|
|
468
504
|
.where('o.device_id', '=', deviceId);
|
|
@@ -473,7 +509,8 @@ export const pgClient = (config) => {
|
|
|
473
509
|
},
|
|
474
510
|
async insertBurnedJobs(p, log = consoleLogger) {
|
|
475
511
|
await db.transaction().execute(async (trx) => {
|
|
476
|
-
const { id } = await trx
|
|
512
|
+
const { id } = await trx
|
|
513
|
+
.insertInto(BURN_TABLE)
|
|
477
514
|
.values({
|
|
478
515
|
sol_tx: p.solTx,
|
|
479
516
|
executed_at: p.executedAt,
|
|
@@ -482,42 +519,47 @@ export const pgClient = (config) => {
|
|
|
482
519
|
quote_amt: p.quoteAmt,
|
|
483
520
|
burned: p.burned,
|
|
484
521
|
})
|
|
485
|
-
.onConflict((oc) => oc.column(
|
|
486
|
-
.returning((eb) => [
|
|
522
|
+
.onConflict((oc) => oc.column('sol_tx').doNothing())
|
|
523
|
+
.returning((eb) => ['id as id'])
|
|
487
524
|
.executeTakeFirstOrThrow();
|
|
488
525
|
let num = BigInt(0);
|
|
489
526
|
for (const { amount, completedAt, jobId } of p.jobs) {
|
|
490
|
-
const res = await trx
|
|
527
|
+
const res = await trx
|
|
528
|
+
.insertInto(JOB_TABLE)
|
|
491
529
|
.values({
|
|
492
530
|
id: jobId,
|
|
493
531
|
burn_id: id,
|
|
494
532
|
render_amt: amount,
|
|
495
|
-
completed_at: completedAt
|
|
533
|
+
completed_at: completedAt,
|
|
496
534
|
})
|
|
497
|
-
.onConflict((oc) => oc.column(
|
|
535
|
+
.onConflict((oc) => oc.column('id').doNothing())
|
|
498
536
|
.executeTakeFirstOrThrow();
|
|
499
|
-
num +=
|
|
537
|
+
num += res.numInsertedOrUpdatedRows ?? BigInt(0);
|
|
500
538
|
}
|
|
501
539
|
log.debug(`inserted ${num} of ${p.jobs.length} burn jobs`);
|
|
502
540
|
});
|
|
503
541
|
},
|
|
504
542
|
async markJobsBurned(p, log = consoleLogger) {
|
|
505
543
|
let { markedAt, solTx } = p;
|
|
506
|
-
let res = await db
|
|
544
|
+
let res = await db
|
|
545
|
+
.updateTable(BURN_TABLE)
|
|
507
546
|
.set({ marked_burned_at: markedAt })
|
|
508
|
-
.where('sol_tx', '=', solTx)
|
|
547
|
+
.where('sol_tx', '=', solTx)
|
|
548
|
+
.executeTakeFirstOrThrow();
|
|
509
549
|
log.debug(`set ${res.numUpdatedRows} ${solTx} -> marked_burned_at ${markedAt.toISOString()}`);
|
|
510
|
-
}
|
|
550
|
+
},
|
|
511
551
|
};
|
|
512
552
|
};
|
|
513
553
|
async function begin(db) {
|
|
514
554
|
const connection = new Deferred();
|
|
515
555
|
const result = new Deferred();
|
|
516
556
|
// Do NOT await this line.
|
|
517
|
-
db.transaction()
|
|
557
|
+
db.transaction()
|
|
558
|
+
.execute((trx) => {
|
|
518
559
|
connection.resolve(trx);
|
|
519
560
|
return result.promise;
|
|
520
|
-
})
|
|
561
|
+
})
|
|
562
|
+
.catch((err) => {
|
|
521
563
|
// Don't do anything here. Just swallow the exception.
|
|
522
564
|
});
|
|
523
565
|
const trx = await connection.promise;
|
|
@@ -528,7 +570,7 @@ async function begin(db) {
|
|
|
528
570
|
},
|
|
529
571
|
rollback() {
|
|
530
572
|
result.reject(new Error('rollback'));
|
|
531
|
-
}
|
|
573
|
+
},
|
|
532
574
|
};
|
|
533
575
|
}
|
|
534
576
|
export class Deferred {
|
|
@@ -555,4 +597,4 @@ export class Deferred {
|
|
|
555
597
|
}
|
|
556
598
|
};
|
|
557
599
|
}
|
|
558
|
-
//# sourceMappingURL=
|
|
600
|
+
//# sourceMappingURL=pg.js.map
|