@algorandfoundation/algokit-utils 10.0.0-alpha.31 → 10.0.0-alpha.32

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 (318) hide show
  1. package/account-manager.d.ts +448 -0
  2. package/account-manager.js +623 -0
  3. package/account-manager.js.map +1 -0
  4. package/account-manager.mjs +620 -0
  5. package/account-manager.mjs.map +1 -0
  6. package/account.d.ts +156 -0
  7. package/account.js +10 -0
  8. package/account.js.map +1 -0
  9. package/account.mjs +9 -0
  10. package/account.mjs.map +1 -0
  11. package/algorand-client-transaction-creator.d.ts +1103 -0
  12. package/algorand-client-transaction-creator.js +735 -0
  13. package/algorand-client-transaction-creator.js.map +1 -0
  14. package/algorand-client-transaction-creator.mjs +734 -0
  15. package/algorand-client-transaction-creator.mjs.map +1 -0
  16. package/algorand-client-transaction-sender.d.ts +1317 -0
  17. package/algorand-client-transaction-sender.js +933 -0
  18. package/algorand-client-transaction-sender.js.map +1 -0
  19. package/algorand-client-transaction-sender.mjs +932 -0
  20. package/algorand-client-transaction-sender.mjs.map +1 -0
  21. package/algorand-client.d.ts +246 -0
  22. package/algorand-client.js +325 -0
  23. package/algorand-client.js.map +1 -0
  24. package/algorand-client.mjs +325 -0
  25. package/algorand-client.mjs.map +1 -0
  26. package/amount.d.ts +46 -3
  27. package/amount.js +92 -13
  28. package/amount.js.map +1 -1
  29. package/amount.mjs +80 -3
  30. package/amount.mjs.map +1 -1
  31. package/app-client.d.ts +2130 -0
  32. package/app-client.js +909 -0
  33. package/app-client.js.map +1 -0
  34. package/app-client.mjs +908 -0
  35. package/app-client.mjs.map +1 -0
  36. package/app-deployer.d.ts +166 -0
  37. package/app-deployer.js +353 -0
  38. package/app-deployer.js.map +1 -0
  39. package/app-deployer.mjs +353 -0
  40. package/app-deployer.mjs.map +1 -0
  41. package/app-factory.d.ts +965 -0
  42. package/app-factory.js +448 -0
  43. package/app-factory.js.map +1 -0
  44. package/app-factory.mjs +448 -0
  45. package/app-factory.mjs.map +1 -0
  46. package/app-manager.d.ts +323 -0
  47. package/app-manager.js +468 -0
  48. package/app-manager.js.map +1 -0
  49. package/app-manager.mjs +468 -0
  50. package/app-manager.mjs.map +1 -0
  51. package/app-spec.d.ts +203 -0
  52. package/app-spec.js +137 -0
  53. package/app-spec.js.map +1 -0
  54. package/app-spec.mjs +137 -0
  55. package/app-spec.mjs.map +1 -0
  56. package/app.d.ts +257 -0
  57. package/app.js +49 -0
  58. package/app.js.map +1 -0
  59. package/app.mjs +42 -0
  60. package/app.mjs.map +1 -0
  61. package/asset-manager.d.ts +212 -0
  62. package/asset-manager.js +166 -0
  63. package/asset-manager.js.map +1 -0
  64. package/asset-manager.mjs +166 -0
  65. package/asset-manager.mjs.map +1 -0
  66. package/async-event-emitter.d.ts +16 -0
  67. package/async-event-emitter.js +38 -0
  68. package/async-event-emitter.js.map +1 -0
  69. package/async-event-emitter.mjs +37 -0
  70. package/async-event-emitter.mjs.map +1 -0
  71. package/client-manager.d.ts +475 -0
  72. package/client-manager.js +616 -0
  73. package/client-manager.js.map +1 -0
  74. package/client-manager.mjs +616 -0
  75. package/client-manager.mjs.map +1 -0
  76. package/composer.d.ts +947 -0
  77. package/composer.js +1584 -0
  78. package/composer.js.map +1 -0
  79. package/composer.mjs +1583 -0
  80. package/composer.mjs.map +1 -0
  81. package/config.d.ts +1 -1
  82. package/config.js +2 -2
  83. package/config.js.map +1 -1
  84. package/config.mjs +1 -1
  85. package/config.mjs.map +1 -1
  86. package/debugging.d.ts +47 -0
  87. package/debugging.js +20 -0
  88. package/debugging.js.map +1 -0
  89. package/debugging.mjs +15 -0
  90. package/debugging.mjs.map +1 -0
  91. package/dispenser-client.d.ts +90 -0
  92. package/dispenser-client.js +127 -0
  93. package/dispenser-client.js.map +1 -0
  94. package/dispenser-client.mjs +127 -0
  95. package/dispenser-client.mjs.map +1 -0
  96. package/expand.d.ts +2 -0
  97. package/expand.js +0 -0
  98. package/expand.mjs +0 -0
  99. package/index.d.ts +6 -5
  100. package/index.js +4 -3
  101. package/index.mjs +5 -5
  102. package/indexer-client/indexer-lookup.d.ts +1 -1
  103. package/indexer-client/indexer-lookup.js.map +1 -1
  104. package/indexer-client/indexer-lookup.mjs.map +1 -1
  105. package/indexer.d.ts +40 -0
  106. package/indexer.js +38 -0
  107. package/indexer.js.map +1 -0
  108. package/indexer.mjs +35 -0
  109. package/indexer.mjs.map +1 -0
  110. package/instance-of.d.ts +8 -0
  111. package/kmd-account-manager.d.ts +74 -0
  112. package/kmd-account-manager.js +167 -0
  113. package/kmd-account-manager.js.map +1 -0
  114. package/kmd-account-manager.mjs +165 -0
  115. package/kmd-account-manager.mjs.map +1 -0
  116. package/lifecycle-events.d.ts +14 -0
  117. package/lifecycle-events.js +11 -0
  118. package/lifecycle-events.js.map +1 -0
  119. package/lifecycle-events.mjs +10 -0
  120. package/lifecycle-events.mjs.map +1 -0
  121. package/logging.d.ts +13 -0
  122. package/logging.js +47 -0
  123. package/logging.js.map +1 -0
  124. package/logging.mjs +42 -0
  125. package/logging.mjs.map +1 -0
  126. package/logic-error.d.ts +39 -0
  127. package/logic-error.js +54 -0
  128. package/logic-error.js.map +1 -0
  129. package/logic-error.mjs +53 -0
  130. package/logic-error.mjs.map +1 -0
  131. package/network-client.d.ts +43 -0
  132. package/network-client.js +14 -0
  133. package/network-client.js.map +1 -0
  134. package/network-client.mjs +13 -0
  135. package/network-client.mjs.map +1 -0
  136. package/package.json +11 -1
  137. package/testing/account.d.ts +2 -2
  138. package/testing/account.js +1 -1
  139. package/testing/account.js.map +1 -1
  140. package/testing/account.mjs +1 -1
  141. package/testing/account.mjs.map +1 -1
  142. package/testing/fixtures/algokit-log-capture-fixture.d.ts +1 -1
  143. package/testing/fixtures/algokit-log-capture-fixture.js.map +1 -1
  144. package/testing/fixtures/algokit-log-capture-fixture.mjs.map +1 -1
  145. package/testing/fixtures/algorand-fixture.d.ts +2 -2
  146. package/testing/fixtures/algorand-fixture.js +2 -2
  147. package/testing/fixtures/algorand-fixture.js.map +1 -1
  148. package/testing/fixtures/algorand-fixture.mjs +2 -2
  149. package/testing/fixtures/algorand-fixture.mjs.map +1 -1
  150. package/testing/index.d.ts +2 -1
  151. package/testing/test-logger.d.ts +1 -1
  152. package/testing/test-logger.js.map +1 -1
  153. package/testing/test-logger.mjs.map +1 -1
  154. package/testing/types.d.ts +156 -0
  155. package/transaction/index.d.ts +4 -0
  156. package/transaction/index.js +9 -0
  157. package/transaction/index.mjs +4 -0
  158. package/transaction/perform-transaction-composer-simulate.d.ts +1 -1
  159. package/transaction/perform-transaction-composer-simulate.js.map +1 -1
  160. package/transaction/perform-transaction-composer-simulate.mjs.map +1 -1
  161. package/transaction/transaction.d.ts +2 -2
  162. package/transaction/transaction.js.map +1 -1
  163. package/transaction/transaction.mjs.map +1 -1
  164. package/transaction/types.d.ts +133 -0
  165. package/transactions/app-call.d.ts +1 -1
  166. package/transactions/app-call.js +1 -1
  167. package/transactions/app-call.js.map +1 -1
  168. package/transactions/app-call.mjs +1 -1
  169. package/transactions/app-call.mjs.map +1 -1
  170. package/transactions/common.d.ts +1 -1
  171. package/transactions/common.js.map +1 -1
  172. package/transactions/common.mjs.map +1 -1
  173. package/transactions/method-call.d.ts +1 -1
  174. package/transactions/method-call.js +1 -1
  175. package/transactions/method-call.js.map +1 -1
  176. package/transactions/method-call.mjs +1 -1
  177. package/transactions/method-call.mjs.map +1 -1
  178. package/transactions/payment.d.ts +1 -1
  179. package/transactions/payment.js.map +1 -1
  180. package/transactions/payment.mjs.map +1 -1
  181. package/types/account-manager.d.ts +11 -442
  182. package/types/account-manager.js +5 -616
  183. package/types/account-manager.js.map +1 -1
  184. package/types/account-manager.mjs +5 -614
  185. package/types/account-manager.mjs.map +1 -1
  186. package/types/account.d.ts +8 -150
  187. package/types/account.js +3 -4
  188. package/types/account.js.map +1 -1
  189. package/types/account.mjs +4 -4
  190. package/types/account.mjs.map +1 -1
  191. package/types/algorand-client-transaction-creator.d.ts +5 -1098
  192. package/types/algorand-client-transaction-creator.js +3 -729
  193. package/types/algorand-client-transaction-creator.js.map +1 -1
  194. package/types/algorand-client-transaction-creator.mjs +4 -729
  195. package/types/algorand-client-transaction-creator.mjs.map +1 -1
  196. package/types/algorand-client-transaction-sender.d.ts +5 -1312
  197. package/types/algorand-client-transaction-sender.js +3 -927
  198. package/types/algorand-client-transaction-sender.js.map +1 -1
  199. package/types/algorand-client-transaction-sender.mjs +3 -926
  200. package/types/algorand-client-transaction-sender.mjs.map +1 -1
  201. package/types/algorand-client.d.ts +5 -241
  202. package/types/algorand-client.js +3 -319
  203. package/types/algorand-client.js.map +1 -1
  204. package/types/algorand-client.mjs +3 -319
  205. package/types/algorand-client.mjs.map +1 -1
  206. package/types/amount.d.ts +6 -45
  207. package/types/amount.js +3 -79
  208. package/types/amount.js.map +1 -1
  209. package/types/amount.mjs +4 -79
  210. package/types/amount.mjs.map +1 -1
  211. package/types/app-client.d.ts +75 -2125
  212. package/types/app-client.js +3 -903
  213. package/types/app-client.js.map +1 -1
  214. package/types/app-client.mjs +3 -902
  215. package/types/app-client.mjs.map +1 -1
  216. package/types/app-deployer.d.ts +21 -161
  217. package/types/app-deployer.js +3 -347
  218. package/types/app-deployer.js.map +1 -1
  219. package/types/app-deployer.mjs +3 -347
  220. package/types/app-deployer.mjs.map +1 -1
  221. package/types/app-factory.d.ts +21 -960
  222. package/types/app-factory.js +3 -442
  223. package/types/app-factory.js.map +1 -1
  224. package/types/app-factory.mjs +3 -442
  225. package/types/app-factory.mjs.map +1 -1
  226. package/types/app-manager.d.ts +15 -318
  227. package/types/app-manager.js +3 -462
  228. package/types/app-manager.js.map +1 -1
  229. package/types/app-manager.mjs +3 -462
  230. package/types/app-manager.mjs.map +1 -1
  231. package/types/app-spec.d.ts +39 -198
  232. package/types/app-spec.js +3 -131
  233. package/types/app-spec.js.map +1 -1
  234. package/types/app-spec.mjs +3 -131
  235. package/types/app-spec.mjs.map +1 -1
  236. package/types/app.d.ts +62 -247
  237. package/types/app.js +15 -37
  238. package/types/app.js.map +1 -1
  239. package/types/app.mjs +16 -37
  240. package/types/app.mjs.map +1 -1
  241. package/types/asset-manager.d.ts +9 -207
  242. package/types/asset-manager.js +3 -160
  243. package/types/asset-manager.js.map +1 -1
  244. package/types/asset-manager.mjs +3 -160
  245. package/types/asset-manager.mjs.map +1 -1
  246. package/types/async-event-emitter.d.ts +7 -11
  247. package/types/async-event-emitter.js +3 -32
  248. package/types/async-event-emitter.js.map +1 -1
  249. package/types/async-event-emitter.mjs +4 -32
  250. package/types/async-event-emitter.mjs.map +1 -1
  251. package/types/client-manager.d.ts +27 -470
  252. package/types/client-manager.js +3 -610
  253. package/types/client-manager.js.map +1 -1
  254. package/types/client-manager.mjs +3 -610
  255. package/types/client-manager.mjs.map +1 -1
  256. package/types/composer.d.ts +79 -943
  257. package/types/composer.js +3 -1578
  258. package/types/composer.js.map +1 -1
  259. package/types/composer.mjs +3 -1577
  260. package/types/composer.mjs.map +1 -1
  261. package/types/config.d.ts +7 -52
  262. package/types/config.js +3 -74
  263. package/types/config.js.map +1 -1
  264. package/types/config.mjs +3 -74
  265. package/types/config.mjs.map +1 -1
  266. package/types/debugging.d.ts +12 -37
  267. package/types/debugging.js +11 -10
  268. package/types/debugging.js.map +1 -1
  269. package/types/debugging.mjs +12 -10
  270. package/types/debugging.mjs.map +1 -1
  271. package/types/dispenser-client.d.ts +11 -85
  272. package/types/dispenser-client.js +3 -121
  273. package/types/dispenser-client.js.map +1 -1
  274. package/types/dispenser-client.mjs +3 -121
  275. package/types/dispenser-client.mjs.map +1 -1
  276. package/types/expand.d.ts +8 -2
  277. package/types/indexer.d.ts +16 -36
  278. package/types/indexer.js +7 -30
  279. package/types/indexer.js.map +1 -1
  280. package/types/indexer.mjs +8 -30
  281. package/types/indexer.mjs.map +1 -1
  282. package/types/instance-of.d.ts +4 -4
  283. package/types/kmd-account-manager.d.ts +5 -69
  284. package/types/kmd-account-manager.js +3 -161
  285. package/types/kmd-account-manager.js.map +1 -1
  286. package/types/kmd-account-manager.mjs +3 -159
  287. package/types/kmd-account-manager.mjs.map +1 -1
  288. package/types/lifecycle-events.d.ts +7 -9
  289. package/types/lifecycle-events.js +3 -5
  290. package/types/lifecycle-events.js.map +1 -1
  291. package/types/lifecycle-events.mjs +4 -5
  292. package/types/lifecycle-events.mjs.map +1 -1
  293. package/types/logging.d.ts +14 -9
  294. package/types/logging.js +11 -37
  295. package/types/logging.js.map +1 -1
  296. package/types/logging.mjs +12 -37
  297. package/types/logging.mjs.map +1 -1
  298. package/types/logic-error.d.ts +8 -35
  299. package/types/logic-error.js +3 -48
  300. package/types/logic-error.js.map +1 -1
  301. package/types/logic-error.mjs +4 -48
  302. package/types/logic-error.mjs.map +1 -1
  303. package/types/network-client.d.ts +10 -39
  304. package/types/network-client.js +3 -8
  305. package/types/network-client.js.map +1 -1
  306. package/types/network-client.mjs +4 -8
  307. package/types/network-client.mjs.map +1 -1
  308. package/types/testing.d.ts +13 -151
  309. package/types/transaction.d.ts +33 -128
  310. package/updatable-config.d.ts +57 -0
  311. package/updatable-config.js +80 -0
  312. package/updatable-config.js.map +1 -0
  313. package/updatable-config.mjs +80 -0
  314. package/updatable-config.mjs.map +1 -0
  315. package/util.js +1 -1
  316. package/util.js.map +1 -1
  317. package/util.mjs +1 -1
  318. package/util.mjs.map +1 -1
