@anywalletkit/protocol-swap2-core 0.6.1 → 0.7.0
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/README.md +1 -1
- package/dist/chunk-2KC734UX.js +117 -0
- package/dist/chunk-2KC734UX.js.map +1 -0
- package/dist/{chunk-IGTMUTNJ.js → chunk-5E5FWYZQ.js} +123 -6
- package/dist/chunk-5E5FWYZQ.js.map +1 -0
- package/dist/{chunk-ZMWK3RQ4.js → chunk-CT2REW2V.js} +39 -5
- package/dist/chunk-CT2REW2V.js.map +1 -0
- package/dist/{chunk-JFGMF6FO.js → chunk-U653ZREM.js} +9 -3
- package/dist/chunk-U653ZREM.js.map +1 -0
- package/dist/{chunk-KIAQXIBG.js → chunk-WBYCNN6S.js} +16 -2
- package/dist/chunk-WBYCNN6S.js.map +1 -0
- package/dist/index.cjs +243 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +10 -6
- package/dist/remote.cjs +37 -3
- package/dist/remote.cjs.map +1 -1
- package/dist/remote.d.cts +26 -1
- package/dist/remote.d.ts +26 -1
- package/dist/remote.js +2 -2
- package/dist/server.cjs +23 -4
- package/dist/server.cjs.map +1 -1
- package/dist/server.js +6 -5
- package/dist/server.js.map +1 -1
- package/dist/swap2/base.cjs +180 -15
- package/dist/swap2/base.cjs.map +1 -1
- package/dist/swap2/base.d.cts +19 -1
- package/dist/swap2/base.d.ts +19 -1
- package/dist/swap2/base.js +2 -1
- package/dist/swap2/single.cjs +198 -15
- package/dist/swap2/single.cjs.map +1 -1
- package/dist/swap2/single.js +4 -3
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +32 -1
- package/dist/types.d.ts +32 -1
- package/dist/util.cjs +70 -2
- package/dist/util.cjs.map +1 -1
- package/dist/util.d.cts +13 -2
- package/dist/util.d.ts +13 -2
- package/dist/util.js +7 -3
- package/dist/validate.cjs +14 -0
- package/dist/validate.cjs.map +1 -1
- package/dist/validate.d.cts +10 -2
- package/dist/validate.d.ts +10 -2
- package/dist/validate.js +3 -1
- package/package.json +4 -4
- package/dist/chunk-IGTMUTNJ.js.map +0 -1
- package/dist/chunk-JFGMF6FO.js.map +0 -1
- package/dist/chunk-KIAQXIBG.js.map +0 -1
- package/dist/chunk-MNJTMSFN.js +0 -49
- package/dist/chunk-MNJTMSFN.js.map +0 -1
- package/dist/chunk-ZMWK3RQ4.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -51,12 +51,14 @@ __export(src_exports, {
|
|
|
51
51
|
nativeUnits: () => nativeUnits,
|
|
52
52
|
ownerAssets: () => ownerAssets,
|
|
53
53
|
redactUrls: () => redactUrls,
|
|
54
|
+
remotePinRefusal: () => remotePinRefusal,
|
|
54
55
|
remoteProvider: () => remoteProvider,
|
|
55
56
|
shortAddress: () => shortAddress,
|
|
56
57
|
slippageClassOf: () => slippageClassOf,
|
|
57
58
|
sourceAssetOf: () => sourceAssetOf,
|
|
58
59
|
suggestSlippage: () => suggestSlippage,
|
|
59
60
|
swap2ChainId: () => swap2ChainId,
|
|
61
|
+
tonRawAddress: () => tonRawAddress,
|
|
60
62
|
validQuote: () => validQuote
|
|
61
63
|
});
|
|
62
64
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -157,6 +159,19 @@ function validQuote(q, params, provider) {
|
|
|
157
159
|
return false;
|
|
158
160
|
}
|
|
159
161
|
}
|
|
162
|
+
function remotePinRefusal(provider, q, chain) {
|
|
163
|
+
if (provider.remote !== true || !chain.startsWith("eip155:")) return null;
|
|
164
|
+
const tx = q.txRequest;
|
|
165
|
+
if (!tx || tx.serializedTx !== void 0) return null;
|
|
166
|
+
const pinned = (list, address) => (list ?? []).some((a) => typeof a === "string" && a.toLowerCase() === address.toLowerCase());
|
|
167
|
+
if (tx.approvalAddress && !pinned(provider.spenders?.(chain), tx.approvalAddress)) {
|
|
168
|
+
return `${provider.name}: the route approves ${tx.approvalAddress}, which the app did not pin as a spender on ${chain} (remoteProvider spenders)`;
|
|
169
|
+
}
|
|
170
|
+
if (tx.data !== void 0 && tx.to && !pinned(provider.callTargets?.(chain), tx.to)) {
|
|
171
|
+
return `${provider.name}: the route calls ${tx.to}, which the app did not pin as a call target on ${chain} (remoteProvider callTargets)`;
|
|
172
|
+
}
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
160
175
|
|
|
161
176
|
// src/util.ts
|
|
162
177
|
var import_swap2_assets = require("@anywalletkit/swap2-assets");
|
|
@@ -189,20 +204,90 @@ function anySignal(signals) {
|
|
|
189
204
|
}
|
|
190
205
|
return ctrl.signal;
|
|
191
206
|
}
|
|
207
|
+
var SCHEME_MAX = 32;
|
|
208
|
+
var isLetter = (c) => c >= 97 && c <= 122 || c >= 65 && c <= 90;
|
|
209
|
+
var isSchemeChar = (c) => isLetter(c) || c >= 48 && c <= 57 || c === 43 || c === 45 || c === 46;
|
|
210
|
+
var endsUrl = (c) => c === 34 || c === 39 || c === 60 || c === 62 || c >= 9 && c <= 13 || c === 32 || c === 160 || c === 5760 || c >= 8192 && c <= 8202 || c === 8232 || c === 8233 || c === 8239 || c === 8287 || c === 12288 || c === 65279;
|
|
192
211
|
function redactUrls(text) {
|
|
193
|
-
|
|
212
|
+
let out = "";
|
|
213
|
+
let done = 0;
|
|
214
|
+
for (let at = text.indexOf("://"); at !== -1; at = text.indexOf("://", at + 3)) {
|
|
215
|
+
let start = at;
|
|
216
|
+
while (start > done && at - start < SCHEME_MAX && isSchemeChar(text.charCodeAt(start - 1))) start--;
|
|
217
|
+
while (start < at && !isLetter(text.charCodeAt(start))) start++;
|
|
218
|
+
if (start === at) continue;
|
|
219
|
+
let end = at + 3;
|
|
220
|
+
while (end < text.length && !endsUrl(text.charCodeAt(end))) end++;
|
|
221
|
+
if (end === at + 3) continue;
|
|
222
|
+
out += `${text.slice(done, start)}<url>`;
|
|
223
|
+
done = end;
|
|
224
|
+
at = end - 3;
|
|
225
|
+
}
|
|
226
|
+
return done === 0 ? text : out + text.slice(done);
|
|
194
227
|
}
|
|
195
228
|
function sourceAssetOf(params) {
|
|
196
229
|
const decimals = params.sourceDecimals;
|
|
197
230
|
return (0, import_swap2_assets.resolveAsset)(params.sourceChain, params.sourceToken, decimals === void 0 ? {} : { allowUnlisted: true, decimals });
|
|
198
231
|
}
|
|
232
|
+
var B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
233
|
+
function base64Bytes(text) {
|
|
234
|
+
const s = text.replace(/-/g, "+").replace(/_/g, "/").replace(/=+$/, "");
|
|
235
|
+
if (!/^[A-Za-z0-9+/]*$/.test(s) || s.length % 4 === 1) return null;
|
|
236
|
+
const out = new Uint8Array(Math.floor(s.length * 3 / 4));
|
|
237
|
+
let bits = 0, acc = 0, n = 0;
|
|
238
|
+
for (const ch of s) {
|
|
239
|
+
acc = acc << 6 | B64.indexOf(ch);
|
|
240
|
+
bits += 6;
|
|
241
|
+
if (bits >= 8) {
|
|
242
|
+
bits -= 8;
|
|
243
|
+
out[n++] = acc >> bits & 255;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return out.subarray(0, n);
|
|
247
|
+
}
|
|
248
|
+
function crc16(bytes) {
|
|
249
|
+
let crc = 0;
|
|
250
|
+
for (const b of bytes) {
|
|
251
|
+
crc ^= b << 8;
|
|
252
|
+
for (let i = 0; i < 8; i++) crc = crc & 32768 ? (crc << 1 ^ 4129) & 65535 : crc << 1 & 65535;
|
|
253
|
+
}
|
|
254
|
+
return crc;
|
|
255
|
+
}
|
|
256
|
+
function tonRawAddress(address) {
|
|
257
|
+
if (address && typeof address === "object") {
|
|
258
|
+
const o = address;
|
|
259
|
+
if (typeof o.toRawString === "function") {
|
|
260
|
+
try {
|
|
261
|
+
return tonRawAddress(o.toRawString());
|
|
262
|
+
} catch {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (typeof o.workChain === "number" && o.hash instanceof Uint8Array && o.hash.length === 32) {
|
|
267
|
+
return `${o.workChain}:${Array.from(o.hash, (b) => b.toString(16).padStart(2, "0")).join("")}`;
|
|
268
|
+
}
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
if (typeof address !== "string") return null;
|
|
272
|
+
const raw = /^(-?[0-9]{1,3}):([0-9a-fA-F]{64})$/.exec(address.trim());
|
|
273
|
+
if (raw) return `${Number(raw[1])}:${raw[2].toLowerCase()}`;
|
|
274
|
+
if (!/^[A-Za-z0-9+/_-]{48}$/.test(address.trim())) return null;
|
|
275
|
+
const bytes = base64Bytes(address.trim());
|
|
276
|
+
if (!bytes || bytes.length !== 36 || crc16(bytes.subarray(0, 34)) !== (bytes[34] << 8 | bytes[35])) return null;
|
|
277
|
+
const wc = bytes[1] === 255 ? -1 : bytes[1];
|
|
278
|
+
return `${wc}:${Array.from(bytes.subarray(2, 34), (b) => b.toString(16).padStart(2, "0")).join("")}`;
|
|
279
|
+
}
|
|
199
280
|
|
|
200
281
|
// src/remote.ts
|
|
201
282
|
function remoteProvider(opts) {
|
|
202
283
|
const name = opts.name;
|
|
203
284
|
if (!/^[A-Za-z0-9_-]+$/.test(name)) throw new Error("swap2-remote: invalid provider name");
|
|
204
|
-
const baseUrl = opts.baseUrl.replace(/\/+$/, "");
|
|
285
|
+
const baseUrl = opts.baseUrl.trim().replace(/\/+$/, "");
|
|
205
286
|
if (!baseUrl) throw new Error("swap2-remote: baseUrl is required");
|
|
287
|
+
const insecure = /^[a-z][a-z0-9+.-]*:/i.test(baseUrl) ? !/^https:\/\//i.test(baseUrl) : /^[\\/]{2}/.test(baseUrl);
|
|
288
|
+
if (insecure && opts.allowInsecureBaseUrl !== true) {
|
|
289
|
+
throw new Error("swap2-remote: baseUrl must be https:// or a same-origin path (allowInsecureBaseUrl for local development)");
|
|
290
|
+
}
|
|
206
291
|
const supportsRoute = opts.supportsRoute;
|
|
207
292
|
const enabled = opts.enabled;
|
|
208
293
|
const headers = Object.freeze({ ...opts.headers });
|
|
@@ -211,6 +296,12 @@ function remoteProvider(opts) {
|
|
|
211
296
|
if (!Number.isInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 6e4) {
|
|
212
297
|
throw new Error("swap2-remote: timeoutMs must be an integer between 1 and 60000");
|
|
213
298
|
}
|
|
299
|
+
const requests = /* @__PURE__ */ new Map();
|
|
300
|
+
const remember = (intentId, params) => {
|
|
301
|
+
requests.delete(intentId);
|
|
302
|
+
requests.set(intentId, params);
|
|
303
|
+
if (requests.size > 64) requests.delete(requests.keys().next().value);
|
|
304
|
+
};
|
|
214
305
|
async function call(action, body, signal) {
|
|
215
306
|
const timeout = timeoutSignal(timeoutMs);
|
|
216
307
|
const requestSignal = signal ? anySignal([signal, timeout]) : timeout;
|
|
@@ -231,6 +322,8 @@ function remoteProvider(opts) {
|
|
|
231
322
|
}
|
|
232
323
|
return {
|
|
233
324
|
name,
|
|
325
|
+
remote: true,
|
|
326
|
+
...opts.verifyBuild ? { verifiesBuilds: true } : {},
|
|
234
327
|
// The browser side has no dark-flag env; the backend enforces its own
|
|
235
328
|
// flags — a disabled provider simply quotes null / rejects build.
|
|
236
329
|
enabled: () => enabled ? enabled() : true,
|
|
@@ -242,10 +335,30 @@ function remoteProvider(opts) {
|
|
|
242
335
|
if (out && typeof out === "object" && "noQuote" in out) {
|
|
243
336
|
throw new Error(String(out.noQuote).slice(0, 300));
|
|
244
337
|
}
|
|
338
|
+
if (out && typeof out === "object" && typeof out.intentId === "string") remember(out.intentId, params);
|
|
339
|
+
if (out && opts.verifyQuote) {
|
|
340
|
+
let refusal;
|
|
341
|
+
try {
|
|
342
|
+
refusal = opts.verifyQuote(params, out);
|
|
343
|
+
} catch (e) {
|
|
344
|
+
refusal = e instanceof Error ? e.message : String(e);
|
|
345
|
+
}
|
|
346
|
+
if (refusal) throw new Error(`${name}: the quote fails this app's own check \u2014 ${refusal.slice(0, 240)}`);
|
|
347
|
+
}
|
|
245
348
|
return out;
|
|
246
349
|
},
|
|
247
350
|
async buildDeposit(quote) {
|
|
248
|
-
|
|
351
|
+
const tx = await call("build", { quote });
|
|
352
|
+
if (opts.verifyBuild) {
|
|
353
|
+
let refusal;
|
|
354
|
+
try {
|
|
355
|
+
refusal = await opts.verifyBuild(quote, tx, requests.get(quote.intentId));
|
|
356
|
+
} catch (e) {
|
|
357
|
+
refusal = e instanceof Error ? e.message : String(e);
|
|
358
|
+
}
|
|
359
|
+
if (refusal) throw new Error(`${name}: the build fails this app's own check \u2014 ${refusal.slice(0, 240)}`);
|
|
360
|
+
}
|
|
361
|
+
return tx;
|
|
249
362
|
},
|
|
250
363
|
async preflightDeposit(quote, tx) {
|
|
251
364
|
await call("preflight", { quote, tx });
|
|
@@ -267,7 +380,9 @@ function remoteProvider(opts) {
|
|
|
267
380
|
...opts.supportsChain ? { supportsChain: opts.supportsChain.bind(opts) } : {},
|
|
268
381
|
...opts.supportsChainPair ? { supportsChainPair: opts.supportsChainPair.bind(opts) } : {},
|
|
269
382
|
...opts.supportsSameChain !== void 0 ? { supportsSameChain: opts.supportsSameChain } : {},
|
|
270
|
-
...opts.claimTargets ? { claimTargets: opts.claimTargets } : {}
|
|
383
|
+
...opts.claimTargets ? { claimTargets: opts.claimTargets } : {},
|
|
384
|
+
...opts.callTargets ? { callTargets: opts.callTargets } : {},
|
|
385
|
+
...opts.spenders ? { spenders: opts.spenders } : {}
|
|
271
386
|
};
|
|
272
387
|
}
|
|
273
388
|
|
|
@@ -383,7 +498,64 @@ function normalizeTypedDomain(domain) {
|
|
|
383
498
|
else if (typeof raw === "string" && /^0x[0-9a-fA-F]+$/.test(raw)) chainId = safeChainId(BigInt(raw));
|
|
384
499
|
return chainId === void 0 || !Number.isSafeInteger(chainId) ? domain : { ...domain, chainId };
|
|
385
500
|
}
|
|
501
|
+
function assertTypedOrder(typedData, approvalAddress, e) {
|
|
502
|
+
const fail = (why) => new Swap2ValidationError(`swap2: typed order refused \u2014 ${why}`);
|
|
503
|
+
let parsed;
|
|
504
|
+
try {
|
|
505
|
+
parsed = JSON.parse(typedData);
|
|
506
|
+
} catch {
|
|
507
|
+
throw fail("the typed data is not JSON");
|
|
508
|
+
}
|
|
509
|
+
if (!parsed || typeof parsed !== "object") throw fail("the typed data is not an object");
|
|
510
|
+
const td = parsed;
|
|
511
|
+
const primary = td.primaryType;
|
|
512
|
+
if (typeof primary !== "string" || !primary) throw fail("it names no primaryType");
|
|
513
|
+
if (/^permit/i.test(primary)) throw fail(`${primary} is a token approval, not an order`);
|
|
514
|
+
if (primary !== "Order") throw fail(`primaryType ${primary} is not an order this engine signs`);
|
|
515
|
+
const fields = td.types?.Order;
|
|
516
|
+
const has = (name, type) => Array.isArray(fields) && fields.some((f) => f?.name === name && f?.type === type);
|
|
517
|
+
if (!has("maker", "address") || !has("makerAsset", "address") || !has("makingAmount", "uint256")) throw fail("the Order type has no maker / makerAsset / makingAmount");
|
|
518
|
+
const verifying = td.domain?.verifyingContract;
|
|
519
|
+
if (!approvalAddress || typeof verifying !== "string" || verifying.toLowerCase() !== approvalAddress.toLowerCase()) {
|
|
520
|
+
throw fail(`verifyingContract ${String(verifying)} is not the settlement contract the swap approves`);
|
|
521
|
+
}
|
|
522
|
+
if (e.chainId !== null && normalizeTypedDomain(td.domain)?.chainId !== e.chainId) throw fail("the domain chainId is not the source chain");
|
|
523
|
+
const m = td.message ?? {};
|
|
524
|
+
if (String(m.maker ?? "").toLowerCase() !== e.self.toLowerCase()) throw fail("the maker is not this account");
|
|
525
|
+
if (!e.token || String(m.makerAsset ?? "").toLowerCase() !== e.token.toLowerCase()) throw fail("the makerAsset is not the source token");
|
|
526
|
+
const raw = m.makingAmount;
|
|
527
|
+
const making = typeof raw === "string" && /^(?:0x[0-9a-fA-F]{1,64}|[0-9]{1,78})$/.test(raw) ? BigInt(raw) : typeof raw === "number" && Number.isSafeInteger(raw) ? BigInt(raw) : null;
|
|
528
|
+
if (making === null || making !== BigInt(e.amount)) throw fail(`makingAmount ${String(raw)} is not the swap's ${e.amount}`);
|
|
529
|
+
}
|
|
386
530
|
var DEFAULT_SERIALIZED_NATIVE_ALLOWANCE = 20000000n;
|
|
531
|
+
var DEFAULT_TON_GAS_CEILING = 1000000000n;
|
|
532
|
+
var JETTON_TRANSFER_OP = 260734629;
|
|
533
|
+
function readTonBody(cell) {
|
|
534
|
+
let s;
|
|
535
|
+
try {
|
|
536
|
+
s = cell.beginParse();
|
|
537
|
+
} catch {
|
|
538
|
+
return { op: null };
|
|
539
|
+
}
|
|
540
|
+
let op;
|
|
541
|
+
try {
|
|
542
|
+
op = s.loadUint(32);
|
|
543
|
+
} catch {
|
|
544
|
+
return { op: null };
|
|
545
|
+
}
|
|
546
|
+
if (op !== JETTON_TRANSFER_OP) return { op };
|
|
547
|
+
try {
|
|
548
|
+
s.loadUintBig(64);
|
|
549
|
+
const amount = s.loadCoins();
|
|
550
|
+
const destination = tonRawAddress(s.loadAddress());
|
|
551
|
+
const response = s.loadMaybeAddress();
|
|
552
|
+
s.loadMaybeRef();
|
|
553
|
+
const forwardTon = s.loadCoins();
|
|
554
|
+
return { op, jettonTransfer: { amount, destination, responseDestination: response == null ? null : tonRawAddress(response), forwardTon } };
|
|
555
|
+
} catch {
|
|
556
|
+
return { op };
|
|
557
|
+
}
|
|
558
|
+
}
|
|
387
559
|
function reviewedIdentity(plan) {
|
|
388
560
|
const { feeUsd: _fee, etaSeconds: _eta, validUntilMs: _valid, approvals: _approvals, destAmount: _dest, requoted: _requoted, ...pinned } = plan;
|
|
389
561
|
return JSON.stringify(pinned, (_k, v) => typeof v === "bigint" ? v.toString() : v);
|
|
@@ -876,10 +1048,12 @@ var BaseSwap2Protocol = class extends import_core.Swap2Protocol {
|
|
|
876
1048
|
throw new Error(`swap2: provider transaction chain ${unsigned.chainId} does not match ${this.fromChain}`);
|
|
877
1049
|
}
|
|
878
1050
|
let tokenId;
|
|
1051
|
+
let engineTransfer = false;
|
|
879
1052
|
if (!unsigned.data && !unsigned.bodyBoc && !unsigned.typedData && !serialized) {
|
|
880
1053
|
const src = this.sourceAsset(executionOptions);
|
|
881
1054
|
if (src?.contract) {
|
|
882
1055
|
if (this.fromChain.startsWith("eip155:")) {
|
|
1056
|
+
engineTransfer = true;
|
|
883
1057
|
unsigned = {
|
|
884
1058
|
...unsigned,
|
|
885
1059
|
to: src.contract,
|
|
@@ -918,12 +1092,66 @@ var BaseSwap2Protocol = class extends import_core.Swap2Protocol {
|
|
|
918
1092
|
} else if (!unsigned.typedData && !unsigned.bodyBoc && executionValue !== BigInt(q.sourceAmount) + declaredNativeFee(q, this.fromChain)) {
|
|
919
1093
|
throw new Error("swap2: provider execution amount does not match the validated quote");
|
|
920
1094
|
}
|
|
1095
|
+
let tonBody;
|
|
1096
|
+
if (cellBody !== void 0) {
|
|
1097
|
+
const refuse = (why) => {
|
|
1098
|
+
throw new Swap2ValidationError(`swap2: TON cell-body build refused \u2014 ${why}`);
|
|
1099
|
+
};
|
|
1100
|
+
const raw = this._config.tonGasCeiling;
|
|
1101
|
+
const ceiling = raw === void 0 ? DEFAULT_TON_GAS_CEILING : BigInt(raw);
|
|
1102
|
+
if (ceiling < 0n) throw new Swap2ConfigurationError("swap2: tonGasCeiling must be non-negative");
|
|
1103
|
+
if (provider.remote === true && provider.verifiesBuilds !== true) refuse("a remote provider's cell body needs the app's own validator (remoteProvider verifyBuild)");
|
|
1104
|
+
tonBody = readTonBody(cellBody);
|
|
1105
|
+
const input = BigInt(q.sourceAmount);
|
|
1106
|
+
if (source.contract) {
|
|
1107
|
+
if (executionValue > ceiling) refuse(`it attaches ${executionValue} nanoTON to a jetton transfer, more than the ${ceiling} gas ceiling`);
|
|
1108
|
+
const t = tonBody.jettonTransfer;
|
|
1109
|
+
if (!t) refuse("a jetton-source body must be a jetton transfer");
|
|
1110
|
+
if (t.amount !== input) refuse(`the jetton transfer moves ${t.amount}, not the swap's ${input}`);
|
|
1111
|
+
if (!t.responseDestination || t.responseDestination !== tonRawAddress(selfAddress)) refuse("the jetton transfer returns its excess to another wallet");
|
|
1112
|
+
if (t.forwardTon >= executionValue) refuse("the jetton transfer forwards more TON than the message carries");
|
|
1113
|
+
const own = typeof signer.getJettonWalletAddress === "function" ? tonRawAddress(await signer.getJettonWalletAddress(source.contract)) : null;
|
|
1114
|
+
if (!own) refuse("the account cannot name its own jetton wallet (getJettonWalletAddress), so the target cannot be checked");
|
|
1115
|
+
if (tonRawAddress(unsigned.to) !== own) refuse(`it is sent to ${unsigned.to}, not this account's ${source.symbol ?? "source"} jetton wallet`);
|
|
1116
|
+
} else {
|
|
1117
|
+
if (executionValue < input || executionValue - input > ceiling) refuse(`it attaches ${executionValue} nanoTON for a ${input} input, more than the ${ceiling} gas ceiling on top`);
|
|
1118
|
+
const pinned = provider.remote === true ? [...provider.callTargets?.(this.fromChain) ?? [], ...q.depositAddress ? [q.depositAddress] : []].map(tonRawAddress).filter((a) => !!a) : [];
|
|
1119
|
+
if (pinned.length && !pinned.includes(tonRawAddress(unsigned.to) ?? "")) refuse(`remote provider '${q.provider}' sends it to ${unsigned.to}, which the app did not pin (remoteProvider callTargets)`);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
const remote = provider.remote === true;
|
|
1123
|
+
const pins = (list) => (list ?? []).filter((a) => typeof a === "string" && /^0x[0-9a-fA-F]{40}$/.test(a)).map((a) => a.toLowerCase());
|
|
1124
|
+
const pinnedTargets = remote ? pins(provider.callTargets?.(this.fromChain)) : [];
|
|
1125
|
+
const pinnedSpenders = remote ? pins(provider.spenders?.(this.fromChain)) : [];
|
|
1126
|
+
if (remote && unsigned.approvalAddress && !pinnedSpenders.includes(unsigned.approvalAddress.toLowerCase())) {
|
|
1127
|
+
throw new Swap2ValidationError(`swap2: remote provider '${q.provider}' asks to approve ${unsigned.approvalAddress} on ${this.fromChain}, which the app did not pin as a spender (remoteProvider spenders) \u2014 refused`);
|
|
1128
|
+
}
|
|
1129
|
+
if (unsigned.typedData !== void 0) {
|
|
1130
|
+
const chainId = this.fromChain.startsWith("eip155:") ? Number(this.fromChain.slice("eip155:".length)) : null;
|
|
1131
|
+
assertTypedOrder(unsigned.typedData, unsigned.approvalAddress, { self: selfAddress, token: source.contract ?? void 0, amount: q.sourceAmount, chainId });
|
|
1132
|
+
}
|
|
921
1133
|
if (unsigned.data !== void 0 && this.fromChain.startsWith("eip155:")) {
|
|
922
1134
|
const call = (0, import_tx_preview_evm.decodeCall)({ to: unsigned.to, data: unsigned.data, value: executionValue });
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
1135
|
+
if (call.undecodable) throw new Swap2ValidationError("swap2: quote calldata is not hex bytes \u2014 refused");
|
|
1136
|
+
if (call.approval && call.approval.kind !== "erc20") {
|
|
1137
|
+
throw new Swap2ValidationError(`swap2: quote calldata is ${call.approval.kind === "operator" ? "an operator approval (setApprovalForAll)" : "a Permit2 approval"} \u2014 never a swap's main call; refused`);
|
|
1138
|
+
}
|
|
1139
|
+
if (call.transfer || call.approval) {
|
|
1140
|
+
if (!source.contract || unsigned.to.toLowerCase() !== source.contract.toLowerCase()) {
|
|
1141
|
+
throw new Swap2ValidationError(`swap2: quote calldata (${call.method}) moves ${(0, import_viem.getAddress)(unsigned.to)}, not the source token \u2014 refused`);
|
|
1142
|
+
}
|
|
1143
|
+
if (call.method === "transferFrom") throw new Swap2ValidationError("swap2: quote calldata pulls the source token with transferFrom \u2014 refused");
|
|
1144
|
+
const amount = call.transfer?.amount ?? call.approval?.amount ?? null;
|
|
1145
|
+
if (amount === null || amount > BigInt(q.sourceAmount)) {
|
|
1146
|
+
throw new Swap2ValidationError(`swap2: quote calldata (${call.method}) covers ${amount === null ? "an unlimited amount" : amount} of the source token, more than the swap's ${q.sourceAmount} \u2014 refused`);
|
|
1147
|
+
}
|
|
1148
|
+
const named = (call.transfer?.to ?? call.approval.spender).toLowerCase();
|
|
1149
|
+
const allowed = engineTransfer ? [q.depositAddress] : remote ? [selfAddress, ...pinnedTargets, ...pinnedSpenders] : [selfAddress, q.depositAddress, unsigned.approvalAddress];
|
|
1150
|
+
if (!allowed.filter((a) => !!a).map((a) => a.toLowerCase()).includes(named)) {
|
|
1151
|
+
throw new Swap2ValidationError(`swap2: quote calldata (${call.method}) names ${(0, import_viem.getAddress)(named)}, which is not this account, the deposit address or ${remote ? "a spender the app pinned" : "the declared spender"} \u2014 refused`);
|
|
1152
|
+
}
|
|
1153
|
+
} else if (remote && !pinnedTargets.includes(unsigned.to.toLowerCase())) {
|
|
1154
|
+
throw new Swap2ValidationError(`swap2: remote provider '${q.provider}' calls ${(0, import_viem.getAddress)(unsigned.to)} on ${this.fromChain}, which the app did not pin as a call target (remoteProvider callTargets) \u2014 refused`);
|
|
927
1155
|
}
|
|
928
1156
|
}
|
|
929
1157
|
const needsApproval = !!unsigned.approvalAddress && !!source.contract;
|
|
@@ -985,7 +1213,7 @@ var BaseSwap2Protocol = class extends import_core.Swap2Protocol {
|
|
|
985
1213
|
...unsigned.chainId !== void 0 ? { chainId: unsigned.chainId } : {},
|
|
986
1214
|
...unsigned.gasLimit ? { gasLimit: BigInt(unsigned.gasLimit) } : {},
|
|
987
1215
|
...unsigned.memo ? { memo: unsigned.memo, ...unsigned.memoEncoding ? { memoEncoding: unsigned.memoEncoding } : {} } : {},
|
|
988
|
-
...cellBody !== void 0 ? { cellBody: true } : {}
|
|
1216
|
+
...cellBody !== void 0 ? { cellBody: true, ...tonBody ? { cellBodyDecoded: tonBody } : {} } : {}
|
|
989
1217
|
}
|
|
990
1218
|
};
|
|
991
1219
|
const previous = carried?.reviewed;
|
|
@@ -1250,6 +1478,7 @@ var BaseSwap2Protocol = class extends import_core.Swap2Protocol {
|
|
|
1250
1478
|
throw new Swap2ValidationError(`swap2: claim target ${unsigned.to} is not a contract '${name}' declares for ${unsigned.chain} \u2014 refused`);
|
|
1251
1479
|
}
|
|
1252
1480
|
const claimCall = (0, import_tx_preview_evm.decodeCall)({ to: unsigned.to, data: unsigned.data, value: 0n });
|
|
1481
|
+
if (claimCall.undecodable) throw new Swap2ValidationError("swap2: claim calldata is not hex bytes \u2014 refused");
|
|
1253
1482
|
if (claimCall.transfer || claimCall.approval) {
|
|
1254
1483
|
throw new Swap2ValidationError(`swap2: claim calldata (${claimCall.method}) moves tokens or grants an allowance \u2014 refused`);
|
|
1255
1484
|
}
|
|
@@ -1382,6 +1611,11 @@ var SingleProviderSwap2 = class extends BaseSwap2Protocol {
|
|
|
1382
1611
|
this.lastRefusal ||= `${name}: rejected by validation (minDest=${quote.minDestAmount} vs floor=${snapshot.minAmountOut ?? "-"})`;
|
|
1383
1612
|
return null;
|
|
1384
1613
|
}
|
|
1614
|
+
const unpinned = remotePinRefusal(this.provider, quote, snapshot.sourceChain);
|
|
1615
|
+
if (unpinned) {
|
|
1616
|
+
this.lastRefusal ||= unpinned;
|
|
1617
|
+
return null;
|
|
1618
|
+
}
|
|
1385
1619
|
return { ...quote, provider: name };
|
|
1386
1620
|
} catch (err) {
|
|
1387
1621
|
this.lastRefusal ||= `${name}: ${(err instanceof Error ? err.message : String(err)).slice(0, 160)}`;
|