@ocap/tx-protocols 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/execute.d.mts +7 -0
- package/esm/execute.mjs +196 -71
- package/esm/gas-security-guard.d.mts +374 -0
- package/esm/gas-security-guard.mjs +1101 -0
- package/esm/index.d.mts +35 -5
- package/esm/index.mjs +3 -1
- package/esm/pipes/ensure-cost.mjs +314 -58
- package/esm/protocols/account/delegate.mjs +64 -16
- package/esm/protocols/account/migrate.d.mts +3 -0
- package/esm/protocols/account/migrate.mjs +235 -35
- package/esm/protocols/account/revoke-delegate.d.mts +3 -3
- package/esm/protocols/account/revoke-delegate.mjs +70 -38
- package/esm/protocols/asset/acquire-v2.mjs +29 -12
- package/esm/protocols/asset/acquire-v3.mjs +78 -22
- package/esm/protocols/asset/calls/transfer-token.mjs +2 -1
- package/esm/protocols/asset/calls/transfer.mjs +1 -0
- package/esm/protocols/asset/consume.mjs +12 -4
- package/esm/protocols/asset/create.mjs +18 -5
- package/esm/protocols/asset/mint.mjs +23 -1
- package/esm/protocols/asset/pipes/exec-mint-hook.d.mts +21 -1
- package/esm/protocols/asset/pipes/exec-mint-hook.mjs +115 -1
- package/esm/protocols/asset/pipes/extract-factory-tokens.mjs +14 -8
- package/esm/protocols/asset/pipes/verify-itx-address.mjs +9 -0
- package/esm/protocols/asset/pipes/verify-itx-assets.mjs +21 -5
- package/esm/protocols/asset/update.d.mts +13 -2
- package/esm/protocols/asset/update.mjs +133 -14
- package/esm/protocols/factory/create.mjs +14 -6
- package/esm/protocols/governance/claim-stake.mjs +92 -10
- package/esm/protocols/governance/return-stake.mjs +44 -12
- package/esm/protocols/governance/revoke-stake.mjs +17 -5
- package/esm/protocols/governance/slash-stake.mjs +64 -25
- package/esm/protocols/governance/stake.mjs +63 -13
- package/esm/protocols/rollup/claim-reward.mjs +39 -20
- package/esm/protocols/rollup/close.mjs +1 -1
- package/esm/protocols/rollup/create-block.mjs +8 -7
- package/esm/protocols/rollup/create.mjs +17 -6
- package/esm/protocols/rollup/join.mjs +3 -3
- package/esm/protocols/rollup/leave.mjs +1 -1
- package/esm/protocols/rollup/migrate.mjs +7 -4
- package/esm/protocols/rollup/pipes/verify-evidence.mjs +12 -4
- package/esm/protocols/rollup/update.mjs +6 -1
- package/esm/protocols/token/create.mjs +23 -12
- package/esm/protocols/token/deposit-v2.d.mts +5 -1
- package/esm/protocols/token/deposit-v2.mjs +81 -29
- package/esm/protocols/token/withdraw-v2.mjs +33 -16
- package/esm/protocols/token-factory/burn.mjs +43 -10
- package/esm/protocols/token-factory/create.mjs +23 -14
- package/esm/protocols/token-factory/mint.mjs +54 -21
- package/esm/protocols/token-factory/pipes/verify-icon.mjs +1 -1
- package/esm/protocols/token-factory/update.mjs +16 -6
- package/esm/protocols/trade/exchange-v2.d.mts +4 -0
- package/esm/protocols/trade/exchange-v2.mjs +78 -22
- package/esm/protocols/trade/transfer-v2.mjs +44 -13
- package/esm/protocols/trade/transfer-v3.mjs +72 -31
- package/esm/util.d.mts +3 -1
- package/esm/util.mjs +26 -17
- package/lib/execute.cjs +193 -67
- package/lib/execute.d.cts +7 -0
- package/lib/gas-security-guard.cjs +1140 -0
- package/lib/gas-security-guard.d.cts +374 -0
- package/lib/index.cjs +122 -1
- package/lib/index.d.cts +36 -6
- package/lib/pipes/ensure-cost.cjs +313 -57
- package/lib/protocols/account/delegate.cjs +64 -16
- package/lib/protocols/account/migrate.cjs +235 -35
- package/lib/protocols/account/migrate.d.cts +3 -0
- package/lib/protocols/account/revoke-delegate.cjs +71 -39
- package/lib/protocols/account/revoke-delegate.d.cts +3 -3
- package/lib/protocols/asset/acquire-v2.cjs +28 -11
- package/lib/protocols/asset/acquire-v3.cjs +77 -20
- package/lib/protocols/asset/calls/transfer-token.cjs +3 -2
- package/lib/protocols/asset/calls/transfer.cjs +1 -0
- package/lib/protocols/asset/consume.cjs +11 -3
- package/lib/protocols/asset/create.cjs +19 -6
- package/lib/protocols/asset/mint.cjs +23 -1
- package/lib/protocols/asset/pipes/exec-mint-hook.cjs +117 -1
- package/lib/protocols/asset/pipes/exec-mint-hook.d.cts +21 -1
- package/lib/protocols/asset/pipes/extract-factory-tokens.cjs +14 -9
- package/lib/protocols/asset/pipes/verify-itx-address.cjs +9 -0
- package/lib/protocols/asset/pipes/verify-itx-assets.cjs +21 -5
- package/lib/protocols/asset/update.cjs +131 -12
- package/lib/protocols/asset/update.d.cts +13 -2
- package/lib/protocols/factory/create.cjs +14 -6
- package/lib/protocols/governance/claim-stake.cjs +92 -9
- package/lib/protocols/governance/claim-stake.d.cts +1 -1
- package/lib/protocols/governance/return-stake.cjs +44 -11
- package/lib/protocols/governance/return-stake.d.cts +1 -1
- package/lib/protocols/governance/revoke-stake.cjs +17 -5
- package/lib/protocols/governance/revoke-stake.d.cts +1 -1
- package/lib/protocols/governance/slash-stake.cjs +64 -24
- package/lib/protocols/governance/slash-stake.d.cts +1 -1
- package/lib/protocols/governance/stake.cjs +65 -14
- package/lib/protocols/rollup/claim-reward.cjs +39 -20
- package/lib/protocols/rollup/close.cjs +1 -1
- package/lib/protocols/rollup/create-block.cjs +9 -8
- package/lib/protocols/rollup/create-block.d.cts +1 -1
- package/lib/protocols/rollup/create.cjs +18 -7
- package/lib/protocols/rollup/join.cjs +3 -3
- package/lib/protocols/rollup/leave.cjs +1 -1
- package/lib/protocols/rollup/migrate.cjs +7 -4
- package/lib/protocols/rollup/pipes/verify-evidence.cjs +11 -3
- package/lib/protocols/rollup/update.cjs +6 -1
- package/lib/protocols/token/create.cjs +24 -13
- package/lib/protocols/token/deposit-v2.cjs +80 -28
- package/lib/protocols/token/deposit-v2.d.cts +5 -1
- package/lib/protocols/token/withdraw-v2.cjs +32 -15
- package/lib/protocols/token-factory/burn.cjs +42 -9
- package/lib/protocols/token-factory/create.cjs +24 -15
- package/lib/protocols/token-factory/mint.cjs +53 -20
- package/lib/protocols/token-factory/pipes/verify-icon.cjs +1 -1
- package/lib/protocols/token-factory/update.cjs +16 -6
- package/lib/protocols/trade/exchange-v2.cjs +76 -20
- package/lib/protocols/trade/exchange-v2.d.cts +4 -0
- package/lib/protocols/trade/transfer-v2.cjs +45 -14
- package/lib/protocols/trade/transfer-v3.cjs +72 -30
- package/lib/util.cjs +26 -17
- package/lib/util.d.cts +4 -2
- package/package.json +16 -16
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
|
|
3
|
+
let _arcblock_did = require("@arcblock/did");
|
|
4
|
+
let _ocap_state = require("@ocap/state");
|
|
3
5
|
let _ocap_util = require("@ocap/util");
|
|
4
6
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
7
|
+
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
5
8
|
let lodash_get = require("lodash/get");
|
|
6
9
|
lodash_get = require_rolldown_runtime.__toESM(lodash_get);
|
|
7
|
-
let _arcblock_did = require("@arcblock/did");
|
|
8
|
-
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
9
10
|
let _ocap_wallet = require("@ocap/wallet");
|
|
10
11
|
|
|
11
12
|
//#region src/protocols/rollup/pipes/verify-evidence.ts
|
|
@@ -19,9 +20,16 @@ function CreateVerifyEvidencePipe({ evidenceKey, signaturesKey, verifyMethod = "
|
|
|
19
20
|
for (const sig of signatures) {
|
|
20
21
|
const { signer, pk, delegator, signature } = sig;
|
|
21
22
|
const address = delegator || signer;
|
|
23
|
+
if ((0, _arcblock_did.isFromPublicKey)(address, pk) === false) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Signature for evidence signer ${address} and pk do not match`));
|
|
22
24
|
const wallet = (0, _ocap_wallet.fromPublicKey)(pk, (0, _arcblock_did.toTypeInfo)(address));
|
|
23
25
|
try {
|
|
24
|
-
|
|
26
|
+
const targetType = _ocap_state.rollup.getRollupTargetType(context.rollupState);
|
|
27
|
+
const effectiveMethod = verifyMethod === "ethVerify" && targetType !== "EVM" && !(0, _arcblock_did.isEthereumDid)(address) ? "verify" : verifyMethod;
|
|
28
|
+
const hexEvidence = (0, _ocap_util.toHex)(evidence);
|
|
29
|
+
const hexSig = (0, _ocap_util.toHex)(signature);
|
|
30
|
+
let verified = await wallet[effectiveMethod](hexEvidence, hexSig);
|
|
31
|
+
if (!verified && verifyMethod === "ethVerify" && targetType !== "EVM" && (0, _arcblock_did.isEthereumDid)(address)) verified = await wallet.verify(hexEvidence, hexSig);
|
|
32
|
+
if (verified === false) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Signature for evidence from ${address} is not valid`));
|
|
25
33
|
} catch (err) {
|
|
26
34
|
return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Signature for evidence from ${address} verify failed: ${err.message}`));
|
|
27
35
|
}
|
|
@@ -95,11 +95,16 @@ runner.use(_ocap_tx_pipeline.pipes.TakeStateSnapshot());
|
|
|
95
95
|
runner.use(async (context, next) => {
|
|
96
96
|
const { tx, itx, rollupState, statedb, senderState, senderUpdates, updateVaults } = context;
|
|
97
97
|
const data = require_util.decodeAnySafe(itx.data ?? null);
|
|
98
|
+
const sanitizedItx = { ...itx };
|
|
99
|
+
delete sanitizedItx.reserves;
|
|
100
|
+
delete sanitizedItx.targetType;
|
|
101
|
+
delete sanitizedItx.targetChainId;
|
|
102
|
+
delete sanitizedItx.enforceReserveWatermark;
|
|
98
103
|
const [newSenderState, newRollupState] = await Promise.all([statedb.account.update(senderState.address, _ocap_state.account.update(senderState, {
|
|
99
104
|
nonce: tx.nonce,
|
|
100
105
|
...senderUpdates
|
|
101
106
|
}, context), context), statedb.rollup.update(itx.rollup, _ocap_state.rollup.update(rollupState, {
|
|
102
|
-
...
|
|
107
|
+
...sanitizedItx,
|
|
103
108
|
data
|
|
104
109
|
}, context), context)]);
|
|
105
110
|
if (updateVaults) await updateVaults();
|
|
@@ -3,15 +3,15 @@ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
|
3
3
|
const require_util = require('../../util.cjs');
|
|
4
4
|
const require_pipes_ensure_cost = require('../../pipes/ensure-cost.cjs');
|
|
5
5
|
const require_pipes_ensure_gas = require('../../pipes/ensure-gas.cjs');
|
|
6
|
+
let _arcblock_did_util = require("@arcblock/did-util");
|
|
6
7
|
let _ocap_state = require("@ocap/state");
|
|
7
8
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
8
9
|
let _ocap_util = require("@ocap/util");
|
|
9
10
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
10
|
-
let _arcblock_did_util = require("@arcblock/did-util");
|
|
11
|
-
let debug = require("debug");
|
|
12
|
-
debug = require_rolldown_runtime.__toESM(debug);
|
|
13
11
|
let lodash_cloneDeep = require("lodash/cloneDeep");
|
|
14
12
|
lodash_cloneDeep = require_rolldown_runtime.__toESM(lodash_cloneDeep);
|
|
13
|
+
let debug = require("debug");
|
|
14
|
+
debug = require_rolldown_runtime.__toESM(debug);
|
|
15
15
|
let _arcblock_validator = require("@arcblock/validator");
|
|
16
16
|
let lodash_uniq = require("lodash/uniq");
|
|
17
17
|
lodash_uniq = require_rolldown_runtime.__toESM(lodash_uniq);
|
|
@@ -33,11 +33,11 @@ const schema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
|
|
|
33
33
|
symbol: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.minLength(2), _arcblock_validator.v.maxLength(6), _arcblock_validator.v.toUpperCase()),
|
|
34
34
|
unit: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.minLength(1), _arcblock_validator.v.maxLength(6), _arcblock_validator.v.toLowerCase()),
|
|
35
35
|
decimal: _arcblock_validator.v.pipe(_arcblock_validator.v.number(), _arcblock_validator.v.minValue(2), _arcblock_validator.v.maxValue(18)),
|
|
36
|
-
icon: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.
|
|
36
|
+
icon: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.string())),
|
|
37
37
|
totalSupply: (0, _arcblock_validator.vBN)((0, _arcblock_validator.vBNGreater)(0), (0, _arcblock_validator.vBNMax)(MAX_TOTAL_SUPPLY)),
|
|
38
38
|
initialSupply: (0, _arcblock_validator.vBN)((0, _arcblock_validator.vBNGreater)(0)),
|
|
39
39
|
maxTotalSupply: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.union([(0, _arcblock_validator.vBN)((0, _arcblock_validator.vBNGreater)(0)), _arcblock_validator.v.literal("")]))),
|
|
40
|
-
foreignToken: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.schemas.foreignToken)),
|
|
40
|
+
foreignToken: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_ocap_state.token.multiChainForeignTokenSchema ?? _arcblock_validator.schemas.foreignToken)),
|
|
41
41
|
spendersList: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.pipe(_arcblock_validator.v.array((0, _arcblock_validator.vDID)({ prefix: true })), _arcblock_validator.v.maxLength(30)))),
|
|
42
42
|
tokenFactoryAddress: _arcblock_validator.v.optional((0, _arcblock_validator.vForbidden)("tokenFactoryAddress is forbidden")),
|
|
43
43
|
data: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.any()))
|
|
@@ -51,6 +51,13 @@ runner.use(({ itx }, next) => {
|
|
|
51
51
|
const spendersList = value.spendersList;
|
|
52
52
|
itx.spenders = spendersList?.length ? (0, lodash_uniq.default)(spendersList) : null;
|
|
53
53
|
delete itx.spendersList;
|
|
54
|
+
if (typeof itx.icon === "string" && itx.icon.trim()) {
|
|
55
|
+
const trimmedIcon = itx.icon.trim();
|
|
56
|
+
if (trimmedIcon.startsWith("<svg")) {
|
|
57
|
+
if (Buffer.byteLength(trimmedIcon, "utf8") > 16 * 1024) return next(new _ocap_util_lib_error.CustomError("INVALID_ICON", "Token Icon must be less than 16k"));
|
|
58
|
+
if (_ocap_state.token.sanitizeTokenSvg(trimmedIcon) !== trimmedIcon) return next(new _ocap_util_lib_error.CustomError("INVALID_ICON", "Token Icon contains unsafe SVG elements or attributes"));
|
|
59
|
+
} else if (/^data:/i.test(trimmedIcon) && !_ocap_state.token.iconToNftDisplay(trimmedIcon)) return next(new _ocap_util_lib_error.CustomError("INVALID_ICON", "Token Icon data URI must be a safe image format"));
|
|
60
|
+
}
|
|
54
61
|
return next();
|
|
55
62
|
});
|
|
56
63
|
runner.use(_ocap_tx_pipeline.pipes.VerifyInfo([{
|
|
@@ -84,6 +91,7 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
|
|
|
84
91
|
stateKey: "delegatorState",
|
|
85
92
|
addressKey: "tx.delegator"
|
|
86
93
|
}));
|
|
94
|
+
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: ["senderState", "delegatorState"] }));
|
|
87
95
|
runner.use(_ocap_tx_pipeline.pipes.VerifyDelegation({
|
|
88
96
|
type: "signature",
|
|
89
97
|
signerKey: "senderState",
|
|
@@ -129,20 +137,23 @@ runner.use(async (context, next) => {
|
|
|
129
137
|
delegatorUpdates.tokens = delegatorState.tokens || {};
|
|
130
138
|
delegatorUpdates.tokens[itx.address] = itx.initialSupply;
|
|
131
139
|
} else updates.tokens[itx.address] = itx.initialSupply;
|
|
140
|
+
const initialTokenState = _ocap_state.token.create({
|
|
141
|
+
...(0, lodash_cloneDeep.default)(itx),
|
|
142
|
+
data,
|
|
143
|
+
issuer: owner,
|
|
144
|
+
type: "Token"
|
|
145
|
+
}, context);
|
|
146
|
+
const charterAsset = _ocap_state.token.buildTokenCharterAssetState(initialTokenState, context, owner);
|
|
132
147
|
const [newSenderState, tokenState, newDelegatorState, newDelegationState] = await Promise.all([
|
|
133
148
|
statedb.account.update(senderState.address, _ocap_state.account.update(senderState, {
|
|
134
149
|
nonce: tx.nonce,
|
|
135
|
-
pk: tx.pk,
|
|
150
|
+
pk: tx.delegator ? senderState.pk : tx.pk,
|
|
136
151
|
...senderUpdates
|
|
137
152
|
}, context), context),
|
|
138
|
-
statedb.token.create(itx.address,
|
|
139
|
-
...(0, lodash_cloneDeep.default)(itx),
|
|
140
|
-
data,
|
|
141
|
-
issuer: owner,
|
|
142
|
-
type: "Token"
|
|
143
|
-
}, context), context),
|
|
153
|
+
statedb.token.create(itx.address, initialTokenState, context),
|
|
144
154
|
delegatorState ? statedb.account.update(delegatorState.address, _ocap_state.account.update(delegatorState, delegatorUpdates, context), context) : null,
|
|
145
|
-
context.isDelegationChanged ? statedb.delegation.update(delegationState.address, _ocap_state.delegation.update(delegationState, {}, context), context) : delegationState
|
|
155
|
+
context.isDelegationChanged ? statedb.delegation.update(delegationState.address, _ocap_state.delegation.update(delegationState, {}, context), context) : delegationState,
|
|
156
|
+
statedb.asset?.create ? Promise.resolve(statedb.asset.get?.(charterAsset.address, context)).then((existing) => existing && statedb.asset.update ? statedb.asset.update(charterAsset.address, charterAsset, context) : statedb.asset.create(charterAsset.address, charterAsset, context)) : null
|
|
146
157
|
]);
|
|
147
158
|
if (updateVaults) await updateVaults();
|
|
148
159
|
context.senderState = newSenderState;
|
|
@@ -5,28 +5,22 @@ const require_pipes_ensure_cost = require('../../pipes/ensure-cost.cjs');
|
|
|
5
5
|
const require_pipes_ensure_gas = require('../../pipes/ensure-gas.cjs');
|
|
6
6
|
const require_protocols_rollup_pipes_verify_signers = require('../rollup/pipes/verify-signers.cjs');
|
|
7
7
|
const require_protocols_rollup_pipes_verify_status = require('../rollup/pipes/verify-status.cjs');
|
|
8
|
+
let _arcblock_did_util = require("@arcblock/did-util");
|
|
8
9
|
let _ocap_state = require("@ocap/state");
|
|
9
10
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
10
11
|
let _ocap_util = require("@ocap/util");
|
|
11
12
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
12
|
-
let
|
|
13
|
+
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
13
14
|
let debug = require("debug");
|
|
14
15
|
debug = require_rolldown_runtime.__toESM(debug);
|
|
15
|
-
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
16
16
|
let _arcblock_validator = require("@arcblock/validator");
|
|
17
17
|
|
|
18
18
|
//#region src/protocols/token/deposit-v2.ts
|
|
19
19
|
const debug$1 = (0, debug.default)("@ocap/tx-protocols:deposit-token");
|
|
20
20
|
const schema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
|
|
21
21
|
token: _arcblock_validator.schemas.tokenInput,
|
|
22
|
-
to: (0, _arcblock_validator.vDID)({
|
|
23
|
-
|
|
24
|
-
wallet: "ethereum"
|
|
25
|
-
}),
|
|
26
|
-
proposer: (0, _arcblock_validator.vDID)({
|
|
27
|
-
prefix: true,
|
|
28
|
-
wallet: "ethereum"
|
|
29
|
-
}),
|
|
22
|
+
to: (0, _arcblock_validator.vDID)({ prefix: true }),
|
|
23
|
+
proposer: (0, _arcblock_validator.vDID)({ prefix: true }),
|
|
30
24
|
evidence: _arcblock_validator.v.object({ hash: _arcblock_validator.v.pipe(_arcblock_validator.v.string(), _arcblock_validator.v.regex(_arcblock_validator.patterns.txHash)) }),
|
|
31
25
|
rollup: (0, _arcblock_validator.vDID)({
|
|
32
26
|
prefix: true,
|
|
@@ -36,13 +30,9 @@ const schema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
|
|
|
36
30
|
data: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.any()))
|
|
37
31
|
}), (0, _arcblock_validator.vStripUnknown)());
|
|
38
32
|
const runner = new _ocap_tx_pipeline.Runner("deposit_token_v2");
|
|
39
|
-
runner.use(({
|
|
33
|
+
runner.use(({ itx }, next) => {
|
|
40
34
|
const { error } = (0, _arcblock_validator.vValidate)(schema, itx);
|
|
41
35
|
if (error) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Invalid itx: ${error.message}`));
|
|
42
|
-
if (itx.to !== tx.from) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "You can only deposit to tx sender account"));
|
|
43
|
-
const signatures = (0, _ocap_util_lib_get_list_field.getListField)(tx, "signatures");
|
|
44
|
-
if (signatures.some((x) => x.signer === itx.proposer) === false) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "itx.proposer must exist in signatures"));
|
|
45
|
-
if (signatures.some((x) => x.signer === tx.from)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "tx.from should not exist in signatures"));
|
|
46
36
|
return next();
|
|
47
37
|
});
|
|
48
38
|
runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
@@ -51,10 +41,31 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
51
41
|
status: "INVALID_ROLLUP",
|
|
52
42
|
table: "rollup"
|
|
53
43
|
}));
|
|
44
|
+
runner.use((context, next) => {
|
|
45
|
+
const { tx, itx, rollupState } = context;
|
|
46
|
+
if (_ocap_state.rollup.getRollupTargetType(rollupState) === "EVM") {
|
|
47
|
+
if ((0, _arcblock_validator.vValidate)((0, _arcblock_validator.vDID)({
|
|
48
|
+
prefix: true,
|
|
49
|
+
wallet: "ethereum"
|
|
50
|
+
}), itx.to).error) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Invalid itx: \"to\" Expected wallet type to be \"ethereum\""));
|
|
51
|
+
if ((0, _arcblock_validator.vValidate)((0, _arcblock_validator.vDID)({
|
|
52
|
+
prefix: true,
|
|
53
|
+
wallet: "ethereum"
|
|
54
|
+
}), itx.proposer).error) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Invalid itx: \"proposer\" Expected wallet type to be \"ethereum\""));
|
|
55
|
+
}
|
|
56
|
+
const isProposerSponsor = _ocap_state.rollup.isProposerSponsoredDepositAllowed(rollupState) && tx.from === itx.proposer && itx.to !== tx.from;
|
|
57
|
+
context.isProposerSponsor = isProposerSponsor;
|
|
58
|
+
if (itx.to !== tx.from && !isProposerSponsor) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "You can only deposit to tx sender account"));
|
|
59
|
+
const signatures = (0, _ocap_util_lib_get_list_field.getListField)(tx, "signatures");
|
|
60
|
+
if (signatures.some((x) => x.signer === itx.proposer) === false) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "itx.proposer must exist in signatures"));
|
|
61
|
+
if (!isProposerSponsor && signatures.some((x) => x.signer === tx.from)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "tx.from should not exist in signatures"));
|
|
62
|
+
if (isProposerSponsor && signatures.some((x) => x.signer === itx.to)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "itx.to should not exist in signatures"));
|
|
63
|
+
return next();
|
|
64
|
+
});
|
|
54
65
|
runner.use(require_protocols_rollup_pipes_verify_status.default({ paused: false }));
|
|
55
66
|
runner.use((context, next) => {
|
|
56
67
|
const { itx, rollupState } = context;
|
|
57
|
-
if (rollupState
|
|
68
|
+
if (!_ocap_state.rollup.isTokenSupportedByRollup(rollupState, itx.token.address)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Deposit token address does not match with rollup state"));
|
|
58
69
|
if (rollupState.validators.some((x) => x.address === itx.proposer) === false) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "itx.proposer does not exist in validators"));
|
|
59
70
|
return next();
|
|
60
71
|
});
|
|
@@ -105,7 +116,7 @@ runner.use((context, next) => {
|
|
|
105
116
|
const maxDepositAmount = new _ocap_util.BN(rollupState.maxDepositAmount);
|
|
106
117
|
if (depositAmount.lt(minDepositAmount)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Deposit amount must be greater than minDepositAmount: ${(0, _ocap_util.fromUnitToToken)(minDepositAmount, tokenState.decimal)}`));
|
|
107
118
|
if (depositAmount.gt(maxDepositAmount)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Deposit amount must be less than maxDepositAmount: ${(0, _ocap_util.fromUnitToToken)(maxDepositAmount, tokenState.decimal)}`));
|
|
108
|
-
const stakedAmount = new _ocap_util.BN(stakeState.tokens[
|
|
119
|
+
const stakedAmount = new _ocap_util.BN(stakeState.tokens[itx.token.address] || 0);
|
|
109
120
|
if (depositAmount.gt(stakedAmount)) return next(new _ocap_util_lib_error.CustomError("INSUFFICIENT_STAKE", `Deposit amount must be less than available stake: ${(0, _ocap_util.fromUnitToToken)(stakedAmount, tokenState.decimal)}`));
|
|
110
121
|
return next();
|
|
111
122
|
});
|
|
@@ -127,6 +138,13 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
127
138
|
status: "OK",
|
|
128
139
|
table: "account"
|
|
129
140
|
}));
|
|
141
|
+
runner.use(async (context, next) => {
|
|
142
|
+
if (context.isProposerSponsor) {
|
|
143
|
+
context.receiverState = await context.statedb.account.get(context.itx.to, context);
|
|
144
|
+
if (context.receiverState && (context.receiverState.address !== context.itx.to || _ocap_state.account.isMigrated(context.receiverState))) return next(new _ocap_util_lib_error.CustomError("ACCOUNT_MIGRATED", `Recipient account is already migrated: ${context.itx.to}`));
|
|
145
|
+
}
|
|
146
|
+
return next();
|
|
147
|
+
});
|
|
130
148
|
runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
131
149
|
from: "signers",
|
|
132
150
|
to: "signerStates",
|
|
@@ -138,6 +156,11 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
|
|
|
138
156
|
stateKey: "senderState",
|
|
139
157
|
addressKey: "tx.from"
|
|
140
158
|
}));
|
|
159
|
+
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: [
|
|
160
|
+
"senderState",
|
|
161
|
+
"receiverState",
|
|
162
|
+
"signerStates"
|
|
163
|
+
] }));
|
|
141
164
|
runner.use(require_pipes_ensure_gas.default((context) => {
|
|
142
165
|
const result = {
|
|
143
166
|
create: 1,
|
|
@@ -146,6 +169,8 @@ runner.use(require_pipes_ensure_gas.default((context) => {
|
|
|
146
169
|
};
|
|
147
170
|
if (context.senderState) result.update += 1;
|
|
148
171
|
else result.create += 1;
|
|
172
|
+
if (context.isProposerSponsor) if (context.receiverState) result.update += 1;
|
|
173
|
+
else result.create += 1;
|
|
149
174
|
if (context.lockerState) result.update += 1;
|
|
150
175
|
else result.create += 1;
|
|
151
176
|
return result;
|
|
@@ -153,7 +178,7 @@ runner.use(require_pipes_ensure_gas.default((context) => {
|
|
|
153
178
|
runner.use(require_pipes_ensure_cost.default({ attachSenderChanges: true }));
|
|
154
179
|
runner.use(_ocap_tx_pipeline.pipes.TakeStateSnapshot());
|
|
155
180
|
runner.use(async (context, next) => {
|
|
156
|
-
const { tx, itx, statedb, senderState, stakeState, stakeAddress, lockerState, lockerAddress, senderChange, updateVaults } = context;
|
|
181
|
+
const { tx, itx, statedb, rollupState, senderState, receiverState, isProposerSponsor, stakeState, stakeAddress, lockerState, lockerAddress, senderChange, updateVaults } = context;
|
|
157
182
|
const user = itx.token.value;
|
|
158
183
|
const fee = itx.actualFee;
|
|
159
184
|
const total = require_util.getBNSum(user, fee);
|
|
@@ -161,23 +186,43 @@ runner.use(async (context, next) => {
|
|
|
161
186
|
address: itx.token.address,
|
|
162
187
|
value: total
|
|
163
188
|
}], stakeState, "sub");
|
|
164
|
-
let senderUpdates = require_util.applyTokenUpdates([{
|
|
165
|
-
address: itx.token.address,
|
|
166
|
-
value: user
|
|
167
|
-
}], senderState || {}, "add");
|
|
168
189
|
const lockerUpdates = require_util.applyTokenUpdates([{
|
|
169
190
|
address: itx.token.address,
|
|
170
191
|
value: fee
|
|
171
192
|
}], lockerState || { tokens: {} }, "add");
|
|
172
|
-
if (senderChange && senderState) senderUpdates = require_util.applyTokenChange(senderUpdates, senderChange);
|
|
173
193
|
const sender = senderState ? senderState.address : tx.from;
|
|
174
|
-
|
|
175
|
-
|
|
194
|
+
let newSenderState;
|
|
195
|
+
let newReceiverState;
|
|
196
|
+
if (isProposerSponsor) {
|
|
197
|
+
let proposerUpdates = {};
|
|
198
|
+
if (senderChange && senderState) proposerUpdates = require_util.applyTokenChange(senderState, senderChange);
|
|
199
|
+
const recipientUpdates = require_util.applyTokenUpdates([{
|
|
200
|
+
address: itx.token.address,
|
|
201
|
+
value: user
|
|
202
|
+
}], receiverState || {}, "add");
|
|
203
|
+
[newSenderState, newReceiverState] = await Promise.all([statedb.account.updateOrCreate(senderState ?? null, _ocap_state.account.updateOrCreate(senderState ?? null, {
|
|
204
|
+
address: sender,
|
|
205
|
+
nonce: tx.nonce,
|
|
206
|
+
pk: tx.pk,
|
|
207
|
+
...proposerUpdates
|
|
208
|
+
}, context), context), statedb.account.updateOrCreate(receiverState ?? null, _ocap_state.account.updateOrCreate(receiverState ?? null, {
|
|
209
|
+
address: itx.to,
|
|
210
|
+
...recipientUpdates
|
|
211
|
+
}, context), context)]);
|
|
212
|
+
} else {
|
|
213
|
+
let senderUpdates = require_util.applyTokenUpdates([{
|
|
214
|
+
address: itx.token.address,
|
|
215
|
+
value: user
|
|
216
|
+
}], senderState || {}, "add");
|
|
217
|
+
if (senderChange && senderState) senderUpdates = require_util.applyTokenChange(senderUpdates, senderChange);
|
|
218
|
+
newSenderState = await statedb.account.updateOrCreate(senderState ?? null, _ocap_state.account.updateOrCreate(senderState ?? null, {
|
|
176
219
|
address: sender,
|
|
177
220
|
nonce: tx.nonce,
|
|
178
221
|
pk: tx.pk,
|
|
179
222
|
...senderUpdates
|
|
180
|
-
}, context), context)
|
|
223
|
+
}, context), context);
|
|
224
|
+
}
|
|
225
|
+
const [newStakeState, newLockerState, evidenceState, newRollupState] = await Promise.all([
|
|
181
226
|
statedb.stake.update(stakeAddress, _ocap_state.stake.update(stakeState, stakeUpdates, context), context),
|
|
182
227
|
lockerState ? statedb.stake.update(lockerState.address, _ocap_state.stake.update(lockerState, lockerUpdates, context), context) : statedb.stake.create(lockerAddress, _ocap_state.stake.create({
|
|
183
228
|
address: lockerAddress,
|
|
@@ -191,13 +236,20 @@ runner.use(async (context, next) => {
|
|
|
191
236
|
statedb.evidence.create(itx.evidence.hash, _ocap_state.evidence.create({
|
|
192
237
|
hash: itx.evidence.hash,
|
|
193
238
|
data: "rollup-deposit"
|
|
194
|
-
}, context), context)
|
|
239
|
+
}, context), context),
|
|
240
|
+
_ocap_state.rollup.hasRollupReserveTracking(rollupState) ? statedb.rollup.update(itx.rollup, _ocap_state.rollup.recordRollupDeposit(rollupState, itx.token.address, itx.token.value, context), context) : Promise.resolve(rollupState)
|
|
195
241
|
]);
|
|
196
242
|
if (updateVaults) await updateVaults();
|
|
197
243
|
context.senderState = newSenderState;
|
|
244
|
+
if (isProposerSponsor && newReceiverState) {
|
|
245
|
+
context.receiverState = newReceiverState;
|
|
246
|
+
context.signerStates = (context.signerStates || []).map((s) => s.address === newSenderState.address ? newSenderState : s);
|
|
247
|
+
}
|
|
248
|
+
context.rollupState = newRollupState;
|
|
198
249
|
context.stakeState = newStakeState;
|
|
199
250
|
context.evidenceState = evidenceState;
|
|
200
251
|
context.stakeStates = [newStakeState, newLockerState];
|
|
252
|
+
const recipientAddress = isProposerSponsor ? itx.to : sender;
|
|
201
253
|
context.updatedAccounts = context.updatedAccounts || [];
|
|
202
254
|
context.updatedAccounts.push({
|
|
203
255
|
address: stakeAddress,
|
|
@@ -205,7 +257,7 @@ runner.use(async (context, next) => {
|
|
|
205
257
|
delta: `-${total}`,
|
|
206
258
|
action: "unlock"
|
|
207
259
|
}, {
|
|
208
|
-
address:
|
|
260
|
+
address: recipientAddress,
|
|
209
261
|
token: itx.token.address,
|
|
210
262
|
delta: user,
|
|
211
263
|
action: "unlock"
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IReadyContext, IStakeState, IWithEvidence, IWithRollup, IWithSender, IWithSigners, IWithStake, IWithToken, TDepositTokenV2Tx } from "@ocap/types";
|
|
1
|
+
import { IAccountState, IReadyContext, IStakeState, IWithEvidence, IWithRollup, IWithSender, IWithSigners, IWithStake, IWithToken, TDepositTokenV2Tx } from "@ocap/types";
|
|
2
2
|
import { Runner } from "@ocap/tx-pipeline";
|
|
3
3
|
|
|
4
4
|
//#region src/protocols/token/deposit-v2.d.ts
|
|
@@ -21,6 +21,10 @@ type Context = IReadyContext<TDepositTokenV2Tx> & Partial<IWithSender> & Partial
|
|
|
21
21
|
lockerAddress?: string;
|
|
22
22
|
/** Locker stake state */
|
|
23
23
|
lockerState?: IStakeState;
|
|
24
|
+
/** Recipient account state when proposer-sponsored (itx.to !== tx.from) */
|
|
25
|
+
receiverState?: IAccountState | null;
|
|
26
|
+
/** Whether deposit is submitted by proposer on behalf of itx.to */
|
|
27
|
+
isProposerSponsor?: boolean;
|
|
24
28
|
/** Signer addresses */
|
|
25
29
|
signers?: string[];
|
|
26
30
|
/** All updated stake states */
|
|
@@ -4,14 +4,14 @@ const require_util = require('../../util.cjs');
|
|
|
4
4
|
const require_pipes_ensure_cost = require('../../pipes/ensure-cost.cjs');
|
|
5
5
|
const require_pipes_ensure_gas = require('../../pipes/ensure-gas.cjs');
|
|
6
6
|
const require_protocols_rollup_pipes_verify_status = require('../rollup/pipes/verify-status.cjs');
|
|
7
|
+
let _arcblock_did_util = require("@arcblock/did-util");
|
|
7
8
|
let _ocap_state = require("@ocap/state");
|
|
8
9
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
9
10
|
let _ocap_util = require("@ocap/util");
|
|
10
11
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
11
|
-
let
|
|
12
|
+
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
12
13
|
let debug = require("debug");
|
|
13
14
|
debug = require_rolldown_runtime.__toESM(debug);
|
|
14
|
-
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
15
15
|
let _arcblock_validator = require("@arcblock/validator");
|
|
16
16
|
|
|
17
17
|
//#region src/protocols/token/withdraw-v2.ts
|
|
@@ -19,27 +19,20 @@ const debug$1 = (0, debug.default)("@ocap/tx-protocols:withdraw-token");
|
|
|
19
19
|
const verifyMultiSigV2 = _ocap_tx_pipeline.pipes.VerifyMultiSigV2({ signersKey: "signers" });
|
|
20
20
|
const schema = _arcblock_validator.v.pipe(_arcblock_validator.v.object({
|
|
21
21
|
token: _arcblock_validator.schemas.tokenInput,
|
|
22
|
-
to: (0, _arcblock_validator.vDID)({
|
|
23
|
-
prefix: true,
|
|
24
|
-
wallet: "ethereum"
|
|
25
|
-
}),
|
|
22
|
+
to: (0, _arcblock_validator.vDID)({ prefix: true }),
|
|
26
23
|
rollup: (0, _arcblock_validator.vDID)({
|
|
27
24
|
prefix: true,
|
|
28
25
|
role: "ROLE_ROLLUP"
|
|
29
26
|
}),
|
|
30
|
-
proposer: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.union([_arcblock_validator.v.literal(""), (0, _arcblock_validator.vDID)({
|
|
31
|
-
prefix: true,
|
|
32
|
-
wallet: "ethereum"
|
|
33
|
-
})])), ""),
|
|
27
|
+
proposer: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.union([_arcblock_validator.v.literal(""), (0, _arcblock_validator.vDID)({ prefix: true })])), ""),
|
|
34
28
|
actualFee: (0, _arcblock_validator.vBN)((0, _arcblock_validator.vBNMin)(0)),
|
|
35
29
|
maxFee: (0, _arcblock_validator.vBN)((0, _arcblock_validator.vBNMin)(0)),
|
|
36
30
|
data: _arcblock_validator.v.optional(_arcblock_validator.v.nullable(_arcblock_validator.v.any()))
|
|
37
31
|
}), (0, _arcblock_validator.vStripUnknown)());
|
|
38
32
|
const runner = new _ocap_tx_pipeline.Runner("withdraw_token_v2");
|
|
39
|
-
runner.use(({
|
|
33
|
+
runner.use(({ itx }, next) => {
|
|
40
34
|
const { error } = (0, _arcblock_validator.vValidate)(schema, itx);
|
|
41
35
|
if (error) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Invalid itx: ${error.message}`));
|
|
42
|
-
if (itx.to !== tx.from) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "You can only withdraw to tx sender account"));
|
|
43
36
|
return next();
|
|
44
37
|
});
|
|
45
38
|
runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
@@ -48,10 +41,31 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
48
41
|
status: "INVALID_ROLLUP",
|
|
49
42
|
table: "rollup"
|
|
50
43
|
}));
|
|
44
|
+
runner.use((context, next) => {
|
|
45
|
+
const { tx, itx, rollupState } = context;
|
|
46
|
+
if (_ocap_state.rollup.getRollupTargetType(rollupState) === "EVM") {
|
|
47
|
+
if ((0, _arcblock_validator.vValidate)((0, _arcblock_validator.vDID)({
|
|
48
|
+
prefix: true,
|
|
49
|
+
wallet: "ethereum"
|
|
50
|
+
}), itx.to).error) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Invalid itx: \"to\" Expected wallet type to be \"ethereum\""));
|
|
51
|
+
if (itx.proposer && (0, _arcblock_validator.vValidate)((0, _arcblock_validator.vDID)({
|
|
52
|
+
prefix: true,
|
|
53
|
+
wallet: "ethereum"
|
|
54
|
+
}), itx.proposer).error) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Invalid itx: \"proposer\" Expected wallet type to be \"ethereum\""));
|
|
55
|
+
}
|
|
56
|
+
if (itx.to !== tx.from) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "You can only withdraw to tx sender account"));
|
|
57
|
+
return next();
|
|
58
|
+
});
|
|
51
59
|
runner.use(require_protocols_rollup_pipes_verify_status.default({ paused: false }));
|
|
52
60
|
runner.use((context, next) => {
|
|
53
61
|
const { itx, rollupState } = context;
|
|
54
|
-
if (rollupState
|
|
62
|
+
if (!_ocap_state.rollup.isTokenSupportedByRollup(rollupState, itx.token.address)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", "Withdraw token address does not match with rollup"));
|
|
63
|
+
if (_ocap_state.rollup.hasRollupReserveTracking(rollupState)) {
|
|
64
|
+
const reserve = _ocap_state.rollup.getRollupTokenReserve(rollupState, itx.token.address);
|
|
65
|
+
const nextWithdrawn = new _ocap_util.BN(reserve.totalWithdrawn).add(new _ocap_util.BN(itx.token.value));
|
|
66
|
+
const totalDeposited = new _ocap_util.BN(reserve.totalDeposited);
|
|
67
|
+
if (nextWithdrawn.gt(totalDeposited)) return next(new _ocap_util_lib_error.CustomError("INVALID_TX", `Withdraw amount (${itx.token.value}) exceeds rollup net inflow reserve watermark: cumulativeWithdrawn (${nextWithdrawn.toString(10)}) > cumulativeDeposited (${totalDeposited.toString(10)})`));
|
|
68
|
+
}
|
|
55
69
|
return next();
|
|
56
70
|
});
|
|
57
71
|
runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
@@ -125,6 +139,7 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
|
|
|
125
139
|
stateKey: "senderState",
|
|
126
140
|
addressKey: "tx.from"
|
|
127
141
|
}));
|
|
142
|
+
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: ["senderState", "signerStates"] }));
|
|
128
143
|
runner.use((context, next) => {
|
|
129
144
|
const { itx, senderState } = context;
|
|
130
145
|
context.stakeAddress = (0, _arcblock_did_util.toStakeAddress)(senderState.address, itx.rollup);
|
|
@@ -179,7 +194,7 @@ runner.use(async (context, next) => {
|
|
|
179
194
|
address: itx.token.address,
|
|
180
195
|
value: fee
|
|
181
196
|
}], lockerState || { tokens: {} }, "add");
|
|
182
|
-
const [newSenderState, newStakeState, newLockerState] = await Promise.all([
|
|
197
|
+
const [newSenderState, newStakeState, newLockerState, newRollupState] = await Promise.all([
|
|
183
198
|
statedb.account.update(senderState.address, _ocap_state.account.update(senderState, {
|
|
184
199
|
nonce: tx.nonce,
|
|
185
200
|
...senderUpdates
|
|
@@ -201,10 +216,12 @@ runner.use(async (context, next) => {
|
|
|
201
216
|
message: "pending-block-reward",
|
|
202
217
|
revokeWaitingPeriod: 0,
|
|
203
218
|
...lockerUpdates
|
|
204
|
-
}, context), context)
|
|
219
|
+
}, context), context),
|
|
220
|
+
_ocap_state.rollup.hasRollupReserveTracking(context.rollupState) ? statedb.rollup.update(itx.rollup, _ocap_state.rollup.recordRollupWithdraw(context.rollupState, itx.token.address, itx.token.value, context), context) : Promise.resolve(context.rollupState)
|
|
205
221
|
]);
|
|
206
222
|
if (updateVaults) await updateVaults();
|
|
207
223
|
context.senderState = newSenderState;
|
|
224
|
+
context.rollupState = newRollupState;
|
|
208
225
|
context.stakeState = newStakeState;
|
|
209
226
|
context.lockerState = newLockerState;
|
|
210
227
|
context.stakeStates = [newStakeState, newLockerState];
|
|
@@ -8,10 +8,12 @@ let _ocap_state = require("@ocap/state");
|
|
|
8
8
|
let _ocap_tx_pipeline = require("@ocap/tx-pipeline");
|
|
9
9
|
let _ocap_util = require("@ocap/util");
|
|
10
10
|
let _ocap_util_lib_error = require("@ocap/util/lib/error");
|
|
11
|
+
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
12
|
+
let _ocap_util_lib_get_related_addr = require("@ocap/util/lib/get-related-addr");
|
|
11
13
|
let lodash_pick = require("lodash/pick");
|
|
12
14
|
lodash_pick = require_rolldown_runtime.__toESM(lodash_pick);
|
|
13
|
-
let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
|
|
14
15
|
let _arcblock_validator = require("@arcblock/validator");
|
|
16
|
+
let _ocap_util_lib_curve = require("@ocap/util/lib/curve");
|
|
15
17
|
|
|
16
18
|
//#region src/protocols/token-factory/burn.ts
|
|
17
19
|
const runner = new _ocap_tx_pipeline.Runner("burn_token");
|
|
@@ -36,6 +38,11 @@ runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
|
36
38
|
status: "INVALID_TOKEN_FACTORY",
|
|
37
39
|
table: "tokenFactory"
|
|
38
40
|
}));
|
|
41
|
+
runner.use((context, next) => {
|
|
42
|
+
const { tokenFactoryState } = context;
|
|
43
|
+
if (tokenFactoryState?.status && tokenFactoryState.status !== "ACTIVE") return next(new _ocap_util_lib_error.CustomError("INVALID_TOKEN_FACTORY", `Token factory is ${tokenFactoryState.status} and cannot burn tokens`));
|
|
44
|
+
return next();
|
|
45
|
+
});
|
|
39
46
|
runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
40
47
|
from: "tokenFactoryState.tokenAddress",
|
|
41
48
|
to: "tokenState",
|
|
@@ -96,7 +103,11 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyAccountMigration({
|
|
|
96
103
|
stateKey: "receiverState",
|
|
97
104
|
addressKey: "itx.receiver"
|
|
98
105
|
}));
|
|
99
|
-
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: [
|
|
106
|
+
runner.use(_ocap_tx_pipeline.pipes.VerifyBlocked({ stateKeys: [
|
|
107
|
+
"senderState",
|
|
108
|
+
"signerStates",
|
|
109
|
+
"receiverState"
|
|
110
|
+
] }));
|
|
100
111
|
runner.use(_ocap_tx_pipeline.pipes.ExtractState({
|
|
101
112
|
from: "tokenFactoryState.owner",
|
|
102
113
|
to: "ownerState",
|
|
@@ -109,6 +120,15 @@ runner.use(_ocap_tx_pipeline.pipes.verifyTokenAccess({
|
|
|
109
120
|
accountKeys: ["senderState"],
|
|
110
121
|
errorMessage: "Account {address} is not allowed to burn token {tokenAddress}"
|
|
111
122
|
}));
|
|
123
|
+
runner.use((context, next) => {
|
|
124
|
+
const { tokenFactoryState, tokenState, senderState, ownerState, tx } = context;
|
|
125
|
+
if (!tokenFactoryState?.curve && (!tokenState?.minters || tokenState.minters.length === 0)) {
|
|
126
|
+
const senderAddrs = senderState ? (0, _ocap_util_lib_get_related_addr.getRelatedAddresses)(senderState) : [tx.from];
|
|
127
|
+
const ownerAddrs = ownerState ? (0, _ocap_util_lib_get_related_addr.getRelatedAddresses)(ownerState) : [tokenFactoryState.owner];
|
|
128
|
+
if (!senderAddrs.some((s) => ownerAddrs.some((o) => (0, _ocap_util.isSameDid)(s, o)))) return next(new _ocap_util_lib_error.CustomError("FORBIDDEN", `verify-token-access: Account ${senderState?.address || tx.from} is not allowed to burn token ${tokenState.address}`, { persist: true }));
|
|
129
|
+
}
|
|
130
|
+
return next();
|
|
131
|
+
});
|
|
112
132
|
runner.use((context, next) => {
|
|
113
133
|
const { inputs = [], tokenFactoryState } = context;
|
|
114
134
|
let amount = new _ocap_util.BN("0");
|
|
@@ -128,6 +148,18 @@ runner.use(require_protocols_token_factory_pipes_calc_reserve.default({
|
|
|
128
148
|
amountKey: "burnAmount",
|
|
129
149
|
direction: "burn"
|
|
130
150
|
}), { shouldSkip: (ctx) => !ctx.tokenFactoryState?.curve });
|
|
151
|
+
runner.use((context, next) => {
|
|
152
|
+
const { tokenFactoryState, burnAmount = "0", reserveAmount = "0" } = context;
|
|
153
|
+
const reserveBalance = new _ocap_util.BN(tokenFactoryState.reserveBalance || "0");
|
|
154
|
+
if (new _ocap_util.BN(burnAmount).eq(new _ocap_util.BN(tokenFactoryState.currentSupply)) && new _ocap_util.BN(reserveAmount).gt(reserveBalance)) {
|
|
155
|
+
context.reserveAmount = reserveBalance.toString(10);
|
|
156
|
+
context.reserveFee = (0, _ocap_util_lib_curve.calcFee)({
|
|
157
|
+
reserveAmount: context.reserveAmount,
|
|
158
|
+
feeRate: String(tokenFactoryState.feeRate)
|
|
159
|
+
})?.toString();
|
|
160
|
+
}
|
|
161
|
+
return next();
|
|
162
|
+
}, { shouldSkip: (ctx) => !ctx.tokenFactoryState?.curve });
|
|
131
163
|
runner.use((context, next) => {
|
|
132
164
|
const { reserveAmount = "0", reserveFee } = context;
|
|
133
165
|
const { minReserve } = context.itx;
|
|
@@ -195,6 +227,7 @@ runner.use(async (context, next) => {
|
|
|
195
227
|
delta: new _ocap_util.BN(reserveAmount).sub(new _ocap_util.BN(reserveFee || "0")).toString()
|
|
196
228
|
});
|
|
197
229
|
if (senderChange) accountUpdates[senderChange.address] = require_util.applyTokenChange(accountUpdates[senderChange.address] || senderState, senderChange);
|
|
230
|
+
const isSameAddr = (a, b) => Boolean(a && b && (a === b || (0, _ocap_util.toAddress)(a) === (0, _ocap_util.toAddress)(b)));
|
|
198
231
|
const [newAccountStates, newTokenFactoryState, newTokenState] = await Promise.all([
|
|
199
232
|
Promise.all(Object.entries(accountUpdates).map(([address, updates]) => {
|
|
200
233
|
const state = [
|
|
@@ -202,13 +235,13 @@ runner.use(async (context, next) => {
|
|
|
202
235
|
receiverState,
|
|
203
236
|
ownerState,
|
|
204
237
|
...signerStates
|
|
205
|
-
].find((x) => x?.address
|
|
238
|
+
].find((x) => isSameAddr(x?.address, address));
|
|
206
239
|
const actualUpdates = state ? (0, lodash_pick.default)(updates, ["tokens"]) : {
|
|
207
240
|
...updates,
|
|
208
241
|
address
|
|
209
242
|
};
|
|
210
|
-
if (address
|
|
211
|
-
actualUpdates.pk = tx.pk;
|
|
243
|
+
if (isSameAddr(address, tx.from)) {
|
|
244
|
+
actualUpdates.pk = tx.delegator ? state?.pk || tx.pk : tx.pk;
|
|
212
245
|
actualUpdates.nonce = tx.nonce;
|
|
213
246
|
}
|
|
214
247
|
return statedb.account.updateOrCreate(state ?? null, _ocap_state.account.updateOrCreate(state ?? null, actualUpdates, context), context);
|
|
@@ -219,10 +252,10 @@ runner.use(async (context, next) => {
|
|
|
219
252
|
}, context), context),
|
|
220
253
|
statedb.token.update(tokenAddress, _ocap_state.token.update(tokenState, { totalSupply: new _ocap_util.BN(tokenState.totalSupply).sub(new _ocap_util.BN(burnAmount)).toString() }), context)
|
|
221
254
|
]);
|
|
222
|
-
context.senderState = newAccountStates.find((x) => x.address
|
|
223
|
-
context.receiverState = newAccountStates.find((x) => x.address
|
|
224
|
-
context.ownerState = newAccountStates.find((x) => x.address
|
|
225
|
-
context.signerStates = newAccountStates.filter((x) => signerStates.find((s) => s.address
|
|
255
|
+
context.senderState = newAccountStates.find((x) => isSameAddr(x.address, tx.from));
|
|
256
|
+
context.receiverState = newAccountStates.find((x) => isSameAddr(x.address, receiver));
|
|
257
|
+
context.ownerState = newAccountStates.find((x) => isSameAddr(x.address, ownerState.address)) || ownerState;
|
|
258
|
+
context.signerStates = newAccountStates.filter((x) => signerStates.find((s) => isSameAddr(s.address, x.address)));
|
|
226
259
|
context.tokenFactoryState = newTokenFactoryState;
|
|
227
260
|
context.tokenState = newTokenState;
|
|
228
261
|
context.inputChanges = inputChanges;
|