@ocap/tx-protocols 1.28.8 → 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.
Files changed (284) hide show
  1. package/esm/execute.d.mts +53 -0
  2. package/esm/execute.mjs +225 -0
  3. package/esm/index.d.mts +95 -0
  4. package/esm/index.mjs +100 -0
  5. package/esm/pipes/ensure-cost.d.mts +18 -0
  6. package/esm/pipes/ensure-cost.mjs +136 -0
  7. package/esm/pipes/ensure-gas.d.mts +14 -0
  8. package/esm/pipes/ensure-gas.mjs +38 -0
  9. package/esm/protocols/account/delegate.d.mts +4 -0
  10. package/esm/protocols/account/delegate.mjs +195 -0
  11. package/esm/protocols/account/migrate.d.mts +4 -0
  12. package/esm/protocols/account/migrate.mjs +130 -0
  13. package/esm/protocols/account/revoke-delegate.d.mts +4 -0
  14. package/esm/protocols/account/revoke-delegate.mjs +102 -0
  15. package/esm/protocols/asset/acquire-v2.d.mts +9 -0
  16. package/esm/protocols/asset/acquire-v2.mjs +213 -0
  17. package/esm/protocols/asset/acquire-v3.d.mts +17 -0
  18. package/esm/protocols/asset/acquire-v3.mjs +265 -0
  19. package/esm/protocols/asset/calls/transfer-token.d.mts +4 -0
  20. package/esm/protocols/asset/calls/transfer-token.mjs +36 -0
  21. package/esm/protocols/asset/calls/transfer.d.mts +4 -0
  22. package/esm/protocols/asset/calls/transfer.mjs +32 -0
  23. package/esm/protocols/asset/consume.d.mts +4 -0
  24. package/esm/protocols/asset/consume.mjs +92 -0
  25. package/esm/protocols/asset/create.d.mts +4 -0
  26. package/esm/protocols/asset/create.mjs +136 -0
  27. package/esm/protocols/asset/mint.d.mts +4 -0
  28. package/esm/protocols/asset/mint.mjs +156 -0
  29. package/esm/protocols/asset/pipes/exec-mint-hook.d.mts +24 -0
  30. package/esm/protocols/asset/pipes/exec-mint-hook.mjs +54 -0
  31. package/esm/protocols/asset/pipes/extract-factory-tokens.d.mts +21 -0
  32. package/esm/protocols/asset/pipes/extract-factory-tokens.mjs +22 -0
  33. package/esm/protocols/asset/pipes/verify-itx-address.d.mts +32 -0
  34. package/esm/protocols/asset/pipes/verify-itx-address.mjs +56 -0
  35. package/esm/protocols/asset/pipes/verify-itx-assets.d.mts +18 -0
  36. package/esm/protocols/asset/pipes/verify-itx-assets.mjs +27 -0
  37. package/esm/protocols/asset/pipes/verify-itx-variables.d.mts +17 -0
  38. package/esm/protocols/asset/pipes/verify-itx-variables.mjs +20 -0
  39. package/esm/protocols/asset/pipes/verify-mint-limit.d.mts +15 -0
  40. package/esm/protocols/asset/pipes/verify-mint-limit.mjs +14 -0
  41. package/esm/protocols/asset/update.d.mts +4 -0
  42. package/esm/protocols/asset/update.mjs +111 -0
  43. package/esm/protocols/factory/create.d.mts +29 -0
  44. package/esm/protocols/factory/create.mjs +155 -0
  45. package/esm/protocols/governance/claim-stake.d.mts +27 -0
  46. package/esm/protocols/governance/claim-stake.mjs +220 -0
  47. package/esm/protocols/governance/return-stake.d.mts +27 -0
  48. package/esm/protocols/governance/return-stake.mjs +211 -0
  49. package/esm/protocols/governance/revoke-stake.d.mts +27 -0
  50. package/esm/protocols/governance/revoke-stake.mjs +178 -0
  51. package/esm/protocols/governance/slash-stake.d.mts +17 -0
  52. package/esm/protocols/governance/slash-stake.mjs +213 -0
  53. package/esm/protocols/governance/stake.d.mts +15 -0
  54. package/esm/protocols/governance/stake.mjs +270 -0
  55. package/esm/protocols/rollup/claim-reward.d.mts +11 -0
  56. package/esm/protocols/rollup/claim-reward.mjs +322 -0
  57. package/esm/protocols/rollup/close.d.mts +4 -0
  58. package/esm/protocols/rollup/close.mjs +105 -0
  59. package/esm/protocols/rollup/create-block.d.mts +11 -0
  60. package/esm/protocols/rollup/create-block.mjs +303 -0
  61. package/esm/protocols/rollup/create.d.mts +4 -0
  62. package/esm/protocols/rollup/create.mjs +164 -0
  63. package/esm/protocols/rollup/join.d.mts +4 -0
  64. package/esm/protocols/rollup/join.mjs +152 -0
  65. package/esm/protocols/rollup/leave.d.mts +4 -0
  66. package/esm/protocols/rollup/leave.mjs +137 -0
  67. package/esm/protocols/rollup/migrate.d.mts +4 -0
  68. package/esm/protocols/rollup/migrate.mjs +85 -0
  69. package/esm/protocols/rollup/pause.d.mts +4 -0
  70. package/esm/protocols/rollup/pause.mjs +76 -0
  71. package/esm/protocols/rollup/pipes/ensure-validator.d.mts +6 -0
  72. package/esm/protocols/rollup/pipes/ensure-validator.mjs +12 -0
  73. package/esm/protocols/rollup/pipes/verify-evidence.d.mts +15 -0
  74. package/esm/protocols/rollup/pipes/verify-evidence.mjs +29 -0
  75. package/esm/protocols/rollup/pipes/verify-signers.d.mts +15 -0
  76. package/esm/protocols/rollup/pipes/verify-signers.mjs +36 -0
  77. package/esm/protocols/rollup/pipes/verify-status.d.mts +13 -0
  78. package/esm/protocols/rollup/pipes/verify-status.mjs +26 -0
  79. package/esm/protocols/rollup/resume.d.mts +4 -0
  80. package/esm/protocols/rollup/resume.mjs +79 -0
  81. package/esm/protocols/rollup/update.d.mts +4 -0
  82. package/esm/protocols/rollup/update.mjs +111 -0
  83. package/esm/protocols/token/create.d.mts +4 -0
  84. package/esm/protocols/token/create.mjs +150 -0
  85. package/esm/protocols/token/deposit-v2.d.mts +11 -0
  86. package/esm/protocols/token/deposit-v2.mjs +216 -0
  87. package/esm/protocols/token/withdraw-v2.d.mts +9 -0
  88. package/esm/protocols/token/withdraw-v2.mjs +222 -0
  89. package/esm/protocols/token-factory/burn.d.mts +15 -0
  90. package/esm/protocols/token-factory/burn.mjs +233 -0
  91. package/esm/protocols/token-factory/create.d.mts +4 -0
  92. package/esm/protocols/token-factory/create.mjs +254 -0
  93. package/esm/protocols/token-factory/mint.d.mts +15 -0
  94. package/esm/protocols/token-factory/mint.mjs +234 -0
  95. package/esm/protocols/token-factory/pipes/calc-reserve.d.mts +21 -0
  96. package/esm/protocols/token-factory/pipes/calc-reserve.mjs +34 -0
  97. package/esm/protocols/token-factory/pipes/verify-icon.d.mts +14 -0
  98. package/esm/protocols/token-factory/pipes/verify-icon.mjs +18 -0
  99. package/esm/protocols/token-factory/pipes/verify-ownership.d.mts +12 -0
  100. package/esm/protocols/token-factory/pipes/verify-ownership.mjs +63 -0
  101. package/esm/protocols/token-factory/pipes/verify-url.d.mts +12 -0
  102. package/esm/protocols/token-factory/pipes/verify-url.mjs +26 -0
  103. package/esm/protocols/token-factory/update.d.mts +10 -0
  104. package/esm/protocols/token-factory/update.mjs +152 -0
  105. package/esm/protocols/trade/exchange-v2.d.mts +9 -0
  106. package/esm/protocols/trade/exchange-v2.mjs +239 -0
  107. package/esm/protocols/trade/transfer-v2.d.mts +9 -0
  108. package/esm/protocols/trade/transfer-v2.mjs +226 -0
  109. package/esm/protocols/trade/transfer-v3.d.mts +17 -0
  110. package/esm/protocols/trade/transfer-v3.mjs +270 -0
  111. package/esm/util.d.mts +141 -0
  112. package/esm/util.mjs +278 -0
  113. package/lib/_virtual/rolldown_runtime.cjs +29 -0
  114. package/lib/execute.cjs +231 -0
  115. package/lib/execute.d.cts +53 -0
  116. package/lib/index.cjs +105 -0
  117. package/lib/index.d.cts +95 -0
  118. package/lib/pipes/ensure-cost.cjs +141 -0
  119. package/lib/pipes/ensure-cost.d.cts +18 -0
  120. package/lib/pipes/ensure-gas.cjs +41 -0
  121. package/lib/pipes/ensure-gas.d.cts +14 -0
  122. package/lib/protocols/account/delegate.cjs +201 -0
  123. package/lib/protocols/account/delegate.d.cts +4 -0
  124. package/lib/protocols/account/migrate.cjs +135 -0
  125. package/lib/protocols/account/migrate.d.cts +4 -0
  126. package/lib/protocols/account/revoke-delegate.cjs +107 -0
  127. package/lib/protocols/account/revoke-delegate.d.cts +4 -0
  128. package/lib/protocols/asset/acquire-v2.cjs +216 -0
  129. package/lib/protocols/asset/acquire-v2.d.cts +9 -0
  130. package/lib/protocols/asset/acquire-v3.cjs +269 -0
  131. package/lib/protocols/asset/acquire-v3.d.cts +17 -0
  132. package/lib/protocols/asset/calls/transfer-token.cjs +40 -0
  133. package/lib/protocols/asset/calls/transfer-token.d.cts +4 -0
  134. package/lib/protocols/asset/calls/transfer.cjs +35 -0
  135. package/lib/protocols/asset/calls/transfer.d.cts +4 -0
  136. package/lib/protocols/asset/consume.cjs +95 -0
  137. package/lib/protocols/asset/consume.d.cts +4 -0
  138. package/lib/protocols/asset/create.cjs +140 -0
  139. package/lib/protocols/asset/create.d.cts +4 -0
  140. package/lib/protocols/asset/mint.cjs +159 -0
  141. package/lib/protocols/asset/mint.d.cts +4 -0
  142. package/lib/protocols/asset/pipes/exec-mint-hook.cjs +57 -0
  143. package/lib/protocols/asset/pipes/exec-mint-hook.d.cts +24 -0
  144. package/lib/protocols/asset/pipes/extract-factory-tokens.cjs +25 -0
  145. package/lib/protocols/asset/pipes/extract-factory-tokens.d.cts +21 -0
  146. package/lib/protocols/asset/pipes/verify-itx-address.cjs +59 -0
  147. package/lib/protocols/asset/pipes/verify-itx-address.d.cts +32 -0
  148. package/lib/protocols/asset/pipes/verify-itx-assets.cjs +29 -0
  149. package/lib/protocols/asset/pipes/verify-itx-assets.d.cts +18 -0
  150. package/lib/protocols/asset/pipes/verify-itx-variables.cjs +22 -0
  151. package/lib/protocols/asset/pipes/verify-itx-variables.d.cts +17 -0
  152. package/lib/protocols/asset/pipes/verify-mint-limit.cjs +16 -0
  153. package/lib/protocols/asset/pipes/verify-mint-limit.d.cts +15 -0
  154. package/lib/protocols/asset/update.cjs +114 -0
  155. package/lib/protocols/asset/update.d.cts +4 -0
  156. package/lib/protocols/factory/create.cjs +161 -0
  157. package/lib/protocols/factory/create.d.cts +29 -0
  158. package/lib/protocols/governance/claim-stake.cjs +223 -0
  159. package/lib/protocols/governance/claim-stake.d.cts +27 -0
  160. package/lib/protocols/governance/return-stake.cjs +215 -0
  161. package/lib/protocols/governance/return-stake.d.cts +27 -0
  162. package/lib/protocols/governance/revoke-stake.cjs +182 -0
  163. package/lib/protocols/governance/revoke-stake.d.cts +27 -0
  164. package/lib/protocols/governance/slash-stake.cjs +217 -0
  165. package/lib/protocols/governance/slash-stake.d.cts +17 -0
  166. package/lib/protocols/governance/stake.cjs +275 -0
  167. package/lib/protocols/governance/stake.d.cts +15 -0
  168. package/lib/protocols/rollup/claim-reward.cjs +328 -0
  169. package/lib/protocols/rollup/claim-reward.d.cts +11 -0
  170. package/lib/protocols/rollup/close.cjs +107 -0
  171. package/lib/protocols/rollup/close.d.cts +4 -0
  172. package/lib/protocols/rollup/create-block.cjs +309 -0
  173. package/lib/protocols/rollup/create-block.d.cts +11 -0
  174. package/lib/protocols/rollup/create.cjs +169 -0
  175. package/lib/protocols/rollup/create.d.cts +4 -0
  176. package/lib/protocols/rollup/join.cjs +157 -0
  177. package/lib/protocols/rollup/join.d.cts +4 -0
  178. package/lib/protocols/rollup/leave.cjs +141 -0
  179. package/lib/protocols/rollup/leave.d.cts +4 -0
  180. package/lib/protocols/rollup/migrate.cjs +87 -0
  181. package/lib/protocols/rollup/migrate.d.cts +4 -0
  182. package/lib/protocols/rollup/pause.cjs +78 -0
  183. package/lib/protocols/rollup/pause.d.cts +4 -0
  184. package/lib/protocols/rollup/pipes/ensure-validator.cjs +14 -0
  185. package/lib/protocols/rollup/pipes/ensure-validator.d.cts +6 -0
  186. package/lib/protocols/rollup/pipes/verify-evidence.cjs +32 -0
  187. package/lib/protocols/rollup/pipes/verify-evidence.d.cts +15 -0
  188. package/lib/protocols/rollup/pipes/verify-signers.cjs +39 -0
  189. package/lib/protocols/rollup/pipes/verify-signers.d.cts +15 -0
  190. package/lib/protocols/rollup/pipes/verify-status.cjs +28 -0
  191. package/lib/protocols/rollup/pipes/verify-status.d.cts +13 -0
  192. package/lib/protocols/rollup/resume.cjs +81 -0
  193. package/lib/protocols/rollup/resume.d.cts +4 -0
  194. package/lib/protocols/rollup/update.cjs +114 -0
  195. package/lib/protocols/rollup/update.d.cts +4 -0
  196. package/lib/protocols/token/create.cjs +156 -0
  197. package/lib/protocols/token/create.d.cts +4 -0
  198. package/lib/protocols/token/deposit-v2.cjs +219 -0
  199. package/lib/protocols/token/deposit-v2.d.cts +11 -0
  200. package/lib/protocols/token/withdraw-v2.cjs +225 -0
  201. package/lib/protocols/token/withdraw-v2.d.cts +9 -0
  202. package/lib/protocols/token-factory/burn.cjs +236 -0
  203. package/lib/protocols/token-factory/burn.d.cts +15 -0
  204. package/lib/protocols/token-factory/create.cjs +260 -0
  205. package/lib/protocols/token-factory/create.d.cts +4 -0
  206. package/lib/protocols/token-factory/mint.cjs +237 -0
  207. package/lib/protocols/token-factory/mint.d.cts +15 -0
  208. package/lib/protocols/token-factory/pipes/calc-reserve.cjs +38 -0
  209. package/lib/protocols/token-factory/pipes/calc-reserve.d.cts +21 -0
  210. package/lib/protocols/token-factory/pipes/verify-icon.cjs +22 -0
  211. package/lib/protocols/token-factory/pipes/verify-icon.d.cts +14 -0
  212. package/lib/protocols/token-factory/pipes/verify-ownership.cjs +66 -0
  213. package/lib/protocols/token-factory/pipes/verify-ownership.d.cts +12 -0
  214. package/lib/protocols/token-factory/pipes/verify-url.cjs +29 -0
  215. package/lib/protocols/token-factory/pipes/verify-url.d.cts +12 -0
  216. package/lib/protocols/token-factory/update.cjs +155 -0
  217. package/lib/protocols/token-factory/update.d.cts +10 -0
  218. package/lib/protocols/trade/exchange-v2.cjs +243 -0
  219. package/lib/protocols/trade/exchange-v2.d.cts +9 -0
  220. package/lib/protocols/trade/transfer-v2.cjs +229 -0
  221. package/lib/protocols/trade/transfer-v2.d.cts +9 -0
  222. package/lib/protocols/trade/transfer-v3.cjs +274 -0
  223. package/lib/protocols/trade/transfer-v3.d.cts +17 -0
  224. package/lib/util.cjs +296 -0
  225. package/lib/util.d.cts +141 -0
  226. package/package.json +49 -22
  227. package/tools/fixtures.ts +564 -0
  228. package/lib/execute.js +0 -254
  229. package/lib/index.js +0 -117
  230. package/lib/pipes/ensure-cost.js +0 -193
  231. package/lib/pipes/ensure-gas.js +0 -48
  232. package/lib/protocols/account/delegate.js +0 -223
  233. package/lib/protocols/account/migrate.js +0 -153
  234. package/lib/protocols/account/revoke-delegate.js +0 -110
  235. package/lib/protocols/asset/acquire-v2.js +0 -262
  236. package/lib/protocols/asset/acquire-v3.js +0 -330
  237. package/lib/protocols/asset/calls/README.md +0 -5
  238. package/lib/protocols/asset/calls/transfer-token.js +0 -36
  239. package/lib/protocols/asset/calls/transfer.js +0 -28
  240. package/lib/protocols/asset/consume.js +0 -105
  241. package/lib/protocols/asset/create.js +0 -151
  242. package/lib/protocols/asset/mint.js +0 -199
  243. package/lib/protocols/asset/pipes/exec-mint-hook.js +0 -62
  244. package/lib/protocols/asset/pipes/extract-factory-tokens.js +0 -18
  245. package/lib/protocols/asset/pipes/verify-itx-address.js +0 -54
  246. package/lib/protocols/asset/pipes/verify-itx-assets.js +0 -51
  247. package/lib/protocols/asset/pipes/verify-itx-variables.js +0 -26
  248. package/lib/protocols/asset/pipes/verify-mint-limit.js +0 -13
  249. package/lib/protocols/asset/update.js +0 -131
  250. package/lib/protocols/factory/create.js +0 -191
  251. package/lib/protocols/governance/claim-stake.js +0 -266
  252. package/lib/protocols/governance/return-stake.js +0 -248
  253. package/lib/protocols/governance/revoke-stake.js +0 -172
  254. package/lib/protocols/governance/slash-stake.js +0 -271
  255. package/lib/protocols/governance/stake.js +0 -303
  256. package/lib/protocols/rollup/claim-reward.js +0 -342
  257. package/lib/protocols/rollup/close.js +0 -104
  258. package/lib/protocols/rollup/create-block.js +0 -413
  259. package/lib/protocols/rollup/create.js +0 -197
  260. package/lib/protocols/rollup/join.js +0 -182
  261. package/lib/protocols/rollup/leave.js +0 -145
  262. package/lib/protocols/rollup/migrate.js +0 -85
  263. package/lib/protocols/rollup/pause.js +0 -75
  264. package/lib/protocols/rollup/pipes/ensure-validator.js +0 -12
  265. package/lib/protocols/rollup/pipes/verify-evidence.js +0 -37
  266. package/lib/protocols/rollup/pipes/verify-signers.js +0 -87
  267. package/lib/protocols/rollup/pipes/verify-status.js +0 -30
  268. package/lib/protocols/rollup/resume.js +0 -75
  269. package/lib/protocols/rollup/update.js +0 -122
  270. package/lib/protocols/token/create.js +0 -199
  271. package/lib/protocols/token/deposit-v2.js +0 -290
  272. package/lib/protocols/token/withdraw-v2.js +0 -305
  273. package/lib/protocols/token-factory/burn.js +0 -371
  274. package/lib/protocols/token-factory/create.js +0 -342
  275. package/lib/protocols/token-factory/mint.js +0 -385
  276. package/lib/protocols/token-factory/pipes/calc-reserve.js +0 -37
  277. package/lib/protocols/token-factory/pipes/verify-icon.js +0 -27
  278. package/lib/protocols/token-factory/pipes/verify-ownership.js +0 -93
  279. package/lib/protocols/token-factory/pipes/verify-url.js +0 -32
  280. package/lib/protocols/token-factory/update.js +0 -208
  281. package/lib/protocols/trade/exchange-v2.js +0 -239
  282. package/lib/protocols/trade/transfer-v2.js +0 -233
  283. package/lib/protocols/trade/transfer-v3.js +0 -333
  284. package/lib/util.js +0 -442
