@ninsei-labs/sdk 0.56.1 → 0.57.2
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/CHANGELOG.md +13 -0
- package/LICENSE-POLICY.md +9 -2
- package/README.md +17 -4
- package/THIRD_PARTY_NOTICES.md +174 -0
- package/assets/vendors/MANIFEST.json +1 -5
- package/assets/vendors/sdk/sdk-browser.js +5 -4
- package/engine/atomic/dleq2.js +116 -15
- package/engine/atomic/order.js +21 -3
- package/engine/core/config.js +17 -24
- package/engine/core/deployments.generated.js +103 -0
- package/engine/core/swap.js +7 -7
- package/engine/evm/asyncRoute.js +1 -1
- package/engine/evm/claimGas.js +2 -1
- package/engine/evm/escrow.js +13 -8
- package/engine/evm/factory.js +11 -6
- package/engine/evm/funding.js +8 -3
- package/engine/evm/index.js +2 -4
- package/engine/evm/session.js +3 -3
- package/engine/monero/scan.js +101 -22
- package/engine/recovery/restore.js +8 -8
- package/engine/sdk/bridge.js +2 -5
- package/index.d.ts +48 -11
- package/package.json +4 -2
- package/src/actions.mjs +8 -5
- package/src/codes.mjs +5 -0
- package/src/config.mjs +1 -0
- package/src/engine.mjs +6 -6
- package/src/feeEstimate.mjs +11 -4
- package/src/http.mjs +4 -1
- package/src/index.mjs +1 -1
- package/src/legs/kyber.mjs +23 -0
- package/src/lock.mjs +1 -0
- package/src/order.mjs +108 -20
- package/src/primitives.mjs +4 -5
- package/src/probes.mjs +9 -3
- package/src/public/networks.d.ts +45 -0
- package/src/public/networks.mjs +10 -1
- package/src/public/xmr.mjs +1 -1
- package/src/quotes.mjs +97 -23
- package/src/recovery-key.mjs +32 -14
- package/src/relayFeatures.mjs +25 -13
- package/src/relayFirmClient.mjs +33 -7
- package/src/relayQuote.mjs +87 -10
- package/src/relayView.mjs +3 -3
- package/src/swap-flow.mjs +100 -58
- package/src/swaps.mjs +234 -64
- package/src/sweep-restore.mjs +8 -4
- package/src/sweep.mjs +5 -1
- package/src/version.mjs +1 -1
- package/src/xmr-arrival.mjs +55 -24
- package/assets/vendors/atomic/atomic-browser.js +0 -24
- package/engine/atomic/order-client.js +0 -56
- package/engine/atomic/order-worker.js +0 -156
- package/engine/evm/asyncExec.js +0 -577
- package/engine/evm/asyncLegs.js +0 -308
package/src/swap-flow.mjs
CHANGED
|
@@ -4,21 +4,21 @@
|
|
|
4
4
|
// the UI called a simulation while the funding module waited for ready-made sealedCommitment and sealedHalf. Now
|
|
5
5
|
// the order is fixed here, and there is no step that can be skipped by accident:
|
|
6
6
|
//
|
|
7
|
-
// 1) build your own side (the half + the proof)
|
|
7
|
+
// 1) build your own side (the half + the proof) with the engine's order builder;
|
|
8
8
|
// 2) assemble the counterparty's side (in the demo - a local stand-in, marked standIn);
|
|
9
9
|
// 3) CHECK the counterparty against the single mandatory condition - the DLEQ proof in the context of THIS
|
|
10
10
|
// order (there is no envelope in the scheme any more, doc 27) - before any locking: after it, that would no
|
|
11
11
|
// longer help. The item "the envelope that confirms the half" left together with the envelope itself;
|
|
12
|
-
// 4) obtain commitments on BOTH halves: our own is computed
|
|
13
|
-
//
|
|
12
|
+
// 4) obtain commitments on BOTH halves: our own is computed from our half, the counterparty's commitment
|
|
13
|
+
// arrives in the quote. There is no longer any need to encrypt the half
|
|
14
14
|
// and hand it to the counterparty: it is revealed on-chain at settlement (claim/refund);
|
|
15
15
|
// 5) and only now put the money in: with ONE createOrderAndFund transaction that creates the escrow together
|
|
16
16
|
// with the money (the "lock" step below). The former name lock(sealedHalf) lied twice here: the contract has
|
|
17
17
|
// no such function - the payable constructor moves the money - and there is no envelope in the order. Nor
|
|
18
18
|
// does the escrow have a separate funding function: an order is either born with money or not born at all.
|
|
19
19
|
//
|
|
20
|
-
// THE CRYPTOGRAPHY LIVES UNDER www/js/atomic/* (mirrored as ../engine/atomic/*) AND IS CALLED ONLY THROUGH
|
|
21
|
-
//
|
|
20
|
+
// THE CRYPTOGRAPHY LIVES UNDER www/js/atomic/* (mirrored as ../engine/atomic/*) AND IS CALLED ONLY THROUGH
|
|
21
|
+
// THE IN-PROCESS ORDER BUILDER. There is no crypto here - here is the order.
|
|
22
22
|
//
|
|
23
23
|
// WHY IT IS IN THE PACKAGE NOW. The page used to import this module straight from www/js/core/swap-flow.js; the
|
|
24
24
|
// core (www/js, mirrored under ../engine/) was the second home of the deal flow. That is exactly the debt
|
|
@@ -32,6 +32,11 @@ import * as engine from "./engine.mjs";
|
|
|
32
32
|
// reported in. It is its own module because TWO surfaces ask the same question (this flow and the swaps watch)
|
|
33
33
|
// and a second implementation of the same event would be a second truth about it.
|
|
34
34
|
import { declinedFromLogs, logReaderOf, blockBoundaryOf, DECLINED_TOPIC0 } from "./decline.mjs";
|
|
35
|
+
// THE ORDER SIDE IS BUILT IN-PROCESS, by the same engine builder the record and the facade use. The old
|
|
36
|
+
// browser worker shipped its own copy of the crypto bundle. Its verifySide checked the DLEQ equation but not
|
|
37
|
+
// that the proven point matched the quote's commitment; the package no longer needs a second build of the same
|
|
38
|
+
// primitives.
|
|
39
|
+
import { buildOwnSideChunked, orderStepChunked } from "./order.mjs";
|
|
35
40
|
|
|
36
41
|
// THE ENGINE SURFACES, TAKEN THROUGH THE ONE SEAM (./engine.mjs) - exactly as the rest of the core does.
|
|
37
42
|
const { chainById, DEFAULT_CHAIN, MONERO } = engine.config;
|
|
@@ -42,9 +47,8 @@ const {
|
|
|
42
47
|
} = engine.escrow;
|
|
43
48
|
// THE COUNTERPARTY GUARDS: whether a stand-in may be signed on this chain.
|
|
44
49
|
const { liveOrderSlotsVerdict, counterpartyVerdict } = engine.guards;
|
|
45
|
-
// THE
|
|
46
|
-
//
|
|
47
|
-
const { createOrderWorker } = engine.orderClient;
|
|
50
|
+
// THE ORDER CONTEXT, THE ORDER QUOTE AND THE SHARED CLAIM GAS LIMIT - all through the mirror, so the page and
|
|
51
|
+
// the package never keep a second copy of these rules.
|
|
48
52
|
const { orderContextString } = engine.orderContext;
|
|
49
53
|
const { requestOrderQuote, checkOrderQuoteShape } = engine.rfqSource;
|
|
50
54
|
const { CLAIM_GAS_LIMIT } = engine.claimGas;
|
|
@@ -72,15 +76,13 @@ export async function runAtomicOrder({
|
|
|
72
76
|
chainSlug, locker, claimer, amountWei, amountLabel, providerId = null,
|
|
73
77
|
xmrNetwork = null,
|
|
74
78
|
claimWindowSeconds = 3600, readyWindowSeconds = 14400, salt: providedSalt, onStage,
|
|
75
|
-
// THE SUBSTITUTION POINTS FOR THE SAKE OF THE CHECK, and they are not decoration. The live flow
|
|
76
|
-
//
|
|
77
|
-
//
|
|
78
|
-
// signature did not accept the mandatory fields, and after the fix a reference to a variable outside its scope
|
|
79
|
-
// remained - such things are visible only in operation. The defaults here are real: substitution is only in
|
|
80
|
-
// checks.
|
|
79
|
+
// THE SUBSTITUTION POINTS FOR THE SAKE OF THE CHECK, and they are not decoration. The live flow reaches a
|
|
80
|
+
// wallet and a chain; the check drives the same sequence with synthetic sides. The defaults here are real:
|
|
81
|
+
// substitution is only in checks.
|
|
81
82
|
deps = {},
|
|
82
83
|
}) {
|
|
83
|
-
const
|
|
84
|
+
const buildSide = deps.buildOwnSide || buildOwnSideChunked;
|
|
85
|
+
const stepOrder = deps.orderStep || orderStepChunked;
|
|
84
86
|
const fundLive = deps.fundOrderLive || fundOrderLive;
|
|
85
87
|
const askQuote = deps.requestOrderQuote || requestOrderQuote;
|
|
86
88
|
const factory = escrowFactoryAddress(chainSlug);
|
|
@@ -124,9 +126,8 @@ export async function runAtomicOrder({
|
|
|
124
126
|
// commitments and BOTH public ed25519 points, and the half is published by the settlement itself
|
|
125
127
|
// (claim/refund). The secret that used to be placed in the recovery file is now MY HALF: it is exactly what is
|
|
126
128
|
// needed to claim the XMR.
|
|
127
|
-
const worker = makeWorker();
|
|
128
129
|
const say = (stage, detail) => onStage?.(stage, detail);
|
|
129
|
-
|
|
130
|
+
{
|
|
130
131
|
// THE QUOTE FIRST, THEN YOUR OWN SIDE. The order of steps is set by the fact that the claimer is named by the
|
|
131
132
|
// provider: the half binding includes the claimer's address, so our own half must be computed over the
|
|
132
133
|
// context that is already fully known. It used to be the other way round, and the provider got an order with
|
|
@@ -157,7 +158,15 @@ export async function runAtomicOrder({
|
|
|
157
158
|
const orderContext = orderContextString(order);
|
|
158
159
|
|
|
159
160
|
say("half", "side");
|
|
160
|
-
|
|
161
|
+
// THE PROOF AND THE VERIFICATION YIELD BETWEEN CHUNKS. `runAtomicOrder` is already async, so the browser can
|
|
162
|
+
// paint while the same engine builder does the arithmetic.
|
|
163
|
+
const chunkOptions = {
|
|
164
|
+
onBit: (bit, total) => say("proof", `proving DLEQ proof: ${bit} of ${total} bits`),
|
|
165
|
+
onProgress: (step) => say("verify", step.phase === "sum"
|
|
166
|
+
? `summing commitments: ${step.done} of ${step.total}`
|
|
167
|
+
: `verifying DLEQ proof: ${step.done} of ${step.total}`),
|
|
168
|
+
};
|
|
169
|
+
const mine = await buildSide(order, chunkOptions);
|
|
161
170
|
say("half", "side-ready");
|
|
162
171
|
|
|
163
172
|
// The counterparty stand-in. Honestly: only a real counterparty with its own key gives a guarantee; a
|
|
@@ -193,9 +202,7 @@ export async function runAtomicOrder({
|
|
|
193
202
|
// THE STAND-IN IS COMPUTED OVER THE SAME CONTEXT AS THE ORDER. It used to append ":counterparty" to the
|
|
194
203
|
// string, and that turned the check into a ritual: the stand-in's proof was bound to a different string,
|
|
195
204
|
// that is, to nothing. Now exactly the path a real provider would take is checked.
|
|
196
|
-
|
|
197
|
-
counter = { proof: mock.proof, edPub: mock.pub.ed, mock, viewHalf: mock.viewHalf, viewPoint: mock.viewPub,
|
|
198
|
-
commitHalfClaimer: (await worker.halfCommitment(mock.half)).commitment };
|
|
205
|
+
counter = null;
|
|
199
206
|
}
|
|
200
207
|
// THE COUNTERPARTY CHECK REMAINS ONE AND IT IS THE MAIN ONE: the DLEQ proof in the context of this order.
|
|
201
208
|
// It confirms that both public points stand for ONE scalar - that is, that the half declared for the Monero
|
|
@@ -204,16 +211,18 @@ export async function runAtomicOrder({
|
|
|
204
211
|
// TOGETHER WITH IT, the fact is checked that the proven ed25519 point IS THE VERY ONE the contract will
|
|
205
212
|
// receive: without that check a correct proof would guarantee nothing about the chain.
|
|
206
213
|
say("verify", "dleq");
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
214
|
+
const step = await stepOrder({
|
|
215
|
+
order, side: mine.side, counterparty: counter, standIn: !quote,
|
|
216
|
+
network: xmrNetwork || (chain.monero && chain.monero.networkType) || MONERO.networkType,
|
|
217
|
+
options: chunkOptions,
|
|
218
|
+
});
|
|
219
|
+
const accepted = step.counterparty;
|
|
211
220
|
|
|
212
|
-
// COMMITMENTS ON BOTH HALVES: our own
|
|
213
|
-
//
|
|
214
|
-
//
|
|
215
|
-
const commitHalfLocker =
|
|
216
|
-
const commitHalfClaimer =
|
|
221
|
+
// COMMITMENTS ON BOTH HALVES: our own and the counterparty's are the ones the proof was verified against,
|
|
222
|
+
// by the same builder the contract and the recovery file use. Exactly these values are checked by the
|
|
223
|
+
// contract in claim and refund and are included in termsHash.
|
|
224
|
+
const commitHalfLocker = step.wallet.commitHalfLocker;
|
|
225
|
+
const commitHalfClaimer = step.wallet.commitHalfClaimer;
|
|
217
226
|
|
|
218
227
|
// THE SHARED MONERO ADDRESS - FROM THE HALVES, AND IT IS COMPUTED BEFORE THE MONEY IS LOCKED. The address used
|
|
219
228
|
// to be assembled from the maker's seed, that is, from the FULL key on its side: that is a custodial scheme,
|
|
@@ -223,16 +232,12 @@ export async function runAtomicOrder({
|
|
|
223
232
|
// We compute it BEFORE funding on purpose: the address is derived from the same points that will go into the
|
|
224
233
|
// contract, and if it cannot be assembled (the counterparty has neither a point nor a view half), we must
|
|
225
234
|
// learn about it before the user signs the deposit.
|
|
226
|
-
const otherViewPoint =
|
|
235
|
+
const otherViewPoint = step.wallet.otherViewPoint;
|
|
227
236
|
if (!otherViewPoint) {
|
|
228
237
|
throw new SdkError("quote-refused", { step: "view-half" });
|
|
229
238
|
}
|
|
230
239
|
say("address", "joint-address");
|
|
231
|
-
const joint =
|
|
232
|
-
ownSpendHalf: mine.half, ownViewHalf: mine.viewHalf,
|
|
233
|
-
otherSpendPoint: counter.edPub, otherViewPoint,
|
|
234
|
-
network: xmrNetwork || (chain.monero && chain.monero.networkType) || MONERO.networkType,
|
|
235
|
-
});
|
|
240
|
+
const joint = { address: step.wallet.address, spendPub: step.wallet.spendPub, viewPub: step.wallet.viewPub };
|
|
236
241
|
|
|
237
242
|
// THE COUNTERPARTY STAND-IN ON A LIVE NETWORK IS A REFUSAL, NOT A SIGNATURE (the auditor's finding, review_02
|
|
238
243
|
// section 6.3 and block D1, item 5). Without a provider the counterparty side is assembled locally, the proof
|
|
@@ -271,10 +276,10 @@ export async function runAtomicOrder({
|
|
|
271
276
|
// The ed25519 points go into the order explicitly: the other side assembles the shared address from them
|
|
272
277
|
// and checks the proof EXACTLY against the on-chain fields, not against what arrived in the quote (doc 27
|
|
273
278
|
// section 11).
|
|
274
|
-
edPointLocker:
|
|
279
|
+
edPointLocker: step.own.spendPoint, edPointClaimer: accepted.edPub,
|
|
275
280
|
// THE DEPOSITOR'S VIEW POINT: from it the claimer assembles the address FROM THE ORDER FIELDS. Without it
|
|
276
281
|
// it does not know where to send the XMR - and learns about it only after the money is deposited.
|
|
277
|
-
edViewPointLocker:
|
|
282
|
+
edViewPointLocker: step.own.viewPoint,
|
|
278
283
|
salt, // our swap salt: see funding.js
|
|
279
284
|
amountLabel, // the ready amount string for the label: formatting lives in the UI
|
|
280
285
|
amountWei,
|
|
@@ -296,19 +301,19 @@ export async function runAtomicOrder({
|
|
|
296
301
|
feeWei: (funded && funded.feeWei) || 0n,
|
|
297
302
|
feeTotalWei: (funded && funded.feeTotalWei) || null,
|
|
298
303
|
feeBps: (funded && funded.feeBps) || 0,
|
|
299
|
-
half:
|
|
300
|
-
counterHalf: standIn ?
|
|
304
|
+
half: step.side.half,
|
|
305
|
+
counterHalf: standIn ? accepted.half : null,
|
|
301
306
|
// THE VIEW HALVES: our own and the counterparty's. Without them the address cannot be assembled and the
|
|
302
307
|
// version 3 recovery file cannot be filled in, and without the file the user will not claim the XMR after
|
|
303
308
|
// losing the tab.
|
|
304
|
-
viewHalf:
|
|
309
|
+
viewHalf: step.side.viewHalf,
|
|
305
310
|
commitHalfLocker,
|
|
306
311
|
commitHalfClaimer,
|
|
307
|
-
edPointLocker:
|
|
308
|
-
edPointClaimer:
|
|
312
|
+
edPointLocker: step.own.spendPoint,
|
|
313
|
+
edPointClaimer: accepted.edPub,
|
|
309
314
|
// THE DEPOSITOR'S VIEW POINT goes into the record: without it there is nothing to compare the chain slot
|
|
310
315
|
// edViewPointLocker against before signing - and it is part of the address the claimer will see.
|
|
311
|
-
edViewPointLocker:
|
|
316
|
+
edViewPointLocker: step.own.viewPoint,
|
|
312
317
|
// THE COUNTERPARTY'S VIEW HALF: the stand-in has it, a real provider does NOT, and this is a known gap of
|
|
313
318
|
// the protocol, not a trifle: without its view half the shared Monero address cannot be assembled. While
|
|
314
319
|
// the address in the application is built the previous way, the path does not break, but a quote under the
|
|
@@ -316,7 +321,7 @@ export async function runAtomicOrder({
|
|
|
316
321
|
// ONE KIND OF HALF ON THE OUTPUT, and this is not pedantry: the stand-in's half is a number, the provider's
|
|
317
322
|
// arrives as a string, and the consumer (the recovery file and the sweep) must not guess which it was
|
|
318
323
|
// given. Next to the spend half, which is also a number.
|
|
319
|
-
counterViewHalf:
|
|
324
|
+
counterViewHalf: accepted.viewHalf === null || accepted.viewHalf === undefined ? null : BigInt(accepted.viewHalf),
|
|
320
325
|
counterViewPoint: otherViewPoint,
|
|
321
326
|
// THE SHARED ADDRESS: where the maker sends the XMR. Nobody can spend from it before the half is revealed.
|
|
322
327
|
moneroAddress: joint.address,
|
|
@@ -329,8 +334,6 @@ export async function runAtomicOrder({
|
|
|
329
334
|
// order_ref is empty (the regular payout does not reach the order).
|
|
330
335
|
quoteId: (quote && quote.id) || null,
|
|
331
336
|
};
|
|
332
|
-
} finally {
|
|
333
|
-
worker.stop();
|
|
334
337
|
}
|
|
335
338
|
}
|
|
336
339
|
|
|
@@ -356,7 +359,7 @@ export async function orderStatus(escrow, deps = {}) {
|
|
|
356
359
|
// the engine read. The page goes through the bridge, which hands in the wallet's own reader, so the state is
|
|
357
360
|
// read by the same wallet that will sign.
|
|
358
361
|
const call = deps.call || readContract;
|
|
359
|
-
const raw = await call({ to: escrow, data: encodeStatus() });
|
|
362
|
+
const raw = await call({ to: escrow, data: encodeStatus(), block: deps.blockTag || "latest" });
|
|
360
363
|
return decodeStatus(raw);
|
|
361
364
|
}
|
|
362
365
|
|
|
@@ -395,13 +398,14 @@ export async function orderDeadlines(escrow, deps = {}) {
|
|
|
395
398
|
// THE SAME READER SEAM AS orderStatus: the deadline must be read by the wallet that will sign, not by a second
|
|
396
399
|
// provider.
|
|
397
400
|
const call = deps.call || readContract;
|
|
401
|
+
const block = deps.blockTag || "latest";
|
|
398
402
|
const [raw1, rawH, rawReadyBy] = await Promise.all([
|
|
399
|
-
call({ to: escrow, data: encodeT1() }),
|
|
400
|
-
call({ to: escrow, data: encodeTermsHash() }),
|
|
403
|
+
call({ to: escrow, data: encodeT1(), block }),
|
|
404
|
+
call({ to: escrow, data: encodeTermsHash(), block }),
|
|
401
405
|
// THE READY-MARK DEADLINE IS READ HERE TOO: without it the screen cannot say that the person is already in
|
|
402
|
-
// the dead zone (the mark is closed,
|
|
406
|
+
// the dead zone (the mark is closed, the claim is open, refunding is not open yet) - and that is exactly
|
|
403
407
|
// where the deal hangs silently.
|
|
404
|
-
call({ to: escrow, data: encodeRead("readyBy") }),
|
|
408
|
+
call({ to: escrow, data: encodeRead("readyBy"), block }),
|
|
405
409
|
]);
|
|
406
410
|
// An empty eth_call response means there is NO CODE at the address: the contract is not deployed. This happens
|
|
407
411
|
// when the createOrder transaction did not go through (for example the wallet rejected it over the fee) while
|
|
@@ -433,7 +437,8 @@ export async function orderLiveSlots(escrow, deps = {}) {
|
|
|
433
437
|
if (!escrow) throw new SdkError("bad-input", { field: "escrow" });
|
|
434
438
|
const keys = ["edPointLocker", "edPointClaimer", "edViewPointLocker", "commitHalfLocker", "commitHalfClaimer"];
|
|
435
439
|
try {
|
|
436
|
-
const
|
|
440
|
+
const block = deps.blockTag || "latest";
|
|
441
|
+
const raws = await Promise.all(keys.map((k) => call({ to: escrow, data: encodeRead(k), block })));
|
|
437
442
|
const slots = {};
|
|
438
443
|
keys.forEach((k, i) => { slots[k] = decodeBytes32(raws[i], k + "()"); });
|
|
439
444
|
return { slots };
|
|
@@ -471,24 +476,58 @@ async function assertOrderOpen(escrow, what, deps) {
|
|
|
471
476
|
async function assertActionWindow(escrow, what, deps, now = null) {
|
|
472
477
|
if (typeof deps.blockTime !== "function") return;
|
|
473
478
|
const call = deps.call || readContract;
|
|
479
|
+
const block = deps.blockTag || "latest";
|
|
474
480
|
const [readyRaw, deadlines] = await Promise.all([
|
|
475
|
-
call({ to: escrow, data: encodeRead("ready") }),
|
|
481
|
+
call({ to: escrow, data: encodeRead("ready"), block }),
|
|
476
482
|
orderDeadlines(escrow, deps),
|
|
477
483
|
]);
|
|
478
484
|
const marked = BigInt(readyRaw) !== 0n;
|
|
479
485
|
const at = now === null ? Number(await deps.blockTime()) : Number(now);
|
|
480
|
-
|
|
486
|
+
// A HALF-REVEALING SEND NEEDS A READABLE CLOCK. Without it the deadline rule would be skipped, and a claim or
|
|
487
|
+
// refund that lands after its window still publishes the half. The actions facade always brings this seam.
|
|
488
|
+
if (!Number.isSafeInteger(at) || at <= 0) {
|
|
489
|
+
throw new SdkError("contract-reverted", { step: what, why: "clock-unreadable" });
|
|
490
|
+
}
|
|
491
|
+
// A TRANSACTION LANDS IN A LATER BLOCK. Keep a margin before every deadline so a send that is valid when signed
|
|
492
|
+
// does not become a revert that publishes the half.
|
|
493
|
+
const margin = 60;
|
|
481
494
|
if (what === "claim") {
|
|
482
|
-
if (at >= Number(deadlines.t1)) throw new SdkError("contract-reverted", { step: what, why: "window-closed" });
|
|
495
|
+
if (at + margin >= Number(deadlines.t1)) throw new SdkError("contract-reverted", { step: what, why: "window-closed" });
|
|
483
496
|
if (!marked && at < Number(deadlines.readyBy)) throw new SdkError("contract-reverted", { step: what, why: "not-ready" });
|
|
484
497
|
} else if (what === "refund") {
|
|
485
498
|
// refund(): before t1 it is allowed only while the mark is absent and readyBy has not passed.
|
|
486
|
-
if (at < Number(deadlines.t1) && (at >= Number(deadlines.readyBy) || marked)) {
|
|
499
|
+
if (at < Number(deadlines.t1) && (at + margin >= Number(deadlines.readyBy) || marked)) {
|
|
487
500
|
throw new SdkError("contract-reverted", { step: what, why: "too-early" });
|
|
488
501
|
}
|
|
489
502
|
} else if (what === "mark-ready") {
|
|
490
503
|
if (marked) throw new SdkError("contract-reverted", { step: what, why: "already-ready" });
|
|
491
|
-
if (at >= Number(deadlines.readyBy)) throw new SdkError("contract-reverted", { step: what, why: "ready-window-closed" });
|
|
504
|
+
if (at + margin >= Number(deadlines.readyBy)) throw new SdkError("contract-reverted", { step: what, why: "ready-window-closed" });
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// THE ACTION IS EXECUTED ON PAPER BEFORE IT IS SIGNED. `eth_call` runs the same call against the latest state
|
|
509
|
+
// from the same sender, so a revert becomes a named refusal instead of a mined transaction that changed nothing.
|
|
510
|
+
// The guards above answer the questions we know how to ask; this answers the contract itself, including its own
|
|
511
|
+
// deadlines and the half checks. A caller without a call seam keeps the old behaviour.
|
|
512
|
+
async function simulateAction(escrow, data, what, deps) {
|
|
513
|
+
const call = deps.call;
|
|
514
|
+
if (typeof call !== "function") return;
|
|
515
|
+
try {
|
|
516
|
+
await call({ to: escrow, data, from: deps.from });
|
|
517
|
+
} catch (error) {
|
|
518
|
+
const detail = error && (error.data || error.message);
|
|
519
|
+
const message = String((error && error.message) || "");
|
|
520
|
+
// A transport or wallet failure is not a revert. Keep it as it came: only a contract answer is turned into
|
|
521
|
+
// this action's refusal.
|
|
522
|
+
const reverted = (error && error.code === "contract-reverted")
|
|
523
|
+
|| (error && typeof error.data === "string" && error.data.startsWith("0x"))
|
|
524
|
+
|| /execution reverted|revert/i.test(message);
|
|
525
|
+
if (!reverted) throw error;
|
|
526
|
+
throw new SdkError("contract-reverted", {
|
|
527
|
+
step: what,
|
|
528
|
+
why: "simulation",
|
|
529
|
+
detail: typeof detail === "string" ? detail.slice(0, 200) : null,
|
|
530
|
+
});
|
|
492
531
|
}
|
|
493
532
|
}
|
|
494
533
|
|
|
@@ -508,6 +547,7 @@ export async function claimOrder({ escrow, halfClaimer, expect, onStep, deps = {
|
|
|
508
547
|
await assertOrderOpen(escrow, "claim", deps);
|
|
509
548
|
await assertActionWindow(escrow, "claim", deps);
|
|
510
549
|
await assertOrderSlots(escrow, expect, "claim", deps);
|
|
550
|
+
await simulateAction(escrow, encodeClaim(halfClaimer), "claim", deps);
|
|
511
551
|
onStep?.("confirm-claim");
|
|
512
552
|
// THE CLAIM GAS LIMIT COMES FROM THE SHARED MODULE, NOT A LITERAL HERE (issue #127): the node lays exactly
|
|
513
553
|
// this limit into the gas gift. It is set explicitly and with a margin: claim has the same trap as order
|
|
@@ -535,6 +575,7 @@ export async function refundOrder({ escrow, halfLocker, onStep, expect = null, d
|
|
|
535
575
|
// for the same reason. Expectations are optional for compatibility with previous calls (the page calls without
|
|
536
576
|
// them); the core always passes them.
|
|
537
577
|
if (expect) await assertOrderSlots(escrow, expect, "refund", deps);
|
|
578
|
+
await simulateAction(escrow, encodeRefund(halfLocker), "refund", deps);
|
|
538
579
|
onStep?.("confirm-refund");
|
|
539
580
|
const hash = await (deps.send || sendTransaction)({ to: escrow, data: encodeRefund(halfLocker), gas: 250_000n });
|
|
540
581
|
return { hash };
|
|
@@ -552,6 +593,7 @@ export async function markReadyOrder({ escrow, expect, onStep, deps = {} }) {
|
|
|
552
593
|
await assertOrderOpen(escrow, "mark-ready", deps);
|
|
553
594
|
await assertActionWindow(escrow, "mark-ready", deps);
|
|
554
595
|
await assertOrderSlots(escrow, expect, "mark-ready", deps);
|
|
596
|
+
await simulateAction(escrow, encodeMarkReady(), "mark-ready", deps);
|
|
555
597
|
onStep?.("confirm-mark-ready");
|
|
556
598
|
const hash = await (deps.send || sendTransaction)({ to: escrow, data: encodeMarkReady(), gas: 120_000n });
|
|
557
599
|
return { hash };
|