@algorandfoundation/algokit-utils 6.3.0-beta.1 → 7.0.0-beta.10

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 (360) hide show
  1. package/README.md +3 -25
  2. package/account/account.d.ts +8 -9
  3. package/account/account.js +7 -7
  4. package/account/account.js.map +1 -1
  5. package/account/account.mjs +7 -7
  6. package/account/account.mjs.map +1 -1
  7. package/account/get-account-config-from-environment.d.ts +0 -1
  8. package/account/get-account-config-from-environment.js.map +1 -1
  9. package/account/get-account-config-from-environment.mjs.map +1 -1
  10. package/account/get-account.d.ts +2 -3
  11. package/account/get-account.js +1 -1
  12. package/account/get-account.js.map +1 -1
  13. package/account/get-account.mjs +1 -1
  14. package/account/get-account.mjs.map +1 -1
  15. package/account/get-dispenser-account.d.ts +1 -2
  16. package/account/get-dispenser-account.js +1 -1
  17. package/account/get-dispenser-account.js.map +1 -1
  18. package/account/get-dispenser-account.mjs +1 -1
  19. package/account/get-dispenser-account.mjs.map +1 -1
  20. package/account/index.d.ts +0 -1
  21. package/account/mnemonic-account.d.ts +1 -2
  22. package/account/mnemonic-account.js +1 -1
  23. package/account/mnemonic-account.js.map +1 -1
  24. package/account/mnemonic-account.mjs +1 -1
  25. package/account/mnemonic-account.mjs.map +1 -1
  26. package/amount.d.ts +14 -5
  27. package/amount.js +6 -0
  28. package/amount.js.map +1 -1
  29. package/amount.mjs +6 -0
  30. package/amount.mjs.map +1 -1
  31. package/app-client.d.ts +15 -1
  32. package/app-client.js +15 -0
  33. package/app-client.js.map +1 -1
  34. package/app-client.mjs +15 -0
  35. package/app-client.mjs.map +1 -1
  36. package/app-deploy.d.ts +18 -2
  37. package/app-deploy.js +140 -333
  38. package/app-deploy.js.map +1 -1
  39. package/app-deploy.mjs +143 -336
  40. package/app-deploy.mjs.map +1 -1
  41. package/app.d.ts +39 -3
  42. package/app.js +116 -369
  43. package/app.js.map +1 -1
  44. package/app.mjs +117 -370
  45. package/app.mjs.map +1 -1
  46. package/asset.d.ts +5 -6
  47. package/asset.js +5 -8
  48. package/asset.js.map +1 -1
  49. package/asset.mjs +5 -8
  50. package/asset.mjs.map +1 -1
  51. package/config.d.ts +0 -1
  52. package/config.js.map +1 -1
  53. package/config.mjs.map +1 -1
  54. package/debugging/debugging.d.ts +3 -5
  55. package/debugging/debugging.js +4 -128
  56. package/debugging/debugging.js.map +1 -1
  57. package/debugging/debugging.mjs +4 -109
  58. package/debugging/debugging.mjs.map +1 -1
  59. package/debugging/index.d.ts +0 -2
  60. package/dispenser-client.d.ts +0 -1
  61. package/dispenser-client.js.map +1 -1
  62. package/dispenser-client.mjs.map +1 -1
  63. package/index.d.ts +4 -4
  64. package/index.js +31 -26
  65. package/index.js.map +1 -1
  66. package/index.mjs +4 -4
  67. package/indexer-lookup.d.ts +0 -1
  68. package/indexer-lookup.js.map +1 -1
  69. package/indexer-lookup.mjs.map +1 -1
  70. package/localnet/get-kmd-wallet-account.d.ts +1 -2
  71. package/localnet/get-kmd-wallet-account.js +1 -1
  72. package/localnet/get-kmd-wallet-account.js.map +1 -1
  73. package/localnet/get-kmd-wallet-account.mjs +1 -1
  74. package/localnet/get-kmd-wallet-account.mjs.map +1 -1
  75. package/localnet/get-localnet-dispenser-account.d.ts +1 -2
  76. package/localnet/get-localnet-dispenser-account.js +1 -1
  77. package/localnet/get-localnet-dispenser-account.js.map +1 -1
  78. package/localnet/get-localnet-dispenser-account.mjs +1 -1
  79. package/localnet/get-localnet-dispenser-account.mjs.map +1 -1
  80. package/localnet/get-or-create-kmd-wallet-account.d.ts +1 -2
  81. package/localnet/get-or-create-kmd-wallet-account.js +1 -1
  82. package/localnet/get-or-create-kmd-wallet-account.js.map +1 -1
  83. package/localnet/get-or-create-kmd-wallet-account.mjs +1 -1
  84. package/localnet/get-or-create-kmd-wallet-account.mjs.map +1 -1
  85. package/localnet/index.d.ts +0 -1
  86. package/localnet/is-localnet.d.ts +1 -2
  87. package/localnet/is-localnet.js +1 -1
  88. package/localnet/is-localnet.js.map +1 -1
  89. package/localnet/is-localnet.mjs +1 -1
  90. package/localnet/is-localnet.mjs.map +1 -1
  91. package/network-client.d.ts +2 -3
  92. package/network-client.js +2 -2
  93. package/network-client.js.map +1 -1
  94. package/network-client.mjs +2 -2
  95. package/network-client.mjs.map +1 -1
  96. package/package.json +1 -1
  97. package/testing/_asset.d.ts +0 -1
  98. package/testing/account.d.ts +0 -1
  99. package/testing/account.js.map +1 -1
  100. package/testing/account.mjs.map +1 -1
  101. package/testing/fixtures/algokit-log-capture-fixture.d.ts +0 -1
  102. package/testing/fixtures/algokit-log-capture-fixture.js.map +1 -1
  103. package/testing/fixtures/algokit-log-capture-fixture.mjs.map +1 -1
  104. package/testing/fixtures/algorand-fixture.d.ts +0 -1
  105. package/testing/fixtures/algorand-fixture.js +12 -12
  106. package/testing/fixtures/algorand-fixture.js.map +1 -1
  107. package/testing/fixtures/algorand-fixture.mjs +12 -12
  108. package/testing/fixtures/algorand-fixture.mjs.map +1 -1
  109. package/testing/fixtures/index.d.ts +0 -1
  110. package/testing/index.d.ts +0 -1
  111. package/testing/indexer.d.ts +0 -1
  112. package/testing/indexer.js.map +1 -1
  113. package/testing/indexer.mjs.map +1 -1
  114. package/testing/test-logger.d.ts +0 -1
  115. package/testing/test-logger.js +1 -3
  116. package/testing/test-logger.js.map +1 -1
  117. package/testing/test-logger.mjs +1 -3
  118. package/testing/test-logger.mjs.map +1 -1
  119. package/testing/transaction-logger.d.ts +0 -1
  120. package/testing/transaction-logger.js.map +1 -1
  121. package/testing/transaction-logger.mjs.map +1 -1
  122. package/transaction/index.d.ts +0 -1
  123. package/transaction/legacy-bridge.d.ts +31 -6
  124. package/transaction/legacy-bridge.js +100 -6
  125. package/transaction/legacy-bridge.js.map +1 -1
  126. package/transaction/legacy-bridge.mjs +99 -8
  127. package/transaction/legacy-bridge.mjs.map +1 -1
  128. package/transaction/perform-atomic-transaction-composer-simulate.d.ts +0 -1
  129. package/transaction/perform-atomic-transaction-composer-simulate.js.map +1 -1
  130. package/transaction/perform-atomic-transaction-composer-simulate.mjs.map +1 -1
  131. package/transaction/transaction.d.ts +41 -6
  132. package/transaction/transaction.js +107 -45
  133. package/transaction/transaction.js.map +1 -1
  134. package/transaction/transaction.mjs +107 -46
  135. package/transaction/transaction.mjs.map +1 -1
  136. package/transfer/index.d.ts +0 -1
  137. package/transfer/transfer-algos.d.ts +1 -2
  138. package/transfer/transfer-algos.js +1 -1
  139. package/transfer/transfer-algos.js.map +1 -1
  140. package/transfer/transfer-algos.mjs +1 -1
  141. package/transfer/transfer-algos.mjs.map +1 -1
  142. package/transfer/transfer.d.ts +1 -2
  143. package/transfer/transfer.js +3 -3
  144. package/transfer/transfer.js.map +1 -1
  145. package/transfer/transfer.mjs +3 -3
  146. package/transfer/transfer.mjs.map +1 -1
  147. package/types/account-manager.d.ts +14 -6
  148. package/types/account-manager.js +18 -4
  149. package/types/account-manager.js.map +1 -1
  150. package/types/account-manager.mjs +18 -4
  151. package/types/account-manager.mjs.map +1 -1
  152. package/types/account.d.ts +0 -1
  153. package/types/account.js.map +1 -1
  154. package/types/account.mjs.map +1 -1
  155. package/types/algo-http-client-with-retry.d.ts +0 -1
  156. package/types/algo-http-client-with-retry.js.map +1 -1
  157. package/types/algo-http-client-with-retry.mjs.map +1 -1
  158. package/types/algorand-client-interface.d.ts +28 -0
  159. package/types/algorand-client-interface.js +3 -0
  160. package/types/algorand-client-interface.js.map +1 -0
  161. package/types/algorand-client-interface.mjs +2 -0
  162. package/types/algorand-client-interface.mjs.map +1 -0
  163. package/types/algorand-client-transaction-creator.d.ts +450 -21
  164. package/types/algorand-client-transaction-creator.js +406 -22
  165. package/types/algorand-client-transaction-creator.js.map +1 -1
  166. package/types/algorand-client-transaction-creator.mjs +406 -22
  167. package/types/algorand-client-transaction-creator.mjs.map +1 -1
  168. package/types/algorand-client-transaction-sender.d.ts +758 -33
  169. package/types/algorand-client-transaction-sender.js +444 -14
  170. package/types/algorand-client-transaction-sender.js.map +1 -1
  171. package/types/algorand-client-transaction-sender.mjs +444 -14
  172. package/types/algorand-client-transaction-sender.mjs.map +1 -1
  173. package/types/algorand-client.d.ts +18 -10
  174. package/types/algorand-client.js +23 -10
  175. package/types/algorand-client.js.map +1 -1
  176. package/types/algorand-client.mjs +23 -10
  177. package/types/algorand-client.mjs.map +1 -1
  178. package/types/amount.d.ts +10 -11
  179. package/types/amount.js +7 -7
  180. package/types/amount.js.map +1 -1
  181. package/types/amount.mjs +7 -7
  182. package/types/amount.mjs.map +1 -1
  183. package/types/app-arc56.d.ts +348 -0
  184. package/types/app-arc56.js +184 -0
  185. package/types/app-arc56.js.map +1 -0
  186. package/types/app-arc56.mjs +175 -0
  187. package/types/app-arc56.mjs.map +1 -0
  188. package/types/app-client.d.ts +1496 -9
  189. package/types/app-client.js +903 -21
  190. package/types/app-client.js.map +1 -1
  191. package/types/app-client.mjs +904 -23
  192. package/types/app-client.mjs.map +1 -1
  193. package/types/app-deployer.d.ts +126 -0
  194. package/types/app-deployer.js +354 -0
  195. package/types/app-deployer.js.map +1 -0
  196. package/types/app-deployer.mjs +352 -0
  197. package/types/app-deployer.mjs.map +1 -0
  198. package/types/app-factory.d.ts +846 -0
  199. package/types/app-factory.js +411 -0
  200. package/types/app-factory.js.map +1 -0
  201. package/types/app-factory.mjs +409 -0
  202. package/types/app-factory.mjs.map +1 -0
  203. package/types/app-manager.d.ts +244 -0
  204. package/types/app-manager.js +334 -0
  205. package/types/app-manager.js.map +1 -0
  206. package/types/app-manager.mjs +332 -0
  207. package/types/app-manager.mjs.map +1 -0
  208. package/types/app-spec.d.ts +2 -1
  209. package/types/app-spec.js +121 -0
  210. package/types/app-spec.js.map +1 -1
  211. package/types/app-spec.mjs +120 -0
  212. package/types/app-spec.mjs.map +1 -1
  213. package/types/app.d.ts +53 -13
  214. package/types/app.js +1 -1
  215. package/types/app.js.map +1 -1
  216. package/types/app.mjs +1 -1
  217. package/types/app.mjs.map +1 -1
  218. package/types/asset-manager.d.ts +4 -4
  219. package/types/asset-manager.js +3 -4
  220. package/types/asset-manager.js.map +1 -1
  221. package/types/asset-manager.mjs +3 -4
  222. package/types/asset-manager.mjs.map +1 -1
  223. package/types/asset.d.ts +0 -1
  224. package/types/async-event-emitter.d.ts +23 -0
  225. package/types/async-event-emitter.js +55 -0
  226. package/types/async-event-emitter.js.map +1 -0
  227. package/types/async-event-emitter.mjs +53 -0
  228. package/types/async-event-emitter.mjs.map +1 -0
  229. package/types/client-manager.d.ts +133 -51
  230. package/types/client-manager.js +147 -23
  231. package/types/client-manager.js.map +1 -1
  232. package/types/client-manager.mjs +148 -24
  233. package/types/client-manager.mjs.map +1 -1
  234. package/types/composer.d.ts +232 -49
  235. package/types/composer.js +307 -65
  236. package/types/composer.js.map +1 -1
  237. package/types/composer.mjs +308 -66
  238. package/types/composer.mjs.map +1 -1
  239. package/types/config.d.ts +3 -6
  240. package/types/config.js +5 -44
  241. package/types/config.js.map +1 -1
  242. package/types/config.mjs +5 -27
  243. package/types/config.mjs.map +1 -1
  244. package/types/debugging.d.ts +29 -101
  245. package/types/debugging.js +16 -109
  246. package/types/debugging.js.map +1 -1
  247. package/types/debugging.mjs +12 -107
  248. package/types/debugging.mjs.map +1 -1
  249. package/types/dispenser-client.d.ts +1 -2
  250. package/types/dispenser-client.js +6 -5
  251. package/types/dispenser-client.js.map +1 -1
  252. package/types/dispenser-client.mjs +6 -5
  253. package/types/dispenser-client.mjs.map +1 -1
  254. package/types/expand.d.ts +7 -0
  255. package/types/expand.js +3 -0
  256. package/types/expand.js.map +1 -0
  257. package/types/expand.mjs +2 -0
  258. package/types/expand.mjs.map +1 -0
  259. package/types/indexer.d.ts +0 -1
  260. package/types/indexer.js.map +1 -1
  261. package/types/indexer.mjs.map +1 -1
  262. package/types/kmd-account-manager.d.ts +0 -1
  263. package/types/kmd-account-manager.js +2 -2
  264. package/types/kmd-account-manager.js.map +1 -1
  265. package/types/kmd-account-manager.mjs +2 -2
  266. package/types/kmd-account-manager.mjs.map +1 -1
  267. package/types/logging.d.ts +0 -1
  268. package/types/logging.js.map +1 -1
  269. package/types/logging.mjs.map +1 -1
  270. package/types/logic-error.d.ts +0 -1
  271. package/types/logic-error.js.map +1 -1
  272. package/types/logic-error.mjs.map +1 -1
  273. package/types/network-client.d.ts +13 -1
  274. package/types/network-client.js.map +1 -1
  275. package/types/network-client.mjs.map +1 -1
  276. package/types/testing.d.ts +3 -4
  277. package/types/transaction.d.ts +17 -4
  278. package/types/transfer.d.ts +0 -1
  279. package/types/urlTokenBaseHTTPClient.d.ts +0 -1
  280. package/types/urlTokenBaseHTTPClient.js +1 -1
  281. package/types/urlTokenBaseHTTPClient.js.map +1 -1
  282. package/types/urlTokenBaseHTTPClient.mjs +1 -1
  283. package/types/urlTokenBaseHTTPClient.mjs.map +1 -1
  284. package/util.d.ts +2 -2
  285. package/util.js +11 -10
  286. package/util.js.map +1 -1
  287. package/util.mjs +11 -10
  288. package/util.mjs.map +1 -1
  289. package/account/account.d.ts.map +0 -1
  290. package/account/get-account-config-from-environment.d.ts.map +0 -1
  291. package/account/get-account.d.ts.map +0 -1
  292. package/account/get-dispenser-account.d.ts.map +0 -1
  293. package/account/index.d.ts.map +0 -1
  294. package/account/mnemonic-account.d.ts.map +0 -1
  295. package/amount.d.ts.map +0 -1
  296. package/app-client.d.ts.map +0 -1
  297. package/app-deploy.d.ts.map +0 -1
  298. package/app.d.ts.map +0 -1
  299. package/asset.d.ts.map +0 -1
  300. package/config.d.ts.map +0 -1
  301. package/debugging/debugging.d.ts.map +0 -1
  302. package/debugging/index.d.ts.map +0 -1
  303. package/debugging/simulate-and-persist-response.d.ts +0 -20
  304. package/debugging/simulate-and-persist-response.d.ts.map +0 -1
  305. package/debugging/simulate-and-persist-response.js +0 -108
  306. package/debugging/simulate-and-persist-response.js.map +0 -1
  307. package/debugging/simulate-and-persist-response.mjs +0 -89
  308. package/debugging/simulate-and-persist-response.mjs.map +0 -1
  309. package/dispenser-client.d.ts.map +0 -1
  310. package/index.d.ts.map +0 -1
  311. package/indexer-lookup.d.ts.map +0 -1
  312. package/localnet/get-kmd-wallet-account.d.ts.map +0 -1
  313. package/localnet/get-localnet-dispenser-account.d.ts.map +0 -1
  314. package/localnet/get-or-create-kmd-wallet-account.d.ts.map +0 -1
  315. package/localnet/index.d.ts.map +0 -1
  316. package/localnet/is-localnet.d.ts.map +0 -1
  317. package/network-client.d.ts.map +0 -1
  318. package/testing/_asset.d.ts.map +0 -1
  319. package/testing/account.d.ts.map +0 -1
  320. package/testing/fixtures/algokit-log-capture-fixture.d.ts.map +0 -1
  321. package/testing/fixtures/algorand-fixture.d.ts.map +0 -1
  322. package/testing/fixtures/index.d.ts.map +0 -1
  323. package/testing/index.d.ts.map +0 -1
  324. package/testing/indexer.d.ts.map +0 -1
  325. package/testing/test-logger.d.ts.map +0 -1
  326. package/testing/transaction-logger.d.ts.map +0 -1
  327. package/transaction/index.d.ts.map +0 -1
  328. package/transaction/legacy-bridge.d.ts.map +0 -1
  329. package/transaction/perform-atomic-transaction-composer-simulate.d.ts.map +0 -1
  330. package/transaction/transaction.d.ts.map +0 -1
  331. package/transfer/index.d.ts.map +0 -1
  332. package/transfer/transfer-algos.d.ts.map +0 -1
  333. package/transfer/transfer.d.ts.map +0 -1
  334. package/types/account-manager.d.ts.map +0 -1
  335. package/types/account.d.ts.map +0 -1
  336. package/types/algo-http-client-with-retry.d.ts.map +0 -1
  337. package/types/algorand-client-transaction-creator.d.ts.map +0 -1
  338. package/types/algorand-client-transaction-sender.d.ts.map +0 -1
  339. package/types/algorand-client.d.ts.map +0 -1
  340. package/types/amount.d.ts.map +0 -1
  341. package/types/app-client.d.ts.map +0 -1
  342. package/types/app-spec.d.ts.map +0 -1
  343. package/types/app.d.ts.map +0 -1
  344. package/types/asset-manager.d.ts.map +0 -1
  345. package/types/asset.d.ts.map +0 -1
  346. package/types/client-manager.d.ts.map +0 -1
  347. package/types/composer.d.ts.map +0 -1
  348. package/types/config.d.ts.map +0 -1
  349. package/types/debugging.d.ts.map +0 -1
  350. package/types/dispenser-client.d.ts.map +0 -1
  351. package/types/indexer.d.ts.map +0 -1
  352. package/types/kmd-account-manager.d.ts.map +0 -1
  353. package/types/logging.d.ts.map +0 -1
  354. package/types/logic-error.d.ts.map +0 -1
  355. package/types/network-client.d.ts.map +0 -1
  356. package/types/testing.d.ts.map +0 -1
  357. package/types/transaction.d.ts.map +0 -1
  358. package/types/transfer.d.ts.map +0 -1
  359. package/types/urlTokenBaseHTTPClient.d.ts.map +0 -1
  360. package/util.d.ts.map +0 -1