@@ -1,208 +0,0 @@
1
- const { Joi } = require('@arcblock/validator');
2
- const { CustomError: Error } = require('@ocap/util/lib/error');
3
- const { Runner, pipes } = require('@ocap/tx-pipeline');
4
- const { account, tokenFactory, delegation, token } = require('@ocap/state');
5
- const { applyTokenChange, getDelegationRequirements, decodeAnySafe } = require('../../util');
6
-
7
- const debug = require('debug')(`${require('../../../package.json').name}:update-token-factory`);
8
-
9
- const EnsureTxGas = require('../../pipes/ensure-gas');
10
- const EnsureTxCost = require('../../pipes/ensure-cost');
11
- const verifyIcon = require('./pipes/verify-icon');
12
- const verifyUrl = require('./pipes/verify-url');
13
- const verifyOwnership = require('./pipes/verify-ownership');
14
-
15
- const runner = new Runner();
16
-
17
- const isTest = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test' || process.env.CI;
18
-
19
- runner.use(pipes.VerifyMultiSig(0));
20
-
21
- const schema = Joi.object({
22
- address: Joi.DID().prefix().role('ROLE_TOKEN_FACTORY').required(),
23
- feeRate: Joi.number().min(0).max(2000).optional().allow(null),
24
- token: Joi.object({
25
- icon: Joi.string().optional().allow(null, ''),
26
- website: Joi.string()
27
- .uri({
28
- scheme: isTest ? ['http', 'https'] : ['https'],
29
- allowRelative: false,
30
- })
31
- .max(256)
32
- .optional()
33
- .allow(null, ''),
34
- metadata: Joi.any().optional().allow(null),
35
- })
36
- .optional()
37
- .allow(null),
38
- data: Joi.any().optional().allow(null),
39
- }).options({ stripUnknown: true, noDefaults: false });
40
-
41
- // 1. verify itx
42
- runner.use((context, next) => {
43
- const { error } = schema.validate(context.itx);
44
- if (error) {
45
- return next(new Error('INVALID_TX', `Invalid itx: ${error.message}`));
46
- }
47
-
48
- // decode and verify metadata
49
- if (context.itx.token?.metadata) {
50
- const metadata = decodeAnySafe(context.itx.token.metadata);
51
- const { error: metadataError, value: metadataValue } = token.metadataSchema.validate(metadata.value);
52
- if (metadataError) {
53
- return next(new Error('INVALID_TX', `Invalid metadata: ${metadataError.message}`));
54
- }
55
- context.itx.token.metadata = { ...metadata, value: metadataValue };
56
- }
57
-
58
- return next();
59
- });
60
-
61
- runner.use(verifyIcon({ iconKey: 'itx.token.icon' }));
62
- runner.use(verifyUrl({ urlKeys: ['itx.token.website', 'itx.token.metadata.value.communityUrl'] }));
63
-
64
- // ensure token factory exists
65
- runner.use(
66
- pipes.ExtractState({
67
- from: 'itx.address',
68
- to: 'tokenFactoryState',
69
- table: 'tokenFactory',
70
- status: 'INVALID_TOKEN_FACTORY',
71
- })
72
- );
73
-
74
- // ensure token exists
75
- runner.use(
76
- pipes.ExtractState({
77
- from: 'tokenFactoryState.tokenAddress',
78
- to: 'tokenState',
79
- table: 'token',
80
- status: 'INVALID_TOKEN',
81
- })
82
- );
83
-
84
- // verify website
85
- runner.use((context, next) => {
86
- const { tokenState, itx } = context;
87
- context.itxToken = { symbol: tokenState.symbol, address: tokenState.address, website: itx.token?.website };
88
- return next();
89
- });
90
- runner.use(verifyOwnership({ tokenKey: 'itxToken' }));
91
-
92
- // Ensure sender
93
- runner.use(
94
- pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'INVALID_SENDER_STATE', table: 'account' })
95
- );
96
- runner.use(pipes.VerifyAccountMigration({ stateKey: 'senderState', addressKey: 'tx.from' }));
97
- runner.use(pipes.VerifyBlocked({ stateKeys: ['senderState'] }));
98
-
99
- // Ensure delegation
100
- runner.use(pipes.ExtractState({ from: 'tx.delegator', to: 'delegatorState', status: 'OK', table: 'account' }));
101
- runner.use(pipes.VerifyAccountMigration({ stateKey: 'delegatorState', addressKey: 'tx.delegator' }));
102
- runner.use(
103
- pipes.VerifyDelegation({
104
- type: 'signature',
105
- signerKey: 'senderState',
106
- delegatorKey: 'delegatorState',
107
- getRequirements: getDelegationRequirements,
108
- })
109
- );
110
-
111
- // verify owner
112
- runner.use((context, next) => {
113
- const { tokenFactoryState, delegatorState, senderState } = context;
114
- const owner = delegatorState ? delegatorState.address : senderState.address;
115
- if (owner !== tokenFactoryState.owner) {
116
- return next(new Error('FORBIDDEN', 'Token factory can only be updated by owner'));
117
- }
118
-
119
- return next();
120
- });
121
-
122
- // Ensure tx fee and gas
123
- runner.use(
124
- EnsureTxGas((context) => {
125
- const result = { create: 0, update: 2, payment: 0 };
126
-
127
- if (context.isDelegationChanged) {
128
- result.update += 1;
129
- }
130
-
131
- return result;
132
- })
133
- );
134
- runner.use(EnsureTxCost({ attachSenderChanges: true }));
135
-
136
- // Save context snapshot before updating states
137
- runner.use(pipes.TakeStateSnapshot());
138
-
139
- // Update sender state, token factory state
140
- runner.use(
141
- async (context, next) => {
142
- const {
143
- tx,
144
- itx,
145
- statedb,
146
- senderState,
147
- tokenFactoryState,
148
- tokenState,
149
- senderChange,
150
- updateVaults,
151
- delegationState,
152
- } = context;
153
-
154
- const { tokens: senderTokens = {} } = senderState;
155
-
156
- const senderUpdates = senderChange
157
- ? applyTokenChange({ tokens: senderTokens }, senderChange)
158
- : { tokens: senderTokens };
159
-
160
- const factoryUpdates = itx.feeRate ? { feeRate: itx.feeRate } : {};
161
- const tokenUpdates = itx.token
162
- ? {
163
- icon: itx.token?.icon || tokenState.icon,
164
- website: itx.token?.website || tokenState.website,
165
- metadata: itx.token?.metadata || tokenState.metadata,
166
- }
167
- : null;
168
-
169
- const [newSenderState, newTokenFactoryState, newTokenState, newDelegationState] = await Promise.all([
170
- statedb.account.update(
171
- senderState.address,
172
- account.update(senderState, { nonce: tx.nonce, pk: tx.pk, ...senderUpdates }, context),
173
- context
174
- ),
175
-
176
- statedb.tokenFactory.update(
177
- tokenFactoryState.address,
178
- tokenFactory.update(tokenFactoryState, factoryUpdates, context),
179
- context
180
- ),
181
-
182
- tokenUpdates
183
- ? statedb.token.update(tokenState.address, token.update(tokenState, tokenUpdates, context), context)
184
- : tokenState,
185
-
186
- // Update delegation state
187
- context.isDelegationChanged
188
- ? statedb.delegation.update(delegationState.address, delegation.update(delegationState, {}, context), context)
189
- : delegationState,
190
- ]);
191
-
192
- await updateVaults();
193
-
194
- context.senderState = newSenderState;
195
- context.tokenFactoryState = newTokenFactoryState;
196
- context.tokenState = newTokenState;
197
- context.delegationState = newDelegationState;
198
-
199
- debug('update token factory', newTokenFactoryState, newTokenState);
200
-
201
- next();
202
- },
203
- { persistError: true }
204
- );
205
-
206
- runner.use(pipes.VerifyStateDiff());
207
-
208
- module.exports = runner;
@@ -1,239 +0,0 @@
1
- const isEmpty = require('empty-value');
2
- const { CustomError: Error } = require('@ocap/util/lib/error');
3
- const { Joi, schemas } = require('@arcblock/validator');
4
- const { getListField } = require('@ocap/util/lib/get-list-field');
5
- const { createSortedList } = require('@ocap/util/lib/create-sorted-list');
6
- const { decodeBigInt } = require('@ocap/message');
7
- const { BN } = require('@ocap/util');
8
- const { Runner, pipes } = require('@ocap/tx-pipeline');
9
- const { account } = require('@ocap/state');
10
-
11
- const debug = require('debug')(`${require('../../../package.json').name}:exchange-v2`);
12
-
13
- const EnsureTxGas = require('../../pipes/ensure-gas');
14
- const EnsureTxCost = require('../../pipes/ensure-cost');
15
- const { applyTokenChange } = require('../../util');
16
-
17
- const runner = new Runner();
18
-
19
- // 0. verify itx
20
- const exchangeInfoSchema = Joi.object({
21
- value: Joi.any().optional().allow(null),
22
- tokensList: Joi.array().items(schemas.tokenInput).default([]),
23
- assetsList: Joi.array().items(Joi.DID().prefix().role('ROLE_ASSET')).default([]),
24
- });
25
- const schema = Joi.object({
26
- to: schemas.tokenHolder.required(),
27
- sender: exchangeInfoSchema.required(),
28
- receiver: exchangeInfoSchema.required(),
29
- data: Joi.any().optional().allow(null),
30
- }).options({ stripUnknown: true, noDefaults: false });
31
- runner.use(({ itx }, next) => {
32
- const { error } = schema.validate(itx);
33
- return next(error ? new Error('INVALID_TX', `Invalid itx: ${error.message}`) : null);
34
- });
35
-
36
- runner.use(pipes.VerifyMultiSig(1));
37
- runner.use(pipes.ExtractReceiver({ from: ['tx.signaturesList', 'tx.signatures', 'itx.to'] }));
38
-
39
- runner.use(
40
- pipes.VerifyInfo([
41
- {
42
- error: 'INVALID_TX',
43
- message: 'Can not exchange without any token or assets',
44
- fn: (context) => {
45
- const zero = new BN(0);
46
- const { itx, config } = context;
47
-
48
- context.senderAssets = getListField(itx, 'sender.assets');
49
- context.receiverAssets = getListField(itx, 'receiver.assets');
50
-
51
- context.senderTokens = getListField(itx, 'sender.tokens');
52
- context.receiverTokens = getListField(itx, 'receiver.tokens');
53
-
54
- const senderAmount = itx.sender.value ? decodeBigInt(itx.sender.value) : 0;
55
- if (new BN(senderAmount).gt(zero)) {
56
- context.senderTokens.push({ address: config.token.address, value: senderAmount });
57
- itx.sender.value = '0';
58
- }
59
-
60
- const receiverAmount = itx.receiver.value ? decodeBigInt(itx.receiver.value) : 0;
61
- if (new BN(receiverAmount).gt(zero)) {
62
- context.receiverTokens.push({ address: config.token.address, value: receiverAmount });
63
- itx.receiver.value = '0';
64
- }
65
-
66
- context.tokenAddress = createSortedList(
67
- context.senderTokens.concat(context.receiverTokens).map((x) => x.address)
68
- );
69
-
70
- if (isEmpty(context.senderAssets) && isEmpty(context.senderTokens)) {
71
- return false;
72
- }
73
- if (isEmpty(context.receiverAssets) && isEmpty(context.receiverTokens)) {
74
- return false;
75
- }
76
-
77
- return true;
78
- },
79
- },
80
- ])
81
- );
82
-
83
- // TODO: verify-expiration
84
-
85
- runner.use(pipes.VerifySigner({ signer: 'itx.to' }));
86
- runner.use(pipes.VerifyListSize({ listKey: ['senderAssets', 'receiverAssets'] }));
87
- runner.use(pipes.VerifyListSize({ listKey: ['senderTokens', 'receiverTokens'] }));
88
-
89
- runner.use(
90
- pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'INVALID_SENDER_STATE', table: 'account' })
91
- );
92
- runner.use(pipes.ExtractState({ from: 'tx.delegator', to: 'delegatorState', status: 'OK', table: 'account' }));
93
- runner.use(pipes.VerifyDelegation({ type: 'signature', signerKey: 'senderState', delegatorKey: 'delegatorState' }));
94
- runner.use(pipes.ExtractSigner({ signerKey: 'signerStates', delegatorKey: 'delegatorStates' }));
95
- runner.use(
96
- pipes.VerifyDelegation({
97
- type: 'multisig',
98
- signerKey: 'signerStates',
99
- delegatorKey: 'delegatorStates',
100
- delegationKey: 'delegationStates',
101
- // FIXME: this is not working
102
- getRequirements: () => [],
103
- })
104
- );
105
-
106
- runner.use(pipes.ExtractState({ from: 'tokenAddress', to: 'tokenStates', status: 'INVALID_TOKEN', table: 'token' }));
107
-
108
- runner.use(
109
- pipes.ExtractState({ from: 'receiver', to: 'receiverState', status: 'INVALID_RECEIVER_STATE', table: 'account' })
110
- );
111
- runner.use(pipes.VerifyAccountMigration({ signerKey: 'signerStates', stateKey: 'senderState', addressKey: 'tx.from' }));
112
- runner.use((context, next) => {
113
- context.senderTokenConditions = {
114
- owner: context.senderState.address,
115
- tokens: context.senderTokens,
116
- };
117
- context.receiverTokenConditions = {
118
- owner: context.receiverState.address,
119
- tokens: context.receiverTokens,
120
- };
121
- next();
122
- });
123
- runner.use(pipes.VerifyTokenBalance({ ownerKey: 'senderState', conditionKey: 'senderTokenConditions' }));
124
- runner.use(pipes.VerifyTokenBalance({ ownerKey: 'receiverState', conditionKey: 'receiverTokenConditions' }));
125
-
126
- runner.use(pipes.AntiLandAttack({ senderState: 'senderState', receiverState: 'receiverState' }));
127
- runner.use(pipes.VerifyBlocked({ stateKeys: ['senderState', 'receiverState'] }));
128
- runner.use(
129
- pipes.verifyTokenAccess({
130
- statesKey: 'tokenStates',
131
- listFieldKey: 'spenders',
132
- accountKeys: ['senderState'],
133
- errorMessage: 'Account {address} is not allowed to exchange token {tokenAddress}',
134
- })
135
- );
136
-
137
- runner.use(
138
- pipes.ExtractState({ from: 'senderAssets', to: 'priv.senderAssets', status: 'INVALID_ASSET', table: 'asset' })
139
- );
140
- runner.use(pipes.VerifyTransferrable({ assets: 'priv.senderAssets' }));
141
- runner.use(pipes.VerifyUpdater({ assetKey: 'priv.senderAssets', ownerKey: 'senderState' }));
142
-
143
- runner.use(
144
- pipes.ExtractState({ from: 'receiverAssets', to: 'priv.receiverAssets', status: 'INVALID_ASSET', table: 'asset' })
145
- );
146
- runner.use(pipes.VerifyTransferrable({ assets: 'priv.receiverAssets' }));
147
- runner.use(pipes.VerifyUpdater({ assetKey: 'priv.receiverAssets', ownerKey: 'receiverState' }));
148
-
149
- // Ensure tx fee and gas
150
- runner.use(
151
- EnsureTxGas((context) => {
152
- // FIXME: payment check
153
- const ops = { create: 0, update: 3, payment: 0 };
154
- ops.update += context.senderAssets.length;
155
- ops.update += context.receiverAssets.length;
156
-
157
- return ops;
158
- })
159
- );
160
- runner.use(EnsureTxCost({ attachSenderChanges: true }));
161
-
162
- runner.use(pipes.UpdateOwner({ assets: 'priv.senderAssets', owner: 'receiverState' }));
163
- runner.use(pipes.UpdateOwner({ assets: 'priv.receiverAssets', owner: 'senderState' }));
164
-
165
- // Save context snapshot before updating states
166
- runner.use(pipes.TakeStateSnapshot());
167
-
168
- // update statedb
169
- runner.use(
170
- async (context, next) => {
171
- const {
172
- tx,
173
- itx,
174
- senderAssets,
175
- receiverAssets,
176
- senderTokens,
177
- receiverTokens,
178
- senderState,
179
- receiverState,
180
- senderChange,
181
- updateVaults,
182
- statedb,
183
- } = context;
184
-
185
- const senderStateTokens = senderState.tokens || {};
186
- const receiverStateTokens = receiverState.tokens || {};
187
-
188
- for (const { address, value } of senderTokens) {
189
- const delta = new BN(value);
190
- senderStateTokens[address] = new BN(senderStateTokens[address]).sub(delta).toString();
191
- receiverStateTokens[address] = new BN(receiverStateTokens[address] || '0').add(delta).toString();
192
- }
193
-
194
- for (const { address, value } of receiverTokens) {
195
- const delta = new BN(value);
196
- senderStateTokens[address] = new BN(senderStateTokens[address]).add(delta).toString();
197
- receiverStateTokens[address] = new BN(receiverStateTokens[address] || '0').sub(delta).toString();
198
- }
199
-
200
- const senderUpdates = senderChange
201
- ? applyTokenChange({ tokens: senderStateTokens }, senderChange)
202
- : { tokens: senderStateTokens };
203
-
204
- const [newSenderState, newReceiverState] = await Promise.all([
205
- // Update sender state
206
- statedb.account.update(
207
- senderState.address,
208
- account.update(senderState, { nonce: tx.nonce, ...senderUpdates }, context),
209
- context
210
- ),
211
-
212
- // Update receiver state
213
- statedb.account.update(
214
- receiverState.address,
215
- account.update(receiverState, { tokens: receiverStateTokens }, context),
216
- context
217
- ),
218
- ]);
219
-
220
- await updateVaults();
221
-
222
- context.senderState = newSenderState;
223
- context.receiverState = newReceiverState;
224
-
225
- debug('exchange', {
226
- from: tx.from,
227
- to: itx.to,
228
- sender: { assets: senderAssets.length, token: senderTokens.length },
229
- receiver: { assets: receiverAssets.length, token: receiverTokens.length },
230
- });
231
-
232
- next();
233
- },
234
- { persistError: true }
235
- );
236
-
237
- runner.use(pipes.VerifyStateDiff());
238
-
239
- module.exports = runner;
@@ -1,233 +0,0 @@
1
- const isEmpty = require('empty-value');
2
- const { CustomError: Error } = require('@ocap/util/lib/error');
3
- const { getListField } = require('@ocap/util/lib/get-list-field');
4
- const { decodeBigInt } = require('@ocap/message');
5
- const { Joi, schemas } = require('@arcblock/validator');
6
- const { BN } = require('@ocap/util');
7
- const { Runner, pipes } = require('@ocap/tx-pipeline');
8
- const { account, delegation } = require('@ocap/state');
9
- const { toStakeAddress } = require('@arcblock/did-util');
10
-
11
- const EnsureTxGas = require('../../pipes/ensure-gas');
12
- const EnsureTxCost = require('../../pipes/ensure-cost');
13
- const { applyTokenChange } = require('../../util');
14
-
15
- const runner = new Runner();
16
-
17
- runner.use(pipes.VerifyMultiSig(0));
18
-
19
- // 1. verify itx
20
- const schema = Joi.object({
21
- to: schemas.tokenHolder.required(),
22
- value: Joi.any().optional().allow(null),
23
- tokensList: Joi.array().items(schemas.tokenInput).default([]),
24
- assetsList: Joi.array().items(Joi.DID().prefix().role('ROLE_ASSET')).default([]),
25
- data: Joi.any().optional().allow(null),
26
- }).options({ stripUnknown: true, noDefaults: false });
27
- runner.use(({ itx }, next) => {
28
- const { error } = schema.validate(itx);
29
- if (error) {
30
- return next(new Error('INVALID_TX', `Invalid itx: ${error.message}`));
31
- }
32
- return next();
33
- });
34
- runner.use((context, next) => {
35
- context.assets = getListField(context, 'itx.assets');
36
- context.tokens = getListField(context, 'itx.tokens');
37
- context.tokenAddress = context.tokens.map((x) => x.address);
38
- next();
39
- });
40
- runner.use(
41
- pipes.VerifyInfo([
42
- {
43
- error: 'INSUFFICIENT_DATA',
44
- message: 'Can not transfer without any primary token or assets or secondary tokens',
45
- fn: ({ itx, assets, tokens }) => {
46
- if (itx.to && (itx.value || isEmpty(assets) === false || isEmpty(tokens) === false)) {
47
- return true;
48
- }
49
- return false;
50
- },
51
- },
52
- {
53
- error: 'INVALID_TX',
54
- message: 'Can not transfer primary token smaller than 0',
55
- fn: ({ itx, assets, tokens }) => {
56
- if (isEmpty(tokens) && isEmpty(assets)) {
57
- const amount = itx.value ? decodeBigInt(itx.value) : 0;
58
- if (new BN(amount).lte(new BN(0))) {
59
- return false;
60
- }
61
- }
62
-
63
- return true;
64
- },
65
- },
66
- ])
67
- );
68
-
69
- runner.use(pipes.VerifyListSize({ listKey: ['assets', 'tokens'] }));
70
-
71
- // For backwards compatibility: merge primary and secondary tokens
72
- runner.use((context, next) => {
73
- if (context.itx.value) {
74
- const amount = new BN(decodeBigInt(context.itx.value));
75
- if (amount.gt(new BN(0))) {
76
- context.tokens.push({ address: context.config.token.address, value: amount.toString() });
77
- context.itx.value = undefined;
78
- }
79
- }
80
- next();
81
- });
82
-
83
- runner.use(
84
- EnsureTxGas((context) => {
85
- // FIXME: payment check
86
- const result = { create: 0, update: 2, payment: 0 };
87
- result.update += context.assetStates?.length || 0;
88
-
89
- if (context.receiverState) {
90
- result.update += 1;
91
- } else {
92
- result.create += 1;
93
- }
94
-
95
- return result;
96
- })
97
- );
98
-
99
- // Pre-query states to cache them for later use
100
- runner.use(async (context, next) => {
101
- const { statedb, tx, gasVault } = context;
102
- const accounts = [tx.from, context.receiver, gasVault].filter(Boolean);
103
-
104
- await Promise.all([
105
- ...accounts.map((x) => statedb.account.get(x, context)),
106
- statedb.stake.get(toStakeAddress(tx.from, tx.from), context),
107
- ]);
108
-
109
- next();
110
- });
111
-
112
- runner.use(
113
- pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'INVALID_SENDER_STATE', table: 'account' })
114
- );
115
- runner.use(pipes.VerifyAccountMigration({ stateKey: 'senderState', addressKey: 'tx.from' }));
116
-
117
- runner.use(pipes.ExtractState({ from: 'tokenAddress', to: 'tokenStates', status: 'INVALID_TOKEN', table: 'token' }));
118
- runner.use((context, next) => {
119
- context.tokenConditions = {
120
- owner: context.senderState.address,
121
- tokens: context.tokens,
122
- };
123
- next();
124
- });
125
- runner.use(pipes.VerifyTokenBalance({ ownerKey: 'senderState', conditionKey: 'tokenConditions' }));
126
-
127
- runner.use(pipes.ExtractState({ from: 'tx.delegator', to: 'delegatorState', status: 'OK', table: 'account' }));
128
- runner.use(pipes.VerifyAccountMigration({ stateKey: 'delegatorState', addressKey: 'tx.delegator' }));
129
- runner.use(
130
- pipes.VerifyDelegation({
131
- type: 'signature',
132
- signerKey: 'senderState',
133
- delegatorKey: 'delegatorState',
134
- getRequirements: (ctx) => [
135
- ...ctx.tokens.map((x) => ({ type: 'token', ...x, to: ctx.itx.to })),
136
- ...ctx.assets.map((x) => ({ type: 'asset', address: x, to: ctx.itx.to })),
137
- ],
138
- })
139
- );
140
-
141
- runner.use(pipes.ExtractReceiver({ from: 'itx.to', to: 'receiver' }));
142
- runner.use(pipes.ExtractState({ from: 'receiver', to: 'receiverState', status: 'OK', table: 'account' }));
143
- runner.use(pipes.AntiLandAttack({ senderState: 'senderState', receiverState: 'receiverState' }));
144
- runner.use(pipes.VerifyBlocked({ stateKeys: ['senderState', 'receiverState'] }));
145
- runner.use(
146
- pipes.verifyTokenAccess({
147
- statesKey: 'tokenStates',
148
- listFieldKey: 'spenders',
149
- accountKeys: ['senderState'],
150
- errorMessage: 'Account {address} is not allowed to transfer token {tokenAddress}',
151
- })
152
- );
153
-
154
- runner.use(pipes.ExtractState({ from: 'assets', to: 'assetStates', status: 'INVALID_ASSET', table: 'asset' }));
155
- runner.use(pipes.VerifyTransferrable({ assets: 'assetStates' }));
156
- runner.use(pipes.VerifyUpdater({ assetKey: 'assetStates', ownerKey: 'senderState' }));
157
-
158
- // Ensure tx fee and gas
159
- runner.use(EnsureTxCost({ attachSenderChanges: true }));
160
-
161
- // transfer assets to new owner
162
- runner.use((context, next) => {
163
- const { itx, receiverState } = context;
164
- context.receiverAddr = receiverState ? receiverState.address : itx.to;
165
- return next();
166
- });
167
- runner.use(pipes.UpdateOwner({ assets: 'assetStates', owner: 'receiverAddr' }));
168
-
169
- // Save context snapshot before updating states
170
- runner.use(pipes.TakeStateSnapshot());
171
-
172
- // update statedb: transfer tokens to new owner
173
- runner.use(
174
- async (context, next) => {
175
- const {
176
- tx,
177
- tokens,
178
- senderState,
179
- receiverAddr,
180
- receiverState,
181
- statedb,
182
- senderChange,
183
- delegationState,
184
- updateVaults,
185
- } = context;
186
-
187
- const { tokens: senderTokens = {} } = senderState;
188
- const { tokens: receiverTokens = {} } = receiverState || {};
189
- for (const token of tokens) {
190
- const delta = new BN(token.value);
191
- senderTokens[token.address] = new BN(senderTokens[token.address]).sub(delta).toString();
192
- receiverTokens[token.address] = new BN(receiverTokens[token.address] || '0').add(delta).toString();
193
- }
194
-
195
- const senderUpdates = senderChange
196
- ? applyTokenChange({ tokens: senderTokens }, senderChange)
197
- : { tokens: senderTokens };
198
-
199
- const [newSenderState, newReceiverState, newDelegationState] = await Promise.all([
200
- // Update sender state
201
- statedb.account.update(
202
- senderState.address,
203
- account.update(senderState, { nonce: tx.nonce, pk: tx.pk, ...senderUpdates }, context),
204
- context
205
- ),
206
-
207
- // Update receiver state
208
- statedb.account.updateOrCreate(
209
- receiverState,
210
- account.updateOrCreate(receiverState, { address: receiverAddr, tokens: receiverTokens }, context),
211
- context
212
- ),
213
-
214
- // Update delegation state
215
- context.isDelegationChanged
216
- ? statedb.delegation.update(delegationState.address, delegation.update(delegationState, {}, context), context)
217
- : delegationState,
218
- ]);
219
-
220
- context.senderState = newSenderState;
221
- context.receiverState = newReceiverState;
222
- context.delegationState = newDelegationState;
223
-
224
- await updateVaults();
225
-
226
- next();
227
- },
228
- { persistError: true }
229
- );
230
-
231
- runner.use(pipes.VerifyStateDiff());
232
-
233
- module.exports = runner;