@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.
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,290 +0,0 @@
1
- const { CustomError: Error } = require('@ocap/util/lib/error');
2
- const { Joi, schemas, patterns } = require('@arcblock/validator');
3
- const { getListField } = require('@ocap/util/lib/get-list-field');
4
- const { BN, fromUnitToToken } = require('@ocap/util');
5
- const { Runner, pipes } = require('@ocap/tx-pipeline');
6
- const { account, stake, evidence } = require('@ocap/state');
7
- const { toStakeAddress } = require('@arcblock/did-util');
8
-
9
- const debug = require('debug')(`${require('../../../package.json').name}:deposit-token`);
10
-
11
- const EnsureTxGas = require('../../pipes/ensure-gas');
12
- const EnsureTxCost = require('../../pipes/ensure-cost');
13
-
14
- const VerifySigners = require('../rollup/pipes/verify-signers');
15
- const VerifyStatus = require('../rollup/pipes/verify-status');
16
- const { applyTokenUpdates, applyTokenChange, getTxFee, getBNSum, getRewardLocker } = require('../../util');
17
-
18
- const schema = Joi.object({
19
- token: schemas.tokenInput.required(),
20
- to: Joi.DID().prefix().wallet('ethereum').required(),
21
- proposer: Joi.DID().prefix().wallet('ethereum').required(),
22
- evidence: Joi.object({
23
- hash: Joi.string().regex(patterns.txHash).required(),
24
- }).required(),
25
- rollup: Joi.DID().prefix().role('ROLE_ROLLUP').required(),
26
- actualFee: Joi.BN().min(0).required(),
27
- data: Joi.any().optional().allow(null),
28
- }).options({ stripUnknown: true, noDefaults: false });
29
-
30
- const runner = new Runner();
31
-
32
- // 1. verify itx
33
- runner.use(({ tx, itx }, next) => {
34
- const { error } = schema.validate(itx);
35
- if (error) {
36
- return next(new Error('INVALID_TX', `Invalid itx: ${error.message}`));
37
- }
38
-
39
- if (itx.to !== tx.from) {
40
- return next(new Error('INVALID_TX', 'You can only deposit to tx sender account'));
41
- }
42
-
43
- // ensure proposer in signer list
44
- const signatures = getListField(tx, 'signatures');
45
- if (signatures.some((x) => x.signer === itx.proposer) === false) {
46
- return next(new Error('INVALID_TX', 'itx.proposer must exist in signatures'));
47
- }
48
-
49
- // ensure tx.from not in validator list
50
- if (signatures.some((x) => x.signer === tx.from)) {
51
- return next(new Error('INVALID_TX', 'tx.from should not exist in signatures'));
52
- }
53
-
54
- return next();
55
- });
56
-
57
- // 2. verify rollup against itx
58
- runner.use(pipes.ExtractState({ from: 'itx.rollup', to: 'rollupState', status: 'INVALID_ROLLUP', table: 'rollup' }));
59
- runner.use(VerifyStatus({ paused: false }));
60
- runner.use((context, next) => {
61
- const { itx, rollupState } = context;
62
- if (rollupState.tokenAddress !== itx.token.address) {
63
- return next(new Error('INVALID_TX', 'Deposit token address does not match with rollup state'));
64
- }
65
-
66
- if (rollupState.validators.some((x) => x.address === itx.proposer) === false) {
67
- return next(new Error('INVALID_TX', 'itx.proposer does not exist in validators'));
68
- }
69
-
70
- return next();
71
- });
72
-
73
- // 3. verify evidence
74
- runner.use(pipes.ExtractState({ from: 'itx.evidence.hash', to: 'evidenceState', status: 'OK', table: 'evidence' }));
75
- runner.use(({ evidenceState }, next) => {
76
- if (evidenceState) {
77
- return next(new Error('INVALID_TX', 'Deposit evidence already seen on this chain'));
78
- }
79
-
80
- return next();
81
- });
82
-
83
- // 4. verify signers & signatures
84
- runner.use(VerifySigners({ signersKey: 'tx.signaturesList', allowSender: true }));
85
- runner.use(pipes.VerifyMultiSigV2({ signersKey: 'signers' }));
86
-
87
- // 5. verify token state
88
- runner.use(
89
- pipes.ExtractState({ from: 'itx.token.address', to: 'tokenState', status: 'INVALID_TOKEN', table: 'token' })
90
- );
91
-
92
- // 6. verify staking: get address, extract state, verify amount
93
- runner.use((context, next) => {
94
- const { itx } = context;
95
- context.stakeAddress = toStakeAddress(itx.proposer, itx.rollup);
96
- context.lockerAddress = getRewardLocker(itx.rollup);
97
- return next();
98
- });
99
- runner.use(pipes.ExtractState({ from: 'lockerAddress', to: 'lockerState', status: 'OK', table: 'stake' }));
100
- runner.use(
101
- pipes.ExtractState({ from: 'stakeAddress', to: 'stakeState', status: 'INVALID_STAKE_STATE', table: 'stake' })
102
- );
103
- runner.use((context, next) => {
104
- const { itx, stakeState, tokenState, rollupState } = context;
105
- if (stakeState.revocable === true) {
106
- return next(new Error('INVALID_STAKE_STATE', `Staking not locked for deposit: ${stakeState.address}`));
107
- }
108
-
109
- const depositAmount = new BN(itx.token.value).add(new BN(itx.actualFee));
110
- const minDepositAmount = new BN(rollupState.minDepositAmount);
111
- const maxDepositAmount = new BN(rollupState.maxDepositAmount);
112
- if (depositAmount.lt(minDepositAmount)) {
113
- return next(
114
- new Error(
115
- 'INVALID_TX',
116
- `Deposit amount must be greater than minDepositAmount: ${fromUnitToToken(minDepositAmount, tokenState.decimal)}`
117
- )
118
- );
119
- }
120
- if (depositAmount.gt(maxDepositAmount)) {
121
- return next(
122
- new Error(
123
- 'INVALID_TX',
124
- `Deposit amount must be less than maxDepositAmount: ${fromUnitToToken(maxDepositAmount, tokenState.decimal)}`
125
- )
126
- );
127
- }
128
-
129
- const stakedAmount = new BN(stakeState.tokens[rollupState.tokenAddress] || 0);
130
- if (depositAmount.gt(stakedAmount)) {
131
- return next(
132
- new Error(
133
- 'INSUFFICIENT_STAKE',
134
- `Deposit amount must be less than available stake: ${fromUnitToToken(stakedAmount, tokenState.decimal)}`
135
- )
136
- );
137
- }
138
-
139
- return next();
140
- });
141
-
142
- // 7. verify actualFee
143
- runner.use((context, next) => {
144
- const { itx, rollupState, tokenState } = context;
145
- const { depositFeeRate, maxDepositFee, minDepositFee } = rollupState;
146
-
147
- const { reward } = getTxFee({
148
- amount: itx.token.value,
149
- feeRate: depositFeeRate,
150
- maxFee: maxDepositFee,
151
- minFee: minDepositFee,
152
- });
153
-
154
- if (new BN(itx.actualFee).lt(new BN(reward))) {
155
- const expected = fromUnitToToken(reward, tokenState.decimal);
156
- const actual = fromUnitToToken(itx.actualFee, tokenState.decimal);
157
- return next(new Error('INVALID_TX', `itx.actualFee too low, expect at least ${expected}, got ${actual}`));
158
- }
159
-
160
- return next();
161
- });
162
-
163
- // 7. verify sender and signer exists and not migrated
164
- runner.use(pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'OK', table: 'account' }));
165
- runner.use(
166
- pipes.ExtractState({ from: 'signers', to: 'signerStates', status: 'INVALID_SIGNER_STATE', table: 'account' })
167
- );
168
- runner.use(pipes.VerifyAccountMigration({ signerKey: 'signerStates', stateKey: 'senderState', addressKey: 'tx.from' }));
169
-
170
- // Ensure tx fee and gas
171
- runner.use(
172
- EnsureTxGas((context) => {
173
- // FIXME: payment check
174
- const result = { create: 1, update: 1, payment: 0 };
175
- if (context.senderState) {
176
- result.update += 1;
177
- } else {
178
- result.create += 1;
179
- }
180
- if (context.lockerState) {
181
- result.update += 1;
182
- } else {
183
- result.create += 1;
184
- }
185
- return result;
186
- })
187
- );
188
- runner.use(EnsureTxCost({ attachSenderChanges: true }));
189
-
190
- // Save context snapshot before updating states
191
- runner.use(pipes.TakeStateSnapshot());
192
-
193
- // 8. update state: the token minting is done when deposit finalized in rollup-block
194
- runner.use(
195
- async (context, next) => {
196
- const {
197
- tx,
198
- itx,
199
- statedb,
200
- senderState,
201
- stakeState,
202
- stakeAddress,
203
- lockerState,
204
- lockerAddress,
205
- senderChange,
206
- updateVaults,
207
- } = context;
208
-
209
- const user = itx.token.value;
210
- const fee = itx.actualFee;
211
- const total = getBNSum(user, fee);
212
-
213
- const stakeUpdates = applyTokenUpdates([{ address: itx.token.address, value: total }], stakeState, 'sub');
214
- let senderUpdates = applyTokenUpdates([{ address: itx.token.address, value: user }], senderState || {}, 'add');
215
- const lockerUpdates = applyTokenUpdates(
216
- [{ address: itx.token.address, value: fee }],
217
- lockerState || { tokens: {} },
218
- 'add'
219
- );
220
-
221
- if (senderChange && senderState) {
222
- senderUpdates = applyTokenChange(senderUpdates, senderChange);
223
- }
224
-
225
- const sender = senderState ? senderState.address : tx.from;
226
- const [newSenderState, newStakeState, newLockerState, evidenceState] = await Promise.all([
227
- // updateOrCreate user account
228
- statedb.account.updateOrCreate(
229
- senderState,
230
- account.updateOrCreate(senderState, { address: sender, nonce: tx.nonce, pk: tx.pk, ...senderUpdates }, context),
231
- context
232
- ),
233
-
234
- // update stake amount from the proposer
235
- statedb.stake.update(stakeAddress, stake.update(stakeState, stakeUpdates, context), context),
236
-
237
- // Update pending fee locker state
238
- lockerState
239
- ? statedb.stake.update(lockerState.address, stake.update(lockerState, lockerUpdates, context), context)
240
- : statedb.stake.create(
241
- lockerAddress,
242
- stake.create(
243
- {
244
- address: lockerAddress,
245
- sender: itx.rollup,
246
- receiver: itx.rollup,
247
- revocable: false,
248
- message: 'pending-block-reward',
249
- revokeWaitingPeriod: 0,
250
- ...lockerUpdates,
251
- },
252
- context
253
- ),
254
- context
255
- ),
256
-
257
- // Create evidence state
258
- statedb.evidence.create(
259
- itx.evidence.hash,
260
- evidence.create({ hash: itx.evidence.hash, data: 'rollup-deposit' }, context),
261
- context
262
- ),
263
- ]);
264
-
265
- await updateVaults();
266
-
267
- context.senderState = newSenderState;
268
- context.stakeState = newStakeState;
269
- context.evidenceState = evidenceState;
270
- context.stakeStates = [newStakeState, newLockerState];
271
-
272
- context.updatedAccounts.push(
273
- // stake for tx proposer is decreased
274
- { address: stakeAddress, token: itx.token.address, delta: `-${total}`, action: 'unlock' },
275
- // mint to depositor from stake
276
- { address: sender, token: itx.token.address, delta: user, action: 'unlock' },
277
- // tx fee is locked for later claiming
278
- { address: lockerAddress, token: itx.token.address, delta: fee, action: 'pending' }
279
- );
280
-
281
- debug('deposit-token-v2', itx);
282
-
283
- next();
284
- },
285
- { persistError: true }
286
- );
287
-
288
- runner.use(pipes.VerifyStateDiff());
289
-
290
- module.exports = runner;
@@ -1,305 +0,0 @@
1
- const { CustomError: Error } = require('@ocap/util/lib/error');
2
- const { Joi, schemas } = require('@arcblock/validator');
3
- const { getListField } = require('@ocap/util/lib/get-list-field');
4
- const { BN, fromUnitToToken } = require('@ocap/util');
5
- const { Runner, pipes } = require('@ocap/tx-pipeline');
6
- const { account, stake } = require('@ocap/state');
7
- const { toStakeAddress } = require('@arcblock/did-util');
8
-
9
- const debug = require('debug')(`${require('../../../package.json').name}:withdraw-token`);
10
-
11
- const EnsureTxGas = require('../../pipes/ensure-gas');
12
- const EnsureTxCost = require('../../pipes/ensure-cost');
13
- const VerifyStatus = require('../rollup/pipes/verify-status');
14
-
15
- const { applyTokenUpdates, getTxFee, getBNSum, getRewardLocker, applyTokenChange } = require('../../util');
16
-
17
- const verifyMultiSigV2 = pipes.VerifyMultiSigV2({ signersKey: 'signers' });
18
-
19
- const schema = Joi.object({
20
- token: schemas.tokenInput.required(),
21
- to: Joi.DID().prefix().wallet('ethereum').required(),
22
- rollup: Joi.DID().prefix().role('ROLE_ROLLUP').required(),
23
- proposer: Joi.DID().prefix().wallet('ethereum').optional().allow(null).allow('').default(''),
24
- actualFee: Joi.BN().min(0).required(),
25
- maxFee: Joi.BN().min(0).required(),
26
- data: Joi.any().optional().allow(null),
27
- }).options({ stripUnknown: true, noDefaults: false });
28
-
29
- const runner = new Runner();
30
-
31
- // 1. verify itx
32
- runner.use(({ tx, itx }, next) => {
33
- const { error } = schema.validate(itx);
34
- if (error) {
35
- return next(new Error('INVALID_TX', `Invalid itx: ${error.message}`));
36
- }
37
-
38
- if (itx.to !== tx.from) {
39
- return next(new Error('INVALID_TX', 'You can only withdraw to tx sender account'));
40
- }
41
-
42
- return next();
43
- });
44
-
45
- // 2. verify rollup against itx
46
- runner.use(pipes.ExtractState({ from: 'itx.rollup', to: 'rollupState', status: 'INVALID_ROLLUP', table: 'rollup' }));
47
- runner.use(VerifyStatus({ paused: false }));
48
- runner.use((context, next) => {
49
- const { itx, rollupState } = context;
50
- if (rollupState.tokenAddress !== itx.token.address) {
51
- return next(new Error('INVALID_TX', 'Withdraw token address does not match with rollup'));
52
- }
53
-
54
- return next();
55
- });
56
-
57
- // 3. verify token state
58
- runner.use(
59
- pipes.ExtractState({ from: 'itx.token.address', to: 'tokenState', status: 'INVALID_TOKEN', table: 'token' })
60
- );
61
-
62
- // 4. verify sender and signer states
63
- runner.use(
64
- pipes.ExtractState({ from: 'tx.from', to: 'senderState', status: 'INVALID_SENDER_STATE', table: 'account' })
65
- );
66
-
67
- // 5. verify amount
68
- runner.use((context, next) => {
69
- const { tx, itx, tokenState, rollupState } = context;
70
-
71
- // withdraw amount
72
- const withdrawAmount = new BN(itx.token.value);
73
- const minWithdrawAmount = new BN(rollupState.minWithdrawAmount);
74
- const maxWithdrawAmount = new BN(rollupState.maxWithdrawAmount);
75
- if (withdrawAmount.lt(minWithdrawAmount)) {
76
- return next(
77
- new Error(
78
- 'INVALID_TX',
79
- `Withdraw amount must be greater than: ${fromUnitToToken(minWithdrawAmount, tokenState.decimal)}`
80
- )
81
- );
82
- }
83
- if (withdrawAmount.gt(maxWithdrawAmount)) {
84
- return next(
85
- new Error(
86
- 'INVALID_TX',
87
- `Withdraw amount must be less than maxWithdrawAmount: ${fromUnitToToken(maxWithdrawAmount, tokenState.decimal)}`
88
- )
89
- );
90
- }
91
-
92
- // user balance
93
- const maxAmount = getBNSum(itx.token.value, itx.actualFee, itx.maxFee);
94
- context.tokenConditions = { owner: tx.from, tokens: [{ address: itx.token.address, value: maxAmount }] };
95
-
96
- return next();
97
- });
98
- runner.use(pipes.VerifyTokenBalance({ ownerKey: 'senderState', conditionKey: 'tokenConditions' }));
99
-
100
- // 6. verify actualFee
101
- runner.use((context, next) => {
102
- const { itx, rollupState, tokenState } = context;
103
- const { withdrawFeeRate, maxWithdrawFee, minWithdrawFee } = rollupState;
104
-
105
- const isFixedFee = new BN(itx.maxFee).isZero();
106
- if (isFixedFee) {
107
- const { reward } = getTxFee({
108
- amount: itx.token.value,
109
- feeRate: withdrawFeeRate,
110
- maxFee: maxWithdrawFee,
111
- minFee: minWithdrawFee,
112
- });
113
-
114
- if (new BN(itx.actualFee).lt(new BN(reward))) {
115
- const expected = fromUnitToToken(reward, tokenState.decimal);
116
- const actual = fromUnitToToken(itx.actualFee, tokenState.decimal);
117
- return next(new Error('INVALID_TX', `itx.actualFee too low, expect at least ${expected}, got ${actual}`));
118
- }
119
- }
120
-
121
- return next();
122
- });
123
-
124
- // 7. verify maxFee and conditional multi-sig
125
- runner.use((context, next) => {
126
- const { tx, itx } = context;
127
- const isFixedFee = new BN(itx.maxFee).isZero();
128
- if (isFixedFee) {
129
- return next();
130
- }
131
-
132
- const signatures = getListField(tx, 'signatures');
133
-
134
- // ensure signature count
135
- if (signatures.length !== 1) {
136
- return next(new Error('INVALID_TX', 'Withdraw with maxFee should have one multi-sig'));
137
- }
138
-
139
- // ensure proposer in signer list
140
- if (signatures.some((x) => x.signer === itx.proposer) === false) {
141
- return next(new Error('INVALID_TX', 'itx.proposer must exist in tx.signatures'));
142
- }
143
-
144
- // ensure signature is valid
145
- context.signers = signatures.map((x) => x.signer);
146
- verifyMultiSigV2(context, next);
147
- });
148
-
149
- // 7. verify signers (sender + proposer)
150
- runner.use(
151
- pipes.ExtractState({ from: 'signers', to: 'signerStates', status: 'INVALID_SIGNER_STATE', table: 'account' })
152
- );
153
- runner.use(pipes.VerifyAccountMigration({ signerKey: 'signerStates', stateKey: 'senderState', addressKey: 'tx.from' }));
154
-
155
- // 8. verify staking: user withdraw is locked in stake
156
- runner.use((context, next) => {
157
- const { itx, senderState } = context;
158
- context.stakeAddress = toStakeAddress(senderState.address, itx.rollup);
159
- context.lockerAddress = getRewardLocker(itx.rollup);
160
- return next();
161
- });
162
- runner.use(pipes.ExtractState({ from: 'lockerAddress', to: 'lockerState', status: 'OK', table: 'stake' }));
163
- runner.use(pipes.ExtractState({ from: 'stakeAddress', to: 'stakeState', status: 'OK', table: 'stake' }));
164
- runner.use((context, next) => {
165
- const { stakeState } = context;
166
- if (stakeState && stakeState.revocable === true) {
167
- return next(new Error('INVALID_STAKE_STATE', `Staking not locked for withdraw: ${stakeState.address}`));
168
- }
169
-
170
- return next();
171
- });
172
-
173
- // Ensure tx fee and gas
174
- runner.use(
175
- EnsureTxGas((context) => {
176
- // FIXME: payment check
177
- const result = { create: 0, update: 1, payment: 0 };
178
- if (context.stakeState) {
179
- result.update += 1;
180
- } else {
181
- result.create += 1;
182
- }
183
- if (context.lockerState) {
184
- result.update += 1;
185
- } else {
186
- result.create += 1;
187
- }
188
- return result;
189
- })
190
- );
191
- runner.use(EnsureTxCost({ attachSenderChanges: true }));
192
-
193
- // Save context snapshot before updating states
194
- runner.use(pipes.TakeStateSnapshot());
195
-
196
- // 8. update state: the fee splitting and token burning is done when withdraw finalized in rollup-block
197
- runner.use(
198
- async (context, next) => {
199
- const {
200
- tx,
201
- itx,
202
- statedb,
203
- stakeState,
204
- senderState,
205
- lockerState,
206
- lockerAddress,
207
- stakeAddress,
208
- senderChange,
209
- updateVaults,
210
- } = context;
211
-
212
- const total = getBNSum(itx.token.value, itx.actualFee, itx.maxFee);
213
- const fee = getBNSum(itx.actualFee, itx.maxFee);
214
-
215
- let senderUpdates = applyTokenUpdates([{ address: itx.token.address, value: total }], senderState, 'sub');
216
- if (senderChange) {
217
- senderUpdates = applyTokenChange(senderUpdates, senderChange);
218
- }
219
-
220
- // Burned amount should equal to user received amount
221
- const stakeUpdates = applyTokenUpdates(
222
- [{ address: itx.token.address, value: itx.token.value }],
223
- stakeState || {},
224
- 'add'
225
- );
226
-
227
- // Fees are locked to reward locker, and funded later
228
- const lockerUpdates = applyTokenUpdates(
229
- [{ address: itx.token.address, value: fee }],
230
- lockerState || { tokens: {} },
231
- 'add'
232
- );
233
-
234
- const [newSenderState, newStakeState, newLockerState] = await Promise.all([
235
- // update user account
236
- statedb.account.update(
237
- senderState.address,
238
- account.update(senderState, { nonce: tx.nonce, ...senderUpdates }, context),
239
- context
240
- ),
241
-
242
- // lock withdraw amount to a not-revocable stake
243
- stakeState
244
- ? statedb.stake.update(stakeAddress, stake.update(stakeState, stakeUpdates, context), context)
245
- : statedb.stake.create(
246
- stakeAddress,
247
- stake.create(
248
- {
249
- address: stakeAddress,
250
- sender: senderState.address,
251
- receiver: itx.rollup,
252
- revocable: false,
253
- tokens: stakeUpdates.tokens,
254
- message: 'withdraw-locker',
255
- assets: [],
256
- },
257
- context
258
- ),
259
- context
260
- ),
261
-
262
- // Update pending fee locker state
263
- lockerState
264
- ? statedb.stake.update(lockerState.address, stake.update(lockerState, lockerUpdates, context), context)
265
- : statedb.stake.create(
266
- lockerAddress,
267
- stake.create(
268
- {
269
- address: lockerAddress,
270
- sender: itx.rollup,
271
- receiver: itx.rollup,
272
- revocable: false,
273
- message: 'pending-block-reward',
274
- revokeWaitingPeriod: 0,
275
- ...lockerUpdates,
276
- },
277
- context
278
- ),
279
- context
280
- ),
281
- ]);
282
-
283
- await updateVaults();
284
-
285
- context.senderState = newSenderState;
286
- context.stakeState = newStakeState;
287
- context.lockerState = newLockerState;
288
- context.stakeStates = [newStakeState, newLockerState];
289
-
290
- context.updatedAccounts.push(
291
- { address: senderState.address, token: itx.token.address, delta: `-${total}`, action: 'lock' },
292
- { address: stakeAddress, token: itx.token.address, delta: itx.token.value, action: 'lock' },
293
- { address: lockerAddress, token: itx.token.address, delta: fee, action: 'pending' }
294
- );
295
-
296
- debug('withdraw-token-v2', itx);
297
-
298
- next();
299
- },
300
- { persistError: true }
301
- );
302
-
303
- runner.use(pipes.VerifyStateDiff());
304
-
305
- module.exports = runner;