@anywalletkit/protocol-swap2-symbiosis 0.1.0 → 0.1.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/dist/{chunk-4OE47QPO.js → chunk-6HTJDYA4.js} +2 -2
- package/dist/{chunk-UCHTN275.js → chunk-BLPE6M3U.js} +168 -157
- package/dist/chunk-BLPE6M3U.js.map +1 -0
- package/dist/{chunk-BVFMCR2O.js → chunk-K3EPNDVO.js} +93 -68
- package/dist/chunk-K3EPNDVO.js.map +1 -0
- package/dist/index.cjs +256 -222
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +3 -3
- package/dist/protocol.cjs +254 -220
- package/dist/protocol.cjs.map +1 -1
- package/dist/protocol.js +3 -3
- package/dist/provider.cjs +256 -222
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.js +2 -2
- package/dist/verify.cjs +166 -156
- package/dist/verify.cjs.map +1 -1
- package/dist/verify.js +1 -1
- package/package.json +5 -5
- package/dist/chunk-BVFMCR2O.js.map +0 -1
- package/dist/chunk-UCHTN275.js.map +0 -1
- /package/dist/{chunk-4OE47QPO.js.map → chunk-6HTJDYA4.js.map} +0 -0
|
@@ -26,6 +26,7 @@ import { decodeFunctionData, encodeFunctionData, getAddress, isAddress, parseAbi
|
|
|
26
26
|
import { bigintToNumber as bigintToNumber2 } from "@anywalletkit/core";
|
|
27
27
|
import { ccAssetByToken } from "@anywalletkit/swap2-assets";
|
|
28
28
|
import { sourceAssetOf, tonRawAddress } from "@anywalletkit/protocol-swap2-core/util";
|
|
29
|
+
import { refusalOf, withReason } from "@anywalletkit/protocol-swap2-core/errors";
|
|
29
30
|
|
|
30
31
|
// src/boc.ts
|
|
31
32
|
import { bigintToNumber } from "@anywalletkit/core";
|
|
@@ -168,6 +169,13 @@ function snakeHex(cell, length) {
|
|
|
168
169
|
return out.slice(0, length * 2);
|
|
169
170
|
}
|
|
170
171
|
|
|
172
|
+
// src/reasons.ts
|
|
173
|
+
import { refusalNotes } from "@anywalletkit/protocol-swap2-core/errors";
|
|
174
|
+
var notes = refusalNotes(256);
|
|
175
|
+
function noted(text, info) {
|
|
176
|
+
return info === void 0 ? text : notes.note(text, info);
|
|
177
|
+
}
|
|
178
|
+
|
|
171
179
|
// src/verify.ts
|
|
172
180
|
var ABI = parseAbi([
|
|
173
181
|
"struct MetaRouteTransaction { bytes firstSwapCalldata; bytes secondSwapCalldata; address[] approvedTokens; address firstDexRouter; address secondDexRouter; uint256 amount; bool nativeIn; address relayRecipient; bytes otherSideCalldata; }",
|
|
@@ -225,31 +233,31 @@ var OP_TON_META_SYNTHESIZE = 0x5e7d9020n;
|
|
|
225
233
|
var SYMBIOSIS_TON_VALUE_CEILING = 1000000000n;
|
|
226
234
|
var Refusal = class extends Error {
|
|
227
235
|
};
|
|
228
|
-
var refuse = (reason2) => {
|
|
229
|
-
throw new Refusal(reason2);
|
|
236
|
+
var refuse = (reason2, why) => {
|
|
237
|
+
throw withReason(new Refusal(reason2), why);
|
|
230
238
|
};
|
|
231
239
|
var lower = (a) => a.toLowerCase();
|
|
232
240
|
var same = (a, b) => typeof b === "string" && lower(a) === lower(b);
|
|
233
241
|
function symbiosisRequest(params, options = {}) {
|
|
234
242
|
const source = symbiosisChain(params.sourceChain);
|
|
235
243
|
const dest = symbiosisChain(params.destChain);
|
|
236
|
-
if (!source || !dest) return "unsupported chain";
|
|
237
|
-
if (source === dest) return "same-chain swaps are not served";
|
|
238
|
-
if (dest.family === "bitcoin") return "Bitcoin is not a destination";
|
|
244
|
+
if (!source || !dest) return noted("unsupported chain", "unsupported_pair");
|
|
245
|
+
if (source === dest) return noted("same-chain swaps are not served", "unsupported_pair");
|
|
246
|
+
if (dest.family === "bitcoin") return noted("Bitcoin is not a destination", "unsupported_pair");
|
|
239
247
|
if (typeof params.amountIn !== "string" || !/^\d+$/.test(params.amountIn) || BigInt(params.amountIn) <= 0n) return "amount must be a positive integer string";
|
|
240
248
|
const asset = sourceAssetOf(params);
|
|
241
|
-
if (!asset) return `unknown source token ${params.sourceToken}
|
|
249
|
+
if (!asset) return noted(`unknown source token ${params.sourceToken}`, "unsupported_pair");
|
|
242
250
|
const buy = ccAssetByToken(params.destChain, params.destToken);
|
|
243
|
-
if (!buy) return `destination token ${params.destToken} is not in the catalog
|
|
251
|
+
if (!buy) return noted(`destination token ${params.destToken} is not in the catalog`, "unsupported_pair");
|
|
244
252
|
const out = symbiosisToken(dest, buy.contract);
|
|
245
|
-
if (!out) return "malformed destination token";
|
|
253
|
+
if (!out) return noted("malformed destination token", "unsupported_pair");
|
|
246
254
|
const sender = params.refundAddress ?? "";
|
|
247
|
-
if (!validAddress(source, sender)) return `the sender is not a ${source.name} address
|
|
248
|
-
if (!validAddress(dest, params.destAddress)) return `the recipient is not a ${dest.name} address
|
|
255
|
+
if (!validAddress(source, sender)) return noted(`the sender is not a ${source.name} address`, "recipient_mismatch");
|
|
256
|
+
if (!validAddress(dest, params.destAddress)) return noted(`the recipient is not a ${dest.name} address`, "recipient_mismatch");
|
|
249
257
|
let fallbackReceiver = null;
|
|
250
258
|
if (source.family !== "evm") {
|
|
251
259
|
const own = dest.family === "evm" ? params.destAddress : options.fallbackReceiver;
|
|
252
|
-
if (!own || !isAddress(own) || same(own, ZERO)) return "a non-EVM source to a non-EVM destination needs the user's own EVM address (fallbackReceiver)";
|
|
260
|
+
if (!own || !isAddress(own) || same(own, ZERO)) return noted("a non-EVM source to a non-EVM destination needs the user's own EVM address (fallbackReceiver)", "unsupported_pair");
|
|
253
261
|
fallbackReceiver = getAddress(own);
|
|
254
262
|
}
|
|
255
263
|
return {
|
|
@@ -274,52 +282,52 @@ function ownSet(r) {
|
|
|
274
282
|
return out;
|
|
275
283
|
}
|
|
276
284
|
function decode(abi, data, selector, name, what = name) {
|
|
277
|
-
if (typeof data !== "string" || !/^0x([0-9a-fA-F]{2})*$/.test(data)) refuse(`${what}: not hex calldata
|
|
278
|
-
if (lower(data.slice(0, 10)) !== selector) refuse(`${what}: unexpected function ${data.slice(0, 10)}
|
|
285
|
+
if (typeof data !== "string" || !/^0x([0-9a-fA-F]{2})*$/.test(data)) refuse(`${what}: not hex calldata`, "policy_refused");
|
|
286
|
+
if (lower(data.slice(0, 10)) !== selector) refuse(`${what}: unexpected function ${data.slice(0, 10)}`, "policy_refused");
|
|
279
287
|
let decoded;
|
|
280
288
|
try {
|
|
281
289
|
decoded = decodeFunctionData({ abi, data });
|
|
282
290
|
} catch {
|
|
283
|
-
return refuse(`${what}: calldata does not decode
|
|
291
|
+
return refuse(`${what}: calldata does not decode`, "policy_refused");
|
|
284
292
|
}
|
|
285
|
-
if (decoded.functionName !== name) refuse(`${what}: unexpected function ${decoded.functionName}
|
|
293
|
+
if (decoded.functionName !== name) refuse(`${what}: unexpected function ${decoded.functionName}`, "policy_refused");
|
|
286
294
|
let again;
|
|
287
295
|
try {
|
|
288
296
|
again = encodeFunctionData({ abi, functionName: decoded.functionName, args: decoded.args });
|
|
289
297
|
} catch {
|
|
290
|
-
return refuse(`${what}: calldata does not re-encode
|
|
298
|
+
return refuse(`${what}: calldata does not re-encode`, "policy_refused");
|
|
291
299
|
}
|
|
292
|
-
if (lower(again) !== lower(data)) refuse(`${what}: calldata is not canonically encoded
|
|
300
|
+
if (lower(again) !== lower(data)) refuse(`${what}: calldata is not canonically encoded`, "policy_refused");
|
|
293
301
|
return decoded.args ?? [];
|
|
294
302
|
}
|
|
295
303
|
function patchedWordEnd(a, b, what) {
|
|
296
|
-
if (a.length !== b.length) return refuse(`${what}: the amount does not sit in one fixed word
|
|
304
|
+
if (a.length !== b.length) return refuse(`${what}: the amount does not sit in one fixed word`, "policy_refused");
|
|
297
305
|
let at = -1;
|
|
298
306
|
for (let i = 10; i < a.length; i += 64) {
|
|
299
307
|
if (a.slice(i, i + 64) !== b.slice(i, i + 64)) {
|
|
300
|
-
if (at !== -1) return refuse(`${what}: the amount is written in more than one place
|
|
308
|
+
if (at !== -1) return refuse(`${what}: the amount is written in more than one place`, "policy_refused");
|
|
301
309
|
at = i;
|
|
302
310
|
}
|
|
303
311
|
}
|
|
304
|
-
if (at === -1) return refuse(`${what}: the amount word was not found
|
|
312
|
+
if (at === -1) return refuse(`${what}: the amount word was not found`, "policy_refused");
|
|
305
313
|
return BigInt((at - 2) / 2 + 32);
|
|
306
314
|
}
|
|
307
315
|
function decodeAggregatorSwap(chain, target, data, what) {
|
|
308
|
-
if (chain.family !== "evm") refuse(`${what}: no aggregator router is allow-listed on ${chain.name}
|
|
316
|
+
if (chain.family !== "evm") refuse(`${what}: no aggregator router is allow-listed on ${chain.name}`, "vault_mismatch");
|
|
309
317
|
if (same(target, SYMBIOSIS_AGGREGATORS.oneInchV6)) {
|
|
310
318
|
const [, d] = decode(ONE_INCH_ABI, data, SEL_ONE_INCH_SWAP, "swap", what);
|
|
311
|
-
if (d.flags !== 0n) refuse(`${what}: the 1inch swap sets flags ${d.flags} (partial fill, extra value or Permit2 are not accepted)
|
|
312
|
-
if (d.minReturnAmount <= 0n) refuse(`${what}: the swap names no minimum
|
|
319
|
+
if (d.flags !== 0n) refuse(`${what}: the 1inch swap sets flags ${d.flags} (partial fill, extra value or Permit2 are not accepted)`, "policy_refused");
|
|
320
|
+
if (d.minReturnAmount <= 0n) refuse(`${what}: the swap names no minimum`, "price_check_failed");
|
|
313
321
|
return { router: "1inch-v6", srcToken: d.srcToken, dstToken: d.dstToken, dstReceiver: d.dstReceiver, amount: d.amount, minReturnAmount: d.minReturnAmount, amountOffset: 196n };
|
|
314
322
|
}
|
|
315
323
|
if (same(target, SYMBIOSIS_AGGREGATORS.kyberV2)) {
|
|
316
324
|
const [x] = decode(KYBER_ABI, data, SEL_KYBER_SWAP, "swap", what);
|
|
317
325
|
const d = x.desc;
|
|
318
|
-
if ((d.flags & ~KYBER_FLAGS_ALLOWED) !== 0n) refuse(`${what}: the KyberSwap swap sets flags ${d.flags} (partial fill, extra value, burns or a fee on the output are not accepted)
|
|
319
|
-
if (d.feeReceivers.length !== 0 || d.feeAmounts.length !== 0) refuse(`${what}: the KyberSwap swap pays a fee receiver
|
|
320
|
-
if (d.permit !== "0x") refuse(`${what}: the KyberSwap swap carries a permit
|
|
321
|
-
if (d.minReturnAmount <= 0n) refuse(`${what}: the swap names no minimum
|
|
322
|
-
if (d.srcReceivers.length !== d.srcAmounts.length) refuse(`${what}: malformed KyberSwap receivers
|
|
326
|
+
if ((d.flags & ~KYBER_FLAGS_ALLOWED) !== 0n) refuse(`${what}: the KyberSwap swap sets flags ${d.flags} (partial fill, extra value, burns or a fee on the output are not accepted)`, "policy_refused");
|
|
327
|
+
if (d.feeReceivers.length !== 0 || d.feeAmounts.length !== 0) refuse(`${what}: the KyberSwap swap pays a fee receiver`, "policy_refused");
|
|
328
|
+
if (d.permit !== "0x") refuse(`${what}: the KyberSwap swap carries a permit`, "policy_refused");
|
|
329
|
+
if (d.minReturnAmount <= 0n) refuse(`${what}: the swap names no minimum`, "price_check_failed");
|
|
330
|
+
if (d.srcReceivers.length !== d.srcAmounts.length) refuse(`${what}: malformed KyberSwap receivers`, "policy_refused");
|
|
323
331
|
const flipped = encodeFunctionData({ abi: KYBER_ABI, functionName: "swap", args: [{ ...x, desc: { ...d, amount: d.amount ^ (1n << 256n) - 1n } }] });
|
|
324
332
|
return { router: "kyberswap-v2", srcToken: d.srcToken, dstToken: d.dstToken, dstReceiver: d.dstReceiver, amount: d.amount, minReturnAmount: d.minReturnAmount, amountOffset: patchedWordEnd(lower(data), lower(flipped), what) };
|
|
325
333
|
}
|
|
@@ -328,46 +336,46 @@ function decodeAggregatorSwap(chain, target, data, what) {
|
|
|
328
336
|
const selector = lower(String(data).slice(0, 10));
|
|
329
337
|
if (selector === SEL_V2_FROM_ETH) {
|
|
330
338
|
const [least, route, payTo] = decode(V2_ABI, data, selector, "swapExactETHForTokens", what);
|
|
331
|
-
if (route.length < 2 || route.length > MAX_V2_PATH) refuse(`${what}: unexpected swap path
|
|
332
|
-
if (least <= 0n) refuse(`${what}: the swap names no minimum
|
|
339
|
+
if (route.length < 2 || route.length > MAX_V2_PATH) refuse(`${what}: unexpected swap path`, "policy_refused");
|
|
340
|
+
if (least <= 0n) refuse(`${what}: the swap names no minimum`, "price_check_failed");
|
|
333
341
|
return { router: "uniswap-v2", srcToken: AGGREGATOR_NATIVE, dstToken: route[route.length - 1], dstReceiver: payTo, amount: null, minReturnAmount: least, amountOffset: null };
|
|
334
342
|
}
|
|
335
|
-
if (selector !== SEL_V2_TOKENS && selector !== SEL_V2_ETH) refuse(`${what}: unexpected V2 router function ${selector}
|
|
343
|
+
if (selector !== SEL_V2_TOKENS && selector !== SEL_V2_ETH) refuse(`${what}: unexpected V2 router function ${selector}`, "policy_refused");
|
|
336
344
|
const toNative = selector === SEL_V2_ETH;
|
|
337
345
|
const [amount, min, path, to] = decode(V2_ABI, data, selector, toNative ? "swapExactTokensForETH" : "swapExactTokensForTokens", what);
|
|
338
|
-
if (path.length < 2 || path.length > MAX_V2_PATH) refuse(`${what}: unexpected swap path
|
|
339
|
-
if (min <= 0n) refuse(`${what}: the swap names no minimum
|
|
346
|
+
if (path.length < 2 || path.length > MAX_V2_PATH) refuse(`${what}: unexpected swap path`, "policy_refused");
|
|
347
|
+
if (min <= 0n) refuse(`${what}: the swap names no minimum`, "price_check_failed");
|
|
340
348
|
return { router: "uniswap-v2", srcToken: path[0], dstToken: toNative ? AGGREGATOR_NATIVE : path[path.length - 1], dstReceiver: to, amount, minReturnAmount: min, amountOffset: FIRST_WORD_END };
|
|
341
349
|
}
|
|
342
|
-
return refuse(`${what}: ${target} is not an allow-listed swap router
|
|
350
|
+
return refuse(`${what}: ${target} is not an allow-listed swap router`, "vault_mismatch");
|
|
343
351
|
}
|
|
344
352
|
var scale18 = (amount, decimals) => amount * 10n ** BigInt(18 - decimals);
|
|
345
353
|
function checkPools(s) {
|
|
346
|
-
if (!same(s.secondDexRouter, SYMBIOSIS_HOST.multicallRouter)) refuse(`the host-chain swap calls ${s.secondDexRouter}, not the pinned MulticallRouter
|
|
354
|
+
if (!same(s.secondDexRouter, SYMBIOSIS_HOST.multicallRouter)) refuse(`the host-chain swap calls ${s.secondDexRouter}, not the pinned MulticallRouter`, "vault_mismatch");
|
|
347
355
|
const [amountIn, calls, receiveSides, path, offsets, to] = decode(ABI, s.secondSwapCalldata, SEL_MULTICALL, "multicall");
|
|
348
356
|
const n = calls.length;
|
|
349
|
-
if (n < 1 || n > MAX_POOL_STEPS || receiveSides.length !== n || offsets.length !== n || path.length !== n + 1) refuse("the host-chain swap has a malformed step list");
|
|
350
|
-
if (amountIn !== s.amount) refuse("the host-chain swap is sized for another amount than the bridge request");
|
|
351
|
-
if (!same(to, SYMBIOSIS_HOST_META_ROUTER)) refuse(`the host-chain swap pays ${to}, not the pinned host-chain MetaRouter
|
|
352
|
-
if (s.swapTokens.length !== 2 || !same(path[0], s.swapTokens[0]) || !same(path[n], s.swapTokens[1])) refuse("the host-chain swap path is not the bridge request's token pair");
|
|
357
|
+
if (n < 1 || n > MAX_POOL_STEPS || receiveSides.length !== n || offsets.length !== n || path.length !== n + 1) refuse("the host-chain swap has a malformed step list", "policy_refused");
|
|
358
|
+
if (amountIn !== s.amount) refuse("the host-chain swap is sized for another amount than the bridge request", "policy_refused");
|
|
359
|
+
if (!same(to, SYMBIOSIS_HOST_META_ROUTER)) refuse(`the host-chain swap pays ${to}, not the pinned host-chain MetaRouter`, "vault_mismatch");
|
|
360
|
+
if (s.swapTokens.length !== 2 || !same(path[0], s.swapTokens[0]) || !same(path[n], s.swapTokens[1])) refuse("the host-chain swap path is not the bridge request's token pair", "policy_refused");
|
|
353
361
|
let minDy = 0n;
|
|
354
362
|
for (let i = 0; i < n; i++) {
|
|
355
363
|
const pool = SYMBIOSIS_POOLS[lower(receiveSides[i])];
|
|
356
|
-
if (!pool) refuse(`host-chain swap step ${i + 1} calls ${receiveSides[i]}, not a pinned Symbiosis pool
|
|
364
|
+
if (!pool) refuse(`host-chain swap step ${i + 1} calls ${receiveSides[i]}, not a pinned Symbiosis pool`, "vault_mismatch");
|
|
357
365
|
const [from, into, , min, payTo] = decode(POOL_ABI, calls[i], SEL_POOL_SWAP, "swap", `host-chain swap step ${i + 1}`);
|
|
358
|
-
if (offsets[i] !== POOL_OFFSET) refuse(`host-chain swap step ${i + 1} writes the running amount at ${offsets[i]}, not into the swap amount
|
|
359
|
-
if (from >= BigInt(pool.length) || into >= BigInt(pool.length) || from === into) refuse(`host-chain swap step ${i + 1} names a token index the pool does not have
|
|
366
|
+
if (offsets[i] !== POOL_OFFSET) refuse(`host-chain swap step ${i + 1} writes the running amount at ${offsets[i]}, not into the swap amount`, "policy_refused");
|
|
367
|
+
if (from >= BigInt(pool.length) || into >= BigInt(pool.length) || from === into) refuse(`host-chain swap step ${i + 1} names a token index the pool does not have`, "policy_refused");
|
|
360
368
|
const a = pool[bigintToNumber2(from)];
|
|
361
369
|
const b = pool[bigintToNumber2(into)];
|
|
362
|
-
if (!a || !b || !same(a, path[i]) || !same(b, path[i + 1])) refuse(`host-chain swap step ${i + 1} does not trade the path's tokens
|
|
363
|
-
if (!same(payTo, SYMBIOSIS_HOST.multicallRouter)) refuse(`host-chain swap step ${i + 1} pays ${payTo}, not the MulticallRouter
|
|
364
|
-
if (min <= 0n) refuse(`host-chain swap step ${i + 1} names no minimum
|
|
370
|
+
if (!a || !b || !same(a, path[i]) || !same(b, path[i + 1])) refuse(`host-chain swap step ${i + 1} does not trade the path's tokens`, "policy_refused");
|
|
371
|
+
if (!same(payTo, SYMBIOSIS_HOST.multicallRouter)) refuse(`host-chain swap step ${i + 1} pays ${payTo}, not the MulticallRouter`, "vault_mismatch");
|
|
372
|
+
if (min <= 0n) refuse(`host-chain swap step ${i + 1} names no minimum`, "price_check_failed");
|
|
365
373
|
minDy = min;
|
|
366
374
|
}
|
|
367
375
|
const first = SYMBIOSIS_SYNTHS[lower(path[0])];
|
|
368
376
|
const last = SYMBIOSIS_SYNTHS[lower(path[n])];
|
|
369
|
-
if (!first || !last) return refuse("the host-chain swap moves a token that is not a pinned synthetic token");
|
|
370
|
-
if (scale18(s.amount, first.decimals) * 10000n < scale18(minDy, last.decimals) * CONSISTENCY_BPS) refuse("the bridge request states an amount far below the minimum its pool swap demands");
|
|
377
|
+
if (!first || !last) return refuse("the host-chain swap moves a token that is not a pinned synthetic token", "policy_refused");
|
|
378
|
+
if (scale18(s.amount, first.decimals) * 10000n < scale18(minDy, last.decimals) * CONSISTENCY_BPS) refuse("the bridge request states an amount far below the minimum its pool swap demands", "price_check_failed");
|
|
371
379
|
return minDy;
|
|
372
380
|
}
|
|
373
381
|
function checkFinalCall(r, data, own, burned, poolMin, minOut) {
|
|
@@ -375,121 +383,121 @@ function checkFinalCall(r, data, own, burned, poolMin, minOut) {
|
|
|
375
383
|
const revert = (a) => {
|
|
376
384
|
if (own.includes(lower(a))) return false;
|
|
377
385
|
if (same(a, SYMBIOSIS_REVERTER)) return true;
|
|
378
|
-
return refuse(`the burn names ${a} as the address that may revert it \u2014 neither the user nor Symbiosis' revert service
|
|
386
|
+
return refuse(`the burn names ${a} as the address that may revert it \u2014 neither the user nor Symbiosis' revert service`, "recipient_mismatch");
|
|
379
387
|
};
|
|
380
388
|
const synth = SYMBIOSIS_SYNTHS[lower(burned)];
|
|
381
|
-
if (!synth || synth.chain !== r.dest.id) return refuse(`the route burns ${burned}, which is not a pinned synthetic of a ${r.dest.name} token
|
|
389
|
+
if (!synth || synth.chain !== r.dest.id) return refuse(`the route burns ${burned}, which is not a pinned synthetic of a ${r.dest.name} token`, "policy_refused");
|
|
382
390
|
const direct = (fee) => {
|
|
383
|
-
if (r.destToken === null || !same(synth.real, r.destToken)) refuse("the route releases another token than the one quoted");
|
|
384
|
-
if (poolMin - fee < minOut) refuse(`the route guarantees ${poolMin - fee}, below the quoted minimum ${minOut}
|
|
391
|
+
if (r.destToken === null || !same(synth.real, r.destToken)) refuse("the route releases another token than the one quoted", "price_check_failed");
|
|
392
|
+
if (poolMin - fee < minOut) refuse(`the route guarantees ${poolMin - fee}, below the quoted minimum ${minOut}`, "price_check_failed");
|
|
385
393
|
};
|
|
386
394
|
if (r.dest.family === "ton") {
|
|
387
|
-
if (selector !== SEL_BURN_TON) refuse(`a TON destination must end in burnSyntheticTokenTON, got ${selector}
|
|
395
|
+
if (selector !== SEL_BURN_TON) refuse(`a TON destination must end in burnSyntheticTokenTON, got ${selector}`, "policy_refused");
|
|
388
396
|
const [fee, stoken, , , to, receiveSide, oppositeBridge, revertable, chainID] = decode(ABI, data, SEL_BURN_TON, "burnSyntheticTokenTON");
|
|
389
|
-
if (!same(stoken, burned)) refuse("the burn names another token than the one the pools pay out");
|
|
390
|
-
if (chainID !== BigInt(SYMBIOSIS_TON_CHAIN_ID)) refuse(`the burn names chain ${chainID}, not TON
|
|
397
|
+
if (!same(stoken, burned)) refuse("the burn names another token than the one the pools pay out", "policy_refused");
|
|
398
|
+
if (chainID !== BigInt(SYMBIOSIS_TON_CHAIN_ID)) refuse(`the burn names chain ${chainID}, not TON`, "recipient_mismatch");
|
|
391
399
|
const want = tonRawAddress(r.recipient);
|
|
392
|
-
if (!want || `${to.workchain}:${lower(to.address_hash).slice(2)}` !== want) refuse("the burn pays a TON address that is not the recipient");
|
|
393
|
-
if (!same(receiveSide, ZERO) || !same(oppositeBridge, ZERO)) refuse("the TON burn names an unexpected receive side or bridge");
|
|
400
|
+
if (!want || `${to.workchain}:${lower(to.address_hash).slice(2)}` !== want) refuse("the burn pays a TON address that is not the recipient", "recipient_mismatch");
|
|
401
|
+
if (!same(receiveSide, ZERO) || !same(oppositeBridge, ZERO)) refuse("the TON burn names an unexpected receive side or bridge", "vault_mismatch");
|
|
394
402
|
direct(fee);
|
|
395
403
|
return { destSwap: null, serviceReverts: revert(revertable) };
|
|
396
404
|
}
|
|
397
|
-
if (r.dest.family !== "evm" && r.dest.family !== "tron") refuse(`${r.dest.name} is not a destination
|
|
405
|
+
if (r.dest.family !== "evm" && r.dest.family !== "tron") refuse(`${r.dest.name} is not a destination`, "unsupported_pair");
|
|
398
406
|
const recipient = account20(r.dest, r.recipient);
|
|
399
|
-
if (!recipient || !r.dest.portal || !r.dest.bridge) return refuse(`no pinned deployment on ${r.dest.name}
|
|
400
|
-
if (selector !== SEL_META_BURN) refuse(`the route does not end in metaBurnSyntheticToken (${selector})
|
|
407
|
+
if (!recipient || !r.dest.portal || !r.dest.bridge) return refuse(`no pinned deployment on ${r.dest.name}`, "unsupported_pair");
|
|
408
|
+
if (selector !== SEL_META_BURN) refuse(`the route does not end in metaBurnSyntheticToken (${selector})`, "policy_refused");
|
|
401
409
|
const [b] = decode(ABI, data, SEL_META_BURN, "metaBurnSyntheticToken");
|
|
402
|
-
if (!same(b.sToken, burned)) refuse("the burn names another token than the one the pools pay out");
|
|
403
|
-
if (b.chainID !== BigInt(r.dest.id)) refuse(`the burn names chain ${b.chainID}, not ${r.dest.name}
|
|
404
|
-
if (!same(b.chain2address, recipient)) refuse(`the burn pays ${b.chain2address}, not the recipient
|
|
405
|
-
if (!same(b.receiveSide, r.dest.portal)) refuse(`the burn releases through ${b.receiveSide}, not the pinned ${r.dest.name} Portal
|
|
406
|
-
if (!same(b.oppositeBridge, r.dest.bridge)) refuse(`the burn names bridge ${b.oppositeBridge}, not the pinned ${r.dest.name} Bridge
|
|
407
|
-
if (!own.includes(lower(b.syntCaller))) refuse(`the burn is made on behalf of ${b.syntCaller}, not the user
|
|
410
|
+
if (!same(b.sToken, burned)) refuse("the burn names another token than the one the pools pay out", "policy_refused");
|
|
411
|
+
if (b.chainID !== BigInt(r.dest.id)) refuse(`the burn names chain ${b.chainID}, not ${r.dest.name}`, "recipient_mismatch");
|
|
412
|
+
if (!same(b.chain2address, recipient)) refuse(`the burn pays ${b.chain2address}, not the recipient`, "recipient_mismatch");
|
|
413
|
+
if (!same(b.receiveSide, r.dest.portal)) refuse(`the burn releases through ${b.receiveSide}, not the pinned ${r.dest.name} Portal`, "vault_mismatch");
|
|
414
|
+
if (!same(b.oppositeBridge, r.dest.bridge)) refuse(`the burn names bridge ${b.oppositeBridge}, not the pinned ${r.dest.name} Bridge`, "vault_mismatch");
|
|
415
|
+
if (!own.includes(lower(b.syntCaller))) refuse(`the burn is made on behalf of ${b.syntCaller}, not the user`, "recipient_mismatch");
|
|
408
416
|
const serviceReverts = revert(b.revertableAddress);
|
|
409
417
|
if (b.finalCallData === "0x") {
|
|
410
|
-
if (!same(b.finalReceiveSide, ZERO) || b.finalOffset !== 0n) refuse("the burn names a destination call target without a call");
|
|
418
|
+
if (!same(b.finalReceiveSide, ZERO) || b.finalOffset !== 0n) refuse("the burn names a destination call target without a call", "policy_refused");
|
|
411
419
|
direct(b.stableBridgingFee);
|
|
412
420
|
return { destSwap: null, serviceReverts };
|
|
413
421
|
}
|
|
414
422
|
const unwrapper = SYMBIOSIS_UNWRAPPERS[r.dest.id];
|
|
415
423
|
if (unwrapper && same(b.finalReceiveSide, unwrapper.unwrapper)) {
|
|
416
424
|
const [, to] = decode(UNWRAP_ABI, b.finalCallData, SEL_UNWRAP, "unwrap", "destination unwrap");
|
|
417
|
-
if (r.destToken !== null) refuse("the route unwraps to the native coin, which is not the token quoted");
|
|
418
|
-
if (!same(synth.real, unwrapper.wrapped)) refuse("the route unwraps a token that is not the chain's wrapped native token");
|
|
419
|
-
if (!same(to, recipient)) refuse(`the unwrap pays ${to}, not the recipient
|
|
420
|
-
if (b.finalOffset !== FIRST_WORD_END) refuse(`the unwrap gets the released amount written at ${b.finalOffset}, not into its amount
|
|
421
|
-
if (poolMin - b.stableBridgingFee < minOut) refuse(`the route guarantees ${poolMin - b.stableBridgingFee}, below the quoted minimum ${minOut}
|
|
425
|
+
if (r.destToken !== null) refuse("the route unwraps to the native coin, which is not the token quoted", "price_check_failed");
|
|
426
|
+
if (!same(synth.real, unwrapper.wrapped)) refuse("the route unwraps a token that is not the chain's wrapped native token", "policy_refused");
|
|
427
|
+
if (!same(to, recipient)) refuse(`the unwrap pays ${to}, not the recipient`, "recipient_mismatch");
|
|
428
|
+
if (b.finalOffset !== FIRST_WORD_END) refuse(`the unwrap gets the released amount written at ${b.finalOffset}, not into its amount`, "policy_refused");
|
|
429
|
+
if (poolMin - b.stableBridgingFee < minOut) refuse(`the route guarantees ${poolMin - b.stableBridgingFee}, below the quoted minimum ${minOut}`, "price_check_failed");
|
|
422
430
|
return { destSwap: "unwrap", serviceReverts };
|
|
423
431
|
}
|
|
424
432
|
const swap = decodeAggregatorSwap(r.dest, b.finalReceiveSide, b.finalCallData, "destination swap");
|
|
425
|
-
if (!same(swap.srcToken, synth.real)) refuse("the destination swap sells another token than the one released");
|
|
426
|
-
if (!same(swap.dstToken, r.destToken ?? AGGREGATOR_NATIVE)) refuse("the destination swap buys another token than the one quoted");
|
|
427
|
-
if (!same(swap.dstReceiver, recipient)) refuse(`the destination swap pays ${swap.dstReceiver}, not the recipient
|
|
428
|
-
if (swap.minReturnAmount < minOut) refuse(`the destination swap guarantees ${swap.minReturnAmount}, below the quoted minimum ${minOut}
|
|
429
|
-
if (swap.amountOffset === null || b.finalOffset !== swap.amountOffset) refuse(`the destination swap gets the released amount written at ${b.finalOffset}, not into its amount
|
|
433
|
+
if (!same(swap.srcToken, synth.real)) refuse("the destination swap sells another token than the one released", "policy_refused");
|
|
434
|
+
if (!same(swap.dstToken, r.destToken ?? AGGREGATOR_NATIVE)) refuse("the destination swap buys another token than the one quoted", "price_check_failed");
|
|
435
|
+
if (!same(swap.dstReceiver, recipient)) refuse(`the destination swap pays ${swap.dstReceiver}, not the recipient`, "recipient_mismatch");
|
|
436
|
+
if (swap.minReturnAmount < minOut) refuse(`the destination swap guarantees ${swap.minReturnAmount}, below the quoted minimum ${minOut}`, "price_check_failed");
|
|
437
|
+
if (swap.amountOffset === null || b.finalOffset !== swap.amountOffset) refuse(`the destination swap gets the released amount written at ${b.finalOffset}, not into its amount`, "policy_refused");
|
|
430
438
|
return { destSwap: swap.router, serviceReverts };
|
|
431
439
|
}
|
|
432
440
|
function checkSynthesize(r, s, own, minOut) {
|
|
433
|
-
if (s.chainID !== BigInt(SYMBIOSIS_HOST_CHAIN_ID)) refuse(`the bridge request targets chain ${s.chainID}, not Symbiosis' host chain
|
|
434
|
-
if (!same(s.receiveSide, SYMBIOSIS_HOST.synthesis) || !same(s.finalReceiveSide, SYMBIOSIS_HOST.synthesis)) refuse("the bridge request does not name the pinned host-chain Synthesis");
|
|
435
|
-
if (!same(s.oppositeBridge, SYMBIOSIS_HOST.bridge)) refuse("the bridge request does not name the pinned host-chain Bridge");
|
|
436
|
-
if (!own.includes(lower(s.chain2address))) refuse(`the bridge request credits ${s.chain2address} on the host chain, not one of the user's addresses
|
|
441
|
+
if (s.chainID !== BigInt(SYMBIOSIS_HOST_CHAIN_ID)) refuse(`the bridge request targets chain ${s.chainID}, not Symbiosis' host chain`, "vault_mismatch");
|
|
442
|
+
if (!same(s.receiveSide, SYMBIOSIS_HOST.synthesis) || !same(s.finalReceiveSide, SYMBIOSIS_HOST.synthesis)) refuse("the bridge request does not name the pinned host-chain Synthesis", "vault_mismatch");
|
|
443
|
+
if (!same(s.oppositeBridge, SYMBIOSIS_HOST.bridge)) refuse("the bridge request does not name the pinned host-chain Bridge", "vault_mismatch");
|
|
444
|
+
if (!own.includes(lower(s.chain2address))) refuse(`the bridge request credits ${s.chain2address} on the host chain, not one of the user's addresses`, "recipient_mismatch");
|
|
437
445
|
let serviceReverts = false;
|
|
438
446
|
if (!own.includes(lower(s.revertableAddress))) {
|
|
439
|
-
if (!same(s.revertableAddress, SYMBIOSIS_REVERTER)) refuse(`the bridge request lets ${s.revertableAddress} revert it \u2014 neither the user nor Symbiosis' revert service
|
|
447
|
+
if (!same(s.revertableAddress, SYMBIOSIS_REVERTER)) refuse(`the bridge request lets ${s.revertableAddress} revert it \u2014 neither the user nor Symbiosis' revert service`, "recipient_mismatch");
|
|
440
448
|
serviceReverts = true;
|
|
441
449
|
}
|
|
442
|
-
if (s.amount <= s.stableBridgingFee) refuse("the bridge request's fee takes the whole amount");
|
|
450
|
+
if (s.amount <= s.stableBridgingFee) refuse("the bridge request's fee takes the whole amount", "amount_too_small");
|
|
443
451
|
const minted = s.swapTokens.length > 0 ? SYMBIOSIS_SYNTHS[lower(s.swapTokens[0])] : void 0;
|
|
444
|
-
if (!minted || minted.chain !== r.source.id || !same(minted.real, s.rtoken)) refuse("the bridge request does not start from the pinned synthetic of the bridged token");
|
|
445
|
-
if (s.secondSwapCalldata === "0x") refuse("the bridge request carries no host-chain pool swap");
|
|
452
|
+
if (!minted || minted.chain !== r.source.id || !same(minted.real, s.rtoken)) refuse("the bridge request does not start from the pinned synthetic of the bridged token", "policy_refused");
|
|
453
|
+
if (s.secondSwapCalldata === "0x") refuse("the bridge request carries no host-chain pool swap", "policy_refused");
|
|
446
454
|
const poolMin = checkPools(s);
|
|
447
|
-
if (s.finalOffset !== FINAL_OFFSET) refuse(`unexpected amount offset ${s.finalOffset} in the final call
|
|
455
|
+
if (s.finalOffset !== FINAL_OFFSET) refuse(`unexpected amount offset ${s.finalOffset} in the final call`, "policy_refused");
|
|
448
456
|
const final = checkFinalCall(r, s.finalCalldata, own, s.swapTokens[1], poolMin, minOut);
|
|
449
457
|
return { destSwap: final.destSwap, serviceReverts: serviceReverts || final.serviceReverts };
|
|
450
458
|
}
|
|
451
459
|
function checkSymbiosisCall(r, call, minOut) {
|
|
452
|
-
if (r.source.family !== "evm" && r.source.family !== "tron") refuse(`${r.source.name} sources are not contract calls
|
|
453
|
-
if (minOut <= 0n) refuse("the quote names no minimum");
|
|
460
|
+
if (r.source.family !== "evm" && r.source.family !== "tron") refuse(`${r.source.name} sources are not contract calls`, "unsupported_pair");
|
|
461
|
+
if (minOut <= 0n) refuse("the quote names no minimum", "price_check_failed");
|
|
454
462
|
const gateway = r.source.gateway;
|
|
455
463
|
const sender = account20(r.source, r.sender);
|
|
456
|
-
if (!gateway || !r.source.portal || !r.source.executor || !sender) return refuse(`no pinned deployment on ${r.source.name}
|
|
464
|
+
if (!gateway || !r.source.portal || !r.source.executor || !sender) return refuse(`no pinned deployment on ${r.source.name}`, "unsupported_pair");
|
|
457
465
|
const to = r.source.family === "tron" ? tronToEvm(call.to) : typeof call.to === "string" && isAddress(call.to, { strict: false }) ? call.to : null;
|
|
458
|
-
if (!to || !same(to, gateway)) refuse(`the transaction calls ${call.to}, not the pinned ${r.source.name} MetaRouterGateway
|
|
459
|
-
if (typeof call.value !== "string" || !/^\d+$/.test(call.value)) refuse("the transaction value is not a number");
|
|
466
|
+
if (!to || !same(to, gateway)) refuse(`the transaction calls ${call.to}, not the pinned ${r.source.name} MetaRouterGateway`, "vault_mismatch");
|
|
467
|
+
if (typeof call.value !== "string" || !/^\d+$/.test(call.value)) refuse("the transaction value is not a number", "policy_refused");
|
|
460
468
|
const native = r.sellToken === null;
|
|
461
469
|
const sell = native ? ZERO : account20(r.source, r.sellToken);
|
|
462
|
-
if (!sell) return refuse("the sell token is not a contract address");
|
|
463
|
-
if (BigInt(call.value) !== (native ? r.amountIn : 0n)) refuse(`the transaction attaches ${call.value}, expected ${native ? r.amountIn : 0n}
|
|
470
|
+
if (!sell) return refuse("the sell token is not a contract address", "unsupported_pair");
|
|
471
|
+
if (BigInt(call.value) !== (native ? r.amountIn : 0n)) refuse(`the transaction attaches ${call.value}, expected ${native ? r.amountIn : 0n}`, "policy_refused");
|
|
464
472
|
const [t] = decode(ABI, call.data, SYMBIOSIS_META_ROUTE_SELECTOR, "metaRoute");
|
|
465
|
-
if (t.amount !== r.amountIn) refuse(`the call sells ${t.amount}, not ${r.amountIn}
|
|
466
|
-
if (t.nativeIn !== native) refuse("the call's native flag does not match the sell token");
|
|
467
|
-
if (t.approvedTokens.length < 1 || t.approvedTokens.length > 2) refuse("unexpected token list");
|
|
468
|
-
if (!same(t.approvedTokens[0], sell)) refuse(`the call sells token ${t.approvedTokens[0]}, not the requested one
|
|
469
|
-
if (t.secondSwapCalldata !== "0x") refuse("the call carries a second source-chain swap");
|
|
470
|
-
if (!same(t.relayRecipient, r.source.portal)) refuse(`the call hands the funds to ${t.relayRecipient}, not the pinned ${r.source.name} Portal
|
|
473
|
+
if (t.amount !== r.amountIn) refuse(`the call sells ${t.amount}, not ${r.amountIn}`, "policy_refused");
|
|
474
|
+
if (t.nativeIn !== native) refuse("the call's native flag does not match the sell token", "policy_refused");
|
|
475
|
+
if (t.approvedTokens.length < 1 || t.approvedTokens.length > 2) refuse("unexpected token list", "policy_refused");
|
|
476
|
+
if (!same(t.approvedTokens[0], sell)) refuse(`the call sells token ${t.approvedTokens[0]}, not the requested one`, "policy_refused");
|
|
477
|
+
if (t.secondSwapCalldata !== "0x") refuse("the call carries a second source-chain swap", "policy_refused");
|
|
478
|
+
if (!same(t.relayRecipient, r.source.portal)) refuse(`the call hands the funds to ${t.relayRecipient}, not the pinned ${r.source.name} Portal`, "vault_mismatch");
|
|
471
479
|
const [s] = decode(ABI, t.otherSideCalldata, SEL_META_SYNTHESIZE, "metaSynthesize");
|
|
472
480
|
const bridged = t.approvedTokens[t.approvedTokens.length - 1];
|
|
473
|
-
if (!same(s.rtoken, bridged)) refuse("the bridged token is not the last approved token");
|
|
474
|
-
if (same(s.rtoken, ZERO)) refuse("the bridged token is the zero address");
|
|
475
|
-
if (!same(s.syntCaller, sender)) refuse(`the bridge request is made on behalf of ${s.syntCaller}, not the sender
|
|
481
|
+
if (!same(s.rtoken, bridged)) refuse("the bridged token is not the last approved token", "policy_refused");
|
|
482
|
+
if (same(s.rtoken, ZERO)) refuse("the bridged token is the zero address", "policy_refused");
|
|
483
|
+
if (!same(s.syntCaller, sender)) refuse(`the bridge request is made on behalf of ${s.syntCaller}, not the sender`, "recipient_mismatch");
|
|
476
484
|
let sourceSwap = null;
|
|
477
485
|
if (t.firstSwapCalldata === "0x") {
|
|
478
|
-
if (native) refuse("a native sale without a wrap step");
|
|
479
|
-
if (!same(t.firstDexRouter, ZERO)) refuse("a source swap target without a call");
|
|
480
|
-
if (!same(s.rtoken, sell)) refuse("no source swap, yet the bridged token is not the sell token");
|
|
481
|
-
if (s.amount !== r.amountIn) refuse(`the bridge request moves ${s.amount}, not ${r.amountIn}
|
|
486
|
+
if (native) refuse("a native sale without a wrap step", "policy_refused");
|
|
487
|
+
if (!same(t.firstDexRouter, ZERO)) refuse("a source swap target without a call", "policy_refused");
|
|
488
|
+
if (!same(s.rtoken, sell)) refuse("no source swap, yet the bridged token is not the sell token", "policy_refused");
|
|
489
|
+
if (s.amount !== r.amountIn) refuse(`the bridge request moves ${s.amount}, not ${r.amountIn}`, "policy_refused");
|
|
482
490
|
} else if (native && lower(t.firstSwapCalldata) === SEL_WRAP) {
|
|
483
|
-
if (!same(t.firstDexRouter, s.rtoken)) refuse("the wrap step does not call the bridged token");
|
|
484
|
-
if (s.amount !== r.amountIn) refuse(`the bridge request moves ${s.amount}, not ${r.amountIn}
|
|
491
|
+
if (!same(t.firstDexRouter, s.rtoken)) refuse("the wrap step does not call the bridged token", "vault_mismatch");
|
|
492
|
+
if (s.amount !== r.amountIn) refuse(`the bridge request moves ${s.amount}, not ${r.amountIn}`, "policy_refused");
|
|
485
493
|
} else {
|
|
486
|
-
if (t.approvedTokens.length !== 2) refuse("a source swap without the token it buys");
|
|
494
|
+
if (t.approvedTokens.length !== 2) refuse("a source swap without the token it buys", "policy_refused");
|
|
487
495
|
const swap = decodeAggregatorSwap(r.source, t.firstDexRouter, t.firstSwapCalldata, "source swap");
|
|
488
|
-
if (!same(swap.srcToken, native ? AGGREGATOR_NATIVE : sell)) refuse("the source swap sells another token than the one requested");
|
|
489
|
-
if (swap.amount !== null && swap.amount !== r.amountIn) refuse(`the source swap sells ${swap.amount}, not ${r.amountIn}
|
|
490
|
-
if (!same(swap.dstToken, s.rtoken) || same(s.rtoken, sell)) refuse("the source swap does not buy the bridged token");
|
|
491
|
-
if (!same(swap.dstReceiver, r.source.executor)) refuse(`the source swap pays ${swap.dstReceiver}, not the pinned ${r.source.name} MetaRouter
|
|
492
|
-
if (swap.minReturnAmount * 10000n < s.amount * CONSISTENCY_BPS) refuse("the source swap's minimum is far below the amount the bridge request states");
|
|
496
|
+
if (!same(swap.srcToken, native ? AGGREGATOR_NATIVE : sell)) refuse("the source swap sells another token than the one requested", "policy_refused");
|
|
497
|
+
if (swap.amount !== null && swap.amount !== r.amountIn) refuse(`the source swap sells ${swap.amount}, not ${r.amountIn}`, "policy_refused");
|
|
498
|
+
if (!same(swap.dstToken, s.rtoken) || same(s.rtoken, sell)) refuse("the source swap does not buy the bridged token", "policy_refused");
|
|
499
|
+
if (!same(swap.dstReceiver, r.source.executor)) refuse(`the source swap pays ${swap.dstReceiver}, not the pinned ${r.source.name} MetaRouter`, "vault_mismatch");
|
|
500
|
+
if (swap.minReturnAmount * 10000n < s.amount * CONSISTENCY_BPS) refuse("the source swap's minimum is far below the amount the bridge request states", "price_check_failed");
|
|
493
501
|
sourceSwap = swap.router;
|
|
494
502
|
}
|
|
495
503
|
const rest = checkSynthesize(r, s, ownSet(r), minOut);
|
|
@@ -497,46 +505,46 @@ function checkSymbiosisCall(r, call, minOut) {
|
|
|
497
505
|
}
|
|
498
506
|
var hexAddress = (s) => `0x${s.hex(20)}`;
|
|
499
507
|
function checkSymbiosisTonMessage(r, msg, minOut) {
|
|
500
|
-
if (r.source.family !== "ton") refuse("not a TON source");
|
|
501
|
-
if (minOut <= 0n) refuse("the quote names no minimum");
|
|
502
|
-
if (r.sellToken === null) refuse("native TON sources are not served");
|
|
508
|
+
if (r.source.family !== "ton") refuse("not a TON source", "unsupported_pair");
|
|
509
|
+
if (minOut <= 0n) refuse("the quote names no minimum", "price_check_failed");
|
|
510
|
+
if (r.sellToken === null) refuse("native TON sources are not served", "unsupported_pair");
|
|
503
511
|
const master = tonRawAddress(r.sellToken);
|
|
504
512
|
const sender = tonRawAddress(r.sender);
|
|
505
513
|
const rtoken = symbiosisToken(r.source, r.sellToken)?.address;
|
|
506
|
-
if (!master || !sender || !rtoken) return refuse("malformed TON address in the request");
|
|
507
|
-
if (!tonRawAddress(msg.to)) refuse("the message is not addressed to a TON account");
|
|
508
|
-
if (typeof msg.value !== "string" || !/^\d+$/.test(msg.value)) refuse("the attached value is not a number");
|
|
514
|
+
if (!master || !sender || !rtoken) return refuse("malformed TON address in the request", "unsupported_pair");
|
|
515
|
+
if (!tonRawAddress(msg.to)) refuse("the message is not addressed to a TON account", "policy_refused");
|
|
516
|
+
if (typeof msg.value !== "string" || !/^\d+$/.test(msg.value)) refuse("the attached value is not a number", "policy_refused");
|
|
509
517
|
const value = BigInt(msg.value);
|
|
510
|
-
if (value <= 0n || value > SYMBIOSIS_TON_VALUE_CEILING) refuse(`the message attaches ${value} nanotons, above the ${SYMBIOSIS_TON_VALUE_CEILING} ceiling
|
|
518
|
+
if (value <= 0n || value > SYMBIOSIS_TON_VALUE_CEILING) refuse(`the message attaches ${value} nanotons, above the ${SYMBIOSIS_TON_VALUE_CEILING} ceiling`, "policy_refused");
|
|
511
519
|
const own = ownSet(r);
|
|
512
520
|
let root;
|
|
513
521
|
try {
|
|
514
522
|
root = parseBoc(msg.bodyBoc);
|
|
515
523
|
} catch (e) {
|
|
516
|
-
return refuse(`the message body does not parse: ${e.message}
|
|
524
|
+
return refuse(`the message body does not parse: ${e.message}`, "policy_refused");
|
|
517
525
|
}
|
|
518
526
|
try {
|
|
519
527
|
const body = new Slice(root);
|
|
520
|
-
if (body.uint(32) !== OP_JETTON_TRANSFER) refuse("the message is not a jetton transfer");
|
|
528
|
+
if (body.uint(32) !== OP_JETTON_TRANSFER) refuse("the message is not a jetton transfer", "policy_refused");
|
|
521
529
|
body.uint(64);
|
|
522
530
|
const amount = body.coins();
|
|
523
|
-
if (amount !== r.amountIn) refuse(`the transfer moves ${amount}, not ${r.amountIn}
|
|
524
|
-
if (body.address() !== SYMBIOSIS_TON_PORTAL_RAW) refuse("the transfer does not go to the pinned TON Portal");
|
|
525
|
-
if (body.address() !== sender) refuse("the transfer's excess does not return to the sender");
|
|
526
|
-
if (body.bool()) refuse("the transfer carries a custom payload");
|
|
531
|
+
if (amount !== r.amountIn) refuse(`the transfer moves ${amount}, not ${r.amountIn}`, "policy_refused");
|
|
532
|
+
if (body.address() !== SYMBIOSIS_TON_PORTAL_RAW) refuse("the transfer does not go to the pinned TON Portal", "vault_mismatch");
|
|
533
|
+
if (body.address() !== sender) refuse("the transfer's excess does not return to the sender", "recipient_mismatch");
|
|
534
|
+
if (body.bool()) refuse("the transfer carries a custom payload", "policy_refused");
|
|
527
535
|
const forwardTon = body.coins();
|
|
528
|
-
if (forwardTon >= value) refuse("the transfer forwards more TON than the message attaches");
|
|
529
|
-
if (!body.bool()) refuse("the bridge request is not carried in a reference");
|
|
536
|
+
if (forwardTon >= value) refuse("the transfer forwards more TON than the message attaches", "policy_refused");
|
|
537
|
+
if (!body.bool()) refuse("the bridge request is not carried in a reference", "policy_refused");
|
|
530
538
|
const request = new Slice(body.nextRef());
|
|
531
539
|
body.end();
|
|
532
|
-
if (request.uint(32) !== OP_TON_META_SYNTHESIZE) refuse("the forward payload is not a Symbiosis bridge request");
|
|
540
|
+
if (request.uint(32) !== OP_TON_META_SYNTHESIZE) refuse("the forward payload is not a Symbiosis bridge request", "policy_refused");
|
|
533
541
|
const head = new Slice(request.nextRef());
|
|
534
542
|
const sides = new Slice(request.nextRef());
|
|
535
543
|
const tail = new Slice(request.nextRef());
|
|
536
544
|
request.end();
|
|
537
545
|
const stableBridgingFee = head.coins();
|
|
538
|
-
if (head.address() !== master) refuse("the bridge request names a jetton that is not the sell token");
|
|
539
|
-
if (head.coins() !== r.amountIn) refuse("the bridge request moves an amount that is not the sell amount");
|
|
546
|
+
if (head.address() !== master) refuse("the bridge request names a jetton that is not the sell token", "policy_refused");
|
|
547
|
+
if (head.coins() !== r.amountIn) refuse("the bridge request moves an amount that is not the sell amount", "policy_refused");
|
|
540
548
|
const chain2address = hexAddress(head);
|
|
541
549
|
head.end();
|
|
542
550
|
const receiveSide = hexAddress(sides);
|
|
@@ -553,11 +561,11 @@ function checkSymbiosisTonMessage(r, msg, minOut) {
|
|
|
553
561
|
const finalCell = tail.nextRef();
|
|
554
562
|
const secondCell = tail.nextRef();
|
|
555
563
|
tail.end();
|
|
556
|
-
if (swapTokens.bits % 160 !== 0 || swapTokens.bits > 320 || swapTokens.refs.length !== 0) refuse("malformed swap token list");
|
|
564
|
+
if (swapTokens.bits % 160 !== 0 || swapTokens.bits > 320 || swapTokens.refs.length !== 0) refuse("malformed swap token list", "policy_refused");
|
|
557
565
|
const tokens = new Slice(swapTokens);
|
|
558
566
|
const swapList = [];
|
|
559
567
|
for (let i = 0; i < swapTokens.bits / 160; i++) swapList.push(hexAddress(tokens));
|
|
560
|
-
if (finalLength > 16384n || secondLength > 65536n) refuse("oversized calldata in the bridge request");
|
|
568
|
+
if (finalLength > 16384n || secondLength > 65536n) refuse("oversized calldata in the bridge request", "policy_refused");
|
|
561
569
|
const finalCalldata = `0x${snakeHex(finalCell, bigintToNumber2(finalLength))}`;
|
|
562
570
|
const secondSwapCalldata = `0x${snakeHex(secondCell, bigintToNumber2(secondLength))}`;
|
|
563
571
|
const rest = checkSynthesize(r, {
|
|
@@ -579,31 +587,31 @@ function checkSymbiosisTonMessage(r, msg, minOut) {
|
|
|
579
587
|
return { sourceSwap: null, ...rest };
|
|
580
588
|
} catch (e) {
|
|
581
589
|
if (e instanceof Refusal) throw e;
|
|
582
|
-
return refuse(`the message body is not a Symbiosis bridge request: ${e.message}
|
|
590
|
+
return refuse(`the message body is not a Symbiosis bridge request: ${e.message}`, "policy_refused");
|
|
583
591
|
}
|
|
584
592
|
}
|
|
585
593
|
function quotedMinimum(quote) {
|
|
586
594
|
const m = quote.minDestAmount;
|
|
587
|
-
if (typeof m !== "string" || !/^\d+$/.test(m) || BigInt(m) <= 0n) return refuse("the quote names no minimum");
|
|
595
|
+
if (typeof m !== "string" || !/^\d+$/.test(m) || BigInt(m) <= 0n) return refuse("the quote names no minimum", "price_check_failed");
|
|
588
596
|
return BigInt(m);
|
|
589
597
|
}
|
|
590
598
|
var reason = (e) => `symbiosis: ${e instanceof Error ? e.message : String(e)}`;
|
|
591
599
|
function checkRoute(r, tx, minOut) {
|
|
592
600
|
if (r.source.family === "ton") {
|
|
593
|
-
if (tx.data !== void 0 || tx.approvalAddress !== void 0) refuse("a TON source carries no calldata or approval");
|
|
594
|
-
if (tx.bodyBoc === void 0) refuse("a TON source needs a message body");
|
|
601
|
+
if (tx.data !== void 0 || tx.approvalAddress !== void 0) refuse("a TON source carries no calldata or approval", "policy_refused");
|
|
602
|
+
if (tx.bodyBoc === void 0) refuse("a TON source needs a message body", "policy_refused");
|
|
595
603
|
return checkSymbiosisTonMessage(r, { to: tx.to ?? "", value: tx.value ?? "0", bodyBoc: tx.bodyBoc }, minOut);
|
|
596
604
|
}
|
|
597
|
-
if (r.source.family !== "evm" && r.source.family !== "tron") refuse(`${r.source.name} sources carry nothing this module can check
|
|
598
|
-
if (tx.bodyBoc !== void 0) refuse("unexpected message body");
|
|
599
|
-
if (tx.chainId !== r.source.id) refuse(`the transaction is for chain ${String(tx.chainId)}, not ${r.source.name}
|
|
605
|
+
if (r.source.family !== "evm" && r.source.family !== "tron") refuse(`${r.source.name} sources carry nothing this module can check`, "recipient_unverifiable");
|
|
606
|
+
if (tx.bodyBoc !== void 0) refuse("unexpected message body", "policy_refused");
|
|
607
|
+
if (tx.chainId !== r.source.id) refuse(`the transaction is for chain ${String(tx.chainId)}, not ${r.source.name}`, "policy_refused");
|
|
600
608
|
const gateway = gatewayAddress(r.source);
|
|
601
609
|
if (r.sellToken === null) {
|
|
602
|
-
if (tx.approvalAddress !== void 0) refuse("a native sale asks for an approval");
|
|
610
|
+
if (tx.approvalAddress !== void 0) refuse("a native sale asks for an approval", "policy_refused");
|
|
603
611
|
} else if (tx.approvalAddress === void 0 || lower(tx.approvalAddress) !== lower(gateway)) {
|
|
604
|
-
refuse(`the approval names ${String(tx.approvalAddress)}, not the pinned ${r.source.name} MetaRouterGateway
|
|
612
|
+
refuse(`the approval names ${String(tx.approvalAddress)}, not the pinned ${r.source.name} MetaRouterGateway`, "vault_mismatch");
|
|
605
613
|
}
|
|
606
|
-
if (r.source.family === "tron" && tx.approvalAddress !== void 0 && tx.approvalAddress !== gateway) refuse("the approval does not name the pinned Tron MetaRouterGateway");
|
|
614
|
+
if (r.source.family === "tron" && tx.approvalAddress !== void 0 && tx.approvalAddress !== gateway) refuse("the approval does not name the pinned Tron MetaRouterGateway", "vault_mismatch");
|
|
607
615
|
return checkSymbiosisCall(r, { to: tx.to ?? "", data: tx.data ?? "", value: tx.value ?? "0" }, minOut);
|
|
608
616
|
}
|
|
609
617
|
function symbiosisTonMessage(quote) {
|
|
@@ -613,22 +621,22 @@ function symbiosisTonMessage(quote) {
|
|
|
613
621
|
function symbiosisInspectQuote(params, quote, options = {}) {
|
|
614
622
|
try {
|
|
615
623
|
const r = symbiosisRequest(params, options);
|
|
616
|
-
if (typeof r === "string") return { refusal: `symbiosis: ${r}
|
|
617
|
-
if (quote.sourceAmount !== params.amountIn) return { refusal: "symbiosis: the quote is for a different amount" };
|
|
624
|
+
if (typeof r === "string") return { refusal: noted(`symbiosis: ${r}`, notes.reasonOf(r)) };
|
|
625
|
+
if (quote.sourceAmount !== params.amountIn) return { refusal: noted("symbiosis: the quote is for a different amount", "policy_refused") };
|
|
618
626
|
if (r.source.family === "bitcoin") {
|
|
619
|
-
return quote.txRequest === void 0 && quote.depositAddress !== "" && symbiosisTonMessage(quote) === null ? { bound: null } : { refusal: "symbiosis: a Bitcoin source is a deposit-address route" };
|
|
627
|
+
return quote.txRequest === void 0 && quote.depositAddress !== "" && symbiosisTonMessage(quote) === null ? { bound: null } : { refusal: noted("symbiosis: a Bitcoin source is a deposit-address route", "policy_refused") };
|
|
620
628
|
}
|
|
621
629
|
if (r.source.family === "ton") {
|
|
622
630
|
const msg = symbiosisTonMessage(quote);
|
|
623
|
-
if (!msg || quote.txRequest !== void 0 || quote.depositAddress !== "") return { refusal: "symbiosis: a TON source carries one jetton transfer" };
|
|
631
|
+
if (!msg || quote.txRequest !== void 0 || quote.depositAddress !== "") return { refusal: noted("symbiosis: a TON source carries one jetton transfer", "policy_refused") };
|
|
624
632
|
return { bound: checkRoute(r, msg, quotedMinimum(quote)) };
|
|
625
633
|
}
|
|
626
634
|
const tx = quote.txRequest;
|
|
627
|
-
if (!tx || quote.depositAddress !== "") return { refusal: "symbiosis: the quote carries no transaction" };
|
|
628
|
-
if (tx.serializedTx !== void 0) return { refusal: "symbiosis: unexpected serialized transaction" };
|
|
635
|
+
if (!tx || quote.depositAddress !== "") return { refusal: noted("symbiosis: the quote carries no transaction", "policy_refused") };
|
|
636
|
+
if (tx.serializedTx !== void 0) return { refusal: noted("symbiosis: unexpected serialized transaction", "policy_refused") };
|
|
629
637
|
return { bound: checkRoute(r, tx, quotedMinimum(quote)) };
|
|
630
638
|
} catch (e) {
|
|
631
|
-
return { refusal: reason(e) };
|
|
639
|
+
return { refusal: noted(reason(e), refusalOf(e)) };
|
|
632
640
|
}
|
|
633
641
|
}
|
|
634
642
|
function symbiosisVerifyQuote(params, quote, options = {}) {
|
|
@@ -637,18 +645,18 @@ function symbiosisVerifyQuote(params, quote, options = {}) {
|
|
|
637
645
|
}
|
|
638
646
|
function symbiosisVerifyBuild(quote, tx, params, options = {}) {
|
|
639
647
|
try {
|
|
640
|
-
if (!params) return "symbiosis: the build cannot be checked without the request";
|
|
648
|
+
if (!params) return noted("symbiosis: the build cannot be checked without the request", "policy_refused");
|
|
641
649
|
const r = symbiosisRequest(params, options);
|
|
642
|
-
if (typeof r === "string") return `symbiosis: ${r}
|
|
643
|
-
if (symbiosisChain(tx.chain) !== r.source) return "symbiosis: the build is for another chain";
|
|
644
|
-
if (tx.serializedTx !== void 0 || tx.stateInitBoc !== void 0 || tx.typedData !== void 0 || tx.memo != null) return "symbiosis: the build carries a part this route never has";
|
|
650
|
+
if (typeof r === "string") return noted(`symbiosis: ${r}`, notes.reasonOf(r));
|
|
651
|
+
if (symbiosisChain(tx.chain) !== r.source) return noted("symbiosis: the build is for another chain", "policy_refused");
|
|
652
|
+
if (tx.serializedTx !== void 0 || tx.stateInitBoc !== void 0 || tx.typedData !== void 0 || tx.memo != null) return noted("symbiosis: the build carries a part this route never has", "policy_refused");
|
|
645
653
|
if (r.source.family === "bitcoin") {
|
|
646
|
-
return tx.data === void 0 && tx.bodyBoc === void 0 && tx.to === quote.depositAddress && tx.to !== "" ? null : "symbiosis: a Bitcoin source pays the quote's deposit address";
|
|
654
|
+
return tx.data === void 0 && tx.bodyBoc === void 0 && tx.to === quote.depositAddress && tx.to !== "" ? null : noted("symbiosis: a Bitcoin source pays the quote's deposit address", "vault_mismatch");
|
|
647
655
|
}
|
|
648
656
|
checkRoute(r, tx, quotedMinimum(quote));
|
|
649
657
|
return null;
|
|
650
658
|
} catch (e) {
|
|
651
|
-
return reason(e);
|
|
659
|
+
return noted(reason(e), refusalOf(e));
|
|
652
660
|
}
|
|
653
661
|
}
|
|
654
662
|
function symbiosisPinsFor(options = {}) {
|
|
@@ -661,12 +669,15 @@ function symbiosisPinsFor(options = {}) {
|
|
|
661
669
|
spenders: targets,
|
|
662
670
|
verifyQuote: (params, quote) => symbiosisVerifyQuote(params, quote, options),
|
|
663
671
|
verifyBuild: (quote, tx, params) => symbiosisVerifyBuild(quote, tx, params, options),
|
|
672
|
+
// The reason of each text verifyQuote / verifyBuild returned (remoteProvider puts it on its refusal).
|
|
673
|
+
reasonOf: notes.reasonOf,
|
|
664
674
|
...options.btcSource ? { depositAddressRoutes: true } : {}
|
|
665
675
|
};
|
|
666
676
|
}
|
|
667
677
|
var symbiosisPins = symbiosisPinsFor();
|
|
668
678
|
|
|
669
679
|
export {
|
|
680
|
+
notes,
|
|
670
681
|
SYMBIOSIS_META_ROUTE_SELECTOR,
|
|
671
682
|
SYMBIOSIS_META_ROUTE_SIGNATURE,
|
|
672
683
|
SYMBIOSIS_TON_VALUE_CEILING,
|
|
@@ -680,4 +691,4 @@ export {
|
|
|
680
691
|
symbiosisPinsFor,
|
|
681
692
|
symbiosisPins
|
|
682
693
|
};
|
|
683
|
-
//# sourceMappingURL=chunk-
|
|
694
|
+
//# sourceMappingURL=chunk-BLPE6M3U.js.map
|