@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
package/lib/util.cjs ADDED
@@ -0,0 +1,296 @@
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ let _ocap_util_lib_error = require("@ocap/util/lib/error");
3
+ let _arcblock_did_util = require("@arcblock/did-util");
4
+ let _ocap_util = require("@ocap/util");
5
+ let _ocap_message = require("@ocap/message");
6
+ let _ocap_util_lib_get_list_field = require("@ocap/util/lib/get-list-field");
7
+ let lodash_cloneDeep = require("lodash/cloneDeep");
8
+ lodash_cloneDeep = require_rolldown_runtime.__toESM(lodash_cloneDeep);
9
+ let lodash_flattenDeep = require("lodash/flattenDeep");
10
+ lodash_flattenDeep = require_rolldown_runtime.__toESM(lodash_flattenDeep);
11
+ let lodash_groupBy = require("lodash/groupBy");
12
+ lodash_groupBy = require_rolldown_runtime.__toESM(lodash_groupBy);
13
+
14
+ //#region src/util.ts
15
+ const ZERO = new _ocap_util.BN(0);
16
+ const RATE_BASE = new _ocap_util.BN(1e4);
17
+ const decodeAnyNested = (encoded) => {
18
+ if (!encoded) return encoded;
19
+ if (Array.isArray(encoded)) return encoded.map((k) => decodeAnyNested(k));
20
+ if (typeof encoded === "object") {
21
+ const obj = encoded;
22
+ if (obj.typeUrl && obj.value) {
23
+ const result = (0, _ocap_message.decodeAny)(obj);
24
+ result.value = decodeAnyNested(result.value);
25
+ return result;
26
+ }
27
+ return Object.keys(obj).reduce((acc, x) => {
28
+ acc[x] = decodeAnyNested(obj[x]);
29
+ return acc;
30
+ }, {});
31
+ }
32
+ return encoded;
33
+ };
34
+ const decodeAnySafe = (encoded) => {
35
+ if (!encoded) return null;
36
+ try {
37
+ const decoded = (0, _ocap_message.decodeAny)(encoded);
38
+ if (decoded.value && typeof decoded.value === "object") decoded.value = decodeAnyNested(decoded.value);
39
+ return decoded;
40
+ } catch (err) {
41
+ console.error("decodeAnySafe failed", err);
42
+ return null;
43
+ }
44
+ };
45
+ const applyTokenUpdates = (tokens, state, operator) => {
46
+ if (["add", "sub"].includes(operator) === false) throw new _ocap_util_lib_error.CustomError("UNEXPECTED_OPERATOR", `Invalid operator when applyTokenUpdates: ${operator}`);
47
+ if (!state) return {};
48
+ const states = (0, lodash_cloneDeep.default)((Array.isArray(state) ? state : [state]).map((x) => ({ tokens: x.tokens || {} })));
49
+ for (let i = 0; i < tokens.length; i++) {
50
+ const { address, value } = tokens[i];
51
+ const expectedDelta = new _ocap_util.BN(value);
52
+ let fulfilled = new _ocap_util.BN(0);
53
+ for (let j = 0; j < states.length; j++) {
54
+ const origin = states[j].tokens;
55
+ const updated = (0, lodash_cloneDeep.default)(origin);
56
+ const balance = new _ocap_util.BN(origin[address] || 0);
57
+ let newBalance;
58
+ if (operator === "add") {
59
+ fulfilled = fulfilled.add(expectedDelta);
60
+ newBalance = balance.add(expectedDelta);
61
+ } else {
62
+ const unfulfilledDelta = expectedDelta.sub(fulfilled);
63
+ const actualDelta = balance.lt(unfulfilledDelta) ? balance : unfulfilledDelta;
64
+ fulfilled = fulfilled.add(actualDelta);
65
+ newBalance = balance.sub(actualDelta);
66
+ }
67
+ updated[address] = newBalance.toString(10);
68
+ states[j].tokens = updated;
69
+ }
70
+ if (fulfilled.lt(expectedDelta)) throw new _ocap_util_lib_error.CustomError("INSUFFICIENT_FUND", `Negative token balance when applyTokenUpdates for ${address}, expected: ${expectedDelta.toString()} fulfilled: ${fulfilled.toString()}`);
71
+ }
72
+ return Array.isArray(state) ? states : states[0];
73
+ };
74
+ const applyTokenChange = (state, change) => {
75
+ const delta = typeof change.delta === "string" ? new _ocap_util.BN(change.delta) : change.delta;
76
+ if (delta.gt(ZERO)) return applyTokenUpdates([{
77
+ address: change.token,
78
+ value: delta.toString(10)
79
+ }], state, "add");
80
+ return applyTokenUpdates([{
81
+ address: change.token,
82
+ value: delta.abs().toString(10)
83
+ }], state, "sub");
84
+ };
85
+ const fixTokenInput = (input, config) => {
86
+ (0, _ocap_util_lib_get_list_field.getListField)(input, "tokens").forEach((t) => {
87
+ if (!t.address) t.address = config.token.address;
88
+ });
89
+ return input;
90
+ };
91
+ const getTxFee = ({ amount, feeRate, maxFee, minFee, stringify = true }) => {
92
+ const userAmount = new _ocap_util.BN(amount);
93
+ const maxFeeAmount = new _ocap_util.BN(maxFee);
94
+ const minFeeAmount = new _ocap_util.BN(minFee);
95
+ if (feeRate < 0) throw new _ocap_util_lib_error.CustomError("NEGATIVE_FEE_RATE", "Unexpected negative feeRate when getTxFee, abort!");
96
+ if (userAmount.isNeg()) throw new _ocap_util_lib_error.CustomError("NEGATIVE_AMOUNT", "Unexpected negative amount when getTxFee, abort!");
97
+ if (maxFeeAmount.isNeg()) throw new _ocap_util_lib_error.CustomError("NEGATIVE_MAX_FEE", "Unexpected negative maxFee when getTxFee, abort!");
98
+ if (minFeeAmount.isNeg()) throw new _ocap_util_lib_error.CustomError("NEGATIVE_MIN_FEE", "Unexpected negative minFee when getTxFee, abort!");
99
+ let rewardAmount = userAmount.mul(new _ocap_util.BN(feeRate)).div(RATE_BASE);
100
+ if (rewardAmount.lt(minFeeAmount)) rewardAmount = minFeeAmount;
101
+ if (rewardAmount.gt(maxFeeAmount)) rewardAmount = maxFeeAmount;
102
+ const totalAmount = userAmount.add(rewardAmount);
103
+ if (stringify) return {
104
+ total: totalAmount.toString(10),
105
+ user: userAmount.toString(10),
106
+ reward: rewardAmount.toString(10)
107
+ };
108
+ return {
109
+ total: totalAmount,
110
+ user: userAmount,
111
+ reward: rewardAmount
112
+ };
113
+ };
114
+ const getDelegationRequirements = (context) => {
115
+ const { txType, config } = context;
116
+ const txFee = config.transaction.txFee[txType];
117
+ if (!txFee) return [];
118
+ return [{
119
+ type: "token",
120
+ address: config.token.address,
121
+ value: (0, _ocap_util.fromTokenToUnit)(txFee, config.token.decimal).toString()
122
+ }];
123
+ };
124
+ const splitTxFee = ({ total, shares = {}, stringify = true, rateBase = RATE_BASE }) => {
125
+ const totalAmount = new _ocap_util.BN(total);
126
+ if (totalAmount.isNeg()) throw new _ocap_util_lib_error.CustomError("NEGATIVE_TOTAL_AMOUNT", "Unexpected negative total when splitTxFee, abort!");
127
+ Object.keys(shares).forEach((key) => {
128
+ if (shares[key] < 0) throw new _ocap_util_lib_error.CustomError("NEGATIVE_FEE_SHARE", `Unexpected negative shares[${key}] when splitTxFee, abort!`);
129
+ });
130
+ if (Object.values(shares).reduce((sum, x) => sum.add(new _ocap_util.BN(x)), new _ocap_util.BN(0)).eq(rateBase) === false) throw new _ocap_util_lib_error.CustomError("INVALID_FEE_SHARE", "Invalid share config when splitTxFee, abort!");
131
+ const rewardShares = Object.keys(shares).reduce((acc, x) => {
132
+ acc[x] = totalAmount.mul(new _ocap_util.BN(shares[x])).div(rateBase);
133
+ return acc;
134
+ }, {});
135
+ if (Object.values(rewardShares).reduce((sum, x) => sum.add(x), new _ocap_util.BN(0)).eq(totalAmount) === false) {
136
+ const firstKey = Object.keys(rewardShares)[0];
137
+ const currentSum = Object.values(rewardShares).reduce((sum, x) => sum.add(x), new _ocap_util.BN(0));
138
+ rewardShares[firstKey] = rewardShares[firstKey].add(totalAmount.sub(currentSum));
139
+ }
140
+ return Object.keys(rewardShares).reduce((acc, x) => {
141
+ acc[x] = stringify ? rewardShares[x].toString(10) : rewardShares[x];
142
+ return acc;
143
+ }, {});
144
+ };
145
+ const getRewardLocker = (rollupAddress) => (0, _arcblock_did_util.toStakeAddress)(rollupAddress, rollupAddress);
146
+ const getBNSum = (...args) => (0, lodash_flattenDeep.default)(args).reduce((sum, x) => sum.add(new _ocap_util.BN(x)), new _ocap_util.BN(0)).toString(10);
147
+ const isGovernanceTx = (x) => [
148
+ "pause_rollup",
149
+ "resume_rollup",
150
+ "migrate_rollup"
151
+ ].includes(x.type);
152
+ const isFixedFee = (x) => !x.tx.itxJson.maxFee || new _ocap_util.BN(x.tx.itxJson.maxFee).isZero();
153
+ const ensureBlockReward = (rollupState, minReward, txStates) => {
154
+ const { address, withdrawFeeRate, minWithdrawFee, maxWithdrawFee, tokenAddress } = rollupState;
155
+ const locker = getRewardLocker(address);
156
+ const result = {
157
+ mintedAmount: new _ocap_util.BN(0),
158
+ burnedAmount: new _ocap_util.BN(0),
159
+ rewardAmount: new _ocap_util.BN(0),
160
+ stakeUpdates: {},
161
+ accountUpdates: {}
162
+ };
163
+ const maxPossibleReward = txStates.reduce((sum, x) => sum.add(new _ocap_util.BN(isFixedFee(x) ? x.tx.itxJson.actualFee : x.tx.itxJson.maxFee)), new _ocap_util.BN(0));
164
+ const minRequiredReward = new _ocap_util.BN(minReward);
165
+ if (maxPossibleReward.lt(minRequiredReward)) throw new _ocap_util_lib_error.CustomError("INVALID_BLOCK", "Block reward does not match minReward requirement");
166
+ const dynamicFeeTxs = txStates.filter((x) => isGovernanceTx(x) === false && isFixedFee(x) === false);
167
+ const totalDynamicFee = dynamicFeeTxs.reduce((sum, x) => sum.add(new _ocap_util.BN(x.tx.itxJson.maxFee)), new _ocap_util.BN(0));
168
+ const totalFixedFee = txStates.filter((x) => isGovernanceTx(x) === false && isFixedFee(x)).reduce((sum, x) => sum.add(new _ocap_util.BN(x.tx.itxJson.actualFee)), new _ocap_util.BN(0));
169
+ const totalMissingFee = minRequiredReward.sub(totalFixedFee);
170
+ const minTxFee = minRequiredReward.div(new _ocap_util.BN(txStates.length));
171
+ const changes = {
172
+ stake: [],
173
+ account: []
174
+ };
175
+ dynamicFeeTxs.forEach((x) => {
176
+ const maxFee = new _ocap_util.BN(x.tx.itxJson.maxFee);
177
+ const defaults = getTxFee({
178
+ amount: x.tx.itxJson.token.value,
179
+ feeRate: withdrawFeeRate,
180
+ maxFee: maxWithdrawFee,
181
+ minFee: minWithdrawFee,
182
+ stringify: false
183
+ });
184
+ let actualFee = new _ocap_util.BN(0);
185
+ if (totalMissingFee.isNeg()) actualFee = defaults.reward;
186
+ else actualFee = totalMissingFee.mul(maxFee).div(totalDynamicFee);
187
+ if (actualFee.isNeg()) throw new _ocap_util_lib_error.CustomError("NEGATIVE_ACTUAL_FEE", "Got negative actualFee for tx, abort!");
188
+ if (actualFee.lt(defaults.reward)) actualFee = defaults.reward;
189
+ if (actualFee.lt(minTxFee)) actualFee = minTxFee;
190
+ if (actualFee.lt(maxFee)) {
191
+ const refundFee = maxFee.sub(actualFee).toString(10);
192
+ changes.account.push({
193
+ address: x.tx.from,
194
+ delta: refundFee,
195
+ action: "refund"
196
+ });
197
+ changes.stake.push({
198
+ address: locker,
199
+ delta: `-${refundFee}`,
200
+ action: "refund"
201
+ });
202
+ }
203
+ x.tx.itxJson.actualFee = actualFee.toString(10);
204
+ });
205
+ txStates.filter((x) => isGovernanceTx(x) === false).forEach((x) => {
206
+ const user = x.tx.itxJson.token.value;
207
+ const fee = x.tx.itxJson.actualFee;
208
+ const total = getBNSum(user, fee);
209
+ if (x.type === "deposit_token_v2") {
210
+ result.rewardAmount = result.rewardAmount.add(new _ocap_util.BN(fee));
211
+ result.mintedAmount = result.mintedAmount.add(new _ocap_util.BN(total));
212
+ changes.stake.push({
213
+ address: (0, _arcblock_did_util.toStakeAddress)(x.tx.itxJson.proposer, address),
214
+ delta: total,
215
+ action: "mint"
216
+ });
217
+ } else if (x.type === "withdraw_token_v2") {
218
+ result.rewardAmount = result.rewardAmount.add(new _ocap_util.BN(fee));
219
+ result.burnedAmount = result.burnedAmount.add(new _ocap_util.BN(user));
220
+ changes.stake.push({
221
+ address: (0, _arcblock_did_util.toStakeAddress)(x.tx.from, address),
222
+ delta: `-${user}`,
223
+ action: "burn"
224
+ });
225
+ }
226
+ });
227
+ const grouped = {
228
+ stake: (0, lodash_groupBy.default)(changes.stake, "address"),
229
+ account: (0, lodash_groupBy.default)(changes.account, "address")
230
+ };
231
+ result.stakeUpdates = Object.keys(grouped.stake).reduce((acc, x) => {
232
+ acc[x] = {
233
+ address: x,
234
+ token: tokenAddress,
235
+ delta: getBNSum(...grouped.stake[x].map((c) => c.delta)),
236
+ action: grouped.stake[x][grouped.stake[x].length - 1].action
237
+ };
238
+ return acc;
239
+ }, {});
240
+ result.accountUpdates = Object.keys(grouped.account).reduce((acc, x) => {
241
+ acc[x] = {
242
+ address: x,
243
+ token: tokenAddress,
244
+ delta: getBNSum(...grouped.account[x].map((c) => c.delta)),
245
+ action: grouped.account[x][grouped.account[x].length - 1].action
246
+ };
247
+ return acc;
248
+ }, {});
249
+ return {
250
+ mintedAmount: result.mintedAmount.toString(10),
251
+ burnedAmount: result.burnedAmount.toString(10),
252
+ rewardAmount: result.rewardAmount.toString(10),
253
+ stakeUpdates: result.stakeUpdates,
254
+ accountUpdates: result.accountUpdates
255
+ };
256
+ };
257
+ const isGasStakeAddress = (sender, stakeId) => (0, _arcblock_did_util.toStakeAddress)(sender, sender) === stakeId;
258
+ const isGasStakeInput = (inputs, token) => {
259
+ if (inputs.length !== 1) return false;
260
+ const [{ assetsList, tokensList }] = inputs;
261
+ if (assetsList.length > 0) return false;
262
+ if (tokensList.length !== 1) return false;
263
+ const [tokenInput] = tokensList;
264
+ if (tokenInput.address !== token) return false;
265
+ return true;
266
+ };
267
+ const isGasStakeValid = (state, config) => {
268
+ if (!state) return false;
269
+ const { token, transaction } = config;
270
+ const { minStake } = transaction.txGas;
271
+ const expectedMin = (0, _ocap_util.fromTokenToUnit)(minStake, token.decimal);
272
+ if (state.tokens) {
273
+ if (new _ocap_util.BN(state.tokens[token.address] || 0).gte(expectedMin)) return true;
274
+ }
275
+ if (state.revokedTokens) {
276
+ if (new _ocap_util.BN(state.revokedTokens[token.address] || 0).gte(expectedMin)) return true;
277
+ }
278
+ return false;
279
+ };
280
+
281
+ //#endregion
282
+ exports.RATE_BASE = RATE_BASE;
283
+ exports.applyTokenChange = applyTokenChange;
284
+ exports.applyTokenUpdates = applyTokenUpdates;
285
+ exports.decodeAnySafe = decodeAnySafe;
286
+ exports.ensureBlockReward = ensureBlockReward;
287
+ exports.fixTokenInput = fixTokenInput;
288
+ exports.getBNSum = getBNSum;
289
+ exports.getDelegationRequirements = getDelegationRequirements;
290
+ exports.getRewardLocker = getRewardLocker;
291
+ exports.getTxFee = getTxFee;
292
+ exports.isFixedFee = isFixedFee;
293
+ exports.isGasStakeAddress = isGasStakeAddress;
294
+ exports.isGasStakeInput = isGasStakeInput;
295
+ exports.isGasStakeValid = isGasStakeValid;
296
+ exports.splitTxFee = splitTxFee;
package/lib/util.d.cts ADDED
@@ -0,0 +1,141 @@
1
+ import { IChainConfig, IStakeState, TokenBalanceMap } from "@ocap/types";
2
+ import { BN } from "@ocap/util";
3
+ import { AnyData } from "@ocap/message";
4
+
5
+ //#region src/util.d.ts
6
+ /** Token update input for applyTokenUpdates */
7
+ interface ITokenUpdateInput {
8
+ address: string;
9
+ value: string;
10
+ }
11
+ /** State with token balances */
12
+ interface IStateWithTokens {
13
+ tokens?: TokenBalanceMap;
14
+ }
15
+ /** Token change input for applyTokenChange */
16
+ interface ITokenChange {
17
+ token: string;
18
+ delta: string | BN;
19
+ }
20
+ /** Tx fee calculation params */
21
+ interface ITxFeeParams {
22
+ amount: string;
23
+ feeRate: number;
24
+ maxFee: string;
25
+ minFee: string;
26
+ stringify?: boolean;
27
+ }
28
+ /** Tx fee result (stringified) */
29
+ interface ITxFeeResultString {
30
+ total: string;
31
+ user: string;
32
+ reward: string;
33
+ }
34
+ /** Tx fee result (BN) */
35
+ interface ITxFeeResultBN {
36
+ total: BN;
37
+ user: BN;
38
+ reward: BN;
39
+ }
40
+ /** Context for getDelegationRequirements */
41
+ interface IDelegationContext {
42
+ txType: string;
43
+ config: IChainConfig;
44
+ }
45
+ /** Delegation requirement */
46
+ interface IDelegationRequirement {
47
+ type: string;
48
+ address: string;
49
+ value: string;
50
+ }
51
+ /** Split tx fee params */
52
+ interface ISplitTxFeeParams {
53
+ total: string;
54
+ shares?: Record<string, number>;
55
+ stringify?: boolean;
56
+ rateBase?: BN;
57
+ }
58
+ /** Rollup state for ensureBlockReward */
59
+ interface IRollupStateForReward {
60
+ address: string;
61
+ withdrawFeeRate: number;
62
+ minWithdrawFee: string;
63
+ maxWithdrawFee: string;
64
+ tokenAddress: string;
65
+ }
66
+ /** Tx state for ensureBlockReward */
67
+ interface ITxStateForReward {
68
+ type: string;
69
+ tx: {
70
+ from: string;
71
+ itxJson: {
72
+ maxFee?: string;
73
+ actualFee?: string;
74
+ token?: {
75
+ value: string;
76
+ };
77
+ proposer?: string;
78
+ };
79
+ };
80
+ }
81
+ /** Block reward result */
82
+ interface IBlockRewardResult {
83
+ mintedAmount: string;
84
+ burnedAmount: string;
85
+ rewardAmount: string;
86
+ stakeUpdates: Record<string, {
87
+ address: string;
88
+ token: string;
89
+ delta: string;
90
+ action: string;
91
+ }>;
92
+ accountUpdates: Record<string, {
93
+ address: string;
94
+ token: string;
95
+ delta: string;
96
+ action: string;
97
+ }>;
98
+ }
99
+ /** Tx input for isGasStakeInput */
100
+ interface ITxInput {
101
+ assetsList: string[];
102
+ tokensList: Array<{
103
+ address: string;
104
+ value: string;
105
+ }>;
106
+ }
107
+ /** Token input for fixTokenInput */
108
+ interface ITokenInputForFix {
109
+ tokensList: Array<{
110
+ address?: string;
111
+ value: string;
112
+ }>;
113
+ }
114
+ declare const RATE_BASE: any;
115
+ declare const decodeAnySafe: (encoded: AnyData | null) => AnyData | null;
116
+ declare const applyTokenUpdates: (tokens: ITokenUpdateInput[], state: IStateWithTokens | IStateWithTokens[] | null | undefined, operator: "add" | "sub") => IStateWithTokens | IStateWithTokens[] | Record<string, string>;
117
+ declare const applyTokenChange: (state: IStateWithTokens | IStateWithTokens[], change: ITokenChange) => IStateWithTokens | IStateWithTokens[] | Record<string, never>;
118
+ declare const fixTokenInput: (input: ITokenInputForFix, config: IChainConfig) => ITokenInputForFix;
119
+ declare const getTxFee: ({
120
+ amount,
121
+ feeRate,
122
+ maxFee,
123
+ minFee,
124
+ stringify
125
+ }: ITxFeeParams) => ITxFeeResultString | ITxFeeResultBN;
126
+ declare const getDelegationRequirements: (context: IDelegationContext) => IDelegationRequirement[];
127
+ declare const splitTxFee: ({
128
+ total,
129
+ shares,
130
+ stringify,
131
+ rateBase
132
+ }: ISplitTxFeeParams) => Record<string, string | BN>;
133
+ declare const getRewardLocker: (rollupAddress: string) => string;
134
+ declare const getBNSum: (...args: Array<string | string[]>) => string;
135
+ declare const isFixedFee: (x: ITxStateForReward) => boolean;
136
+ declare const ensureBlockReward: (rollupState: IRollupStateForReward, minReward: string, txStates: ITxStateForReward[]) => IBlockRewardResult;
137
+ declare const isGasStakeAddress: (sender: string, stakeId: string) => boolean;
138
+ declare const isGasStakeInput: (inputs: ITxInput[], token: string) => boolean;
139
+ declare const isGasStakeValid: (state: IStakeState | null | undefined, config: IChainConfig) => boolean;
140
+ //#endregion
141
+ export { RATE_BASE, applyTokenChange, applyTokenUpdates, decodeAnySafe, ensureBlockReward, fixTokenInput, getBNSum, getDelegationRequirements, getRewardLocker, getTxFee, isFixedFee, isGasStakeAddress, isGasStakeInput, isGasStakeValid, splitTxFee };
package/package.json CHANGED
@@ -3,13 +3,38 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.28.9",
6
+ "version": "1.29.0",
7
7
  "description": "Predefined tx pipeline sets to execute certain type of transactions",