package/app-client.mjs ADDED
@@ -0,0 +1,908 @@
1
+ import { getAddress, getApplicationAddress, getOptionalAddress } from "./packages/common/src/address.mjs";
2
+ import { Config } from "./config.mjs";
3
+ import { asJson, binaryStartsWith } from "./util.mjs";
4
+ import { ProgramSourceMap } from "./packages/common/src/sourcemap.mjs";
5
+ import { OnApplicationComplete } from "./packages/transact/src/transactions/app-call.mjs";
6
+ import { EventType } from "./lifecycle-events.mjs";
7
+ import { argTypeIsTransaction, getABIDecodedValue, getABIEncodedValue, getABIMethod } from "./packages/abi/src/abi-method.mjs";
8
+ import { getBoxABIStorageKey, getBoxABIStorageKeys, getBoxABIStorageMap, getGlobalABIStorageKeys, getGlobalABIStorageMaps, getLocalABIStorageKeys, getLocalABIStorageMaps } from "./packages/abi/src/arc56-contract.mjs";
9
+ import { arc32ToArc56 } from "./app-spec.mjs";
10
+ import { LogicError } from "./logic-error.mjs";
11
+ import { Buffer } from "buffer";
12
+
13
+ //#region src/app-client.ts
14
+ /** The maximum opcode budget for a simulate call as per https://github.com/algorand/go-algorand/blob/807b29a91c371d225e12b9287c5d56e9b33c4e4c/ledger/simulation/trace.go#L104 */
15
+ const MAX_SIMULATE_OPCODE_BUDGET = 2e4 * 16;
16
+ const BYTE_CBLOCK = 38;
17
+ const INT_CBLOCK = 32;
18
+ /**
19
+ * Get the offset of the last constant block at the beginning of the program
20
+ * This value is used to calculate the program counter for an ARC56 program that has a pcOffsetMethod of "cblocks"
21
+ *
22
+ * @param program The program to parse
23
+ * @returns The PC value of the opcode after the last constant block
24
+ */
25
+ function getConstantBlockOffset(program) {
26
+ const bytes = [...program];
27
+ const programSize = bytes.length;
28
+ bytes.shift();
29
+ /** The PC of the opcode after the bytecblock */
30
+ let bytecblockOffset;
31
+ /** The PC of the opcode after the intcblock */
32
+ let intcblockOffset;
33
+ while (bytes.length > 0) {
34
+ /** The current byte from the beginning of the byte array */
35
+ const byte = bytes.shift();
36
+ if (byte === BYTE_CBLOCK || byte === INT_CBLOCK) {
37
+ const isBytecblock = byte === BYTE_CBLOCK;
38
+ /** The byte following the opcode is the number of values in the constant block */
39
+ const valuesRemaining = bytes.shift();
40
+ for (let i = 0; i < valuesRemaining; i++) if (isBytecblock) {
41
+ /** The byte following the opcode is the length of the next element */
42
+ const length = bytes.shift();
43
+ bytes.splice(0, length);
44
+ } else while ((bytes.shift() & 128) !== 0);
45
+ if (isBytecblock) bytecblockOffset = programSize - bytes.length - 1;
46
+ else intcblockOffset = programSize - bytes.length - 1;
47
+ if (bytes[0] !== BYTE_CBLOCK && bytes[0] !== INT_CBLOCK) break;
48
+ }
49
+ }
50
+ return Math.max(bytecblockOffset ?? 0, intcblockOffset ?? 0);
51
+ }
52
+ /** ARC-56/ARC-32 application client that allows you to manage calls and
53
+ * state for a specific deployed instance of an app (with a known app ID). */
54
+ var AppClient = class AppClient {
55
+ _appId;
56
+ _appAddress;
57
+ _appName;
58
+ _appSpec;
59
+ _algorand;
60
+ _defaultSender;
61
+ _defaultSigner;
62
+ _approvalSourceMap;
63
+ _clearSourceMap;
64
+ _localStateMethods;
65
+ _globalStateMethods;
66
+ _boxStateMethods;
67
+ _paramsMethods;
68
+ _createTransactionsMethods;
69
+ _sendMethods;
70
+ _lastCompiled;
71
+ /**
72
+ * Create a new app client.
73
+ * @param params The parameters to create the app client
74
+ * @returns The `AppClient` instance
75
+ * @example
76
+ * ```typescript
77
+ * const appClient = new AppClient({
78
+ * appId: 12345678n,
79
+ * appSpec: appSpec,
80
+ * algorand: AlgorandClient.mainNet(),
81
+ * })
82
+ */
83
+ constructor(params) {
84
+ this._appId = params.appId;
85
+ this._appAddress = getApplicationAddress(this._appId);
86
+ this._appSpec = AppClient.normaliseAppSpec(params.appSpec);
87
+ this._appName = params.appName ?? this._appSpec.name;
88
+ this._algorand = params.algorand;
89
+ this._algorand.registerErrorTransformer(this.handleCallErrors);
90
+ this._defaultSender = getOptionalAddress(params.defaultSender);
91
+ this._defaultSigner = params.defaultSigner;
92
+ this._lastCompiled = {};
93
+ this._approvalSourceMap = params.approvalSourceMap;
94
+ this._clearSourceMap = params.clearSourceMap;
95
+ this._localStateMethods = (address) => this.getStateMethods(() => this.getLocalState(address), () => getLocalABIStorageKeys(this._appSpec), () => getLocalABIStorageMaps(this._appSpec));
96
+ this._globalStateMethods = this.getStateMethods(() => this.getGlobalState(), () => getGlobalABIStorageKeys(this._appSpec), () => getGlobalABIStorageMaps(this._appSpec));
97
+ this._boxStateMethods = this.getBoxMethods();
98
+ this._paramsMethods = {
99
+ ...this.getMethodCallParamsMethods(),
100
+ bare: this.getBareParamsMethods()
101
+ };
102
+ this._createTransactionsMethods = {
103
+ ...this.getMethodCallCreateTransactionMethods(),
104
+ bare: this.getBareCreateTransactionMethods()
105
+ };
106
+ this._sendMethods = {
107
+ ...this.getMethodCallSendMethods(),
108
+ bare: this.getBareSendMethods()
109
+ };
110
+ }
111
+ /**
112
+ * Clone this app client with different params
113
+ *
114
+ * @param params The params to use for the the cloned app client. Omit a param to keep the original value. Set a param to override the original value. Setting to undefined will clear the original value.
115
+ * @returns A new app client with the altered params
116
+ * @example
117
+ * ```typescript
118
+ * const appClient2 = appClient.clone({ defaultSender: 'NEW_SENDER_ADDRESS' })
119
+ * ```
120
+ */
121
+ clone(params) {
122
+ return new AppClient({
123
+ appId: this._appId,
124
+ appSpec: this._appSpec,
125
+ algorand: this._algorand,
126
+ appName: this._appName,
127
+ defaultSender: this._defaultSender,
128
+ defaultSigner: this._defaultSigner,
129
+ approvalSourceMap: this._approvalSourceMap,
130
+ clearSourceMap: this._clearSourceMap,
131
+ ...params
132
+ });
133
+ }
134
+ /**
135
+ * Returns a new `AppClient` client, resolving the app by creator address and name
136
+ * using AlgoKit app deployment semantics (i.e. looking for the app creation transaction note).
137
+ * @param params The parameters to create the app client
138
+ * @returns The `AppClient` instance
139
+ * @example
140
+ * ```typescript
141
+ * const appClient = await AppClient.fromCreatorAndName({
142
+ * creatorAddress: 'CREATOR_ADDRESS',
143
+ * name: 'APP_NAME',
144
+ * appSpec: appSpec,
145
+ * algorand: AlgorandClient.mainNet(),
146
+ * })
147
+ */
148
+ static async fromCreatorAndName(params) {
149
+ const appSpec = AppClient.normaliseAppSpec(params.appSpec);
150
+ const appMetadata = (params.appLookupCache ?? await params.algorand.appDeployer.getCreatorAppsByName(params.creatorAddress, params.ignoreCache)).apps[params.appName ?? appSpec.name];
151
+ if (!appMetadata) throw new Error(`App not found for creator ${params.creatorAddress} and name ${params.appName ?? appSpec.name}`);
152
+ return new AppClient({
153
+ ...params,
154
+ algorand: params.algorand,
155
+ appId: appMetadata.appId
156
+ });
157
+ }
158
+ /**
159
+ * Returns an `AppClient` instance for the current network based on
160
+ * pre-determined network-specific app IDs specified in the ARC-56 app spec.
161
+ *
162
+ * If no IDs are in the app spec or the network isn't recognised, an error is thrown.
163
+ * @param params The parameters to create the app client
164
+ * @returns The `AppClient` instance
165
+ * @example
166
+ * ```typescript
167
+ * const appClient = await AppClient.fromNetwork({
168
+ * appSpec: appSpec,
169
+ * algorand: AlgorandClient.mainNet(),
170
+ * })
171
+ */
172
+ static async fromNetwork(params) {
173
+ const network = await params.algorand.client.network();
174
+ const appSpec = AppClient.normaliseAppSpec(params.appSpec);
175
+ const networkNames = [network.genesisHash];
176
+ if (network.isLocalNet) networkNames.push("localnet");
177
+ if (network.isTestNet) networkNames.push("testnet");
178
+ if (network.isMainNet) networkNames.push("mainnet");
179
+ const networkIndex = Object.keys(appSpec.networks ?? {}).findIndex((n) => networkNames.includes(n));
180
+ if (networkIndex === -1) throw new Error(`No app ID found for network ${asJson(networkNames)} in the app spec`);
181
+ const appId = BigInt(appSpec.networks[networkIndex].appID);
182
+ return new AppClient({
183
+ ...params,
184
+ appId,
185
+ appSpec
186
+ });
187
+ }
188
+ /**
189
+ * Takes a string or parsed JSON object that could be ARC-32 or ARC-56 format and
190
+ * normalises it into a parsed ARC-56 contract object.
191
+ * @param spec The spec to normalise
192
+ * @returns The normalised ARC-56 contract object
193
+ * @example
194
+ * ```typescript
195
+ * const arc56AppSpec = AppClient.normaliseAppSpec(appSpec)
196
+ * ```
197
+ */
198
+ static normaliseAppSpec(spec) {
199
+ const parsedSpec = typeof spec === "string" ? JSON.parse(spec) : spec;
200
+ return "hints" in parsedSpec ? arc32ToArc56(parsedSpec) : parsedSpec;
201
+ }
202
+ /** The ID of the app instance this client is linked to. */
203
+ get appId() {
204
+ return this._appId;
205
+ }
206
+ /** The app address of the app instance this client is linked to. */
207
+ get appAddress() {
208
+ return this._appAddress;
209
+ }
210
+ /** The name of the app (from the ARC-32 / ARC-56 app spec or override). */
211
+ get appName() {
212
+ return this._appName;
213
+ }
214
+ /** The ARC-56 app spec being used */
215
+ get appSpec() {
216
+ return this._appSpec;
217
+ }
218
+ /** A reference to the underlying `AlgorandClient` this app client is using. */
219
+ get algorand() {
220
+ return this._algorand;
221
+ }
222
+ /** Get parameters to create transactions for the current app.
223
+ *
224
+ * A good mental model for this is that these parameters represent a deferred transaction creation.
225
+ * @example Create a transaction in the future using Algorand Client
226
+ * ```typescript
227
+ * const myMethodCall = appClient.params.call({method: 'my_method', args: [123, 'hello']})
228
+ * // ...
229
+ * await algorand.send.AppMethodCall(myMethodCall)
230
+ * ```
231
+ * @example Define a nested transaction as an ABI argument
232
+ * ```typescript
233
+ * const myMethodCall = appClient.params.call({method: 'my_method', args: [123, 'hello']})
234
+ * await appClient.send.call({method: 'my_method2', args: [myMethodCall]})
235
+ * ```
236
+ */
237
+ get params() {
238
+ return this._paramsMethods;
239
+ }
240
+ /** Create transactions for the current app */
241
+ get createTransaction() {
242
+ return this._createTransactionsMethods;
243
+ }
244
+ /** Send transactions to the current app */
245
+ get send() {
246
+ return this._sendMethods;
247
+ }
248
+ /** Get state (local, global, box) from the current app */
249
+ get state() {
250
+ return {
251
+ local: this._localStateMethods,
252
+ global: this._globalStateMethods,
253
+ box: this._boxStateMethods
254
+ };
255
+ }
256
+ /**
257
+ * Funds Algo into the app account for this app.
258
+ *
259
+ * An alias for `appClient.send.fundAppAccount(params)`.
260
+ * @param params The parameters for the funding transaction
261
+ * @returns The result of the funding
262
+ * @example
263
+ * ```typescript
264
+ * await appClient.fundAppAccount({ amount: algo(1) })
265
+ * ```
266
+ */
267
+ async fundAppAccount(params) {
268
+ return this.send.fundAppAccount(params);
269
+ }
270
+ /**
271
+ * Returns raw global state for the current app.
272
+ * @returns The global state
273
+ * @example
274
+ * ```typescript
275
+ * const globalState = await appClient.getGlobalState()
276
+ * ```
277
+ */
278
+ async getGlobalState() {
279
+ return await this._algorand.app.getGlobalState(this.appId);
280
+ }
281
+ /**
282
+ * Returns raw local state for the given account address.
283
+ * @param address The address of the account to get the local state for
284
+ * @returns The local state
285
+ * @example
286
+ * ```typescript
287
+ * const localState = await appClient.getLocalState('ACCOUNT_ADDRESS')
288
+ * ```
289
+ */
290
+ async getLocalState(address) {
291
+ return await this._algorand.app.getLocalState(this.appId, getAddress(address));
292
+ }
293
+ /**
294
+ * Returns the names of all current boxes for the current app.
295
+ * @returns The names of the boxes
296
+ * @example
297
+ * ```typescript
298
+ * const boxNames = await appClient.getBoxNames()
299
+ * ```
300
+ */
301
+ async getBoxNames() {
302
+ return await this._algorand.app.getBoxNames(this.appId);
303
+ }
304
+ /**
305
+ * Returns the value of the given box for the current app.
306
+ * @param name The identifier of the box to return
307
+ * @returns The current box value as a byte array
308
+ * @example
309
+ * ```typescript
310
+ * const boxValue = await appClient.getBoxValue('boxName')
311
+ * ```
312
+ */
313
+ async getBoxValue(name) {
314
+ return await this._algorand.app.getBoxValue(this.appId, name);
315
+ }
316
+ /**
317
+ * Returns the value of the given box for the current app.
318
+ * @param name The identifier of the box to return
319
+ * @param type
320
+ * @returns The current box value as a byte array
321
+ * @example
322
+ * ```typescript
323
+ * const boxValue = await appClient.getBoxValueFromABIType('boxName', new ABIUintType(32))
324
+ * ```
325
+ */
326
+ async getBoxValueFromABIType(name, type) {
327
+ return await this._algorand.app.getBoxValueFromABIType({
328
+ appId: this.appId,
329
+ boxName: name,
330
+ type
331
+ });
332
+ }
333
+ /**
334
+ * Returns the values of all current boxes for the current app.
335
+ * Note: This will issue multiple HTTP requests (one per box) and it's not an atomic operation so values may be out of sync.
336
+ * @param filter Optional filter to filter which boxes' values are returned
337
+ * @returns The (name, value) pair of the boxes with values as raw byte arrays
338
+ * @example
339
+ * ```typescript
340
+ * const boxValues = await appClient.getBoxValues()
341
+ * ```
342
+ */
343
+ async getBoxValues(filter) {
344
+ const names = (await this.getBoxNames()).filter(filter ?? ((_) => true));
345
+ const values = await this._algorand.app.getBoxValues(this.appId, names.map((name) => name.nameRaw));
346
+ return names.map((name, i) => ({
347
+ name,
348
+ value: values[i]
349
+ }));
350
+ }
351
+ /**
352
+ * Returns the values of all current boxes for the current app decoded using an ABI Type.
353
+ * Note: This will issue multiple HTTP requests (one per box) and it's not an atomic operation so values may be out of sync.
354
+ * @param type The ABI type to decode the values with
355
+ * @param filter Optional filter to filter which boxes' values are returned
356
+ * @returns The (name, value) pair of the boxes with values as the ABI Value
357
+ * @example
358
+ * ```typescript
359
+ * const boxValues = await appClient.getBoxValuesFromABIType(new ABIUintType(32))
360
+ * ```
361
+ */
362
+ async getBoxValuesFromABIType(type, filter) {
363
+ const names = (await this.getBoxNames()).filter(filter ?? ((_) => true));
364
+ const values = await this._algorand.app.getBoxValuesFromABIType({
365
+ appId: this.appId,
366
+ boxNames: names.map((name) => name.nameRaw),
367
+ type
368
+ });
369
+ return names.map((name, i) => ({
370
+ name,
371
+ value: values[i]
372
+ }));
373
+ }
374
+ /**
375
+ * Takes an error that may include a logic error from a call to the current app and re-exposes the
376
+ * error to include source code information via the source map and ARC-56 spec.
377
+ * @param e The error to parse
378
+ * @param isClearStateProgram Whether or not the code was running the clear state program (defaults to approval program)
379
+ * @returns The new error, or if there was no logic error or source map then the wrapped error with source details
380
+ */
381
+ async exposeLogicError(e, isClearStateProgram) {
382
+ const pcOffsetMethod = this._appSpec.sourceInfo?.[isClearStateProgram ? "clear" : "approval"]?.pcOffsetMethod;
383
+ let program;
384
+ if (pcOffsetMethod === "cblocks") {
385
+ const appInfo = await this._algorand.app.getById(this.appId);
386
+ program = isClearStateProgram ? appInfo.clearStateProgram : appInfo.approvalProgram;
387
+ }
388
+ return AppClient.exposeLogicError(e, this._appSpec, {
389
+ isClearStateProgram,
390
+ approvalSourceMap: this._approvalSourceMap,
391
+ clearSourceMap: this._clearSourceMap,
392
+ program
393
+ });
394
+ }
395
+ /**
396
+ * Export the current source maps for the app.
397
+ * @returns The source maps
398
+ */
399
+ exportSourceMaps() {
400
+ if (!this._approvalSourceMap || !this._clearSourceMap) 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");
401
+ return {
402
+ approvalSourceMap: this._approvalSourceMap,
403
+ clearSourceMap: this._clearSourceMap
404
+ };
405
+ }
406
+ /**
407
+ * Import source maps for the app.
408
+ * @param sourceMaps The source maps to import
409
+ */
410
+ importSourceMaps(sourceMaps) {
411
+ this._approvalSourceMap = new ProgramSourceMap(sourceMaps.approvalSourceMap);
412
+ this._clearSourceMap = new ProgramSourceMap(sourceMaps.clearSourceMap);
413
+ }
414
+ /**
415
+ * Returns the ABI Method spec for the given method string for the app represented by this application client instance
416
+ * @param methodNameOrSignature The method name or method signature to call if an ABI call is being emitted.
417
+ * e.g. `my_method` or `my_method(unit64,string)bytes`
418
+ * @returns A tuple with: [ARC-56 `Method`, algosdk `ABIMethod`]
419
+ */
420
+ getABIMethod(methodNameOrSignature) {
421
+ return getABIMethod(methodNameOrSignature, this._appSpec);
422
+ }
423
+ /**
424
+ * Checks for decode errors on the SendAppTransactionResult and maps the return value to the specified type
425
+ * on the ARC-56 method, replacing the `return` property with the decoded type.
426
+ *
427
+ * If the return type is an ARC-56 struct then the struct will be returned.
428
+ *
429
+ * @param result The SendAppTransactionResult to be mapped
430
+ * @param method The method that was called
431
+ * @returns The smart contract response with an updated return value
432
+ */
433
+ async processMethodCallReturn(result) {
434
+ const resultValue = await result;
435
+ return {
436
+ ...resultValue,
437
+ return: resultValue.return?.returnValue
438
+ };
439
+ }
440
+ /**
441
+ * Compiles the approval and clear state programs (if TEAL templates provided),
442
+ * performing any provided deploy-time parameter replacement and stores
443
+ * the source maps.
444
+ *
445
+ * If no TEAL templates provided it will use any byte code provided in the app spec.
446
+ *
447
+ * Will store any generated source maps for later use in debugging.
448
+ * @param compilation Any compilation parameters to use
449
+ * @returns The compiled code and any compilation results (including source maps)
450
+ */
451
+ async compile(compilation) {
452
+ const result = await AppClient.compile(this._appSpec, this._algorand.app, compilation);
453
+ if (result.compiledApproval) {
454
+ this._approvalSourceMap = result.compiledApproval.sourceMap;
455
+ this._lastCompiled.approval = result.compiledApproval.compiledBase64ToBytes;
456
+ }
457
+ if (result.compiledClear) {
458
+ this._clearSourceMap = result.compiledClear.sourceMap;
459
+ this._lastCompiled.clear = result.compiledClear.compiledBase64ToBytes;
460
+ }
461
+ return result;
462
+ }
463
+ /**
464
+ * Takes an error that may include a logic error from a call to the current app and re-exposes the
465
+ * error to include source code information via the source map and ARC-56 spec.
466
+ * @param e The error to parse
467
+ * @param appSpec The app spec for the app
468
+ * @param details Additional information to inform the error
469
+ * @returns The new error, or if there was no logic error or source map then the wrapped error with source details
470
+ */
471
+ static exposeLogicError(e, appSpec, details) {
472
+ const { isClearStateProgram, approvalSourceMap, clearSourceMap, program } = details;
473
+ const sourceMap = isClearStateProgram ? clearSourceMap : approvalSourceMap;
474
+ const errorDetails = LogicError.parseLogicError(e);
475
+ if (errorDetails === void 0 || errorDetails?.pc === void 0) return e;
476
+ /** The PC value to find in the ARC56 SourceInfo */
477
+ let arc56Pc = errorDetails?.pc;
478
+ const programSourceInfo = isClearStateProgram ? appSpec.sourceInfo?.clear : appSpec.sourceInfo?.approval;
479
+ /** The offset to apply to the PC if using the cblocks pc offset method */
480
+ let cblocksOffset = 0;
481
+ if (programSourceInfo?.pcOffsetMethod === "cblocks") {
482
+ if (program === void 0) throw new Error("Program bytes are required to calculate the ARC56 cblocks PC offset");
483
+ cblocksOffset = getConstantBlockOffset(program);
484
+ arc56Pc = errorDetails.pc - cblocksOffset;
485
+ }
486
+ const errorMessage = (programSourceInfo?.sourceInfo.find((s) => s.pc.includes(arc56Pc)))?.errorMessage;
487
+ if (appSpec.source) {
488
+ let getLineForPc = (inputPc) => sourceMap?.getLocationForPc?.(inputPc)?.line;
489
+ if (sourceMap === void 0) getLineForPc = (inputPc) => {
490
+ const teal = programSourceInfo?.sourceInfo.find((s) => s.pc.includes(inputPc - cblocksOffset))?.teal;
491
+ if (teal === void 0) return void 0;
492
+ return teal - 1;
493
+ };
494
+ e = new LogicError(errorDetails, Buffer.from(isClearStateProgram ? appSpec.source.clear : appSpec.source.approval, "base64").toString().split("\n"), getLineForPc);
495
+ }
496
+ if (errorMessage) {
497
+ const appId = asJson(e).match(/(?<=app=)\d+/)?.[0] || "";
498
+ const txId = asJson(e).match(/(?<=transaction )\S+(?=:)/)?.[0];
499
+ const error = /* @__PURE__ */ new Error(`Runtime error when executing ${appSpec.name} (appId: ${appId}) in transaction ${txId}: ${errorMessage}`);
500
+ error.cause = e;
501
+ return error;
502
+ }
503
+ return e;
504
+ }
505
+ /**
506
+ * Compiles the approval and clear state programs (if TEAL templates provided),
507
+ * performing any provided deploy-time parameter replacement and returns
508
+ * the compiled code and any compilation results (including source maps).
509
+ *
510
+ * If no TEAL templates provided it will use any byte code provided in the app spec.
511
+ *
512
+ * Will store any generated source maps for later use in debugging.
513
+ * @param appSpec The app spec for the app
514
+ * @param appManager The app manager to use for compilation
515
+ * @param compilation Any compilation parameters to use
516
+ * @returns The compiled code and any compilation results (including source maps)
517
+ */
518
+ static async compile(appSpec, appManager, compilation) {
519
+ const { deployTimeParams, updatable, deletable } = compilation ?? {};
520
+ if (!appSpec.source) {
521
+ if (!appSpec.byteCode?.approval || !appSpec.byteCode?.clear) throw new Error(`Attempt to compile app ${appSpec.name} without source or byteCode`);
522
+ return {
523
+ approvalProgram: Buffer.from(appSpec.byteCode.approval, "base64"),
524
+ clearStateProgram: Buffer.from(appSpec.byteCode.clear, "base64")
525
+ };
526
+ }
527
+ const approvalTemplate = Buffer.from(appSpec.source.approval, "base64").toString("utf-8");
528
+ const compiledApproval = await appManager.compileTealTemplate(approvalTemplate, deployTimeParams, {
529
+ updatable,
530
+ deletable
531
+ });
532
+ const clearTemplate = Buffer.from(appSpec.source.clear, "base64").toString("utf-8");
533
+ const compiledClear = await appManager.compileTealTemplate(clearTemplate, deployTimeParams);
534
+ if (Config.debug) await Config.events.emitAsync(EventType.AppCompiled, { sources: [{
535
+ compiledTeal: compiledApproval,
536
+ appName: appSpec.name,
537
+ fileName: "approval"
538
+ }, {
539
+ compiledTeal: compiledClear,
540
+ appName: appSpec.name,
541
+ fileName: "clear"
542
+ }] });
543
+ return {
544
+ approvalProgram: compiledApproval.compiledBase64ToBytes,
545
+ compiledApproval,
546
+ clearStateProgram: compiledClear.compiledBase64ToBytes,
547
+ compiledClear
548
+ };
549
+ }
550
+ /**
551
+ * Returns ABI method arguments ready for a method call params object with default values populated
552
+ * and structs replaced with tuples.
553
+ *
554
+ * It does this by replacing any `undefined` values with the equivalent default value from the given ARC-56 app spec.
555
+ * @param methodNameOrSignature The method name or method signature to call if an ABI call is being emitted.
556
+ * e.g. `my_method` or `my_method(unit64,string)bytes`
557
+ * @param args The arguments to the method with `undefined` for any that should be populated with a default value
558
+ */
559
+ async getABIArgsWithDefaultValues(methodNameOrSignature, args, sender) {
560
+ const m = getABIMethod(methodNameOrSignature, this._appSpec);
561
+ return await Promise.all(args?.map(async (arg, i) => {
562
+ const methodArg = m.args[i];
563
+ if (!methodArg) throw new Error(`Unexpected arg at position ${i}. ${m.name} only expects ${m.args.length} args`);
564
+ if (argTypeIsTransaction(methodArg.type)) return arg;
565
+ if (arg !== void 0) return arg;
566
+ const defaultValue = methodArg.defaultValue;
567
+ if (defaultValue) switch (defaultValue.source) {
568
+ case "literal": {
569
+ const bytes = Buffer.from(defaultValue.data, "base64");
570
+ return getABIDecodedValue(defaultValue.type ?? methodArg.type, bytes);
571
+ }
572
+ case "method": {
573
+ const method = this.getABIMethod(defaultValue.data);
574
+ const result = await this.send.call({
575
+ method: defaultValue.data,
576
+ args: method.args.map(() => void 0),
577
+ sender
578
+ });
579
+ if (result.return === void 0) throw new Error("Default value method call did not return a value");
580
+ return result.return;
581
+ }
582
+ case "local":
583
+ case "global":
584
+ case "box": return await this.getDefaultValueFromStorage({
585
+ data: defaultValue.data,
586
+ source: defaultValue.source
587
+ }, methodArg.name ?? `arg${i + 1}`, sender);
588
+ }
589
+ }) ?? []);
590
+ }
591
+ async getDefaultValueFromStorage(defaultValue, argName, sender) {
592
+ const keys = defaultValue.source === "box" ? getBoxABIStorageKeys(this.appSpec) : defaultValue.source === "global" ? getGlobalABIStorageKeys(this.appSpec) : getLocalABIStorageKeys(this.appSpec);
593
+ const key = Object.values(keys).find((s) => s.key === defaultValue.data);
594
+ if (!key) throw new Error(`Unable to find default value for argument '${argName}': The storage key (base64: '${defaultValue.data}') is not defined in the contract's ${defaultValue.source} storage schema`);
595
+ if (defaultValue.source === "box") {
596
+ const value$1 = await this.getBoxValue(Buffer.from(defaultValue.data, "base64"));
597
+ return getABIDecodedValue(key.valueType, value$1);
598
+ }
599
+ const state = defaultValue.source === "global" ? await this.getGlobalState() : await this.getLocalState(sender);
600
+ const value = Object.values(state).find((s) => s.keyBase64 === defaultValue.data);
601
+ if (!value) throw new Error(`Unable to find default value for argument '${argName}': No value exists in ${defaultValue.source} storage for key (base64: '${defaultValue.data}')`);
602
+ return "valueRaw" in value ? getABIDecodedValue(key.valueType, value.valueRaw) : value.value;
603
+ }
604
+ getBareParamsMethods() {
605
+ return {
606
+ update: async (params) => {
607
+ return this.getBareParams({
608
+ ...params,
609
+ ...await this.compile(params)
610
+ }, OnApplicationComplete.UpdateApplication);
611
+ },
612
+ optIn: (params) => {
613
+ return this.getBareParams(params, OnApplicationComplete.OptIn);
614
+ },
615
+ delete: (params) => {
616
+ return this.getBareParams(params, OnApplicationComplete.DeleteApplication);
617
+ },
618
+ clearState: (params) => {
619
+ return this.getBareParams(params, OnApplicationComplete.ClearState);
620
+ },
621
+ closeOut: (params) => {
622
+ return this.getBareParams(params, OnApplicationComplete.CloseOut);
623
+ },
624
+ call: (params) => {
625
+ return this.getBareParams(params, params?.onComplete ?? OnApplicationComplete.NoOp);
626
+ }
627
+ };
628
+ }
629
+ getBareCreateTransactionMethods() {
630
+ return {
631
+ update: async (params) => {
632
+ return this._algorand.createTransaction.appUpdate(await this.params.bare.update(params));
633
+ },
634
+ optIn: (params) => {
635
+ return this._algorand.createTransaction.appCall(this.params.bare.optIn(params));
636
+ },
637
+ delete: (params) => {
638
+ return this._algorand.createTransaction.appDelete(this.params.bare.delete(params));
639
+ },
640
+ clearState: (params) => {
641
+ return this._algorand.createTransaction.appCall(this.params.bare.clearState(params));
642
+ },
643
+ closeOut: (params) => {
644
+ return this._algorand.createTransaction.appCall(this.params.bare.closeOut(params));
645
+ },
646
+ call: (params) => {
647
+ return this._algorand.createTransaction.appCall(this.params.bare.call(params));
648
+ }
649
+ };
650
+ }
651
+ getBareSendMethods() {
652
+ return {
653
+ update: async (params) => {
654
+ const compiled = await this.compile(params);
655
+ return {
656
+ ...await this._algorand.send.appUpdate(await this.params.bare.update(params)),
657
+ ...compiled
658
+ };
659
+ },
660
+ optIn: (params) => {
661
+ return this._algorand.send.appCall(this.params.bare.optIn(params));
662
+ },
663
+ delete: (params) => {
664
+ return this._algorand.send.appDelete(this.params.bare.delete(params));
665
+ },
666
+ clearState: (params) => {
667
+ return this._algorand.send.appCall(this.params.bare.clearState(params));
668
+ },
669
+ closeOut: (params) => {
670
+ return this._algorand.send.appCall(this.params.bare.closeOut(params));
671
+ },
672
+ call: (params) => {
673
+ return this._algorand.send.appCall(this.params.bare.call(params));
674
+ }
675
+ };
676
+ }
677
+ getMethodCallParamsMethods() {
678
+ return {
679
+ fundAppAccount: (params) => {
680
+ return {
681
+ ...params,
682
+ sender: this.getSender(params.sender),
683
+ signer: this.getSigner(params.sender, params.signer),
684
+ receiver: this.appAddress
685
+ };
686
+ },
687
+ update: async (params) => {
688
+ return await this.getABIParams({
689
+ ...params,
690
+ ...await this.compile(params)
691
+ }, OnApplicationComplete.UpdateApplication);
692
+ },
693
+ optIn: async (params) => {
694
+ return await this.getABIParams(params, OnApplicationComplete.OptIn);
695
+ },
696
+ delete: async (params) => {
697
+ return await this.getABIParams(params, OnApplicationComplete.DeleteApplication);
698
+ },
699
+ closeOut: async (params) => {
700
+ return await this.getABIParams(params, OnApplicationComplete.CloseOut);
701
+ },
702
+ call: async (params) => {
703
+ return await this.getABIParams(params, params.onComplete ?? OnApplicationComplete.NoOp);
704
+ }
705
+ };
706
+ }
707
+ getMethodCallSendMethods() {
708
+ return {
709
+ fundAppAccount: (params) => {
710
+ return this._algorand.send.payment(this.params.fundAppAccount(params));
711
+ },
712
+ update: async (params) => {
713
+ const compiled = await this.compile(params);
714
+ return {
715
+ ...await this.processMethodCallReturn(this._algorand.send.appUpdateMethodCall(await this.params.update({ ...params }))),
716
+ ...compiled
717
+ };
718
+ },
719
+ optIn: async (params) => {
720
+ return this.processMethodCallReturn(this._algorand.send.appCallMethodCall(await this.params.optIn(params)));
721
+ },
722
+ delete: async (params) => {
723
+ return this.processMethodCallReturn(this._algorand.send.appDeleteMethodCall(await this.params.delete(params)));
724
+ },
725
+ closeOut: async (params) => {
726
+ return this.processMethodCallReturn(this._algorand.send.appCallMethodCall(await this.params.closeOut(params)));
727
+ },
728
+ call: async (params) => {
729
+ if ((params.onComplete === OnApplicationComplete.NoOp || !params.onComplete) && getABIMethod(params.method, this._appSpec).readonly) {
730
+ const readonlyParams = { ...params };
731
+ if (params.coverAppCallInnerTransactionFees && params.maxFee) {
732
+ readonlyParams.staticFee = params.maxFee;
733
+ readonlyParams.extraFee = void 0;
734
+ }
735
+ try {
736
+ const result = await this._algorand.newGroup().addAppCallMethodCall(await this.params.call(readonlyParams)).simulate({
737
+ allowUnnamedResources: params.populateAppCallResources ?? true,
738
+ skipSignatures: true,
739
+ extraOpcodeBudget: MAX_SIMULATE_OPCODE_BUDGET
740
+ });
741
+ return this.processMethodCallReturn({
742
+ ...result,
743
+ transaction: result.transactions.at(-1),
744
+ confirmation: result.confirmations.at(-1),
745
+ return: result.returns && result.returns.length > 0 ? result.returns.at(-1) : void 0
746
+ });
747
+ } catch (e) {
748
+ const error = e;
749
+ if (params.coverAppCallInnerTransactionFees && error && error.message && error.message.match(/fee too small/)) throw Error(`Fees were too small. You may need to increase the transaction maxFee.`);
750
+ throw e;
751
+ }
752
+ }
753
+ return this.processMethodCallReturn(this._algorand.send.appCallMethodCall(await this.params.call(params)));
754
+ }
755
+ };
756
+ }
757
+ getMethodCallCreateTransactionMethods() {
758
+ return {
759
+ fundAppAccount: (params) => {
760
+ return this._algorand.createTransaction.payment(this.params.fundAppAccount(params));
761
+ },
762
+ update: async (params) => {
763
+ return this._algorand.createTransaction.appUpdateMethodCall(await this.params.update(params));
764
+ },
765
+ optIn: async (params) => {
766
+ return this._algorand.createTransaction.appCallMethodCall(await this.params.optIn(params));
767
+ },
768
+ delete: async (params) => {
769
+ return this._algorand.createTransaction.appDeleteMethodCall(await this.params.delete(params));
770
+ },
771
+ closeOut: async (params) => {
772
+ return this._algorand.createTransaction.appCallMethodCall(await this.params.closeOut(params));
773
+ },
774
+ call: async (params) => {
775
+ return this._algorand.createTransaction.appCallMethodCall(await this.params.call(params));
776
+ }
777
+ };
778
+ }
779
+ /** Returns the sender for a call, using the provided sender or using the `defaultSender`
780
+ * if none provided and throws an error if neither provided */
781
+ getSender(sender) {
782
+ if (!sender && !this._defaultSender) throw new Error(`No sender provided and no default sender present in app client for call to app ${this._appName}`);
783
+ return getAddress(sender ?? this._defaultSender);
784
+ }
785
+ /** Returns the signer for a call, using the provided signer or the `defaultSigner`
786
+ * if no signer was provided and the sender resolves to the default sender, the call will use default signer
787
+ * or `undefined` otherwise (so the signer is resolved from `AlgorandClient`) */
788
+ getSigner(sender, signer) {
789
+ return signer ?? (!sender || sender === this._defaultSender ? this._defaultSigner : void 0);
790
+ }
791
+ getBareParams(params, onComplete) {
792
+ return {
793
+ ...params,
794
+ appId: this._appId,
795
+ sender: this.getSender(params?.sender),
796
+ signer: this.getSigner(params?.sender, params?.signer),
797
+ onComplete
798
+ };
799
+ }
800
+ async getABIParams(params, onComplete) {
801
+ const sender = this.getSender(params.sender);
802
+ const method = getABIMethod(params.method, this._appSpec);
803
+ const args = await this.getABIArgsWithDefaultValues(params.method, params.args, sender);
804
+ return {
805
+ ...params,
806
+ appId: this._appId,
807
+ sender,
808
+ signer: this.getSigner(params.sender, params.signer),
809
+ method,
810
+ onComplete,
811
+ args
812
+ };
813
+ }
814
+ /** Make the given call and catch any errors, augmenting with debugging information before re-throwing. */
815
+ handleCallErrors = async (e) => {
816
+ if (this.appId === 0n) {
817
+ if (e.sentTransactions === void 0) return e;
818
+ const txn = e.sentTransactions.find((t) => e.message.includes(t.txId()));
819
+ const programsDefinedAndEqual = (a, b) => {
820
+ if (a === void 0 || b === void 0) return false;
821
+ if (a.length !== b.length) return false;
822
+ for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
823
+ return true;
824
+ };
825
+ if (!programsDefinedAndEqual(txn?.appCall?.clearStateProgram, this._lastCompiled.clear) || !programsDefinedAndEqual(txn?.appCall?.approvalProgram, this._lastCompiled?.approval)) return e;
826
+ } else {
827
+ const appIdString = `app=${this._appId.toString()}`;
828
+ if (!e.message.includes(appIdString)) return e;
829
+ }
830
+ const logicError = await this.exposeLogicError(e);
831
+ if (logicError instanceof LogicError) {
832
+ let currentLine = logicError.teal_line - logicError.lines - 1;
833
+ const stackWithLines = logicError.stack?.split("\n").map((line) => `${currentLine += 1}: ${line}`).join("\n");
834
+ Config.logger.error(`${logicError.message}\n\n${stackWithLines}`);
835
+ }
836
+ return logicError;
837
+ };
838
+ getBoxMethods() {
839
+ const that = this;
840
+ const stateMethods = {
841
+ getAll: async () => {
842
+ return Object.fromEntries(await Promise.all(Object.keys(that._appSpec.state.keys.box).map(async (key) => [key, await stateMethods.getValue(key)])));
843
+ },
844
+ getValue: async (name) => {
845
+ const metadata = getBoxABIStorageKey(that._appSpec, name);
846
+ const value = await that.getBoxValue(Buffer.from(metadata.key, "base64"));
847
+ return getABIDecodedValue(metadata.valueType, value);
848
+ },
849
+ getMapValue: async (mapName, key) => {
850
+ const metadata = getBoxABIStorageMap(that._appSpec, mapName);
851
+ const prefix = Buffer.from(metadata.prefix ?? "", "base64");
852
+ const encodedKey = Buffer.concat([prefix, getABIEncodedValue(metadata.keyType, key)]);
853
+ const base64Key = Buffer.from(encodedKey).toString("base64");
854
+ const value = await that.getBoxValue(Buffer.from(base64Key, "base64"));
855
+ return getABIDecodedValue(metadata.valueType, value);
856
+ },
857
+ getMap: async (mapName) => {
858
+ const metadata = getBoxABIStorageMap(that._appSpec, mapName);
859
+ const prefix = Buffer.from(metadata.prefix ?? "", "base64");
860
+ const boxNames = await that.getBoxNames();
861
+ return new Map(await Promise.all(boxNames.filter((b) => binaryStartsWith(b.nameRaw, prefix)).map(async (b) => {
862
+ return [getABIDecodedValue(metadata.keyType, b.nameRaw.slice(prefix.length)), getABIDecodedValue(metadata.valueType, await that.getBoxValue(b.nameRaw))];
863
+ })));
864
+ }
865
+ };
866
+ return stateMethods;
867
+ }
868
+ getStateMethods(stateGetter, keyGetter, mapGetter) {
869
+ const stateMethods = {
870
+ getAll: async () => {
871
+ const appState = await stateGetter();
872
+ return Object.fromEntries(await Promise.all(Object.keys(keyGetter()).map(async (key) => [key, await stateMethods.getValue(key, appState)])));
873
+ },
874
+ getValue: async (name, appState) => {
875
+ const state = Object.values(appState ?? await stateGetter());
876
+ const metadata = keyGetter()[name];
877
+ if (metadata === void 0) throw new Error(`Attempted to get state value ${name}, but it does not exist`);
878
+ const value = state.find((s) => s.keyBase64 === metadata.key);
879
+ if (value && "valueRaw" in value) return getABIDecodedValue(metadata.valueType, value.valueRaw);
880
+ return value?.value;
881
+ },
882
+ getMapValue: async (mapName, key, appState) => {
883
+ const state = Object.values(appState ?? await stateGetter());
884
+ const metadata = mapGetter()[mapName];
885
+ const prefix = Buffer.from(metadata.prefix ?? "", "base64");
886
+ const encodedKey = Buffer.concat([prefix, getABIEncodedValue(metadata.keyType, key)]);
887
+ const base64Key = Buffer.from(encodedKey).toString("base64");
888
+ const value = state.find((s) => s.keyBase64 === base64Key);
889
+ if (value && "valueRaw" in value) return getABIDecodedValue(metadata.valueType, value.valueRaw);
890
+ return value?.value;
891
+ },
892
+ getMap: async (mapName) => {
893
+ const state = Object.values(await stateGetter());
894
+ const metadata = mapGetter()[mapName];
895
+ const prefix = Buffer.from(metadata.prefix ?? "", "base64");
896
+ return new Map(state.filter((s) => binaryStartsWith(s.keyRaw, prefix)).map((s) => {
897
+ const key = s.keyRaw.slice(prefix.length);
898
+ return [getABIDecodedValue(metadata.keyType, key), "valueRaw" in s ? getABIDecodedValue(metadata.valueType, s.valueRaw) : s.value];
899
+ }));
900
+ }
901
+ };
902
+ return stateMethods;
903
+ }
904
+ };
905
+
906
+ //#endregion
907
+ export { AppClient };
908
+ //# sourceMappingURL=app-client.mjs.map