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