@ocap/tx-protocols 1.28.9 → 1.29.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 +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,239 @@
|
|
|
1
|
+
import { applyTokenChange } 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 { Runner, pipes } from "@ocap/tx-pipeline";
|
|
5
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
6
|
+
import { account } from "@ocap/state";
|
|
7
|
+
import { BN } from "@ocap/util";
|
|
8
|
+
import Debug from "debug";
|
|
9
|
+
import { decodeBigInt } from "@ocap/message";
|
|
10
|
+
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
11
|
+
import { Joi, schemas } from "@arcblock/validator";
|
|
12
|
+
import { createSortedList } from "@ocap/util/lib/create-sorted-list";
|
|
13
|
+
import isEmpty from "lodash/isEmpty.js";
|
|
14
|
+
|
|
15
|
+
//#region src/protocols/trade/exchange-v2.ts
|
|
16
|
+
const debug = Debug("@ocap/tx-protocols:exchange-v2");
|
|
17
|
+
const runner = new Runner("exchange_v2");
|
|
18
|
+
const exchangeInfoSchema = Joi.object({
|
|
19
|
+
value: Joi.any().optional().allow(null),
|
|
20
|
+
tokensList: Joi.array().items(schemas.tokenInput).default([]),
|
|
21
|
+
assetsList: Joi.array().items(Joi.DID().prefix().role("ROLE_ASSET")).default([])
|
|
22
|
+
});
|
|
23
|
+
const schema = Joi.object({
|
|
24
|
+
to: schemas.tokenHolder.required(),
|
|
25
|
+
sender: exchangeInfoSchema.required(),
|
|
26
|
+
receiver: exchangeInfoSchema.required(),
|
|
27
|
+
data: Joi.any().optional().allow(null)
|
|
28
|
+
}).options({
|
|
29
|
+
stripUnknown: true,
|
|
30
|
+
noDefaults: false
|
|
31
|
+
});
|
|
32
|
+
runner.use(({ itx }, next) => {
|
|
33
|
+
const { error } = schema.validate(itx);
|
|
34
|
+
return next(error ? new CustomError("INVALID_TX", `Invalid itx: ${error.message}`) : void 0);
|
|
35
|
+
});
|
|
36
|
+
runner.use(pipes.VerifyMultiSig(1));
|
|
37
|
+
runner.use(pipes.ExtractReceiver({ from: [
|
|
38
|
+
"tx.signaturesList",
|
|
39
|
+
"tx.signatures",
|
|
40
|
+
"itx.to"
|
|
41
|
+
] }));
|
|
42
|
+
runner.use(pipes.VerifyInfo([{
|
|
43
|
+
error: "INVALID_TX",
|
|
44
|
+
message: "Can not exchange without any token or assets",
|
|
45
|
+
fn: (ctx) => {
|
|
46
|
+
const context = ctx;
|
|
47
|
+
const zero = new BN(0);
|
|
48
|
+
const { itx, config } = context;
|
|
49
|
+
context.senderAssets = getListField(itx, "sender.assets");
|
|
50
|
+
context.receiverAssets = getListField(itx, "receiver.assets");
|
|
51
|
+
const senderTokensLocal = getListField(itx, "sender.tokens");
|
|
52
|
+
const receiverTokensLocal = getListField(itx, "receiver.tokens");
|
|
53
|
+
const senderValue = itx.sender?.value;
|
|
54
|
+
const senderAmount = senderValue ? decodeBigInt(senderValue) : 0;
|
|
55
|
+
if (new BN(senderAmount).gt(zero)) {
|
|
56
|
+
senderTokensLocal.push({
|
|
57
|
+
address: config.token.address,
|
|
58
|
+
value: senderAmount
|
|
59
|
+
});
|
|
60
|
+
itx.sender.value = "0";
|
|
61
|
+
}
|
|
62
|
+
const receiverValue = itx.receiver?.value;
|
|
63
|
+
const receiverAmount = receiverValue ? decodeBigInt(receiverValue) : 0;
|
|
64
|
+
if (new BN(receiverAmount).gt(zero)) {
|
|
65
|
+
receiverTokensLocal.push({
|
|
66
|
+
address: config.token.address,
|
|
67
|
+
value: receiverAmount
|
|
68
|
+
});
|
|
69
|
+
itx.receiver.value = "0";
|
|
70
|
+
}
|
|
71
|
+
context.senderTokens = senderTokensLocal;
|
|
72
|
+
context.receiverTokens = receiverTokensLocal;
|
|
73
|
+
context.tokenAddress = createSortedList((context.senderTokens || []).concat(context.receiverTokens || []).map((x) => x.address));
|
|
74
|
+
if (isEmpty(context.senderAssets) && isEmpty(context.senderTokens)) return false;
|
|
75
|
+
if (isEmpty(context.receiverAssets) && isEmpty(context.receiverTokens)) return false;
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
}]));
|
|
79
|
+
runner.use(pipes.VerifySigner({ signer: "itx.to" }));
|
|
80
|
+
runner.use(pipes.VerifyListSize({ listKey: ["senderAssets", "receiverAssets"] }));
|
|
81
|
+
runner.use(pipes.VerifyListSize({ listKey: ["senderTokens", "receiverTokens"] }));
|
|
82
|
+
runner.use(pipes.ExtractState({
|
|
83
|
+
from: "tx.from",
|
|
84
|
+
to: "senderState",
|
|
85
|
+
status: "INVALID_SENDER_STATE",
|
|
86
|
+
table: "account"
|
|
87
|
+
}));
|
|
88
|
+
runner.use(pipes.ExtractState({
|
|
89
|
+
from: "tx.delegator",
|
|
90
|
+
to: "delegatorState",
|
|
91
|
+
status: "OK",
|
|
92
|
+
table: "account"
|
|
93
|
+
}));
|
|
94
|
+
runner.use(pipes.VerifyDelegation({
|
|
95
|
+
type: "signature",
|
|
96
|
+
signerKey: "senderState",
|
|
97
|
+
delegatorKey: "delegatorState"
|
|
98
|
+
}));
|
|
99
|
+
runner.use(pipes.ExtractSigner({
|
|
100
|
+
signerKey: "signerStates",
|
|
101
|
+
delegatorKey: "delegatorStates"
|
|
102
|
+
}));
|
|
103
|
+
runner.use(pipes.VerifyDelegation({
|
|
104
|
+
type: "multisig",
|
|
105
|
+
signerKey: "signerStates",
|
|
106
|
+
delegatorKey: "delegatorStates",
|
|
107
|
+
delegationKey: "delegationStates",
|
|
108
|
+
getRequirements: () => []
|
|
109
|
+
}));
|
|
110
|
+
runner.use(pipes.ExtractState({
|
|
111
|
+
from: "tokenAddress",
|
|
112
|
+
to: "tokenStates",
|
|
113
|
+
status: "INVALID_TOKEN",
|
|
114
|
+
table: "token"
|
|
115
|
+
}));
|
|
116
|
+
runner.use(pipes.ExtractState({
|
|
117
|
+
from: "receiver",
|
|
118
|
+
to: "receiverState",
|
|
119
|
+
status: "INVALID_RECEIVER_STATE",
|
|
120
|
+
table: "account"
|
|
121
|
+
}));
|
|
122
|
+
runner.use(pipes.VerifyAccountMigration({
|
|
123
|
+
signerKey: "signerStates",
|
|
124
|
+
stateKey: "senderState",
|
|
125
|
+
addressKey: "tx.from"
|
|
126
|
+
}));
|
|
127
|
+
runner.use((context, next) => {
|
|
128
|
+
context.senderTokenConditions = {
|
|
129
|
+
owner: context.senderState.address,
|
|
130
|
+
tokens: context.senderTokens || []
|
|
131
|
+
};
|
|
132
|
+
context.receiverTokenConditions = {
|
|
133
|
+
owner: context.receiverState.address,
|
|
134
|
+
tokens: context.receiverTokens || []
|
|
135
|
+
};
|
|
136
|
+
next();
|
|
137
|
+
});
|
|
138
|
+
runner.use(pipes.VerifyTokenBalance({
|
|
139
|
+
ownerKey: "senderState",
|
|
140
|
+
conditionKey: "senderTokenConditions"
|
|
141
|
+
}));
|
|
142
|
+
runner.use(pipes.VerifyTokenBalance({
|
|
143
|
+
ownerKey: "receiverState",
|
|
144
|
+
conditionKey: "receiverTokenConditions"
|
|
145
|
+
}));
|
|
146
|
+
runner.use(pipes.AntiLandAttack({
|
|
147
|
+
senderState: "senderState",
|
|
148
|
+
receiverState: "receiverState"
|
|
149
|
+
}));
|
|
150
|
+
runner.use(pipes.VerifyBlocked({ stateKeys: ["senderState", "receiverState"] }));
|
|
151
|
+
runner.use(pipes.verifyTokenAccess({
|
|
152
|
+
statesKey: "tokenStates",
|
|
153
|
+
listFieldKey: "spenders",
|
|
154
|
+
accountKeys: ["senderState"],
|
|
155
|
+
errorMessage: "Account {address} is not allowed to exchange token {tokenAddress}"
|
|
156
|
+
}));
|
|
157
|
+
runner.use(pipes.ExtractState({
|
|
158
|
+
from: "senderAssets",
|
|
159
|
+
to: "priv.senderAssets",
|
|
160
|
+
status: "INVALID_ASSET",
|
|
161
|
+
table: "asset"
|
|
162
|
+
}));
|
|
163
|
+
runner.use(pipes.VerifyTransferrable({ assets: "priv.senderAssets" }));
|
|
164
|
+
runner.use(pipes.VerifyUpdater({
|
|
165
|
+
assetKey: "priv.senderAssets",
|
|
166
|
+
ownerKey: "senderState"
|
|
167
|
+
}));
|
|
168
|
+
runner.use(pipes.ExtractState({
|
|
169
|
+
from: "receiverAssets",
|
|
170
|
+
to: "priv.receiverAssets",
|
|
171
|
+
status: "INVALID_ASSET",
|
|
172
|
+
table: "asset"
|
|
173
|
+
}));
|
|
174
|
+
runner.use(pipes.VerifyTransferrable({ assets: "priv.receiverAssets" }));
|
|
175
|
+
runner.use(pipes.VerifyUpdater({
|
|
176
|
+
assetKey: "priv.receiverAssets",
|
|
177
|
+
ownerKey: "receiverState"
|
|
178
|
+
}));
|
|
179
|
+
runner.use(ensure_gas_default((context) => {
|
|
180
|
+
const ops = {
|
|
181
|
+
create: 0,
|
|
182
|
+
update: 3,
|
|
183
|
+
payment: 0
|
|
184
|
+
};
|
|
185
|
+
ops.update += (context.senderAssets || []).length;
|
|
186
|
+
ops.update += (context.receiverAssets || []).length;
|
|
187
|
+
return ops;
|
|
188
|
+
}));
|
|
189
|
+
runner.use(ensure_cost_default({ attachSenderChanges: true }));
|
|
190
|
+
runner.use(pipes.UpdateOwner({
|
|
191
|
+
assets: "priv.senderAssets",
|
|
192
|
+
owner: "receiverState"
|
|
193
|
+
}));
|
|
194
|
+
runner.use(pipes.UpdateOwner({
|
|
195
|
+
assets: "priv.receiverAssets",
|
|
196
|
+
owner: "senderState"
|
|
197
|
+
}));
|
|
198
|
+
runner.use(pipes.TakeStateSnapshot());
|
|
199
|
+
runner.use(async (context, next) => {
|
|
200
|
+
const { tx: tx$1, itx, senderAssets = [], receiverAssets = [], senderTokens = [], receiverTokens = [], senderState, receiverState, senderChange, updateVaults, statedb } = context;
|
|
201
|
+
const senderStateTokens = senderState?.tokens || {};
|
|
202
|
+
const receiverStateTokens = receiverState?.tokens || {};
|
|
203
|
+
for (const { address, value } of senderTokens) {
|
|
204
|
+
const delta = new BN(value);
|
|
205
|
+
senderStateTokens[address] = new BN(senderStateTokens[address]).sub(delta).toString();
|
|
206
|
+
receiverStateTokens[address] = new BN(receiverStateTokens[address] || "0").add(delta).toString();
|
|
207
|
+
}
|
|
208
|
+
for (const { address, value } of receiverTokens) {
|
|
209
|
+
const delta = new BN(value);
|
|
210
|
+
senderStateTokens[address] = new BN(senderStateTokens[address]).add(delta).toString();
|
|
211
|
+
receiverStateTokens[address] = new BN(receiverStateTokens[address] || "0").sub(delta).toString();
|
|
212
|
+
}
|
|
213
|
+
const senderUpdates = senderChange ? applyTokenChange({ tokens: senderStateTokens }, senderChange) : { tokens: senderStateTokens };
|
|
214
|
+
const [newSenderState, newReceiverState] = await Promise.all([statedb.account.update(senderState.address, account.update(senderState, {
|
|
215
|
+
nonce: tx$1.nonce,
|
|
216
|
+
...senderUpdates
|
|
217
|
+
}, context), context), statedb.account.update(receiverState.address, account.update(receiverState, { tokens: receiverStateTokens }, context), context)]);
|
|
218
|
+
if (updateVaults) await updateVaults();
|
|
219
|
+
context.senderState = newSenderState;
|
|
220
|
+
context.receiverState = newReceiverState;
|
|
221
|
+
debug("exchange", {
|
|
222
|
+
from: tx$1.from,
|
|
223
|
+
to: itx.to,
|
|
224
|
+
sender: {
|
|
225
|
+
assets: senderAssets.length,
|
|
226
|
+
token: senderTokens.length
|
|
227
|
+
},
|
|
228
|
+
receiver: {
|
|
229
|
+
assets: receiverAssets.length,
|
|
230
|
+
token: receiverTokens.length
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
next();
|
|
234
|
+
}, { persistError: true });
|
|
235
|
+
runner.use(pipes.VerifyStateDiff());
|
|
236
|
+
var exchange_v2_default = runner;
|
|
237
|
+
|
|
238
|
+
//#endregion
|
|
239
|
+
export { exchange_v2_default as default };
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { applyTokenChange } 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 { Runner, pipes } from "@ocap/tx-pipeline";
|
|
5
|
+
import { CustomError } from "@ocap/util/lib/error";
|
|
6
|
+
import { toStakeAddress } from "@arcblock/did-util";
|
|
7
|
+
import { account, delegation } from "@ocap/state";
|
|
8
|
+
import { BN } from "@ocap/util";
|
|
9
|
+
import { decodeBigInt } from "@ocap/message";
|
|
10
|
+
import { getListField } from "@ocap/util/lib/get-list-field";
|
|
11
|
+
import { Joi, schemas } from "@arcblock/validator";
|
|
12
|
+
import isEmpty from "lodash/isEmpty.js";
|
|
13
|
+
|
|
14
|
+
//#region src/protocols/trade/transfer-v2.ts
|
|
15
|
+
const runner = new Runner("transfer_v2");
|
|
16
|
+
runner.use(pipes.VerifyMultiSig(0));
|
|
17
|
+
const schema = Joi.object({
|
|
18
|
+
to: schemas.tokenHolder.required(),
|
|
19
|
+
value: Joi.any().optional().allow(null),
|
|
20
|
+
tokensList: Joi.array().items(schemas.tokenInput).default([]),
|
|
21
|
+
assetsList: Joi.array().items(Joi.DID().prefix().role("ROLE_ASSET")).default([]),
|
|
22
|
+
data: Joi.any().optional().allow(null)
|
|
23
|
+
}).options({
|
|
24
|
+
stripUnknown: true,
|
|
25
|
+
noDefaults: false
|
|
26
|
+
});
|
|
27
|
+
runner.use((context, next) => {
|
|
28
|
+
const { error } = schema.validate(context.itx);
|
|
29
|
+
if (error) return next(new CustomError("INVALID_TX", `Invalid itx: ${error.message}`));
|
|
30
|
+
return next();
|
|
31
|
+
});
|
|
32
|
+
runner.use((context, next) => {
|
|
33
|
+
context.assets = getListField(context, "itx.assets");
|
|
34
|
+
context.tokens = getListField(context, "itx.tokens");
|
|
35
|
+
context.tokenAddress = (context.tokens || []).map((x) => x.address);
|
|
36
|
+
next();
|
|
37
|
+
});
|
|
38
|
+
runner.use(pipes.VerifyInfo([{
|
|
39
|
+
error: "INSUFFICIENT_DATA",
|
|
40
|
+
message: "Can not transfer without any primary token or assets or secondary tokens",
|
|
41
|
+
fn: (ctx) => {
|
|
42
|
+
const { itx, assets, tokens } = ctx;
|
|
43
|
+
if (itx.to && (itx.value || isEmpty(assets) === false || isEmpty(tokens) === false)) return true;
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}, {
|
|
47
|
+
error: "INVALID_TX",
|
|
48
|
+
message: "Can not transfer primary token smaller than 0",
|
|
49
|
+
fn: (ctx) => {
|
|
50
|
+
const { itx, assets, tokens } = ctx;
|
|
51
|
+
if (isEmpty(tokens) && isEmpty(assets)) {
|
|
52
|
+
if (new BN(itx.value ? decodeBigInt(itx.value) : 0).lte(new BN(0))) return false;
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
}]));
|
|
57
|
+
runner.use(pipes.VerifyListSize({ listKey: ["assets", "tokens"] }));
|
|
58
|
+
runner.use((context, next) => {
|
|
59
|
+
if (context.itx.value) {
|
|
60
|
+
const amount = new BN(decodeBigInt(context.itx.value));
|
|
61
|
+
if (amount.gt(new BN(0))) {
|
|
62
|
+
const tokens = context.tokens || [];
|
|
63
|
+
tokens.push({
|
|
64
|
+
address: context.config.token.address,
|
|
65
|
+
value: amount.toString()
|
|
66
|
+
});
|
|
67
|
+
context.tokens = tokens;
|
|
68
|
+
context.itx.value = void 0;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
next();
|
|
72
|
+
});
|
|
73
|
+
runner.use(ensure_gas_default((context) => {
|
|
74
|
+
const result = {
|
|
75
|
+
create: 0,
|
|
76
|
+
update: 2,
|
|
77
|
+
payment: 0
|
|
78
|
+
};
|
|
79
|
+
result.update += context.assetStates?.length || 0;
|
|
80
|
+
if (context.receiverState) result.update += 1;
|
|
81
|
+
else result.create += 1;
|
|
82
|
+
return result;
|
|
83
|
+
}));
|
|
84
|
+
runner.use(async (context, next) => {
|
|
85
|
+
const { statedb, tx: tx$1, gasVault } = context;
|
|
86
|
+
const accounts = [
|
|
87
|
+
tx$1.from,
|
|
88
|
+
context.receiver,
|
|
89
|
+
gasVault
|
|
90
|
+
].filter(Boolean);
|
|
91
|
+
await Promise.all([...accounts.map((x) => statedb.account.get(x, context)), statedb.stake.get(toStakeAddress(tx$1.from, tx$1.from), context)]);
|
|
92
|
+
next();
|
|
93
|
+
});
|
|
94
|
+
runner.use(pipes.ExtractState({
|
|
95
|
+
from: "tx.from",
|
|
96
|
+
to: "senderState",
|
|
97
|
+
status: "INVALID_SENDER_STATE",
|
|
98
|
+
table: "account"
|
|
99
|
+
}));
|
|
100
|
+
runner.use(pipes.VerifyAccountMigration({
|
|
101
|
+
stateKey: "senderState",
|
|
102
|
+
addressKey: "tx.from"
|
|
103
|
+
}));
|
|
104
|
+
runner.use(pipes.ExtractState({
|
|
105
|
+
from: "tokenAddress",
|
|
106
|
+
to: "tokenStates",
|
|
107
|
+
status: "INVALID_TOKEN",
|
|
108
|
+
table: "token"
|
|
109
|
+
}));
|
|
110
|
+
runner.use((context, next) => {
|
|
111
|
+
context.tokenConditions = {
|
|
112
|
+
owner: context.senderState.address,
|
|
113
|
+
tokens: context.tokens || []
|
|
114
|
+
};
|
|
115
|
+
next();
|
|
116
|
+
});
|
|
117
|
+
runner.use(pipes.VerifyTokenBalance({
|
|
118
|
+
ownerKey: "senderState",
|
|
119
|
+
conditionKey: "tokenConditions"
|
|
120
|
+
}));
|
|
121
|
+
runner.use(pipes.ExtractState({
|
|
122
|
+
from: "tx.delegator",
|
|
123
|
+
to: "delegatorState",
|
|
124
|
+
status: "OK",
|
|
125
|
+
table: "account"
|
|
126
|
+
}));
|
|
127
|
+
runner.use(pipes.VerifyAccountMigration({
|
|
128
|
+
stateKey: "delegatorState",
|
|
129
|
+
addressKey: "tx.delegator"
|
|
130
|
+
}));
|
|
131
|
+
runner.use(pipes.VerifyDelegation({
|
|
132
|
+
type: "signature",
|
|
133
|
+
signerKey: "senderState",
|
|
134
|
+
delegatorKey: "delegatorState",
|
|
135
|
+
getRequirements: (ctx) => {
|
|
136
|
+
const context = ctx;
|
|
137
|
+
const tokens = context.tokens || [];
|
|
138
|
+
const assets = context.assets || [];
|
|
139
|
+
return [...tokens.map((x) => ({
|
|
140
|
+
type: "token",
|
|
141
|
+
address: x.address,
|
|
142
|
+
value: x.value,
|
|
143
|
+
to: context.itx.to
|
|
144
|
+
})), ...assets.map((x) => ({
|
|
145
|
+
type: "asset",
|
|
146
|
+
address: x,
|
|
147
|
+
to: context.itx.to
|
|
148
|
+
}))];
|
|
149
|
+
}
|
|
150
|
+
}));
|
|
151
|
+
runner.use(pipes.ExtractReceiver({
|
|
152
|
+
from: "itx.to",
|
|
153
|
+
to: "receiver"
|
|
154
|
+
}));
|
|
155
|
+
runner.use(pipes.ExtractState({
|
|
156
|
+
from: "receiver",
|
|
157
|
+
to: "receiverState",
|
|
158
|
+
status: "OK",
|
|
159
|
+
table: "account"
|
|
160
|
+
}));
|
|
161
|
+
runner.use(pipes.AntiLandAttack({
|
|
162
|
+
senderState: "senderState",
|
|
163
|
+
receiverState: "receiverState"
|
|
164
|
+
}));
|
|
165
|
+
runner.use(pipes.VerifyBlocked({ stateKeys: ["senderState", "receiverState"] }));
|
|
166
|
+
runner.use(pipes.verifyTokenAccess({
|
|
167
|
+
statesKey: "tokenStates",
|
|
168
|
+
listFieldKey: "spenders",
|
|
169
|
+
accountKeys: ["senderState"],
|
|
170
|
+
errorMessage: "Account {address} is not allowed to transfer token {tokenAddress}"
|
|
171
|
+
}));
|
|
172
|
+
runner.use(pipes.ExtractState({
|
|
173
|
+
from: "assets",
|
|
174
|
+
to: "assetStates",
|
|
175
|
+
status: "INVALID_ASSET",
|
|
176
|
+
table: "asset"
|
|
177
|
+
}));
|
|
178
|
+
runner.use(pipes.VerifyTransferrable({ assets: "assetStates" }));
|
|
179
|
+
runner.use(pipes.VerifyUpdater({
|
|
180
|
+
assetKey: "assetStates",
|
|
181
|
+
ownerKey: "senderState"
|
|
182
|
+
}));
|
|
183
|
+
runner.use(ensure_cost_default({ attachSenderChanges: true }));
|
|
184
|
+
runner.use((context, next) => {
|
|
185
|
+
const { itx, receiverState } = context;
|
|
186
|
+
context.receiverAddr = receiverState ? receiverState.address : itx.to;
|
|
187
|
+
return next();
|
|
188
|
+
});
|
|
189
|
+
runner.use(pipes.UpdateOwner({
|
|
190
|
+
assets: "assetStates",
|
|
191
|
+
owner: "receiverAddr"
|
|
192
|
+
}));
|
|
193
|
+
runner.use(pipes.TakeStateSnapshot());
|
|
194
|
+
runner.use(async (context, next) => {
|
|
195
|
+
const { tx: tx$1, tokens, senderState, receiverAddr, receiverState, statedb, senderChange, delegationState, updateVaults } = context;
|
|
196
|
+
const senderTokens = senderState?.tokens || {};
|
|
197
|
+
const receiverTokens = receiverState?.tokens || {};
|
|
198
|
+
for (const token$1 of tokens || []) {
|
|
199
|
+
const delta = new BN(token$1.value);
|
|
200
|
+
senderTokens[token$1.address] = new BN(senderTokens[token$1.address]).sub(delta).toString();
|
|
201
|
+
receiverTokens[token$1.address] = new BN(receiverTokens[token$1.address] || "0").add(delta).toString();
|
|
202
|
+
}
|
|
203
|
+
const senderUpdates = senderChange ? applyTokenChange({ tokens: senderTokens }, senderChange) : { tokens: senderTokens };
|
|
204
|
+
const [newSenderState, newReceiverState, newDelegationState] = await Promise.all([
|
|
205
|
+
statedb.account.update(senderState.address, account.update(senderState, {
|
|
206
|
+
nonce: tx$1.nonce,
|
|
207
|
+
pk: tx$1.pk,
|
|
208
|
+
...senderUpdates
|
|
209
|
+
}, context), context),
|
|
210
|
+
statedb.account.updateOrCreate(receiverState ?? null, account.updateOrCreate(receiverState ?? null, {
|
|
211
|
+
address: receiverAddr,
|
|
212
|
+
tokens: receiverTokens
|
|
213
|
+
}, context), context),
|
|
214
|
+
context.isDelegationChanged && delegationState ? statedb.delegation.update(delegationState.address, delegation.update(delegationState, {}, context), context) : delegationState
|
|
215
|
+
]);
|
|
216
|
+
context.senderState = newSenderState;
|
|
217
|
+
context.receiverState = newReceiverState;
|
|
218
|
+
context.delegationState = newDelegationState;
|
|
219
|
+
if (updateVaults) await updateVaults();
|
|
220
|
+
next();
|
|
221
|
+
}, { persistError: true });
|
|
222
|
+
runner.use(pipes.VerifyStateDiff());
|
|
223
|
+
var transfer_v2_default = runner;
|
|
224
|
+
|
|
225
|
+
//#endregion
|
|
226
|
+
export { transfer_v2_default as default };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region src/protocols/trade/transfer-v3.d.ts
|
|
2
|
+
/** Token input type */
|
|
3
|
+
interface TokenInput {
|
|
4
|
+
address: string;
|
|
5
|
+
value: string;
|
|
6
|
+
}
|
|
7
|
+
/** Multi-input type - uses List suffix in proto but runtime has both */
|
|
8
|
+
interface MultiInput {
|
|
9
|
+
owner: string;
|
|
10
|
+
tokensList?: TokenInput[];
|
|
11
|
+
tokens?: TokenInput[];
|
|
12
|
+
assetsList?: string[];
|
|
13
|
+
assets?: string[];
|
|
14
|
+
}
|
|
15
|
+
declare const runner: any;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { MultiInput, TokenInput, runner as default };
|