8
- "main": "lib/index.js",
8
+ "type": "module",
9
+ "main": "./lib/index.cjs",
10
+ "module": "./esm/index.mjs",
11
+ "types": "./esm/index.d.mts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./esm/index.d.mts",
15
+ "import": "./esm/index.mjs",
16
+ "default": "./lib/index.cjs"
17
+ },
18
+ "./lib/*.js": {
19
+ "types": "./esm/*.d.mts",
20
+ "import": "./esm/*.mjs",
21
+ "default": "./lib/*.cjs"
22
+ },
23
+ "./lib/*": {
24
+ "types": "./esm/*.d.mts",
25
+ "import": "./esm/*.mjs",
26
+ "default": "./lib/*.cjs"
27
+ },
28
+ "./tools/*": "./tools/*.ts"
29
+ },
9
30
  "files": [
10
- "lib"
31
+ "lib",
32
+ "esm",
33
+ "tools"
11
34
  ],
12
35
  "scripts": {
36
+ "build": "tsdown",
37
+ "prebuild": "rm -rf lib esm",
13
38
  "lint": "biome check",
14
39
  "lint:fix": "biome check --write",
15
40
  "test": "bun test",
@@ -20,25 +45,25 @@
20
45
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
21
46
  "license": "MIT",
22
47
  "dependencies": {
23
- "@arcblock/did": "1.28.9",
24
- "@arcblock/did-util": "1.28.9",
25
- "@arcblock/jwt": "1.28.9",
26
- "@arcblock/validator": "1.28.9",
27
- "@arcblock/vc": "1.28.9",
48
+ "@arcblock/did": "1.29.0",
49
+ "@arcblock/did-util": "1.29.0",
50
+ "@arcblock/jwt": "1.29.0",
51
+ "@arcblock/validator": "1.29.0",
52
+ "@arcblock/vc": "1.29.0",
28
53
  "@blocklet/xss": "^0.3.7",
29
- "@ocap/asset": "1.28.9",
30
- "@ocap/client": "1.28.9",
31
- "@ocap/mcrypto": "1.28.9",
32
- "@ocap/merkle-tree": "1.28.9",
33
- "@ocap/message": "1.28.9",
34
- "@ocap/state": "1.28.9",
35
- "@ocap/tx-pipeline": "1.28.9",
36
- "@ocap/util": "1.28.9",
37
- "@ocap/wallet": "1.28.9",
38
- "debug": "^4.3.6",
54
+ "@ocap/asset": "1.29.0",
55
+ "@ocap/client": "1.29.0",
56
+ "@ocap/mcrypto": "1.29.0",
57
+ "@ocap/merkle-tree": "1.29.0",
58
+ "@ocap/message": "1.29.0",
59
+ "@ocap/state": "1.29.0",
60
+ "@ocap/tx-pipeline": "1.29.0",
61
+ "@ocap/types": "1.29.0",
62
+ "@ocap/util": "1.29.0",
63
+ "@ocap/wallet": "1.29.0",
64
+ "debug": "^4.4.3",
39
65
  "deep-diff": "^1.0.2",
40
- "empty-value": "^1.0.1",
41
- "lodash": "^4.17.21",
66
+ "lodash": "^4.17.23",
42
67
  "url-join": "^4.0.1"
43
68
  },
44
69
  "resolutions": {
@@ -46,8 +71,10 @@
46
71
  "elliptic": "6.5.3"
47
72
  },
48
73
  "devDependencies": {
49
- "@ocap/e2e-test": "1.28.9",
50
- "@ocap/statedb-memory": "1.28.9",
74
+ "@ocap/e2e-test": "1.29.0",
75
+ "@ocap/statedb-memory": "1.29.0",
76
+ "@types/debug": "^4.1.12",
77
+ "@types/lodash": "^4.17.16",
51
78
  "start-server-and-test": "^1.14.0"
52
79
  }
53
80
  }