@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,352 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_ofac_screening = require('./ofac-screening.cjs');
|
|
3
|
+
require('./uniswap-router.cjs');
|
|
4
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
5
|
+
let _ocap_util = require("@ocap/util");
|
|
6
|
+
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
7
|
+
|
|
8
|
+
//#region src/appchain-sovereign-hub.ts
|
|
9
|
+
const FORBIDDEN_KEYS = new Set([
|
|
10
|
+
"__proto__",
|
|
11
|
+
"constructor",
|
|
12
|
+
"prototype"
|
|
13
|
+
]);
|
|
14
|
+
const CANONICAL_UINT_REGEX = /^\d+$/;
|
|
15
|
+
const BYTES32_HEX_REGEX = /^0x[0-9a-fA-F]{64}$/;
|
|
16
|
+
const ACC_PRECISION = new _ocap_util.BN("1000000000000000000", 10);
|
|
17
|
+
const MAX_FREE_RELAY_CREDITS_PER_EPOCH = 1e6;
|
|
18
|
+
const MAX_FREE_RELAY_CREDITS_BN = new _ocap_util.BN(MAX_FREE_RELAY_CREDITS_PER_EPOCH);
|
|
19
|
+
function assertSafeId(val, label) {
|
|
20
|
+
if (typeof val !== "string") throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", `${label} must be a non-empty string`);
|
|
21
|
+
const trimmed = val.trim();
|
|
22
|
+
if (!trimmed || FORBIDDEN_KEYS.has(trimmed)) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", `Invalid or unsafe ${label}`);
|
|
23
|
+
return trimmed;
|
|
24
|
+
}
|
|
25
|
+
function normalizeDelimiterSafeId(val, label) {
|
|
26
|
+
const normalized = require_ofac_screening.normalizeScreeningAddress(assertSafeId(val, label));
|
|
27
|
+
if (normalized.includes(":") || normalized.includes("|")) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", `${label} must not contain reserved delimiter characters (':' or '|') after normalization`);
|
|
28
|
+
return normalized;
|
|
29
|
+
}
|
|
30
|
+
function parsePositiveBn(val, label) {
|
|
31
|
+
if (typeof val !== "string" || !CANONICAL_UINT_REGEX.test(val)) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", `${label} must be a canonical base-10 integer string`);
|
|
32
|
+
const bn = new _ocap_util.BN(val, 10);
|
|
33
|
+
if (bn.lte(new _ocap_util.BN(0))) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", `${label} must be strictly positive`);
|
|
34
|
+
return bn;
|
|
35
|
+
}
|
|
36
|
+
function parseNonNegativeBn(val, label) {
|
|
37
|
+
if (typeof val !== "string" || !CANONICAL_UINT_REGEX.test(val)) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", `${label} must be a canonical non-negative integer string`);
|
|
38
|
+
return new _ocap_util.BN(val, 10);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Deterministically derives the Sovereign Reserve Vault DID for a given `spokeChainDid`
|
|
42
|
+
* (Polkadot XCM Sovereign Account model).
|
|
43
|
+
*/
|
|
44
|
+
function deriveSovereignVaultDid(spokeChainDid) {
|
|
45
|
+
const normalizedSpoke = normalizeDelimiterSafeId(spokeChainDid, "spokeChainDid");
|
|
46
|
+
return `zSovVault_${_ocap_mcrypto.Hasher.SHA3.hash256(Buffer.from(`ocap:sovereign-reserve:v1:${normalizedSpoke.length}:${normalizedSpoke}`, "utf8")).replace(/^0x/, "").slice(0, 36).toLowerCase()}`;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Computes the deterministic VLedger state root transition commitment:
|
|
50
|
+
* `SHA3-256("ocap:vledger-checkpoint:v1:" + spoke.length + ":" + spokeChainDid + ":" + blockHeight + ":" + prevStateRoot + ":" + stateDiffMerkleRoot)`
|
|
51
|
+
*/
|
|
52
|
+
function computeVLedgerCheckpointStateRoot(params) {
|
|
53
|
+
const spoke = normalizeDelimiterSafeId(params.spokeChainDid, "spokeChainDid");
|
|
54
|
+
if (!Number.isSafeInteger(params.blockHeight) || params.blockHeight <= 0) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", "blockHeight must be a positive safe integer");
|
|
55
|
+
if (typeof params.prevStateRoot !== "string" || !BYTES32_HEX_REGEX.test(params.prevStateRoot)) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", "prevStateRoot must be a 32-byte 0x hex string");
|
|
56
|
+
if (typeof params.stateDiffMerkleRoot !== "string" || !BYTES32_HEX_REGEX.test(params.stateDiffMerkleRoot)) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", "stateDiffMerkleRoot must be a 32-byte 0x hex string");
|
|
57
|
+
const payload = `ocap:vledger-checkpoint:v1:${spoke.length}:${spoke}:${params.blockHeight}:${params.prevStateRoot.toLowerCase()}:${params.stateDiffMerkleRoot.toLowerCase()}`;
|
|
58
|
+
return `0x${_ocap_mcrypto.Hasher.SHA3.hash256(Buffer.from(payload, "utf8")).replace(/^0x/, "").toLowerCase()}`;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Polkadot XCM Sovereign Reserve Vault + Crowdloan Quota Staking + VLedger Checkpoint + Cosmos PFM Multi-Hop Hub Engine (#1173).
|
|
62
|
+
*/
|
|
63
|
+
var OcapSovereignAppChainHubEngine = class {
|
|
64
|
+
constructor(options) {
|
|
65
|
+
this.reserves = /* @__PURE__ */ new Map();
|
|
66
|
+
this.crowdloans = /* @__PURE__ */ new Map();
|
|
67
|
+
this.hubAmmReserves = /* @__PURE__ */ new Map();
|
|
68
|
+
this.consumedPacketIds = /* @__PURE__ */ new Set();
|
|
69
|
+
this.ofacSet = /* @__PURE__ */ new Set();
|
|
70
|
+
if (!options || typeof options !== "object") throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", "Missing OcapSovereignAppChainHubEngine options");
|
|
71
|
+
this.arcHubDid = require_ofac_screening.normalizeScreeningAddress(assertSafeId(options.arcHubDid, "arcHubDid"));
|
|
72
|
+
this.abtTokenAddress = require_ofac_screening.normalizeScreeningAddress(assertSafeId(options.abtTokenAddress, "abtTokenAddress"));
|
|
73
|
+
const creditsPerUnit = options.relayCreditsPerAbtUnit ?? 10;
|
|
74
|
+
if (!Number.isSafeInteger(creditsPerUnit) || creditsPerUnit <= 0 || creditsPerUnit > 1e4) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", "relayCreditsPerAbtUnit must be an integer between 1 and 10000");
|
|
75
|
+
this.relayCreditsPerAbtUnit = creditsPerUnit;
|
|
76
|
+
if (options.ofacBlocklist) for (const a of options.ofacBlocklist) this.ofacSet.add(require_ofac_screening.normalizeScreeningAddress(a));
|
|
77
|
+
require_ofac_screening.assertNotOfacSanctioned([this.arcHubDid, this.abtTokenAddress], this.ofacSet);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Registers an isomorphic Spoke AppChain with its own isolated `SovereignReserveState` and validator set.
|
|
81
|
+
*/
|
|
82
|
+
registerSpokeChain(params) {
|
|
83
|
+
const spokeChainDid = require_ofac_screening.normalizeScreeningAddress(assertSafeId(params.spokeChainDid, "spokeChainDid"));
|
|
84
|
+
if (spokeChainDid === this.arcHubDid) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", "spokeChainDid cannot equal arcHubDid");
|
|
85
|
+
if (this.reserves.has(spokeChainDid)) throw new _ocap_util_lib_error.CustomError("DUPLICATE_SPOKE_CHAIN", `Spoke chain ${spokeChainDid} is already registered`);
|
|
86
|
+
if (typeof params.genesisStateRoot !== "string" || !BYTES32_HEX_REGEX.test(params.genesisStateRoot)) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", "genesisStateRoot must be a 32-byte 0x hex string");
|
|
87
|
+
if (!Array.isArray(params.validators) || params.validators.length === 0) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", "validators must be a non-empty array");
|
|
88
|
+
const normalizedValidators = Array.from(new Set(params.validators.map((v, i) => require_ofac_screening.normalizeScreeningAddress(assertSafeId(v, `validators[${i}]`)))));
|
|
89
|
+
if (normalizedValidators.length !== params.validators.length) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", "validators must not contain duplicate DIDs");
|
|
90
|
+
require_ofac_screening.assertNotOfacSanctioned([spokeChainDid, ...normalizedValidators], this.ofacSet);
|
|
91
|
+
const tokens = {};
|
|
92
|
+
for (const [k, v] of Object.entries(params.initialTokens || {})) {
|
|
93
|
+
const tokAddr = require_ofac_screening.normalizeScreeningAddress(assertSafeId(k, "tokenAddress"));
|
|
94
|
+
tokens[tokAddr] = parseNonNegativeBn(v, `initialTokens[${tokAddr}]`).toString(10);
|
|
95
|
+
}
|
|
96
|
+
const nfts = Array.from(new Set((params.initialNfts || []).map((n, idx) => require_ofac_screening.normalizeScreeningAddress(assertSafeId(n, `initialNfts[${idx}]`)))));
|
|
97
|
+
const state = {
|
|
98
|
+
spokeChainDid,
|
|
99
|
+
sovereignVaultDid: deriveSovereignVaultDid(spokeChainDid),
|
|
100
|
+
tokens,
|
|
101
|
+
nfts,
|
|
102
|
+
lastCheckpointedHeight: 0,
|
|
103
|
+
currentStateRoot: params.genesisStateRoot.toLowerCase(),
|
|
104
|
+
registeredValidators: normalizedValidators
|
|
105
|
+
};
|
|
106
|
+
this.reserves.set(spokeChainDid, state);
|
|
107
|
+
this.crowdloans.set(spokeChainDid, {
|
|
108
|
+
spokeChainDid,
|
|
109
|
+
totalStakedAbt: "0",
|
|
110
|
+
accRewardPerShareScaled: "0",
|
|
111
|
+
unclaimedRewardReserveAbt: "0",
|
|
112
|
+
freeRelayCreditsPerEpoch: 0,
|
|
113
|
+
remainingEpochRelayCredits: 0,
|
|
114
|
+
contributors: {}
|
|
115
|
+
});
|
|
116
|
+
return JSON.parse(JSON.stringify(state));
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Seeds Hub AMM liquidity for cross-spoke token conversion during Cosmos PFM multi-hop routing.
|
|
120
|
+
*/
|
|
121
|
+
seedHubSwapLiquidity(tokenAddress, amount) {
|
|
122
|
+
const tok = require_ofac_screening.normalizeScreeningAddress(assertSafeId(tokenAddress, "tokenAddress"));
|
|
123
|
+
const amtBn = parsePositiveBn(amount, "amount");
|
|
124
|
+
const cur = this.hubAmmReserves.get(tok) || new _ocap_util.BN(0);
|
|
125
|
+
this.hubAmmReserves.set(tok, cur.add(amtBn));
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Deposits fungible tokens into a Spoke's `SovereignReserveState` on `ArcHub`.
|
|
129
|
+
*/
|
|
130
|
+
depositToSpokeReserve(params) {
|
|
131
|
+
const spoke = require_ofac_screening.normalizeScreeningAddress(assertSafeId(params.spokeChainDid, "spokeChainDid"));
|
|
132
|
+
const reserve = this.reserves.get(spoke);
|
|
133
|
+
if (!reserve) throw new _ocap_util_lib_error.CustomError("NOT_FOUND", `Spoke chain ${spoke} not found`);
|
|
134
|
+
const tok = require_ofac_screening.normalizeScreeningAddress(assertSafeId(params.tokenAddress, "tokenAddress"));
|
|
135
|
+
const amtBn = parsePositiveBn(params.amount, "amount");
|
|
136
|
+
const cur = new _ocap_util.BN(Object.hasOwn(reserve.tokens, tok) ? reserve.tokens[tok] : "0", 10);
|
|
137
|
+
reserve.tokens[tok] = cur.add(amtBn).toString(10);
|
|
138
|
+
return JSON.parse(JSON.stringify(reserve));
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Withdraws fungible tokens from a Spoke's `SovereignReserveState` on `ArcHub`.
|
|
142
|
+
* Enforces the Polkadot XCM Sovereign Containment Invariant: a compromised Spoke can NEVER
|
|
143
|
+
* withdraw more than its own `SovereignReserveState` balance.
|
|
144
|
+
*/
|
|
145
|
+
withdrawFromSpokeReserve(params) {
|
|
146
|
+
const spoke = require_ofac_screening.normalizeScreeningAddress(assertSafeId(params.spokeChainDid, "spokeChainDid"));
|
|
147
|
+
const reserve = this.reserves.get(spoke);
|
|
148
|
+
if (!reserve) throw new _ocap_util_lib_error.CustomError("NOT_FOUND", `Spoke chain ${spoke} not found`);
|
|
149
|
+
const tok = require_ofac_screening.normalizeScreeningAddress(assertSafeId(params.tokenAddress, "tokenAddress"));
|
|
150
|
+
const amtBn = parsePositiveBn(params.amount, "amount");
|
|
151
|
+
const cur = new _ocap_util.BN(Object.hasOwn(reserve.tokens, tok) ? reserve.tokens[tok] : "0", 10);
|
|
152
|
+
if (cur.lt(amtBn)) throw new _ocap_util_lib_error.CustomError("SOVEREIGN_RESERVE_EXCEEDED", `Spoke ${spoke} attempted to withdraw ${amtBn.toString(10)} of ${tok}, exceeding its SovereignReserveState balance (${cur.toString(10)})`);
|
|
153
|
+
reserve.tokens[tok] = cur.sub(amtBn).toString(10);
|
|
154
|
+
return JSON.parse(JSON.stringify(reserve));
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Polkadot Crowdloan & Agile Coretime `ABT` Quota Staking (`CrowdloanContributeTx`):
|
|
158
|
+
* Locks `ABT` for a target `spokeChainDid`, dynamically increasing its `freeRelayCreditsPerEpoch`
|
|
159
|
+
* and tracking `O(1)` `accRewardPerShare` revenue shares without Sybil split inflation.
|
|
160
|
+
*/
|
|
161
|
+
contributeCrowdloanAbt(params) {
|
|
162
|
+
const spoke = require_ofac_screening.normalizeScreeningAddress(assertSafeId(params.spokeChainDid, "spokeChainDid"));
|
|
163
|
+
const contributor = require_ofac_screening.normalizeScreeningAddress(assertSafeId(params.contributorDid, "contributorDid"));
|
|
164
|
+
require_ofac_screening.assertNotOfacSanctioned([spoke, contributor], this.ofacSet);
|
|
165
|
+
const pool = this.crowdloans.get(spoke);
|
|
166
|
+
if (!pool) throw new _ocap_util_lib_error.CustomError("NOT_FOUND", `Spoke chain ${spoke} not found`);
|
|
167
|
+
const addAmtBn = parsePositiveBn(params.abtAmount, "abtAmount");
|
|
168
|
+
const accBn = new _ocap_util.BN(pool.accRewardPerShareScaled, 10);
|
|
169
|
+
const existing = Object.hasOwn(pool.contributors, contributor) ? pool.contributors[contributor] : {
|
|
170
|
+
contributorDid: contributor,
|
|
171
|
+
stakedAbt: "0",
|
|
172
|
+
rewardDebtScaled: "0",
|
|
173
|
+
accruedUnclaimedRewards: "0"
|
|
174
|
+
};
|
|
175
|
+
const prevStakeBn = new _ocap_util.BN(existing.stakedAbt, 10);
|
|
176
|
+
const prevDebtScaledBn = new _ocap_util.BN(existing.rewardDebtScaled, 10);
|
|
177
|
+
const prevAccruedBn = new _ocap_util.BN(existing.accruedUnclaimedRewards, 10);
|
|
178
|
+
const pendingBn = prevStakeBn.mul(accBn).sub(prevDebtScaledBn).div(ACC_PRECISION);
|
|
179
|
+
const nextStakeBn = prevStakeBn.add(addAmtBn);
|
|
180
|
+
const nextDebtScaledBn = nextStakeBn.mul(accBn);
|
|
181
|
+
pool.contributors[contributor] = {
|
|
182
|
+
contributorDid: contributor,
|
|
183
|
+
stakedAbt: nextStakeBn.toString(10),
|
|
184
|
+
rewardDebtScaled: nextDebtScaledBn.toString(10),
|
|
185
|
+
accruedUnclaimedRewards: prevAccruedBn.add(pendingBn).toString(10)
|
|
186
|
+
};
|
|
187
|
+
const nextTotalStakedBn = new _ocap_util.BN(pool.totalStakedAbt, 10).add(addAmtBn);
|
|
188
|
+
pool.totalStakedAbt = nextTotalStakedBn.toString(10);
|
|
189
|
+
const totalCreditsBn = nextTotalStakedBn.muln(this.relayCreditsPerAbtUnit);
|
|
190
|
+
const cappedTotalCredits = (totalCreditsBn.gt(MAX_FREE_RELAY_CREDITS_BN) ? MAX_FREE_RELAY_CREDITS_BN : totalCreditsBn).toNumber();
|
|
191
|
+
const addedCredits = Math.max(0, cappedTotalCredits - pool.freeRelayCreditsPerEpoch);
|
|
192
|
+
pool.freeRelayCreditsPerEpoch = cappedTotalCredits;
|
|
193
|
+
pool.remainingEpochRelayCredits = Math.min(MAX_FREE_RELAY_CREDITS_PER_EPOCH, pool.remainingEpochRelayCredits + addedCredits);
|
|
194
|
+
return JSON.parse(JSON.stringify(pool));
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Distributes Spoke business revenue to all Crowdloan `ABT` contributors in `O(1)` time via `accRewardPerShare`.
|
|
198
|
+
* Enforces 100% reserve backing by debiting `revenueAmount` from the Spoke's `SovereignReserveState` `ABT` balance.
|
|
199
|
+
*/
|
|
200
|
+
distributeSpokeRevenue(params) {
|
|
201
|
+
const spoke = require_ofac_screening.normalizeScreeningAddress(assertSafeId(params.spokeChainDid, "spokeChainDid"));
|
|
202
|
+
const pool = this.crowdloans.get(spoke);
|
|
203
|
+
const reserve = this.reserves.get(spoke);
|
|
204
|
+
if (!pool || !reserve) throw new _ocap_util_lib_error.CustomError("NOT_FOUND", `Spoke chain ${spoke} not found`);
|
|
205
|
+
const revBn = parsePositiveBn(params.revenueAmount, "revenueAmount");
|
|
206
|
+
const totalStakedBn = new _ocap_util.BN(pool.totalStakedAbt, 10);
|
|
207
|
+
if (totalStakedBn.isZero()) throw new _ocap_util_lib_error.CustomError("EMPTY_CROWDLOAN_POOL", `Cannot distribute revenue when Spoke ${spoke} has 0 staked ABT`);
|
|
208
|
+
const reserveAbtBn = new _ocap_util.BN(Object.hasOwn(reserve.tokens, this.abtTokenAddress) ? reserve.tokens[this.abtTokenAddress] : "0", 10);
|
|
209
|
+
if (reserveAbtBn.lt(revBn)) throw new _ocap_util_lib_error.CustomError("UNBACKED_SPOKE_REVENUE", `Spoke ${spoke} lacks SovereignReserveState ABT (${reserveAbtBn.toString(10)}) to back revenue distribution (${revBn.toString(10)})`);
|
|
210
|
+
const deltaAccScaled = revBn.mul(ACC_PRECISION).div(totalStakedBn);
|
|
211
|
+
if (deltaAccScaled.isZero()) throw new _ocap_util_lib_error.CustomError("REVENUE_TOO_SMALL_FOR_PRECISION", `Revenue amount (${revBn.toString(10)}) is too small relative to totalStakedAbt (${totalStakedBn.toString(10)}) and would round to 0`);
|
|
212
|
+
reserve.tokens[this.abtTokenAddress] = reserveAbtBn.sub(revBn).toString(10);
|
|
213
|
+
pool.unclaimedRewardReserveAbt = new _ocap_util.BN(pool.unclaimedRewardReserveAbt, 10).add(revBn).toString(10);
|
|
214
|
+
pool.accRewardPerShareScaled = new _ocap_util.BN(pool.accRewardPerShareScaled, 10).add(deltaAccScaled).toString(10);
|
|
215
|
+
return JSON.parse(JSON.stringify(pool));
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Claims accrued Crowdloan business revenue for a contributor in `O(1)` time.
|
|
219
|
+
*/
|
|
220
|
+
claimCrowdloanRewards(params) {
|
|
221
|
+
const spoke = require_ofac_screening.normalizeScreeningAddress(assertSafeId(params.spokeChainDid, "spokeChainDid"));
|
|
222
|
+
const contributor = require_ofac_screening.normalizeScreeningAddress(assertSafeId(params.contributorDid, "contributorDid"));
|
|
223
|
+
const pool = this.crowdloans.get(spoke);
|
|
224
|
+
if (!pool) throw new _ocap_util_lib_error.CustomError("NOT_FOUND", `Spoke chain ${spoke} not found`);
|
|
225
|
+
const pos = Object.hasOwn(pool.contributors, contributor) ? pool.contributors[contributor] : void 0;
|
|
226
|
+
if (!pos) throw new _ocap_util_lib_error.CustomError("NOT_FOUND", `Contributor ${contributor} not found in Spoke ${spoke} crowdloan`);
|
|
227
|
+
const stakeBn = new _ocap_util.BN(pos.stakedAbt, 10);
|
|
228
|
+
const accBn = new _ocap_util.BN(pool.accRewardPerShareScaled, 10);
|
|
229
|
+
const debtScaledBn = new _ocap_util.BN(pos.rewardDebtScaled, 10);
|
|
230
|
+
const accruedBn = new _ocap_util.BN(pos.accruedUnclaimedRewards, 10);
|
|
231
|
+
const newlyEarnedBn = stakeBn.mul(accBn).sub(debtScaledBn).div(ACC_PRECISION);
|
|
232
|
+
const totalClaimableBn = accruedBn.add(newlyEarnedBn);
|
|
233
|
+
const unclaimedReserveBn = new _ocap_util.BN(pool.unclaimedRewardReserveAbt, 10);
|
|
234
|
+
if (unclaimedReserveBn.lt(totalClaimableBn)) throw new _ocap_util_lib_error.CustomError("INSUFFICIENT_REWARD_RESERVE", "Insufficient unclaimed reward reserve");
|
|
235
|
+
pool.unclaimedRewardReserveAbt = unclaimedReserveBn.sub(totalClaimableBn).toString(10);
|
|
236
|
+
pos.rewardDebtScaled = stakeBn.mul(accBn).toString(10);
|
|
237
|
+
pos.accruedUnclaimedRewards = "0";
|
|
238
|
+
return {
|
|
239
|
+
claimedAmount: totalClaimableBn.toString(10),
|
|
240
|
+
contributorPosition: { ...pos }
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Verifies a Spoke's `CheckpointRollupTx` using both `2f + 1` Validator Quorum and VLedger incremental
|
|
245
|
+
* state-root transition commitment (`prevStateRoot -> newStateRoot`).
|
|
246
|
+
*/
|
|
247
|
+
verifyAndApplySpokeCheckpoint(input) {
|
|
248
|
+
if (!input || typeof input !== "object") throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", "Missing VLedgerCheckpointInput");
|
|
249
|
+
const spoke = require_ofac_screening.normalizeScreeningAddress(assertSafeId(input.spokeChainDid, "spokeChainDid"));
|
|
250
|
+
const reserve = this.reserves.get(spoke);
|
|
251
|
+
if (!reserve) throw new _ocap_util_lib_error.CustomError("NOT_FOUND", `Spoke chain ${spoke} not found`);
|
|
252
|
+
if (!Number.isSafeInteger(input.blockHeight) || input.blockHeight <= reserve.lastCheckpointedHeight) throw new _ocap_util_lib_error.CustomError("STALE_CHECKPOINT_HEIGHT", `Checkpoint height (${input.blockHeight}) must be strictly greater than lastCheckpointedHeight (${reserve.lastCheckpointedHeight})`);
|
|
253
|
+
if (typeof input.prevStateRoot !== "string" || input.prevStateRoot.toLowerCase() !== reserve.currentStateRoot) throw new _ocap_util_lib_error.CustomError("STATE_ROOT_DISCONTINUITY", `Checkpoint prevStateRoot (${input.prevStateRoot}) does not match currentStateRoot (${reserve.currentStateRoot})`);
|
|
254
|
+
const expectedNewRoot = computeVLedgerCheckpointStateRoot({
|
|
255
|
+
spokeChainDid: spoke,
|
|
256
|
+
blockHeight: input.blockHeight,
|
|
257
|
+
prevStateRoot: input.prevStateRoot,
|
|
258
|
+
stateDiffMerkleRoot: input.stateDiffMerkleRoot
|
|
259
|
+
});
|
|
260
|
+
if (typeof input.newStateRoot !== "string" || input.newStateRoot.toLowerCase() !== expectedNewRoot) throw new _ocap_util_lib_error.CustomError("VLEDGER_COMMITMENT_MISMATCH", `Checkpoint newStateRoot (${input.newStateRoot}) does not match VLedger commitment (${expectedNewRoot})`);
|
|
261
|
+
if (!Array.isArray(input.validatorSigners)) throw new _ocap_util_lib_error.CustomError("INVALID_SOVEREIGN_HUB", "validatorSigners must be an array");
|
|
262
|
+
const registeredSet = new Set(reserve.registeredValidators);
|
|
263
|
+
const validUniqueSigners = /* @__PURE__ */ new Set();
|
|
264
|
+
for (const signer of input.validatorSigners) {
|
|
265
|
+
const norm = require_ofac_screening.normalizeScreeningAddress(assertSafeId(signer, "validatorSigner"));
|
|
266
|
+
if (!registeredSet.has(norm)) throw new _ocap_util_lib_error.CustomError("UNAUTHORIZED_VALIDATOR", `Signer ${norm} is not a registered validator for Spoke ${spoke}`);
|
|
267
|
+
validUniqueSigners.add(norm);
|
|
268
|
+
}
|
|
269
|
+
const minQuorum = Math.floor(2 * reserve.registeredValidators.length / 3) + 1;
|
|
270
|
+
if (validUniqueSigners.size < minQuorum) throw new _ocap_util_lib_error.CustomError("INSUFFICIENT_VALIDATOR_QUORUM", `Checkpoint signed by ${validUniqueSigners.size} validators, fewer than BFT quorum (${minQuorum} of ${reserve.registeredValidators.length})`);
|
|
271
|
+
reserve.lastCheckpointedHeight = input.blockHeight;
|
|
272
|
+
reserve.currentStateRoot = expectedNewRoot;
|
|
273
|
+
return JSON.parse(JSON.stringify(reserve));
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Cosmos PFM (Packet Forward Middleware) Atomic Multi-Hop Cross-Spoke Routing (`Spoke A -> ArcHub -> Spoke B`):
|
|
277
|
+
* Atomically verifies `Spoke A` reserve solvency, consumes 1 Crowdloan free relay credit,
|
|
278
|
+
* executes optional Hub token conversion (`tokenIn -> tokenOut` with `minAmountOut` slippage guard),
|
|
279
|
+
* and credits `Spoke B`'s `SovereignReserveState`.
|
|
280
|
+
*/
|
|
281
|
+
executeForwardCrossSpokePacket(input) {
|
|
282
|
+
if (!input || typeof input !== "object") throw new _ocap_util_lib_error.CustomError("INVALID_PFM_PACKET", "Missing PfmCrossSpokePacketInput");
|
|
283
|
+
const packetId = assertSafeId(input.packetId, "packetId");
|
|
284
|
+
if (this.consumedPacketIds.has(packetId)) throw new _ocap_util_lib_error.CustomError("REPLAYED_PFM_PACKET", `PFM packet ${packetId} has already been processed`);
|
|
285
|
+
if (!Array.isArray(input.hopRoute) || input.hopRoute.length !== 3) throw new _ocap_util_lib_error.CustomError("INVALID_PFM_ROUTE", "hopRoute must be a 3-hop tuple [originSpokeDid, arcHubDid, destinationSpokeDid]");
|
|
286
|
+
const originSpoke = require_ofac_screening.normalizeScreeningAddress(assertSafeId(input.hopRoute[0], "hopRoute[0]"));
|
|
287
|
+
const hubHop = require_ofac_screening.normalizeScreeningAddress(assertSafeId(input.hopRoute[1], "hopRoute[1]"));
|
|
288
|
+
const destSpoke = require_ofac_screening.normalizeScreeningAddress(assertSafeId(input.hopRoute[2], "hopRoute[2]"));
|
|
289
|
+
if (hubHop !== this.arcHubDid) throw new _ocap_util_lib_error.CustomError("INVALID_PFM_ROUTE", `Intermediate hop (${hubHop}) must equal arcHubDid (${this.arcHubDid})`);
|
|
290
|
+
if (originSpoke === destSpoke) throw new _ocap_util_lib_error.CustomError("INVALID_PFM_ROUTE", "originSpokeDid and destinationSpokeDid must be distinct");
|
|
291
|
+
const senderDid = require_ofac_screening.normalizeScreeningAddress(assertSafeId(input.senderDid, "senderDid"));
|
|
292
|
+
const recipientDid = require_ofac_screening.normalizeScreeningAddress(assertSafeId(input.recipientDid, "recipientDid"));
|
|
293
|
+
const tokenIn = require_ofac_screening.normalizeScreeningAddress(assertSafeId(input.tokenIn, "tokenIn"));
|
|
294
|
+
const tokenOut = require_ofac_screening.normalizeScreeningAddress(assertSafeId(input.tokenOut, "tokenOut"));
|
|
295
|
+
const combinedOfac = new Set(this.ofacSet);
|
|
296
|
+
if (input.ofacBlocklist) for (const a of input.ofacBlocklist) combinedOfac.add(require_ofac_screening.normalizeScreeningAddress(a));
|
|
297
|
+
require_ofac_screening.assertNotOfacSanctioned([
|
|
298
|
+
originSpoke,
|
|
299
|
+
destSpoke,
|
|
300
|
+
senderDid,
|
|
301
|
+
recipientDid,
|
|
302
|
+
tokenIn,
|
|
303
|
+
tokenOut
|
|
304
|
+
], combinedOfac);
|
|
305
|
+
const originReserve = this.reserves.get(originSpoke);
|
|
306
|
+
const destReserve = this.reserves.get(destSpoke);
|
|
307
|
+
if (!originReserve) throw new _ocap_util_lib_error.CustomError("NOT_FOUND", `Origin Spoke ${originSpoke} not found`);
|
|
308
|
+
if (!destReserve) throw new _ocap_util_lib_error.CustomError("NOT_FOUND", `Destination Spoke ${destSpoke} not found`);
|
|
309
|
+
const originCrowdloan = this.crowdloans.get(originSpoke);
|
|
310
|
+
if (originCrowdloan.remainingEpochRelayCredits <= 0) throw new _ocap_util_lib_error.CustomError("INSUFFICIENT_RELAY_QUOTA", `Origin Spoke ${originSpoke} has exhausted its Crowdloan freeRelayCreditsPerEpoch`);
|
|
311
|
+
const amountInBn = parsePositiveBn(input.amountIn, "amountIn");
|
|
312
|
+
const quotedOutBn = parsePositiveBn(input.quotedAmountOut, "quotedAmountOut");
|
|
313
|
+
const minOutBn = parsePositiveBn(input.minAmountOut, "minAmountOut");
|
|
314
|
+
if (quotedOutBn.lt(minOutBn)) throw new _ocap_util_lib_error.CustomError("PFM_SLIPPAGE_EXCEEDED", `quotedAmountOut (${quotedOutBn.toString(10)}) is below minAmountOut (${minOutBn.toString(10)})`);
|
|
315
|
+
const originAvailBn = new _ocap_util.BN(Object.hasOwn(originReserve.tokens, tokenIn) ? originReserve.tokens[tokenIn] : "0", 10);
|
|
316
|
+
if (originAvailBn.lt(amountInBn)) throw new _ocap_util_lib_error.CustomError("SOVEREIGN_RESERVE_EXCEEDED", `Origin Spoke ${originSpoke} has insufficient SovereignReserveState balance (${originAvailBn.toString(10)}) for ${amountInBn.toString(10)} ${tokenIn}`);
|
|
317
|
+
if (tokenIn === tokenOut) {
|
|
318
|
+
if (!quotedOutBn.eq(amountInBn)) throw new _ocap_util_lib_error.CustomError("INVALID_PFM_SWAP", "Same-token PFM forwarding requires quotedAmountOut === amountIn");
|
|
319
|
+
} else {
|
|
320
|
+
const hubAvailOut = this.hubAmmReserves.get(tokenOut) || new _ocap_util.BN(0);
|
|
321
|
+
if (hubAvailOut.lt(quotedOutBn)) throw new _ocap_util_lib_error.CustomError("INSUFFICIENT_HUB_SWAP_LIQUIDITY", `ArcHub lacks ${tokenOut} liquidity (${hubAvailOut.toString(10)}) for cross-spoke swap (${quotedOutBn.toString(10)})`);
|
|
322
|
+
const hubCurIn = this.hubAmmReserves.get(tokenIn) || new _ocap_util.BN(0);
|
|
323
|
+
if (!hubCurIn.isZero()) {
|
|
324
|
+
const maxConstantProductOut = hubAvailOut.mul(amountInBn).div(hubCurIn.add(amountInBn));
|
|
325
|
+
if (quotedOutBn.gt(maxConstantProductOut)) throw new _ocap_util_lib_error.CustomError("AMM_INVARIANT_VIOLATION", `Quoted output (${quotedOutBn.toString(10)}) exceeds constant-product AMM bound (${maxConstantProductOut.toString(10)})`);
|
|
326
|
+
}
|
|
327
|
+
this.hubAmmReserves.set(tokenIn, hubCurIn.add(amountInBn));
|
|
328
|
+
this.hubAmmReserves.set(tokenOut, hubAvailOut.sub(quotedOutBn));
|
|
329
|
+
}
|
|
330
|
+
originReserve.tokens[tokenIn] = originAvailBn.sub(amountInBn).toString(10);
|
|
331
|
+
const destCurOutBn = new _ocap_util.BN(Object.hasOwn(destReserve.tokens, tokenOut) ? destReserve.tokens[tokenOut] : "0", 10);
|
|
332
|
+
destReserve.tokens[tokenOut] = destCurOutBn.add(quotedOutBn).toString(10);
|
|
333
|
+
originCrowdloan.remainingEpochRelayCredits -= 1;
|
|
334
|
+
this.consumedPacketIds.add(packetId);
|
|
335
|
+
return {
|
|
336
|
+
originReserve: JSON.parse(JSON.stringify(originReserve)),
|
|
337
|
+
destinationReserve: JSON.parse(JSON.stringify(destReserve)),
|
|
338
|
+
remainingOriginRelayCredits: originCrowdloan.remainingEpochRelayCredits
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
getSpokeReserve(spokeChainDid) {
|
|
342
|
+
const spoke = require_ofac_screening.normalizeScreeningAddress(assertSafeId(spokeChainDid, "spokeChainDid"));
|
|
343
|
+
const r = this.reserves.get(spoke);
|
|
344
|
+
return r ? JSON.parse(JSON.stringify(r)) : void 0;
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
//#endregion
|
|
349
|
+
exports.MAX_FREE_RELAY_CREDITS_PER_EPOCH = MAX_FREE_RELAY_CREDITS_PER_EPOCH;
|
|
350
|
+
exports.OcapSovereignAppChainHubEngine = OcapSovereignAppChainHubEngine;
|
|
351
|
+
exports.computeVLedgerCheckpointStateRoot = computeVLedgerCheckpointStateRoot;
|
|
352
|
+
exports.deriveSovereignVaultDid = deriveSovereignVaultDid;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
//#region src/appchain-sovereign-hub.d.ts
|
|
2
|
+
declare const MAX_FREE_RELAY_CREDITS_PER_EPOCH = 1000000;
|
|
3
|
+
/**
|
|
4
|
+
* Deterministically derives the Sovereign Reserve Vault DID for a given `spokeChainDid`
|
|
5
|
+
* (Polkadot XCM Sovereign Account model).
|
|
6
|
+
*/
|
|
7
|
+
declare function deriveSovereignVaultDid(spokeChainDid: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Computes the deterministic VLedger state root transition commitment:
|
|
10
|
+
* `SHA3-256("ocap:vledger-checkpoint:v1:" + spoke.length + ":" + spokeChainDid + ":" + blockHeight + ":" + prevStateRoot + ":" + stateDiffMerkleRoot)`
|
|
11
|
+
*/
|
|
12
|
+
declare function computeVLedgerCheckpointStateRoot(params: {
|
|
13
|
+
spokeChainDid: string;
|
|
14
|
+
blockHeight: number;
|
|
15
|
+
prevStateRoot: string;
|
|
16
|
+
stateDiffMerkleRoot: string;
|
|
17
|
+
}): string;
|
|
18
|
+
interface SovereignReserveState {
|
|
19
|
+
spokeChainDid: string;
|
|
20
|
+
sovereignVaultDid: string;
|
|
21
|
+
tokens: Record<string, string>;
|
|
22
|
+
nfts: string[];
|
|
23
|
+
lastCheckpointedHeight: number;
|
|
24
|
+
currentStateRoot: string;
|
|
25
|
+
registeredValidators: string[];
|
|
26
|
+
}
|
|
27
|
+
interface CrowdloanContributorPosition {
|
|
28
|
+
contributorDid: string;
|
|
29
|
+
stakedAbt: string;
|
|
30
|
+
rewardDebtScaled: string;
|
|
31
|
+
accruedUnclaimedRewards: string;
|
|
32
|
+
}
|
|
33
|
+
interface SpokeCrowdloanState {
|
|
34
|
+
spokeChainDid: string;
|
|
35
|
+
totalStakedAbt: string;
|
|
36
|
+
accRewardPerShareScaled: string;
|
|
37
|
+
unclaimedRewardReserveAbt: string;
|
|
38
|
+
freeRelayCreditsPerEpoch: number;
|
|
39
|
+
remainingEpochRelayCredits: number;
|
|
40
|
+
contributors: Record<string, CrowdloanContributorPosition>;
|
|
41
|
+
}
|
|
42
|
+
interface VLedgerCheckpointInput {
|
|
43
|
+
spokeChainDid: string;
|
|
44
|
+
blockHeight: number;
|
|
45
|
+
prevStateRoot: string;
|
|
46
|
+
stateDiffMerkleRoot: string;
|
|
47
|
+
newStateRoot: string;
|
|
48
|
+
validatorSigners: string[];
|
|
49
|
+
}
|
|
50
|
+
interface PfmCrossSpokePacketInput {
|
|
51
|
+
packetId: string;
|
|
52
|
+
senderDid: string;
|
|
53
|
+
recipientDid: string;
|
|
54
|
+
/** Must be `[originSpokeDid, arcHubDid, destinationSpokeDid]` */
|
|
55
|
+
hopRoute: [string, string, string];
|
|
56
|
+
tokenIn: string;
|
|
57
|
+
amountIn: string;
|
|
58
|
+
tokenOut: string;
|
|
59
|
+
quotedAmountOut: string;
|
|
60
|
+
minAmountOut: string;
|
|
61
|
+
ofacBlocklist?: Iterable<string>;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Polkadot XCM Sovereign Reserve Vault + Crowdloan Quota Staking + VLedger Checkpoint + Cosmos PFM Multi-Hop Hub Engine (#1173).
|
|
65
|
+
*/
|
|
66
|
+
declare class OcapSovereignAppChainHubEngine {
|
|
67
|
+
readonly arcHubDid: string;
|
|
68
|
+
readonly abtTokenAddress: string;
|
|
69
|
+
readonly relayCreditsPerAbtUnit: number;
|
|
70
|
+
private readonly reserves;
|
|
71
|
+
private readonly crowdloans;
|
|
72
|
+
private readonly hubAmmReserves;
|
|
73
|
+
private readonly consumedPacketIds;
|
|
74
|
+
private readonly ofacSet;
|
|
75
|
+
constructor(options: {
|
|
76
|
+
arcHubDid: string;
|
|
77
|
+
abtTokenAddress: string;
|
|
78
|
+
relayCreditsPerAbtUnit?: number;
|
|
79
|
+
ofacBlocklist?: Iterable<string>;
|
|
80
|
+
});
|
|
81
|
+
/**
|
|
82
|
+
* Registers an isomorphic Spoke AppChain with its own isolated `SovereignReserveState` and validator set.
|
|
83
|
+
*/
|
|
84
|
+
registerSpokeChain(params: {
|
|
85
|
+
spokeChainDid: string;
|
|
86
|
+
genesisStateRoot: string;
|
|
87
|
+
validators: string[];
|
|
88
|
+
initialTokens?: Record<string, string>;
|
|
89
|
+
initialNfts?: string[];
|
|
90
|
+
}): SovereignReserveState;
|
|
91
|
+
/**
|
|
92
|
+
* Seeds Hub AMM liquidity for cross-spoke token conversion during Cosmos PFM multi-hop routing.
|
|
93
|
+
*/
|
|
94
|
+
seedHubSwapLiquidity(tokenAddress: string, amount: string): void;
|
|
95
|
+
/**
|
|
96
|
+
* Deposits fungible tokens into a Spoke's `SovereignReserveState` on `ArcHub`.
|
|
97
|
+
*/
|
|
98
|
+
depositToSpokeReserve(params: {
|
|
99
|
+
spokeChainDid: string;
|
|
100
|
+
tokenAddress: string;
|
|
101
|
+
amount: string;
|
|
102
|
+
}): SovereignReserveState;
|
|
103
|
+
/**
|
|
104
|
+
* Withdraws fungible tokens from a Spoke's `SovereignReserveState` on `ArcHub`.
|
|
105
|
+
* Enforces the Polkadot XCM Sovereign Containment Invariant: a compromised Spoke can NEVER
|
|
106
|
+
* withdraw more than its own `SovereignReserveState` balance.
|
|
107
|
+
*/
|
|
108
|
+
withdrawFromSpokeReserve(params: {
|
|
109
|
+
spokeChainDid: string;
|
|
110
|
+
tokenAddress: string;
|
|
111
|
+
amount: string;
|
|
112
|
+
}): SovereignReserveState;
|
|
113
|
+
/**
|
|
114
|
+
* Polkadot Crowdloan & Agile Coretime `ABT` Quota Staking (`CrowdloanContributeTx`):
|
|
115
|
+
* Locks `ABT` for a target `spokeChainDid`, dynamically increasing its `freeRelayCreditsPerEpoch`
|
|
116
|
+
* and tracking `O(1)` `accRewardPerShare` revenue shares without Sybil split inflation.
|
|
117
|
+
*/
|
|
118
|
+
contributeCrowdloanAbt(params: {
|
|
119
|
+
spokeChainDid: string;
|
|
120
|
+
contributorDid: string;
|
|
121
|
+
abtAmount: string;
|
|
122
|
+
}): SpokeCrowdloanState;
|
|
123
|
+
/**
|
|
124
|
+
* Distributes Spoke business revenue to all Crowdloan `ABT` contributors in `O(1)` time via `accRewardPerShare`.
|
|
125
|
+
* Enforces 100% reserve backing by debiting `revenueAmount` from the Spoke's `SovereignReserveState` `ABT` balance.
|
|
126
|
+
*/
|
|
127
|
+
distributeSpokeRevenue(params: {
|
|
128
|
+
spokeChainDid: string;
|
|
129
|
+
revenueAmount: string;
|
|
130
|
+
}): SpokeCrowdloanState;
|
|
131
|
+
/**
|
|
132
|
+
* Claims accrued Crowdloan business revenue for a contributor in `O(1)` time.
|
|
133
|
+
*/
|
|
134
|
+
claimCrowdloanRewards(params: {
|
|
135
|
+
spokeChainDid: string;
|
|
136
|
+
contributorDid: string;
|
|
137
|
+
}): {
|
|
138
|
+
claimedAmount: string;
|
|
139
|
+
contributorPosition: CrowdloanContributorPosition;
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* Verifies a Spoke's `CheckpointRollupTx` using both `2f + 1` Validator Quorum and VLedger incremental
|
|
143
|
+
* state-root transition commitment (`prevStateRoot -> newStateRoot`).
|
|
144
|
+
*/
|
|
145
|
+
verifyAndApplySpokeCheckpoint(input: VLedgerCheckpointInput): SovereignReserveState;
|
|
146
|
+
/**
|
|
147
|
+
* Cosmos PFM (Packet Forward Middleware) Atomic Multi-Hop Cross-Spoke Routing (`Spoke A -> ArcHub -> Spoke B`):
|
|
148
|
+
* Atomically verifies `Spoke A` reserve solvency, consumes 1 Crowdloan free relay credit,
|
|
149
|
+
* executes optional Hub token conversion (`tokenIn -> tokenOut` with `minAmountOut` slippage guard),
|
|
150
|
+
* and credits `Spoke B`'s `SovereignReserveState`.
|
|
151
|
+
*/
|
|
152
|
+
executeForwardCrossSpokePacket(input: PfmCrossSpokePacketInput): {
|
|
153
|
+
originReserve: SovereignReserveState;
|
|
154
|
+
destinationReserve: SovereignReserveState;
|
|
155
|
+
remainingOriginRelayCredits: number;
|
|
156
|
+
};
|
|
157
|
+
getSpokeReserve(spokeChainDid: string): SovereignReserveState | undefined;
|
|
158
|
+
}
|
|
159
|
+
//#endregion
|
|
160
|
+
export { CrowdloanContributorPosition, MAX_FREE_RELAY_CREDITS_PER_EPOCH, OcapSovereignAppChainHubEngine, PfmCrossSpokePacketInput, SovereignReserveState, SpokeCrowdloanState, VLedgerCheckpointInput, computeVLedgerCheckpointStateRoot, deriveSovereignVaultDid };
|