@rabby-wallet/rabby-swap 0.0.89-alpha.0 → 0.0.89-alpha.1

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.
@@ -3,7 +3,24 @@ import type { DecodeCalldataOptions, DecodeCalldataResult, TxWithChainId } from
3
3
  import type { QuoteResult } from "../quote";
4
4
  /** 解码出的 minReceive 相对「报价 × (1-滑点)」允许的相对偏差。 */
5
5
  export declare const CALLDATA_MIN_RECEIVE_TOLERANCE = 0.05;
6
- /** wrap、无 dex、无 tx、解码失败时返回 null。 */
6
+ /**
7
+ * unsupported_selector: the top-level function is not supported by the parser.
8
+ * decode_error: decoding threw; the input or parser needs investigation.
9
+ * returned_null: the decoder returned no fields without throwing.
10
+ * Diagnostic only: never attach raw errors or transaction data.
11
+ */
12
+ export type SwapCalldataDecodeFailure = {
13
+ reason: "unsupported_selector" | "decode_error" | "returned_null";
14
+ selector?: string;
15
+ errorCode?: string;
16
+ };
17
+ export type SwapCalldataDecodeResult = {
18
+ decoded: DecodeCalldataResult | null;
19
+ decodeFailure?: SwapCalldataDecodeFailure;
20
+ };
21
+ /** Preserve decode failures separately from the existing verification policy. */
22
+ export declare const decodeSwapCalldataWithDiagnostics: (dexId: DEX_ENUM | null, tx?: TxWithChainId, options?: DecodeCalldataOptions) => SwapCalldataDecodeResult;
23
+ /** wrap、无 dex、无 tx、解码失败时返回 null,保持原有接口。 */
7
24
  export declare const decodeSwapCalldata: (dexId: DEX_ENUM | null, tx?: TxWithChainId, options?: DecodeCalldataOptions) => DecodeCalldataResult | null;
8
25
  /** token、支付数量、最低到手比对;收款地址由 receiver 模块校验。 */
9
26
  export declare const matchDecodedCalldata: (decoded: DecodeCalldataResult, quote: QuoteResult, slippageRatio: string | number, requirePositiveMinimum?: boolean) => boolean;
@@ -7,17 +7,38 @@ import { decodeCalldata } from "../quote";
7
7
  import { isSameAddress } from "../utils";
8
8
  /** 解码出的 minReceive 相对「报价 × (1-滑点)」允许的相对偏差。 */
9
9
  export const CALLDATA_MIN_RECEIVE_TOLERANCE = 0.05;
