@ocap/tx-protocols 1.28.8 → 1.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (284) hide show
  1. package/esm/execute.d.mts +53 -0
  2. package/esm/execute.mjs +225 -0
  3. package/esm/index.d.mts +95 -0
  4. package/esm/index.mjs +100 -0
  5. package/esm/pipes/ensure-cost.d.mts +18 -0
  6. package/esm/pipes/ensure-cost.mjs +136 -0
  7. package/esm/pipes/ensure-gas.d.mts +14 -0
  8. package/esm/pipes/ensure-gas.mjs +38 -0
  9. package/esm/protocols/account/delegate.d.mts +4 -0
  10. package/esm/protocols/account/delegate.mjs +195 -0
  11. package/esm/protocols/account/migrate.d.mts +4 -0
  12. package/esm/protocols/account/migrate.mjs +130 -0
  13. package/esm/protocols/account/revoke-delegate.d.mts +4 -0
  14. package/esm/protocols/account/revoke-delegate.mjs +102 -0
  15. package/esm/protocols/asset/acquire-v2.d.mts +9 -0
  16. package/esm/protocols/asset/acquire-v2.mjs +213 -0
  17. package/esm/protocols/asset/acquire-v3.d.mts +17 -0
  18. package/esm/protocols/asset/acquire-v3.mjs +265 -0
  19. package/esm/protocols/asset/calls/transfer-token.d.mts +4 -0
  20. package/esm/protocols/asset/calls/transfer-token.mjs +36 -0
  21. package/esm/protocols/asset/calls/transfer.d.mts +4 -0
  22. package/esm/protocols/asset/calls/transfer.mjs +32 -0
  23. package/esm/protocols/asset/consume.d.mts +4 -0
  24. package/esm/protocols/asset/consume.mjs +92 -0
  25. package/esm/protocols/asset/create.d.mts +4 -0
  26. package/esm/protocols/asset/create.mjs +136 -0
  27. package/esm/protocols/asset/mint.d.mts +4 -0
  28. package/esm/protocols/asset/mint.mjs +156 -0
  29. package/esm/protocols/asset/pipes/exec-mint-hook.d.mts +24 -0
  30. package/esm/protocols/asset/pipes/exec-mint-hook.mjs +54 -0
  31. package/esm/protocols/asset/pipes/extract-factory-tokens.d.mts +21 -0
  32. package/esm/protocols/asset/pipes/extract-factory-tokens.mjs +22 -0
  33. package/esm/protocols/asset/pipes/verify-itx-address.d.mts +32 -0
  34. package/esm/protocols/asset/pipes/verify-itx-address.mjs +56 -0
  35. package/esm/protocols/asset/pipes/verify-itx-assets.d.mts +18 -0
  36. package/esm/protocols/asset/pipes/verify-itx-assets.mjs +27 -0
  37. package/esm/protocols/asset/pipes/verify-itx-variables.d.mts +17 -0
  38. package/esm/protocols/asset/pipes/verify-itx-variables.mjs +20 -0
  39. package/esm/protocols/asset/pipes/verify-mint-limit.d.mts +15 -0
  40. package/esm/protocols/asset/pipes/verify-mint-limit.mjs +14 -0
  41. package/esm/protocols/asset/update.d.mts +4 -0
  42. package/esm/protocols/asset/update.mjs +111 -0
  43. package/esm/protocols/factory/create.d.mts +29 -0
  44. package/esm/protocols/factory/create.mjs +155 -0
  45. package/esm/protocols/governance/claim-stake.d.mts +27 -0
  46. package/esm/protocols/governance/claim-stake.mjs +220 -0
  47. package/esm/protocols/governance/return-stake.d.mts +27 -0
  48. package/esm/protocols/governance/return-stake.mjs +211 -0
  49. package/esm/protocols/governance/revoke-stake.d.mts +27 -0
  50. package/esm/protocols/governance/revoke-stake.mjs +178 -0
  51. package/esm/protocols/governance/slash-stake.d.mts +17 -0
  52. package/esm/protocols/governance/slash-stake.mjs +213 -0
  53. package/esm/protocols/governance/stake.d.mts +15 -0
  54. package/esm/protocols/governance/stake.mjs +270 -0
  55. package/esm/protocols/rollup/claim-reward.d.mts +11 -0
  56. package/esm/protocols/rollup/claim-reward.mjs +322 -0
  57. package/esm/protocols/rollup/close.d.mts +4 -0
  58. package/esm/protocols/rollup/close.mjs +105 -0
  59. package/esm/protocols/rollup/create-block.d.mts +11 -0
  60. package/esm/protocols/rollup/create-block.mjs +303 -0
  61. package/esm/protocols/rollup/create.d.mts +4 -0
  62. package/esm/protocols/rollup/create.mjs +164 -0
  63. package/esm/protocols/rollup/join.d.mts +4 -0
  64. package/esm/protocols/rollup/join.mjs +152 -0
  65. package/esm/protocols/rollup/leave.d.mts +4 -0
  66. package/esm/protocols/rollup/leave.mjs +137 -0
  67. package/esm/protocols/rollup/migrate.d.mts +4 -0
  68. package/esm/protocols/rollup/migrate.mjs +85 -0
  69. package/esm/protocols/rollup/pause.d.mts +4 -0
  70. package/esm/protocols/rollup/pause.mjs +76 -0
  71. package/esm/protocols/rollup/pipes/ensure-validator.d.mts +6 -0
  72. package/esm/protocols/rollup/pipes/ensure-validator.mjs +12 -0
  73. package/esm/protocols/rollup/pipes/verify-evidence.d.mts +15 -0
  74. package/esm/protocols/rollup/pipes/verify-evidence.mjs +29 -0
  75. package/esm/protocols/rollup/pipes/verify-signers.d.mts +15 -0
  76. package/esm/protocols/rollup/pipes/verify-signers.mjs +36 -0
  77. package/esm/protocols/rollup/pipes/verify-status.d.mts +13 -0
  78. package/esm/protocols/rollup/pipes/verify-status.mjs +26 -0
  79. package/esm/protocols/rollup/resume.d.mts +4 -0
  80. package/esm/protocols/rollup/resume.mjs +79 -0
  81. package/esm/protocols/rollup/update.d.mts +4 -0
  82. package/esm/protocols/rollup/update.mjs +111 -0
  83. package/esm/protocols/token/create.d.mts +4 -0
  84. package/esm/protocols/token/create.mjs +150 -0
  85. package/esm/protocols/token/deposit-v2.d.mts +11 -0
  86. package/esm/protocols/token/deposit-v2.mjs +216 -0
  87. package/esm/protocols/token/withdraw-v2.d.mts +9 -0
  88. package/esm/protocols/token/withdraw-v2.mjs +222 -0
  89. package/esm/protocols/token-factory/burn.d.mts +15 -0
  90. package/esm/protocols/token-factory/burn.mjs +233 -0
  91. package/esm/protocols/token-factory/create.d.mts +4 -0
  92. package/esm/protocols/token-factory/create.mjs +254 -0
  93. package/esm/protocols/token-factory/mint.d.mts +15 -0
  94. package/esm/protocols/token-factory/mint.mjs +234 -0
  95. package/esm/protocols/token-factory/pipes/calc-reserve.d.mts +21 -0
  96. package/esm/protocols/token-factory/pipes/calc-reserve.mjs +34 -0
  97. package/esm/protocols/token-factory/pipes/verify-icon.d.mts +14 -0
  98. package/esm/protocols/token-factory/pipes/verify-icon.mjs +18 -0
  99. package/esm/protocols/token-factory/pipes/verify-ownership.d.mts +12 -0
  100. package/esm/protocols/token-factory/pipes/verify-ownership.mjs +63 -0
  101. package/esm/protocols/token-factory/pipes/verify-url.d.mts +12 -0
  102. package/esm/protocols/token-factory/pipes/verify-url.mjs +26 -0
  103. package/esm/protocols/token-factory/update.d.mts +10 -0
  104. package/esm/protocols/token-factory/update.mjs +152 -0
  105. package/esm/protocols/trade/exchange-v2.d.mts +9 -0
  106. package/esm/protocols/trade/exchange-v2.mjs +239 -0
  107. package/esm/protocols/trade/transfer-v2.d.mts +9 -0
  108. package/esm/protocols/trade/transfer-v2.mjs +226 -0
  109. package/esm/protocols/trade/transfer-v3.d.mts +17 -0
  110. package/esm/protocols/trade/transfer-v3.mjs +270 -0
  111. package/esm/util.d.mts +141 -0
  112. package/esm/util.mjs +278 -0
  113. package/lib/_virtual/rolldown_runtime.cjs +29 -0
  114. package/lib/execute.cjs +231 -0
  115. package/lib/execute.d.cts +53 -0
  116. package/lib/index.cjs +105 -0
  117. package/lib/index.d.cts +95 -0
  118. package/lib/pipes/ensure-cost.cjs +141 -0
  119. package/lib/pipes/ensure-cost.d.cts +18 -0
  120. package/lib/pipes/ensure-gas.cjs +41 -0
  121. package/lib/pipes/ensure-gas.d.cts +14 -0
  122. package/lib/protocols/account/delegate.cjs +201 -0
  123. package/lib/protocols/account/delegate.d.cts +4 -0
  124. package/lib/protocols/account/migrate.cjs +135 -0
  125. package/lib/protocols/account/migrate.d.cts +4 -0
  126. package/lib/protocols/account/revoke-delegate.cjs +107 -0
  127. package/lib/protocols/account/revoke-delegate.d.cts +4 -0
  128. package/lib/protocols/asset/acquire-v2.cjs +216 -0
  129. package/lib/protocols/asset/acquire-v2.d.cts +9 -0
  130. package/lib/protocols/asset/acquire-v3.cjs +269 -0
  131. package/lib/protocols/asset/acquire-v3.d.cts +17 -0
  132. package/lib/protocols/asset/calls/transfer-token.cjs +40 -0
  133. package/lib/protocols/asset/calls/transfer-token.d.cts +4 -0
  134. package/lib/protocols/asset/calls/transfer.cjs +35 -0
  135. package/lib/protocols/asset/calls/transfer.d.cts +4 -0
  136. package/lib/protocols/asset/consume.cjs +95 -0
  137. package/lib/protocols/asset/consume.d.cts +4 -0
  138. package/lib/protocols/asset/create.cjs +140 -0
  139. package/lib/protocols/asset/create.d.cts +4 -0
  140. package/lib/protocols/asset/mint.cjs +159 -0
  141. package/lib/protocols/asset/mint.d.cts +4 -0
  142. package/lib/protocols/asset/pipes/exec-mint-hook.cjs +57 -0
  143. package/lib/protocols/asset/pipes/exec-mint-hook.d.cts +24 -0
  144. package/lib/protocols/asset/pipes/extract-factory-tokens.cjs +25 -0
  145. package/lib/protocols/asset/pipes/extract-factory-tokens.d.cts +21 -0
  146. package/lib/protocols/asset/pipes/verify-itx-address.cjs +59 -0
  147. package/lib/protocols/asset/pipes/verify-itx-address.d.cts +32 -0
  148. package/lib/protocols/asset/pipes/verify-itx-assets.cjs +29 -0
  149. package/lib/protocols/asset/pipes/verify-itx-assets.d.cts +18 -0
  150. package/lib/protocols/asset/pipes/verify-itx-variables.cjs +22 -0
  151. package/lib/protocols/asset/pipes/verify-itx-variables.d.cts +17 -0
  152. package/lib/protocols/asset/pipes/verify-mint-limit.cjs +16 -0
  153. package/lib/protocols/asset/pipes/verify-mint-limit.d.cts +15 -0
  154. package/lib/protocols/asset/update.cjs +114 -0
  155. package/lib/protocols/asset/update.d.cts +4 -0
  156. package/lib/protocols/factory/create.cjs +161 -0
  157. package/lib/protocols/factory/create.d.cts +29 -0
  158. package/lib/protocols/governance/claim-stake.cjs +223 -0
  159. package/lib/protocols/governance/claim-stake.d.cts +27 -0
  160. package/lib/protocols/governance/return-stake.cjs +215 -0
  161. package/lib/protocols/governance/return-stake.d.cts +27 -0
  162. package/lib/protocols/governance/revoke-stake.cjs +182 -0
  163. package/lib/protocols/governance/revoke-stake.d.cts +27 -0
  164. package/lib/protocols/governance/slash-stake.cjs +217 -0
  165. package/lib/protocols/governance/slash-stake.d.cts +17 -0
  166. package/lib/protocols/governance/stake.cjs +275 -0
  167. package/lib/protocols/governance/stake.d.cts +15 -0
  168. package/lib/protocols/rollup/claim-reward.cjs +328 -0
  169. package/lib/protocols/rollup/claim-reward.d.cts +11 -0
  170. package/lib/protocols/rollup/close.cjs +107 -0
  171. package/lib/protocols/rollup/close.d.cts +4 -0
  172. package/lib/protocols/rollup/create-block.cjs +309 -0
  173. package/lib/protocols/rollup/create-block.d.cts +11 -0
  174. package/lib/protocols/rollup/create.cjs +169 -0
  175. package/lib/protocols/rollup/create.d.cts +4 -0
  176. package/lib/protocols/rollup/join.cjs +157 -0
  177. package/lib/protocols/rollup/join.d.cts +4 -0
  178. package/lib/protocols/rollup/leave.cjs +141 -0
  179. package/lib/protocols/rollup/leave.d.cts +4 -0
  180. package/lib/protocols/rollup/migrate.cjs +87 -0
  181. package/lib/protocols/rollup/migrate.d.cts +4 -0
  182. package/lib/protocols/rollup/pause.cjs +78 -0
  183. package/lib/protocols/rollup/pause.d.cts +4 -0
  184. package/lib/protocols/rollup/pipes/ensure-validator.cjs +14 -0
  185. package/lib/protocols/rollup/pipes/ensure-validator.d.cts +6 -0
  186. package/lib/protocols/rollup/pipes/verify-evidence.cjs +32 -0
  187. package/lib/protocols/rollup/pipes/verify-evidence.d.cts +15 -0
  188. package/lib/protocols/rollup/pipes/verify-signers.cjs +39 -0
  189. package/lib/protocols/rollup/pipes/verify-signers.d.cts +15 -0
  190. package/lib/protocols/rollup/pipes/verify-status.cjs +28 -0
  191. package/lib/protocols/rollup/pipes/verify-status.d.cts +13 -0
  192. package/lib/protocols/rollup/resume.cjs +81 -0
  193. package/lib/protocols/rollup/resume.d.cts +4 -0
  194. package/lib/protocols/rollup/update.cjs +114 -0
  195. package/lib/protocols/rollup/update.d.cts +4 -0
  196. package/lib/protocols/token/create.cjs +156 -0
  197. package/lib/protocols/token/create.d.cts +4 -0
  198. package/lib/protocols/token/deposit-v2.cjs +219 -0
  199. package/lib/protocols/token/deposit-v2.d.cts +11 -0
  200. package/lib/protocols/token/withdraw-v2.cjs +225 -0
  201. package/lib/protocols/token/withdraw-v2.d.cts +9 -0
  202. package/lib/protocols/token-factory/burn.cjs +236 -0
  203. package/lib/protocols/token-factory/burn.d.cts +15 -0
  204. package/lib/protocols/token-factory/create.cjs +260 -0
  205. package/lib/protocols/token-factory/create.d.cts +4 -0
  206. package/lib/protocols/token-factory/mint.cjs +237 -0
  207. package/lib/protocols/token-factory/mint.d.cts +15 -0
  208. package/lib/protocols/token-factory/pipes/calc-reserve.cjs +38 -0
  209. package/lib/protocols/token-factory/pipes/calc-reserve.d.cts +21 -0
  210. package/lib/protocols/token-factory/pipes/verify-icon.cjs +22 -0
  211. package/lib/protocols/token-factory/pipes/verify-icon.d.cts +14 -0
  212. package/lib/protocols/token-factory/pipes/verify-ownership.cjs +66 -0
  213. package/lib/protocols/token-factory/pipes/verify-ownership.d.cts +12 -0
  214. package/lib/protocols/token-factory/pipes/verify-url.cjs +29 -0
  215. package/lib/protocols/token-factory/pipes/verify-url.d.cts +12 -0
  216. package/lib/protocols/token-factory/update.cjs +155 -0
  217. package/lib/protocols/token-factory/update.d.cts +10 -0
  218. package/lib/protocols/trade/exchange-v2.cjs +243 -0
  219. package/lib/protocols/trade/exchange-v2.d.cts +9 -0
  220. package/lib/protocols/trade/transfer-v2.cjs +229 -0
  221. package/lib/protocols/trade/transfer-v2.d.cts +9 -0
  222. package/lib/protocols/trade/transfer-v3.cjs +274 -0
  223. package/lib/protocols/trade/transfer-v3.d.cts +17 -0
  224. package/lib/util.cjs +296 -0
  225. package/lib/util.d.cts +141 -0
  226. package/package.json +49 -22
  227. package/tools/fixtures.ts +564 -0
  228. package/lib/execute.js +0 -254
  229. package/lib/index.js +0 -117
  230. package/lib/pipes/ensure-cost.js +0 -193
  231. package/lib/pipes/ensure-gas.js +0 -48
  232. package/lib/protocols/account/delegate.js +0 -223
  233. package/lib/protocols/account/migrate.js +0 -153
  234. package/lib/protocols/account/revoke-delegate.js +0 -110
  235. package/lib/protocols/asset/acquire-v2.js +0 -262
  236. package/lib/protocols/asset/acquire-v3.js +0 -330
  237. package/lib/protocols/asset/calls/README.md +0 -5
  238. package/lib/protocols/asset/calls/transfer-token.js +0 -36
  239. package/lib/protocols/asset/calls/transfer.js +0 -28
  240. package/lib/protocols/asset/consume.js +0 -105
  241. package/lib/protocols/asset/create.js +0 -151
  242. package/lib/protocols/asset/mint.js +0 -199
  243. package/lib/protocols/asset/pipes/exec-mint-hook.js +0 -62
  244. package/lib/protocols/asset/pipes/extract-factory-tokens.js +0 -18
  245. package/lib/protocols/asset/pipes/verify-itx-address.js +0 -54
  246. package/lib/protocols/asset/pipes/verify-itx-assets.js +0 -51
  247. package/lib/protocols/asset/pipes/verify-itx-variables.js +0 -26
  248. package/lib/protocols/asset/pipes/verify-mint-limit.js +0 -13
  249. package/lib/protocols/asset/update.js +0 -131
  250. package/lib/protocols/factory/create.js +0 -191
  251. package/lib/protocols/governance/claim-stake.js +0 -266
  252. package/lib/protocols/governance/return-stake.js +0 -248
  253. package/lib/protocols/governance/revoke-stake.js +0 -172
  254. package/lib/protocols/governance/slash-stake.js +0 -271
  255. package/lib/protocols/governance/stake.js +0 -303
  256. package/lib/protocols/rollup/claim-reward.js +0 -342
  257. package/lib/protocols/rollup/close.js +0 -104
  258. package/lib/protocols/rollup/create-block.js +0 -413
  259. package/lib/protocols/rollup/create.js +0 -197
  260. package/lib/protocols/rollup/join.js +0 -182
  261. package/lib/protocols/rollup/leave.js +0 -145
  262. package/lib/protocols/rollup/migrate.js +0 -85
  263. package/lib/protocols/rollup/pause.js +0 -75
  264. package/lib/protocols/rollup/pipes/ensure-validator.js +0 -12
  265. package/lib/protocols/rollup/pipes/verify-evidence.js +0 -37
  266. package/lib/protocols/rollup/pipes/verify-signers.js +0 -87
  267. package/lib/protocols/rollup/pipes/verify-status.js +0 -30
  268. package/lib/protocols/rollup/resume.js +0 -75
  269. package/lib/protocols/rollup/update.js +0 -122
  270. package/lib/protocols/token/create.js +0 -199
  271. package/lib/protocols/token/deposit-v2.js +0 -290
  272. package/lib/protocols/token/withdraw-v2.js +0 -305
  273. package/lib/protocols/token-factory/burn.js +0 -371
  274. package/lib/protocols/token-factory/create.js +0 -342
  275. package/lib/protocols/token-factory/mint.js +0 -385
  276. package/lib/protocols/token-factory/pipes/calc-reserve.js +0 -37
  277. package/lib/protocols/token-factory/pipes/verify-icon.js +0 -27
  278. package/lib/protocols/token-factory/pipes/verify-ownership.js +0 -93
  279. package/lib/protocols/token-factory/pipes/verify-url.js +0 -32
  280. package/lib/protocols/token-factory/update.js +0 -208
  281. package/lib/protocols/trade/exchange-v2.js +0 -239
  282. package/lib/protocols/trade/transfer-v2.js +0 -233
  283. package/lib/protocols/trade/transfer-v3.js +0 -333
  284. package/lib/util.js +0 -442
