@argonprotocol/mainchain 1.3.12 → 1.3.14
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/browser/index.d.ts +117 -91
- package/browser/index.js +316 -241
- package/browser/index.js.map +1 -1
- package/lib/index.cjs +309 -244
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +117 -91
- package/lib/index.d.ts +117 -91
- package/lib/index.js +308 -243
- package/lib/index.js.map +1 -1
- package/package.json +2 -2
package/lib/index.cjs
CHANGED
|
@@ -81,163 +81,6 @@ var WageProtector = class _WageProtector {
|
|
|
81
81
|
}
|
|
82
82
|
};
|
|
83
83
|
|
|
84
|
-
// src/TxSubmitter.ts
|
|
85
|
-
function logExtrinsicResult(result) {
|
|
86
|
-
const json = result.status.toJSON();
|
|
87
|
-
const status = Object.keys(json)[0];
|
|
88
|
-
console.debug('Transaction update: "%s"', status, json[status]);
|
|
89
|
-
}
|
|
90
|
-
var TxSubmitter = class {
|
|
91
|
-
constructor(client, tx, pair) {
|
|
92
|
-
this.client = client;
|
|
93
|
-
this.tx = tx;
|
|
94
|
-
this.pair = pair;
|
|
95
|
-
}
|
|
96
|
-
async feeEstimate(tip) {
|
|
97
|
-
const { partialFee } = await this.tx.paymentInfo(this.pair, { tip });
|
|
98
|
-
return partialFee.toBigInt();
|
|
99
|
-
}
|
|
100
|
-
async canAfford(options = {}) {
|
|
101
|
-
const { tip, unavailableBalance } = options;
|
|
102
|
-
const account = await this.client.query.system.account(this.pair.address);
|
|
103
|
-
let availableBalance = account.data.free.toBigInt();
|
|
104
|
-
const userBalance = availableBalance;
|
|
105
|
-
if (unavailableBalance) {
|
|
106
|
-
availableBalance -= unavailableBalance;
|
|
107
|
-
}
|
|
108
|
-
const existentialDeposit = options.includeExistentialDeposit ? this.client.consts.balances.existentialDeposit.toBigInt() : 0n;
|
|
109
|
-
const fees = await this.feeEstimate(tip);
|
|
110
|
-
const totalCharge = fees + (_nullishCoalesce(tip, () => ( 0n)));
|
|
111
|
-
const canAfford = availableBalance >= totalCharge + existentialDeposit;
|
|
112
|
-
return { canAfford, availableBalance: userBalance, txFee: fees };
|
|
113
|
-
}
|
|
114
|
-
async submit(options = {}) {
|
|
115
|
-
const { logResults, waitForBlock, useLatestNonce, ...apiOptions } = options;
|
|
116
|
-
await waitForLoad();
|
|
117
|
-
const result = new TxResult(this.client, logResults);
|
|
118
|
-
result.txProgressCallback = options.txProgressCallback;
|
|
119
|
-
let toHuman = this.tx.toHuman().method;
|
|
120
|
-
const txString = [];
|
|
121
|
-
let api = formatCall(toHuman);
|
|
122
|
-
const args = [];
|
|
123
|
-
if (api === "proxy.proxy") {
|
|
124
|
-
toHuman = toHuman.args.call;
|
|
125
|
-
txString.push("Proxy");
|
|
126
|
-
api = formatCall(toHuman);
|
|
127
|
-
}
|
|
128
|
-
if (api.startsWith("utility.batch")) {
|
|
129
|
-
const calls = toHuman.args.calls.map(formatCall).join(", ");
|
|
130
|
-
txString.push(`Batch[${calls}]`);
|
|
131
|
-
} else {
|
|
132
|
-
txString.push(api);
|
|
133
|
-
args.push(toHuman.args);
|
|
134
|
-
}
|
|
135
|
-
args.unshift(txString.join("->"));
|
|
136
|
-
if (useLatestNonce && !apiOptions.nonce) {
|
|
137
|
-
apiOptions.nonce = await this.client.rpc.system.accountNextIndex(this.pair.address);
|
|
138
|
-
}
|
|
139
|
-
console.log("Submitting transaction from %s:", this.pair.address, ...args);
|
|
140
|
-
await this.tx.signAndSend(this.pair, apiOptions, result.onResult.bind(result));
|
|
141
|
-
if (waitForBlock) {
|
|
142
|
-
await result.inBlockPromise;
|
|
143
|
-
}
|
|
144
|
-
return result;
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
function formatCall(call) {
|
|
148
|
-
return `${call.section}.${call.method}`;
|
|
149
|
-
}
|
|
150
|
-
var TxResult = (_class = class {
|
|
151
|
-
constructor(client, shouldLog = false) {;_class.prototype.__init.call(this);
|
|
152
|
-
this.client = client;
|
|
153
|
-
this.shouldLog = shouldLog;
|
|
154
|
-
this.inBlockPromise = new Promise((resolve, reject) => {
|
|
155
|
-
this.inBlockResolve = resolve;
|
|
156
|
-
this.inBlockReject = reject;
|
|
157
|
-
});
|
|
158
|
-
this.finalizedPromise = new Promise((resolve, reject) => {
|
|
159
|
-
this.finalizedResolve = resolve;
|
|
160
|
-
this.finalizedReject = reject;
|
|
161
|
-
});
|
|
162
|
-
this.inBlockPromise.catch(() => null);
|
|
163
|
-
this.finalizedPromise.catch(() => null);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
__init() {this.events = []}
|
|
169
|
-
/**
|
|
170
|
-
* The index of the batch that was interrupted, if any.
|
|
171
|
-
*/
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* The final fee paid for the transaction, including the fee tip.
|
|
176
|
-
*/
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* The fee tip paid for the transaction.
|
|
180
|
-
*/
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
onResult(result) {
|
|
188
|
-
this.status = result.status;
|
|
189
|
-
if (this.shouldLog) {
|
|
190
|
-
logExtrinsicResult(result);
|
|
191
|
-
}
|
|
192
|
-
const { events, status, dispatchError, isFinalized } = result;
|
|
193
|
-
if (status.isInBlock) {
|
|
194
|
-
this.includedInBlock = new Uint8Array(status.asInBlock);
|
|
195
|
-
let encounteredError = dispatchError;
|
|
196
|
-
let batchErrorIndex;
|
|
197
|
-
for (const event of events) {
|
|
198
|
-
this.events.push(event.event);
|
|
199
|
-
if (this.client.events.utility.BatchInterrupted.is(event.event)) {
|
|
200
|
-
batchErrorIndex = event.event.data[0].toNumber();
|
|
201
|
-
this.batchInterruptedIndex = batchErrorIndex;
|
|
202
|
-
encounteredError = event.event.data[1];
|
|
203
|
-
}
|
|
204
|
-
if (this.client.events.transactionPayment.TransactionFeePaid.is(event.event)) {
|
|
205
|
-
const [_who, actualFee, tip] = event.event.data;
|
|
206
|
-
this.finalFee = actualFee.toBigInt();
|
|
207
|
-
this.finalFeeTip = tip.toBigInt();
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
if (encounteredError) {
|
|
211
|
-
const error = dispatchErrorToExtrinsicError(
|
|
212
|
-
this.client,
|
|
213
|
-
encounteredError,
|
|
214
|
-
batchErrorIndex,
|
|
215
|
-
this.finalFee
|
|
216
|
-
);
|
|
217
|
-
this.reject(error);
|
|
218
|
-
} else {
|
|
219
|
-
this.inBlockResolve(new Uint8Array(status.asInBlock));
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
if (isFinalized) {
|
|
223
|
-
this.finalizedResolve(status.asFinalized);
|
|
224
|
-
}
|
|
225
|
-
if (this.txProgressCallback) {
|
|
226
|
-
let percent = 0;
|
|
227
|
-
if (result.status.isBroadcast) {
|
|
228
|
-
percent = 50;
|
|
229
|
-
} else if (result.status.isInBlock) {
|
|
230
|
-
percent = 100;
|
|
231
|
-
}
|
|
232
|
-
this.txProgressCallback(percent, this);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
reject(error) {
|
|
236
|
-
this.inBlockReject(error);
|
|
237
|
-
this.finalizedReject(error);
|
|
238
|
-
}
|
|
239
|
-
}, _class);
|
|
240
|
-
|
|
241
84
|
// src/utils.ts
|
|
242
85
|
var _bignumberjs = require('bignumber.js'); var BN = _interopRequireWildcard(_bignumberjs); var BN2 = _interopRequireWildcard(_bignumberjs);
|
|
243
86
|
var { ROUND_FLOOR } = BN;
|
|
@@ -285,7 +128,7 @@ function dispatchErrorToString(client, error) {
|
|
|
285
128
|
}
|
|
286
129
|
return message;
|
|
287
130
|
}
|
|
288
|
-
var
|
|
131
|
+
var ExtrinsicError = class extends Error {
|
|
289
132
|
constructor(errorCode, details, batchInterruptedIndex, txFee = 0n) {
|
|
290
133
|
super(errorCode);
|
|
291
134
|
this.errorCode = errorCode;
|
|
@@ -304,9 +147,9 @@ function dispatchErrorToExtrinsicError(client, error, batchInterruptedIndex, txF
|
|
|
304
147
|
if (error.isModule) {
|
|
305
148
|
const decoded = client.registry.findMetaError(error.asModule);
|
|
306
149
|
const { docs, name, section } = decoded;
|
|
307
|
-
return new
|
|
150
|
+
return new ExtrinsicError(`${section}.${name}`, docs.join(" "), batchInterruptedIndex, txFee);
|
|
308
151
|
}
|
|
309
|
-
return new
|
|
152
|
+
return new ExtrinsicError(error.toString(), void 0, batchInterruptedIndex, txFee);
|
|
310
153
|
}
|
|
311
154
|
function checkForExtrinsicSuccess(events, client) {
|
|
312
155
|
return new Promise((resolve, reject) => {
|
|
@@ -326,6 +169,243 @@ function checkForExtrinsicSuccess(events, client) {
|
|
|
326
169
|
});
|
|
327
170
|
}
|
|
328
171
|
|
|
172
|
+
// src/TxResult.ts
|
|
173
|
+
var TxResult = (_class = class {
|
|
174
|
+
constructor(client, extrinsic) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);
|
|
175
|
+
this.client = client;
|
|
176
|
+
this.extrinsic = extrinsic;
|
|
177
|
+
this.waitForFinalizedBlock = new Promise((resolve, reject) => {
|
|
178
|
+
this.finalizedResolve = resolve;
|
|
179
|
+
this.finalizedReject = reject;
|
|
180
|
+
});
|
|
181
|
+
this.waitForInFirstBlock = new Promise((resolve, reject) => {
|
|
182
|
+
this.inBlockResolve = resolve;
|
|
183
|
+
this.inBlockReject = reject;
|
|
184
|
+
});
|
|
185
|
+
this.waitForFinalizedBlock.catch(() => null);
|
|
186
|
+
this.waitForInFirstBlock.catch(() => null);
|
|
187
|
+
}
|
|
188
|
+
#isBroadcast = false;
|
|
189
|
+
#submissionError;
|
|
190
|
+
set isBroadcast(value) {
|
|
191
|
+
this.#isBroadcast = value;
|
|
192
|
+
this.updateProgress();
|
|
193
|
+
}
|
|
194
|
+
get isBroadcast() {
|
|
195
|
+
return this.#isBroadcast;
|
|
196
|
+
}
|
|
197
|
+
set submissionError(value) {
|
|
198
|
+
if (value) {
|
|
199
|
+
this.#submissionError = value;
|
|
200
|
+
this.finalizedReject(value);
|
|
201
|
+
this.inBlockReject(value);
|
|
202
|
+
this.updateProgress();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
get submissionError() {
|
|
206
|
+
return this.#submissionError;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
__init() {this.events = []}
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* The index of the batch that was interrupted, if any.
|
|
216
|
+
*/
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The final fee paid for the transaction, including the fee tip.
|
|
222
|
+
*/
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* The fee tip paid for the transaction.
|
|
226
|
+
*/
|
|
227
|
+
|
|
228
|
+
__init2() {this.txProgress = 0}
|
|
229
|
+
__init3() {this.isFinalized = false}
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
async setSeenInBlock(block) {
|
|
235
|
+
const { blockHash, blockNumber, events } = block;
|
|
236
|
+
if (blockHash !== this.blockHash) {
|
|
237
|
+
this.parseEvents(events);
|
|
238
|
+
this.blockHash = blockHash;
|
|
239
|
+
this.blockNumber = await _asyncNullishCoalesce(blockNumber, async () => ( await this.client.rpc.chain.getHeader(blockHash).then((h) => h.number.toNumber())));
|
|
240
|
+
this.extrinsicIndex = block.extrinsicIndex;
|
|
241
|
+
this.updateProgress();
|
|
242
|
+
if (this.extrinsicError) {
|
|
243
|
+
this.inBlockReject(this.extrinsicError);
|
|
244
|
+
} else {
|
|
245
|
+
this.inBlockResolve(blockHash);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
setFinalized() {
|
|
250
|
+
this.isFinalized = true;
|
|
251
|
+
this.updateProgress();
|
|
252
|
+
let error = _nullishCoalesce(this.extrinsicError, () => ( this.submissionError));
|
|
253
|
+
if (!error && !this.blockHash) {
|
|
254
|
+
error = new Error("Cannot finalize transaction before it is included in a block");
|
|
255
|
+
}
|
|
256
|
+
if (error) {
|
|
257
|
+
this.finalizedReject(error);
|
|
258
|
+
this.inBlockReject(error);
|
|
259
|
+
} else {
|
|
260
|
+
this.finalizedResolve(this.blockHash);
|
|
261
|
+
this.inBlockResolve(this.blockHash);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
onSubscriptionResult(result) {
|
|
265
|
+
const { events, status, isFinalized, txIndex } = result;
|
|
266
|
+
const extrinsicEvents = events.map((x) => x.event);
|
|
267
|
+
if (status.isBroadcast) {
|
|
268
|
+
this.isBroadcast = true;
|
|
269
|
+
if (result.internalError) this.submissionError = result.internalError;
|
|
270
|
+
}
|
|
271
|
+
if (status.isInBlock) {
|
|
272
|
+
void this.setSeenInBlock({
|
|
273
|
+
blockHash: Uint8Array.from(status.asInBlock),
|
|
274
|
+
events: extrinsicEvents,
|
|
275
|
+
extrinsicIndex: txIndex
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
if (isFinalized) {
|
|
279
|
+
this.setFinalized();
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
updateProgress() {
|
|
283
|
+
if (this.isFinalized || this.submissionError) {
|
|
284
|
+
this.txProgress = 100;
|
|
285
|
+
} else if (this.blockNumber) {
|
|
286
|
+
const elapsedBlocks = this.blockNumber - this.extrinsic.submittedAtBlockNumber;
|
|
287
|
+
const FINALIZATION_BLOCKS = 5;
|
|
288
|
+
const remainingPercent = Math.max(0, FINALIZATION_BLOCKS - elapsedBlocks) * 20;
|
|
289
|
+
const percent = 100 - remainingPercent;
|
|
290
|
+
this.txProgress = Math.min(percent, 99);
|
|
291
|
+
} else if (this.extrinsic.submittedAtBlockNumber) {
|
|
292
|
+
this.txProgress = 10;
|
|
293
|
+
}
|
|
294
|
+
_optionalChain([this, 'access', _ => _.txProgressCallback, 'optionalCall', _2 => _2(this.txProgress)]);
|
|
295
|
+
}
|
|
296
|
+
parseEvents(events) {
|
|
297
|
+
let encounteredError;
|
|
298
|
+
for (const event of events) {
|
|
299
|
+
if (this.client.events.system.ExtrinsicFailed.is(event)) {
|
|
300
|
+
const { dispatchError } = event.data;
|
|
301
|
+
encounteredError ??= dispatchError;
|
|
302
|
+
}
|
|
303
|
+
if (this.client.events.utility.BatchInterrupted.is(event)) {
|
|
304
|
+
const { index, error } = event.data;
|
|
305
|
+
this.batchInterruptedIndex = index.toNumber();
|
|
306
|
+
encounteredError = error;
|
|
307
|
+
}
|
|
308
|
+
if (this.client.events.transactionPayment.TransactionFeePaid.is(event)) {
|
|
309
|
+
const { actualFee, tip } = event.data;
|
|
310
|
+
this.finalFee = actualFee.toBigInt();
|
|
311
|
+
this.finalFeeTip = tip.toBigInt();
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (encounteredError) {
|
|
315
|
+
this.extrinsicError = dispatchErrorToExtrinsicError(
|
|
316
|
+
this.client,
|
|
317
|
+
encounteredError,
|
|
318
|
+
this.batchInterruptedIndex,
|
|
319
|
+
this.finalFee
|
|
320
|
+
);
|
|
321
|
+
} else {
|
|
322
|
+
this.extrinsicError = void 0;
|
|
323
|
+
}
|
|
324
|
+
this.events = events;
|
|
325
|
+
}
|
|
326
|
+
}, _class);
|
|
327
|
+
|
|
328
|
+
// src/TxSubmitter.ts
|
|
329
|
+
var TxSubmitter = class {
|
|
330
|
+
constructor(client, tx, pair) {
|
|
331
|
+
this.client = client;
|
|
332
|
+
this.tx = tx;
|
|
333
|
+
this.pair = pair;
|
|
334
|
+
}
|
|
335
|
+
async feeEstimate(tip) {
|
|
336
|
+
const { partialFee } = await this.tx.paymentInfo(this.pair, { tip });
|
|
337
|
+
return partialFee.toBigInt();
|
|
338
|
+
}
|
|
339
|
+
async canAfford(options = {}) {
|
|
340
|
+
const { tip, unavailableBalance } = options;
|
|
341
|
+
const account = await this.client.query.system.account(this.pair.address);
|
|
342
|
+
let availableBalance = account.data.free.toBigInt();
|
|
343
|
+
const userBalance = availableBalance;
|
|
344
|
+
if (unavailableBalance) {
|
|
345
|
+
availableBalance -= unavailableBalance;
|
|
346
|
+
}
|
|
347
|
+
const existentialDeposit = options.includeExistentialDeposit ? this.client.consts.balances.existentialDeposit.toBigInt() : 0n;
|
|
348
|
+
const fees = await this.feeEstimate(tip);
|
|
349
|
+
const totalCharge = fees + (_nullishCoalesce(tip, () => ( 0n)));
|
|
350
|
+
const canAfford = availableBalance >= totalCharge + existentialDeposit;
|
|
351
|
+
return { canAfford, availableBalance: userBalance, txFee: fees };
|
|
352
|
+
}
|
|
353
|
+
async submit(options = {}) {
|
|
354
|
+
const { useLatestNonce, ...apiOptions } = options;
|
|
355
|
+
await waitForLoad();
|
|
356
|
+
const blockHeight = await this.client.rpc.chain.getHeader().then((h) => h.number.toNumber());
|
|
357
|
+
if (options.logResults) {
|
|
358
|
+
this.logRequest();
|
|
359
|
+
}
|
|
360
|
+
if (useLatestNonce && !apiOptions.nonce) {
|
|
361
|
+
apiOptions.nonce = await this.client.rpc.system.accountNextIndex(this.pair.address);
|
|
362
|
+
}
|
|
363
|
+
const signedTx = await this.tx.signAsync(this.pair, apiOptions);
|
|
364
|
+
const txHash = signedTx.hash.toHex();
|
|
365
|
+
const result = new TxResult(this.client, {
|
|
366
|
+
signedHash: txHash,
|
|
367
|
+
method: signedTx.method.toHuman(),
|
|
368
|
+
accountAddress: this.pair.address,
|
|
369
|
+
submittedTime: /* @__PURE__ */ new Date(),
|
|
370
|
+
submittedAtBlockNumber: blockHeight
|
|
371
|
+
});
|
|
372
|
+
if (options.disableAutomaticTxTracking !== true) {
|
|
373
|
+
await signedTx.send(result.onSubscriptionResult.bind(result));
|
|
374
|
+
} else {
|
|
375
|
+
try {
|
|
376
|
+
await signedTx.send();
|
|
377
|
+
result.isBroadcast = true;
|
|
378
|
+
} catch (error) {
|
|
379
|
+
result.submissionError = error;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return result;
|
|
383
|
+
}
|
|
384
|
+
logRequest() {
|
|
385
|
+
let toHuman = this.tx.toHuman().method;
|
|
386
|
+
const txString = [];
|
|
387
|
+
let api = formatCall(toHuman);
|
|
388
|
+
const args = [];
|
|
389
|
+
if (api === "proxy.proxy") {
|
|
390
|
+
toHuman = toHuman.args.call;
|
|
391
|
+
txString.push("Proxy");
|
|
392
|
+
api = formatCall(toHuman);
|
|
393
|
+
}
|
|
394
|
+
if (api.startsWith("utility.batch")) {
|
|
395
|
+
const calls = toHuman.args.calls.map(formatCall).join(", ");
|
|
396
|
+
txString.push(`Batch[${calls}]`);
|
|
397
|
+
} else {
|
|
398
|
+
txString.push(api);
|
|
399
|
+
args.push(toHuman.args);
|
|
400
|
+
}
|
|
401
|
+
args.unshift(txString.join("->"));
|
|
402
|
+
console.log("Submitting transaction from %s:", this.pair.address, ...args);
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
function formatCall(call) {
|
|
406
|
+
return `${call.section}.${call.method}`;
|
|
407
|
+
}
|
|
408
|
+
|
|
329
409
|
// src/keyringUtils.ts
|
|
330
410
|
function keyringFromSuri(suri, cryptoType = "sr25519") {
|
|
331
411
|
return new (0, _api.Keyring)({ type: cryptoType }).createFromUri(suri);
|
|
@@ -523,29 +603,29 @@ var Vault = class _Vault {
|
|
|
523
603
|
);
|
|
524
604
|
}
|
|
525
605
|
const result = await tx.submit({
|
|
526
|
-
|
|
527
|
-
useLatestNonce: true
|
|
528
|
-
waitForBlock: true,
|
|
529
|
-
txProgressCallback
|
|
606
|
+
...args,
|
|
607
|
+
useLatestNonce: true
|
|
530
608
|
});
|
|
531
|
-
await
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
609
|
+
const tickDuration = await _asyncNullishCoalesce(config.tickDurationMillis, async () => ( await client.query.ticks.genesisTicker().then((x) => x.tickDurationMillis.toNumber())));
|
|
610
|
+
async function getVault() {
|
|
611
|
+
await result.waitForFinalizedBlock;
|
|
612
|
+
let vaultId;
|
|
613
|
+
for (const event of result.events) {
|
|
614
|
+
if (client.events.vaults.VaultCreated.is(event)) {
|
|
615
|
+
vaultId = event.data.vaultId.toNumber();
|
|
616
|
+
break;
|
|
617
|
+
}
|
|
537
618
|
}
|
|
619
|
+
if (vaultId === void 0) {
|
|
620
|
+
throw new Error("Vault creation failed, no VaultCreated event found");
|
|
621
|
+
}
|
|
622
|
+
const rawVault = await client.query.vaults.vaultsById(vaultId);
|
|
623
|
+
if (rawVault.isNone) {
|
|
624
|
+
throw new Error("Vault creation failed, vault not found");
|
|
625
|
+
}
|
|
626
|
+
return new _Vault(vaultId, rawVault.unwrap(), tickDuration);
|
|
538
627
|
}
|
|
539
|
-
|
|
540
|
-
throw new Error("Vault creation failed, no VaultCreated event found");
|
|
541
|
-
}
|
|
542
|
-
const rawVault = await client.query.vaults.vaultsById(vaultId);
|
|
543
|
-
if (rawVault.isNone) {
|
|
544
|
-
throw new Error("Vault creation failed, vault not found");
|
|
545
|
-
}
|
|
546
|
-
const tickDuration = await _asyncNullishCoalesce(config.tickDurationMillis, async () => ( await client.query.ticks.genesisTicker().then((x) => x.tickDurationMillis.toNumber())));
|
|
547
|
-
const vault = new _Vault(vaultId, rawVault.unwrap(), tickDuration);
|
|
548
|
-
return { vault, txResult: result };
|
|
628
|
+
return { getVault, txResult: result };
|
|
549
629
|
}
|
|
550
630
|
};
|
|
551
631
|
|
|
@@ -635,7 +715,7 @@ var BitcoinLocks = class {
|
|
|
635
715
|
};
|
|
636
716
|
}
|
|
637
717
|
async getBitcoinConfirmedBlockHeight() {
|
|
638
|
-
return await this.client.query.bitcoinUtxos.confirmedBitcoinBlockTip().then((x) => _nullishCoalesce(_optionalChain([x, 'access',
|
|
718
|
+
return await this.client.query.bitcoinUtxos.confirmedBitcoinBlockTip().then((x) => _nullishCoalesce(_optionalChain([x, 'access', _3 => _3.value, 'optionalAccess', _4 => _4.blockHeight, 'access', _5 => _5.toNumber, 'call', _6 => _6()]), () => ( 0)));
|
|
639
719
|
}
|
|
640
720
|
/**
|
|
641
721
|
* Gets the UTXO reference by ID.
|
|
@@ -684,7 +764,7 @@ var BitcoinLocks = class {
|
|
|
684
764
|
const signature = _util.u8aToHex.call(void 0, vaultSignature);
|
|
685
765
|
const tx = client.tx.bitcoinLocks.cosignRelease(utxoId, signature);
|
|
686
766
|
const submitter = new TxSubmitter(client, tx, argonKeyring);
|
|
687
|
-
return await submitter.submit(
|
|
767
|
+
return await submitter.submit(args);
|
|
688
768
|
}
|
|
689
769
|
async getBitcoinLock(utxoId) {
|
|
690
770
|
const utxoRaw = await this.client.query.bitcoinLocks.locksByUtxoId(utxoId);
|
|
@@ -763,7 +843,7 @@ var BitcoinLocks = class {
|
|
|
763
843
|
const atHeight = header.number.toNumber();
|
|
764
844
|
this.getVaultCosignSignature(utxoId, atHeight).then((signature) => {
|
|
765
845
|
if (signature) {
|
|
766
|
-
_optionalChain([unsub, 'optionalCall',
|
|
846
|
+
_optionalChain([unsub, 'optionalCall', _7 => _7()]);
|
|
767
847
|
clearTimeout(timeout);
|
|
768
848
|
resolve({ signature, blockHeight: atHeight });
|
|
769
849
|
}
|
|
@@ -773,7 +853,7 @@ var BitcoinLocks = class {
|
|
|
773
853
|
});
|
|
774
854
|
if (waitForSignatureMillis !== -1) {
|
|
775
855
|
timeout = setTimeout(() => {
|
|
776
|
-
_optionalChain([unsub, 'optionalCall',
|
|
856
|
+
_optionalChain([unsub, 'optionalCall', _8 => _8()]);
|
|
777
857
|
reject(new Error(`Timeout waiting for cosign signature for UTXO ID ${utxoId}`));
|
|
778
858
|
}, waitForSignatureMillis);
|
|
779
859
|
}
|
|
@@ -853,9 +933,8 @@ var BitcoinLocks = class {
|
|
|
853
933
|
return { tx, securityFee, txFee, canAfford, availableBalance, txFeePlusTip: txFee + tip };
|
|
854
934
|
}
|
|
855
935
|
async getBitcoinLockFromTxResult(txResult) {
|
|
856
|
-
|
|
857
|
-
const
|
|
858
|
-
const blockHeight = await client.at(blockHash).then((x) => x.query.system.number()).then((x) => x.toNumber());
|
|
936
|
+
await txResult.waitForFinalizedBlock;
|
|
937
|
+
const blockHeight = txResult.blockNumber;
|
|
859
938
|
const utxoId = await _asyncNullishCoalesce(await this.getUtxoIdFromEvents(txResult.events), async () => ( 0));
|
|
860
939
|
if (utxoId === 0) {
|
|
861
940
|
throw new Error("Bitcoin lock creation failed, no UTXO ID found in transaction events");
|
|
@@ -867,7 +946,7 @@ var BitcoinLocks = class {
|
|
|
867
946
|
return { lock, createdAtHeight: blockHeight, txResult };
|
|
868
947
|
}
|
|
869
948
|
async initializeLock(args) {
|
|
870
|
-
const { argonKeyring
|
|
949
|
+
const { argonKeyring } = args;
|
|
871
950
|
const client = this.client;
|
|
872
951
|
const { tx, securityFee, canAfford, txFeePlusTip } = await this.createInitializeLockTx(args);
|
|
873
952
|
if (!canAfford) {
|
|
@@ -876,16 +955,9 @@ var BitcoinLocks = class {
|
|
|
876
955
|
);
|
|
877
956
|
}
|
|
878
957
|
const submitter = new TxSubmitter(client, tx, argonKeyring);
|
|
879
|
-
const txResult = await submitter.submit({
|
|
880
|
-
waitForBlock: true,
|
|
881
|
-
logResults: true,
|
|
882
|
-
tip,
|
|
883
|
-
txProgressCallback
|
|
884
|
-
});
|
|
885
|
-
const { lock, createdAtHeight } = await this.getBitcoinLockFromTxResult(txResult);
|
|
958
|
+
const txResult = await submitter.submit({ logResults: true, ...args });
|
|
886
959
|
return {
|
|
887
|
-
|
|
888
|
-
createdAtHeight,
|
|
960
|
+
getLock: () => this.getBitcoinLockFromTxResult(txResult),
|
|
889
961
|
txResult,
|
|
890
962
|
securityFee
|
|
891
963
|
};
|
|
@@ -901,8 +973,7 @@ var BitcoinLocks = class {
|
|
|
901
973
|
priceIndex,
|
|
902
974
|
releaseRequest: { bitcoinNetworkFee, toScriptPubkey },
|
|
903
975
|
argonKeyring,
|
|
904
|
-
tip = 0n
|
|
905
|
-
txProgressCallback
|
|
976
|
+
tip = 0n
|
|
906
977
|
} = args;
|
|
907
978
|
if (!toScriptPubkey.startsWith("0x")) {
|
|
908
979
|
throw new Error("toScriptPubkey must be a hex string starting with 0x");
|
|
@@ -922,18 +993,10 @@ var BitcoinLocks = class {
|
|
|
922
993
|
`Insufficient funds to release lock. Available: ${formatArgons(canAfford.availableBalance)}, Required: ${formatArgons(redemptionPrice + canAfford.txFee + tip)}`
|
|
923
994
|
);
|
|
924
995
|
}
|
|
925
|
-
|
|
926
|
-
waitForBlock: true,
|
|
996
|
+
return submitter.submit({
|
|
927
997
|
logResults: true,
|
|
928
|
-
|
|
929
|
-
txProgressCallback
|
|
998
|
+
...args
|
|
930
999
|
});
|
|
931
|
-
const blockHash = await txResult.inBlockPromise;
|
|
932
|
-
const blockHeight = await client.at(blockHash).then((x) => x.query.system.number()).then((x) => x.toNumber());
|
|
933
|
-
return {
|
|
934
|
-
blockHash,
|
|
935
|
-
blockHeight
|
|
936
|
-
};
|
|
937
1000
|
}
|
|
938
1001
|
async releasePrice(priceIndex, lock) {
|
|
939
1002
|
return await this.getRedemptionRate(priceIndex, lock);
|
|
@@ -980,41 +1043,43 @@ var BitcoinLocks = class {
|
|
|
980
1043
|
)}`
|
|
981
1044
|
);
|
|
982
1045
|
}
|
|
983
|
-
const
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
(
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1046
|
+
const txResult = await txSubmitter.submit(args);
|
|
1047
|
+
const getRatchetResult = async () => {
|
|
1048
|
+
const blockHash = await txResult.waitForFinalizedBlock;
|
|
1049
|
+
const ratchetEvent = txResult.events.find(
|
|
1050
|
+
(x) => client.events.bitcoinLocks.BitcoinLockRatcheted.is(x)
|
|
1051
|
+
);
|
|
1052
|
+
if (!ratchetEvent) {
|
|
1053
|
+
throw new Error(`Ratchet event not found in transaction events`);
|
|
1054
|
+
}
|
|
1055
|
+
const api = await client.at(blockHash);
|
|
1056
|
+
const bitcoinBlockHeight = await api.query.bitcoinUtxos.confirmedBitcoinBlockTip().then((x) => x.unwrap().blockHeight.toNumber());
|
|
1057
|
+
const {
|
|
1058
|
+
amountBurned,
|
|
1059
|
+
liquidityPromised: liquidityPromisedRaw,
|
|
1060
|
+
newPeggedPrice,
|
|
1061
|
+
originalPeggedPrice,
|
|
1062
|
+
securityFee
|
|
1063
|
+
} = ratchetEvent.data;
|
|
1064
|
+
const liquidityPromised = liquidityPromisedRaw.toBigInt();
|
|
1065
|
+
let mintAmount = liquidityPromised;
|
|
1066
|
+
if (liquidityPromised > originalPeggedPrice.toBigInt()) {
|
|
1067
|
+
mintAmount -= originalPeggedPrice.toBigInt();
|
|
1068
|
+
}
|
|
1069
|
+
return {
|
|
1070
|
+
txFee: _nullishCoalesce(txResult.finalFee, () => ( 0n)),
|
|
1071
|
+
blockHeight: txResult.blockNumber,
|
|
1072
|
+
bitcoinBlockHeight,
|
|
1073
|
+
pendingMint: mintAmount,
|
|
1074
|
+
liquidityPromised,
|
|
1075
|
+
newPeggedPrice: newPeggedPrice.toBigInt(),
|
|
1076
|
+
burned: amountBurned.toBigInt(),
|
|
1077
|
+
securityFee: securityFee.toBigInt()
|
|
1078
|
+
};
|
|
1079
|
+
};
|
|
1009
1080
|
return {
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
pendingMint: mintAmount,
|
|
1013
|
-
liquidityPromised,
|
|
1014
|
-
newPeggedPrice: newPeggedPrice.toBigInt(),
|
|
1015
|
-
burned: amountBurned.toBigInt(),
|
|
1016
|
-
blockHeight,
|
|
1017
|
-
bitcoinBlockHeight
|
|
1081
|
+
txResult,
|
|
1082
|
+
getRatchetResult
|
|
1018
1083
|
};
|
|
1019
1084
|
}
|
|
1020
1085
|
};
|
|
@@ -1172,5 +1237,5 @@ async function getClient(host, options) {
|
|
|
1172
1237
|
|
|
1173
1238
|
|
|
1174
1239
|
|
|
1175
|
-
exports.BTreeMap = _typescodec.BTreeMap; exports.BitcoinLocks = BitcoinLocks; exports.Bool = _typescodec.Bool; exports.Bytes = _typescodec.Bytes; exports.Compact = _typescodec.Compact; exports.Enum = _typescodec.Enum; exports.ExtrinsicError =
|
|
1240
|
+
exports.BTreeMap = _typescodec.BTreeMap; exports.BitcoinLocks = BitcoinLocks; exports.Bool = _typescodec.Bool; exports.Bytes = _typescodec.Bytes; exports.Compact = _typescodec.Compact; exports.Enum = _typescodec.Enum; exports.ExtrinsicError = ExtrinsicError; exports.FIXED_U128_DECIMALS = FIXED_U128_DECIMALS; exports.GenericAddress = _generic.GenericAddress; exports.GenericBlock = _generic.GenericBlock; exports.GenericEvent = _generic.GenericEvent; exports.Keyring = _api.Keyring; exports.MICROGONS_PER_ARGON = MICROGONS_PER_ARGON; exports.Null = _typescodec.Null; exports.Option = _typescodec.Option; exports.PERMILL_DECIMALS = PERMILL_DECIMALS; exports.PriceIndex = PriceIndex; exports.Range = _typescodec.Range; exports.Result = _typescodec.Result; exports.SATS_PER_BTC = SATS_PER_BTC; exports.Struct = _typescodec.Struct; exports.Text = _typescodec.Text; exports.Tuple = _typescodec.Tuple; exports.TxResult = TxResult; exports.TxSubmitter = TxSubmitter; exports.U256 = _typescodec.U256; exports.U8aFixed = _typescodec.U8aFixed; exports.Vault = Vault; exports.Vec = _typescodec.Vec; exports.WageProtector = WageProtector; exports.bool = _typescodec.bool; exports.checkForExtrinsicSuccess = checkForExtrinsicSuccess; exports.createKeyringPair = createKeyringPair; exports.decodeAddress = _utilcrypto.decodeAddress; exports.dispatchErrorToExtrinsicError = dispatchErrorToExtrinsicError; exports.dispatchErrorToString = dispatchErrorToString; exports.formatArgons = formatArgons; exports.fromFixedNumber = fromFixedNumber; exports.getAuthorFromHeader = getAuthorFromHeader; exports.getClient = getClient; exports.getTickFromHeader = getTickFromHeader; exports.gettersToObject = gettersToObject; exports.hexToU8a = _util.hexToU8a; exports.i128 = _typescodec.i128; exports.keyringFromSuri = keyringFromSuri; exports.mnemonicGenerate = _utilcrypto.mnemonicGenerate; exports.toFixedNumber = toFixedNumber; exports.u128 = _typescodec.u128; exports.u16 = _typescodec.u16; exports.u32 = _typescodec.u32; exports.u64 = _typescodec.u64; exports.u8 = _typescodec.u8; exports.u8aEq = _util.u8aEq; exports.u8aToHex = _util.u8aToHex; exports.waitForLoad = waitForLoad;
|
|
1176
1241
|
//# sourceMappingURL=index.cjs.map
|