10
- /** wrap、无 dex、无 tx、解码失败时返回 null。 */
11
- export const decodeSwapCalldata = (dexId, tx, options = {}) => {
10
+ /** Preserve decode failures separately from the existing verification policy. */
11
+ export const decodeSwapCalldataWithDiagnostics = (dexId, tx, options = {}) => {
12
12
  if (!dexId || dexId === DEX_ENUM.WRAPTOKEN || !tx)
13
- return null;
13
+ return { decoded: null };
14
+ const selector = typeof tx.data === "string" && /^0x[0-9a-f]{8}/i.test(tx.data)
15
+ ? tx.data.slice(0, 10).toLowerCase()
16
+ : undefined;
14
17
  try {
15
- return decodeCalldata(dexId, tx, options);
18
+ const decoded = decodeCalldata(dexId, tx, options);
19
+ return decoded
20
+ ? { decoded }
21
+ : { decoded, decodeFailure: { reason: "returned_null", selector } };
16
22
  }
17
- catch (_a) {
18
- return null;
23
+ catch (error) {
24
+ const decodeError = error;
25
+ const unsupportedSelector = !!selector &&
26
+ ((decodeError === null || decodeError === void 0 ? void 0 : decodeError.reason) === "no matching function" ||
27
+ (decodeError === null || decodeError === void 0 ? void 0 : decodeError.message) === `Unsupported Magpie calldata selector: ${selector}`);
28
+ return {
29
+ decoded: null,
30
+ decodeFailure: {
31
+ reason: unsupportedSelector ? "unsupported_selector" : "decode_error",
32
+ selector,
33
+ errorCode: typeof (decodeError === null || decodeError === void 0 ? void 0 : decodeError.code) === "string"
34
+ ? decodeError.code
35
+ : "UNKNOWN_ERROR",
36
+ },
37
+ };
19
38
  }
20
39
  };
40
+ /** wrap、无 dex、无 tx、解码失败时返回 null,保持原有接口。 */
41
+ export const decodeSwapCalldata = (dexId, tx, options = {}) => decodeSwapCalldataWithDiagnostics(dexId, tx, options).decoded;
21
42
  /** token、支付数量、最低到手比对;收款地址由 receiver 模块校验。 */
22
43
  export const matchDecodedCalldata = (decoded, quote, slippageRatio, requirePositiveMinimum = false) => {
23
44
  const minimum = new BigNumber(quote.toTokenAmount).times(new BigNumber(1).minus(slippageRatio));
@@ -5,8 +5,8 @@
5
5
  export { isSwapWrapToken } from "./wrap";
6
6
  export { getRouter, getSpender, verifyRouterAndSpender } from "./contracts";
7
7
  export type { VerifyRouterAndSpenderParams } from "./contracts";
8
- export { CALLDATA_MIN_RECEIVE_TOLERANCE, decodeSwapCalldata, matchDecodedCalldata, shouldSkipCalldataMatch, verifyCalldata, } from "./calldata";
9
- export type { VerifyCalldataParams } from "./calldata";
8
+ export { CALLDATA_MIN_RECEIVE_TOLERANCE, decodeSwapCalldata, decodeSwapCalldataWithDiagnostics, matchDecodedCalldata, shouldSkipCalldataMatch, verifyCalldata, } from "./calldata";
9
+ export type { VerifyCalldataParams, SwapCalldataDecodeFailure, SwapCalldataDecodeResult, } from "./calldata";
10
10
  export { isSwapCalldataReceiverAllowed } from "./receiver";
11
11
  export { verifySdk } from "./sdk";
12
12
  export type { VerifySdkParams, VerifySdkResult } from "./sdk";
@@ -4,6 +4,6 @@
4
4
  */
5
5
  export { isSwapWrapToken } from "./wrap";
6
6
  export { getRouter, getSpender, verifyRouterAndSpender } from "./contracts";
7
- export { CALLDATA_MIN_RECEIVE_TOLERANCE, decodeSwapCalldata, matchDecodedCalldata, shouldSkipCalldataMatch, verifyCalldata, } from "./calldata";
7
+ export { CALLDATA_MIN_RECEIVE_TOLERANCE, decodeSwapCalldata, decodeSwapCalldataWithDiagnostics, matchDecodedCalldata, shouldSkipCalldataMatch, verifyCalldata, } from "./calldata";
8
8
  export { isSwapCalldataReceiverAllowed } from "./receiver";
9
9
  export { verifySdk } from "./sdk";
@@ -2,6 +2,7 @@ import { CHAINS_ENUM } from "@debank/common";
2
2
  import { DEX_ENUM } from "../consts";
3
3
  import type { DecodeCalldataResult } from "../calldata/types";
4
4
  import type { QuoteResult } from "../quote";
5
+ import type { SwapCalldataDecodeFailure } from "./calldata";
5
6
  export type VerifySdkParams = {
6
7
  chain: CHAINS_ENUM;
7
8
  dexId: DEX_ENUM;
@@ -23,6 +24,8 @@ export type VerifySdkResult = {
23
24
  receiverPass: boolean;
24
25
  isSdkDataPass: boolean;
25
26
  decoded: DecodeCalldataResult | null;
27
+ /** Diagnostic only; does not change isSdkDataPass. */
28
+ decodeFailure?: SwapCalldataDecodeFailure;
26
29
  };
27
30
  /**
28
31
  * 组合 router / spender / calldata。
@@ -1,6 +1,6 @@
1
1
  import BigNumber from "bignumber.js";
2
2
  import { DEX_ENUM } from "../consts";
3
- import { decodeSwapCalldata, verifyCalldata } from "./calldata";
3
+ import { decodeSwapCalldataWithDiagnostics, verifyCalldata } from "./calldata";
4
4
  import { verifyRouterAndSpender } from "./contracts";
5
5
  import { isSwapCalldataReceiverAllowed } from "./receiver";
6
6
  import { isSwapWrapToken } from "./wrap";
@@ -26,9 +26,7 @@ export const verifySdk = (p) => {
26
26
  });
27
27
  const tx = toTxWithChainId(p.data, p.chainId);
28
28
  const slippageRatio = new BigNumber(p.slippage).div(100).toFixed();
29
- const decoded = decodeSwapCalldata(actualDexId, tx, {
30
- nativeTokenAddress: p.nativeTokenAddress,
31
- });
29
+ const { decoded, decodeFailure } = decodeSwapCalldataWithDiagnostics(actualDexId, tx, { nativeTokenAddress: p.nativeTokenAddress });
32
30
  const callDataPass = verifyCalldata({
33
31
  data: p.data,
34
32
  dexId: actualDexId,
@@ -45,5 +43,6 @@ export const verifySdk = (p) => {
45
43
  receiverPass,
46
44
  isSdkDataPass: routerPass && spenderPass && callDataPass && receiverPass,
47
45
  decoded,
46
+ decodeFailure,
48
47
  };
49
48
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rabby-wallet/rabby-swap",
3
- "version": "0.0.89-alpha.0",
3
+ "version": "0.0.89-alpha.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {