@algorandfoundation/algokit-utils 9.2.0 → 9.2.1-beta.2

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 (340) hide show
  1. package/_virtual/_rolldown/runtime.js +33 -0
  2. package/_virtual/_rolldown/runtime.mjs +13 -0
  3. package/account/account.d.ts +25 -30
  4. package/account/account.js +151 -146
  5. package/account/account.js.map +1 -1
  6. package/account/account.mjs +149 -144
  7. package/account/account.mjs.map +1 -1
  8. package/account/get-account-config-from-environment.d.ts +7 -2
  9. package/account/get-account-config-from-environment.js +19 -21
  10. package/account/get-account-config-from-environment.js.map +1 -1
  11. package/account/get-account-config-from-environment.mjs +19 -19
  12. package/account/get-account-config-from-environment.mjs.map +1 -1
  13. package/account/get-account.d.ts +14 -12
  14. package/account/get-account.js +56 -59
  15. package/account/get-account.js.map +1 -1
  16. package/account/get-account.mjs +56 -57
  17. package/account/get-account.mjs.map +1 -1
  18. package/account/get-dispenser-account.d.ts +9 -5
  19. package/account/get-dispenser-account.js +20 -18
  20. package/account/get-dispenser-account.js.map +1 -1
  21. package/account/get-dispenser-account.mjs +20 -16
  22. package/account/get-dispenser-account.mjs.map +1 -1
  23. package/account/mnemonic-account.d.ts +7 -3
  24. package/account/mnemonic-account.js +17 -17
  25. package/account/mnemonic-account.js.map +1 -1
  26. package/account/mnemonic-account.mjs +15 -15
  27. package/account/mnemonic-account.mjs.map +1 -1
  28. package/amount.d.ts +40 -35
  29. package/amount.js +33 -34
  30. package/amount.js.map +1 -1
  31. package/amount.mjs +33 -32
  32. package/amount.mjs.map +1 -1
  33. package/app-client.d.ts +10 -6
  34. package/app-client.js +91 -86
  35. package/app-client.js.map +1 -1
  36. package/app-client.mjs +91 -84
  37. package/app-client.mjs.map +1 -1
  38. package/app-deploy.d.ts +26 -24
  39. package/app-deploy.js +235 -253
  40. package/app-deploy.js.map +1 -1
  41. package/app-deploy.mjs +233 -251
  42. package/app-deploy.mjs.map +1 -1
  43. package/app.d.ts +45 -46
  44. package/app.js +253 -271
  45. package/app.js.map +1 -1
  46. package/app.mjs +249 -267
  47. package/app.mjs.map +1 -1
  48. package/asset.d.ts +16 -12
  49. package/asset.js +116 -122
  50. package/asset.js.map +1 -1
  51. package/asset.mjs +116 -120
  52. package/asset.mjs.map +1 -1
  53. package/config.d.ts +7 -2
  54. package/config.js +5 -7
  55. package/config.js.map +1 -1
  56. package/config.mjs +5 -4
  57. package/config.mjs.map +1 -1
  58. package/debugging/debugging.d.ts +5 -1
  59. package/debugging/debugging.js +11 -11
  60. package/debugging/debugging.js.map +1 -1
  61. package/debugging/debugging.mjs +11 -9
  62. package/debugging/debugging.mjs.map +1 -1
  63. package/dispenser-client.d.ts +7 -2
  64. package/dispenser-client.js +23 -24
  65. package/dispenser-client.js.map +1 -1
  66. package/dispenser-client.mjs +23 -22
  67. package/dispenser-client.mjs.map +1 -1
  68. package/index.d.ts +27 -18
  69. package/index.js +140 -143
  70. package/index.mjs +27 -29
  71. package/indexer-lookup.d.ts +18 -11
  72. package/indexer-lookup.js +112 -120
  73. package/indexer-lookup.js.map +1 -1
  74. package/indexer-lookup.mjs +105 -118
  75. package/indexer-lookup.mjs.map +1 -1
  76. package/localnet/get-kmd-wallet-account.d.ts +9 -7
  77. package/localnet/get-kmd-wallet-account.js +28 -26
  78. package/localnet/get-kmd-wallet-account.js.map +1 -1
  79. package/localnet/get-kmd-wallet-account.mjs +28 -24
  80. package/localnet/get-kmd-wallet-account.mjs.map +1 -1
  81. package/localnet/get-localnet-dispenser-account.d.ts +7 -5
  82. package/localnet/get-localnet-dispenser-account.js +17 -15
  83. package/localnet/get-localnet-dispenser-account.js.map +1 -1
  84. package/localnet/get-localnet-dispenser-account.mjs +17 -13
  85. package/localnet/get-localnet-dispenser-account.mjs.map +1 -1
  86. package/localnet/get-or-create-kmd-wallet-account.d.ts +10 -8
  87. package/localnet/get-or-create-kmd-wallet-account.js +28 -26
  88. package/localnet/get-or-create-kmd-wallet-account.js.map +1 -1
  89. package/localnet/get-or-create-kmd-wallet-account.mjs +28 -24
  90. package/localnet/get-or-create-kmd-wallet-account.mjs.map +1 -1
  91. package/localnet/is-localnet.d.ts +7 -3
  92. package/localnet/is-localnet.js +9 -10
  93. package/localnet/is-localnet.js.map +1 -1
  94. package/localnet/is-localnet.mjs +9 -8
  95. package/localnet/is-localnet.mjs.map +1 -1
  96. package/network-client.d.ts +17 -15
  97. package/network-client.js +115 -116
  98. package/network-client.js.map +1 -1
  99. package/network-client.mjs +115 -114
  100. package/network-client.mjs.map +1 -1
  101. package/package.json +12 -3
  102. package/testing/account.d.ts +11 -9
  103. package/testing/account.js +31 -33
  104. package/testing/account.js.map +1 -1
  105. package/testing/account.mjs +29 -31
  106. package/testing/account.mjs.map +1 -1
  107. package/testing/fixtures/algokit-log-capture-fixture.d.ts +7 -2
  108. package/testing/fixtures/algokit-log-capture-fixture.js +36 -41
  109. package/testing/fixtures/algokit-log-capture-fixture.js.map +1 -1
  110. package/testing/fixtures/algokit-log-capture-fixture.mjs +36 -39
  111. package/testing/fixtures/algokit-log-capture-fixture.mjs.map +1 -1
  112. package/testing/fixtures/algorand-fixture.d.ts +9 -4
  113. package/testing/fixtures/algorand-fixture.js +67 -62
  114. package/testing/fixtures/algorand-fixture.js.map +1 -1
  115. package/testing/fixtures/algorand-fixture.mjs +66 -59
  116. package/testing/fixtures/algorand-fixture.mjs.map +1 -1
  117. package/testing/index.d.ts +7 -5
  118. package/testing/index.js +13 -18
  119. package/testing/index.mjs +7 -7
  120. package/testing/indexer.d.ts +5 -1
  121. package/testing/indexer.js +28 -37
  122. package/testing/indexer.js.map +1 -1
  123. package/testing/indexer.mjs +28 -35
  124. package/testing/indexer.mjs.map +1 -1
  125. package/testing/test-logger.d.ts +41 -36
  126. package/testing/test-logger.js +71 -76
  127. package/testing/test-logger.js.map +1 -1
  128. package/testing/test-logger.mjs +71 -74
  129. package/testing/test-logger.mjs.map +1 -1
  130. package/testing/transaction-logger.d.ts +30 -27
  131. package/testing/transaction-logger.js +77 -93
  132. package/testing/transaction-logger.js.map +1 -1
  133. package/testing/transaction-logger.mjs +74 -90
  134. package/testing/transaction-logger.mjs.map +1 -1
  135. package/transaction/legacy-bridge.js +100 -111
  136. package/transaction/legacy-bridge.js.map +1 -1
  137. package/transaction/legacy-bridge.mjs +97 -108
  138. package/transaction/legacy-bridge.mjs.map +1 -1
  139. package/transaction/perform-atomic-transaction-composer-simulate.d.ts +7 -5
  140. package/transaction/perform-atomic-transaction-composer-simulate.js +31 -36
  141. package/transaction/perform-atomic-transaction-composer-simulate.js.map +1 -1
  142. package/transaction/perform-atomic-transaction-composer-simulate.mjs +28 -33
  143. package/transaction/perform-atomic-transaction-composer-simulate.mjs.map +1 -1
  144. package/transaction/transaction.d.ts +34 -35
  145. package/transaction/transaction.js +716 -914
  146. package/transaction/transaction.js.map +1 -1
  147. package/transaction/transaction.mjs +713 -911
  148. package/transaction/transaction.mjs.map +1 -1
  149. package/transfer/transfer-algos.d.ts +9 -5
  150. package/transfer/transfer-algos.js +26 -27
  151. package/transfer/transfer-algos.js.map +1 -1
  152. package/transfer/transfer-algos.mjs +26 -25
  153. package/transfer/transfer-algos.mjs.map +1 -1
  154. package/transfer/transfer.d.ts +11 -8
  155. package/transfer/transfer.js +94 -100
  156. package/transfer/transfer.js.map +1 -1
  157. package/transfer/transfer.mjs +94 -98
  158. package/transfer/transfer.mjs.map +1 -1
  159. package/types/account-manager.d.ts +432 -429
  160. package/types/account-manager.js +591 -602
  161. package/types/account-manager.js.map +1 -1
  162. package/types/account-manager.mjs +587 -599
  163. package/types/account-manager.mjs.map +1 -1
  164. package/types/account.d.ts +192 -202
  165. package/types/account.js +91 -88
  166. package/types/account.js.map +1 -1
  167. package/types/account.mjs +88 -86
  168. package/types/account.mjs.map +1 -1
  169. package/types/algo-http-client-with-retry.d.ts +15 -10
  170. package/types/algo-http-client-with-retry.js +70 -92
  171. package/types/algo-http-client-with-retry.js.map +1 -1
  172. package/types/algo-http-client-with-retry.mjs +69 -90
  173. package/types/algo-http-client-with-retry.mjs.map +1 -1
  174. package/types/algorand-client-transaction-creator.d.ts +778 -771
  175. package/types/algorand-client-transaction-creator.js +733 -732
  176. package/types/algorand-client-transaction-creator.js.map +1 -1
  177. package/types/algorand-client-transaction-creator.mjs +732 -730
  178. package/types/algorand-client-transaction-creator.mjs.map +1 -1
  179. package/types/algorand-client-transaction-sender.d.ts +1090 -1360
  180. package/types/algorand-client-transaction-sender.js +930 -961
  181. package/types/algorand-client-transaction-sender.js.map +1 -1
  182. package/types/algorand-client-transaction-sender.mjs +927 -959
  183. package/types/algorand-client-transaction-sender.mjs.map +1 -1
  184. package/types/algorand-client.d.ts +239 -236
  185. package/types/algorand-client.js +322 -320
  186. package/types/algorand-client.js.map +1 -1
  187. package/types/algorand-client.mjs +321 -318
  188. package/types/algorand-client.mjs.map +1 -1
  189. package/types/amount.d.ts +47 -43
  190. package/types/amount.js +66 -70
  191. package/types/amount.js.map +1 -1
  192. package/types/amount.mjs +63 -68
  193. package/types/amount.mjs.map +1 -1
  194. package/types/app-arc56.d.ts +235 -272
  195. package/types/app-arc56.js +124 -170
  196. package/types/app-arc56.js.map +1 -1
  197. package/types/app-arc56.mjs +121 -168
  198. package/types/app-arc56.mjs.map +1 -1
  199. package/types/app-client.d.ts +1128 -2009
  200. package/types/app-client.js +1633 -1791
  201. package/types/app-client.js.map +1 -1
  202. package/types/app-client.mjs +1624 -1783
  203. package/types/app-client.mjs.map +1 -1
  204. package/types/app-deployer.d.ts +139 -141
  205. package/types/app-deployer.js +344 -384
  206. package/types/app-deployer.js.map +1 -1
  207. package/types/app-deployer.mjs +341 -382
  208. package/types/app-deployer.mjs.map +1 -1
  209. package/types/app-factory.d.ts +762 -932
  210. package/types/app-factory.js +496 -488
  211. package/types/app-factory.js.map +1 -1
  212. package/types/app-factory.mjs +491 -484
  213. package/types/app-factory.mjs.map +1 -1
  214. package/types/app-manager.d.ts +304 -310
  215. package/types/app-manager.js +422 -475
  216. package/types/app-manager.js.map +1 -1
  217. package/types/app-manager.mjs +419 -473
  218. package/types/app-manager.mjs.map +1 -1
  219. package/types/app-spec.d.ts +118 -117
  220. package/types/app-spec.js +125 -135
  221. package/types/app-spec.js.map +1 -1
  222. package/types/app-spec.mjs +121 -132
  223. package/types/app-spec.mjs.map +1 -1
  224. package/types/app.d.ts +229 -241
  225. package/types/app.js +36 -28
  226. package/types/app.js.map +1 -1
  227. package/types/app.mjs +33 -26
  228. package/types/app.mjs.map +1 -1
  229. package/types/asset-manager.d.ts +204 -199
  230. package/types/asset-manager.js +165 -174
  231. package/types/asset-manager.js.map +1 -1
  232. package/types/asset-manager.mjs +164 -172
  233. package/types/asset-manager.mjs.map +1 -1
  234. package/types/asset.d.ts +95 -91
  235. package/types/asset.js +0 -3
  236. package/types/asset.mjs +0 -2
  237. package/types/async-event-emitter.d.ts +18 -13
  238. package/types/async-event-emitter.js +37 -49
  239. package/types/async-event-emitter.js.map +1 -1
  240. package/types/async-event-emitter.mjs +36 -47
  241. package/types/async-event-emitter.mjs.map +1 -1
  242. package/types/client-manager.d.ts +453 -451
  243. package/types/client-manager.js +602 -598
  244. package/types/client-manager.js.map +1 -1
  245. package/types/client-manager.mjs +597 -594
  246. package/types/client-manager.mjs.map +1 -1
  247. package/types/composer.d.ts +1211 -1258
  248. package/types/composer.js +1436 -1498
  249. package/types/composer.js.map +1 -1
  250. package/types/composer.mjs +1430 -1493
  251. package/types/composer.mjs.map +1 -1
  252. package/types/config.d.ts +53 -48
  253. package/types/config.js +78 -77
  254. package/types/config.js.map +1 -1
  255. package/types/config.mjs +77 -75
  256. package/types/config.mjs.map +1 -1
  257. package/types/debugging.d.ts +25 -23
  258. package/types/debugging.js +9 -11
  259. package/types/debugging.js.map +1 -1
  260. package/types/debugging.mjs +8 -9
  261. package/types/debugging.mjs.map +1 -1
  262. package/types/dispenser-client.d.ts +57 -52
  263. package/types/dispenser-client.js +120 -141
  264. package/types/dispenser-client.js.map +1 -1
  265. package/types/dispenser-client.mjs +119 -139
  266. package/types/dispenser-client.mjs.map +1 -1
  267. package/types/expand.d.ts +5 -3
  268. package/types/expand.js +0 -3
  269. package/types/expand.mjs +0 -2
  270. package/types/indexer.d.ts +104 -100
  271. package/types/indexer.js +36 -31
  272. package/types/indexer.js.map +1 -1
  273. package/types/indexer.mjs +32 -30
  274. package/types/indexer.mjs.map +1 -1
  275. package/types/instance-of.d.ts +5 -3
  276. package/types/instance-of.js +0 -3
  277. package/types/instance-of.mjs +0 -2
  278. package/types/kmd-account-manager.d.ts +75 -70
  279. package/types/kmd-account-manager.js +150 -184
  280. package/types/kmd-account-manager.js.map +1 -1
  281. package/types/kmd-account-manager.mjs +147 -182
  282. package/types/kmd-account-manager.mjs.map +1 -1
  283. package/types/lifecycle-events.d.ts +13 -8
  284. package/types/lifecycle-events.js +10 -7
  285. package/types/lifecycle-events.js.map +1 -1
  286. package/types/lifecycle-events.mjs +9 -7
  287. package/types/lifecycle-events.mjs.map +1 -1
  288. package/types/logging.d.ts +15 -11
  289. package/types/logging.js +30 -31
  290. package/types/logging.js.map +1 -1
  291. package/types/logging.mjs +29 -29
  292. package/types/logging.mjs.map +1 -1
  293. package/types/logic-error.d.ts +33 -29
  294. package/types/logic-error.js +50 -49
  295. package/types/logic-error.js.map +1 -1
  296. package/types/logic-error.mjs +49 -47
  297. package/types/logic-error.mjs.map +1 -1
  298. package/types/network-client.d.ts +32 -27
  299. package/types/network-client.js +10 -9
  300. package/types/network-client.js.map +1 -1
  301. package/types/network-client.mjs +9 -7
  302. package/types/network-client.mjs.map +1 -1
  303. package/types/testing.d.ts +131 -132
  304. package/types/testing.js +0 -3
  305. package/types/testing.mjs +0 -2
  306. package/types/transaction.d.ts +110 -110
  307. package/types/transaction.js +0 -3
  308. package/types/transaction.mjs +0 -2
  309. package/types/transfer.d.ts +70 -66
  310. package/types/transfer.js +0 -3
  311. package/types/transfer.mjs +0 -2
  312. package/util.js +73 -118
  313. package/util.js.map +1 -1
  314. package/util.mjs +73 -116
  315. package/util.mjs.map +1 -1
  316. package/account/index.d.ts +0 -5
  317. package/debugging/index.d.ts +0 -1
  318. package/index.js.map +0 -1
  319. package/index.mjs.map +0 -1
  320. package/localnet/index.d.ts +0 -4
  321. package/testing/_asset.d.ts +0 -3
  322. package/testing/fixtures/index.d.ts +0 -2
  323. package/testing/index.js.map +0 -1
  324. package/testing/index.mjs.map +0 -1
  325. package/transaction/index.d.ts +0 -2
  326. package/transaction/legacy-bridge.d.ts +0 -35
  327. package/transfer/index.d.ts +0 -2
  328. package/types/asset.js.map +0 -1
  329. package/types/asset.mjs.map +0 -1
  330. package/types/expand.js.map +0 -1
  331. package/types/expand.mjs.map +0 -1
  332. package/types/instance-of.js.map +0 -1
  333. package/types/instance-of.mjs.map +0 -1
  334. package/types/testing.js.map +0 -1
  335. package/types/testing.mjs.map +0 -1
  336. package/types/transaction.js.map +0 -1
  337. package/types/transaction.mjs.map +0 -1
  338. package/types/transfer.js.map +0 -1
  339. package/types/transfer.mjs.map +0 -1
  340. package/util.d.ts +0 -48
@@ -1,488 +1,435 @@
1
- 'use strict';
2
-
3
- var algosdk = require('algosdk');
4
- var transaction = require('../transaction/transaction.js');
5
- var types_app = require('./app.js');
6
-
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_runtime = require("../_virtual/_rolldown/runtime.js");
3
+ const require_app = require("./app.js");
4
+ const require_transaction = require("../transaction/transaction.js");
5
+ let algosdk = require("algosdk");
6
+ algosdk = require_runtime.__toESM(algosdk);
7
+ //#region src/types/app-manager.ts
7
8
  /** Allows management of application information. */
8
- class AppManager {
9
- /**
10
- * Creates an `AppManager`
11
- * @param algod An algod instance
12
- */
13
- constructor(algod) {
14
- this._compilationResults = {};
15
- this._algod = algod;
16
- }
17
- /**
18
- * Compiles the given TEAL using algod and returns the result, including source map.
19
- *
20
- * The result of this compilation is also cached keyed by the TEAL
21
- * code so it can be retrieved via `getCompilationResult`.
22
- *
23
- * This function is re-entrant; it will only compile the same code once.
24
- *
25
- * @param tealCode The TEAL code
26
- * @returns The information about the compiled file
27
- * @example
28
- * ```typescript
29
- * const compiled = await appManager.compileTeal(tealProgram)
30
- * ```
31
- */
32
- async compileTeal(tealCode) {
33
- if (this._compilationResults[tealCode]) {
34
- return this._compilationResults[tealCode];
35
- }
36
- const compiled = await this._algod.compile(tealCode).sourcemap(true).do();
37
- const result = {
38
- teal: tealCode,
39
- compiled: compiled.result,
40
- compiledHash: compiled.hash,
41
- compiledBase64ToBytes: new Uint8Array(Buffer.from(compiled.result, 'base64')),
42
- sourceMap: new algosdk.ProgramSourceMap(JSON.parse(algosdk.encodeJSON(compiled.sourcemap))),
43
- };
44
- this._compilationResults[tealCode] = result;
45
- return result;
46
- }
47
- /**
48
- * Performs template substitution of a teal template and compiles it, returning the compiled result.
49
- *
50
- * Looks for `TMPL_{parameter}` for template replacements and replaces AlgoKit deploy-time control parameters
51
- * if deployment metadata is specified.
52
- *
53
- * * `TMPL_UPDATABLE` for updatability / immutability control
54
- * * `TMPL_DELETABLE` for deletability / permanence control
55
- *
56
- * @param tealTemplateCode The TEAL logic to compile
57
- * @param templateParams Any parameters to replace in the .teal file before compiling
58
- * @param deploymentMetadata The deployment metadata the app will be deployed with
59
- * @returns The information about the compiled code
60
- * @example
61
- * ```typescript
62
- * const compiled = await appManager.compileTealTemplate(tealTemplate, { TMPL_APP_ID: 12345n }, { updatable: true, deletable: false })
63
- * ```
64
- */
65
- async compileTealTemplate(tealTemplateCode, templateParams, deploymentMetadata) {
66
- let tealCode = AppManager.stripTealComments(tealTemplateCode);
67
- tealCode = AppManager.replaceTealTemplateParams(tealCode, templateParams);
68
- if (deploymentMetadata) {
69
- tealCode = AppManager.replaceTealTemplateDeployTimeControlParams(tealCode, deploymentMetadata);
70
- }
71
- return await this.compileTeal(tealCode);
72
- }
73
- /**
74
- * Returns a previous compilation result.
75
- * @param tealCode The TEAL code
76
- * @returns The information about the previously compiled file
77
- * or `undefined` if that TEAL code wasn't previously compiled
78
- * @example
79
- * ```typescript
80
- * const compiled = appManager.getCompilationResult(tealProgram)
81
- * ```
82
- */
83
- getCompilationResult(tealCode) {
84
- return this._compilationResults[tealCode];
85
- }
86
- /**
87
- * Returns the current app information for the app with the given ID.
88
- *
89
- * @example
90
- * ```typescript
91
- * const appInfo = await appManager.getById(12353n);
92
- * ```
93
- *
94
- * @param appId The ID of the app
95
- * @returns The app information
96
- */
97
- async getById(appId) {
98
- const app = await this._algod.getApplicationByID(Number(appId)).do();
99
- return {
100
- appId: BigInt(app.id),
101
- appAddress: algosdk.getApplicationAddress(app.id),
102
- approvalProgram: app.params.approvalProgram,
103
- clearStateProgram: app.params.clearStateProgram,
104
- creator: app.params.creator,
105
- localInts: Number(app.params.localStateSchema?.numUint ?? 0),
106
- localByteSlices: Number(app.params.localStateSchema?.numByteSlice ?? 0),
107
- globalInts: Number(app.params.globalStateSchema?.numUint ?? 0),
108
- globalByteSlices: Number(app.params.globalStateSchema?.numByteSlice ?? 0),
109
- extraProgramPages: Number(app.params.extraProgramPages ?? 0),
110
- globalState: AppManager.decodeAppState(app.params.globalState ?? []),
111
- version: app.params.version,
112
- };
113
- }
114
- /**
115
- * Returns the current global state values for the given app ID and account address
116
- *
117
- * @param appId The ID of the app to return global state for
118
- * @returns The current global state for the given app
119
- * @example
120
- * ```typescript
121
- * const globalState = await appManager.getGlobalState(12353n);
122
- * ```
123
- */
124
- async getGlobalState(appId) {
125
- return (await this.getById(appId)).globalState;
126
- }
127
- /**
128
- * Returns the current local state values for the given app ID and account address
129
- *
130
- * @param appId The ID of the app to return local state for
131
- * @param address The string address of the account to get local state for the given app
132
- * @returns The current local state for the given (app, account) combination
133
- * @example
134
- * ```typescript
135
- * const localState = await appManager.getLocalState(12353n, 'ACCOUNTADDRESS');
136
- * ```
137
- */
138
- async getLocalState(appId, address) {
139
- const appInfo = await this._algod.accountApplicationInformation(address, appId).do();
140
- if (!appInfo.appLocalState?.keyValue) {
141
- throw new Error("Couldn't find local state");
142
- }
143
- return AppManager.decodeAppState(appInfo.appLocalState.keyValue);
144
- }
145
- /**
146
- * Returns the names of the current boxes for the given app.
147
- * @param appId The ID of the app return box names for
148
- * @returns The current box names
149
- * @example
150
- * ```typescript
151
- * const boxNames = await appManager.getBoxNames(12353n);
152
- * ```
153
- */
154
- async getBoxNames(appId) {
155
- const boxResult = await this._algod.getApplicationBoxes(appId).do();
156
- return boxResult.boxes.map((b) => {
157
- return {
158
- nameRaw: b.name,
159
- nameBase64: Buffer.from(b.name).toString('base64'),
160
- name: Buffer.from(b.name).toString('utf-8'),
161
- };
162
- });
163
- }
164
- /**
165
- * Returns the value of the given box name for the given app.
166
- * @param appId The ID of the app return box names for
167
- * @param boxName The name of the box to return either as a string, binary array or `BoxName`
168
- * @returns The current box value as a byte array
169
- * @example
170
- * ```typescript
171
- * const boxValue = await appManager.getBoxValue(12353n, 'boxName');
172
- * ```
173
- */
174
- async getBoxValue(appId, boxName) {
175
- const boxId = typeof boxName === 'object' && 'nameRaw' in boxName ? boxName.nameRaw : boxName;
176
- const name = AppManager.getBoxReference(boxId).name;
177
- const boxResult = await this._algod.getApplicationBoxByName(Number(appId), name).do();
178
- return boxResult.value;
179
- }
180
- /**
181
- * Returns the value of the given box names for the given app.
182
- * @param appId The ID of the app return box names for
183
- * @param boxNames The names of the boxes to return either as a string, binary array or `BoxName`
184
- * @returns The current box values as a byte array in the same order as the passed in box names
185
- * @example
186
- * ```typescript
187
- * const boxValues = await appManager.getBoxValues(12353n, ['boxName1', 'boxName2']);
188
- * ```
189
- */
190
- async getBoxValues(appId, boxNames) {
191
- return await Promise.all(boxNames.map(async (boxName) => await this.getBoxValue(appId, boxName)));
192
- }
193
- /**
194
- * Returns the value of the given box name for the given app decoded based on the given ABI type.
195
- * @param request The parameters for the box value request
196
- * @returns The current box value as an ABI value
197
- * @example
198
- * ```typescript
199
- * const boxValue = await appManager.getBoxValueFromABIType({ appId: 12353n, boxName: 'boxName', type: new ABIUintType(32) });
200
- * ```
201
- */
202
- async getBoxValueFromABIType(request) {
203
- const { appId, boxName, type } = request;
204
- const value = await this.getBoxValue(appId, boxName);
205
- return type.decode(value);
206
- }
207
- /**
208
- * Returns the value of the given box names for the given app decoded based on the given ABI type.
209
- * @param request The parameters for the box value request
210
- * @returns The current box values as an ABI value in the same order as the passed in box names
211
- * @example
212
- * ```typescript
213
- * const boxValues = await appManager.getBoxValuesFromABIType({ appId: 12353n, boxNames: ['boxName1', 'boxName2'], type: new ABIUintType(32) });
214
- * ```
215
- */
216
- async getBoxValuesFromABIType(request) {
217
- const { appId, boxNames, type } = request;
218
- return await Promise.all(boxNames.map(async (boxName) => await this.getBoxValueFromABIType({ appId, boxName, type })));
219
- }
220
- /**
221
- * Returns a `algosdk.BoxReference` given a `BoxIdentifier` or `BoxReference`.
222
- * @param boxId The box to return a reference for
223
- * @returns The box reference ready to pass into a `algosdk.Transaction`
224
- * @example
225
- * ```typescript
226
- * const boxRef = AppManager.getBoxReference('boxName');
227
- * ```
228
- */
229
- static getBoxReference(boxId) {
230
- const ref = typeof boxId === 'object' && 'appId' in boxId ? boxId : { appId: 0n, name: boxId };
231
- return {
232
- appIndex: ref.appId,
233
- name: typeof ref.name === 'string' ? new TextEncoder().encode(ref.name) : 'length' in ref.name ? ref.name : ref.name.addr.publicKey,
234
- };
235
- }
236
- /**
237
- * Converts an array of global/local state values from the algod api to a more friendly
238
- * generic object keyed by the UTF-8 value of the key.
239
- * @param state A `global-state`, `local-state`, `global-state-deltas` or `local-state-deltas`
240
- * @returns An object keyeed by the UTF-8 representation of the key with various parsings of the values
241
- * @example
242
- * ```typescript
243
- * const stateValues = AppManager.decodeAppState(state);
244
- * ```
245
- */
246
- static decodeAppState(state) {
247
- const stateValues = {};
248
- // Start with empty set
249
- for (const stateVal of state) {
250
- const keyBase64 = Buffer.from(stateVal.key).toString('base64');
251
- const keyRaw = stateVal.key;
252
- const key = Buffer.from(stateVal.key).toString('utf-8');
253
- const tealValue = stateVal.value;
254
- const dataTypeFlag = 'action' in tealValue ? tealValue.action : tealValue.type;
255
- let valueBase64;
256
- let valueRaw;
257
- switch (dataTypeFlag) {
258
- case 1:
259
- valueBase64 =
260
- typeof tealValue.bytes === 'string' ? tealValue.bytes : tealValue.bytes ? Buffer.from(tealValue.bytes).toString('base64') : '';
261
- valueRaw = Buffer.from(valueBase64, 'base64');
262
- stateValues[key] = {
263
- keyRaw,
264
- keyBase64,
265
- valueRaw: new Uint8Array(valueRaw),
266
- valueBase64: valueBase64,
267
- value: valueRaw.toString('utf-8'),
268
- };
269
- break;
270
- case 2: {
271
- const value = tealValue.uint ?? 0;
272
- stateValues[key] = {
273
- keyRaw,
274
- keyBase64,
275
- value: BigInt(value),
276
- };
277
- break;
278
- }
279
- default:
280
- throw new Error(`Received unknown state data type of ${dataTypeFlag}`);
281
- }
282
- }
283
- return stateValues;
284
- }
285
- /**
286
- * Returns any ABI return values for the given app call arguments and transaction confirmation.
287
- * @param confirmation The transaction confirmation from algod
288
- * @param method The ABI method
289
- * @returns The return value for the method call
290
- * @example
291
- * ```typescript
292
- * const returnValue = AppManager.getABIReturn(confirmation, ABIMethod.fromSignature('hello(string)void'));
293
- * ```
294
- */
295
- static getABIReturn(confirmation, method) {
296
- if (!method || !confirmation || method.returns.type === 'void') {
297
- return undefined;
298
- }
299
- // The parseMethodResponse method mutates the second parameter :(
300
- const resultDummy = {
301
- txID: '',
302
- method,
303
- rawReturnValue: new Uint8Array(),
304
- };
305
- return transaction.getABIReturnValue(algosdk.AtomicTransactionComposer.parseMethodResponse(method, resultDummy, confirmation), method.returns.type);
306
- }
307
- /**
308
- * Replaces AlgoKit deploy-time deployment control parameters within the given TEAL template code.
309
- *
310
- * * `TMPL_UPDATABLE` for updatability / immutability control
311
- * * `TMPL_DELETABLE` for deletability / permanence control
312
- *
313
- * Note: If these values are defined, but the corresponding `TMPL_*` value
314
- * isn't in the teal code it will throw an exception.
315
- *
316
- * @param tealTemplateCode The TEAL template code to substitute
317
- * @param params The deploy-time deployment control parameter value to replace
318
- * @returns The replaced TEAL code
319
- * @example
320
- * ```typescript
321
- * const tealCode = AppManager.replaceTealTemplateDeployTimeControlParams(tealTemplate, { updatable: true, deletable: false });
322
- * ```
323
- */
324
- static replaceTealTemplateDeployTimeControlParams(tealTemplateCode, params) {
325
- if (params.updatable !== undefined) {
326
- if (!tealTemplateCode.includes(types_app.UPDATABLE_TEMPLATE_NAME)) {
327
- throw new Error(`Deploy-time updatability control requested for app deployment, but ${types_app.UPDATABLE_TEMPLATE_NAME} not present in TEAL code`);
328
- }
329
- tealTemplateCode = replaceTemplateVariable(tealTemplateCode, types_app.UPDATABLE_TEMPLATE_NAME, (params.updatable ? 1 : 0).toString());
330
- }
331
- if (params.deletable !== undefined) {
332
- if (!tealTemplateCode.includes(types_app.DELETABLE_TEMPLATE_NAME)) {
333
- throw new Error(`Deploy-time deletability control requested for app deployment, but ${types_app.DELETABLE_TEMPLATE_NAME} not present in TEAL code`);
334
- }
335
- tealTemplateCode = replaceTemplateVariable(tealTemplateCode, types_app.DELETABLE_TEMPLATE_NAME, (params.deletable ? 1 : 0).toString());
336
- }
337
- return tealTemplateCode;
338
- }
339
- /**
340
- * Performs template substitution of a teal file.
341
- *
342
- * Looks for `TMPL_{parameter}` for template replacements.
343
- *
344
- * @param tealTemplateCode The TEAL template code to make parameter replacements in
345
- * @param templateParams Any parameters to replace in the teal code
346
- * @returns The TEAL code with replacements
347
- * @example
348
- * ```typescript
349
- * const tealCode = AppManager.replaceTealTemplateParams(tealTemplate, { TMPL_APP_ID: 12345n });
350
- * ```
351
- */
352
- static replaceTealTemplateParams(tealTemplateCode, templateParams) {
353
- if (templateParams !== undefined) {
354
- for (const key in templateParams) {
355
- const value = templateParams[key];
356
- const token = `TMPL_${key.replace(/^TMPL_/, '')}`;
357
- // If this is a number, first replace any byte representations of the number
358
- // These may appear in the TEAL in order to circumvent int compression and preserve PC values
359
- if (typeof value === 'number' || typeof value === 'bigint') {
360
- tealTemplateCode = tealTemplateCode.replace(new RegExp(`(?<=bytes )${token}`, 'g'), `0x${value.toString(16).padStart(16, '0')}`);
361
- // We could probably return here since mixing pushint and pushbytes is likely not going to happen, but might as well do both
362
- }
363
- tealTemplateCode = replaceTemplateVariable(tealTemplateCode, token, typeof value === 'string'
364
- ? `0x${Buffer.from(value, 'utf-8').toString('hex')}`
365
- : ArrayBuffer.isView(value)
366
- ? `0x${Buffer.from(value).toString('hex')}`
367
- : value.toString());
368
- }
369
- }
370
- return tealTemplateCode;
371
- }
372
- /**
373
- * Remove comments from TEAL code (useful to reduce code size before compilation).
374
- *
375
- * @param tealCode The TEAL logic to strip
376
- * @returns The TEAL without comments
377
- * @example
378
- * ```typescript
379
- * const stripped = AppManager.stripTealComments(tealProgram);
380
- * ```
381
- */
382
- static stripTealComments(tealCode) {
383
- const stripCommentFromLine = (line) => {
384
- const commentIndex = findUnquotedString(line, '//');
385
- if (commentIndex === undefined) {
386
- return line;
387
- }
388
- return line.slice(0, commentIndex).trimEnd();
389
- };
390
- return tealCode
391
- .split('\n')
392
- .map((line) => stripCommentFromLine(line))
393
- .join('\n');
394
- }
395
- }
9
+ var AppManager = class AppManager {
10
+ _algod;
11
+ _compilationResults = {};
12
+ /**
13
+ * Creates an `AppManager`
14
+ * @param algod An algod instance
15
+ */
16
+ constructor(algod) {
17
+ this._algod = algod;
18
+ }
19
+ /**
20
+ * Compiles the given TEAL using algod and returns the result, including source map.
21
+ *
22
+ * The result of this compilation is also cached keyed by the TEAL
23
+ * code so it can be retrieved via `getCompilationResult`.
24
+ *
25
+ * This function is re-entrant; it will only compile the same code once.
26
+ *
27
+ * @param tealCode The TEAL code
28
+ * @returns The information about the compiled file
29
+ * @example
30
+ * ```typescript
31
+ * const compiled = await appManager.compileTeal(tealProgram)
32
+ * ```
33
+ */
34
+ async compileTeal(tealCode) {
35
+ if (this._compilationResults[tealCode]) return this._compilationResults[tealCode];
36
+ const compiled = await this._algod.compile(tealCode).sourcemap(true).do();
37
+ const result = {
38
+ teal: tealCode,
39
+ compiled: compiled.result,
40
+ compiledHash: compiled.hash,
41
+ compiledBase64ToBytes: new Uint8Array(Buffer.from(compiled.result, "base64")),
42
+ sourceMap: new algosdk.ProgramSourceMap(JSON.parse(algosdk.default.encodeJSON(compiled.sourcemap)))
43
+ };
44
+ this._compilationResults[tealCode] = result;
45
+ return result;
46
+ }
47
+ /**
48
+ * Performs template substitution of a teal template and compiles it, returning the compiled result.
49
+ *
50
+ * Looks for `TMPL_{parameter}` for template replacements and replaces AlgoKit deploy-time control parameters
51
+ * if deployment metadata is specified.
52
+ *
53
+ * * `TMPL_UPDATABLE` for updatability / immutability control
54
+ * * `TMPL_DELETABLE` for deletability / permanence control
55
+ *
56
+ * @param tealTemplateCode The TEAL logic to compile
57
+ * @param templateParams Any parameters to replace in the .teal file before compiling
58
+ * @param deploymentMetadata The deployment metadata the app will be deployed with
59
+ * @returns The information about the compiled code
60
+ * @example
61
+ * ```typescript
62
+ * const compiled = await appManager.compileTealTemplate(tealTemplate, { TMPL_APP_ID: 12345n }, { updatable: true, deletable: false })
63
+ * ```
64
+ */
65
+ async compileTealTemplate(tealTemplateCode, templateParams, deploymentMetadata) {
66
+ let tealCode = AppManager.stripTealComments(tealTemplateCode);
67
+ tealCode = AppManager.replaceTealTemplateParams(tealCode, templateParams);
68
+ if (deploymentMetadata) tealCode = AppManager.replaceTealTemplateDeployTimeControlParams(tealCode, deploymentMetadata);
69
+ return await this.compileTeal(tealCode);
70
+ }
71
+ /**
72
+ * Returns a previous compilation result.
73
+ * @param tealCode The TEAL code
74
+ * @returns The information about the previously compiled file
75
+ * or `undefined` if that TEAL code wasn't previously compiled
76
+ * @example
77
+ * ```typescript
78
+ * const compiled = appManager.getCompilationResult(tealProgram)
79
+ * ```
80
+ */
81
+ getCompilationResult(tealCode) {
82
+ return this._compilationResults[tealCode];
83
+ }
84
+ /**
85
+ * Returns the current app information for the app with the given ID.
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * const appInfo = await appManager.getById(12353n);
90
+ * ```
91
+ *
92
+ * @param appId The ID of the app
93
+ * @returns The app information
94
+ */
95
+ async getById(appId) {
96
+ const app = await this._algod.getApplicationByID(Number(appId)).do();
97
+ const params = app.params;
98
+ return {
99
+ appId: BigInt(app.id),
100
+ appAddress: algosdk.default.getApplicationAddress(app.id),
101
+ approvalProgram: params.approvalProgram,
102
+ clearStateProgram: params.clearStateProgram,
103
+ creator: params.creator,
104
+ localInts: Number(params.localStateSchema?.numUint ?? 0),
105
+ localByteSlices: Number(params.localStateSchema?.numByteSlice ?? 0),
106
+ globalInts: Number(params.globalStateSchema?.numUint ?? 0),
107
+ globalByteSlices: Number(params.globalStateSchema?.numByteSlice ?? 0),
108
+ extraProgramPages: Number(params.extraProgramPages ?? 0),
109
+ globalState: AppManager.decodeAppState(params.globalState ?? []),
110
+ version: params.version
111
+ };
112
+ }
113
+ /**
114
+ * Returns the current global state values for the given app ID and account address
115
+ *
116
+ * @param appId The ID of the app to return global state for
117
+ * @returns The current global state for the given app
118
+ * @example
119
+ * ```typescript
120
+ * const globalState = await appManager.getGlobalState(12353n);
121
+ * ```
122
+ */
123
+ async getGlobalState(appId) {
124
+ return (await this.getById(appId)).globalState;
125
+ }
126
+ /**
127
+ * Returns the current local state values for the given app ID and account address
128
+ *
129
+ * @param appId The ID of the app to return local state for
130
+ * @param address The string address of the account to get local state for the given app
131
+ * @returns The current local state for the given (app, account) combination
132
+ * @example
133
+ * ```typescript
134
+ * const localState = await appManager.getLocalState(12353n, 'ACCOUNTADDRESS');
135
+ * ```
136
+ */
137
+ async getLocalState(appId, address) {
138
+ const appInfo = await this._algod.accountApplicationInformation(address, appId).do();
139
+ if (!appInfo.appLocalState?.keyValue) throw new Error("Couldn't find local state");
140
+ return AppManager.decodeAppState(appInfo.appLocalState.keyValue);
141
+ }
142
+ /**
143
+ * Returns the names of the current boxes for the given app.
144
+ * @param appId The ID of the app return box names for
145
+ * @returns The current box names
146
+ * @example
147
+ * ```typescript
148
+ * const boxNames = await appManager.getBoxNames(12353n);
149
+ * ```
150
+ */
151
+ async getBoxNames(appId) {
152
+ return (await this._algod.getApplicationBoxes(appId).do()).boxes.map((b) => {
153
+ return {
154
+ nameRaw: b.name,
155
+ nameBase64: Buffer.from(b.name).toString("base64"),
156
+ name: Buffer.from(b.name).toString("utf-8")
157
+ };
158
+ });
159
+ }
160
+ /**
161
+ * Returns the value of the given box name for the given app.
162
+ * @param appId The ID of the app return box names for
163
+ * @param boxName The name of the box to return either as a string, binary array or `BoxName`
164
+ * @returns The current box value as a byte array
165
+ * @example
166
+ * ```typescript
167
+ * const boxValue = await appManager.getBoxValue(12353n, 'boxName');
168
+ * ```
169
+ */
170
+ async getBoxValue(appId, boxName) {
171
+ const boxId = typeof boxName === "object" && "nameRaw" in boxName ? boxName.nameRaw : boxName;
172
+ const name = AppManager.getBoxReference(boxId).name;
173
+ return (await this._algod.getApplicationBoxByName(Number(appId), name).do()).value;
174
+ }
175
+ /**
176
+ * Returns the value of the given box names for the given app.
177
+ * @param appId The ID of the app return box names for
178
+ * @param boxNames The names of the boxes to return either as a string, binary array or `BoxName`
179
+ * @returns The current box values as a byte array in the same order as the passed in box names
180
+ * @example
181
+ * ```typescript
182
+ * const boxValues = await appManager.getBoxValues(12353n, ['boxName1', 'boxName2']);
183
+ * ```
184
+ */
185
+ async getBoxValues(appId, boxNames) {
186
+ return await Promise.all(boxNames.map(async (boxName) => await this.getBoxValue(appId, boxName)));
187
+ }
188
+ /**
189
+ * Returns the value of the given box name for the given app decoded based on the given ABI type.
190
+ * @param request The parameters for the box value request
191
+ * @returns The current box value as an ABI value
192
+ * @example
193
+ * ```typescript
194
+ * const boxValue = await appManager.getBoxValueFromABIType({ appId: 12353n, boxName: 'boxName', type: new ABIUintType(32) });
195
+ * ```
196
+ */
197
+ async getBoxValueFromABIType(request) {
198
+ const { appId, boxName, type } = request;
199
+ const value = await this.getBoxValue(appId, boxName);
200
+ return type.decode(value);
201
+ }
202
+ /**
203
+ * Returns the value of the given box names for the given app decoded based on the given ABI type.
204
+ * @param request The parameters for the box value request
205
+ * @returns The current box values as an ABI value in the same order as the passed in box names
206
+ * @example
207
+ * ```typescript
208
+ * const boxValues = await appManager.getBoxValuesFromABIType({ appId: 12353n, boxNames: ['boxName1', 'boxName2'], type: new ABIUintType(32) });
209
+ * ```
210
+ */
211
+ async getBoxValuesFromABIType(request) {
212
+ const { appId, boxNames, type } = request;
213
+ return await Promise.all(boxNames.map(async (boxName) => await this.getBoxValueFromABIType({
214
+ appId,
215
+ boxName,
216
+ type
217
+ })));
218
+ }
219
+ /**
220
+ * Returns a `algosdk.BoxReference` given a `BoxIdentifier` or `BoxReference`.
221
+ * @param boxId The box to return a reference for
222
+ * @returns The box reference ready to pass into a `algosdk.Transaction`
223
+ * @example
224
+ * ```typescript
225
+ * const boxRef = AppManager.getBoxReference('boxName');
226
+ * ```
227
+ */
228
+ static getBoxReference(boxId) {
229
+ const ref = typeof boxId === "object" && "appId" in boxId ? boxId : {
230
+ appId: 0n,
231
+ name: boxId
232
+ };
233
+ return {
234
+ appIndex: ref.appId,
235
+ name: typeof ref.name === "string" ? new TextEncoder().encode(ref.name) : "length" in ref.name ? ref.name : ref.name.addr.publicKey
236
+ };
237
+ }
238
+ /**
239
+ * Converts an array of global/local state values from the algod api to a more friendly
240
+ * generic object keyed by the UTF-8 value of the key.
241
+ * @param state A `global-state`, `local-state`, `global-state-deltas` or `local-state-deltas`
242
+ * @returns An object keyeed by the UTF-8 representation of the key with various parsings of the values
243
+ * @example
244
+ * ```typescript
245
+ * const stateValues = AppManager.decodeAppState(state);
246
+ * ```
247
+ */
248
+ static decodeAppState(state) {
249
+ const stateValues = {};
250
+ for (const stateVal of state) {
251
+ const keyBase64 = Buffer.from(stateVal.key).toString("base64");
252
+ const keyRaw = stateVal.key;
253
+ const key = Buffer.from(stateVal.key).toString("utf-8");
254
+ const tealValue = stateVal.value;
255
+ const dataTypeFlag = "action" in tealValue ? tealValue.action : tealValue.type;
256
+ let valueBase64;
257
+ let valueRaw;
258
+ switch (dataTypeFlag) {
259
+ case 1:
260
+ valueBase64 = typeof tealValue.bytes === "string" ? tealValue.bytes : tealValue.bytes ? Buffer.from(tealValue.bytes).toString("base64") : "";
261
+ valueRaw = Buffer.from(valueBase64, "base64");
262
+ stateValues[key] = {
263
+ keyRaw,
264
+ keyBase64,
265
+ valueRaw: new Uint8Array(valueRaw),
266
+ valueBase64,
267
+ value: valueRaw.toString("utf-8")
268
+ };
269
+ break;
270
+ case 2:
271
+ stateValues[key] = {
272
+ keyRaw,
273
+ keyBase64,
274
+ value: BigInt(tealValue.uint ?? 0)
275
+ };
276
+ break;
277
+ default: throw new Error(`Received unknown state data type of ${dataTypeFlag}`);
278
+ }
279
+ }
280
+ return stateValues;
281
+ }
282
+ /**
283
+ * Returns any ABI return values for the given app call arguments and transaction confirmation.
284
+ * @param confirmation The transaction confirmation from algod
285
+ * @param method The ABI method
286
+ * @returns The return value for the method call
287
+ * @example
288
+ * ```typescript
289
+ * const returnValue = AppManager.getABIReturn(confirmation, ABIMethod.fromSignature('hello(string)void'));
290
+ * ```
291
+ */
292
+ static getABIReturn(confirmation, method) {
293
+ if (!method || !confirmation || method.returns.type === "void") return;
294
+ return require_transaction.getABIReturnValue(algosdk.default.AtomicTransactionComposer.parseMethodResponse(method, {
295
+ txID: "",
296
+ method,
297
+ rawReturnValue: /* @__PURE__ */ new Uint8Array()
298
+ }, confirmation), method.returns.type);
299
+ }
300
+ /**
301
+ * Replaces AlgoKit deploy-time deployment control parameters within the given TEAL template code.
302
+ *
303
+ * * `TMPL_UPDATABLE` for updatability / immutability control
304
+ * * `TMPL_DELETABLE` for deletability / permanence control
305
+ *
306
+ * Note: If these values are defined, but the corresponding `TMPL_*` value
307
+ * isn't in the teal code it will throw an exception.
308
+ *
309
+ * @param tealTemplateCode The TEAL template code to substitute
310
+ * @param params The deploy-time deployment control parameter value to replace
311
+ * @returns The replaced TEAL code
312
+ * @example
313
+ * ```typescript
314
+ * const tealCode = AppManager.replaceTealTemplateDeployTimeControlParams(tealTemplate, { updatable: true, deletable: false });
315
+ * ```
316
+ */
317
+ static replaceTealTemplateDeployTimeControlParams(tealTemplateCode, params) {
318
+ if (params.updatable !== void 0) {
319
+ if (!tealTemplateCode.includes("TMPL_UPDATABLE")) throw new Error(`Deploy-time updatability control requested for app deployment, but ${require_app.UPDATABLE_TEMPLATE_NAME} not present in TEAL code`);
320
+ tealTemplateCode = replaceTemplateVariable(tealTemplateCode, require_app.UPDATABLE_TEMPLATE_NAME, (params.updatable ? 1 : 0).toString());
321
+ }
322
+ if (params.deletable !== void 0) {
323
+ if (!tealTemplateCode.includes("TMPL_DELETABLE")) throw new Error(`Deploy-time deletability control requested for app deployment, but ${require_app.DELETABLE_TEMPLATE_NAME} not present in TEAL code`);
324
+ tealTemplateCode = replaceTemplateVariable(tealTemplateCode, require_app.DELETABLE_TEMPLATE_NAME, (params.deletable ? 1 : 0).toString());
325
+ }
326
+ return tealTemplateCode;
327
+ }
328
+ /**
329
+ * Performs template substitution of a teal file.
330
+ *
331
+ * Looks for `TMPL_{parameter}` for template replacements.
332
+ *
333
+ * @param tealTemplateCode The TEAL template code to make parameter replacements in
334
+ * @param templateParams Any parameters to replace in the teal code
335
+ * @returns The TEAL code with replacements
336
+ * @example
337
+ * ```typescript
338
+ * const tealCode = AppManager.replaceTealTemplateParams(tealTemplate, { TMPL_APP_ID: 12345n });
339
+ * ```
340
+ */
341
+ static replaceTealTemplateParams(tealTemplateCode, templateParams) {
342
+ if (templateParams !== void 0) for (const key in templateParams) {
343
+ const value = templateParams[key];
344
+ const token = `TMPL_${key.replace(/^TMPL_/, "")}`;
345
+ if (typeof value === "number" || typeof value === "bigint") tealTemplateCode = tealTemplateCode.replace(new RegExp(`(?<=bytes )${token}`, "g"), `0x${value.toString(16).padStart(16, "0")}`);
346
+ tealTemplateCode = replaceTemplateVariable(tealTemplateCode, token, typeof value === "string" ? `0x${Buffer.from(value, "utf-8").toString("hex")}` : ArrayBuffer.isView(value) ? `0x${Buffer.from(value).toString("hex")}` : value.toString());
347
+ }
348
+ return tealTemplateCode;
349
+ }
350
+ /**
351
+ * Remove comments from TEAL code (useful to reduce code size before compilation).
352
+ *
353
+ * @param tealCode The TEAL logic to strip
354
+ * @returns The TEAL without comments
355
+ * @example
356
+ * ```typescript
357
+ * const stripped = AppManager.stripTealComments(tealProgram);
358
+ * ```
359
+ */
360
+ static stripTealComments(tealCode) {
361
+ const stripCommentFromLine = (line) => {
362
+ const commentIndex = findUnquotedString(line, "//");
363
+ if (commentIndex === void 0) return line;
364
+ return line.slice(0, commentIndex).trimEnd();
365
+ };
366
+ return tealCode.split("\n").map((line) => stripCommentFromLine(line)).join("\n");
367
+ }
368
+ };
396
369
  /**
397
- * Find the first string within a line of TEAL. Only matches outside of quotes and base64 are returned.
398
- * Returns undefined if not found
399
- */
370
+ * Find the first string within a line of TEAL. Only matches outside of quotes and base64 are returned.
371
+ * Returns undefined if not found
372
+ */
400
373
  const findUnquotedString = (line, token, startIndex = 0, _endIndex) => {
401
- const endIndex = _endIndex === undefined ? line.length : _endIndex;
402
- let index = startIndex;
403
- let inQuotes = false;
404
- let inBase64 = false;
405
- while (index < endIndex) {
406
- const currentChar = line[index];
407
- if ((currentChar === ' ' || currentChar === '(') && !inQuotes && lastTokenBase64(line, index)) {
408
- // enter base64
409
- inBase64 = true;
410
- }
411
- else if ((currentChar === ' ' || currentChar === ')') && !inQuotes && inBase64) {
412
- // exit base64
413
- inBase64 = false;
414
- }
415
- else if (currentChar === '\\' && inQuotes) {
416
- // escaped char, skip next character
417
- index += 1;
418
- }
419
- else if (currentChar === '"') {
420
- // quote boundary
421
- inQuotes = !inQuotes;
422
- }
423
- else if (!inQuotes && !inBase64 && line.startsWith(token, index)) {
424
- // can test for match
425
- return index;
426
- }
427
- index += 1;
428
- }
429
- return undefined;
374
+ const endIndex = _endIndex === void 0 ? line.length : _endIndex;
375
+ let index = startIndex;
376
+ let inQuotes = false;
377
+ let inBase64 = false;
378
+ while (index < endIndex) {
379
+ const currentChar = line[index];
380
+ if ((currentChar === " " || currentChar === "(") && !inQuotes && lastTokenBase64(line, index)) inBase64 = true;
381
+ else if ((currentChar === " " || currentChar === ")") && !inQuotes && inBase64) inBase64 = false;
382
+ else if (currentChar === "\\" && inQuotes) index += 1;
383
+ else if (currentChar === "\"") inQuotes = !inQuotes;
384
+ else if (!inQuotes && !inBase64 && line.startsWith(token, index)) return index;
385
+ index += 1;
386
+ }
430
387
  };
431
388
  const lastTokenBase64 = (line, index) => {
432
- try {
433
- const tokens = line.slice(0, index).split(/\s+/);
434
- const last = tokens[tokens.length - 1];
435
- return ['base64', 'b64'].includes(last);
436
- }
437
- catch {
438
- return false;
439
- }
389
+ try {
390
+ const tokens = line.slice(0, index).split(/\s+/);
391
+ const last = tokens[tokens.length - 1];
392
+ return ["base64", "b64"].includes(last);
393
+ } catch {
394
+ return false;
395
+ }
440
396
  };
441
397
  function replaceTemplateVariable(program, token, replacement) {
442
- const result = [];
443
- const tokenIndexOffset = replacement.length - token.length;
444
- const programLines = program.split('\n');
445
- for (const line of programLines) {
446
- const _commentIndex = findUnquotedString(line, '//');
447
- const commentIndex = _commentIndex === undefined ? line.length : _commentIndex;
448
- let code = line.substring(0, commentIndex);
449
- const comment = line.substring(commentIndex);
450
- let trailingIndex = 0;
451
- while (true) {
452
- const tokenIndex = findTemplateToken(code, token, trailingIndex);
453
- if (tokenIndex === undefined) {
454
- break;
455
- }
456
- trailingIndex = tokenIndex + token.length;
457
- const prefix = code.substring(0, tokenIndex);
458
- const suffix = code.substring(trailingIndex);
459
- code = `${prefix}${replacement}${suffix}`;
460
- trailingIndex += tokenIndexOffset;
461
- }
462
- result.push(code + comment);
463
- }
464
- return result.join('\n');
398
+ const result = [];
399
+ const tokenIndexOffset = replacement.length - token.length;
400
+ const programLines = program.split("\n");
401
+ for (const line of programLines) {
402
+ const _commentIndex = findUnquotedString(line, "//");
403
+ const commentIndex = _commentIndex === void 0 ? line.length : _commentIndex;
404
+ let code = line.substring(0, commentIndex);
405
+ const comment = line.substring(commentIndex);
406
+ let trailingIndex = 0;
407
+ while (true) {
408
+ const tokenIndex = findTemplateToken(code, token, trailingIndex);
409
+ if (tokenIndex === void 0) break;
410
+ trailingIndex = tokenIndex + token.length;
411
+ code = `${code.substring(0, tokenIndex)}${replacement}${code.substring(trailingIndex)}`;
412
+ trailingIndex += tokenIndexOffset;
413
+ }
414
+ result.push(code + comment);
415
+ }
416
+ return result.join("\n");
465
417
  }
466
418
  const findTemplateToken = (line, token, startIndex = 0, _endIndex) => {
467
- const endIndex = line.length ;
468
- let index = startIndex;
469
- while (index < endIndex) {
470
- const tokenIndex = findUnquotedString(line, token, index, endIndex);
471
- if (tokenIndex === undefined) {
472
- break;
473
- }
474
- const trailingIndex = tokenIndex + token.length;
475
- if ((tokenIndex === 0 || !isValidTokenCharacter(line[tokenIndex - 1])) &&
476
- (trailingIndex >= line.length || !isValidTokenCharacter(line[trailingIndex]))) {
477
- return tokenIndex;
478
- }
479
- index = trailingIndex;
480
- }
481
- return undefined;
419
+ const endIndex = _endIndex === void 0 ? line.length : _endIndex;
420
+ let index = startIndex;
421
+ while (index < endIndex) {
422
+ const tokenIndex = findUnquotedString(line, token, index, endIndex);
423
+ if (tokenIndex === void 0) break;
424
+ const trailingIndex = tokenIndex + token.length;
425
+ if ((tokenIndex === 0 || !isValidTokenCharacter(line[tokenIndex - 1])) && (trailingIndex >= line.length || !isValidTokenCharacter(line[trailingIndex]))) return tokenIndex;
426
+ index = trailingIndex;
427
+ }
482
428
  };
483
429
  function isValidTokenCharacter(char) {
484
- return char.length === 1 && (/\w/.test(char) || char === '_');
430
+ return char.length === 1 && (/\w/.test(char) || char === "_");
485
431
  }
486
-
432
+ //#endregion
487
433
  exports.AppManager = AppManager;
488
- //# sourceMappingURL=app-manager.js.map
434
+
435
+ //# sourceMappingURL=app-manager.js.map