@ocap/tx-protocols 1.28.9 → 1.29.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/execute.d.mts +53 -0
- package/esm/execute.mjs +225 -0
- package/esm/index.d.mts +95 -0
- package/esm/index.mjs +100 -0
- package/esm/pipes/ensure-cost.d.mts +18 -0
- package/esm/pipes/ensure-cost.mjs +136 -0
- package/esm/pipes/ensure-gas.d.mts +14 -0
- package/esm/pipes/ensure-gas.mjs +38 -0
- package/esm/protocols/account/delegate.d.mts +4 -0
- package/esm/protocols/account/delegate.mjs +195 -0
- package/esm/protocols/account/migrate.d.mts +4 -0
- package/esm/protocols/account/migrate.mjs +130 -0
- package/esm/protocols/account/revoke-delegate.d.mts +4 -0
- package/esm/protocols/account/revoke-delegate.mjs +102 -0
- package/esm/protocols/asset/acquire-v2.d.mts +9 -0
- package/esm/protocols/asset/acquire-v2.mjs +213 -0
- package/esm/protocols/asset/acquire-v3.d.mts +17 -0
- package/esm/protocols/asset/acquire-v3.mjs +265 -0
- package/esm/protocols/asset/calls/transfer-token.d.mts +4 -0
- package/esm/protocols/asset/calls/transfer-token.mjs +36 -0
- package/esm/protocols/asset/calls/transfer.d.mts +4 -0
- package/esm/protocols/asset/calls/transfer.mjs +32 -0
- package/esm/protocols/asset/consume.d.mts +4 -0
- package/esm/protocols/asset/consume.mjs +92 -0
- package/esm/protocols/asset/create.d.mts +4 -0
- package/esm/protocols/asset/create.mjs +136 -0
- package/esm/protocols/asset/mint.d.mts +4 -0
- package/esm/protocols/asset/mint.mjs +156 -0
- package/esm/protocols/asset/pipes/exec-mint-hook.d.mts +24 -0
- package/esm/protocols/asset/pipes/exec-mint-hook.mjs +54 -0
- package/esm/protocols/asset/pipes/extract-factory-tokens.d.mts +21 -0
- package/esm/protocols/asset/pipes/extract-factory-tokens.mjs +22 -0
- package/esm/protocols/asset/pipes/verify-itx-address.d.mts +32 -0
- package/esm/protocols/asset/pipes/verify-itx-address.mjs +56 -0
- package/esm/protocols/asset/pipes/verify-itx-assets.d.mts +18 -0
- package/esm/protocols/asset/pipes/verify-itx-assets.mjs +27 -0
- package/esm/protocols/asset/pipes/verify-itx-variables.d.mts +17 -0
- package/esm/protocols/asset/pipes/verify-itx-variables.mjs +20 -0
- package/esm/protocols/asset/pipes/verify-mint-limit.d.mts +15 -0
- package/esm/protocols/asset/pipes/verify-mint-limit.mjs +14 -0
- package/esm/protocols/asset/update.d.mts +4 -0
- package/esm/protocols/asset/update.mjs +111 -0
- package/esm/protocols/factory/create.d.mts +29 -0
- package/esm/protocols/factory/create.mjs +155 -0
- package/esm/protocols/governance/claim-stake.d.mts +27 -0
- package/esm/protocols/governance/claim-stake.mjs +220 -0
- package/esm/protocols/governance/return-stake.d.mts +27 -0
- package/esm/protocols/governance/return-stake.mjs +211 -0
- package/esm/protocols/governance/revoke-stake.d.mts +27 -0
- package/esm/protocols/governance/revoke-stake.mjs +178 -0
- package/esm/protocols/governance/slash-stake.d.mts +17 -0
- package/esm/protocols/governance/slash-stake.mjs +213 -0
- package/esm/protocols/governance/stake.d.mts +15 -0
- package/esm/protocols/governance/stake.mjs +270 -0
- package/esm/protocols/rollup/claim-reward.d.mts +11 -0
- package/esm/protocols/rollup/claim-reward.mjs +322 -0
- package/esm/protocols/rollup/close.d.mts +4 -0
- package/esm/protocols/rollup/close.mjs +105 -0
- package/esm/protocols/rollup/create-block.d.mts +11 -0
- package/esm/protocols/rollup/create-block.mjs +303 -0
- package/esm/protocols/rollup/create.d.mts +4 -0
- package/esm/protocols/rollup/create.mjs +164 -0
- package/esm/protocols/rollup/join.d.mts +4 -0
- package/esm/protocols/rollup/join.mjs +152 -0
- package/esm/protocols/rollup/leave.d.mts +4 -0
- package/esm/protocols/rollup/leave.mjs +137 -0
- package/esm/protocols/rollup/migrate.d.mts +4 -0
- package/esm/protocols/rollup/migrate.mjs +85 -0
- package/esm/protocols/rollup/pause.d.mts +4 -0
- package/esm/protocols/rollup/pause.mjs +76 -0
- package/esm/protocols/rollup/pipes/ensure-validator.d.mts +6 -0
- package/esm/protocols/rollup/pipes/ensure-validator.mjs +12 -0
- package/esm/protocols/rollup/pipes/verify-evidence.d.mts +15 -0
- package/esm/protocols/rollup/pipes/verify-evidence.mjs +29 -0
- package/esm/protocols/rollup/pipes/verify-signers.d.mts +15 -0
- package/esm/protocols/rollup/pipes/verify-signers.mjs +36 -0
- package/esm/protocols/rollup/pipes/verify-status.d.mts +13 -0
- package/esm/protocols/rollup/pipes/verify-status.mjs +26 -0
- package/esm/protocols/rollup/resume.d.mts +4 -0
- package/esm/protocols/rollup/resume.mjs +79 -0
- package/esm/protocols/rollup/update.d.mts +4 -0
- package/esm/protocols/rollup/update.mjs +111 -0
- package/esm/protocols/token/create.d.mts +4 -0
- package/esm/protocols/token/create.mjs +150 -0
- package/esm/protocols/token/deposit-v2.d.mts +11 -0
- package/esm/protocols/token/deposit-v2.mjs +216 -0
- package/esm/protocols/token/withdraw-v2.d.mts +9 -0
- package/esm/protocols/token/withdraw-v2.mjs +222 -0
- package/esm/protocols/token-factory/burn.d.mts +15 -0
- package/esm/protocols/token-factory/burn.mjs +233 -0
- package/esm/protocols/token-factory/create.d.mts +4 -0
- package/esm/protocols/token-factory/create.mjs +254 -0
- package/esm/protocols/token-factory/mint.d.mts +15 -0
- package/esm/protocols/token-factory/mint.mjs +234 -0
- package/esm/protocols/token-factory/pipes/calc-reserve.d.mts +21 -0
- package/esm/protocols/token-factory/pipes/calc-reserve.mjs +34 -0
- package/esm/protocols/token-factory/pipes/verify-icon.d.mts +14 -0
- package/esm/protocols/token-factory/pipes/verify-icon.mjs +18 -0
- package/esm/protocols/token-factory/pipes/verify-ownership.d.mts +12 -0
- package/esm/protocols/token-factory/pipes/verify-ownership.mjs +63 -0
- package/esm/protocols/token-factory/pipes/verify-url.d.mts +12 -0
- package/esm/protocols/token-factory/pipes/verify-url.mjs +26 -0
- package/esm/protocols/token-factory/update.d.mts +10 -0
- package/esm/protocols/token-factory/update.mjs +152 -0
- package/esm/protocols/trade/exchange-v2.d.mts +9 -0
- package/esm/protocols/trade/exchange-v2.mjs +239 -0
- package/esm/protocols/trade/transfer-v2.d.mts +9 -0
- package/esm/protocols/trade/transfer-v2.mjs +226 -0
- package/esm/protocols/trade/transfer-v3.d.mts +17 -0
- package/esm/protocols/trade/transfer-v3.mjs +270 -0
- package/esm/util.d.mts +141 -0
- package/esm/util.mjs +278 -0
- package/lib/_virtual/rolldown_runtime.cjs +29 -0
- package/lib/execute.cjs +231 -0
- package/lib/execute.d.cts +53 -0
- package/lib/index.cjs +105 -0
- package/lib/index.d.cts +95 -0
- package/lib/pipes/ensure-cost.cjs +141 -0
- package/lib/pipes/ensure-cost.d.cts +18 -0
- package/lib/pipes/ensure-gas.cjs +41 -0
- package/lib/pipes/ensure-gas.d.cts +14 -0
- package/lib/protocols/account/delegate.cjs +201 -0
- package/lib/protocols/account/delegate.d.cts +4 -0
- package/lib/protocols/account/migrate.cjs +135 -0
- package/lib/protocols/account/migrate.d.cts +4 -0
- package/lib/protocols/account/revoke-delegate.cjs +107 -0
- package/lib/protocols/account/revoke-delegate.d.cts +4 -0
- package/lib/protocols/asset/acquire-v2.cjs +216 -0
- package/lib/protocols/asset/acquire-v2.d.cts +9 -0
- package/lib/protocols/asset/acquire-v3.cjs +269 -0
- package/lib/protocols/asset/acquire-v3.d.cts +17 -0
- package/lib/protocols/asset/calls/transfer-token.cjs +40 -0
- package/lib/protocols/asset/calls/transfer-token.d.cts +4 -0
- package/lib/protocols/asset/calls/transfer.cjs +35 -0
- package/lib/protocols/asset/calls/transfer.d.cts +4 -0
- package/lib/protocols/asset/consume.cjs +95 -0
- package/lib/protocols/asset/consume.d.cts +4 -0
- package/lib/protocols/asset/create.cjs +140 -0
- package/lib/protocols/asset/create.d.cts +4 -0
- package/lib/protocols/asset/mint.cjs +159 -0
- package/lib/protocols/asset/mint.d.cts +4 -0
- package/lib/protocols/asset/pipes/exec-mint-hook.cjs +57 -0
- package/lib/protocols/asset/pipes/exec-mint-hook.d.cts +24 -0
- package/lib/protocols/asset/pipes/extract-factory-tokens.cjs +25 -0
- package/lib/protocols/asset/pipes/extract-factory-tokens.d.cts +21 -0
- package/lib/protocols/asset/pipes/verify-itx-address.cjs +59 -0
- package/lib/protocols/asset/pipes/verify-itx-address.d.cts +32 -0
- package/lib/protocols/asset/pipes/verify-itx-assets.cjs +29 -0
- package/lib/protocols/asset/pipes/verify-itx-assets.d.cts +18 -0
- package/lib/protocols/asset/pipes/verify-itx-variables.cjs +22 -0
- package/lib/protocols/asset/pipes/verify-itx-variables.d.cts +17 -0
- package/lib/protocols/asset/pipes/verify-mint-limit.cjs +16 -0
- package/lib/protocols/asset/pipes/verify-mint-limit.d.cts +15 -0
- package/lib/protocols/asset/update.cjs +114 -0
- package/lib/protocols/asset/update.d.cts +4 -0
- package/lib/protocols/factory/create.cjs +161 -0
- package/lib/protocols/factory/create.d.cts +29 -0
- package/lib/protocols/governance/claim-stake.cjs +223 -0
- package/lib/protocols/governance/claim-stake.d.cts +27 -0
- package/lib/protocols/governance/return-stake.cjs +215 -0
- package/lib/protocols/governance/return-stake.d.cts +27 -0
- package/lib/protocols/governance/revoke-stake.cjs +182 -0
- package/lib/protocols/governance/revoke-stake.d.cts +27 -0
- package/lib/protocols/governance/slash-stake.cjs +217 -0
- package/lib/protocols/governance/slash-stake.d.cts +17 -0
- package/lib/protocols/governance/stake.cjs +275 -0
- package/lib/protocols/governance/stake.d.cts +15 -0
- package/lib/protocols/rollup/claim-reward.cjs +328 -0
- package/lib/protocols/rollup/claim-reward.d.cts +11 -0
- package/lib/protocols/rollup/close.cjs +107 -0
- package/lib/protocols/rollup/close.d.cts +4 -0
- package/lib/protocols/rollup/create-block.cjs +309 -0
- package/lib/protocols/rollup/create-block.d.cts +11 -0
- package/lib/protocols/rollup/create.cjs +169 -0
- package/lib/protocols/rollup/create.d.cts +4 -0
- package/lib/protocols/rollup/join.cjs +157 -0
- package/lib/protocols/rollup/join.d.cts +4 -0
- package/lib/protocols/rollup/leave.cjs +141 -0
- package/lib/protocols/rollup/leave.d.cts +4 -0
- package/lib/protocols/rollup/migrate.cjs +87 -0
- package/lib/protocols/rollup/migrate.d.cts +4 -0
- package/lib/protocols/rollup/pause.cjs +78 -0
- package/lib/protocols/rollup/pause.d.cts +4 -0
- package/lib/protocols/rollup/pipes/ensure-validator.cjs +14 -0
- package/lib/protocols/rollup/pipes/ensure-validator.d.cts +6 -0
- package/lib/protocols/rollup/pipes/verify-evidence.cjs +32 -0
- package/lib/protocols/rollup/pipes/verify-evidence.d.cts +15 -0
- package/lib/protocols/rollup/pipes/verify-signers.cjs +39 -0
- package/lib/protocols/rollup/pipes/verify-signers.d.cts +15 -0
- package/lib/protocols/rollup/pipes/verify-status.cjs +28 -0
- package/lib/protocols/rollup/pipes/verify-status.d.cts +13 -0
- package/lib/protocols/rollup/resume.cjs +81 -0
- package/lib/protocols/rollup/resume.d.cts +4 -0
- package/lib/protocols/rollup/update.cjs +114 -0
- package/lib/protocols/rollup/update.d.cts +4 -0
- package/lib/protocols/token/create.cjs +156 -0
- package/lib/protocols/token/create.d.cts +4 -0
- package/lib/protocols/token/deposit-v2.cjs +219 -0
- package/lib/protocols/token/deposit-v2.d.cts +11 -0
- package/lib/protocols/token/withdraw-v2.cjs +225 -0
- package/lib/protocols/token/withdraw-v2.d.cts +9 -0
- package/lib/protocols/token-factory/burn.cjs +236 -0
- package/lib/protocols/token-factory/burn.d.cts +15 -0
- package/lib/protocols/token-factory/create.cjs +260 -0
- package/lib/protocols/token-factory/create.d.cts +4 -0
- package/lib/protocols/token-factory/mint.cjs +237 -0
- package/lib/protocols/token-factory/mint.d.cts +15 -0
- package/lib/protocols/token-factory/pipes/calc-reserve.cjs +38 -0
- package/lib/protocols/token-factory/pipes/calc-reserve.d.cts +21 -0
- package/lib/protocols/token-factory/pipes/verify-icon.cjs +22 -0
- package/lib/protocols/token-factory/pipes/verify-icon.d.cts +14 -0
- package/lib/protocols/token-factory/pipes/verify-ownership.cjs +66 -0
- package/lib/protocols/token-factory/pipes/verify-ownership.d.cts +12 -0
- package/lib/protocols/token-factory/pipes/verify-url.cjs +29 -0
- package/lib/protocols/token-factory/pipes/verify-url.d.cts +12 -0
- package/lib/protocols/token-factory/update.cjs +155 -0
- package/lib/protocols/token-factory/update.d.cts +10 -0
- package/lib/protocols/trade/exchange-v2.cjs +243 -0
- package/lib/protocols/trade/exchange-v2.d.cts +9 -0
- package/lib/protocols/trade/transfer-v2.cjs +229 -0
- package/lib/protocols/trade/transfer-v2.d.cts +9 -0
- package/lib/protocols/trade/transfer-v3.cjs +274 -0
- package/lib/protocols/trade/transfer-v3.d.cts +17 -0
- package/lib/util.cjs +296 -0
- package/lib/util.d.cts +141 -0
- package/package.json +49 -22
- package/tools/fixtures.ts +564 -0
- package/lib/execute.js +0 -254
- package/lib/index.js +0 -117
- package/lib/pipes/ensure-cost.js +0 -193
- package/lib/pipes/ensure-gas.js +0 -48
- package/lib/protocols/account/delegate.js +0 -223
- package/lib/protocols/account/migrate.js +0 -153
- package/lib/protocols/account/revoke-delegate.js +0 -110
- package/lib/protocols/asset/acquire-v2.js +0 -262
- package/lib/protocols/asset/acquire-v3.js +0 -330
- package/lib/protocols/asset/calls/README.md +0 -5
- package/lib/protocols/asset/calls/transfer-token.js +0 -36
- package/lib/protocols/asset/calls/transfer.js +0 -28
- package/lib/protocols/asset/consume.js +0 -105
- package/lib/protocols/asset/create.js +0 -151
- package/lib/protocols/asset/mint.js +0 -199
- package/lib/protocols/asset/pipes/exec-mint-hook.js +0 -62
- package/lib/protocols/asset/pipes/extract-factory-tokens.js +0 -18
- package/lib/protocols/asset/pipes/verify-itx-address.js +0 -54
- package/lib/protocols/asset/pipes/verify-itx-assets.js +0 -51
- package/lib/protocols/asset/pipes/verify-itx-variables.js +0 -26
- package/lib/protocols/asset/pipes/verify-mint-limit.js +0 -13
- package/lib/protocols/asset/update.js +0 -131
- package/lib/protocols/factory/create.js +0 -191
- package/lib/protocols/governance/claim-stake.js +0 -266
- package/lib/protocols/governance/return-stake.js +0 -248
- package/lib/protocols/governance/revoke-stake.js +0 -172
- package/lib/protocols/governance/slash-stake.js +0 -271
- package/lib/protocols/governance/stake.js +0 -303
- package/lib/protocols/rollup/claim-reward.js +0 -342
- package/lib/protocols/rollup/close.js +0 -104
- package/lib/protocols/rollup/create-block.js +0 -413
- package/lib/protocols/rollup/create.js +0 -197
- package/lib/protocols/rollup/join.js +0 -182
- package/lib/protocols/rollup/leave.js +0 -145
- package/lib/protocols/rollup/migrate.js +0 -85
- package/lib/protocols/rollup/pause.js +0 -75
- package/lib/protocols/rollup/pipes/ensure-validator.js +0 -12
- package/lib/protocols/rollup/pipes/verify-evidence.js +0 -37
- package/lib/protocols/rollup/pipes/verify-signers.js +0 -87
- package/lib/protocols/rollup/pipes/verify-status.js +0 -30
- package/lib/protocols/rollup/resume.js +0 -75
- package/lib/protocols/rollup/update.js +0 -122
- package/lib/protocols/token/create.js +0 -199
- package/lib/protocols/token/deposit-v2.js +0 -290
- package/lib/protocols/token/withdraw-v2.js +0 -305
- package/lib/protocols/token-factory/burn.js +0 -371
- package/lib/protocols/token-factory/create.js +0 -342
- package/lib/protocols/token-factory/mint.js +0 -385
- package/lib/protocols/token-factory/pipes/calc-reserve.js +0 -37
- package/lib/protocols/token-factory/pipes/verify-icon.js +0 -27
- package/lib/protocols/token-factory/pipes/verify-ownership.js +0 -93
- package/lib/protocols/token-factory/pipes/verify-url.js +0 -32
- package/lib/protocols/token-factory/update.js +0 -208
- package/lib/protocols/trade/exchange-v2.js +0 -239
- package/lib/protocols/trade/transfer-v2.js +0 -233
- package/lib/protocols/trade/transfer-v3.js +0 -333
- package/lib/util.js +0 -442
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { applyTokenChange, applyTokenUpdates } from "../../util.mjs";
|
|
2
|
+
import ensure_cost_default from "../../pipes/ensure-cost.mjs";
|
|
3
|
+
import ensure_gas_default from "../../pipes/ensure-gas.mjs";
|
|
4
|
+
import calc_reserve_default from "./pipes/calc-reserve.mjs";
|
|
5
|
+
import { Runner, pipes } from "@ocap/tx-pipeline";
|
|
6
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
7
|
+
import pick from "lodash/pick.js";
|
|
8
|
+
import { account, token, tokenFactory } from "@ocap/state";
|
|
9
|
+
import { BN, fromUnitToToken } from "@ocap/util";
|
|
10
|
+
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
11
|
+
import { Joi, schemas } from "@arcblock/validator";
|
|
12
|
+
|
|
13
|
+
//#region src/protocols/token-factory/mint.ts
|
|
14
|
+
const runner = new Runner("mint_token");
|
|
15
|
+
const schema = Joi.object({
|
|
16
|
+
tokenFactory: Joi.DID().prefix().role("ROLE_TOKEN_FACTORY").required(),
|
|
17
|
+
receiver: schemas.tokenHolder.required(),
|
|
18
|
+
amount: Joi.BN().greater(0).required(),
|
|
19
|
+
data: Joi.any().optional().allow(null),
|
|
20
|
+
inputsList: schemas.multiInput.optional()
|
|
21
|
+
}).options({
|
|
22
|
+
stripUnknown: true,
|
|
23
|
+
noDefaults: false
|
|
24
|
+
});
|
|
25
|
+
runner.use(({ itx }, next) => {
|
|
26
|
+
const { error } = schema.validate(itx);
|
|
27
|
+
if (error) return next(new CustomError("INVALID_TX", `Invalid itx: ${error.message}`));
|
|
28
|
+
return next();
|
|
29
|
+
});
|
|
30
|
+
runner.use(pipes.ExtractState({
|
|
31
|
+
from: "itx.tokenFactory",
|
|
32
|
+
to: "tokenFactoryState",
|
|
33
|
+
status: "INVALID_TOKEN_FACTORY",
|
|
34
|
+
table: "tokenFactory"
|
|
35
|
+
}));
|
|
36
|
+
runner.use(pipes.ExtractState({
|
|
37
|
+
from: "tokenFactoryState.tokenAddress",
|
|
38
|
+
to: "tokenState",
|
|
39
|
+
status: "INVALID_TOKEN",
|
|
40
|
+
table: "token"
|
|
41
|
+
}));
|
|
42
|
+
runner.use(pipes.VerifyTxInput({
|
|
43
|
+
fieldKey: "itx.inputs",
|
|
44
|
+
inputsKey: "inputs",
|
|
45
|
+
sendersKey: "senders",
|
|
46
|
+
tokensKey: "tokens",
|
|
47
|
+
assetsKey: null
|
|
48
|
+
}), { shouldSkip: (ctx) => !ctx.tokenFactoryState?.curve });
|
|
49
|
+
runner.use(pipes.VerifyListSize({ listKey: [
|
|
50
|
+
"inputs",
|
|
51
|
+
"senders",
|
|
52
|
+
"tokens",
|
|
53
|
+
"assets"
|
|
54
|
+
] }), { shouldSkip: (ctx) => !ctx.tokenFactoryState?.curve });
|
|
55
|
+
runner.use(pipes.VerifyMultiSigV2({ signersKey: "senders" }), { shouldSkip: (ctx) => !ctx.tokenFactoryState?.curve });
|
|
56
|
+
runner.use((context, next) => {
|
|
57
|
+
const { tokenFactoryState, inputs = [] } = context;
|
|
58
|
+
if (!inputs.every((input) => {
|
|
59
|
+
const tokensList = getListField(input, "tokens");
|
|
60
|
+
const assetsList = getListField(input, "assets");
|
|
61
|
+
return tokensList.length && tokensList.every((x) => x.address === tokenFactoryState.reserveAddress) && !assetsList.length;
|
|
62
|
+
})) return next(new CustomError("INVALID_TX", `Inputs only accept ${tokenFactoryState.reserveAddress}`));
|
|
63
|
+
return next();
|
|
64
|
+
}, { shouldSkip: (ctx) => !ctx.tokenFactoryState?.curve });
|
|
65
|
+
runner.use(pipes.ExtractState({
|
|
66
|
+
from: "tx.from",
|
|
67
|
+
to: "senderState",
|
|
68
|
+
status: "OK",
|
|
69
|
+
table: "account"
|
|
70
|
+
}));
|
|
71
|
+
runner.use(pipes.ExtractState({
|
|
72
|
+
from: "senders",
|
|
73
|
+
to: "signerStates",
|
|
74
|
+
status: "INVALID_SIGNER_STATE",
|
|
75
|
+
table: "account"
|
|
76
|
+
}), { shouldSkip: (ctx) => !ctx.tokenFactoryState?.curve });
|
|
77
|
+
runner.use(pipes.VerifyAccountMigration({
|
|
78
|
+
signerKey: "signerStates",
|
|
79
|
+
stateKey: "senderState",
|
|
80
|
+
addressKey: "tx.from"
|
|
81
|
+
}), { shouldSkip: (ctx) => !ctx.tokenFactoryState?.curve });
|
|
82
|
+
runner.use(pipes.ExtractState({
|
|
83
|
+
from: "itx.receiver",
|
|
84
|
+
to: "receiverState",
|
|
85
|
+
status: "OK",
|
|
86
|
+
table: "account"
|
|
87
|
+
}));
|
|
88
|
+
runner.use(pipes.VerifyAccountMigration({
|
|
89
|
+
stateKey: "receiverState",
|
|
90
|
+
addressKey: "itx.receiver"
|
|
91
|
+
}));
|
|
92
|
+
runner.use(pipes.VerifyBlocked({ stateKeys: ["signerStates", "receiverState"] }));
|
|
93
|
+
runner.use(pipes.ExtractState({
|
|
94
|
+
from: "tokenFactoryState.owner",
|
|
95
|
+
to: "ownerState",
|
|
96
|
+
status: "INVALID_OWNER_STATE",
|
|
97
|
+
table: "account"
|
|
98
|
+
}));
|
|
99
|
+
runner.use(pipes.verifyTokenAccess({
|
|
100
|
+
statesKey: "tokenState",
|
|
101
|
+
listFieldKey: "minters",
|
|
102
|
+
accountKeys: ["senderState"],
|
|
103
|
+
errorMessage: "Account {address} is not allowed to mint token {tokenAddress}"
|
|
104
|
+
}));
|
|
105
|
+
runner.use(calc_reserve_default({
|
|
106
|
+
tokenFactoryKey: "tokenFactoryState",
|
|
107
|
+
tokenStateKey: "tokenState",
|
|
108
|
+
reserveKey: "reserveAmount",
|
|
109
|
+
amountKey: "itx.amount"
|
|
110
|
+
}), { shouldSkip: (ctx) => !ctx.tokenFactoryState?.curve });
|
|
111
|
+
runner.use((context, next) => {
|
|
112
|
+
const { tokenState, itx } = context;
|
|
113
|
+
if (tokenState.maxTotalSupply) {
|
|
114
|
+
const availableSupply = new BN(tokenState.maxTotalSupply).sub(new BN(tokenState.totalSupply));
|
|
115
|
+
if (new BN(itx.amount).gt(availableSupply)) return next(new CustomError("INVALID_TOKEN", `Maximum mintable tokens are ${fromUnitToToken(availableSupply, tokenState.decimal)} ${tokenState.symbol}`));
|
|
116
|
+
}
|
|
117
|
+
next();
|
|
118
|
+
});
|
|
119
|
+
runner.use((context, next) => {
|
|
120
|
+
const { reserveAmount, reserveFee, inputs = [], tokenFactoryState } = context;
|
|
121
|
+
const maxReserve = inputs.reduce((total, input) => {
|
|
122
|
+
const reserveToken = getListField(input, "tokens").find((x) => x.address === tokenFactoryState.reserveAddress);
|
|
123
|
+
if (reserveToken) return total.add(new BN(reserveToken.value));
|
|
124
|
+
return total;
|
|
125
|
+
}, new BN(0));
|
|
126
|
+
const actual = new BN(reserveAmount ?? "0").add(new BN(reserveFee || "0"));
|
|
127
|
+
if (actual.gt(maxReserve)) return next(new CustomError("SLIPPAGE_EXCEEDED", `Mint token failed due to price movement. Expected maximum: ${fromUnitToToken(maxReserve)}, actual: ${fromUnitToToken(actual)}. Try increasing your inputs.`));
|
|
128
|
+
next();
|
|
129
|
+
}, { shouldSkip: (ctx) => !ctx.tokenFactoryState?.curve });
|
|
130
|
+
runner.use(pipes.ExtractState({
|
|
131
|
+
from: "tokens",
|
|
132
|
+
to: "tokenStates",
|
|
133
|
+
status: "INVALID_TOKEN",
|
|
134
|
+
table: "token"
|
|
135
|
+
}));
|
|
136
|
+
runner.use(pipes.VerifyTokenBalance({
|
|
137
|
+
ownerKey: "signerStates",
|
|
138
|
+
conditionKey: "inputs"
|
|
139
|
+
}), { shouldSkip: (ctx) => !ctx.tokenFactoryState?.curve });
|
|
140
|
+
runner.use(ensure_gas_default((context) => {
|
|
141
|
+
const result = {
|
|
142
|
+
create: 0,
|
|
143
|
+
update: 2,
|
|
144
|
+
payment: 0
|
|
145
|
+
};
|
|
146
|
+
if (context.senderState) result.update += 1;
|
|
147
|
+
else result.create += 1;
|
|
148
|
+
if (context.receiverState) result.update += 1;
|
|
149
|
+
else result.create += 1;
|
|
150
|
+
if (context.reserveFee) result.update += 1;
|
|
151
|
+
if (context.signerStates) result.update += context.signerStates.length;
|
|
152
|
+
return result;
|
|
153
|
+
}));
|
|
154
|
+
runner.use(ensure_cost_default({ attachSenderChanges: true }));
|
|
155
|
+
runner.use(pipes.TakeStateSnapshot());
|
|
156
|
+
runner.use(async (context, next) => {
|
|
157
|
+
const { tx: tx$1, senderState, receiverState, ownerState, statedb, senderChange, updateVaults, tokenFactoryState, tokenState, reserveAmount, reserveFee, itx } = context;
|
|
158
|
+
const { amount, receiver } = itx;
|
|
159
|
+
const { reserveAddress, tokenAddress } = tokenFactoryState;
|
|
160
|
+
const signerStates = tokenFactoryState.curve ? context.signerStates || [] : [];
|
|
161
|
+
const inputs = tokenFactoryState.curve ? context.inputs || [] : [];
|
|
162
|
+
const totalReserveCost = new BN(reserveAmount || "0").add(new BN(reserveFee || "0"));
|
|
163
|
+
let currentReserveCost = new BN("0");
|
|
164
|
+
const accountUpdates = {};
|
|
165
|
+
const inputChanges = [];
|
|
166
|
+
inputs.forEach((input) => {
|
|
167
|
+
const { owner } = input;
|
|
168
|
+
const reserveTokenChange = getListField(input, "tokens").find((item) => item.address === reserveAddress);
|
|
169
|
+
if (!reserveTokenChange) return;
|
|
170
|
+
let reserveValue = new BN(reserveTokenChange.value);
|
|
171
|
+
if (currentReserveCost.add(reserveValue).gt(totalReserveCost)) {
|
|
172
|
+
reserveTokenChange.value = totalReserveCost.sub(currentReserveCost).toString();
|
|
173
|
+
reserveValue = new BN(reserveTokenChange.value);
|
|
174
|
+
}
|
|
175
|
+
currentReserveCost = currentReserveCost.add(reserveValue);
|
|
176
|
+
accountUpdates[owner] = applyTokenUpdates([reserveTokenChange], signerStates.find((s) => s.address === owner), "sub");
|
|
177
|
+
inputChanges.push({
|
|
178
|
+
address: owner,
|
|
179
|
+
token: reserveAddress,
|
|
180
|
+
delta: reserveValue.neg().toString()
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
if (!accountUpdates[tx$1.from]) accountUpdates[tx$1.from] = senderState || {};
|
|
184
|
+
if (reserveFee && new BN(reserveFee).gt(new BN(0))) accountUpdates[ownerState.address] = applyTokenChange(accountUpdates[ownerState.address] || ownerState, {
|
|
185
|
+
token: reserveAddress,
|
|
186
|
+
delta: reserveFee
|
|
187
|
+
});
|
|
188
|
+
accountUpdates[receiver] = applyTokenChange(accountUpdates[receiver] || receiverState || {
|
|
189
|
+
address: receiver,
|
|
190
|
+
tokens: {}
|
|
191
|
+
}, {
|
|
192
|
+
token: tokenAddress,
|
|
193
|
+
delta: String(amount)
|
|
194
|
+
});
|
|
195
|
+
if (senderChange) accountUpdates[senderChange.address] = applyTokenChange(accountUpdates[senderChange.address] || senderState, senderChange);
|
|
196
|
+
const [newAccountStates, newTokenFactoryState, newTokenState] = await Promise.all([
|
|
197
|
+
Promise.all(Object.entries(accountUpdates).map(([address, updates]) => {
|
|
198
|
+
const state = [
|
|
199
|
+
senderState,
|
|
200
|
+
receiverState,
|
|
201
|
+
ownerState,
|
|
202
|
+
...signerStates
|
|
203
|
+
].find((x) => x?.address === address);
|
|
204
|
+
const actualUpdates = state ? pick(updates, ["tokens"]) : {
|
|
205
|
+
...updates,
|
|
206
|
+
address
|
|
207
|
+
};
|
|
208
|
+
if (address === tx$1.from) {
|
|
209
|
+
actualUpdates.pk = tx$1.pk;
|
|
210
|
+
actualUpdates.nonce = tx$1.nonce;
|
|
211
|
+
}
|
|
212
|
+
return statedb.account.updateOrCreate(state ?? null, account.updateOrCreate(state ?? null, actualUpdates, context), context);
|
|
213
|
+
})),
|
|
214
|
+
statedb.tokenFactory.update(tokenFactoryState.address, tokenFactory.update(tokenFactoryState, {
|
|
215
|
+
currentSupply: new BN(tokenFactoryState.currentSupply).add(new BN(amount)).toString(),
|
|
216
|
+
reserveBalance: new BN(tokenFactoryState.reserveBalance).add(new BN(reserveAmount || "0")).toString()
|
|
217
|
+
}, context), context),
|
|
218
|
+
statedb.token.update(tokenAddress, token.update(tokenState, { totalSupply: new BN(tokenState.totalSupply).add(new BN(amount)).toString() }), context)
|
|
219
|
+
]);
|
|
220
|
+
context.senderState = newAccountStates.find((x) => x.address === tx$1.from);
|
|
221
|
+
context.receiverState = newAccountStates.find((x) => x.address === receiver);
|
|
222
|
+
context.ownerState = newAccountStates.find((x) => x.address === ownerState.address) || ownerState;
|
|
223
|
+
context.signerStates = newAccountStates.filter((x) => signerStates.find((s) => s.address === x.address));
|
|
224
|
+
context.tokenFactoryState = newTokenFactoryState;
|
|
225
|
+
context.tokenState = newTokenState;
|
|
226
|
+
context.inputChanges = inputChanges;
|
|
227
|
+
if (updateVaults) await updateVaults();
|
|
228
|
+
next();
|
|
229
|
+
}, { persistError: true });
|
|
230
|
+
runner.use(pipes.VerifyStateDiff());
|
|
231
|
+
var mint_default = runner;
|
|
232
|
+
|
|
233
|
+
//#endregion
|
|
234
|
+
export { mint_default as default };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { PipeFunction } from "@ocap/tx-pipeline";
|
|
2
|
+
|
|
3
|
+
//#region src/protocols/token-factory/pipes/calc-reserve.d.ts
|
|
4
|
+
interface ICalcReserveOptions {
|
|
5
|
+
tokenFactoryKey?: string;
|
|
6
|
+
tokenStateKey?: string;
|
|
7
|
+
reserveKey?: string;
|
|
8
|
+
feeKey?: string;
|
|
9
|
+
amountKey?: string;
|
|
10
|
+
direction?: 'mint' | 'burn';
|
|
11
|
+
}
|
|
12
|
+
declare const calcReserve: ({
|
|
13
|
+
tokenFactoryKey,
|
|
14
|
+
tokenStateKey,
|
|
15
|
+
reserveKey,
|
|
16
|
+
feeKey,
|
|
17
|
+
amountKey,
|
|
18
|
+
direction
|
|
19
|
+
}?: ICalcReserveOptions) => PipeFunction<any>;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { calcReserve as default };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
2
|
+
import get from "lodash/get.js";
|
|
3
|
+
import { calcCost, calcFee } from "@ocap/util/lib/curve";
|
|
4
|
+
import set from "lodash/set.js";
|
|
5
|
+
|
|
6
|
+
//#region src/protocols/token-factory/pipes/calc-reserve.ts
|
|
7
|
+
const calcReserve = ({ tokenFactoryKey = "tokenFactoryState", tokenStateKey = "tokenState", reserveKey = "reserveAmount", feeKey = "reserveFee", amountKey = "itx.amount", direction = "mint" } = {}) => (context, next) => {
|
|
8
|
+
const tokenFactoryState = get(context, tokenFactoryKey);
|
|
9
|
+
const tokenState = get(context, tokenStateKey);
|
|
10
|
+
const amount = get(context, amountKey);
|
|
11
|
+
if (!tokenFactoryState) return next(new CustomError("INVALID_TOKEN_FACTORY", "Token factory state not found"));
|
|
12
|
+
if (!tokenState) return next(new CustomError("INVALID_TOKEN", "Token state not found"));
|
|
13
|
+
const factoryState = tokenFactoryState;
|
|
14
|
+
const tokenStateTyped = tokenState;
|
|
15
|
+
const { curve, currentSupply, feeRate } = factoryState;
|
|
16
|
+
const { decimal } = tokenStateTyped;
|
|
17
|
+
const reserveAmount = calcCost({
|
|
18
|
+
amount,
|
|
19
|
+
decimal,
|
|
20
|
+
currentSupply,
|
|
21
|
+
direction,
|
|
22
|
+
curve
|
|
23
|
+
});
|
|
24
|
+
set(context, reserveKey, reserveAmount?.toString());
|
|
25
|
+
set(context, feeKey, calcFee({
|
|
26
|
+
reserveAmount: reserveAmount?.toString() || "0",
|
|
27
|
+
feeRate: String(feeRate)
|
|
28
|
+
})?.toString());
|
|
29
|
+
return next();
|
|
30
|
+
};
|
|
31
|
+
var calc_reserve_default = calcReserve;
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
export { calc_reserve_default as default };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PipeFunction } from "@ocap/tx-pipeline";
|
|
2
|
+
import { IDecodedContext } from "@ocap/types";
|
|
3
|
+
|
|
4
|
+
//#region src/protocols/token-factory/pipes/verify-icon.d.ts
|
|
5
|
+
interface IVerifyIconOptions {
|
|
6
|
+
iconKey?: string;
|
|
7
|
+
size?: number;
|
|
8
|
+
}
|
|
9
|
+
declare const verifyIcon: ({
|
|
10
|
+
iconKey,
|
|
11
|
+
size
|
|
12
|
+
}?: IVerifyIconOptions) => PipeFunction<IDecodedContext>;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { verifyIcon as default };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
2
|
+
import get from "lodash/get.js";
|
|
3
|
+
import set from "lodash/set.js";
|
|
4
|
+
import { isSvgFile, sanitizeSvg } from "@blocklet/xss";
|
|
5
|
+
|
|
6
|
+
//#region src/protocols/token-factory/pipes/verify-icon.ts
|
|
7
|
+
const verifyIcon = ({ iconKey = "itx.token.icon", size = 16 * 1024 } = {}) => (context, next) => {
|
|
8
|
+
const icon = get(context, iconKey);
|
|
9
|
+
if (!icon) return next();
|
|
10
|
+
if (!isSvgFile(icon)) return next(new CustomError("INVALID_ICON", "Token Icon is not a valid SVG file"));
|
|
11
|
+
if (Buffer.from(icon, "utf8").length > size) return next(new CustomError("INVALID_ICON", `Token Icon must be less than ${Math.floor(size / 1024)}k`));
|
|
12
|
+
set(context, iconKey, sanitizeSvg(icon, { preserveCase: true }));
|
|
13
|
+
return next();
|
|
14
|
+
};
|
|
15
|
+
var verify_icon_default = verifyIcon;
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { verify_icon_default as default };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PipeFunction } from "@ocap/tx-pipeline";
|
|
2
|
+
import { IDecodedContext } from "@ocap/types";
|
|
3
|
+
|
|
4
|
+
//#region src/protocols/token-factory/pipes/verify-ownership.d.ts
|
|
5
|
+
interface IVerifyOwnershipOptions {
|
|
6
|
+
tokenKey?: string;
|
|
7
|
+
}
|
|
8
|
+
declare const verifyOwnership: ({
|
|
9
|
+
tokenKey
|
|
10
|
+
}?: IVerifyOwnershipOptions) => PipeFunction<IDecodedContext>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { verifyOwnership as default };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
2
|
+
import get from "lodash/get.js";
|
|
3
|
+
import { verify } from "@arcblock/vc";
|
|
4
|
+
import { verifyUrl } from "@ocap/util/lib/url";
|
|
5
|
+
|
|
6
|
+
//#region src/protocols/token-factory/pipes/verify-ownership.ts
|
|
7
|
+
const isTest = ["development", "test"].includes(process.env.NODE_ENV) || !!process.env.CI;
|
|
8
|
+
const verifyOwnership = ({ tokenKey = "itx.token" } = {}) => async (context, next) => {
|
|
9
|
+
const { symbol, address, website } = get(context, tokenKey) || {};
|
|
10
|
+
if (!website) return next();
|
|
11
|
+
try {
|
|
12
|
+
await verifyUrl(website, isTest ? {
|
|
13
|
+
protocols: ["http:", "https:"],
|
|
14
|
+
blockedHosts: [],
|
|
15
|
+
allowIp: true
|
|
16
|
+
} : { checkDns: true });
|
|
17
|
+
} catch (error) {
|
|
18
|
+
return next(new CustomError("INVALID_WEBSITE", `Website ${website} must be a valid URL: ${error.message}`));
|
|
19
|
+
}
|
|
20
|
+
const { from, delegator } = context.tx;
|
|
21
|
+
const chainId = context.config.chainId;
|
|
22
|
+
const sender = delegator || from;
|
|
23
|
+
const { origin } = new URL(website);
|
|
24
|
+
const verificationUrl = `${origin}/.well-known/ocap/tokens.json`;
|
|
25
|
+
try {
|
|
26
|
+
const response = await fetch(verificationUrl, { redirect: "manual" });
|
|
27
|
+
if (!response || !response.ok) {
|
|
28
|
+
context.logger?.info("Website verification failed", {
|
|
29
|
+
symbol,
|
|
30
|
+
website,
|
|
31
|
+
status: response?.status,
|
|
32
|
+
statusText: response?.statusText
|
|
33
|
+
});
|
|
34
|
+
return next(new CustomError("INVALID_WEBSITE", `Website ${website} verification failed`));
|
|
35
|
+
}
|
|
36
|
+
const data = await response.json();
|
|
37
|
+
if ((await Promise.all(data.filter(Boolean).filter((item) => {
|
|
38
|
+
if (item.type !== "TokenIssueCredential") return false;
|
|
39
|
+
if (item.issuer?.id !== sender) return false;
|
|
40
|
+
const { id, issued } = item.credentialSubject || {};
|
|
41
|
+
return id === sender && issued?.address === address && issued?.symbol === symbol && issued?.chainId === chainId && issued?.website === website;
|
|
42
|
+
}).map((item) => {
|
|
43
|
+
return verify({
|
|
44
|
+
vc: item,
|
|
45
|
+
ownerDid: sender,
|
|
46
|
+
trustedIssuers: sender
|
|
47
|
+
});
|
|
48
|
+
}))).some((valid) => valid)) return next();
|
|
49
|
+
context.logger?.warn("Website verification failed", {
|
|
50
|
+
address,
|
|
51
|
+
symbol,
|
|
52
|
+
website,
|
|
53
|
+
data
|
|
54
|
+
});
|
|
55
|
+
return next(new CustomError("INVALID_WEBSITE", `Website ${website} verification failed`));
|
|
56
|
+
} catch (error) {
|
|
57
|
+
return next(new CustomError("INVALID_WEBSITE", `Website ${website} verification failed: ${error.message}`));
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
var verify_ownership_default = verifyOwnership;
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
export { verify_ownership_default as default };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PipeFunction } from "@ocap/tx-pipeline";
|
|
2
|
+
import { IDecodedContext } from "@ocap/types";
|
|
3
|
+
|
|
4
|
+
//#region src/protocols/token-factory/pipes/verify-url.d.ts
|
|
5
|
+
interface IVerifyUrlOptions {
|
|
6
|
+
urlKeys?: string[];
|
|
7
|
+
}
|
|
8
|
+
declare const verifyUrlPipe: ({
|
|
9
|
+
urlKeys
|
|
10
|
+
}?: IVerifyUrlOptions) => PipeFunction<IDecodedContext>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { verifyUrlPipe as default };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
2
|
+
import get from "lodash/get.js";
|
|
3
|
+
import { verifyUrl } from "@ocap/util/lib/url";
|
|
4
|
+
|
|
5
|
+
//#region src/protocols/token-factory/pipes/verify-url.ts
|
|
6
|
+
const isTest = ["development", "test"].includes(process.env.NODE_ENV) || !!process.env.CI;
|
|
7
|
+
const verifyUrlPipe = ({ urlKeys = ["itx.token.website", "itx.token.metadata.value.communityUrl"] } = {}) => async (context, next) => {
|
|
8
|
+
for (const key of urlKeys) {
|
|
9
|
+
const url = get(context, key);
|
|
10
|
+
if (!url) continue;
|
|
11
|
+
try {
|
|
12
|
+
await verifyUrl(url, isTest ? {
|
|
13
|
+
protocols: ["http:", "https:"],
|
|
14
|
+
blockedHosts: [],
|
|
15
|
+
allowIp: true
|
|
16
|
+
} : {});
|
|
17
|
+
} catch (error) {
|
|
18
|
+
return next(new CustomError("INVALID_TX", `${key.split(".").pop()} is not valid: ${error.message}`));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return next();
|
|
22
|
+
};
|
|
23
|
+
var verify_url_default = verifyUrlPipe;
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { verify_url_default as default };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/protocols/token-factory/update.d.ts
|
|
2
|
+
/** Token info for website verification */
|
|
3
|
+
interface TokenInfoForVerification {
|
|
4
|
+
symbol: string;
|
|
5
|
+
address: string;
|
|
6
|
+
website?: string | null;
|
|
7
|
+
}
|
|
8
|
+
declare const runner: any;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { TokenInfoForVerification, runner as default };
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { applyTokenChange, decodeAnySafe, getDelegationRequirements } from "../../util.mjs";
|
|
2
|
+
import ensure_cost_default from "../../pipes/ensure-cost.mjs";
|
|
3
|
+
import ensure_gas_default from "../../pipes/ensure-gas.mjs";
|
|
4
|
+
import verify_icon_default from "./pipes/verify-icon.mjs";
|
|
5
|
+
import verify_ownership_default from "./pipes/verify-ownership.mjs";
|
|
6
|
+
import verify_url_default from "./pipes/verify-url.mjs";
|
|
7
|
+
import { Runner, pipes } from "@ocap/tx-pipeline";
|
|
8
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
9
|
+
import { account, delegation, token, tokenFactory } from "@ocap/state";
|
|
10
|
+
import Debug from "debug";
|
|
11
|
+
import { Joi } from "@arcblock/validator";
|
|
12
|
+
|
|
13
|
+
//#region src/protocols/token-factory/update.ts
|
|
14
|
+
const debug = Debug("@ocap/tx-protocols:update-token-factory");
|
|
15
|
+
const runner = new Runner("update_token_factory");
|
|
16
|
+
const isTest = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test" || process.env.CI;
|
|
17
|
+
runner.use(pipes.VerifyMultiSig(0));
|
|
18
|
+
const schema = Joi.object({
|
|
19
|
+
address: Joi.DID().prefix().role("ROLE_TOKEN_FACTORY").required(),
|
|
20
|
+
feeRate: Joi.number().min(0).max(2e3).optional().allow(null),
|
|
21
|
+
token: Joi.object({
|
|
22
|
+
icon: Joi.string().optional().allow(null, ""),
|
|
23
|
+
website: Joi.string().uri({
|
|
24
|
+
scheme: isTest ? ["http", "https"] : ["https"],
|
|
25
|
+
allowRelative: false
|
|
26
|
+
}).max(256).optional().allow(null, ""),
|
|
27
|
+
metadata: Joi.any().optional().allow(null)
|
|
28
|
+
}).optional().allow(null),
|
|
29
|
+
data: Joi.any().optional().allow(null)
|
|
30
|
+
}).options({
|
|
31
|
+
stripUnknown: true,
|
|
32
|
+
noDefaults: false
|
|
33
|
+
});
|
|
34
|
+
runner.use((context, next) => {
|
|
35
|
+
const { error } = schema.validate(context.itx);
|
|
36
|
+
if (error) return next(new CustomError("INVALID_TX", `Invalid itx: ${error.message}`));
|
|
37
|
+
if (context.itx.token?.metadata) {
|
|
38
|
+
const metadata = decodeAnySafe(context.itx.token.metadata ?? null);
|
|
39
|
+
if (metadata) {
|
|
40
|
+
const { error: metadataError, value: metadataValue } = token.metadataSchema.validate(metadata.value);
|
|
41
|
+
if (metadataError) return next(new CustomError("INVALID_TX", `Invalid metadata: ${metadataError.message}`));
|
|
42
|
+
context.itx.token.metadata = {
|
|
43
|
+
...metadata,
|
|
44
|
+
value: metadataValue
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return next();
|
|
49
|
+
});
|
|
50
|
+
runner.use(verify_icon_default({ iconKey: "itx.token.icon" }));
|
|
51
|
+
runner.use(verify_url_default({ urlKeys: ["itx.token.website", "itx.token.metadata.value.communityUrl"] }));
|
|
52
|
+
runner.use(pipes.ExtractState({
|
|
53
|
+
from: "itx.address",
|
|
54
|
+
to: "tokenFactoryState",
|
|
55
|
+
table: "tokenFactory",
|
|
56
|
+
status: "INVALID_TOKEN_FACTORY"
|
|
57
|
+
}));
|
|
58
|
+
runner.use(pipes.ExtractState({
|
|
59
|
+
from: "tokenFactoryState.tokenAddress",
|
|
60
|
+
to: "tokenState",
|
|
61
|
+
table: "token",
|
|
62
|
+
status: "INVALID_TOKEN"
|
|
63
|
+
}));
|
|
64
|
+
runner.use((context, next) => {
|
|
65
|
+
const { tokenState, itx } = context;
|
|
66
|
+
context.tokenInfoForVerification = {
|
|
67
|
+
symbol: tokenState.symbol,
|
|
68
|
+
address: tokenState.address,
|
|
69
|
+
website: itx.token?.website ?? null
|
|
70
|
+
};
|
|
71
|
+
return next();
|
|
72
|
+
});
|
|
73
|
+
runner.use(verify_ownership_default({ tokenKey: "tokenInfoForVerification" }));
|
|
74
|
+
runner.use(pipes.ExtractState({
|
|
75
|
+
from: "tx.from",
|
|
76
|
+
to: "senderState",
|
|
77
|
+
status: "INVALID_SENDER_STATE",
|
|
78
|
+
table: "account"
|
|
79
|
+
}));
|
|
80
|
+
runner.use(pipes.VerifyAccountMigration({
|
|
81
|
+
stateKey: "senderState",
|
|
82
|
+
addressKey: "tx.from"
|
|
83
|
+
}));
|
|
84
|
+
runner.use(pipes.VerifyBlocked({ stateKeys: ["senderState"] }));
|
|
85
|
+
runner.use(pipes.ExtractState({
|
|
86
|
+
from: "tx.delegator",
|
|
87
|
+
to: "delegatorState",
|
|
88
|
+
status: "OK",
|
|
89
|
+
table: "account"
|
|
90
|
+
}));
|
|
91
|
+
runner.use(pipes.VerifyAccountMigration({
|
|
92
|
+
stateKey: "delegatorState",
|
|
93
|
+
addressKey: "tx.delegator"
|
|
94
|
+
}));
|
|
95
|
+
runner.use(pipes.VerifyDelegation({
|
|
96
|
+
type: "signature",
|
|
97
|
+
signerKey: "senderState",
|
|
98
|
+
delegatorKey: "delegatorState",
|
|
99
|
+
getRequirements: getDelegationRequirements
|
|
100
|
+
}));
|
|
101
|
+
runner.use((context, next) => {
|
|
102
|
+
const { tokenFactoryState, delegatorState, senderState } = context;
|
|
103
|
+
if ((delegatorState ? delegatorState.address : senderState.address) !== tokenFactoryState.owner) return next(new CustomError("FORBIDDEN", "Token factory can only be updated by owner"));
|
|
104
|
+
return next();
|
|
105
|
+
});
|
|
106
|
+
runner.use(ensure_gas_default((context) => {
|
|
107
|
+
const result = {
|
|
108
|
+
create: 0,
|
|
109
|
+
update: 2,
|
|
110
|
+
payment: 0
|
|
111
|
+
};
|
|
112
|
+
if (context.isDelegationChanged) result.update += 1;
|
|
113
|
+
return result;
|
|
114
|
+
}));
|
|
115
|
+
runner.use(ensure_cost_default({ attachSenderChanges: true }));
|
|
116
|
+
runner.use(pipes.TakeStateSnapshot());
|
|
117
|
+
runner.use(async (context, next) => {
|
|
118
|
+
const { tx: tx$1, itx, statedb, senderState, tokenFactoryState, tokenState, senderChange, updateVaults, delegationState } = context;
|
|
119
|
+
const senderStateValue = senderState;
|
|
120
|
+
const tokenFactoryStateValue = tokenFactoryState;
|
|
121
|
+
const tokenStateValue = tokenState;
|
|
122
|
+
const { tokens: senderTokens = {} } = senderStateValue;
|
|
123
|
+
const senderUpdates = senderChange ? applyTokenChange({ tokens: senderTokens }, senderChange) : { tokens: senderTokens };
|
|
124
|
+
const factoryUpdates = itx.feeRate ? { feeRate: itx.feeRate } : {};
|
|
125
|
+
const tokenUpdates = itx.token ? {
|
|
126
|
+
icon: itx.token?.icon || tokenStateValue.icon,
|
|
127
|
+
website: itx.token?.website || tokenStateValue.website,
|
|
128
|
+
metadata: itx.token?.metadata || tokenStateValue.metadata
|
|
129
|
+
} : null;
|
|
130
|
+
const [newSenderState, newTokenFactoryState, newTokenState, newDelegationState] = await Promise.all([
|
|
131
|
+
statedb.account.update(senderStateValue.address, account.update(senderStateValue, {
|
|
132
|
+
nonce: tx$1.nonce,
|
|
133
|
+
pk: tx$1.pk,
|
|
134
|
+
...senderUpdates
|
|
135
|
+
}, context), context),
|
|
136
|
+
statedb.tokenFactory.update(tokenFactoryStateValue.address, tokenFactory.update(tokenFactoryStateValue, factoryUpdates, context), context),
|
|
137
|
+
tokenUpdates ? statedb.token.update(tokenStateValue.address, token.update(tokenStateValue, tokenUpdates), context) : tokenStateValue,
|
|
138
|
+
context.isDelegationChanged && delegationState ? statedb.delegation.update(delegationState.address, delegation.update(delegationState, {}, context), context) : delegationState
|
|
139
|
+
]);
|
|
140
|
+
if (updateVaults) await updateVaults();
|
|
141
|
+
context.senderState = newSenderState;
|
|
142
|
+
context.tokenFactoryState = newTokenFactoryState;
|
|
143
|
+
context.tokenState = newTokenState;
|
|
144
|
+
context.delegationState = newDelegationState;
|
|
145
|
+
debug("update token factory", newTokenFactoryState, newTokenState);
|
|
146
|
+
next();
|
|
147
|
+
}, { persistError: true });
|
|
148
|
+
runner.use(pipes.VerifyStateDiff());
|
|
149
|
+
var update_default = runner;
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
export { update_default as default };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/protocols/trade/exchange-v2.d.ts
|
|
2
|
+
/** Token input - value can be string or number from decodeBigInt */
|
|
3
|
+
interface TokenInput {
|
|
4
|
+
address: string;
|
|
5
|
+
value: string | number;
|
|
6
|
+
}
|
|
7
|
+
declare const runner: any;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { TokenInput, runner as default };
|