@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/execute.js DELETED
@@ -1,254 +0,0 @@
1
- const get = require('lodash/get');
2
- const pick = require('lodash/pick');
3
- const merge = require('lodash/merge');
4
- const camelCase = require('lodash/camelCase');
5
- const { CustomError: Error } = require('@ocap/util/lib/error');
6
- const { Runner, pipes } = require('@ocap/tx-pipeline');
7
-
8
- const createEnsureGasFn = require('./pipes/ensure-gas');
9
- const createEnsureCostFn = require('./pipes/ensure-cost');
10
-
11
- const ensureTxGas = createEnsureGasFn(() => ({ create: 0, update: 2 }));
12
- const ensureTxCost = createEnsureCostFn({ attachSenderChanges: true, gasOnly: true });
13
-
14
- const getTxName = (typeUrl) => camelCase(typeUrl.split(':').pop());
15
-
16
- const flushEvents = (context, extra = {}) => {
17
- (context.events || []).forEach((e) => {
18
- context.statedb[e.table].emit(e.name, e.data, { ...e.ctx, ...extra });
19
- });
20
- context.events = [];
21
- };
22
-
23
- const shouldPersistTx = (err) => !err || get(err, 'props.persist');
24
-
25
- module.exports = ({ filter, runAsLambda }) => {
26
- // pipeline before executing the transaction
27
- const pre = new Runner();
28
- pre.use(pipes.DecodeTx);
29
- pre.use(pipes.VerifyBlocked({ filter, stateKeys: ['tx.from'] }));
30
- pre.use(pipes.DecodeItx);
31
- pre.use(pipes.VerifyTx);
32
- pre.use(pipes.VerifyServiceFee);
33
-
34
- pre.use(async (context, next) => {
35
- if (filter.has(context.txHash)) {
36
- // Since bloom-filters may give false positive, we need to check for statedb if replay detected
37
- const exist = await context.statedb.tx.get(context.txHash, context);
38
- if (exist) {
39
- return next(new Error('DUPLICATE_TX', 'Can not execute duplicate transaction'));
40
- }
41
- }
42
-
43
- filter.add(context.txHash);
44
- return next();
45
- });
46
-
47
- pre.use(pipes.VerifyTxSize);
48
- pre.use(pipes.VerifySignature);
49
-
50
- // charge gas fee for errored tx
51
- const ensureGasFeePaid = (context) => {
52
- const gasRunner = new Runner();
53
-
54
- gasRunner.use(ensureTxGas);
55
- gasRunner.use(ensureTxCost);
56
- gasRunner.use(async (ctx, next) => {
57
- const { tx, statedb, senderUpdates, updateVaults } = ctx;
58
-
59
- // If senderState does not exist and there is no stakeId for gas, an error will be thrown in ensureTxCost before here.
60
- // So if there is no senderState here, it means there has a stake for gas, so allowed to continue.
61
- if (ctx.senderState) {
62
- const senderState = ctx.stateSnapshot?.[ctx.senderState.address] || ctx.senderState;
63
- await statedb.account.update(
64
- senderState.address,
65
- ctx.states.account.update(senderState, { nonce: tx.nonce, pk: tx.pk, ...senderUpdates }, ctx),
66
- ctx
67
- );
68
- await updateVaults();
69
- }
70
-
71
- return next();
72
- });
73
-
74
- return new Promise((resolve, reject) => {
75
- gasRunner.run(context, (err) => {
76
- if (err) {
77
- context.logger?.error('Failed to charge gas fee for errored tx', {
78
- error: err,
79
- tx: context.tx,
80
- txBase64: context.txBase64,
81
- txHash: context.txHash,
82
- });
83
- reject(err);
84
- return;
85
- }
86
-
87
- resolve();
88
- });
89
- });
90
- };
91
-
92
- const execute = async (context, protocols, isRetrySupported = false) =>
93
- new Promise((resolve, reject) => {
94
- pre.run(context, (err) => {
95
- if (err) {
96
- context.logger?.error('Failed to prepare transaction', {
97
- error: err,
98
- txHash: context.txHash,
99
- request: context.extra?.request,
100
- });
101
- return reject(err);
102
- }
103
-
104
- const name = getTxName(context.txType);
105
- const protocol = protocols[name];
106
- if (!protocol) {
107
- return reject(new Error('UNSUPPORTED_TX', `Unsupported tx type ${context.txType}`));
108
- }
109
-
110
- protocol.run(context, async (error) => {
111
- if (isRetrySupported) {
112
- if (error) {
113
- return reject(error);
114
- }
115
- return resolve(context);
116
- }
117
-
118
- const txStatus = error ? error.code || 'INTERNAL' : 'OK';
119
- let txState = context.states.tx.create(context, txStatus);
120
-
121
- if (shouldPersistTx(error)) {
122
- try {
123
- if (error) {
124
- await ensureGasFeePaid(context);
125
- // Recreate tx to pick up gas related fields
126
- txState = context.states.tx.create(context, txStatus);
127
- }
128
-
129
- await context.statedb.tx.create(txState.hash, txState, context);
130
- flushEvents(context, { txState });
131
- } catch (e) {
132
- context.logger?.error('Failed to save transaction to statedb', {
133
- error: e,
134
- txError: error,
135
- txHash: context.txHash,
136
- txState,
137
- });
138
- // If we get an internal error here, should return the original tx error to client
139
- return reject(error);
140
- }
141
- }
142
-
143
- context.logger?.info('Tx finalized', {
144
- txHash: context.txHash,
145
- txStatus,
146
- txState,
147
- error,
148
- });
149
-
150
- if (error) {
151
- return reject(error);
152
- }
153
- return resolve(context);
154
- });
155
- });
156
- });
157
-
158
- if (typeof runAsLambda === 'function') {
159
- return async (context, protocols) => {
160
- let ctx = context;
161
- const startTime = Date.now();
162
-
163
- try {
164
- const txState = await runAsLambda(
165
- async (txn) => {
166
- // create a new context each time in case we are retrying
167
- ctx = pick(context, ['txBase64', 'statedb', 'indexdb', 'config', 'states', 'filter', 'extra', 'logger']);
168
- ctx.txn = txn;
169
-
170
- await execute(ctx, protocols, true);
171
-
172
- const state = context.states.tx.create(ctx, 'OK');
173
- await context.statedb.tx.create(state.hash, state, ctx);
174
-
175
- return state;
176
- },
177
- { cleanWorkingSet: true, verifyHash: true, context: ctx }
178
- );
179
-
180
- // update indexdb after statedb commit
181
- flushEvents(ctx, { txState });
182
-
183
- ctx.logger?.info('Tx finalized', {
184
- txHash: ctx.txHash,
185
- txState,
186
- txStatus: 'OK',
187
- duration: Date.now() - startTime,
188
- });
189
- } catch (error) {
190
- const txStatus = error.code || 'INTERNAL';
191
- let txState = ctx.tx ? context.states.tx.create(ctx, txStatus, false) : null;
192
-
193
- ctx.logger?.error('Failed to execute transaction', {
194
- error,
195
- txHash: ctx.txHash,
196
- txState,
197
- request: context.extra?.request,
198
- });
199
-
200
- if (txState && shouldPersistTx(error)) {
201
- // Avoid sync data in failed transactions to indexdb
202
- ctx.events = [];
203
-
204
- try {
205
- txState = await runAsLambda(
206
- async (txn) => {
207
- ctx = Object.assign({}, ctx, { txn, cacheStates: null });
208
-
209
- await ensureGasFeePaid(ctx);
210
-
211
- // Recreate tx to pick up gas related fields
212
- const state = context.states.tx.create(ctx, txStatus);
213
- await ctx.statedb.tx.create(state.hash, state, ctx);
214
-
215
- return state;
216
- },
217
- { cleanWorkingSet: true, verifyHash: true, context: ctx }
218
- );
219
-
220
- // update indexdb after statedb commit
221
- flushEvents(ctx, { txState });
222
-
223
- ctx.logger?.info('Tx finalized', {
224
- txHash: ctx.txHash,
225
- txState,
226
- txStatus,
227
- error,
228
- duration: Date.now() - startTime,
229
- });
230
- } catch (err) {
231
- ctx.logger?.error('Failed to save invalid transaction to statedb', {
232
- error: err,
233
- txError: error,
234
- txHash: ctx.txHash,
235
- txState,
236
- request: context.extra?.request,
237
- });
238
- // If we get an error here, should return the original tx error to client
239
- throw error;
240
- }
241
- }
242
-
243
- throw error;
244
- } finally {
245
- // Merge new ctx to original context
246
- merge(context, ctx);
247
- }
248
-
249
- return ctx;
250
- };
251
- }
252
-
253
- return execute;
254
- };
package/lib/index.js DELETED
@@ -1,117 +0,0 @@
1
- const states = require('@ocap/state');
2
-
3
- const transferV2 = require('./protocols/trade/transfer-v2');
4
- const transferV3 = require('./protocols/trade/transfer-v3');
5
- const exchangeV2 = require('./protocols/trade/exchange-v2');
6
- const migrate = require('./protocols/account/migrate');
7
- const delegate = require('./protocols/account/delegate');
8
- const revokeDelegate = require('./protocols/account/revoke-delegate');
9
- const createAsset = require('./protocols/asset/create');
10
- const updateAsset = require('./protocols/asset/update');
11
- const consumeAsset = require('./protocols/asset/consume');
12
- const createFactory = require('./protocols/factory/create');
13
- const acquireAssetV2 = require('./protocols/asset/acquire-v2');
14
- const acquireAssetV3 = require('./protocols/asset/acquire-v3');
15
- const mintAsset = require('./protocols/asset/mint');
16
- const createToken = require('./protocols/token/create');
17
- const stake = require('./protocols/governance/stake');
18
- const revokeStake = require('./protocols/governance/revoke-stake');
19
- const returnStake = require('./protocols/governance/return-stake');
20
- const claimStake = require('./protocols/governance/claim-stake');
21
- const slashStake = require('./protocols/governance/slash-stake');
22
- const depositTokenV2 = require('./protocols/token/deposit-v2');
23
- const withdrawTokenV2 = require('./protocols/token/withdraw-v2');
24
- const createRollup = require('./protocols/rollup/create');
25
- const updateRollup = require('./protocols/rollup/update');
26
- const joinRollup = require('./protocols/rollup/join');
27
- const leaveRollup = require('./protocols/rollup/leave');
28
- const pauseRollup = require('./protocols/rollup/pause');
29
- const closeRollup = require('./protocols/rollup/close');
30
- const resumeRollup = require('./protocols/rollup/resume');
31
- const createRollupBlock = require('./protocols/rollup/create-block');
32
- const claimBlockReward = require('./protocols/rollup/claim-reward');
33
- const migrateRollup = require('./protocols/rollup/migrate');
34
- const createTokenFactory = require('./protocols/token-factory/create');
35
- const updateTokenFactory = require('./protocols/token-factory/update');
36
- const mintToken = require('./protocols/token-factory/mint');
37
- const burnToken = require('./protocols/token-factory/burn');
38
-
39
- const executor = require('./execute');
40
-
41
- const createExecutor = ({ filter, runAsLambda }) => {
42
- const protocols = {
43
- // trade
44
- transfer: transferV2,
45
- transferV2,
46
- transferV3,
47
- exchangeV2,
48
-
49
- // account
50
- accountMigrate: migrate,
51
- delegate,
52
- revokeDelegate,
53
-
54
- // asset
55
- createAsset,
56
- updateAsset,
57
- consumeAsset,
58
- acquireAssetV2,
59
- acquireAssetV3,
60
- mintAsset,
61
-
62
- // factory
63
- createFactory,
64
-
65
- // token
66
- createToken,
67
- depositTokenV2,
68
- withdrawTokenV2,
69
- createTokenFactory,
70
- updateTokenFactory,
71
- mintToken,
72
- burnToken,
73
-
74
- // governance
75
- stake,
76
- revokeStake,
77
- claimStake,
78
- slashStake,
79
- returnStake,
80
-
81
- // rollup
82
- createRollup,
83
- updateRollup,
84
- joinRollup,
85
- leaveRollup,
86
- pauseRollup,
87
- closeRollup,
88
- resumeRollup,
89
- createRollupBlock,
90
- claimBlockReward,
91
- migrateRollup,
92
- };
93
-
94
- const execute = executor({ filter, runAsLambda });
95
-
96
- return {
97
- ...protocols,
98
-
99
- execute: (context, done) => {
100
- Object.defineProperty(context, 'states', { value: states });
101
-
102
- if (typeof done === 'function') {
103
- return execute(context, protocols)
104
- .then((res) => done(null, res))
105
- .catch((err) => done(err));
106
- }
107
-
108
- return execute(context, protocols);
109
- },
110
- };
111
- };
112
-
113
- // default in-memory tx-hash-filter
114
- const filter = new states.Blacklist();
115
-
116
- module.exports = createExecutor({ filter });
117
- module.exports.createExecutor = createExecutor;
@@ -1,193 +0,0 @@
1
- const noop = require('lodash/noop');
2
- const { CustomError: Error } = require('@ocap/util/lib/error');
3
- const { fromTokenToUnit, BN, fromUnitToToken, hexToNumber } = require('@ocap/util');
4
- const JWT = require('@arcblock/jwt');
5
- const { account } = require('@ocap/state');
6
- const { toAddress } = require('@arcblock/did');
7
- const { toStakeAddress } = require('@arcblock/did-util');
8
-
9
- const debug = require('debug')(`${require('../../package.json').name}:pipes:ensure-cost`);
10
-
11
- const { applyTokenUpdates, isGasStakeValid } = require('../util');
12
-
13
- const ZERO = new BN(0);
14
-
15
- // We have a layered transaction cost design
16
- // - gas: charged for every tx for creating/updating states on the ledger
17
- // - service fee: charged for creating tokens/assets/factories/rollups
18
- // - protocol fee: charged for moving tokens across the bridge
19
- module.exports = function CreateEnsureTxCostPipe({
20
- attachSenderChanges = true,
21
- throwOnInsufficientFund = true, // In some cases, we can tolerance insufficient fund, such as account migration
22
- gasOnly = false,
23
- } = {}) {
24
- return async function EnsureTxCost(context, next) {
25
- // TODO: we are using the sender as gas payer, this may change in future
26
- const { config, statedb, txType, senderState, gasEstimate, totalGas } = context;
27
-
28
- // verify gas staking headers
29
- const { tx, extra = {}, txHash } = context;
30
- const { token, pk } = extra.gasStakeHeaders || {};
31
- const gasStake = {};
32
- if (token && pk && (await JWT.verify(token, pk, { enforceTimestamp: true, tolerance: 5 }))) {
33
- const decoded = JWT.decode(token);
34
- const txHashes = (decoded.txHash || '')
35
- .split(',')
36
- .map((x) => x.trim())
37
- .filter(Boolean);
38
- if (txHashes.includes(txHash)) {
39
- gasStake.owner = toAddress(decoded.iss);
40
- gasStake.stakeId = toStakeAddress(gasStake.owner, gasStake.owner);
41
- } else {
42
- console.warn(`Invalid gas payer header for: ${txHash}`);
43
- }
44
- }
45
- // fallback to tx sender
46
- if (!gasStake.owner) {
47
- gasStake.owner = tx.from;
48
- gasStake.stakeId = toStakeAddress(gasStake.owner, gasStake.owner);
49
- }
50
- gasStake.state = await statedb.stake.get(gasStake.stakeId, context);
51
- gasStake.valid = isGasStakeValid(gasStake.state, config);
52
- context.gasStake = gasStake;
53
- debug('gasStake', gasStake, context.extra);
54
-
55
- // Get service fee and gas fee
56
- const changes = {};
57
- let txCost = new BN(0);
58
- const txFee = config.transaction.txFee[txType];
59
- // always charge tx fee
60
- if (!gasOnly && txFee) {
61
- const totalFee = fromTokenToUnit(txFee, config.token.decimal);
62
- txCost = txCost.add(totalFee);
63
- changes.fee = { address: config.token.address, value: totalFee.toString(10) };
64
- context.feeVaultChange = changes.fee;
65
- }
66
- // only charge gas when gas stake is not valid
67
- if (!gasStake.valid && totalGas.gt(ZERO)) {
68
- txCost = txCost.add(totalGas);
69
- changes.gas = { address: config.token.address, value: totalGas.toString(10) };
70
- context.gasVaultChange = changes.gas;
71
- }
72
-
73
- // pick fee vault
74
- if (changes.fee) {
75
- if (config.vaults.txFees?.length) {
76
- const feeIndex = hexToNumber(txHash.slice(-8)) % config.vaults.txFees.length;
77
- const feeVault = config.vaults.txFees[feeIndex];
78
- context.feeVault = feeVault;
79
- } else {
80
- context.feeVault = config.vaults.txFee;
81
- }
82
- }
83
-
84
- let isCostCharged = false;
85
- if (txCost.gt(ZERO)) {
86
- const expected = new BN(gasEstimate.payment || 0).add(txCost);
87
- const actual = new BN(senderState?.tokens?.[config.token.address] || 0);
88
- // If we have someone with enough balance to pay for this tx
89
- if (senderState && actual.gte(expected)) {
90
- isCostCharged = true;
91
-
92
- // to be merged into later pipe
93
- context.senderUpdates = applyTokenUpdates(
94
- [{ address: config.token.address, value: txCost.toString(10) }],
95
- senderState,
96
- 'sub'
97
- );
98
- context.senderChange = {
99
- address: senderState.address,
100
- token: config.token.address,
101
- delta: `-${txCost.toString(10)}`,
102
- };
103
-
104
- debug({ changes, senderUpdates: context.senderUpdates });
105
-
106
- // to be called in later pipes
107
- context.updateVaults = async function updateVaults() {
108
- const [feeVaultState, gasVaultState] = await Promise.all([
109
- changes.fee ? statedb.account.get(context.feeVault, context) : null,
110
- changes.gas ? statedb.account.get(context.gasVault, context) : null,
111
- ]);
112
-
113
- const [newFeeVaultState, newGasVaultState] = await Promise.all([
114
- changes.fee
115
- ? statedb.account.update(
116
- feeVaultState.address,
117
- account.update(feeVaultState, applyTokenUpdates([changes.fee], feeVaultState, 'add'), context),
118
- context
119
- )
120
- : null,
121
- changes.gas
122
- ? statedb.account.update(
123
- gasVaultState.address,
124
- account.update(gasVaultState, applyTokenUpdates([changes.gas], gasVaultState, 'add'), context),
125
- context
126
- )
127
- : null,
128
- ]);
129
-
130
- if (newFeeVaultState) context.feeVaultState = newFeeVaultState;
131
- if (newGasVaultState) context.gasVaultState = newGasVaultState;
132
-
133
- context.updatedAccounts = context.updatedAccounts || [];
134
- if (changes.fee) {
135
- context.updatedAccounts.push({
136
- address: feeVaultState.address,
137
- token: config.token.address,
138
- delta: changes.fee.value,
139
- action: 'fee',
140
- });
141
- if (attachSenderChanges) {
142
- context.updatedAccounts.push({
143
- address: senderState.address,
144
- token: config.token.address,
145
- delta: `-${changes.fee.value}`,
146
- action: 'fee',
147
- });
148
- }
149
- }
150
- if (changes.gas) {
151
- context.updatedAccounts.push({
152
- address: gasVaultState.address,
153
- token: config.token.address,
154
- delta: changes.gas.value,
155
- action: 'gas',
156
- });
157
- if (attachSenderChanges) {
158
- context.updatedAccounts.push({
159
- address: senderState.address,
160
- token: config.token.address,
161
- delta: `-${changes.gas.value}`,
162
- action: 'gas',
163
- });
164
- }
165
- }
166
-
167
- context.gasPaid = true;
168
- };
169
- } else if (!senderState) {
170
- return next(new Error('INVALID_GAS_PAYER', `Gas payer ${tx.from} does not exist on chain`));
171
- } else if (throwOnInsufficientFund) {
172
- return next(
173
- new Error(
174
- 'INSUFFICIENT_FUND',
175
- `Insufficient fund to pay for tx cost from ${senderState?.address || tx.from}, expected ${fromUnitToToken(
176
- expected,
177
- config.token.decimal
178
- )} ${config.token.symbol}, got ${fromUnitToToken(actual, config.token.decimal)} ${config.token.symbol}`
179
- )
180
- );
181
- }
182
- }
183
-
184
- if (!isCostCharged) {
185
- context.senderUpdates = {};
186
- context.senderChange = null;
187
- context.updateVaults = noop;
188
- context.updatedAccounts = [];
189
- }
190
-
191
- return next();
192
- };
193
- };
@@ -1,48 +0,0 @@
1
- const { BN, hexToNumber } = require('@ocap/util');
2
-
3
- const debug = require('debug')(`${require('../../package.json').name}:pipes:ensure-gas`);
4
-
5
- // Must be called after all state ready
6
- // Must be called before creating/updating any state
7
- // estimateTxGas should be a function that returns the following data:
8
- // - create: state creating ops
9
- // - update: state updating ops
10
- // - payment: sender payment amount of the gas token
11
- module.exports = function CreateGasEnsureFn(estimateTxGas) {
12
- return function EnsureTxGas(context, next) {
13
- const { txHash, config } = context;
14
-
15
- // total gas in native token
16
- const { txGas } = config.transaction;
17
- const basePrice = new BN(txGas.price);
18
-
19
- const estimate = estimateTxGas(context);
20
- let totalGas = new BN(0);
21
- if (context.txBaseGas) {
22
- totalGas = basePrice.mul(new BN(txGas.dataStorage)).mul(new BN(context.txSize));
23
- totalGas = totalGas.add(basePrice.mul(new BN(txGas.createState)).mul(new BN(estimate.create + 1))); // 1 = tx insert
24
- totalGas = totalGas.add(basePrice.mul(new BN(txGas.updateState)).mul(new BN(estimate.update)));
25
- }
26
-
27
- // gas receiver address
28
- const { txGas: gasVaults } = config.vaults;
29
- const gasIndex = hexToNumber(txHash.slice(-8)) % gasVaults.length;
30
- const gasVault = gasVaults[gasIndex];
31
- context.gasVault = gasVault;
32
-
33
- debug({
34
- txType: context.txType,
35
- txHash: context.txHash,
36
- txSize: context.txSize,
37
- txBaseGas: context.txBaseGas,
38
- totalGas: totalGas.toString(10),
39
- gasVault,
40
- estimate,
41
- });
42
-
43
- context.totalGas = totalGas;
44
- context.gasEstimate = estimate;
45
-
46
- return next();
47
- };
48
- };