@ocap/tx-protocols 1.28.9 → 1.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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.js DELETED
@@ -1,442 +0,0 @@
1
- const groupBy = require('lodash/groupBy');
2
- const flattenDeep = require('lodash/flattenDeep');
3
- const { CustomError: Error } = require('@ocap/util/lib/error');
4
- const { BN, fromTokenToUnit } = require('@ocap/util');
5
- const { decodeAny } = require('@ocap/message');
6
- const { toStakeAddress } = require('@arcblock/did-util');
7
- const cloneDeep = require('lodash/cloneDeep');
8
-
9
- const ZERO = new BN(0);
10
- const RATE_BASE = new BN(10000);
11
-
12
- const decodeAnyNested = (encoded) => {
13
- if (!encoded) {
14
- return encoded;
15
- }
16
-
17
- if (Array.isArray(encoded)) {
18
- return encoded.map((k) => decodeAnyNested(k));
19
- }
20
-
21
- if (typeof encoded === 'object') {
22
- if (encoded.typeUrl && encoded.value) {
23
- const result = decodeAny(encoded);
24
- result.value = decodeAnyNested(result.value);
25
- return result;
26
- }
27
-
28
- return Object.keys(encoded).reduce((acc, x) => {
29
- acc[x] = decodeAnyNested(encoded[x]);
30
- return acc;
31
- }, {});
32
- }
33
-
34
- return encoded;
35
- };
36
-
37
- const decodeAnySafe = (encoded) => {
38
- if (!encoded) {
39
- return null;
40
- }
41
-
42
- try {
43
- const decoded = decodeAny(encoded);
44
- if (decoded.value && typeof decoded.value === 'object') {
45
- decoded.value = decodeAnyNested(decoded.value);
46
- }
47
-
48
- return decoded;
49
- } catch (err) {
50
- console.error('decodeAnySafe failed', err);
51
- return null;
52
- }
53
- };
54
-
55
- // If we are adding tokens to multiple states, same updates applied to all
56
- // If we are removing tokens from multiple states, applied sequentially
57
- const applyTokenUpdates = (tokens, state, operator) => {
58
- if (['add', 'sub'].includes(operator) === false) {
59
- throw new Error('UNEXPECTED_OPERATOR', `Invalid operator when applyTokenUpdates: ${operator}`);
60
- }
61
-
62
- if (!state) {
63
- return {};
64
- }
65
-
66
- const states = cloneDeep((Array.isArray(state) ? state : [state]).map((x) => ({ tokens: x.tokens || {} })));
67
- for (let i = 0; i < tokens.length; i++) {
68
- const { address, value } = tokens[i];
69
- const expectedDelta = new BN(value);
70
-
71
- let fulfilled = new BN(0);
72
- for (let j = 0; j < states.length; j++) {
73
- const origin = states[j].tokens;
74
- const updated = cloneDeep(origin);
75
- const balance = new BN(origin[address] || 0);
76
-
77
- let newBalance;
78
- if (operator === 'add') {
79
- fulfilled = fulfilled.add(expectedDelta);
80
- newBalance = balance.add(expectedDelta);
81
- } else {
82
- const unfulfilledDelta = expectedDelta.sub(fulfilled);
83
- const actualDelta = balance.lt(unfulfilledDelta) ? balance : unfulfilledDelta;
84
- fulfilled = fulfilled.add(actualDelta);
85
- newBalance = balance.sub(actualDelta);
86
- }
87
-
88
- updated[address] = newBalance.toString(10);
89
- states[j].tokens = updated;
90
- }
91
-
92
- if (fulfilled.lt(expectedDelta)) {
93
- throw new Error(
94
- 'INSUFFICIENT_FUND',
95
- `Negative token balance when applyTokenUpdates for ${address}, expected: ${expectedDelta.toString()} fulfilled: ${fulfilled.toString()}`
96
- );
97
- }
98
- }
99
-
100
- return Array.isArray(state) ? states : states[0];
101
- };
102
-
103
- const applyTokenChange = (state, change) => {
104
- const delta = typeof change.delta === 'string' ? new BN(change.delta) : change.delta;
105
- if (delta.gt(ZERO)) {
106
- return applyTokenUpdates([{ address: change.token, value: delta.toString(10) }], state, 'add');
107
- }
108
- return applyTokenUpdates([{ address: change.token, value: delta.abs().toString(10) }], state, 'sub');
109
- };
110
-
111
- const fixTokenInput = (input, config) => {
112
- input.tokensList.forEach((t) => {
113
- if (!t.address) {
114
- t.address = config.token.address;
115
- }
116
- });
117
-
118
- return input;
119
- };
120
-
121
- const getTxFee = ({ amount, feeRate, maxFee, minFee, stringify = true }) => {
122
- const userAmount = new BN(amount);
123
- const maxFeeAmount = new BN(maxFee);
124
- const minFeeAmount = new BN(minFee);
125
-
126
- if (feeRate < 0) {
127
- throw new Error('NEGATIVE_FEE_RATE', 'Unexpected negative feeRate when getTxFee, abort!');
128
- }
129
- if (userAmount.isNeg()) {
130
- throw new Error('NEGATIVE_AMOUNT', 'Unexpected negative amount when getTxFee, abort!');
131
- }
132
- if (maxFeeAmount.isNeg()) {
133
- throw new Error('NEGATIVE_MAX_FEE', 'Unexpected negative maxFee when getTxFee, abort!');
134
- }
135
- if (minFeeAmount.isNeg()) {
136
- throw new Error('NEGATIVE_MIN_FEE', 'Unexpected negative minFee when getTxFee, abort!');
137
- }
138
-
139
- // total fee
140
- let rewardAmount = userAmount.mul(new BN(feeRate)).div(RATE_BASE);
141
- if (rewardAmount.lt(minFeeAmount)) {
142
- rewardAmount = minFeeAmount;
143
- }
144
- if (rewardAmount.gt(maxFeeAmount)) {
145
- rewardAmount = maxFeeAmount;
146
- }
147
-
148
- // total amount
149
- const totalAmount = userAmount.add(rewardAmount);
150
-
151
- if (stringify) {
152
- return {
153
- total: totalAmount.toString(10),
154
- user: userAmount.toString(10),
155
- reward: rewardAmount.toString(10),
156
- };
157
- }
158
-
159
- return {
160
- total: totalAmount,
161
- user: userAmount,
162
- reward: rewardAmount,
163
- };
164
- };
165
-
166
- const getDelegationRequirements = (context) => {
167
- const { txType, config } = context;
168
- const txFee = config.transaction.txFee[txType];
169
- if (!txFee) {
170
- return [];
171
- }
172
-
173
- return [
174
- {
175
- type: 'token',
176
- address: config.token.address,
177
- value: fromTokenToUnit(txFee, config.token.decimal).toString(),
178
- },
179
- ];
180
- };
181
-
182
- const splitTxFee = ({ total, shares = {}, stringify = true, rateBase = RATE_BASE }) => {
183
- const totalAmount = new BN(total);
184
- if (totalAmount.isNeg()) {
185
- throw new Error('NEGATIVE_TOTAL_AMOUNT', 'Unexpected negative total when splitTxFee, abort!');
186
- }
187
- Object.keys(shares).forEach((key) => {
188
- if (shares[key] < 0) {
189
- throw new Error('NEGATIVE_FEE_SHARE', `Unexpected negative shares[${key}] when splitTxFee, abort!`);
190
- }
191
- });
192
- if (
193
- Object.values(shares)
194
- .reduce((sum, x) => sum.add(new BN(x)), new BN(0))
195
- .eq(rateBase) === false
196
- ) {
197
- throw new Error('INVALID_FEE_SHARE', 'Invalid share config when splitTxFee, abort!');
198
- }
199
-
200
- const rewardShares = Object.keys(shares).reduce((acc, x) => {
201
- acc[x] = totalAmount.mul(new BN(shares[x])).div(rateBase);
202
- return acc;
203
- }, {});
204
-
205
- // ensure sum of shares equals to total, to avoid floating point precision issue
206
- // Adjust the first share to compensate for rounding errors
207
- if (
208
- Object.values(rewardShares)
209
- .reduce((sum, x) => sum.add(x), new BN(0))
210
- .eq(totalAmount) === false
211
- ) {
212
- const firstKey = Object.keys(rewardShares)[0];
213
- const currentSum = Object.values(rewardShares).reduce((sum, x) => sum.add(x), new BN(0));
214
- rewardShares[firstKey] = rewardShares[firstKey].add(totalAmount.sub(currentSum));
215
- }
216
-
217
- return Object.keys(rewardShares).reduce((acc, x) => {
218
- acc[x] = stringify ? rewardShares[x].toString(10) : rewardShares[x];
219
- return acc;
220
- }, {});
221
- };
222
-
223
- const getRewardLocker = (rollupAddress) => toStakeAddress(rollupAddress, rollupAddress);
224
- const getBNSum = (...args) =>
225
- flattenDeep(args)
226
- .reduce((sum, x) => sum.add(new BN(x)), new BN(0))
227
- .toString(10);
228
- const isGovernanceTx = (x) => ['pause_rollup', 'resume_rollup', 'migrate_rollup'].includes(x.type);
229
- const isFixedFee = (x) => !x.tx.itxJson.maxFee || new BN(x.tx.itxJson.maxFee).isZero();
230
-
231
- const ensureBlockReward = (rollupState, minReward, txStates) => {
232
- const { address, withdrawFeeRate, minWithdrawFee, maxWithdrawFee, tokenAddress } = rollupState;
233
- const locker = getRewardLocker(address);
234
-
235
- const result = {
236
- mintedAmount: new BN(0),
237
- burnedAmount: new BN(0),
238
- rewardAmount: new BN(0),
239
- };
240
-
241
- // 0. ensure reward requirement
242
- const maxPossibleReward = txStates.reduce(
243
- (sum, x) => sum.add(new BN(isFixedFee(x) ? x.tx.itxJson.actualFee : x.tx.itxJson.maxFee)),
244
- new BN(0)
245
- );
246
- const minRequiredReward = new BN(minReward);
247
- if (maxPossibleReward.lt(minRequiredReward)) {
248
- throw new Error('INVALID_BLOCK', 'Block reward does not match minReward requirement');
249
- }
250
-
251
- // 1. find dynamic reward tx
252
- const dynamicFeeTxs = txStates.filter((x) => isGovernanceTx(x) === false && isFixedFee(x) === false);
253
- const totalDynamicFee = dynamicFeeTxs.reduce((sum, x) => sum.add(new BN(x.tx.itxJson.maxFee)), new BN(0));
254
-
255
- const fixedFeeTxs = txStates.filter((x) => isGovernanceTx(x) === false && isFixedFee(x));
256
- const totalFixedFee = fixedFeeTxs.reduce((sum, x) => sum.add(new BN(x.tx.itxJson.actualFee)), new BN(0));
257
-
258
- const totalMissingFee = minRequiredReward.sub(totalFixedFee);
259
-
260
- // 2. calculate actual reward for each dynamic reward tx, mark tx to be updated
261
- const minTxFee = minRequiredReward.div(new BN(txStates.length));
262
- const changes = { stake: [], account: [] };
263
- dynamicFeeTxs.forEach((x) => {
264
- const maxFee = new BN(x.tx.itxJson.maxFee);
265
- const defaults = getTxFee({
266
- amount: x.tx.itxJson.token.value,
267
- feeRate: withdrawFeeRate,
268
- maxFee: maxWithdrawFee,
269
- minFee: minWithdrawFee,
270
- stringify: false,
271
- });
272
-
273
- let actualFee = new BN(0);
274
- // If totalMissingFee is less than 0, then the tx will be charged for fixedFee
275
- if (totalMissingFee.isNeg()) {
276
- actualFee = defaults.reward;
277
- } else {
278
- // Else the tx is charged for a portion of totalMissingFee
279
- actualFee = totalMissingFee.mul(maxFee).div(totalDynamicFee);
280
- }
281
-
282
- if (actualFee.isNeg()) {
283
- throw new Error('NEGATIVE_ACTUAL_FEE', 'Got negative actualFee for tx, abort!');
284
- }
285
-
286
- // If the actualFee is less than default fee, user will be charged the default fee
287
- if (actualFee.lt(defaults.reward)) {
288
- actualFee = defaults.reward;
289
- }
290
-
291
- // If the actualFee is less than minTxFee, user will be charged the minTxFee
292
- if (actualFee.lt(minTxFee)) {
293
- actualFee = minTxFee;
294
- }
295
-
296
- // If the actualFee is less than the maxFee, user will have a refund
297
- if (actualFee.lt(maxFee)) {
298
- const refundFee = maxFee.sub(actualFee).toString(10);
299
- changes.account.push({
300
- address: x.tx.from,
301
- delta: refundFee,
302
- action: 'refund',
303
- });
304
- changes.stake.push({
305
- address: locker,
306
- delta: `-${refundFee}`,
307
- action: 'refund',
308
- });
309
- }
310
-
311
- x.tx.itxJson.actualFee = actualFee.toString(10);
312
- });
313
-
314
- // 3. bur/mint tokens, update stakes
315
- txStates
316
- .filter((x) => isGovernanceTx(x) === false)
317
- .forEach((x) => {
318
- const user = x.tx.itxJson.token.value;
319
- const fee = x.tx.itxJson.actualFee;
320
- const total = getBNSum(user, fee);
321
-
322
- if (x.type === 'deposit_token_v2') {
323
- result.rewardAmount = result.rewardAmount.add(new BN(fee));
324
- result.mintedAmount = result.mintedAmount.add(new BN(total));
325
-
326
- // mint tokens for deposit proposer
327
- changes.stake.push({
328
- address: toStakeAddress(x.tx.itxJson.proposer, address),
329
- delta: total,
330
- action: 'mint',
331
- });
332
- } else if (x.type === 'withdraw_token_v2') {
333
- result.rewardAmount = result.rewardAmount.add(new BN(fee));
334
- result.burnedAmount = result.burnedAmount.add(new BN(user));
335
-
336
- // burn tokens from locked withdraws: user amount
337
- changes.stake.push({
338
- address: toStakeAddress(x.tx.from, address),
339
- delta: `-${user}`,
340
- action: 'burn',
341
- });
342
- }
343
- });
344
-
345
- const grouped = {
346
- stake: groupBy(changes.stake, 'address'),
347
- account: groupBy(changes.account, 'address'),
348
- };
349
-
350
- result.stakeUpdates = Object.keys(grouped.stake).reduce((acc, x) => {
351
- acc[x] = {
352
- address: x,
353
- token: tokenAddress,
354
- delta: getBNSum(...grouped.stake[x].map((c) => c.delta)),
355
- action: grouped.stake[x][grouped.stake[x].length - 1].action,
356
- };
357
-
358
- return acc;
359
- }, {});
360
-
361
- result.accountUpdates = Object.keys(grouped.account).reduce((acc, x) => {
362
- acc[x] = {
363
- address: x,
364
- token: tokenAddress,
365
- delta: getBNSum(...grouped.account[x].map((c) => c.delta)),
366
- action: grouped.account[x][grouped.account[x].length - 1].action,
367
- };
368
-
369
- return acc;
370
- }, {});
371
-
372
- result.mintedAmount = result.mintedAmount.toString(10);
373
- result.burnedAmount = result.burnedAmount.toString(10);
374
- result.rewardAmount = result.rewardAmount.toString(10);
375
-
376
- return result;
377
- };
378
-
379
- const isGasStakeAddress = (sender, stakeId) => toStakeAddress(sender, sender) === stakeId;
380
- const isGasStakeInput = (inputs, token) => {
381
- if (inputs.length !== 1) {
382
- return false;
383
- }
384
-
385
- const [{ assetsList, tokensList }] = inputs;
386
- if (assetsList.length > 0) {
387
- return false;
388
- }
389
- if (tokensList.length !== 1) {
390
- return false;
391
- }
392
-
393
- const [tokenInput] = tokensList;
394
- if (tokenInput.address !== token) {
395
- return false;
396
- }
397
-
398
- return true;
399
- };
400
- const isGasStakeValid = (state, config) => {
401
- if (!state) {
402
- return false;
403
- }
404
-
405
- const { token, transaction } = config;
406
- const { minStake } = transaction.txGas;
407
- const expectedMin = fromTokenToUnit(minStake, token.decimal);
408
-
409
- if (state.tokens) {
410
- const actualStaked = new BN(state.tokens[token.address] || 0);
411
- if (actualStaked.gte(expectedMin)) {
412
- return true;
413
- }
414
- }
415
-
416
- if (state.revokedTokens) {
417
- const actualRevoked = new BN(state.revokedTokens[token.address] || 0);
418
- if (actualRevoked.gte(expectedMin)) {
419
- return true;
420
- }
421
- }
422
-
423
- return false;
424
- };
425
-
426
- module.exports = {
427
- decodeAnySafe,
428
- applyTokenUpdates,
429
- applyTokenChange,
430
- fixTokenInput,
431
- getTxFee,
432
- splitTxFee,
433
- getRewardLocker,
434
- ensureBlockReward,
435
- getBNSum,
436
- isFixedFee,
437
- isGasStakeAddress,
438
- isGasStakeInput,
439
- isGasStakeValid,
440
- RATE_BASE,
441
- getDelegationRequirements,
442
- };