@@ -1,19 +1,22 @@
1
1
  import algosdk from 'algosdk';
2
2
  import { Buffer } from 'buffer';
3
- import { compileTeal, createApp, updateApp, getABIMethodSignature, callApp, getAppGlobalState, getAppLocalState, getAppBoxNames, getAppBoxValue, getAppBoxValueFromABIType } from '../app.mjs';
3
+ import { compileTeal, createApp, updateApp, callApp, getAppGlobalState, getAppLocalState, getAppBoxNames, getAppBoxValue, getAppBoxValueFromABIType } from '../app.mjs';
4
4
  import { replaceDeployTimeControlParams, performTemplateSubstitution, deployApp, getCreatorAppsByName } from '../app-deploy.mjs';
5
5
  import { Config } from '../config.mjs';
6
- import { persistSourceMaps } from '../debugging/debugging.mjs';
7
6
  import { legacySendTransactionBridge } from '../transaction/legacy-bridge.mjs';
8
7
  import { getSenderAddress, encodeTransactionNote } from '../transaction/transaction.mjs';
8
+ import { binaryStartsWith } from '../util.mjs';
9
9
  import { UPDATABLE_TEMPLATE_NAME, DELETABLE_TEMPLATE_NAME } from './app.mjs';
10
- import { PersistSourceMapInput } from './debugging.mjs';
10
+ import { getArc56Method, getArc56ReturnValue, getABITupleFromABIStruct, getABIDecodedValue, getABIEncodedValue } from './app-arc56.mjs';
11
+ import { arc32ToArc56 } from './app-spec.mjs';
12
+ import { EventType } from './async-event-emitter.mjs';
11
13
  import { LogicError } from './logic-error.mjs';