@@ -0,0 +1,564 @@
1
+ import { spyOn } from 'bun:test';
2
+ import GraphQLClient from '@ocap/client';
3
+ import MemoryStateDB from '@ocap/statedb-memory';
4
+ import crypto from 'node:crypto';
5
+ import cloneDeep from 'lodash/cloneDeep';
6
+ import { types, getRandomBytes, toTxHash } from '@ocap/mcrypto';
7
+ import { fromRandom } from '@ocap/wallet';
8
+ import { createMessage, fromTypeUrl } from '@ocap/message';
9
+ import { toBase64, toBN, fromUnitToToken, fromTokenToUnit, toBase58 } from '@ocap/util';
10
+ import * as states from '@ocap/state';
11
+ import * as JWT from '@arcblock/jwt';
12
+ import { toStakeAddress, toTokenAddress, toTokenFactoryAddress } from '@arcblock/did-util';
13
+ import { create as createVC } from '@arcblock/vc';
14
+ import { getTestChain, closeTestChain } from '@ocap/e2e-test/fixture';
15
+ import defaultConfigModule from '@ocap/e2e-test/config';
16
+
17
+ // @ts-ignore - compiled output
18
+ import { execute, createExecutor } from '../lib';
19
+
20
+ const defaultConfig = cloneDeep(defaultConfigModule);
21
+
22
+ const lambdaExecutor = createExecutor({
23
+ filter: new (states as any).Blacklist(),
24
+ runAsLambda: (cb: () => void) => cb(),
25
+ });
26
+
27
+ export const statedb = new MemoryStateDB();
28
+
29
+ // Client will be initialized in setup()
30
+ let _client: any = null;
31
+
32
+ // Setup function - call this in beforeAll()
33
+ export const setup = async () => {
34
+ const chain = await getTestChain();
35
+ _client = new GraphQLClient(chain.endpoint);
36
+ // Update chainId in both defaultConfig and config
37
+ (defaultConfig as any).chainId = chain.config.chainId;
38
+ config.chainId = chain.config.chainId;
39
+ console.log(`Test chain ready at ${chain.endpoint}`);
40
+ return chain;
41
+ };
42
+
43
+ // Teardown function - call this in afterAll()
44
+ export const teardown = () => {
45
+ closeTestChain();
46
+ };
47
+
48
+ // Getter for client (ensures setup was called)
49
+ const getClient = () => {
50
+ if (!_client) {
51
+ throw new Error('Client not initialized. Call setup() in beforeAll() first.');
52
+ }
53
+ return _client;
54
+ };
55
+
56
+ // Export client as a Proxy that delegates all method calls to the initialized client
57
+ // This allows test files to use client.someMethod() directly while the actual client
58
+ // is lazily initialized in setup()
59
+ export const client: any = new Proxy({}, {
60
+ get(_target, prop) {
61
+ return getClient()[prop];
62
+ },
63
+ });
64
+
65
+ export const getTxHash = () => getRandomBytes(32);
66
+
67
+ export const getTxBase64 = (tx: any) => {
68
+ const txObj = createMessage('Transaction', tx);
69
+ if (!txObj) {
70
+ throw new Error('Failed to create Transaction message');
71
+ }
72
+ const txBytes = txObj.serializeBinary();
73
+ const txStr = toBase64(txBytes);
74
+ return txStr;
75
+ };
76
+
77
+ const context = { txHash: getTxHash(), txTime: new Date().toISOString() };
78
+ const defaultAttrs = {
79
+ asset: {
80
+ moniker: 'test-asset',
81
+ data: { type: 'json', value: { purpose: 'test' } },
82
+ consumedTime: '',
83
+ issuer: '',
84
+ parent: '',
85
+ readonly: false,
86
+ transferrable: true,
87
+ ttl: 0,
88
+ tags: [],
89
+ },
90
+ };
91
+ ['account', 'asset', 'delegation', 'factory', 'token', 'stake', 'rollup', 'rollupBlock', 'evidence'].forEach(
92
+ (table) => {
93
+ (statedb as any)[table].pre('create', function preCreate(this: any) {
94
+ const [address, attrs] = this;
95
+ const final =
96
+ attrs && (attrs.context || attrs.skipValidation)
97
+ ? attrs
98
+ : (states as any)[table].create({ address, ...Object.assign({}, (defaultAttrs as any)[table] || {}, attrs || {}) }, context);
99
+ this[1] = final;
100
+ });
101
+ }
102
+ );
103
+
104
+ export const prepareToken = async () => {
105
+ const tokenState = await statedb.token.get((defaultConfig as any).itx.address);
106
+ if (!tokenState) {
107
+ await statedb.token.create((defaultConfig as any).itx.address, (defaultConfig as any).itx);
108
+ }
109
+ };
110
+
111
+ export const prepareTx = async (type = 'default') => {
112
+ const sender = fromRandom(type as any);
113
+ const receiver = fromRandom(type as any);
114
+ const asset = fromRandom({ role: types.RoleType.ROLE_ASSET });
115
+
116
+ const accountAttrs = { balance: '0', tokens: { [(defaultConfig as any).itx.address]: '100000000000000000000' } };
117
+
118
+ await prepareToken();
119
+
120
+ await statedb.account.create(sender.address, accountAttrs);
121
+ await statedb.account.create(receiver.address, accountAttrs);
122
+ await statedb.asset.create(asset.address, { ...defaultAttrs.asset, owner: sender.address });
123
+
124
+ return { sender, receiver, asset, accountAttrs };
125
+ };
126
+
127
+ export const prepareGasPayer = async () => {
128
+ const gasPayer = fromRandom();
129
+ await statedb.account.create(gasPayer.address, {
130
+ tokens: { [(defaultConfig as any).itx.address]: fromTokenToUnit('10').toString() },
131
+ });
132
+ const stakeState = toStakeAddress(gasPayer.address, gasPayer.address);
133
+ await statedb.stake.create(stakeState, {
134
+ address: stakeState,
135
+ sender: gasPayer.address,
136
+ receiver: gasPayer.address,
137
+ message: 'stake-for-gas',
138
+ tokens: { [(defaultConfig as any).itx.address]: fromTokenToUnit('1').toString() },
139
+ });
140
+ return gasPayer;
141
+ };
142
+
143
+ export const getGasPayerHeader = async (txBuffer: Uint8Array) => {
144
+ const gasPayer = await prepareGasPayer();
145
+ return {
146
+ gasStakeHeaders: {
147
+ pk: toBase58(gasPayer.publicKey),
148
+ token: await JWT.sign(gasPayer.address, gasPayer.secretKey, { txHash: toTxHash(txBuffer) }),
149
+ },
150
+ };
151
+ };
152
+
153
+ export const getTransferTx = async ({ v2 = false } = {}) => {
154
+ const { sender, receiver, asset } = await prepareTx();
155
+ const method = v2 ? 'encodeTransferV2Tx' : 'encodeTransferTx';
156
+
157
+ const { object: txObject, buffer: txBuffer } = await getClient()[method]({
158
+ tx: {
159
+ itx: {
160
+ to: receiver.address,
161
+ value: await getClient().fromTokenToUnit(10),
162
+ assets: [asset.address],
163
+ },
164
+ },
165
+ wallet: sender,
166
+ });
167
+
168
+ return { txBase64: toBase64(txBuffer), txObject, txBuffer, sender, receiver, asset };
169
+ };
170
+
171
+ export const getSignedTransferTx = async ({ v2 = false } = {}) => {
172
+ const { sender, receiver, asset } = await prepareTx();
173
+ const method = v2 ? 'signTransferV2Tx' : 'signTransferTx';
174
+
175
+ const txObject = await getClient()[method]({
176
+ tx: {
177
+ itx: {
178
+ to: receiver.address,
179
+ value: await getClient().fromTokenToUnit(10),
180
+ assets: [asset.address],
181
+ },
182
+ },
183
+ wallet: sender,
184
+ });
185
+
186
+ const txBase64 = getTxBase64(txObject);
187
+ return { txObject, txBase64, sender, receiver, asset };
188
+ };
189
+
190
+ export const getSignedExchangeTx = async () => {
191
+ const { sender, receiver, asset } = await prepareTx();
192
+ const senderSigned = await getClient().signExchangeV2Tx({
193
+ tx: {
194
+ itx: {
195
+ to: receiver.address,
196
+ sender: {
197
+ assets: [asset.address],
198
+ },
199
+ receiver: {
200
+ value: await getClient().fromTokenToUnit(10),
201
+ assets: [],
202
+ },
203
+ data: {
204
+ typeUrl: 'json',
205
+ value: 'empty',
206
+ },
207
+ },
208
+ },
209
+ wallet: sender,
210
+ });
211
+
212
+ const receiverSigned = await getClient().multiSignExchangeV2Tx({
213
+ tx: cloneDeep(senderSigned),
214
+ wallet: receiver,
215
+ data: {
216
+ typeUrl: 'json',
217
+ value: 'empty',
218
+ },
219
+ });
220
+
221
+ const txBase64Sender = getTxBase64(senderSigned);
222
+ const txBase64Receiver = getTxBase64(receiverSigned);
223
+ return { sender, receiver, asset, senderSigned, receiverSigned, txBase64Sender, txBase64Receiver };
224
+ };
225
+
226
+ export const getAssetFactory = async ({
227
+ value = 10,
228
+ assets = [],
229
+ tokens = [],
230
+ hooks = [],
231
+ trustedIssuers = [],
232
+ limit = 0,
233
+ description = undefined as string | undefined,
234
+ output = undefined as any,
235
+ } = {}) => ({
236
+ name: 'NodePurchaseFactory',
237
+ description: description || 'Purchase evidence of t2.small instance for 1 month',
238
+ settlement: 'instant',
239
+ limit,
240
+ trustedIssuers,
241
+ input: {
242
+ value: '0',
243
+ tokens: [
244
+ { address: (defaultConfig as any).itx.address, value: (await getClient().fromTokenToUnit(value)).toString() },
245
+ ...tokens,
246
+ ].filter((x: any) => x.value > 0),
247
+ assets: [...assets],
248
+ variables: [
249
+ {
250
+ name: 'name',
251
+ description: 'The name of the asset',
252
+ required: true,
253
+ },
254
+ {
255
+ name: 'description',
256
+ description: 'The name of the asset',
257
+ required: true,
258
+ },
259
+ {
260
+ name: 'counter',
261
+ description: 'The name of the asset',
262
+ required: true,
263
+ },
264
+ ],
265
+ },
266
+ output: output || {
267
+ issuer: '{{ctx.issuer.id}}',
268
+ parent: '{{ctx.factory}}',
269
+ moniker: 'NodePurchaseNFT',
270
+ readonly: true,
271
+ transferrable: true,
272
+ data: {
273
+ type: 'json',
274
+ value: {
275
+ type: '{{data.type}}',
276
+ period: '{{data.period}}',
277
+ name: '{{input.name}}',
278
+ description: '{{input.description}}',
279
+ counter: '{{input.counter}}',
280
+ },
281
+ },
282
+ endpoint: {
283
+ scope: 'public',
284
+ id: 'https://playground.abtwallet.io/api/nft/status',
285
+ },
286
+ display: {
287
+ type: 'url',
288
+ content: 'https://playground.abtwallet.io/api/nft/display',
289
+ },
290
+ },
291
+ data: {
292
+ type: 'json',
293
+ value: {
294
+ type: 't2.small',
295
+ period: '1 month',
296
+ },
297
+ },
298
+ hooks: [
299
+ {
300
+ name: 'postMint',
301
+ type: 'url',
302
+ hook: 'http://launcher.arcblock.io/api/purchases/{ctx.address}',
303
+ },
304
+ ].concat(hooks || []),
305
+ });
306
+
307
+ export const findTokenBalance = (state: any, field = 'tokens', token = (defaultConfig as any).itx.address) => {
308
+ if (!state[field]) {
309
+ return '0';
310
+ }
311
+
312
+ return fromUnitToToken(state[field][token] || '0', (defaultConfig as any).token.decimal);
313
+ };
314
+
315
+ export const ensureTokenBalanceBelow = (state: any, threshold: number, field = 'tokens', token = (defaultConfig as any).itx.address) => {
316
+ if (!state[field]) {
317
+ return true;
318
+ }
319
+
320
+ return toBN(state[field][token] || '0').lt(fromTokenToUnit(threshold, (defaultConfig as any).token.decimal));
321
+ };
322
+
323
+ export const declare = async (_client: any, sender: any, receiver?: any) => {
324
+ const wallet = receiver || fromRandom();
325
+ const c = _client || getClient();
326
+ const [hash] = await c.delegate({
327
+ from: sender,
328
+ to: wallet,
329
+ privileges: [
330
+ {
331
+ typeUrl: 'fg:t:exchange_v2',
332
+ },
333
+ ],
334
+ });
335
+ return { wallet, hash };
336
+ };
337
+
338
+ export const toBNStr = (n: number | string) => fromTokenToUnit(n, (defaultConfig as any).token.decimal).toString(10);
339
+ export const fromBNStr = (n: string) => fromUnitToToken(n, (defaultConfig as any).token.decimal);
340
+
341
+ export const getRollupConfig = () => ({
342
+ paused: false,
343
+ closed: false,
344
+ tokenAddress: (defaultConfig as any).itx.address,
345
+ vaultAddress: '0x9c41b8E8500aEAE3A376a52E6F7547AFe6A92eB1',
346
+ contractAddress: '0x9c41b8E8500aEAE3A376a52E6F7547AFe6A92eB1',
347
+ minStakeAmount: toBNStr(1000000),
348
+ maxStakeAmount: toBNStr(10000000),
349
+ minSignerCount: 2,
350
+ maxSignerCount: 3,
351
+ minBlockSize: 1,
352
+ maxBlockSize: 10,
353
+ minBlockInterval: 1, // 1 seconds
354
+ minBlockConfirmation: 1,
355
+ minDepositAmount: toBNStr(100),
356
+ minWithdrawAmount: toBNStr(100),
357
+ maxDepositAmount: toBNStr(100000),
358
+ maxWithdrawAmount: toBNStr(100000),
359
+ depositFeeRate: 100, // 1%
360
+ withdrawFeeRate: 100, // 1%
361
+ proposerFeeShare: 6000, // 60%
362
+ publisherFeeShare: 3000, // 30%
363
+ minDepositFee: toBNStr(10),
364
+ maxDepositFee: toBNStr(10000),
365
+ minWithdrawFee: toBNStr(10),
366
+ maxWithdrawFee: toBNStr(10000),
367
+ blockHeight: 0,
368
+ leaveWaitingPeriod: 60,
369
+ publishWaitingPeriod: 60,
370
+ publishSlashRate: 10, // 0.1%
371
+ });
372
+
373
+ export const sleep = (timeout: number) =>
374
+ new Promise<void>((resolve) => {
375
+ setTimeout(resolve, timeout);
376
+ });
377
+
378
+ export const generateSvg = (maxSize: number) => {
379
+ const header = '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">';
380
+ const footer = '</svg>';
381
+
382
+ const baseSize = Buffer.byteLength(header + footer, 'utf8');
383
+ if (maxSize <= baseSize) return header + footer;
384
+
385
+ const availableSpace = maxSize - baseSize;
386
+ const filler = '<g />';
387
+ const fillerSize = Buffer.byteLength(filler, 'utf8');
388
+
389
+ const count = Math.floor(availableSpace / fillerSize);
390
+ const content = filler.repeat(count);
391
+
392
+ return header + content + footer;
393
+ };
394
+
395
+ export const gasVault = fromRandom();
396
+ export const feeVault = fromRandom();
397
+
398
+ export const config: any = {
399
+ ...defaultConfig,
400
+ vaults: {
401
+ ...(defaultConfig as any).vaults,
402
+ txGas: [gasVault.address],
403
+ txFee: feeVault.address,
404
+ },
405
+ token: { ...(defaultConfig as any).token, address: (defaultConfig as any).itx.address },
406
+ };
407
+
408
+ export const gasVaultAddress = config.vaults.txGas[0];
409
+ export const feeVaultAddress = config.vaults.txFee;
410
+
411
+ statedb.account.create(config.vaults.txFee, { tokens: { [config.itx.address]: '0' } });
412
+ statedb.account.create(config.vaults.slashedStake, { tokens: { [config.itx.address]: '0' } });
413
+ config.vaults.txGas.forEach((x: string) => {
414
+ statedb.account.create(x, { tokens: { [config.itx.address]: toBNStr(10000) } });
415
+ });
416
+
417
+ export const prepareTokenFactory = (props: any = {}) => {
418
+ const { token, ...rest } = props;
419
+ const name = crypto.randomUUID().substring(0, 8);
420
+ const itx: any = {
421
+ feeRate: 500, // 5% fee
422
+ token: {
423
+ name,
424
+ description: 'create token factory description',
425
+ decimal: 18,
426
+ symbol: name.substring(0, 6).toUpperCase(),
427
+ unit: 'D',
428
+ icon: '',
429
+ website: 'https://www.arcblock.com',
430
+ type: 'BondingCurveToken',
431
+ metadata: {
432
+ type: 'json',
433
+ value: {
434
+ issuer: 'arcblock',
435
+ communityUrl: 'https://www.arcblock.com',
436
+ },
437
+ },
438
+ ...token,
439
+ },
440
+ curve: {
441
+ type: 'constant',
442
+ fixedPrice: toBNStr('1'),
443
+ },
444
+ reserveAddress: config.token.address,
445
+ data: null,
446
+ ...rest,
447
+ };
448
+
449
+ itx.token.address = token?.address || toTokenAddress(itx.token);
450
+ itx.address = rest?.address || toTokenFactoryAddress(itx);
451
+
452
+ return itx;
453
+ };
454
+
455
+ export const createTokenFactoryStake = async ({ sender, nonce, amount = 100, tokens = null, ...props }: any = {}) => {
456
+ // For token factory creation, receiver should be config.token.address
457
+ const receiver = config.token.address;
458
+ const stakeAddress = toStakeAddress(sender.address, receiver, nonce);
459
+ const stakeTokens = tokens || { [config.token.address]: toBNStr(amount) };
460
+
461
+ const stakeState = await statedb.stake.get(stakeAddress);
462
+
463
+ await statedb.stake.updateOrCreate(stakeState, {
464
+ nonce,
465
+ address: stakeAddress,
466
+ sender: sender.address,
467
+ receiver,
468
+ message: 'test stake for token creation',
469
+ tokens: stakeTokens,
470
+ ...props,
471
+ });
472
+
473
+ return { stakeAddress };
474
+ };
475
+
476
+ export const mockWebsiteVC = (subject: any, issuer: any, options: any = {}) => {
477
+ spyOn(global, 'fetch').mockImplementation(((url: any, ...rest: any[]) => {
478
+ if (!url.includes('/ocap/tokens.json')) {
479
+ return (globalThis as any).require('node-fetch-commonjs')(url, ...rest);
480
+ }
481
+
482
+ return Promise.resolve({
483
+ ok: true,
484
+ json: async () => {
485
+ if (!subject) return options.credentials || null;
486
+
487
+ const vc = await createVC({
488
+ type: options.type || 'TokenIssueCredential',
489
+ issuer: {
490
+ name: options.issuerName || 'Create Token',
491
+ wallet: issuer,
492
+ },
493
+ subject,
494
+ });
495
+ return options.credentials || [vc];
496
+ },
497
+ } as any);
498
+ }) as any);
499
+ };
500
+
501
+ export const executeTx = (tx: any, withTxHash = false, _statedb: any = {}, options: any = {}) => {
502
+ const { runAsLambda, withGasPayer, config: _config, ...extraCtx } = options;
503
+
504
+ return new Promise(async (resolve) => {
505
+ const ctx: any = {
506
+ txBase64: getTxBase64(tx),
507
+ config: _config || config,
508
+ statedb: { ...statedb, ..._statedb },
509
+ ...extraCtx,
510
+ };
511
+
512
+ if (withGasPayer) {
513
+ const typeUrl = fromTypeUrl(tx.itx.typeUrl);
514
+ const { buffer: txBuffer } = await getClient()[`encode${typeUrl}`]({ tx });
515
+ ctx.extra = {
516
+ ...(ctx.extra || {}),
517
+ ...(await getGasPayerHeader(txBuffer)),
518
+ };
519
+ }
520
+
521
+ (runAsLambda ? lambdaExecutor.execute : execute)(ctx, (err: any) => {
522
+ resolve(withTxHash ? { err, txHash: ctx.txHash } : err);
523
+ });
524
+ });
525
+ };
526
+
527
+ export const createTokenFactory = async (owner: any, props: any, { subject, stakeAddress, config: _config, ...rest }: any = {}) => {
528
+ const itx = prepareTokenFactory(props);
529
+
530
+ // stake for token creation
531
+ let _stakeAddress = stakeAddress;
532
+ if (_stakeAddress == null) {
533
+ ({ stakeAddress: _stakeAddress } = await createTokenFactoryStake({ sender: owner, nonce: itx.token.symbol }));
534
+ }
535
+
536
+ // mock for website verification
537
+ mockWebsiteVC(
538
+ subject || {
539
+ id: owner.address,
540
+ issued: {
541
+ address: itx.token.address,
542
+ symbol: itx.token.symbol,
543
+ website: itx.token.website,
544
+ chainId: (_config || config).chainId,
545
+ },
546
+ },
547
+ owner
548
+ );
549
+
550
+ const tx = await getClient().signCreateTokenFactoryTx({ tx: { itx }, wallet: owner, ...rest });
551
+ const err = await executeTx(tx, false, {}, { withGasPayer: true, config: _config || config });
552
+
553
+ const tokenFactoryState = await statedb.tokenFactory.get(itx.address);
554
+
555
+ return {
556
+ itx,
557
+ err,
558
+ tokenFactoryState,
559
+ stakeAddress: _stakeAddress,
560
+ };
561
+ };
562
+
563
+ // Re-export for convenience
564
+ export { toBN, toTxHash };