@ocap/tx-pipeline 1.30.25 → 1.31.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/esm/appchain-sovereign-hub.d.mts +160 -0
- package/esm/appchain-sovereign-hub.mjs +348 -0
- package/esm/arcvault-external-adapter.d.mts +166 -0
- package/esm/arcvault-external-adapter.mjs +376 -0
- package/esm/encrypted-nft-envelope.d.mts +172 -0
- package/esm/encrypted-nft-envelope.mjs +458 -0
- package/esm/evm-simplified-bridge.d.mts +1370 -0
- package/esm/evm-simplified-bridge.mjs +3875 -0
- package/esm/give-to-mint-karma.d.mts +472 -0
- package/esm/give-to-mint-karma.mjs +1057 -0
- package/esm/helpers/replay-guard.d.mts +15 -0
- package/esm/helpers/replay-guard.mjs +23 -0
- package/esm/index.d.mts +10 -5
- package/esm/index.mjs +14 -10
- package/esm/legacy-protocol-sunset.d.mts +251 -0
- package/esm/legacy-protocol-sunset.mjs +1519 -0
- package/esm/library.d.mts +16 -0
- package/esm/library.mjs +17 -0
- package/esm/native-abt-exchange.d.mts +2055 -0
- package/esm/native-abt-exchange.mjs +7250 -0
- package/esm/ofac-screening.d.mts +17 -0
- package/esm/ofac-screening.mjs +39 -0
- package/esm/pipes/anti-land-attack.mjs +1 -1
- package/esm/pipes/decode-itx.mjs +43 -4
- package/esm/pipes/decode-tx.d.mts +2 -3
- package/esm/pipes/decode-tx.mjs +53 -115
- package/esm/pipes/extract-signer.mjs +2 -2
- package/esm/pipes/extract-state.mjs +14 -3
- package/esm/pipes/post-execution-invariant-gate.d.mts +61 -0
- package/esm/pipes/post-execution-invariant-gate.mjs +789 -0
- package/esm/pipes/pre-flight-waf.d.mts +34 -0
- package/esm/pipes/pre-flight-waf.mjs +603 -0
- package/esm/pipes/take-state-snapshot.mjs +3 -2
- package/esm/pipes/update-owner.mjs +39 -4
- package/esm/pipes/verify-account-migration.mjs +14 -4
- package/esm/pipes/verify-blocked.mjs +1 -1
- package/esm/pipes/verify-delegation.d.mts +22 -3
- package/esm/pipes/verify-delegation.mjs +259 -36
- package/esm/pipes/verify-list-size.mjs +1 -1
- package/esm/pipes/verify-multisig-v2.mjs +15 -7
- package/esm/pipes/verify-multisig.mjs +15 -7
- package/esm/pipes/verify-sender.mjs +1 -1
- package/esm/pipes/verify-signature.mjs +35 -6
- package/esm/pipes/verify-signer.mjs +2 -2
- package/esm/pipes/verify-state-diff.mjs +3 -3
- package/esm/pipes/verify-token-access.mjs +1 -1
- package/esm/pipes/verify-token-balance.mjs +13 -5
- package/esm/pipes/verify-transferrable.d.mts +8 -1
- package/esm/pipes/verify-transferrable.mjs +3 -3
- package/esm/pipes/verify-tx-input.mjs +2 -2
- package/esm/pipes/verify-tx-size.mjs +46 -2
- package/esm/pipes/verify-tx.mjs +5 -7
- package/esm/pipes/verify-updater.mjs +1 -1
- package/esm/social-economy-factories.d.mts +346 -0
- package/esm/social-economy-factories.mjs +845 -0
- package/esm/super-suites-foundation.d.mts +513 -0
- package/esm/super-suites-foundation.mjs +1241 -0
- package/esm/transaction-block.d.mts +471 -0
- package/esm/transaction-block.mjs +1906 -0
- package/esm/uniswap-erc7683-solver.d.mts +201 -0
- package/esm/uniswap-erc7683-solver.mjs +321 -0
- package/esm/uniswap-lp-copilot.d.mts +113 -0
- package/esm/uniswap-lp-copilot.mjs +320 -0
- package/esm/uniswap-router.d.mts +335 -0
- package/esm/uniswap-router.mjs +578 -0
- package/esm/uniswap-token-factory-graduation.d.mts +187 -0
- package/esm/uniswap-token-factory-graduation.mjs +426 -0
- package/esm/uniswap-v4-hook.d.mts +173 -0
- package/esm/uniswap-v4-hook.mjs +517 -0
- package/lib/appchain-sovereign-hub.cjs +352 -0
- package/lib/appchain-sovereign-hub.d.cts +160 -0
- package/lib/arcvault-external-adapter.cjs +383 -0
- package/lib/arcvault-external-adapter.d.cts +166 -0
- package/lib/encrypted-nft-envelope.cjs +477 -0
- package/lib/encrypted-nft-envelope.d.cts +172 -0
- package/lib/evm-simplified-bridge.cjs +3943 -0
- package/lib/evm-simplified-bridge.d.cts +1370 -0
- package/lib/give-to-mint-karma.cjs +1084 -0
- package/lib/give-to-mint-karma.d.cts +472 -0
- package/lib/helpers/replay-guard.cjs +27 -0
- package/lib/helpers/replay-guard.d.cts +15 -0
- package/lib/index.cjs +49 -16
- package/lib/index.d.cts +10 -5
- package/lib/legacy-protocol-sunset.cjs +1543 -0
- package/lib/legacy-protocol-sunset.d.cts +251 -0
- package/lib/library.cjs +303 -0
- package/lib/library.d.cts +16 -0
- package/lib/native-abt-exchange.cjs +7322 -0
- package/lib/native-abt-exchange.d.cts +2055 -0
- package/lib/ofac-screening.cjs +42 -0
- package/lib/ofac-screening.d.cts +17 -0
- package/lib/pipes/anti-land-attack.cjs +2 -2
- package/lib/pipes/decode-itx.cjs +43 -4
- package/lib/pipes/decode-itx.d.cts +1 -1
- package/lib/pipes/decode-tx.cjs +52 -116
- package/lib/pipes/decode-tx.d.cts +3 -4
- package/lib/pipes/extract-signer.cjs +2 -2
- package/lib/pipes/extract-state.cjs +14 -3
- package/lib/pipes/post-execution-invariant-gate.cjs +800 -0
- package/lib/pipes/post-execution-invariant-gate.d.cts +61 -0
- package/lib/pipes/pre-flight-waf.cjs +616 -0
- package/lib/pipes/pre-flight-waf.d.cts +34 -0
- package/lib/pipes/take-state-snapshot.cjs +3 -2
- package/lib/pipes/update-owner.cjs +40 -5
- package/lib/pipes/update-owner.d.cts +1 -1
- package/lib/pipes/verify-account-migration.cjs +14 -4
- package/lib/pipes/verify-blocked.cjs +1 -1
- package/lib/pipes/verify-delegation.cjs +259 -36
- package/lib/pipes/verify-delegation.d.cts +22 -3
- package/lib/pipes/verify-list-size.cjs +1 -1
- package/lib/pipes/verify-multisig-v2.cjs +15 -7
- package/lib/pipes/verify-multisig.cjs +15 -7
- package/lib/pipes/verify-sender.cjs +1 -1
- package/lib/pipes/verify-service-fee.d.cts +1 -1
- package/lib/pipes/verify-signature.cjs +35 -6
- package/lib/pipes/verify-signature.d.cts +1 -1
- package/lib/pipes/verify-signer.cjs +2 -2
- package/lib/pipes/verify-state-diff.cjs +4 -4
- package/lib/pipes/verify-token-access.cjs +2 -2
- package/lib/pipes/verify-token-balance.cjs +13 -5
- package/lib/pipes/verify-transferrable.cjs +3 -3
- package/lib/pipes/verify-transferrable.d.cts +8 -1
- package/lib/pipes/verify-tx-input.cjs +3 -3
- package/lib/pipes/verify-tx-size.cjs +46 -2
- package/lib/pipes/verify-tx-size.d.cts +1 -1
- package/lib/pipes/verify-tx.cjs +6 -8
- package/lib/pipes/verify-tx.d.cts +1 -1
- package/lib/pipes/verify-updater.cjs +1 -1
- package/lib/social-economy-factories.cjs +852 -0
- package/lib/social-economy-factories.d.cts +346 -0
- package/lib/super-suites-foundation.cjs +1257 -0
- package/lib/super-suites-foundation.d.cts +513 -0
- package/lib/transaction-block.cjs +1941 -0
- package/lib/transaction-block.d.cts +471 -0
- package/lib/uniswap-erc7683-solver.cjs +327 -0
- package/lib/uniswap-erc7683-solver.d.cts +201 -0
- package/lib/uniswap-lp-copilot.cjs +328 -0
- package/lib/uniswap-lp-copilot.d.cts +113 -0
- package/lib/uniswap-router.cjs +593 -0
- package/lib/uniswap-router.d.cts +335 -0
- package/lib/uniswap-token-factory-graduation.cjs +431 -0
- package/lib/uniswap-token-factory-graduation.d.cts +187 -0
- package/lib/uniswap-v4-hook.cjs +527 -0
- package/lib/uniswap-v4-hook.d.cts +173 -0
- package/package.json +11 -10
- package/esm/helpers/dedup-height.d.mts +0 -16
- package/esm/helpers/dedup-height.mjs +0 -61
- package/lib/helpers/dedup-height.cjs +0 -72
- package/lib/helpers/dedup-height.d.cts +0 -16
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_ofac_screening = require('./ofac-screening.cjs');
|
|
3
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
4
|
+
let _ocap_util = require("@ocap/util");
|
|
5
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
6
|
+
|
|
7
|
+
//#region src/uniswap-router.ts
|
|
8
|
+
/**
|
|
9
|
+
* Default Integrator Tag for Uniswap Universal Router & Base Builder Attribution.
|
|
10
|
+
*/
|
|
11
|
+
const DEFAULT_OCAP_INTEGRATOR_ID = "arcblock-ocap-checkout";
|
|
12
|
+
/**
|
|
13
|
+
* Maximum allowed slippage in basis points (5.00% = 500 bps) for 1-click cross-chain zaps.
|
|
14
|
+
*/
|
|
15
|
+
const MAX_ALLOWED_ZAP_SLIPPAGE_BPS = 500;
|
|
16
|
+
/**
|
|
17
|
+
* Maximum allowed token hops in a single Universal Router path (up to 5 tokens = 4 pool hops).
|
|
18
|
+
*/
|
|
19
|
+
const MAX_ROUTE_PATH_LENGTH = 5;
|
|
20
|
+
/**
|
|
21
|
+
* Standard Uniswap Universal Router command bytes.
|
|
22
|
+
*/
|
|
23
|
+
const UNIVERSAL_ROUTER_COMMANDS = {
|
|
24
|
+
V3_SWAP_EXACT_IN: 0,
|
|
25
|
+
SWEEP: 4,
|
|
26
|
+
TRANSFER: 5,
|
|
27
|
+
PAY_PORTION: 6,
|
|
28
|
+
PERMIT2_PERMIT: 10,
|
|
29
|
+
V4_SWAP: 16
|
|
30
|
+
};
|
|
31
|
+
const FORBIDDEN_KEYS = new Set([
|
|
32
|
+
"__proto__",
|
|
33
|
+
"constructor",
|
|
34
|
+
"prototype"
|
|
35
|
+
]);
|
|
36
|
+
const CANONICAL_UINT_REGEX = /^\d+$/;
|
|
37
|
+
const EVM_HEX_ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/;
|
|
38
|
+
const EVEN_HEX_BYTES_REGEX = /^0x(?:[0-9a-fA-F]{2})+$/;
|
|
39
|
+
const SAFE_INTEGRATOR_ID_REGEX = /^[a-zA-Z0-9._-]{3,64}$/;
|
|
40
|
+
const ALLOWED_SERVICE_TYPES = new Set([
|
|
41
|
+
"blocklet_subscription",
|
|
42
|
+
"aigne_compute_credits",
|
|
43
|
+
"nft_purchase",
|
|
44
|
+
"utility_stake"
|
|
45
|
+
]);
|
|
46
|
+
function assertSafeIdentifier(value, fieldName) {
|
|
47
|
+
if (typeof value !== "string") throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", `${fieldName} must be a non-empty string`);
|
|
48
|
+
const trimmed = value.trim();
|
|
49
|
+
if (!trimmed || FORBIDDEN_KEYS.has(trimmed)) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", `Unsafe or empty ${fieldName}: ${String(value)}`);
|
|
50
|
+
return trimmed;
|
|
51
|
+
}
|
|
52
|
+
function parsePositiveUint(raw, fieldName) {
|
|
53
|
+
if (typeof raw !== "string" || !CANONICAL_UINT_REGEX.test(raw)) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", `${fieldName} must be a canonical integer string`);
|
|
54
|
+
const bn = new _ocap_util.BN(raw, 10);
|
|
55
|
+
if (bn.lte(new _ocap_util.BN(0))) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", `${fieldName} must be strictly positive`);
|
|
56
|
+
return bn;
|
|
57
|
+
}
|
|
58
|
+
function parseNonNegativeUint(raw, fieldName) {
|
|
59
|
+
if (typeof raw !== "string" || !CANONICAL_UINT_REGEX.test(raw)) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", `${fieldName} must be a canonical non-negative integer string`);
|
|
60
|
+
return new _ocap_util.BN(raw, 10);
|
|
61
|
+
}
|
|
62
|
+
function assertEvmAddress(address, fieldName) {
|
|
63
|
+
if (typeof address !== "string" || !EVM_HEX_ADDRESS_REGEX.test(address)) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", `${fieldName} must be a valid 20-byte 0x EVM address`);
|
|
64
|
+
return address.toLowerCase();
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Builds a deterministic, router-friendly Uniswap Universal Router + Permit2 payload
|
|
68
|
+
* that swaps any ERC-20 token into settlement token (`USDC` or `ABT`) and transfers it
|
|
69
|
+
* into the OCAP Rollup Vault in a single EVM transaction.
|
|
70
|
+
*
|
|
71
|
+
* Security & Compliance Invariants:
|
|
72
|
+
* 1. Rejects any non-zero `brokerageFeeBps` or `PAY_PORTION` fee-skimming (SEC Section 15(a) Safe Harbor).
|
|
73
|
+
* 2. Screens sender, router, vault, token addresses, and OCAP buyer DID against OFAC blocklist.
|
|
74
|
+
* 3. Enforces strict `maxSlippageBps <= 500` (5%), loop-free route paths (`<= 5` tokens), and valid Permit2 hex signatures.
|
|
75
|
+
* 4. Embeds official `ocap:v1:<integratorId>` attribution tag for Uniswap & Base ecosystem alignment.
|
|
76
|
+
*/
|
|
77
|
+
function buildUniversalRouterCrossChainPayload(input) {
|
|
78
|
+
if (!input || typeof input !== "object") throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", "Missing Universal Router zap input");
|
|
79
|
+
if (input.brokerageFeeBps !== void 0 && input.brokerageFeeBps !== 0) throw new _ocap_util_lib_error.CustomError("ILLEGAL_BROKERAGE_COMMISSION", "Variable bps brokerage commission on swaps is strictly prohibited (must be 0 bps for SEC Section 15(a) Safe Harbor)");
|
|
80
|
+
const chainId = assertSafeIdentifier(input.chainId, "chainId");
|
|
81
|
+
const buyerOcapDid = assertSafeIdentifier(input.buyerOcapDid, "buyerOcapDid");
|
|
82
|
+
const senderEvmAddress = assertEvmAddress(input.senderEvmAddress, "senderEvmAddress");
|
|
83
|
+
const universalRouterAddress = assertEvmAddress(input.universalRouterAddress, "universalRouterAddress");
|
|
84
|
+
const ocapVaultAddress = assertEvmAddress(input.ocapVaultAddress, "ocapVaultAddress");
|
|
85
|
+
const tokenIn = assertEvmAddress(input.tokenIn, "tokenIn");
|
|
86
|
+
const tokenOut = assertEvmAddress(input.tokenOut, "tokenOut");
|
|
87
|
+
if (tokenIn === tokenOut) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", "tokenIn and tokenOut must be distinct addresses");
|
|
88
|
+
if (!Array.isArray(input.routePath) || input.routePath.length < 2 || input.routePath.length > MAX_ROUTE_PATH_LENGTH) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", `routePath must contain between 2 and ${MAX_ROUTE_PATH_LENGTH} token addresses`);
|
|
89
|
+
const normalizedRoute = input.routePath.map((hop, i) => assertEvmAddress(hop, `routePath[${i}]`));
|
|
90
|
+
if (normalizedRoute[0] !== tokenIn || normalizedRoute[normalizedRoute.length - 1] !== tokenOut) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", "routePath endpoints must match tokenIn and tokenOut");
|
|
91
|
+
if (new Set(normalizedRoute).size !== normalizedRoute.length) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", "routePath cannot contain duplicate tokens or routing loops");
|
|
92
|
+
require_ofac_screening.assertNotOfacSanctioned([
|
|
93
|
+
senderEvmAddress,
|
|
94
|
+
buyerOcapDid,
|
|
95
|
+
universalRouterAddress,
|
|
96
|
+
ocapVaultAddress,
|
|
97
|
+
...normalizedRoute
|
|
98
|
+
], input.ofacBlocklist);
|
|
99
|
+
const amountIn = parsePositiveUint(input.amountIn, "amountIn");
|
|
100
|
+
const expectedAmountOut = parsePositiveUint(input.expectedAmountOut, "expectedAmountOut");
|
|
101
|
+
if (typeof input.maxSlippageBps !== "number" || !Number.isSafeInteger(input.maxSlippageBps) || input.maxSlippageBps < 0 || input.maxSlippageBps > MAX_ALLOWED_ZAP_SLIPPAGE_BPS) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", `maxSlippageBps must be an integer between 0 and ${MAX_ALLOWED_ZAP_SLIPPAGE_BPS}`);
|
|
102
|
+
const amountOutMinimum = expectedAmountOut.mul(new _ocap_util.BN(1e4 - input.maxSlippageBps)).div(new _ocap_util.BN(1e4));
|
|
103
|
+
if (amountOutMinimum.lte(new _ocap_util.BN(0))) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", "Computed amountOutMinimum must be strictly positive");
|
|
104
|
+
const now = input.currentTimestamp ?? Math.floor(Date.now() / 1e3);
|
|
105
|
+
if (typeof now !== "number" || !Number.isSafeInteger(now) || now < 0) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", "currentTimestamp must be a valid non-negative integer");
|
|
106
|
+
if (typeof input.deadline !== "number" || !Number.isSafeInteger(input.deadline) || input.deadline <= now) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", "Swap deadline must be a future Unix timestamp");
|
|
107
|
+
const integratorId = input.integratorId ?? DEFAULT_OCAP_INTEGRATOR_ID;
|
|
108
|
+
if (!SAFE_INTEGRATOR_ID_REGEX.test(integratorId)) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", `Invalid integratorId format: ${integratorId}`);
|
|
109
|
+
const integratorTag = `ocap:v1:${integratorId}`;
|
|
110
|
+
const commandBytes = [];
|
|
111
|
+
const commandNames = [];
|
|
112
|
+
let canonicalPermit2 = null;
|
|
113
|
+
if (input.permit2) {
|
|
114
|
+
const permitToken = assertEvmAddress(input.permit2.token, "permit2.token");
|
|
115
|
+
const permitSpender = assertEvmAddress(input.permit2.spender, "permit2.spender");
|
|
116
|
+
if (permitToken !== tokenIn) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", "permit2.token must match tokenIn");
|
|
117
|
+
if (permitSpender !== universalRouterAddress) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", "permit2.spender must match universalRouterAddress");
|
|
118
|
+
const permitAmount = parsePositiveUint(input.permit2.amount, "permit2.amount");
|
|
119
|
+
if (permitAmount.lt(amountIn)) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", "permit2.amount must be >= amountIn");
|
|
120
|
+
if (typeof input.permit2.expiration !== "number" || !Number.isSafeInteger(input.permit2.expiration) || typeof input.permit2.sigDeadline !== "number" || !Number.isSafeInteger(input.permit2.sigDeadline) || input.permit2.sigDeadline < input.deadline || input.permit2.expiration < input.deadline) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", "permit2 expiration and sigDeadline must be valid integers >= swap deadline");
|
|
121
|
+
if (typeof input.permit2.nonce !== "number" || !Number.isSafeInteger(input.permit2.nonce) || input.permit2.nonce < 0) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", "permit2.nonce must be a non-negative safe integer");
|
|
122
|
+
if (typeof input.permit2.signature !== "string" || !EVEN_HEX_BYTES_REGEX.test(input.permit2.signature)) throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", "permit2.signature must be a valid 0x-prefixed hex byte string");
|
|
123
|
+
canonicalPermit2 = {
|
|
124
|
+
token: permitToken,
|
|
125
|
+
spender: permitSpender,
|
|
126
|
+
amount: permitAmount.toString(10),
|
|
127
|
+
expiration: input.permit2.expiration,
|
|
128
|
+
sigDeadline: input.permit2.sigDeadline,
|
|
129
|
+
nonce: input.permit2.nonce,
|
|
130
|
+
signature: input.permit2.signature.toLowerCase()
|
|
131
|
+
};
|
|
132
|
+
commandBytes.push(UNIVERSAL_ROUTER_COMMANDS.PERMIT2_PERMIT);
|
|
133
|
+
commandNames.push("PERMIT2_PERMIT");
|
|
134
|
+
}
|
|
135
|
+
const poolVersion = input.poolVersion ?? "v4";
|
|
136
|
+
if (poolVersion === "v4") {
|
|
137
|
+
commandBytes.push(UNIVERSAL_ROUTER_COMMANDS.V4_SWAP);
|
|
138
|
+
commandNames.push("V4_SWAP");
|
|
139
|
+
} else if (poolVersion === "v3") {
|
|
140
|
+
commandBytes.push(UNIVERSAL_ROUTER_COMMANDS.V3_SWAP_EXACT_IN);
|
|
141
|
+
commandNames.push("V3_SWAP_EXACT_IN");
|
|
142
|
+
} else throw new _ocap_util_lib_error.CustomError("INVALID_UNISWAP_ZAP", `Unsupported poolVersion: ${String(poolVersion)}`);
|
|
143
|
+
commandBytes.push(UNIVERSAL_ROUTER_COMMANDS.SWEEP);
|
|
144
|
+
commandNames.push("SWEEP");
|
|
145
|
+
const commandsHex = `0x${commandBytes.map((b) => b.toString(16).padStart(2, "0")).join("")}`;
|
|
146
|
+
const canonicalPayloadStr = JSON.stringify({
|
|
147
|
+
chainId,
|
|
148
|
+
router: universalRouterAddress,
|
|
149
|
+
sender: senderEvmAddress,
|
|
150
|
+
buyerOcapDid,
|
|
151
|
+
vault: ocapVaultAddress,
|
|
152
|
+
commandsHex,
|
|
153
|
+
route: normalizedRoute,
|
|
154
|
+
amountIn: amountIn.toString(10),
|
|
155
|
+
amountOutMinimum: amountOutMinimum.toString(10),
|
|
156
|
+
deadline: input.deadline,
|
|
157
|
+
permit2: canonicalPermit2,
|
|
158
|
+
integratorTag
|
|
159
|
+
});
|
|
160
|
+
const calldataDigest = _ocap_mcrypto.Hasher.SHA3.hash256(Buffer.from(canonicalPayloadStr, "utf8"));
|
|
161
|
+
return {
|
|
162
|
+
chainId,
|
|
163
|
+
to: universalRouterAddress,
|
|
164
|
+
senderEvmAddress,
|
|
165
|
+
buyerOcapDid,
|
|
166
|
+
ocapVaultAddress,
|
|
167
|
+
commandsHex,
|
|
168
|
+
commandNames,
|
|
169
|
+
routePath: normalizedRoute,
|
|
170
|
+
amountIn: amountIn.toString(10),
|
|
171
|
+
expectedAmountOut: expectedAmountOut.toString(10),
|
|
172
|
+
amountOutMinimum: amountOutMinimum.toString(10),
|
|
173
|
+
maxSlippageBps: input.maxSlippageBps,
|
|
174
|
+
deadline: input.deadline,
|
|
175
|
+
integratorTag,
|
|
176
|
+
brokerageFeeBps: 0,
|
|
177
|
+
ofacOracleAddress: require_ofac_screening.CHAINALYSIS_OFAC_ORACLE_ADDRESS,
|
|
178
|
+
calldataDigest
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Builds an end-to-end "Invisible ABT Utility Fuel" Pass-Through Plan:
|
|
183
|
+
* 1. User pays any token (`USDC`, `ETH`, etc.) on Base/Ethereum via Uniswap `USDC/ABT` pool.
|
|
184
|
+
* 2. Exact `ABT` needed (`abtServicePrice + abtRelayerFee`) is deposited to OCAP and
|
|
185
|
+
* 100% consumed/staked in the same atomic `TransactionBlock` (`holdingPeriodSeconds = 0`,
|
|
186
|
+
* `netBuyerSpeculativeAbtRetained = '0'`, `treasuryCapitalAtRisk = '0'`).
|
|
187
|
+
*/
|
|
188
|
+
function buildAbtUtilityPassThroughPlan(input) {
|
|
189
|
+
if (!input || typeof input !== "object") throw new _ocap_util_lib_error.CustomError("INVALID_ABT_PASSTHROUGH", "Missing ABT utility pass-through input");
|
|
190
|
+
const buyerOcapDid = assertSafeIdentifier(input.buyerOcapDid, "buyerOcapDid");
|
|
191
|
+
const relayerOcapDid = assertSafeIdentifier(input.relayerOcapDid, "relayerOcapDid");
|
|
192
|
+
const rollupAddress = assertSafeIdentifier(input.rollupAddress, "rollupAddress");
|
|
193
|
+
const abtOcapTokenAddress = assertSafeIdentifier(input.abtOcapTokenAddress, "abtOcapTokenAddress");
|
|
194
|
+
const serviceRecipientDid = assertSafeIdentifier(input.serviceRecipientDid, "serviceRecipientDid");
|
|
195
|
+
const sourceTxEvidenceHash = assertSafeIdentifier(input.sourceTxEvidenceHash, "sourceTxEvidenceHash");
|
|
196
|
+
const targetAssetOrStakeAddress = input.targetAssetOrStakeAddress !== void 0 ? assertSafeIdentifier(input.targetAssetOrStakeAddress, "targetAssetOrStakeAddress") : void 0;
|
|
197
|
+
if (!ALLOWED_SERVICE_TYPES.has(input.serviceType)) throw new _ocap_util_lib_error.CustomError("INVALID_ABT_PASSTHROUGH", `Unsupported serviceType: ${String(input.serviceType)}`);
|
|
198
|
+
if (require_ofac_screening.normalizeScreeningAddress(buyerOcapDid) === require_ofac_screening.normalizeScreeningAddress(serviceRecipientDid) || require_ofac_screening.normalizeScreeningAddress(buyerOcapDid) === require_ofac_screening.normalizeScreeningAddress(relayerOcapDid)) throw new _ocap_util_lib_error.CustomError("SELF_DEALING_CONSUMPTIVE_BYPASS", "buyerOcapDid cannot be identical to serviceRecipientDid or relayerOcapDid in a 0-second consumptive pass-through");
|
|
199
|
+
require_ofac_screening.assertNotOfacSanctioned([
|
|
200
|
+
input.senderEvmAddress,
|
|
201
|
+
buyerOcapDid,
|
|
202
|
+
relayerOcapDid,
|
|
203
|
+
rollupAddress,
|
|
204
|
+
abtOcapTokenAddress,
|
|
205
|
+
serviceRecipientDid,
|
|
206
|
+
targetAssetOrStakeAddress
|
|
207
|
+
], input.ofacBlocklist);
|
|
208
|
+
const abtExpectedOut = parsePositiveUint(input.abtExpectedOut, "abtExpectedOut");
|
|
209
|
+
const abtServicePrice = parsePositiveUint(input.abtServicePrice, "abtServicePrice");
|
|
210
|
+
const abtRelayerFee = parseNonNegativeUint(input.abtRelayerFee, "abtRelayerFee");
|
|
211
|
+
const expectedTotalAbt = abtServicePrice.add(abtRelayerFee);
|
|
212
|
+
if (!abtExpectedOut.eq(expectedTotalAbt)) throw new _ocap_util_lib_error.CustomError("CONSUMPTIVE_USE_MISMATCH", `abtExpectedOut (${abtExpectedOut.toString(10)}) must equal abtServicePrice (${abtServicePrice.toString(10)}) + abtRelayerFee (${abtRelayerFee.toString(10)}) for 0-second consumptive pass-through`);
|
|
213
|
+
const evmSwapPayload = buildUniversalRouterCrossChainPayload({
|
|
214
|
+
chainId: input.chainId,
|
|
215
|
+
senderEvmAddress: input.senderEvmAddress,
|
|
216
|
+
buyerOcapDid,
|
|
217
|
+
universalRouterAddress: input.universalRouterAddress,
|
|
218
|
+
ocapVaultAddress: input.ocapVaultAddress,
|
|
219
|
+
tokenIn: input.paymentTokenEvmAddress,
|
|
220
|
+
tokenOut: input.abtEvmAddress,
|
|
221
|
+
routePath: input.routePath,
|
|
222
|
+
poolVersion: input.poolVersion ?? "v4",
|
|
223
|
+
amountIn: input.paymentAmountIn,
|
|
224
|
+
expectedAmountOut: abtExpectedOut.toString(10),
|
|
225
|
+
maxSlippageBps: input.maxSlippageBps,
|
|
226
|
+
deadline: input.deadline,
|
|
227
|
+
currentTimestamp: input.currentTimestamp,
|
|
228
|
+
integratorId: input.integratorId,
|
|
229
|
+
brokerageFeeBps: 0,
|
|
230
|
+
ofacBlocklist: input.ofacBlocklist
|
|
231
|
+
});
|
|
232
|
+
const consumeStatement = input.serviceType === "nft_purchase" ? {
|
|
233
|
+
type: "acquire_asset_v3",
|
|
234
|
+
typeUrl: "fg:t:acquire_asset_v3",
|
|
235
|
+
from: buyerOcapDid,
|
|
236
|
+
itx: {
|
|
237
|
+
to: serviceRecipientDid,
|
|
238
|
+
factory: targetAssetOrStakeAddress || serviceRecipientDid,
|
|
239
|
+
token: {
|
|
240
|
+
address: abtOcapTokenAddress,
|
|
241
|
+
value: abtServicePrice.toString(10)
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
} : {
|
|
245
|
+
type: "transfer_v3",
|
|
246
|
+
typeUrl: "fg:t:transfer_v3",
|
|
247
|
+
from: buyerOcapDid,
|
|
248
|
+
itx: {
|
|
249
|
+
to: serviceRecipientDid,
|
|
250
|
+
tokens: [{
|
|
251
|
+
address: abtOcapTokenAddress,
|
|
252
|
+
value: abtServicePrice.toString(10)
|
|
253
|
+
}],
|
|
254
|
+
data: {
|
|
255
|
+
type: "json",
|
|
256
|
+
value: JSON.stringify({
|
|
257
|
+
consumptiveUse: true,
|
|
258
|
+
serviceType: input.serviceType,
|
|
259
|
+
target: targetAssetOrStakeAddress || serviceRecipientDid
|
|
260
|
+
})
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
return {
|
|
265
|
+
evmSwapPayload,
|
|
266
|
+
ocapPurchaseBlockInput: {
|
|
267
|
+
relayer: relayerOcapDid,
|
|
268
|
+
buyer: buyerOcapDid,
|
|
269
|
+
deposit: {
|
|
270
|
+
rollup: rollupAddress,
|
|
271
|
+
token: {
|
|
272
|
+
address: abtOcapTokenAddress,
|
|
273
|
+
value: abtServicePrice.toString(10)
|
|
274
|
+
},
|
|
275
|
+
actualFee: abtRelayerFee.toString(10),
|
|
276
|
+
proposer: relayerOcapDid,
|
|
277
|
+
evidence: {
|
|
278
|
+
hash: sourceTxEvidenceHash,
|
|
279
|
+
targetType: "EVM",
|
|
280
|
+
targetChainId: input.chainId
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
purchaseStatements: [consumeStatement],
|
|
284
|
+
minDepositValue: input.minDepositValue,
|
|
285
|
+
minRelayerFee: input.minRelayerFee,
|
|
286
|
+
fallbackToDepositOnPurchaseFailure: input.fallbackToDepositOnPurchaseFailure ?? true,
|
|
287
|
+
ofacBlocklist: input.ofacBlocklist ? Array.from(input.ofacBlocklist) : void 0,
|
|
288
|
+
enforceZeroSum: true
|
|
289
|
+
},
|
|
290
|
+
economics: {
|
|
291
|
+
treasuryCapitalAtRisk: "0",
|
|
292
|
+
brokerageFeeBps: 0,
|
|
293
|
+
netBuyerSpeculativeAbtRetained: "0",
|
|
294
|
+
holdingPeriodSeconds: 0,
|
|
295
|
+
abtServicePrice: abtServicePrice.toString(10),
|
|
296
|
+
abtRelayerFee: abtRelayerFee.toString(10),
|
|
297
|
+
totalAbtRouted: abtExpectedOut.toString(10)
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Maximum TTL (in seconds) for a USD-anchored dynamic ABT price quote (#1274).
|
|
303
|
+
*/
|
|
304
|
+
const MAX_USD_QUOTE_TTL_SECONDS = 300;
|
|
305
|
+
/**
|
|
306
|
+
* Default TTL (in seconds) for a USD-anchored dynamic ABT price quote (#1274).
|
|
307
|
+
*/
|
|
308
|
+
const DEFAULT_USD_QUOTE_TTL_SECONDS = 60;
|
|
309
|
+
/**
|
|
310
|
+
* 18-decimal base unit scale for ABT (`10^18`).
|
|
311
|
+
*/
|
|
312
|
+
const ABT_DECIMALS_SCALE = "1000000000000000000";
|
|
313
|
+
const BYTES32_HEX_DIGEST_REGEX = /^0x[0-9a-fA-F]{64}$/;
|
|
314
|
+
function computeUsdAnchoredQuoteDigest(fields) {
|
|
315
|
+
const canonical = JSON.stringify({
|
|
316
|
+
domain: "OCAP_USD_ANCHORED_ABT_QUOTE_V1",
|
|
317
|
+
quoteId: fields.quoteId,
|
|
318
|
+
serviceId: fields.serviceId,
|
|
319
|
+
buyerOcapDid: fields.buyerOcapDid,
|
|
320
|
+
usdPriceMicro: fields.usdPriceMicro,
|
|
321
|
+
abtUsdPriceMicro: fields.abtUsdPriceMicro,
|
|
322
|
+
requiredAbtWei: fields.requiredAbtWei,
|
|
323
|
+
maxAbtWithSlippageWei: fields.maxAbtWithSlippageWei,
|
|
324
|
+
maxSlippageBps: fields.maxSlippageBps,
|
|
325
|
+
oracleSource: fields.oracleSource,
|
|
326
|
+
issuedAt: fields.issuedAt,
|
|
327
|
+
expiresAt: fields.expiresAt
|
|
328
|
+
});
|
|
329
|
+
return _ocap_mcrypto.Hasher.SHA3.hash256(Buffer.from(canonical, "utf8"));
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Issue #1274:
|
|
333
|
+
* Computes a deterministic, CEX-independent USD-anchored `ABT` price quote from a Uniswap V3/V4
|
|
334
|
+
* TWAP/Spot price (`abtUsdPriceMicro`), giving users stable USD mental pricing (`$0.10`, `$1.00`, `$10.00`)
|
|
335
|
+
* while keeping the on-chain settlement 100% pure `ABT`.
|
|
336
|
+
*/
|
|
337
|
+
function createUsdAnchoredAbtQuote(input) {
|
|
338
|
+
if (!input || typeof input !== "object") throw new _ocap_util_lib_error.CustomError("INVALID_USD_ABT_QUOTE", "Missing USD-anchored ABT quote input");
|
|
339
|
+
const quoteId = assertSafeIdentifier(input.quoteId, "quoteId");
|
|
340
|
+
const serviceId = assertSafeIdentifier(input.serviceId, "serviceId");
|
|
341
|
+
const buyerOcapDid = assertSafeIdentifier(input.buyerOcapDid, "buyerOcapDid");
|
|
342
|
+
const usdPriceMicroBn = parsePositiveUint(input.usdPriceMicro, "usdPriceMicro");
|
|
343
|
+
const abtUsdPriceMicroBn = parsePositiveUint(input.abtUsdPriceMicro, "abtUsdPriceMicro");
|
|
344
|
+
const maxSlippageBps = input.maxSlippageBps ?? 100;
|
|
345
|
+
if (typeof maxSlippageBps !== "number" || !Number.isSafeInteger(maxSlippageBps) || maxSlippageBps < 0 || maxSlippageBps > MAX_ALLOWED_ZAP_SLIPPAGE_BPS) throw new _ocap_util_lib_error.CustomError("INVALID_USD_ABT_QUOTE", `maxSlippageBps must be an integer between 0 and ${MAX_ALLOWED_ZAP_SLIPPAGE_BPS}`);
|
|
346
|
+
if (typeof input.issuedAt !== "number" || !Number.isSafeInteger(input.issuedAt) || input.issuedAt <= 0) throw new _ocap_util_lib_error.CustomError("INVALID_USD_ABT_QUOTE", "issuedAt must be a positive safe integer Unix timestamp");
|
|
347
|
+
const expiresAt = input.expiresAt ?? input.issuedAt + DEFAULT_USD_QUOTE_TTL_SECONDS;
|
|
348
|
+
if (typeof expiresAt !== "number" || !Number.isSafeInteger(expiresAt) || expiresAt <= input.issuedAt || expiresAt - input.issuedAt > MAX_USD_QUOTE_TTL_SECONDS) throw new _ocap_util_lib_error.CustomError("INVALID_USD_ABT_QUOTE", `expiresAt must be strictly after issuedAt and within MAX_USD_QUOTE_TTL_SECONDS (${MAX_USD_QUOTE_TTL_SECONDS}s)`);
|
|
349
|
+
const oracleSource = input.oracleSource ?? "UNISWAP_V4_TWAP";
|
|
350
|
+
if (oracleSource !== "UNISWAP_V4_TWAP" && oracleSource !== "UNISWAP_V3_TWAP") throw new _ocap_util_lib_error.CustomError("INVALID_USD_ABT_QUOTE", `Unsupported oracleSource: ${String(oracleSource)}`);
|
|
351
|
+
const scaleBn = new _ocap_util.BN(ABT_DECIMALS_SCALE, 10);
|
|
352
|
+
const requiredAbtBn = usdPriceMicroBn.mul(scaleBn).add(abtUsdPriceMicroBn.sub(new _ocap_util.BN(1))).div(abtUsdPriceMicroBn);
|
|
353
|
+
if (requiredAbtBn.lte(new _ocap_util.BN(0))) throw new _ocap_util_lib_error.CustomError("INVALID_USD_ABT_QUOTE", "Computed requiredAbtWei must be strictly positive");
|
|
354
|
+
const maxAbtWithSlippageBn = requiredAbtBn.mul(new _ocap_util.BN(1e4 + maxSlippageBps)).add(new _ocap_util.BN(9999)).div(new _ocap_util.BN(1e4));
|
|
355
|
+
const unsignedQuote = {
|
|
356
|
+
quoteId,
|
|
357
|
+
serviceId,
|
|
358
|
+
buyerOcapDid,
|
|
359
|
+
usdPriceMicro: usdPriceMicroBn.toString(10),
|
|
360
|
+
abtUsdPriceMicro: abtUsdPriceMicroBn.toString(10),
|
|
361
|
+
requiredAbtWei: requiredAbtBn.toString(10),
|
|
362
|
+
maxAbtWithSlippageWei: maxAbtWithSlippageBn.toString(10),
|
|
363
|
+
maxSlippageBps,
|
|
364
|
+
oracleSource,
|
|
365
|
+
issuedAt: input.issuedAt,
|
|
366
|
+
expiresAt
|
|
367
|
+
};
|
|
368
|
+
return {
|
|
369
|
+
...unsignedQuote,
|
|
370
|
+
quoteDigest: computeUsdAnchoredQuoteDigest(unsignedQuote)
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Verifies a USD-anchored dynamic `ABT` quote for freshness, digest integrity, anti-replay,
|
|
375
|
+
* and exact slippage-bounded `ABT` payment sufficiency (#1274).
|
|
376
|
+
*/
|
|
377
|
+
function verifyUsdAnchoredAbtQuote(input) {
|
|
378
|
+
if (!input || typeof input !== "object" || !input.quote || typeof input.quote !== "object") throw new _ocap_util_lib_error.CustomError("INVALID_USD_ABT_QUOTE", "Missing quote verification input");
|
|
379
|
+
const { quote } = input;
|
|
380
|
+
const buyerOcapDid = assertSafeIdentifier(input.buyerOcapDid, "buyerOcapDid");
|
|
381
|
+
const serviceId = assertSafeIdentifier(input.serviceId, "serviceId");
|
|
382
|
+
const expectedUsdPriceMicroBn = parsePositiveUint(input.expectedUsdPriceMicro, "expectedUsdPriceMicro");
|
|
383
|
+
const trustedAbtUsdPriceMicroBn = parsePositiveUint(input.trustedAbtUsdPriceMicro, "trustedAbtUsdPriceMicro");
|
|
384
|
+
const paidAbtBn = parsePositiveUint(input.paidAbtWei, "paidAbtWei");
|
|
385
|
+
const maxOracleDeviationBps = input.maxOracleDeviationBps ?? 200;
|
|
386
|
+
if (typeof maxOracleDeviationBps !== "number" || !Number.isSafeInteger(maxOracleDeviationBps) || maxOracleDeviationBps < 0 || maxOracleDeviationBps > MAX_ALLOWED_ZAP_SLIPPAGE_BPS) throw new _ocap_util_lib_error.CustomError("INVALID_USD_ABT_QUOTE", `maxOracleDeviationBps must be an integer between 0 and ${MAX_ALLOWED_ZAP_SLIPPAGE_BPS}`);
|
|
387
|
+
if (typeof input.nowTimestamp !== "number" || !Number.isSafeInteger(input.nowTimestamp) || input.nowTimestamp <= 0) throw new _ocap_util_lib_error.CustomError("INVALID_USD_ABT_QUOTE", "nowTimestamp must be a positive safe integer");
|
|
388
|
+
const recomputed = createUsdAnchoredAbtQuote({
|
|
389
|
+
quoteId: quote.quoteId,
|
|
390
|
+
serviceId: quote.serviceId,
|
|
391
|
+
buyerOcapDid: quote.buyerOcapDid,
|
|
392
|
+
usdPriceMicro: quote.usdPriceMicro,
|
|
393
|
+
abtUsdPriceMicro: quote.abtUsdPriceMicro,
|
|
394
|
+
maxSlippageBps: quote.maxSlippageBps,
|
|
395
|
+
issuedAt: quote.issuedAt,
|
|
396
|
+
expiresAt: quote.expiresAt,
|
|
397
|
+
oracleSource: quote.oracleSource
|
|
398
|
+
});
|
|
399
|
+
if (quote.quoteDigest !== recomputed.quoteDigest || quote.requiredAbtWei !== recomputed.requiredAbtWei || quote.maxAbtWithSlippageWei !== recomputed.maxAbtWithSlippageWei) throw new _ocap_util_lib_error.CustomError("TAMPERED_USD_ABT_QUOTE", "USD-anchored ABT quote digest or computed amount mismatch");
|
|
400
|
+
if (quote.buyerOcapDid !== buyerOcapDid || quote.serviceId !== serviceId) throw new _ocap_util_lib_error.CustomError("MISMATCHED_USD_ABT_QUOTE_CONTEXT", "Quote buyerOcapDid or serviceId does not match checkout context");
|
|
401
|
+
if (recomputed.usdPriceMicro !== expectedUsdPriceMicroBn.toString(10)) throw new _ocap_util_lib_error.CustomError("FORGED_USD_CATALOG_PRICE", `Quote usdPriceMicro (${recomputed.usdPriceMicro}) does not match expectedUsdPriceMicro (${expectedUsdPriceMicroBn.toString(10)})`);
|
|
402
|
+
const quotedRateBn = new _ocap_util.BN(recomputed.abtUsdPriceMicro, 10);
|
|
403
|
+
const rateDiffBn = quotedRateBn.gt(trustedAbtUsdPriceMicroBn) ? quotedRateBn.sub(trustedAbtUsdPriceMicroBn) : trustedAbtUsdPriceMicroBn.sub(quotedRateBn);
|
|
404
|
+
const maxAllowedRateDiffBn = trustedAbtUsdPriceMicroBn.mul(new _ocap_util.BN(maxOracleDeviationBps)).div(new _ocap_util.BN(1e4));
|
|
405
|
+
if (rateDiffBn.gt(maxAllowedRateDiffBn)) throw new _ocap_util_lib_error.CustomError("FORGED_OR_STALE_ORACLE_EXCHANGE_RATE", `Quote abtUsdPriceMicro (${recomputed.abtUsdPriceMicro}) deviates from trustedAbtUsdPriceMicro (${trustedAbtUsdPriceMicroBn.toString(10)}) by more than ${maxOracleDeviationBps} bps`);
|
|
406
|
+
if (input.nowTimestamp < quote.issuedAt || input.nowTimestamp > quote.expiresAt) throw new _ocap_util_lib_error.CustomError("EXPIRED_USD_ABT_QUOTE", `Quote ${quote.quoteId} expired or not yet valid at timestamp ${input.nowTimestamp} (window: ${quote.issuedAt}..${quote.expiresAt})`);
|
|
407
|
+
if (input.usedQuoteIds?.has(quote.quoteId)) throw new _ocap_util_lib_error.CustomError("REPLAYED_USD_ABT_QUOTE", `Quote ${quote.quoteId} has already been consumed`);
|
|
408
|
+
const requiredAbtBn = new _ocap_util.BN(recomputed.requiredAbtWei, 10);
|
|
409
|
+
const maxAbtWithSlippageBn = new _ocap_util.BN(recomputed.maxAbtWithSlippageWei, 10);
|
|
410
|
+
if (paidAbtBn.lt(requiredAbtBn)) throw new _ocap_util_lib_error.CustomError("INSUFFICIENT_ABT_FOR_USD_QUOTE", `paidAbtWei (${paidAbtBn.toString(10)}) is less than requiredAbtWei (${requiredAbtBn.toString(10)})`);
|
|
411
|
+
if (paidAbtBn.gt(maxAbtWithSlippageBn)) throw new _ocap_util_lib_error.CustomError("EXCESSIVE_ABT_SLIPPAGE_FOR_USD_QUOTE", `paidAbtWei (${paidAbtBn.toString(10)}) exceeds maxAbtWithSlippageWei (${maxAbtWithSlippageBn.toString(10)})`);
|
|
412
|
+
if (input.usedQuoteIds) input.usedQuoteIds.add(quote.quoteId);
|
|
413
|
+
return {
|
|
414
|
+
verified: true,
|
|
415
|
+
quoteId: quote.quoteId,
|
|
416
|
+
requiredAbtWei: recomputed.requiredAbtWei,
|
|
417
|
+
paidAbtWei: paidAbtBn.toString(10)
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Issue #1274:
|
|
422
|
+
* Builds a stateless 1-Click Outbound Withdraw + Uniswap Auto-Swap Plan ("Smooth In, Smooth Out"):
|
|
423
|
+
* - OCAP side: atomically locks/burns `netAbtToSwap` to `ocapBridgeEscrowDid` and pays `relayerExitFeeAbt` to `relayerOcapDid`.
|
|
424
|
+
* - EVM side: `MultiSigVaultV2` (100% unmodified, holding only `ABT`) releases `netAbtToSwap` via `OcapPublishedMultiSigCert`
|
|
425
|
+
* to `withdrawSwapperAddress`, which atomically swaps `ABT -> targetPayoutToken (USDC/USDT/ETH)` via Uniswap Universal Router
|
|
426
|
+
* and sweeps 100% of output to `recipientEvmAddress` (or safely falls back to sending `netAbtToSwap` directly to `recipientEvmAddress`).
|
|
427
|
+
*/
|
|
428
|
+
function buildOutboundWithdrawAndSwapPlan(input) {
|
|
429
|
+
if (!input || typeof input !== "object") throw new _ocap_util_lib_error.CustomError("INVALID_OUTBOUND_WITHDRAW_SWAP", "Missing outbound withdraw-and-swap input");
|
|
430
|
+
const chainId = assertSafeIdentifier(input.chainId, "chainId");
|
|
431
|
+
const sellerOcapDid = assertSafeIdentifier(input.sellerOcapDid, "sellerOcapDid");
|
|
432
|
+
const relayerOcapDid = assertSafeIdentifier(input.relayerOcapDid, "relayerOcapDid");
|
|
433
|
+
const ocapBridgeEscrowDid = assertSafeIdentifier(input.ocapBridgeEscrowDid, "ocapBridgeEscrowDid");
|
|
434
|
+
const abtOcapTokenAddress = assertSafeIdentifier(input.abtOcapTokenAddress, "abtOcapTokenAddress");
|
|
435
|
+
const recipientEvmAddress = assertEvmAddress(input.recipientEvmAddress, "recipientEvmAddress");
|
|
436
|
+
const ocapVaultAddress = assertEvmAddress(input.ocapVaultAddress, "ocapVaultAddress");
|
|
437
|
+
const withdrawSwapperAddress = assertEvmAddress(input.withdrawSwapperAddress, "withdrawSwapperAddress");
|
|
438
|
+
const universalRouterAddress = assertEvmAddress(input.universalRouterAddress, "universalRouterAddress");
|
|
439
|
+
const abtEvmAddress = assertEvmAddress(input.abtEvmAddress, "abtEvmAddress");
|
|
440
|
+
const targetPayoutTokenAddress = assertEvmAddress(input.targetPayoutTokenAddress, "targetPayoutTokenAddress");
|
|
441
|
+
if (typeof input.publishedMultiSigCertHash !== "string" || !BYTES32_HEX_DIGEST_REGEX.test(input.publishedMultiSigCertHash)) throw new _ocap_util_lib_error.CustomError("INVALID_OUTBOUND_WITHDRAW_SWAP", "publishedMultiSigCertHash must be a valid 32-byte 0x hex digest");
|
|
442
|
+
if (require_ofac_screening.normalizeScreeningAddress(sellerOcapDid) === require_ofac_screening.normalizeScreeningAddress(ocapBridgeEscrowDid) || require_ofac_screening.normalizeScreeningAddress(relayerOcapDid) === require_ofac_screening.normalizeScreeningAddress(ocapBridgeEscrowDid)) throw new _ocap_util_lib_error.CustomError("INVALID_OUTBOUND_WITHDRAW_SWAP", "ocapBridgeEscrowDid must be distinct from sellerOcapDid and relayerOcapDid");
|
|
443
|
+
require_ofac_screening.assertNotOfacSanctioned([
|
|
444
|
+
sellerOcapDid,
|
|
445
|
+
relayerOcapDid,
|
|
446
|
+
ocapBridgeEscrowDid,
|
|
447
|
+
abtOcapTokenAddress,
|
|
448
|
+
recipientEvmAddress,
|
|
449
|
+
ocapVaultAddress,
|
|
450
|
+
withdrawSwapperAddress,
|
|
451
|
+
universalRouterAddress,
|
|
452
|
+
abtEvmAddress,
|
|
453
|
+
targetPayoutTokenAddress
|
|
454
|
+
], input.ofacBlocklist);
|
|
455
|
+
const grossAbtBn = parsePositiveUint(input.grossAbtWithdrawAmount, "grossAbtWithdrawAmount");
|
|
456
|
+
const relayerExitFeeBn = parseNonNegativeUint(input.relayerExitFeeAbt, "relayerExitFeeAbt");
|
|
457
|
+
if (grossAbtBn.lte(relayerExitFeeBn)) throw new _ocap_util_lib_error.CustomError("INVALID_OUTBOUND_WITHDRAW_SWAP", `grossAbtWithdrawAmount (${grossAbtBn.toString(10)}) must be strictly greater than relayerExitFeeAbt (${relayerExitFeeBn.toString(10)})`);
|
|
458
|
+
const netAbtToSwapBn = grossAbtBn.sub(relayerExitFeeBn);
|
|
459
|
+
const isDirectAbtExit = abtEvmAddress === targetPayoutTokenAddress;
|
|
460
|
+
let evmSwapPayload;
|
|
461
|
+
let minimumTargetTokenOutStr;
|
|
462
|
+
let expectedTargetTokenOutStr;
|
|
463
|
+
if (isDirectAbtExit) {
|
|
464
|
+
expectedTargetTokenOutStr = netAbtToSwapBn.toString(10);
|
|
465
|
+
minimumTargetTokenOutStr = netAbtToSwapBn.toString(10);
|
|
466
|
+
} else {
|
|
467
|
+
evmSwapPayload = buildUniversalRouterCrossChainPayload({
|
|
468
|
+
chainId,
|
|
469
|
+
senderEvmAddress: withdrawSwapperAddress,
|
|
470
|
+
buyerOcapDid: sellerOcapDid,
|
|
471
|
+
universalRouterAddress,
|
|
472
|
+
ocapVaultAddress: recipientEvmAddress,
|
|
473
|
+
tokenIn: abtEvmAddress,
|
|
474
|
+
tokenOut: targetPayoutTokenAddress,
|
|
475
|
+
routePath: input.routePath,
|
|
476
|
+
poolVersion: input.poolVersion ?? "v4",
|
|
477
|
+
amountIn: netAbtToSwapBn.toString(10),
|
|
478
|
+
expectedAmountOut: input.expectedTargetTokenOut,
|
|
479
|
+
maxSlippageBps: input.maxSlippageBps,
|
|
480
|
+
deadline: input.deadline,
|
|
481
|
+
currentTimestamp: input.currentTimestamp,
|
|
482
|
+
integratorId: input.integratorId,
|
|
483
|
+
brokerageFeeBps: 0,
|
|
484
|
+
ofacBlocklist: input.ofacBlocklist
|
|
485
|
+
});
|
|
486
|
+
expectedTargetTokenOutStr = evmSwapPayload.expectedAmountOut;
|
|
487
|
+
minimumTargetTokenOutStr = evmSwapPayload.amountOutMinimum;
|
|
488
|
+
}
|
|
489
|
+
const ocapBurnAndFeeStatements = [{
|
|
490
|
+
type: "transfer_v3",
|
|
491
|
+
typeUrl: "fg:t:transfer_v3",
|
|
492
|
+
from: sellerOcapDid,
|
|
493
|
+
itx: {
|
|
494
|
+
to: ocapBridgeEscrowDid,
|
|
495
|
+
tokens: [{
|
|
496
|
+
address: abtOcapTokenAddress,
|
|
497
|
+
value: netAbtToSwapBn.toString(10)
|
|
498
|
+
}],
|
|
499
|
+
data: {
|
|
500
|
+
type: "json",
|
|
501
|
+
value: JSON.stringify({
|
|
502
|
+
bridgeAction: "WITHDRAW_ABT_AND_SWAP",
|
|
503
|
+
chainId,
|
|
504
|
+
recipientEvmAddress,
|
|
505
|
+
targetPayoutTokenAddress,
|
|
506
|
+
publishedMultiSigCertHash: input.publishedMultiSigCertHash.toLowerCase()
|
|
507
|
+
})
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}];
|
|
511
|
+
if (relayerExitFeeBn.gt(new _ocap_util.BN(0))) ocapBurnAndFeeStatements.push({
|
|
512
|
+
type: "transfer_v3",
|
|
513
|
+
typeUrl: "fg:t:transfer_v3",
|
|
514
|
+
from: sellerOcapDid,
|
|
515
|
+
itx: {
|
|
516
|
+
to: relayerOcapDid,
|
|
517
|
+
tokens: [{
|
|
518
|
+
address: abtOcapTokenAddress,
|
|
519
|
+
value: relayerExitFeeBn.toString(10)
|
|
520
|
+
}],
|
|
521
|
+
data: {
|
|
522
|
+
type: "json",
|
|
523
|
+
value: JSON.stringify({
|
|
524
|
+
bridgeAction: "OUTBOUND_EXIT_RELAYER_FEE",
|
|
525
|
+
chainId,
|
|
526
|
+
publishedMultiSigCertHash: input.publishedMultiSigCertHash.toLowerCase()
|
|
527
|
+
})
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
const ocapPostConditions = {
|
|
532
|
+
signer: sellerOcapDid,
|
|
533
|
+
maxTokenOutflow: { [abtOcapTokenAddress]: grossAbtBn.toString(10) },
|
|
534
|
+
strictZeroUnlistedOutflow: true
|
|
535
|
+
};
|
|
536
|
+
const fallbackToDirectAbt = input.fallbackToDirectAbtOnSwapFailure ?? true;
|
|
537
|
+
return {
|
|
538
|
+
mode: isDirectAbtExit ? "DIRECT_ABT_WITHDRAW" : "WITHDRAW_AND_UNISWAP_SWAP",
|
|
539
|
+
ocapBurnAndFeeStatements,
|
|
540
|
+
ocapPostConditions,
|
|
541
|
+
evmVaultReleaseSpec: {
|
|
542
|
+
vaultAddress: ocapVaultAddress,
|
|
543
|
+
vaultContractModified: false,
|
|
544
|
+
vaultTokenReleased: abtEvmAddress,
|
|
545
|
+
releaseRecipientAddress: isDirectAbtExit ? recipientEvmAddress : withdrawSwapperAddress,
|
|
546
|
+
netAbtReleased: netAbtToSwapBn.toString(10),
|
|
547
|
+
publishedMultiSigCertHash: input.publishedMultiSigCertHash.toLowerCase()
|
|
548
|
+
},
|
|
549
|
+
evmSwapPayload,
|
|
550
|
+
fallbackSafetySpec: {
|
|
551
|
+
fallbackToDirectAbtOnSwapFailure: fallbackToDirectAbt,
|
|
552
|
+
fallbackRecipientEvmAddress: recipientEvmAddress,
|
|
553
|
+
fallbackTokenAddress: abtEvmAddress,
|
|
554
|
+
fallbackAmountAbt: netAbtToSwapBn.toString(10),
|
|
555
|
+
swapperRetainsZeroBalanceAfterTx: true
|
|
556
|
+
},
|
|
557
|
+
economics: {
|
|
558
|
+
userBearsExitCost: true,
|
|
559
|
+
protocolSubsidyAbt: "0",
|
|
560
|
+
grossAbtWithdrawAmount: grossAbtBn.toString(10),
|
|
561
|
+
relayerExitFeeAbt: relayerExitFeeBn.toString(10),
|
|
562
|
+
netAbtToSwap: netAbtToSwapBn.toString(10),
|
|
563
|
+
expectedTargetTokenOut: expectedTargetTokenOutStr,
|
|
564
|
+
minimumTargetTokenOut: minimumTargetTokenOutStr
|
|
565
|
+
},
|
|
566
|
+
clearSigningSummary: {
|
|
567
|
+
youPay: `${grossAbtBn.toString(10)} ABT (Net Swap: ${netAbtToSwapBn.toString(10)} ABT + Exit Fee: ${relayerExitFeeBn.toString(10)} ABT)`,
|
|
568
|
+
actions: isDirectAbtExit ? [`Lock ${netAbtToSwapBn.toString(10)} ABT in OCAP Bridge Escrow (${ocapBridgeEscrowDid})`, `Release ${netAbtToSwapBn.toString(10)} ABT from MultiSigVaultV2 (${ocapVaultAddress}) to ${recipientEvmAddress}`] : [
|
|
569
|
+
`Lock ${netAbtToSwapBn.toString(10)} ABT in OCAP Bridge Escrow (${ocapBridgeEscrowDid})`,
|
|
570
|
+
`Release ${netAbtToSwapBn.toString(10)} ABT from MultiSigVaultV2 (${ocapVaultAddress}) to Stateless Swapper (${withdrawSwapperAddress})`,
|
|
571
|
+
`Swap ${netAbtToSwapBn.toString(10)} ABT -> ${targetPayoutTokenAddress} via Uniswap Universal Router (${universalRouterAddress})`
|
|
572
|
+
],
|
|
573
|
+
youReceiveMinimum: `${minimumTargetTokenOutStr} (${targetPayoutTokenAddress}) at ${recipientEvmAddress}`
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
//#endregion
|
|
579
|
+
exports.ABT_DECIMALS_SCALE = ABT_DECIMALS_SCALE;
|
|
580
|
+
exports.CHAINALYSIS_OFAC_ORACLE_ADDRESS = require_ofac_screening.CHAINALYSIS_OFAC_ORACLE_ADDRESS;
|
|
581
|
+
exports.DEFAULT_OCAP_INTEGRATOR_ID = DEFAULT_OCAP_INTEGRATOR_ID;
|
|
582
|
+
exports.DEFAULT_USD_QUOTE_TTL_SECONDS = DEFAULT_USD_QUOTE_TTL_SECONDS;
|
|
583
|
+
exports.MAX_ALLOWED_ZAP_SLIPPAGE_BPS = MAX_ALLOWED_ZAP_SLIPPAGE_BPS;
|
|
584
|
+
exports.MAX_ROUTE_PATH_LENGTH = MAX_ROUTE_PATH_LENGTH;
|
|
585
|
+
exports.MAX_USD_QUOTE_TTL_SECONDS = MAX_USD_QUOTE_TTL_SECONDS;
|
|
586
|
+
exports.UNIVERSAL_ROUTER_COMMANDS = UNIVERSAL_ROUTER_COMMANDS;
|
|
587
|
+
exports.assertNotOfacSanctioned = require_ofac_screening.assertNotOfacSanctioned;
|
|
588
|
+
exports.buildAbtUtilityPassThroughPlan = buildAbtUtilityPassThroughPlan;
|
|
589
|
+
exports.buildOutboundWithdrawAndSwapPlan = buildOutboundWithdrawAndSwapPlan;
|
|
590
|
+
exports.buildUniversalRouterCrossChainPayload = buildUniversalRouterCrossChainPayload;
|
|
591
|
+
exports.createUsdAnchoredAbtQuote = createUsdAnchoredAbtQuote;
|
|
592
|
+
exports.normalizeScreeningAddress = require_ofac_screening.normalizeScreeningAddress;
|
|
593
|
+
exports.verifyUsdAnchoredAbtQuote = verifyUsdAnchoredAbtQuote;
|