12
14
 
13
15
  var ABIMethod = algosdk.ABIMethod;
14
16
  var AtomicTransactionComposer = algosdk.AtomicTransactionComposer;
15
17
  var getApplicationAddress = algosdk.getApplicationAddress;
16
18
  var Indexer = algosdk.Indexer;
19
+ var OnApplicationComplete = algosdk.OnApplicationComplete;
17
20
  var SourceMap = algosdk.SourceMap;
18
21
  /**
19
22
  * Determines deploy time control (UPDATABLE, DELETABLE) value by inspecting application specification
@@ -37,13 +40,865 @@ function getDeployTimeControl(approval, appSpec, templateVariableName, callConfi
37
40
  return !!abiCallConfig && abiCallConfig !== 'NEVER';
38
41
  });
39
42
  }
40
- /** Application client - a class that wraps an ARC-0032 app spec and provides high productivity methods to deploy and call the app */
43
+ /** ARC-56/ARC-32 application client that allows you to manage calls and
44
+ * state for a specific deployed instance of an app (with a known app ID). */
45
+ class AppClient {
46
+ constructor(params) {
47
+ this._appId = params.appId;
48
+ this._appAddress = algosdk.getApplicationAddress(this._appId);
49
+ this._appSpec = AppClient.normaliseAppSpec(params.appSpec);
50
+ this._appName = params.appName ?? this._appSpec.name;
51
+ this._algorand = params.algorand;
52
+ this._defaultSender = params.defaultSender;
53
+ this._defaultSigner = params.defaultSigner;
54
+ this._approvalSourceMap = params.approvalSourceMap;
55
+ this._clearSourceMap = params.clearSourceMap;
56
+ this._localStateMethods = (address) => this.getStateMethods(() => this.getLocalState(address), () => this._appSpec.state.keys.local, () => this._appSpec.state.maps.local);
57
+ this._globalStateMethods = this.getStateMethods(() => this.getGlobalState(), () => this._appSpec.state.keys.global, () => this._appSpec.state.maps.global);
58
+ this._boxStateMethods = this.getBoxMethods();
59
+ this._paramsMethods = {
60
+ ...this.getMethodCallParamsMethods(),
61
+ /** Get parameters to define bare (raw) transactions to the current app */
62
+ bare: this.getBareParamsMethods(),
63
+ };
64
+ this._createTransactionsMethods = {
65
+ ...this.getMethodCallCreateTransactionMethods(),
66
+ /** Get transactions for bare (raw) calls to the current app */
67
+ bare: this.getBareCreateTransactionMethods(),
68
+ };
69
+ this._sendMethods = {
70
+ ...this.getMethodCallSendMethods(),
71
+ /** Send bare (raw) transactions to the current app */
72
+ bare: this.getBareSendMethods(),
73
+ };
74
+ }
75
+ /** Start a new `AlgoKitComposer` transaction group */
76
+ newGroup() {
77
+ return this._algorand.newGroup();
78
+ }
79
+ /**
80
+ * Returns a new `AppClient` client, resolving the app by creator address and name
81
+ * using AlgoKit app deployment semantics (i.e. looking for the app creation transaction note).
82
+ * @param params The parameters to create the app client
83
+ */
84
+ static async fromCreatorAndName(params) {
85
+ const appSpec = AppClient.normaliseAppSpec(params.appSpec);
86
+ const appLookup = params.appLookupCache ?? (await params.algorand.appDeployer.getCreatorAppsByName(params.creatorAddress, params.ignoreCache));
87
+ const appMetadata = appLookup.apps[params.appName ?? appSpec.name];
88
+ if (!appMetadata) {
89
+ throw new Error(`App not found for creator ${params.creatorAddress} and name ${params.appName ?? appSpec.name}`);
90
+ }
91
+ return new AppClient({
92
+ ...params,
93
+ algorand: params.algorand,
94
+ appId: appMetadata.appId,
95
+ });
96
+ }
97
+ /**
98
+ * Returns an `AppClient` instance for the current network based on
99
+ * pre-determined network-specific app IDs specified in the ARC-56 app spec.
100
+ *
101
+ * If no IDs are in the app spec or the network isn't recognised, an error is thrown.
102
+ * @param params The parameters to create the app client
103
+ */
104
+ static async fromNetwork(params) {
105
+ const network = await params.algorand.client.network();
106
+ const appSpec = AppClient.normaliseAppSpec(params.appSpec);
107
+ const networkNames = [network.genesisHash];
108
+ if (network.isLocalNet)
109
+ networkNames.push('localnet');
110
+ if (network.isTestNet)
111
+ networkNames.push('testnet');
112
+ if (network.isMainNet)
113
+ networkNames.push('mainnet');
114
+ const availableAppSpecNetworks = Object.keys(appSpec.networks ?? {});
115
+ const networkIndex = availableAppSpecNetworks.findIndex((n) => networkNames.includes(n));
116
+ if (networkIndex === -1) {
117
+ throw new Error(`No app ID found for network ${JSON.stringify(networkNames)} in the app spec`);
118
+ }
119
+ const appId = BigInt(appSpec.networks[networkIndex].appID);
120
+ return new AppClient({ ...params, appId, appSpec });
121
+ }
122
+ /**
123
+ * Takes a string or parsed JSON object that could be ARC-32 or ARC-56 format and
124
+ * normalises it into a parsed ARC-56 contract object.
125
+ * @param spec The spec to normalise
126
+ * @returns The normalised ARC-56 contract object
127
+ */
128
+ static normaliseAppSpec(spec) {
129
+ const parsedSpec = typeof spec === 'string' ? JSON.parse(spec) : spec;
130
+ const appSpec = 'hints' in parsedSpec ? arc32ToArc56(parsedSpec) : parsedSpec;
131
+ return appSpec;
132
+ }
133
+ /** The ID of the app instance this client is linked to. */
134
+ get appId() {
135
+ return this._appId;
136
+ }
137
+ /** The app address of the app instance this client is linked to. */
138
+ get appAddress() {
139
+ return this._appAddress;
140
+ }
141
+ /** The name of the app (from the ARC-32 / ARC-56 app spec). */
142
+ get appName() {
143
+ return this._appName;
144
+ }
145
+ /** The ARC-56 app spec being used */
146
+ get appSpec() {
147
+ return this._appSpec;
148
+ }
149
+ /** A reference to the underlying `AlgorandClient` this app client is using. */
150
+ get algorand() {
151
+ return this._algorand;
152
+ }
153
+ /** Get parameters to create transactions for the current app.
154
+ *
155
+ * A good mental model for this is that these parameters represent a deferred transaction creation.
156
+ * @example Create a transaction in the future using Algorand Client
157
+ * ```typescript
158
+ * const myMethodCall = appClient.params.call({method: 'my_method', args: [123, 'hello']})
159
+ * // ...
160
+ * await algorand.send.AppMethodCall(myMethodCall)
161
+ * ```
162
+ * @example Define a nested transaction as an ABI argument
163
+ * ```typescript
164
+ * const myMethodCall = appClient.params.call({method: 'my_method', args: [123, 'hello']})
165
+ * await appClient.send.call({method: 'my_method2', args: [myMethodCall]})
166
+ * ```
167
+ */
168
+ get params() {
169
+ return this._paramsMethods;
170
+ }
171
+ /** Create transactions for the current app */
172
+ get createTransaction() {
173
+ return this._createTransactionsMethods;
174
+ }
175
+ /** Send transactions to the current app */
176
+ get send() {
177
+ return this._sendMethods;
178
+ }
179
+ /** Get state (local, global, box) from the current app */
180
+ get state() {
181
+ return {
182
+ /**
183
+ * Methods to access local state for the current app
184
+ * @param address The address of the account to get the local state for
185
+ */
186
+ local: this._localStateMethods,
187
+ /**
188
+ * Methods to access global state for the current app
189
+ */
190
+ global: this._globalStateMethods,
191
+ /**
192
+ * Methods to access box storage for the current app
193
+ */
194
+ box: this._boxStateMethods,
195
+ };
196
+ }
197
+ /**
198
+ * Funds Algo into the app account for this app.
199
+ *
200
+ * An alias for `appClient.send.fundAppAccount(params)`.
201
+ * @param params The parameters for the funding transaction
202
+ * @returns The result of the funding
203
+ */
204
+ async fundAppAccount(params) {
205
+ return this.send.fundAppAccount(params);
206
+ }
207
+ /**
208
+ * Returns raw global state for the current app.
209
+ * @returns The global state
210
+ */
211
+ async getGlobalState() {
212
+ return await this._algorand.app.getGlobalState(this.appId);
213
+ }
214
+ /**
215
+ * Returns raw local state for the given account address.
216
+ * @param address The address of the account to get the local state for
217
+ * @returns The local state
218
+ */
219
+ async getLocalState(address) {
220
+ return await this._algorand.app.getLocalState(this.appId, address);
221
+ }
222
+ /**
223
+ * Returns the names of all current boxes for the current app.
224
+ * @returns The names of the boxes
225
+ */
226
+ async getBoxNames() {
227
+ return await this._algorand.app.getBoxNames(this.appId);
228
+ }
229
+ /**
230
+ * Returns the value of the given box for the current app.
231
+ * @param name The identifier of the box to return
232
+ * @returns The current box value as a byte array
233
+ */
234
+ async getBoxValue(name) {
235
+ return await this._algorand.app.getBoxValue(this.appId, name);
236
+ }
237
+ /**
238
+ * Returns the value of the given box for the current app.
239
+ * @param name The identifier of the box to return
240
+ * @param type
241
+ * @returns The current box value as a byte array
242
+ */
243
+ async getBoxValueFromABIType(name, type) {
244
+ return await this._algorand.app.getBoxValueFromABIType({
245
+ appId: this.appId,
246
+ boxName: name,
247
+ type,
248
+ });
249
+ }
250
+ /**
251
+ * Returns the values of all current boxes for the current app.
252
+ * Note: This will issue multiple HTTP requests (one per box) and it's not an atomic operation so values may be out of sync.
253
+ * @param filter Optional filter to filter which boxes' values are returned
254
+ * @returns The (name, value) pair of the boxes with values as raw byte arrays
255
+ */
256
+ async getBoxValues(filter) {
257
+ const names = (await this.getBoxNames()).filter(filter ?? ((_) => true));
258
+ const values = await this._algorand.app.getBoxValues(this.appId, names.map((name) => name.nameRaw));
259
+ return names.map((name, i) => ({ name, value: values[i] }));
260
+ }
261
+ /**
262
+ * Returns the values of all current boxes for the current app decoded using an ABI Type.
263
+ * Note: This will issue multiple HTTP requests (one per box) and it's not an atomic operation so values may be out of sync.
264
+ * @param type The ABI type to decode the values with
265
+ * @param filter Optional filter to filter which boxes' values are returned
266
+ * @returns The (name, value) pair of the boxes with values as the ABI Value
267
+ */
268
+ async getBoxValuesFromABIType(type, filter) {
269
+ const names = (await this.getBoxNames()).filter(filter ?? ((_) => true));
270
+ const values = await this._algorand.app.getBoxValuesFromABIType({
271
+ appId: this.appId,
272
+ boxNames: names.map((name) => name.nameRaw),
273
+ type,
274
+ });
275
+ return names.map((name, i) => ({ name, value: values[i] }));
276
+ }
277
+ /**
278
+ * Takes an error that may include a logic error from a call to the current app and re-exposes the
279
+ * error to include source code information via the source map and ARC-56 spec.
280
+ * @param e The error to parse
281
+ * @param isClearStateProgram Whether or not the code was running the clear state program (defaults to approval program)
282
+ * @returns The new error, or if there was no logic error or source map then the wrapped error with source details
283
+ */
284
+ exposeLogicError(e, isClearStateProgram) {
285
+ return AppClient.exposeLogicError(e, this._appSpec, {
286
+ isClearStateProgram,
287
+ approvalSourceMap: this._approvalSourceMap,
288
+ clearSourceMap: this._clearSourceMap,
289
+ });
290
+ }
291
+ /**
292
+ * Export the current source maps for the app.
293
+ * @returns The source maps
294
+ */
295
+ exportSourceMaps() {
296
+ if (!this._approvalSourceMap || !this._clearSourceMap) {
297
+ throw new Error("Unable to export source maps; they haven't been loaded into this client - you need to call create, update, or deploy first");
298
+ }
299
+ return {
300
+ approvalSourceMap: this._approvalSourceMap,
301
+ clearSourceMap: this._clearSourceMap,
302
+ };
303
+ }
304
+ /**
305
+ * Import source maps for the app.
306
+ * @param sourceMaps The source maps to import
307
+ */
308
+ importSourceMaps(sourceMaps) {
309
+ this._approvalSourceMap = new SourceMap(sourceMaps.approvalSourceMap);
310
+ this._clearSourceMap = new SourceMap(sourceMaps.clearSourceMap);
311
+ }
312
+ /**
313
+ * Returns the ABI Method spec for the given method string for the app represented by this application client instance
314
+ * @param methodNameOrSignature The method name or method signature to call if an ABI call is being emitted.
315
+ * e.g. `my_method` or `my_method(unit64,string)bytes`
316
+ * @returns A tuple with: [ARC-56 `Method`, algosdk `ABIMethod`]
317
+ */
318
+ getABIMethod(methodNameOrSignature) {
319
+ return getArc56Method(methodNameOrSignature, this._appSpec);
320
+ }
321
+ /**
322
+ * Checks for decode errors on the SendAppTransactionResult and maps the return value to the specified type
323
+ * on the ARC-56 method, replacing the `return` property with the decoded type.
324
+ *
325
+ * If the return type is an ARC-56 struct then the struct will be returned.
326
+ *
327
+ * @param result The SendAppTransactionResult to be mapped
328
+ * @param method The method that was called
329
+ * @returns The smart contract response with an updated return value
330
+ */
331
+ async processMethodCallReturn(result, method) {
332
+ const resultValue = await result;
333
+ return { ...resultValue, return: getArc56ReturnValue(resultValue.return, method, this._appSpec.structs) };
334
+ }
335
+ /**
336
+ * Compiles the approval and clear state programs (if TEAL templates provided),
337
+ * performing any provided deploy-time parameter replacement and stores
338
+ * the source maps.
339
+ *
340
+ * If no TEAL templates provided it will use any byte code provided in the app spec.
341
+ *
342
+ * Will store any generated source maps for later use in debugging.
343
+ */
344
+ async compile(compilation) {
345
+ const result = await AppClient.compile(this._appSpec, this._algorand.app, compilation);
346
+ if (result.compiledApproval) {
347
+ this._approvalSourceMap = result.compiledApproval.sourceMap;
348
+ }
349
+ if (result.compiledClear) {
350
+ this._clearSourceMap = result.compiledClear.sourceMap;
351
+ }
352
+ return result;
353
+ }
354
+ /**
355
+ * Takes an error that may include a logic error from a call to the current app and re-exposes the
356
+ * error to include source code information via the source map and ARC-56 spec.
357
+ * @param e The error to parse
358
+ * @param appSpec The app spec for the app
359
+ * @param details Additional information to inform the error
360
+ * @returns The new error, or if there was no logic error or source map then the wrapped error with source details
361
+ */
362
+ static exposeLogicError(e, appSpec, details) {
363
+ const { isClearStateProgram, approvalSourceMap, clearSourceMap } = details;
364
+ if ((!isClearStateProgram && approvalSourceMap == undefined) || (isClearStateProgram && clearSourceMap == undefined))
365
+ return e;
366
+ const errorDetails = LogicError.parseLogicError(e);
367
+ const errorMessage = (isClearStateProgram ? appSpec.sourceInfo?.clear : appSpec.sourceInfo?.approval)?.find((s) => s?.pc?.includes(errorDetails?.pc ?? -1))?.errorMessage;
368
+ if (errorDetails !== undefined && appSpec.source)
369
+ e = new LogicError(errorDetails, Buffer.from(isClearStateProgram ? appSpec.source.clear : appSpec.source.approval, 'base64')
370
+ .toString()
371
+ .split('\n'),
372
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
373
+ isClearStateProgram ? clearSourceMap : approvalSourceMap);
374
+ if (errorMessage) {
375
+ const appId = JSON.stringify(e).match(/(?<=app=)\d+/)?.[0] || '';
376
+ const txId = JSON.stringify(e).match(/(?<=transaction )\S+(?=:)/)?.[0];
377
+ const error = new Error(`Runtime error when executing ${appSpec.name} (appId: ${appId}) in transaction ${txId}: ${errorMessage}`);
378
+ error.cause = e;
379
+ return error;
380
+ }
381
+ return e;
382
+ }
383
+ /**
384
+ * Compiles the approval and clear state programs (if TEAL templates provided),
385
+ * performing any provided deploy-time parameter replacement and returns
386
+ * the compiled code and any compilation results (including source maps).
387
+ *
388
+ * If no TEAL templates provided it will use any byte code provided in the app spec.
389
+ *
390
+ * Will store any generated source maps for later use in debugging.
391
+ * @param appSpec The app spec for the app
392
+ * @param compilation Any compilation parameters to use
393
+ */
394
+ static async compile(appSpec, appManager, compilation) {
395
+ const { deployTimeParams, updatable, deletable } = compilation ?? {};
396
+ if (!appSpec.source) {
397
+ if (!appSpec.byteCode?.approval || !appSpec.byteCode?.clear) {
398
+ throw new Error(`Attempt to compile app ${appSpec.name} without source or byteCode`);
399
+ }
400
+ return {
401
+ approvalProgram: Buffer.from(appSpec.byteCode.approval, 'base64'),
402
+ clearStateProgram: Buffer.from(appSpec.byteCode.clear, 'base64'),
403
+ };
404
+ }
405
+ const approvalTemplate = Buffer.from(appSpec.source.approval, 'base64').toString('utf-8');
406
+ const compiledApproval = await appManager.compileTealTemplate(approvalTemplate, deployTimeParams, {
407
+ updatable,
408
+ deletable,
409
+ });
410
+ const clearTemplate = Buffer.from(appSpec.source.clear, 'base64').toString('utf-8');
411
+ const compiledClear = await appManager.compileTealTemplate(clearTemplate, deployTimeParams);
412
+ if (Config.debug) {
413
+ await Config.events.emitAsync(EventType.AppCompiled, {
414
+ sources: [
415
+ { compiledTeal: compiledApproval, appName: appSpec.name, fileName: 'approval' },
416
+ { compiledTeal: compiledClear, appName: appSpec.name, fileName: 'clear' },
417
+ ],
418
+ });
419
+ }
420
+ return {
421
+ approvalProgram: compiledApproval.compiledBase64ToBytes,
422
+ compiledApproval,
423
+ clearStateProgram: compiledClear.compiledBase64ToBytes,
424
+ compiledClear,
425
+ };
426
+ }
427
+ /**
428
+ * Returns ABI method arguments ready for a method call params object with default values populated
429
+ * and structs replaced with tuples.
430
+ *
431
+ * It does this by replacing any `undefined` values with the equivalent default value from the given ARC-56 app spec.
432
+ * @param methodNameOrSignature The method name or method signature to call if an ABI call is being emitted.
433
+ * e.g. `my_method` or `my_method(unit64,string)bytes`
434
+ * @param args The arguments to the method with `undefined` for any that should be populated with a default value
435
+ */
436
+ async getABIArgsWithDefaultValues(methodNameOrSignature, args, sender) {
437
+ const m = getArc56Method(methodNameOrSignature, this._appSpec);
438
+ return await Promise.all(args?.map(async (a, i) => {
439
+ const arg = m.args[i];
440
+ if (a !== undefined) {
441
+ // If a struct then convert to tuple for the underlying call
442
+ return arg.struct && typeof a === 'object' && !Array.isArray(a)
443
+ ? getABITupleFromABIStruct(a, this._appSpec.structs[arg.struct], this._appSpec.structs)
444
+ : a;
445
+ }
446
+ const defaultValue = arg.defaultValue;
447
+ if (defaultValue) {
448
+ switch (defaultValue.source) {
449
+ case 'literal':
450
+ if (typeof defaultValue.data === 'number')
451
+ return defaultValue.data;
452
+ return getABIDecodedValue(Buffer.from(defaultValue.data, 'base64'), m.method.args[i].defaultValue?.type ?? m.method.args[i].type, this._appSpec.structs);
453
+ // todo: When ARC-56 supports ABI calls as default args
454
+ // case 'abi': {
455
+ // const method = this.getABIMethod(defaultValue.data as string)
456
+ // const result = await this.send.call({
457
+ // method: defaultValue.data as string,
458
+ // methodArgs: method.args.map(() => undefined),
459
+ // sender,
460
+ // })
461
+ // return result.return!
462
+ // }
463
+ case 'local':
464
+ case 'global': {
465
+ const state = defaultValue.source === 'global' ? await this.getGlobalState() : await this.getLocalState(sender);
466
+ const value = Object.values(state).find((s) => s.keyBase64 === defaultValue.data);
467
+ if (!value) {
468
+ throw new Error(`Preparing default value for argument ${arg.name ?? `arg${i + 1}`} resulted in the failure: The key '${defaultValue.data}' could not be found in ${defaultValue.source} storage`);
469
+ }
470
+ return 'valueRaw' in value
471
+ ? getABIDecodedValue(value.valueRaw, m.method.args[i].defaultValue?.type ?? m.method.args[i].type, this._appSpec.structs)
472
+ : value.value;
473
+ }
474
+ case 'box': {
475
+ const value = await this.getBoxValue(Buffer.from(defaultValue.data, 'base64'));
476
+ return getABIDecodedValue(value, m.method.args[i].defaultValue?.type ?? m.method.args[i].type, this._appSpec.structs);
477
+ }
478
+ }
479
+ }
480
+ throw new Error(`No value provided for required argument ${arg.name ?? `arg${i + 1}`} in call to method ${m.name}`);
481
+ }) ?? []);
482
+ }
483
+ getBareParamsMethods() {
484
+ return {
485
+ /** Return params for an update call, including deploy-time TEAL template replacements and compilation if provided */
486
+ update: async (params) => {
487
+ return this.getBareParams({
488
+ ...params,
489
+ ...(await this.compile(params)),
490
+ }, OnApplicationComplete.UpdateApplicationOC);
491
+ },
492
+ /** Return params for an opt-in call */
493
+ optIn: (params) => {
494
+ return this.getBareParams(params, OnApplicationComplete.OptInOC);
495
+ },
496
+ /** Return params for a delete call */
497
+ delete: (params) => {
498
+ return this.getBareParams(params, OnApplicationComplete.DeleteApplicationOC);
499
+ },
500
+ /** Return params for a clear state call */
501
+ clearState: (params) => {
502
+ return this.getBareParams(params, OnApplicationComplete.ClearStateOC);
503
+ },
504
+ /** Return params for a close out call */
505
+ closeOut: (params) => {
506
+ return this.getBareParams(params, OnApplicationComplete.CloseOutOC);
507
+ },
508
+ /** Return params for a call (defaults to no-op) */
509
+ call: (params) => {
510
+ return this.getBareParams(params, params?.onComplete ?? OnApplicationComplete.NoOpOC);
511
+ },
512
+ };
513
+ }
514
+ getBareCreateTransactionMethods() {
515
+ return {
516
+ /** Returns a transaction for an update call, including deploy-time TEAL template replacements and compilation if provided */
517
+ update: async (params) => {
518
+ return this._algorand.createTransaction.appUpdate(await this.params.bare.update(params));
519
+ },
520
+ /** Returns a transaction for an opt-in call */
521
+ optIn: (params) => {
522
+ return this._algorand.createTransaction.appCall(this.params.bare.optIn(params));
523
+ },
524
+ /** Returns a transaction for a delete call */
525
+ delete: (params) => {
526
+ return this._algorand.createTransaction.appDelete(this.params.bare.delete(params));
527
+ },
528
+ /** Returns a transaction for a clear state call */
529
+ clearState: (params) => {
530
+ return this._algorand.createTransaction.appCall(this.params.bare.clearState(params));
531
+ },
532
+ /** Returns a transaction for a close out call */
533
+ closeOut: (params) => {
534
+ return this._algorand.createTransaction.appCall(this.params.bare.closeOut(params));
535
+ },
536
+ /** Returns a transaction for a call (defaults to no-op) */
537
+ call: (params) => {
538
+ return this._algorand.createTransaction.appCall(this.params.bare.call(params));
539
+ },
540
+ };
541
+ }
542
+ getBareSendMethods() {
543
+ return {
544
+ /** Signs and sends an update call, including deploy-time TEAL template replacements and compilation if provided */
545
+ update: async (params) => {
546
+ const compiled = await this.compile(params);
547
+ return {
548
+ ...(await this.handleCallErrors(async () => this._algorand.send.appUpdate(await this.params.bare.update(params)))),
549
+ ...compiled,
550
+ };
551
+ },
552
+ /** Signs and sends an opt-in call */
553
+ optIn: (params) => {
554
+ return this.handleCallErrors(() => this._algorand.send.appCall(this.params.bare.optIn(params)));
555
+ },
556
+ /** Signs and sends a delete call */
557
+ delete: (params) => {
558
+ return this.handleCallErrors(() => this._algorand.send.appDelete(this.params.bare.delete(params)));
559
+ },
560
+ /** Signs and sends a clear state call */
561
+ clearState: (params) => {
562
+ return this.handleCallErrors(() => this._algorand.send.appCall(this.params.bare.clearState(params)));
563
+ },
564
+ /** Signs and sends a close out call */
565
+ closeOut: (params) => {
566
+ return this.handleCallErrors(() => this._algorand.send.appCall(this.params.bare.closeOut(params)));
567
+ },
568
+ /** Signs and sends a call (defaults to no-op) */
569
+ call: (params) => {
570
+ return this.handleCallErrors(() => this._algorand.send.appCall(this.params.bare.call(params)));
571
+ },
572
+ };
573
+ }
574
+ getMethodCallParamsMethods() {
575
+ return {
576
+ /** Return params for a payment transaction to fund the app account */
577
+ fundAppAccount: (params) => {
578
+ return {
579
+ ...params,
580
+ sender: this.getSender(params.sender),
581
+ signer: this.getSigner(params.sender, params.signer),
582
+ receiver: this.appAddress,
583
+ };
584
+ },
585
+ /** Return params for an update ABI call, including deploy-time TEAL template replacements and compilation if provided */
586
+ update: async (params) => {
587
+ return (await this.getABIParams({
588
+ ...params,
589
+ ...(await this.compile(params)),
590
+ }, OnApplicationComplete.UpdateApplicationOC));
591
+ },
592
+ /** Return params for an opt-in ABI call */
593
+ optIn: async (params) => {
594
+ return (await this.getABIParams(params, OnApplicationComplete.OptInOC));
595
+ },
596
+ /** Return params for an delete ABI call */
597
+ delete: async (params) => {
598
+ return (await this.getABIParams(params, OnApplicationComplete.DeleteApplicationOC));
599
+ },
600
+ /** Return params for an close out ABI call */
601
+ closeOut: async (params) => {
602
+ return (await this.getABIParams(params, OnApplicationComplete.CloseOutOC));
603
+ },
604
+ /** Return params for an ABI call */
605
+ call: async (params) => {
606
+ return (await this.getABIParams(params, params.onComplete ?? OnApplicationComplete.NoOpOC));
607
+ },
608
+ };
609
+ }
610
+ getMethodCallSendMethods() {
611
+ return {
612
+ /** Sign and send transactions for a payment transaction to fund the app account */
613
+ fundAppAccount: (params) => {
614
+ return this._algorand.send.payment(this.params.fundAppAccount(params));
615
+ },
616
+ /**
617
+ * Sign and send transactions for an update ABI call, including deploy-time TEAL template replacements and compilation if provided
618
+ */
619
+ update: async (params) => {
620
+ const compiled = await this.compile(params);
621
+ return {
622
+ ...(await this.handleCallErrors(async () => this.processMethodCallReturn(this._algorand.send.appUpdateMethodCall(await this.params.update({ ...params })), getArc56Method(params.method, this._appSpec)))),
623
+ ...compiled,
624
+ };
625
+ },
626
+ /**
627
+ * Sign and send transactions for an opt-in ABI call
628
+ */
629
+ optIn: (params) => {
630
+ return this.handleCallErrors(async () => this.processMethodCallReturn(this._algorand.send.appCallMethodCall(await this.params.optIn(params)), getArc56Method(params.method, this._appSpec)));
631
+ },
632
+ /**
633
+ * Sign and send transactions for a delete ABI call
634
+ */
635
+ delete: (params) => {
636
+ return this.handleCallErrors(async () => this.processMethodCallReturn(this._algorand.send.appDeleteMethodCall(await this.params.delete(params)), getArc56Method(params.method, this._appSpec)));
637
+ },
638
+ /**
639
+ * Sign and send transactions for a close out ABI call
640
+ */
641
+ closeOut: (params) => {
642
+ return this.handleCallErrors(async () => this.processMethodCallReturn(this._algorand.send.appCallMethodCall(await this.params.closeOut(params)), getArc56Method(params.method, this._appSpec)));
643
+ },
644
+ /**
645
+ * Sign and send transactions for a call (defaults to no-op)
646
+ */
647
+ call: async (params) => {
648
+ // Read-only call - do it via simulate
649
+ if (params.onComplete === OnApplicationComplete.NoOpOC ||
650
+ (!params.onComplete && getArc56Method(params.method, this._appSpec).method.readonly)) {
651
+ const result = await this._algorand
652
+ .newGroup()
653
+ .addAppCallMethodCall(await this.params.call(params))
654
+ .simulate({
655
+ allowUnnamedResources: params.populateAppCallResources ?? true,
656
+ // Simulate calls for a readonly method shouldn't invoke signing
657
+ skipSignatures: true,
658
+ });
659
+ return this.processMethodCallReturn({
660
+ ...result,
661
+ transaction: result.transactions.at(-1),
662
+ confirmation: result.confirmations.at(-1),
663
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
664
+ return: (result.returns?.length ?? 0 > 0) ? result.returns?.at(-1) : undefined,
665
+ }, getArc56Method(params.method, this._appSpec));
666
+ }
667
+ return this.handleCallErrors(async () => this.processMethodCallReturn(this._algorand.send.appCallMethodCall(await this.params.call(params)), getArc56Method(params.method, this._appSpec)));
668
+ },
669
+ };
670
+ }
671
+ getMethodCallCreateTransactionMethods() {
672
+ return {
673
+ /** Return transaction for a payment transaction to fund the app account */
674
+ fundAppAccount: (params) => {
675
+ return this._algorand.createTransaction.payment(this.params.fundAppAccount(params));
676
+ },
677
+ /**
678
+ * Return transactions for an update ABI call, including deploy-time TEAL template replacements and compilation if provided
679
+ */
680
+ update: async (params) => {
681
+ return this._algorand.createTransaction.appUpdateMethodCall(await this.params.update(params));
682
+ },
683
+ /**
684
+ * Return transactions for an opt-in ABI call
685
+ */
686
+ optIn: async (params) => {
687
+ return this._algorand.createTransaction.appCallMethodCall(await this.params.optIn(params));
688
+ },
689
+ /**
690
+ * Return transactions for a delete ABI call
691
+ */
692
+ delete: async (params) => {
693
+ return this._algorand.createTransaction.appDeleteMethodCall(await this.params.delete(params));
694
+ },
695
+ /**
696
+ * Return transactions for a close out ABI call
697
+ */
698
+ closeOut: async (params) => {
699
+ return this._algorand.createTransaction.appCallMethodCall(await this.params.closeOut(params));
700
+ },
701
+ /**
702
+ * Return transactions for an ABI call (defaults to no-op)
703
+ */
704
+ call: async (params) => {
705
+ return this._algorand.createTransaction.appCallMethodCall(await this.params.call(params));
706
+ },
707
+ };
708
+ }
709
+ /** Returns the sender for a call, using the provided sender or using the `defaultSender`
710
+ * if none provided and throws an error if neither provided */
711
+ getSender(sender) {
712
+ if (!sender && !this._defaultSender) {
713
+ throw new Error(`No sender provided and no default sender present in app client for call to app ${this._appName}`);
714
+ }
715
+ return sender ?? this._defaultSender;
716
+ }
717
+ /** Returns the signer for a call, using the provided signer or the `defaultSigner`
718
+ * if no signer was provided and the call will use default sender
719
+ * or `undefined` otherwise (so the signer is resolved from `AlgorandClient`) */
720
+ getSigner(sender, signer) {
721
+ return signer ?? (!sender ? this._defaultSigner : undefined);
722
+ }
723
+ getBareParams(params, onComplete) {
724
+ return {
725
+ ...params,
726
+ appId: this._appId,
727
+ sender: this.getSender(params?.sender),
728
+ signer: this.getSigner(params?.sender, params?.signer),
729
+ onComplete,
730
+ };
731
+ }
732
+ async getABIParams(params, onComplete) {
733
+ const sender = this.getSender(params.sender);
734
+ const method = getArc56Method(params.method, this._appSpec);
735
+ const args = await this.getABIArgsWithDefaultValues(params.method, params.args, sender);
736
+ return {
737
+ ...params,
738
+ appId: this._appId,
739
+ sender: sender,
740
+ signer: this.getSigner(params.sender, params.signer),
741
+ method,
742
+ onComplete,
743
+ args,
744
+ };
745
+ }
746
+ /** Make the given call and catch any errors, augmenting with debugging information before re-throwing. */
747
+ async handleCallErrors(call) {
748
+ try {
749
+ return await call();
750
+ }
751
+ catch (e) {
752
+ throw this.exposeLogicError(e);
753
+ }
754
+ }
755
+ getBoxMethods() {
756
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
757
+ const that = this;
758
+ const stateMethods = {
759
+ /**
760
+ * Returns all single-key state values in a record keyed by the key name and the value a decoded ABI value.
761
+ */
762
+ getAll: async () => {
763
+ return Object.fromEntries(await Promise.all(Object.keys(that._appSpec.state.keys.box).map(async (key) => [key, await stateMethods.getValue(key)])));
764
+ },
765
+ /**
766
+ * Returns a single state value for the current app with the value a decoded ABI value.
767
+ * @param name The name of the state value to retrieve the value for
768
+ * @returns
769
+ */
770
+ getValue: async (name) => {
771
+ const metadata = that._appSpec.state.keys.box[name];
772
+ const value = await that.getBoxValue(Buffer.from(metadata.key, 'base64'));
773
+ return getABIDecodedValue(value, metadata.valueType, that._appSpec.structs);
774
+ },
775
+ /**
776
+ *
777
+ * @param mapName The name of the map to read from
778
+ * @param key The key within the map (without any map prefix) as either a Buffer with the bytes or a value
779
+ * that will be converted to bytes by encoding it using the specified ABI key type
780
+ * in the ARC-56 spec
781
+ */
782
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
783
+ getMapValue: async (mapName, key) => {
784
+ const metadata = that._appSpec.state.maps.box[mapName];
785
+ const prefix = Buffer.from(metadata.prefix ?? '', 'base64');
786
+ const encodedKey = Buffer.concat([prefix, getABIEncodedValue(key, metadata.keyType, that._appSpec.structs)]);
787
+ const base64Key = Buffer.from(encodedKey).toString('base64');
788
+ const value = await that.getBoxValue(Buffer.from(base64Key, 'base64'));
789
+ return getABIDecodedValue(value, metadata.valueType, that._appSpec.structs);
790
+ },
791
+ /**
792
+ *
793
+ * @param mapName The name of the map to read from
794
+ * @param key The key within the map as either a Buffer with the bytes or a value
795
+ * that will be converted to bytes by encoding it using the specified ABI key type
796
+ * in the ARC-56 spec
797
+ * @param appState
798
+ */
799
+ getMap: async (mapName) => {
800
+ const metadata = that._appSpec.state.maps.box[mapName];
801
+ const prefix = Buffer.from(metadata.prefix ?? '', 'base64');
802
+ const boxNames = await that.getBoxNames();
803
+ return new Map(await Promise.all(boxNames
804
+ .filter((b) => binaryStartsWith(b.nameRaw, prefix))
805
+ .map(async (b) => {
806
+ const encodedKey = Buffer.concat([prefix, b.nameRaw]);
807
+ const base64Key = Buffer.from(encodedKey).toString('base64');
808
+ return [
809
+ getABIDecodedValue(b.nameRaw.slice(prefix.length), metadata.keyType, that._appSpec.structs),
810
+ getABIDecodedValue(await that.getBoxValue(Buffer.from(base64Key, 'base64')), metadata.valueType, that._appSpec.structs),
811
+ ];
812
+ })));
813
+ },
814
+ };
815
+ return stateMethods;
816
+ }
817
+ getStateMethods(stateGetter, keyGetter, mapGetter) {
818
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
819
+ const that = this;
820
+ const stateMethods = {
821
+ /**
822
+ * Returns all single-key state values in a record keyed by the key name and the value a decoded ABI value.
823
+ */
824
+ getAll: async () => {
825
+ const appState = await stateGetter();
826
+ return Object.fromEntries(await Promise.all(Object.keys(keyGetter()).map(async (key) => [key, await stateMethods.getValue(key, appState)])));
827
+ },
828
+ /**
829
+ * Returns a single state value for the current app with the value a decoded ABI value.
830
+ * @param name The name of the state value to retrieve the value for
831
+ * @param appState Optional cached value of the current state
832
+ * @returns
833
+ */
834
+ getValue: async (name, appState) => {
835
+ const state = Object.values(appState ?? (await stateGetter()));
836
+ const metadata = keyGetter()[name];
837
+ const value = state.find((s) => s.keyBase64 === metadata.key);
838
+ if (value && 'valueRaw' in value) {
839
+ return getABIDecodedValue(value.valueRaw, metadata.valueType, that._appSpec.structs);
840
+ }
841
+ return value?.value;
842
+ },
843
+ /**
844
+ * Returns a single value from the given map for the current app with the value a decoded ABI value.
845
+ * @param mapName The name of the map to read from
846
+ * @param key The key within the map (without any map prefix) as either a Buffer with the bytes or a value
847
+ * that will be converted to bytes by encoding it using the specified ABI key type
848
+ * in the ARC-56 spec
849
+ * @param appState Optional cached value of the current state
850
+ */
851
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
852
+ getMapValue: async (mapName, key, appState) => {
853
+ const state = Object.values(appState ?? (await stateGetter()));
854
+ const metadata = mapGetter()[mapName];
855
+ const prefix = Buffer.from(metadata.prefix ?? '', 'base64');
856
+ const encodedKey = Buffer.concat([prefix, getABIEncodedValue(key, metadata.keyType, that._appSpec.structs)]);
857
+ const base64Key = Buffer.from(encodedKey).toString('base64');
858
+ const value = state.find((s) => s.keyBase64 === base64Key);
859
+ if (value && 'valueRaw' in value) {
860
+ return getABIDecodedValue(value.valueRaw, metadata.valueType, that._appSpec.structs);
861
+ }
862
+ return value?.value;
863
+ },
864
+ /**
865
+ * Returns all map values for the given map.
866
+ * @param mapName The name of the map to read from
867
+ * @param appState Optional cached value of the current state
868
+ * @returns A map of all key-value pairs in the map as a `Record<string, ABIValue>`
869
+ */
870
+ getMap: async (mapName) => {
871
+ const state = Object.values(await stateGetter());
872
+ const metadata = mapGetter()[mapName];
873
+ const prefix = Buffer.from(metadata.prefix ?? '', 'base64');
874
+ return new Map(state
875
+ .filter((s) => binaryStartsWith(s.keyRaw, prefix))
876
+ .map((s) => {
877
+ const key = s.keyRaw.slice(prefix.length);
878
+ return [
879
+ getABIDecodedValue(key, metadata.keyType, this._appSpec.structs),
880
+ getABIDecodedValue('valueRaw' in s ? s.valueRaw : s.value, metadata.valueType, this._appSpec.structs),
881
+ ];
882
+ }));
883
+ },
884
+ };
885
+ return stateMethods;
886
+ }
887
+ }
888
+ /**
889
+ * @deprecated Use `AppClient` instead e.g. via `algorand.client.getAppClientById` or
890
+ * `algorand.client.getAppClientByCreatorAndName`.
891
+ * If you want to `create` or `deploy` then use `AppFactory` e.g. via `algorand.client.getAppFactory`,
892
+ * which will in turn give you an `AppClient` instance against the created/deployed app to make other calls.
893
+ *
894
+ * Application client - a class that wraps an ARC-0032 app spec and provides high productivity methods to deploy and call the app */
41
895
  class ApplicationClient {
42
- // todo: process ABI args as needed to make them nicer to deal with like beaker-ts
43
- // todo: support readonly, noop method calls
44
- // todo: find create, update, delete, etc. methods from app spec and call them by default
45
- // todo: intelligent version management when deploying
46
896
  /**
897
+ * @deprecated Use `AppClient` instead e.g. via `algorand.client.getAppClientById` or
898
+ * `algorand.client.getAppClientByCreatorAndName`.
899
+ * If you want to `create` or `deploy` then use `AppFactory` e.g. via `algorand.client.getAppFactory`,
900
+ * which will in turn give you an `AppClient` instance against the created/deployed app to make other calls.
901
+ *
47
902
  * Create a new ApplicationClient instance
48
903
  * @param appDetails The details of the app
49
904
  * @param algod An algod instance
@@ -78,9 +933,11 @@ class ApplicationClient {
78
933
  this.params = params;
79
934
  }
80
935
  /**
81
- * Compiles the approval and clear programs and sets up the source map.
936
+ * @deprecated Use `AppClient.compile()` instead.
937
+ *
938
+ * Compiles the approval and clear state programs and sets up the source map.
82
939
  * @param compilation The deploy-time parameters for the compilation
83
- * @returns The compiled approval and clear programs
940
+ * @returns The compiled approval and clear state programs
84
941
  */
85
942
  async compile(compilation) {
86
943
  const { deployTimeParams, updatable, deletable } = compilation ?? {};
@@ -95,15 +952,12 @@ class ApplicationClient {
95
952
  const clear = performTemplateSubstitution(clearTemplate, deployTimeParams ?? this.deployTimeParams);
96
953
  const clearCompiled = await compileTeal(clear, this.algod);
97
954
  this._clearSourceMap = clearCompiled?.sourceMap;
98
- if (Config.debug && Config.projectRoot) {
99
- persistSourceMaps({
955
+ if (Config.debug) {
956
+ await Config.events.emitAsync(EventType.AppCompiled, {
100
957
  sources: [
101
- PersistSourceMapInput.fromCompiledTeal(approvalCompiled, this._appName, 'approval.teal'),
102
- PersistSourceMapInput.fromCompiledTeal(clearCompiled, this._appName, 'clear.teal'),
958
+ { compiledTeal: approvalCompiled, appName: this._appName, fileName: 'approval' },
959
+ { compiledTeal: clearCompiled, appName: this._appName, fileName: 'clear' },
103
960
  ],
104
- projectRoot: Config.projectRoot,
105
- client: this.algod,
106
- withSources: true,
107
961
  });
108
962
  }
109
963
  return { approvalCompiled, clearCompiled };
@@ -130,6 +984,8 @@ class ApplicationClient {
130
984
  this._clearSourceMap = new SourceMap(sourceMaps.clearSourceMap);
131
985
  }
132
986
  /**
987
+ * @deprecated Use `deploy` from an `AppFactory` instance instead.
988
+ *
133
989
  * Idempotently deploy (create, update/delete if changed) an app against the given name via the given creator account, including deploy-time template placeholder substitutions.
134
990
  *
135
991
  * To understand the architecture decisions behind this functionality please see https://github.com/algorandfoundation/algokit-cli/blob/main/docs/architecture-decisions/2023-01-12_smart-contract-deployment.md
@@ -215,6 +1071,8 @@ class ApplicationClient {
215
1071
  }
216
1072
  }
217
1073
  /**
1074
+ * @deprecated Use `create` from an `AppFactory` instance instead.
1075
+ *
218
1076
  * Creates a smart contract app, returns the details of the created app.
219
1077
  * @param create The parameters to create the app with
220
1078
  * @returns The details of the created app, or the transaction to create it if `skipSending` and the compilation result
@@ -259,6 +1117,8 @@ class ApplicationClient {
259
1117
  }
260
1118
  }
261
1119
  /**
1120
+ * @deprecated Use `appClient.send.update` or `appClient.createTransaction.update` from an `AppClient` instance instead.
1121
+ *
262
1122
  * Updates the smart contract app.
263
1123
  * @param update The parameters to update the app with
264
1124
  * @returns The transaction send result and the compilation result
@@ -291,6 +1151,8 @@ class ApplicationClient {
291
1151
  }
292
1152
  }
293
1153
  /**
1154
+ * @deprecated Use `appClient.send.call` or `appClient.createTransaction.call` from an `AppClient` instance instead.
1155
+ *
294
1156
  * Issues a no_op (normal) call to the app.
295
1157
  * @param call The call details.
296
1158
  * @returns The result of the call
@@ -305,7 +1167,7 @@ class ApplicationClient {
305
1167
  !call.sendParams?.atc &&
306
1168
  // The method is readonly
307
1169
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
308
- this.appSpec.hints[getABIMethodSignature(this.getABIMethod(call.method))].read_only) {
1170
+ this.appSpec.hints[this.getABIMethodSignature(this.getABIMethod(call.method))].read_only) {
309
1171
  const atc = new AtomicTransactionComposer();
310
1172
  await this.callOfType({ ...call, sendParams: { ...call.sendParams, atc } }, 'no_op');
311
1173
  const result = await atc.simulate(this.algod);
@@ -324,6 +1186,8 @@ class ApplicationClient {
324
1186
  return await this.callOfType(call, 'no_op');
325
1187
  }
326
1188
  /**
1189
+ * @deprecated Use `appClient.send.optIn` or `appClient.createTransaction.optIn` from an `AppClient` instance instead.
1190
+ *
327
1191
  * Issues a opt_in call to the app.
328
1192
  * @param call The call details.
329
1193
  * @returns The result of the call
@@ -332,6 +1196,8 @@ class ApplicationClient {
332
1196
  return await this.callOfType(call, 'opt_in');
333
1197
  }
334
1198
  /**
1199
+ * @deprecated Use `appClient.send.closeOut` or `appClient.createTransaction.closeOut` from an `AppClient` instance instead.
1200
+ *
335
1201
  * Issues a close_out call to the app.
336
1202
  * @param call The call details.
337
1203
  * @returns The result of the call
@@ -340,6 +1206,8 @@ class ApplicationClient {
340
1206
  return await this.callOfType(call, 'close_out');
341
1207
  }
342
1208
  /**
1209
+ * @deprecated Use `appClient.send.clearState` or `appClient.createTransaction.clearState` from an `AppClient` instance instead.
1210
+ *
343
1211
  * Issues a clear_state call to the app.
344
1212
  * @param call The call details.
345
1213
  * @returns The result of the call
@@ -348,6 +1216,8 @@ class ApplicationClient {
348
1216
  return await this.callOfType(call, 'clear_state');
349
1217
  }
350
1218
  /**
1219
+ * @deprecated Use `appClient.send.delete` or `appClient.createTransaction.delete` from an `AppClient` instance instead.
1220
+ *
351
1221
  * Issues a delete_application call to the app.
352
1222
  * @param call The call details.
353
1223
  * @returns The result of the call
@@ -356,6 +1226,8 @@ class ApplicationClient {
356
1226
  return await this.callOfType(call, 'delete_application');
357
1227
  }
358
1228
  /**
1229
+ * @deprecated Use `appClient.send.call` or `appClient.createTransaction.call` from an `AppClient` instance instead.
1230
+ *
359
1231
  * Issues a call to the app with the given call type.
360
1232
  * @param call The call details.
361
1233
  * @param callType The call type
@@ -498,6 +1370,8 @@ class ApplicationClient {
498
1370
  })));
499
1371
  }
500
1372
  /**
1373
+ * @deprecated Use `appClient.params.*` from an `AppClient` instance instead.
1374
+ *
501
1375
  * Returns the arguments for an app call for the given ABI method or raw method specification.
502
1376
  * @param args The call args specific to this application client
503
1377
  * @param sender The sender of this call. Will be used to fetch any default argument values if applicable
@@ -512,7 +1386,7 @@ class ApplicationClient {
512
1386
  if (!abiMethod) {
513
1387
  throw new Error(`Attempt to call ABI method ${args.method}, but it wasn't found`);
514
1388
  }
515
- const methodSignature = getABIMethodSignature(abiMethod);
1389
+ const methodSignature = this.getABIMethodSignature(abiMethod);
516
1390
  return {
517
1391
  ...args,
518
1392
  method: abiMethod,
@@ -529,7 +1403,7 @@ class ApplicationClient {
529
1403
  case 'abi-method': {
530
1404
  const method = defaultValueStrategy.data;
531
1405
  const result = await this.callOfType({
532
- method: getABIMethodSignature(method),
1406
+ method: this.getABIMethodSignature(method),
533
1407
  methodArgs: method.args.map(() => undefined),
534
1408
  sender,
535
1409
  }, 'no_op');
@@ -555,6 +1429,8 @@ class ApplicationClient {
555
1429
  }
556
1430
  }
557
1431
  /**
1432
+ * @deprecated Use `appClient.getABIMethod` instead.
1433
+ *
558
1434
  * Returns the ABI Method parameters for the given method name string for the app represented by this application client instance
559
1435
  * @param method Either the name of the method or the ABI method spec definition string
560
1436
  * @returns The ABI method params for the given method
@@ -564,12 +1440,12 @@ class ApplicationClient {
564
1440
  const methods = this.appSpec.contract.methods.filter((m) => m.name === method);
565
1441
  if (methods.length > 1) {
566
1442
  throw new Error(`Received a call to method ${method} in contract ${this._appName}, but this resolved to multiple methods; please pass in an ABI signature instead: ${methods
567
- .map(getABIMethodSignature)
1443
+ .map(this.getABIMethodSignature)
568
1444
  .join(', ')}`);
569
1445
  }
570
1446
  return methods[0];
571
1447
  }
572
- return this.appSpec.contract.methods.find((m) => getABIMethodSignature(m) === method);
1448
+ return this.appSpec.contract.methods.find((m) => this.getABIMethodSignature(m) === method);
573
1449
  }
574
1450
  /**
575
1451
  * Returns the ABI Method for the given method name string for the app represented by this application client instance
@@ -581,6 +1457,8 @@ class ApplicationClient {
581
1457
  return methodParams ? new ABIMethod(methodParams) : undefined;
582
1458
  }
583
1459
  /**
1460
+ * @deprecated Use `appClient.appId` and `appClient.appAddress` from an `AppClient` instance instead.
1461
+ *
584
1462
  * Gets the reference information for the current application instance.
585
1463
  * `appId` will be 0 if it can't find an app.
586
1464
  * @returns The app reference, or if deployed using the `deploy` method, the app metadata too
@@ -626,7 +1504,10 @@ class ApplicationClient {
626
1504
  else
627
1505
  return e;
628
1506
  }
1507
+ getABIMethodSignature(method) {
1508
+ return 'getSignature' in method ? method.getSignature() : new ABIMethod(method).getSignature();
1509
+ }
629
1510
  }
630
1511
 
631
- export { ApplicationClient };
1512
+ export { AppClient, ApplicationClient };
632
1513
  //# sourceMappingURL=app-client.mjs.map