@xyo-network/xl1-protocol-sdk 1.5.35 → 1.5.37
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/dist/neutral/index.mjs +304 -124
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/types/ChainServiceCollection.d.ts +60 -0
- package/dist/types/ChainServiceCollection.d.ts.map +1 -0
- package/dist/types/ChainServiceCollectionV2.d.ts +60 -0
- package/dist/types/ChainServiceCollectionV2.d.ts.map +1 -0
- package/dist/types/SignedBigInt.d.ts +14 -0
- package/dist/types/SignedBigInt.d.ts.map +1 -0
- package/dist/types/block/hydrateBlock.d.ts +1 -1
- package/dist/types/block/hydrateBlock.d.ts.map +1 -1
- package/dist/types/block/primitives/balanceStepSummaryFromRange.d.ts +6 -0
- package/dist/types/block/primitives/balanceStepSummaryFromRange.d.ts.map +1 -0
- package/dist/types/block/primitives/balanceSummary.d.ts +4 -0
- package/dist/types/block/primitives/balanceSummary.d.ts.map +1 -0
- package/dist/types/block/primitives/frames/deepCalculateFramesFromRange.d.ts.map +1 -1
- package/dist/types/block/primitives/frames/index.d.ts +2 -1
- package/dist/types/block/primitives/frames/index.d.ts.map +1 -1
- package/dist/types/block/primitives/hashFromBlockNumber.d.ts +4 -0
- package/dist/types/block/primitives/hashFromBlockNumber.d.ts.map +1 -0
- package/dist/types/block/primitives/index.d.ts +5 -0
- package/dist/types/block/primitives/index.d.ts.map +1 -1
- package/dist/types/block/primitives/model.d.ts +16 -0
- package/dist/types/block/primitives/model.d.ts.map +1 -0
- package/dist/types/block/primitives/payloads.d.ts +21 -0
- package/dist/types/block/primitives/payloads.d.ts.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/payload/index.d.ts +2 -0
- package/dist/types/payload/index.d.ts.map +1 -0
- package/dist/types/payload/netBalancesForPayloads.d.ts +3 -0
- package/dist/types/payload/netBalancesForPayloads.d.ts.map +1 -0
- package/dist/types/transaction/buildTransaction.d.ts +1 -1
- package/dist/types/transaction/buildTransaction.d.ts.map +1 -1
- package/package.json +19 -19
- package/src/ChainServiceCollection.ts +65 -0
- package/src/ChainServiceCollectionV2.ts +65 -0
- package/src/SignedBigInt.ts +51 -0
- package/src/block/hydrateBlock.ts +14 -13
- package/src/block/primitives/balanceStepSummaryFromRange.ts +78 -0
- package/src/block/primitives/balanceSummary.ts +27 -0
- package/src/block/primitives/frames/calculateFramesFromRange.ts +2 -2
- package/src/block/primitives/frames/deepCalculateFramesFromRange.ts +2 -2
- package/src/block/primitives/frames/index.ts +2 -1
- package/src/block/primitives/hashFromBlockNumber.ts +31 -0
- package/src/block/primitives/index.ts +5 -0
- package/src/block/primitives/model.ts +18 -0
- package/src/block/primitives/payloads.ts +29 -0
- package/src/index.ts +4 -0
- package/src/payload/index.ts +1 -0
- package/src/payload/netBalancesForPayloads.ts +19 -0
- package/src/transaction/buildTransaction.ts +16 -9
- package/dist/types/block/primitives/frames/loadFrameSummaries.d.ts +0 -5
- package/dist/types/block/primitives/frames/loadFrameSummaries.d.ts.map +0 -1
- package/dist/types/block/primitives/frames/stepHashFramesFromBlock.d.ts +0 -5
- package/dist/types/block/primitives/frames/stepHashFramesFromBlock.d.ts.map +0 -1
- package/src/block/primitives/frames/loadFrameSummaries.ts +0 -12
- package/src/block/primitives/frames/stepHashFramesFromBlock.ts +0 -72
package/dist/neutral/index.mjs
CHANGED
|
@@ -10,20 +10,38 @@ function allHashesPresent(hashes, payloads) {
|
|
|
10
10
|
return hashes.every((hash) => payloadHashes.has(hash));
|
|
11
11
|
}
|
|
12
12
|
__name(allHashesPresent, "allHashesPresent");
|
|
13
|
-
var tryHydrateBlock = /* @__PURE__ */ __name(async (archivist, hash, maxDepth = 1
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
var tryHydrateBlock = /* @__PURE__ */ __name(async (archivist, hash, maxDepth = 1) => {
|
|
14
|
+
assertEx(maxDepth >= 0, () => "maxDepth must be greater than or equal to 0");
|
|
15
|
+
const bw = filterAs(await archivist.get([
|
|
16
|
+
hash
|
|
17
|
+
]), asBlockBoundWitnessWithStorageMeta).at(0);
|
|
18
|
+
if (!bw) return void 0;
|
|
19
|
+
if (maxDepth === 0) return [
|
|
20
|
+
bw,
|
|
21
|
+
[]
|
|
22
|
+
];
|
|
23
|
+
const blkPayloads = await archivist.get(bw.payload_hashes);
|
|
24
|
+
if (maxDepth === 1) return [
|
|
25
|
+
bw,
|
|
26
|
+
blkPayloads
|
|
27
|
+
];
|
|
28
|
+
const transactions = filterAs(blkPayloads, asTransactionBoundWitnessWithStorageMeta);
|
|
29
|
+
const transactionsPayloadHashes = transactions.flatMap((tx) => tx.payload_hashes);
|
|
30
|
+
const transactionsPayloads = await archivist.get(transactionsPayloadHashes);
|
|
31
|
+
const allPayloadsHashes = new Set([
|
|
32
|
+
...blkPayloads,
|
|
33
|
+
...transactionsPayloads
|
|
34
|
+
].flatMap((p) => p._hash));
|
|
35
|
+
const allPayloads = await archivist.get([
|
|
36
|
+
...allPayloadsHashes
|
|
37
|
+
]);
|
|
38
|
+
const allPayloadsFiltered = allPayloads.filter((p) => allPayloadsHashes.has(p._hash));
|
|
39
|
+
return [
|
|
40
|
+
bw,
|
|
41
|
+
allPayloadsFiltered
|
|
42
|
+
];
|
|
24
43
|
}, "tryHydrateBlock");
|
|
25
|
-
var hydrateBlock = /* @__PURE__ */ __name(async (archivist, hash, maxDepth = 1, minDepth) => {
|
|
26
|
-
if (minDepth === void 0) minDepth = maxDepth;
|
|
44
|
+
var hydrateBlock = /* @__PURE__ */ __name(async (archivist, hash, maxDepth = 1, minDepth = maxDepth) => {
|
|
27
45
|
assertEx(maxDepth >= 0, () => "maxDepth must be greater than or equal to 0");
|
|
28
46
|
assertEx(minDepth >= 0, () => "minDepth must be greater than or equal to 0");
|
|
29
47
|
assertEx(maxDepth >= minDepth, () => "maxDepth must be greater than or equal to minDepth");
|
|
@@ -75,119 +93,267 @@ var blockPayloadsFromHydratedBlock = /* @__PURE__ */ __name((block) => {
|
|
|
75
93
|
return block[0].payload_hashes.map((hash) => assertEx(block[1].find((p) => p._hash === hash), () => `missing payload ${hash}`)).filter((x) => !isTransactionBoundWitnessWithStorageMeta(x));
|
|
76
94
|
}, "blockPayloadsFromHydratedBlock");
|
|
77
95
|
|
|
78
|
-
// src/block/primitives/
|
|
96
|
+
// src/block/primitives/balanceStepSummaryFromRange.ts
|
|
79
97
|
import { assertEx as assertEx2 } from "@xylabs/assert";
|
|
80
|
-
import {
|
|
81
|
-
import {
|
|
82
|
-
import {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
98
|
+
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
99
|
+
import { isAnyPayload } from "@xyo-network/payload-model";
|
|
100
|
+
import { StepSizes as StepSizes3 } from "@xyo-network/xl1-protocol";
|
|
101
|
+
|
|
102
|
+
// src/payload/netBalancesForPayloads.ts
|
|
103
|
+
import { hexToBigInt, toAddress } from "@xylabs/hex";
|
|
104
|
+
import { isTransfer } from "@xyo-network/xl1-protocol";
|
|
105
|
+
var netBalancesForPayloads = /* @__PURE__ */ __name((payloads) => {
|
|
106
|
+
const balances = {};
|
|
107
|
+
for (const payload of payloads) {
|
|
108
|
+
if (isTransfer(payload)) {
|
|
109
|
+
const { from } = payload;
|
|
110
|
+
for (let [address, amount] of Object.entries(payload.transfers)) {
|
|
111
|
+
balances[toAddress(address)] = (balances[toAddress(address)] ?? 0n) + hexToBigInt(amount);
|
|
112
|
+
balances[toAddress(from)] = (balances[toAddress(from)] ?? 0n) - hexToBigInt(amount);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return balances;
|
|
117
|
+
}, "netBalancesForPayloads");
|
|
118
|
+
|
|
119
|
+
// src/SignedBigInt.ts
|
|
120
|
+
import { hexToBigInt as hexToBigInt2, toHex } from "@xylabs/hex";
|
|
121
|
+
import { isObject } from "@xylabs/object";
|
|
122
|
+
var isNegativeBigInt = /* @__PURE__ */ __name((value) => {
|
|
123
|
+
return isObject(value) && "negative" in value && typeof value.negative === "string";
|
|
124
|
+
}, "isNegativeBigInt");
|
|
125
|
+
var isPositiveBigInt = /* @__PURE__ */ __name((value) => {
|
|
126
|
+
return isObject(value) && "positive" in value && typeof value.positive === "string";
|
|
127
|
+
}, "isPositiveBigInt");
|
|
128
|
+
var parseSignedBigInt = /* @__PURE__ */ __name((value) => {
|
|
129
|
+
if (isNegativeBigInt(value)) {
|
|
130
|
+
return -hexToBigInt2(value.negative);
|
|
131
|
+
} else if (isPositiveBigInt(value)) {
|
|
132
|
+
return hexToBigInt2(value.positive);
|
|
133
|
+
} else {
|
|
134
|
+
throw new Error("Invalid balance type");
|
|
135
|
+
}
|
|
136
|
+
}, "parseSignedBigInt");
|
|
137
|
+
var toSignedBigInt = /* @__PURE__ */ __name((value) => {
|
|
138
|
+
return value < 0n ? {
|
|
139
|
+
negative: toHex(-value)
|
|
140
|
+
} : {
|
|
141
|
+
positive: toHex(value)
|
|
142
|
+
};
|
|
143
|
+
}, "toSignedBigInt");
|
|
144
|
+
var toPositiveBigInt = /* @__PURE__ */ __name((value) => {
|
|
145
|
+
if (isNegativeBigInt(value)) {
|
|
146
|
+
return {
|
|
147
|
+
positive: toHex(0n)
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
if (isPositiveBigInt(value)) {
|
|
151
|
+
return {
|
|
152
|
+
positive: value.positive
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
if (typeof value === "bigint") {
|
|
156
|
+
return {
|
|
157
|
+
positive: toHex(value)
|
|
158
|
+
};
|
|
86
159
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
160
|
+
throw new Error("Invalid value for positive big int");
|
|
161
|
+
}, "toPositiveBigInt");
|
|
162
|
+
|
|
163
|
+
// src/block/primitives/frames/deepCalculateFramesFromRange.ts
|
|
164
|
+
import { StepSizes } from "@xyo-network/xl1-protocol";
|
|
165
|
+
|
|
166
|
+
// src/block/primitives/frames/calculateFramesFromRange.ts
|
|
167
|
+
import { stepSize } from "@xyo-network/xl1-protocol";
|
|
168
|
+
function calculateFramesFromRange(range, step) {
|
|
169
|
+
const size = stepSize(step);
|
|
170
|
+
let start = Math.trunc(range[0] / size) * size;
|
|
171
|
+
const fitted = [];
|
|
172
|
+
const remaining = [];
|
|
173
|
+
if (start !== range[0]) {
|
|
174
|
+
start += size;
|
|
175
|
+
remaining.push([
|
|
176
|
+
range[0],
|
|
177
|
+
Math.min(start - 1, range[1])
|
|
92
178
|
]);
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
stepHash
|
|
179
|
+
}
|
|
180
|
+
for (let i = start; i <= range[1]; i += size) {
|
|
181
|
+
if (i + size - 1 <= range[1]) {
|
|
182
|
+
fitted.push([
|
|
183
|
+
i,
|
|
184
|
+
Math.min(i + size - 1, range[1])
|
|
100
185
|
]);
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
assertEx2(stepHashBlock.previous, () => "StepHashBlock must have a previous block")
|
|
186
|
+
} else {
|
|
187
|
+
remaining.push([
|
|
188
|
+
i,
|
|
189
|
+
range[1]
|
|
106
190
|
]);
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return [
|
|
194
|
+
fitted,
|
|
195
|
+
remaining
|
|
196
|
+
];
|
|
197
|
+
}
|
|
198
|
+
__name(calculateFramesFromRange, "calculateFramesFromRange");
|
|
199
|
+
|
|
200
|
+
// src/block/primitives/frames/deepCalculateFramesFromRange.ts
|
|
201
|
+
function deepCalculateFramesFromRange(range, startingStep = StepSizes.length - 1) {
|
|
202
|
+
const fitted = [];
|
|
203
|
+
let remaining = [
|
|
204
|
+
range
|
|
205
|
+
];
|
|
206
|
+
for (let step = startingStep; step >= 0; step--) {
|
|
207
|
+
const newRemaining = [];
|
|
208
|
+
for (const range2 of remaining) {
|
|
209
|
+
const [newFittedFrames, newRemainingFrames] = calculateFramesFromRange(range2, step);
|
|
210
|
+
fitted.push(...newFittedFrames);
|
|
211
|
+
newRemaining.push(...newRemainingFrames);
|
|
212
|
+
}
|
|
213
|
+
remaining = newRemaining;
|
|
214
|
+
}
|
|
215
|
+
for (const range2 of remaining) {
|
|
216
|
+
for (let i = range2[0]; i <= range2[1]; i++) {
|
|
217
|
+
fitted.push([
|
|
218
|
+
i,
|
|
219
|
+
i
|
|
114
220
|
]);
|
|
115
|
-
stepHash = lastBlockInStep.step_hashes.at(sizeIndex);
|
|
116
221
|
}
|
|
117
222
|
}
|
|
118
|
-
return
|
|
223
|
+
return fitted.toSorted((a, b) => a[0] - b[0]);
|
|
119
224
|
}
|
|
120
|
-
__name(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
225
|
+
__name(deepCalculateFramesFromRange, "deepCalculateFramesFromRange");
|
|
226
|
+
|
|
227
|
+
// src/block/primitives/hashFromBlockNumber.ts
|
|
228
|
+
import { asHash } from "@xylabs/hex";
|
|
229
|
+
import { toJsonString } from "@xylabs/object";
|
|
230
|
+
import { asBlockBoundWitnessWithStorageMeta as asBlockBoundWitnessWithStorageMeta2, StepSizes as StepSizes2 } from "@xyo-network/xl1-protocol";
|
|
231
|
+
async function hashFromBlockNumber(context, blockNumber) {
|
|
232
|
+
const result = await context.chainArchivist.get([
|
|
233
|
+
context.head
|
|
124
234
|
]);
|
|
125
|
-
|
|
126
|
-
|
|
235
|
+
let currentBlock = asBlockBoundWitnessWithStorageMeta2(result.at(0), () => `Head block not found for hash: ${context.head}`, {
|
|
236
|
+
required: true
|
|
237
|
+
});
|
|
238
|
+
while (currentBlock.block > blockNumber) {
|
|
239
|
+
let jumpHash = currentBlock.previous;
|
|
240
|
+
let jumpBlockNumber = currentBlock.block - 1;
|
|
241
|
+
for (const [step, stepSize2] of StepSizes2.entries()) {
|
|
242
|
+
const possibleJumpBlockNumber = currentBlock.block - stepSize2;
|
|
243
|
+
if (possibleJumpBlockNumber >= blockNumber && possibleJumpBlockNumber <= jumpBlockNumber) {
|
|
244
|
+
jumpBlockNumber = possibleJumpBlockNumber;
|
|
245
|
+
jumpHash = asHash(currentBlock.step_hashes.at(step), () => `Step hash not found for step ${step} in block ${currentBlock.block}`);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const [newBlock] = await context.chainArchivist.get([
|
|
249
|
+
asHash(jumpHash, () => `Jump hash not found for block number [${blockNumber}]: ${jumpBlockNumber} ${toJsonString(currentBlock, 10)}`)
|
|
250
|
+
]);
|
|
251
|
+
currentBlock = asBlockBoundWitnessWithStorageMeta2(newBlock, () => `Block not found for hash: ${jumpHash}`, {
|
|
252
|
+
required: true
|
|
253
|
+
});
|
|
254
|
+
if (currentBlock.block === blockNumber) {
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
if (currentBlock.block < blockNumber) {
|
|
258
|
+
throw new Error(`Block number ${blockNumber} is not a valid step block number for block ${context.head}.`);
|
|
259
|
+
}
|
|
127
260
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
end + 1,
|
|
171
|
-
range[1]
|
|
172
|
-
]
|
|
173
|
-
];
|
|
174
|
-
}
|
|
175
|
-
return [
|
|
176
|
-
range
|
|
177
|
-
];
|
|
178
|
-
});
|
|
261
|
+
return currentBlock._hash;
|
|
262
|
+
}
|
|
263
|
+
__name(hashFromBlockNumber, "hashFromBlockNumber");
|
|
264
|
+
|
|
265
|
+
// src/block/primitives/payloads.ts
|
|
266
|
+
var StepSummarySchema = "network.xyo.step.summary";
|
|
267
|
+
var BalancesStepSummarySchema = "network.xyo.step.summary.balances";
|
|
268
|
+
|
|
269
|
+
// src/block/primitives/balanceStepSummaryFromRange.ts
|
|
270
|
+
async function balanceStepSummaryFromRange(context, range) {
|
|
271
|
+
const frameHeadHash = await hashFromBlockNumber(context, range[1]);
|
|
272
|
+
const frameSize = range[1] - range[0] + 1;
|
|
273
|
+
let result = void 0;
|
|
274
|
+
if (frameSize === 1) {
|
|
275
|
+
const [, payloads] = await hydrateBlock(context.chainArchivist, context.head);
|
|
276
|
+
const balances = {};
|
|
277
|
+
for (const [address, balance] of Object.entries(netBalancesForPayloads(payloads))) {
|
|
278
|
+
balances[address] = toSignedBigInt(balance);
|
|
279
|
+
}
|
|
280
|
+
result = {
|
|
281
|
+
schema: BalancesStepSummarySchema,
|
|
282
|
+
hash: context.head,
|
|
283
|
+
stepSize: -1,
|
|
284
|
+
balances
|
|
285
|
+
};
|
|
286
|
+
} else {
|
|
287
|
+
const step = StepSizes3.indexOf(frameSize);
|
|
288
|
+
assertEx2(step !== -1, () => `Invalid step size: ${frameSize}. Must be one of ${StepSizes3.join(", ")}`);
|
|
289
|
+
const [summaryResult] = await context.summaryRepository.get([
|
|
290
|
+
frameHeadHash
|
|
291
|
+
]);
|
|
292
|
+
if (isAnyPayload(summaryResult)) {
|
|
293
|
+
result = summaryResult;
|
|
294
|
+
} else {
|
|
295
|
+
const subRanges = deepCalculateFramesFromRange(range, step - 1);
|
|
296
|
+
const promises = subRanges.map((subRange) => balanceStepSummaryFromRange(context, subRange));
|
|
297
|
+
const subResults = await Promise.all(promises);
|
|
298
|
+
const bigIntBalances = {};
|
|
299
|
+
for (const subResult of subResults) {
|
|
300
|
+
for (const [address, balance] of Object.entries(subResult.balances)) {
|
|
301
|
+
bigIntBalances[address] = (bigIntBalances[address] ?? 0n) + parseSignedBigInt(balance);
|
|
302
|
+
}
|
|
179
303
|
}
|
|
304
|
+
const balances = {};
|
|
305
|
+
for (const [address, balance] of Object.entries(bigIntBalances)) {
|
|
306
|
+
balances[address] = toSignedBigInt(balance);
|
|
307
|
+
}
|
|
308
|
+
result = {
|
|
309
|
+
schema: BalancesStepSummarySchema,
|
|
310
|
+
hash: frameHeadHash,
|
|
311
|
+
stepSize: frameSize,
|
|
312
|
+
balances
|
|
313
|
+
};
|
|
314
|
+
await context.summaryRepository.insert([
|
|
315
|
+
result
|
|
316
|
+
]);
|
|
180
317
|
}
|
|
181
318
|
}
|
|
182
|
-
|
|
319
|
+
const finalResult = await PayloadBuilder.addStorageMeta(result);
|
|
320
|
+
return finalResult;
|
|
183
321
|
}
|
|
184
|
-
__name(
|
|
322
|
+
__name(balanceStepSummaryFromRange, "balanceStepSummaryFromRange");
|
|
323
|
+
|
|
324
|
+
// src/block/primitives/balanceSummary.ts
|
|
325
|
+
import { asAddress } from "@xylabs/hex";
|
|
326
|
+
import { isDefined } from "@xylabs/typeof";
|
|
327
|
+
import { asBlockBoundWitnessWithStorageMeta as asBlockBoundWitnessWithStorageMeta3 } from "@xyo-network/xl1-protocol";
|
|
328
|
+
async function balanceSummary(context) {
|
|
329
|
+
const headResult = await context.chainArchivist.get([
|
|
330
|
+
context.head
|
|
331
|
+
]);
|
|
332
|
+
const headBoundWitness = asBlockBoundWitnessWithStorageMeta3(headResult.at(0), () => `Head block not found for hash: ${context.head}`, {
|
|
333
|
+
required: true
|
|
334
|
+
});
|
|
335
|
+
const rangeStart = isDefined(context.windowSize) ? Math.max(headBoundWitness.block - context.windowSize + 1, 0) : 0;
|
|
336
|
+
const ranges = deepCalculateFramesFromRange([
|
|
337
|
+
rangeStart,
|
|
338
|
+
headBoundWitness.block
|
|
339
|
+
]);
|
|
340
|
+
const summaries = await Promise.all(ranges.map((range) => balanceStepSummaryFromRange(context, range)));
|
|
341
|
+
const balances = {};
|
|
342
|
+
for (let summary of summaries) {
|
|
343
|
+
for (const [address, balance] of Object.entries(summary.balances)) {
|
|
344
|
+
const validAddress = asAddress(address, () => `Invalid address: ${address}`);
|
|
345
|
+
balances[validAddress] = (balances[validAddress] ?? 0n) + parseSignedBigInt(balance);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return balances;
|
|
349
|
+
}
|
|
350
|
+
__name(balanceSummary, "balanceSummary");
|
|
185
351
|
|
|
186
352
|
// src/transaction/buildTransaction.ts
|
|
187
353
|
import { assertEx as assertEx3 } from "@xylabs/assert";
|
|
188
|
-
import { toHex } from "@xylabs/hex";
|
|
354
|
+
import { toHex as toHex2 } from "@xylabs/hex";
|
|
189
355
|
import { BoundWitnessBuilder } from "@xyo-network/boundwitness-builder";
|
|
190
|
-
import { PayloadBuilder } from "@xyo-network/payload-builder";
|
|
356
|
+
import { PayloadBuilder as PayloadBuilder2 } from "@xyo-network/payload-builder";
|
|
191
357
|
import { defaultTransactionFees } from "@xyo-network/xl1-protocol";
|
|
192
358
|
async function buildTransaction(chain, elevatedPayloads, additionalPayloads, signer, nbf, exp, from, fees = defaultTransactionFees) {
|
|
193
359
|
if (from === void 0 && Array.isArray(signer)) {
|
|
@@ -196,24 +362,27 @@ async function buildTransaction(chain, elevatedPayloads, additionalPayloads, sig
|
|
|
196
362
|
const txBoundWitnessFields = {
|
|
197
363
|
chain,
|
|
198
364
|
fees: {
|
|
199
|
-
base:
|
|
200
|
-
gasLimit:
|
|
201
|
-
gasPrice:
|
|
202
|
-
priority:
|
|
365
|
+
base: toHex2(fees.base),
|
|
366
|
+
gasLimit: toHex2(fees.gasLimit),
|
|
367
|
+
gasPrice: toHex2(fees.gasPrice),
|
|
368
|
+
priority: toHex2(fees.priority)
|
|
203
369
|
},
|
|
204
370
|
nbf,
|
|
205
371
|
exp
|
|
206
372
|
};
|
|
207
|
-
const elevatedHashes = await
|
|
373
|
+
const elevatedHashes = await PayloadBuilder2.hashes(elevatedPayloads);
|
|
208
374
|
const script = [];
|
|
209
375
|
for (const elevatedHash of elevatedHashes) {
|
|
210
376
|
script.push(`elevate|${elevatedHash}`);
|
|
211
377
|
}
|
|
212
|
-
const
|
|
378
|
+
const fields = {
|
|
213
379
|
...txBoundWitnessFields,
|
|
214
|
-
script,
|
|
215
380
|
from: from ?? (Array.isArray(signer) ? assertEx3(signer.at(0)?.address) : signer.address)
|
|
216
|
-
}
|
|
381
|
+
};
|
|
382
|
+
if (script.length > 0) {
|
|
383
|
+
fields.script = script;
|
|
384
|
+
}
|
|
385
|
+
const [tx, txPayloads] = await new BoundWitnessBuilder().fields(fields).meta({
|
|
217
386
|
$signatures: []
|
|
218
387
|
}).payloads([
|
|
219
388
|
...elevatedPayloads,
|
|
@@ -222,8 +391,8 @@ async function buildTransaction(chain, elevatedPayloads, additionalPayloads, sig
|
|
|
222
391
|
signer
|
|
223
392
|
]).build();
|
|
224
393
|
return [
|
|
225
|
-
await
|
|
226
|
-
await
|
|
394
|
+
await PayloadBuilder2.addStorageMeta(tx),
|
|
395
|
+
await PayloadBuilder2.addStorageMeta(txPayloads)
|
|
227
396
|
];
|
|
228
397
|
}
|
|
229
398
|
__name(buildTransaction, "buildTransaction");
|
|
@@ -276,17 +445,17 @@ var hydrateElevatedTransaction = /* @__PURE__ */ __name(async (archivist, hash)
|
|
|
276
445
|
}, "hydrateElevatedTransaction");
|
|
277
446
|
|
|
278
447
|
// src/transaction/primitives/transactionBlockByteCount.ts
|
|
279
|
-
import { PayloadBuilder as
|
|
448
|
+
import { PayloadBuilder as PayloadBuilder3 } from "@xyo-network/payload-builder";
|
|
280
449
|
function transactionBlockByteCount([transaction, payloads]) {
|
|
281
|
-
const cleanTransaction =
|
|
450
|
+
const cleanTransaction = PayloadBuilder3.omitStorageMeta(transaction);
|
|
282
451
|
const transactionBytes = JSON.stringify(cleanTransaction).length;
|
|
283
|
-
const cleanPayloads =
|
|
452
|
+
const cleanPayloads = PayloadBuilder3.omitStorageMeta(payloads);
|
|
284
453
|
return cleanPayloads.reduce((acc, payload) => acc + JSON.stringify(payload).length, 0) + transactionBytes;
|
|
285
454
|
}
|
|
286
455
|
__name(transactionBlockByteCount, "transactionBlockByteCount");
|
|
287
456
|
|
|
288
457
|
// src/transaction/primitives/transactionElevatedPayloads.ts
|
|
289
|
-
import { asHash } from "@xylabs/hex";
|
|
458
|
+
import { asHash as asHash2 } from "@xylabs/hex";
|
|
290
459
|
|
|
291
460
|
// src/transaction/primitives/transactionOperations.ts
|
|
292
461
|
function crackOperation(operation) {
|
|
@@ -308,7 +477,7 @@ __name(crackOperations, "crackOperations");
|
|
|
308
477
|
// src/transaction/primitives/transactionElevatedPayloads.ts
|
|
309
478
|
function transactionElevatedPayloadHashes(transaction) {
|
|
310
479
|
const elevateOperations = crackOperations(transaction.script ?? []).filter((op) => op[0] === "elevate");
|
|
311
|
-
return elevateOperations.map((op) =>
|
|
480
|
+
return elevateOperations.map((op) => asHash2(op[1][0], true));
|
|
312
481
|
}
|
|
313
482
|
__name(transactionElevatedPayloadHashes, "transactionElevatedPayloadHashes");
|
|
314
483
|
function transactionElevatedPayloads([transaction, payloads]) {
|
|
@@ -336,20 +505,31 @@ function transactionRequiredGas(hydratedTransaction) {
|
|
|
336
505
|
}
|
|
337
506
|
__name(transactionRequiredGas, "transactionRequiredGas");
|
|
338
507
|
export {
|
|
508
|
+
BalancesStepSummarySchema,
|
|
509
|
+
StepSummarySchema,
|
|
339
510
|
allHashesPresent,
|
|
511
|
+
balanceStepSummaryFromRange,
|
|
512
|
+
balanceSummary,
|
|
340
513
|
blockPayloadsFromHydratedBlock,
|
|
341
514
|
buildTransaction,
|
|
515
|
+
calculateFramesFromRange,
|
|
342
516
|
crackOperation,
|
|
343
517
|
crackOperations,
|
|
518
|
+
deepCalculateFramesFromRange,
|
|
344
519
|
flattenHydratedBlock,
|
|
345
520
|
flattenHydratedBlocks,
|
|
346
521
|
flattenHydratedTransaction,
|
|
347
522
|
flattenHydratedTransactions,
|
|
523
|
+
hashFromBlockNumber,
|
|
348
524
|
hydrateBlock,
|
|
349
525
|
hydrateElevatedTransaction,
|
|
350
526
|
hydrateTransaction,
|
|
351
|
-
|
|
352
|
-
|
|
527
|
+
isNegativeBigInt,
|
|
528
|
+
isPositiveBigInt,
|
|
529
|
+
netBalancesForPayloads,
|
|
530
|
+
parseSignedBigInt,
|
|
531
|
+
toPositiveBigInt,
|
|
532
|
+
toSignedBigInt,
|
|
353
533
|
transactionBlockByteCount,
|
|
354
534
|
transactionBytesRequiredGas,
|
|
355
535
|
transactionElevatedPayloadHashes,
|