@algorandfoundation/algorand-typescript-testing 1.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/abi-metadata.d.ts +16 -0
  2. package/constants.d.ts +21 -0
  3. package/context-helpers/internal-context.d.ts +26 -0
  4. package/decode-logs.d.ts +8 -0
  5. package/errors.d.ts +5 -0
  6. package/impl/account.d.ts +31 -0
  7. package/impl/acct-params.d.ts +5 -0
  8. package/impl/app-params.d.ts +3 -0
  9. package/impl/application.d.ts +24 -0
  10. package/impl/asset-holding.d.ts +2 -0
  11. package/impl/asset-params.d.ts +3 -0
  12. package/impl/asset.d.ts +23 -0
  13. package/impl/crypto.d.ts +12 -0
  14. package/impl/global.d.ts +18 -0
  15. package/impl/gtxn.d.ts +2 -0
  16. package/impl/index.d.ts +11 -0
  17. package/impl/inner-transactions.d.ts +51 -0
  18. package/impl/itxn.d.ts +8 -0
  19. package/impl/pure.d.ts +32 -0
  20. package/impl/scratch.d.ts +2 -0
  21. package/impl/transactions.d.ts +139 -0
  22. package/impl/txn.d.ts +3 -0
  23. package/index.d.ts +1 -0
  24. package/index.mjs +3687 -0
  25. package/index.mjs.map +1 -0
  26. package/package.json +41 -0
  27. package/runtime-helpers-B6YRp95T.js +460 -0
  28. package/runtime-helpers-B6YRp95T.js.map +1 -0
  29. package/runtime-helpers.d.ts +9 -0
  30. package/runtime-helpers.mjs +4 -0
  31. package/runtime-helpers.mjs.map +1 -0
  32. package/subcontexts/contract-context.d.ts +10 -0
  33. package/subcontexts/ledger-context.d.ts +27 -0
  34. package/subcontexts/transaction-context.d.ts +57 -0
  35. package/test-execution-context.d.ts +45 -0
  36. package/test-transformer/errors.d.ts +3 -0
  37. package/test-transformer/helpers.d.ts +2 -0
  38. package/test-transformer/index.d.ts +6 -0
  39. package/test-transformer/node-factory.d.ts +10 -0
  40. package/test-transformer/supported-binary-op-string.d.ts +3 -0
  41. package/test-transformer/visitors.d.ts +11 -0
  42. package/test-transformer.mjs +297 -0
  43. package/test-transformer.mjs.map +1 -0
  44. package/typescript-helpers.d.ts +9 -0
  45. package/util.d.ts +26 -0
  46. package/value-generators/avm.d.ts +23 -0
  47. package/value-generators/index.d.ts +6 -0
  48. package/value-generators/txn.d.ts +10 -0
package/index.mjs ADDED
@@ -0,0 +1,3687 @@
1
+ import { internal, Bytes, Uint64, Account, arc4, op, TransactionType, Asset, Application } from '@algorandfoundation/algorand-typescript';
2
+ import algosdk from 'algosdk';
3
+ import { a as asMaybeUint64Cls, b as asUint64, c as asBigInt, d as asBytesCls, e as asBytes, L as LOGIC_DATA_PREFIX, g as getObjectReference, M as MIN_TXN_FEE, D as DEFAULT_ACCOUNT_MIN_BALANCE, f as DEFAULT_MAX_TXN_LIFE, Z as ZERO_ADDRESS, h as DEFAULT_ASSET_CREATE_MIN_BALANCE, i as DEFAULT_ASSET_OPT_IN_MIN_BALANCE, j as DEFAULT_GLOBAL_GENESIS_HASH, k as asUint64Cls, l as asMaybeBytesCls, m as asBigUint, B as BITS_IN_BYTE, n as MAX_BYTES_SIZE, o as MAX_UINT8, p as binaryStringToBytes, U as UINT64_SIZE, q as MAX_UINT64, r as asNumber, A as ALWAYS_APPROVE_TEAL_PROGRAM, s as combineIntoMaxBytePages, t as MAX_ITEMS_IN_LOG, u as getRandomBytes, v as getAbiMetadata, w as hasAbiMetadata, x as getGenericTypeInfo, y as extractGenericTypeArgs, z as iterBigInt, C as toBytes, E as getRandomBigInt, F as captureMethodConfig } from './runtime-helpers-B6YRp95T.js';
4
+ import { ec } from 'elliptic';
5
+ import { sha256 as sha256$1 } from 'js-sha256';
6
+ import { sha3_256 as sha3_256$1, keccak256 as keccak256$1 } from 'js-sha3';
7
+ import { sha512_256 as sha512_256$1 } from 'js-sha512';
8
+ import nacl from 'tweetnacl';
9
+ import { randomBytes } from 'crypto';
10
+
11
+ /**
12
+ * For accessing implementation specific functions, with a convenient single entry
13
+ * point for other modules to import Also allows for a single place to check and
14
+ * provide.
15
+ */
16
+ class InternalContext {
17
+ get value() {
18
+ return internal.ctxMgr.instance;
19
+ }
20
+ get defaultSender() {
21
+ return this.value.defaultSender;
22
+ }
23
+ get ledger() {
24
+ return this.value.ledger;
25
+ }
26
+ get txn() {
27
+ return this.value.txn;
28
+ }
29
+ get contract() {
30
+ return this.value.contract;
31
+ }
32
+ get any() {
33
+ return this.value.any;
34
+ }
35
+ get activeApplication() {
36
+ return this.ledger.getApplication(this.activeGroup.activeApplicationId);
37
+ }
38
+ get activeGroup() {
39
+ return this.value.txn.activeGroup;
40
+ }
41
+ getAccountData(accountPublicKey) {
42
+ const key = internal.primitives.BytesCls.fromCompat(accountPublicKey);
43
+ const data = this.ledger.accountDataMap.get(key.toString());
44
+ if (!data) {
45
+ throw internal.errors.internalError('Unknown account, check correct testing context is active');
46
+ }
47
+ return data;
48
+ }
49
+ getAssetData(id) {
50
+ const key = internal.primitives.Uint64Cls.fromCompat(id);
51
+ const data = this.ledger.assetDataMap.get(key.asBigInt());
52
+ if (!data) {
53
+ throw internal.errors.internalError('Unknown asset, check correct testing context is active');
54
+ }
55
+ return data;
56
+ }
57
+ getApplicationData(id) {
58
+ const key = internal.primitives.Uint64Cls.fromCompat(id);
59
+ const data = this.ledger.applicationDataMap.get(key.asBigInt());
60
+ if (!data) {
61
+ throw internal.errors.internalError('Unknown application, check correct testing context is active');
62
+ }
63
+ return data;
64
+ }
65
+ }
66
+ const lazyContext = new InternalContext();
67
+
68
+ const getAccount = (acct) => {
69
+ const acctId = asMaybeUint64Cls(acct);
70
+ if (acctId !== undefined) {
71
+ const activeTxn = lazyContext.activeGroup.activeTransaction;
72
+ return activeTxn.accounts(acctId.asAlgoTs());
73
+ }
74
+ return acct;
75
+ };
76
+ const balance = (a) => {
77
+ const acct = getAccount(a);
78
+ return acct.balance;
79
+ };
80
+ const minBalance = (a) => {
81
+ const acct = getAccount(a);
82
+ return acct.minBalance;
83
+ };
84
+ const AcctParams = {
85
+ acctBalance: function (a) {
86
+ const acct = getAccount(a);
87
+ return [acct.balance, acct.balance !== 0];
88
+ },
89
+ acctMinBalance: function (a) {
90
+ const acct = getAccount(a);
91
+ return [acct.minBalance, acct.balance !== 0];
92
+ },
93
+ acctAuthAddr: function (a) {
94
+ const acct = getAccount(a);
95
+ return [acct.authAddress, acct.balance !== 0];
96
+ },
97
+ acctTotalNumUint: function (a) {
98
+ const acct = getAccount(a);
99
+ return [acct.totalNumUint, acct.balance !== 0];
100
+ },
101
+ acctTotalNumByteSlice: function (a) {
102
+ const acct = getAccount(a);
103
+ return [acct.totalNumByteSlice, acct.balance !== 0];
104
+ },
105
+ acctTotalExtraAppPages: function (a) {
106
+ const acct = getAccount(a);
107
+ return [acct.totalExtraAppPages, acct.balance !== 0];
108
+ },
109
+ acctTotalAppsCreated: function (a) {
110
+ const acct = getAccount(a);
111
+ return [acct.totalAppsCreated, acct.balance !== 0];
112
+ },
113
+ acctTotalAppsOptedIn: function (a) {
114
+ const acct = getAccount(a);
115
+ return [acct.totalAppsOptedIn, acct.balance !== 0];
116
+ },
117
+ acctTotalAssetsCreated: function (a) {
118
+ const acct = getAccount(a);
119
+ return [acct.totalAssetsCreated, acct.balance !== 0];
120
+ },
121
+ acctTotalAssets: function (a) {
122
+ const acct = getAccount(a);
123
+ return [acct.totalAssets, acct.balance !== 0];
124
+ },
125
+ acctTotalBoxes: function (a) {
126
+ const acct = getAccount(a);
127
+ return [acct.totalBoxes, acct.balance !== 0];
128
+ },
129
+ acctTotalBoxBytes: function (a) {
130
+ const acct = getAccount(a);
131
+ return [acct.totalBoxBytes, acct.balance !== 0];
132
+ },
133
+ };
134
+
135
+ const resolveAppIndex = (appIdOrIndex) => {
136
+ const input = asUint64(appIdOrIndex);
137
+ if (input >= 1001) {
138
+ return input;
139
+ }
140
+ const txn = lazyContext.activeGroup.activeTransaction;
141
+ return txn.apps(input).id;
142
+ };
143
+ const getApp = (app) => {
144
+ try {
145
+ const appId = asMaybeUint64Cls(app);
146
+ if (appId !== undefined) {
147
+ return lazyContext.ledger.getApplication(resolveAppIndex(appId));
148
+ }
149
+ return app;
150
+ }
151
+ catch {
152
+ return undefined;
153
+ }
154
+ };
155
+ const AppParams = {
156
+ appApprovalProgram: function (a) {
157
+ const app = getApp(a);
158
+ return app === undefined ? [Bytes(), false] : [app.approvalProgram, true];
159
+ },
160
+ appClearStateProgram: function (a) {
161
+ const app = getApp(a);
162
+ return app === undefined ? [Bytes(), false] : [app.clearStateProgram, true];
163
+ },
164
+ appGlobalNumUint: function (a) {
165
+ const app = getApp(a);
166
+ return app === undefined ? [Uint64(0), false] : [app.globalNumUint, true];
167
+ },
168
+ appGlobalNumByteSlice: function (a) {
169
+ const app = getApp(a);
170
+ return app === undefined ? [Uint64(0), false] : [app.globalNumBytes, true];
171
+ },
172
+ appLocalNumUint: function (a) {
173
+ const app = getApp(a);
174
+ return app === undefined ? [Uint64(0), false] : [app.localNumUint, true];
175
+ },
176
+ appLocalNumByteSlice: function (a) {
177
+ const app = getApp(a);
178
+ return app === undefined ? [Uint64(0), false] : [app.localNumBytes, true];
179
+ },
180
+ appExtraProgramPages: function (a) {
181
+ const app = getApp(a);
182
+ return app === undefined ? [Uint64(0), false] : [app.extraProgramPages, true];
183
+ },
184
+ appCreator: function (a) {
185
+ const app = getApp(a);
186
+ return app === undefined ? [Account(), false] : [app.creator, true];
187
+ },
188
+ appAddress: function (a) {
189
+ const app = getApp(a);
190
+ return app === undefined ? [Account(), false] : [app.address, true];
191
+ },
192
+ };
193
+
194
+ const resolveAssetIndex = (assetIdOrIndex) => {
195
+ const input = asUint64(assetIdOrIndex);
196
+ if (input >= 1001) {
197
+ return input;
198
+ }
199
+ const txn = lazyContext.activeGroup.activeTransaction;
200
+ return txn.assets(input).id;
201
+ };
202
+ const getAsset = (asset) => {
203
+ try {
204
+ const assetId = asMaybeUint64Cls(asset);
205
+ if (assetId !== undefined) {
206
+ return lazyContext.ledger.getAsset(resolveAssetIndex(assetId));
207
+ }
208
+ return asset;
209
+ }
210
+ catch {
211
+ return undefined;
212
+ }
213
+ };
214
+ const AssetParams = {
215
+ assetTotal: function (a) {
216
+ const asset = getAsset(a);
217
+ return asset === undefined ? [Uint64(0), false] : [asset.total, true];
218
+ },
219
+ assetDecimals: function (a) {
220
+ const asset = getAsset(a);
221
+ return asset === undefined ? [Uint64(0), false] : [asset.decimals, true];
222
+ },
223
+ assetDefaultFrozen: function (a) {
224
+ const asset = getAsset(a);
225
+ return asset === undefined ? [false, false] : [asset.defaultFrozen, true];
226
+ },
227
+ assetUnitName: function (a) {
228
+ const asset = getAsset(a);
229
+ return asset === undefined ? [Bytes(), false] : [asset.unitName, true];
230
+ },
231
+ assetName: function (a) {
232
+ const asset = getAsset(a);
233
+ return asset === undefined ? [Bytes(), false] : [asset.name, true];
234
+ },
235
+ assetUrl: function (a) {
236
+ const asset = getAsset(a);
237
+ return asset === undefined ? [Bytes(), false] : [asset.url, true];
238
+ },
239
+ assetMetadataHash: function (a) {
240
+ const asset = getAsset(a);
241
+ return asset === undefined ? [Bytes(), false] : [asset.metadataHash, true];
242
+ },
243
+ assetManager: function (a) {
244
+ const asset = getAsset(a);
245
+ return asset === undefined ? [Account(), false] : [asset.manager, true];
246
+ },
247
+ assetReserve: function (a) {
248
+ const asset = getAsset(a);
249
+ return asset === undefined ? [Account(), false] : [asset.reserve, true];
250
+ },
251
+ assetFreeze: function (a) {
252
+ const asset = getAsset(a);
253
+ return asset === undefined ? [Account(), false] : [asset.freeze, true];
254
+ },
255
+ assetClawback: function (a) {
256
+ const asset = getAsset(a);
257
+ return asset === undefined ? [Account(), false] : [asset.clawback, true];
258
+ },
259
+ assetCreator: function (a) {
260
+ const asset = getAsset(a);
261
+ return asset === undefined ? [Account(), false] : [asset.creator, true];
262
+ },
263
+ };
264
+
265
+ const getAssetHolding = (acctOrIndex, assetOrIndex) => {
266
+ const account = getAccount(acctOrIndex);
267
+ const asset = getAsset(assetOrIndex);
268
+ if (asset === undefined) {
269
+ return undefined;
270
+ }
271
+ const accountData = lazyContext.getAccountData(account.bytes);
272
+ const holding = accountData.optedAssets.get(asBigInt(asset.id));
273
+ if (holding === undefined) {
274
+ return undefined;
275
+ }
276
+ return holding;
277
+ };
278
+ const AssetHolding$1 = {
279
+ assetBalance: function (a, b) {
280
+ const holding = getAssetHolding(a, b);
281
+ return holding === undefined ? [Uint64(0), false] : [holding.balance, true];
282
+ },
283
+ assetFrozen: function (a, b) {
284
+ const holding = getAssetHolding(a, b);
285
+ return holding === undefined ? [false, false] : [holding.frozen, true];
286
+ },
287
+ };
288
+
289
+ class NotImplementedError extends Error {
290
+ constructor(feature) {
291
+ super(`${feature} is not available in test context. Mock using your preferred testing framework.`);
292
+ }
293
+ }
294
+ function notImplementedError(feature) {
295
+ throw new NotImplementedError(feature);
296
+ }
297
+ function testInvariant(condition, message) {
298
+ if (!condition) {
299
+ throw new internal.errors.InternalError(message);
300
+ }
301
+ }
302
+
303
+ const sha256 = (a) => {
304
+ const bytesA = internal.primitives.BytesCls.fromCompat(a);
305
+ const hashArray = sha256$1.create().update(bytesA.asUint8Array()).digest();
306
+ const hashBytes = internal.primitives.BytesCls.fromCompat(new Uint8Array(hashArray));
307
+ return hashBytes.asAlgoTs();
308
+ };
309
+ const sha3_256 = (a) => {
310
+ const bytesA = internal.primitives.BytesCls.fromCompat(a);
311
+ const hashArray = sha3_256$1.create().update(bytesA.asUint8Array()).digest();
312
+ const hashBytes = internal.primitives.BytesCls.fromCompat(new Uint8Array(hashArray));
313
+ return hashBytes.asAlgoTs();
314
+ };
315
+ const keccak256 = (a) => {
316
+ const bytesA = internal.primitives.BytesCls.fromCompat(a);
317
+ const hashArray = keccak256$1.create().update(bytesA.asUint8Array()).digest();
318
+ const hashBytes = internal.primitives.BytesCls.fromCompat(new Uint8Array(hashArray));
319
+ return hashBytes.asAlgoTs();
320
+ };
321
+ const sha512_256 = (a) => {
322
+ const bytesA = internal.primitives.BytesCls.fromCompat(a);
323
+ const hashArray = sha512_256$1.create().update(bytesA.asUint8Array()).digest();
324
+ const hashBytes = internal.primitives.BytesCls.fromCompat(new Uint8Array(hashArray));
325
+ return hashBytes.asAlgoTs();
326
+ };
327
+ const ed25519verifyBare = (a, b, c) => {
328
+ const bytesA = internal.primitives.BytesCls.fromCompat(a);
329
+ const bytesB = internal.primitives.BytesCls.fromCompat(b);
330
+ const bytesC = internal.primitives.BytesCls.fromCompat(c);
331
+ return nacl.sign.detached.verify(bytesA.asUint8Array(), bytesB.asUint8Array(), bytesC.asUint8Array());
332
+ };
333
+ const ed25519verify = (a, b, c) => {
334
+ const txn = lazyContext.activeGroup.activeTransaction;
335
+ const programBytes = asBytesCls(txn.onCompletion == arc4.OnCompleteAction[arc4.OnCompleteAction.ClearState] ? txn.clearStateProgram : txn.approvalProgram);
336
+ const logicSig = new algosdk.LogicSig(programBytes.asUint8Array());
337
+ const decodedAddress = algosdk.decodeAddress(logicSig.address());
338
+ const addressBytes = asBytes(decodedAddress.publicKey);
339
+ const data = LOGIC_DATA_PREFIX.concat(addressBytes).concat(asBytes(a));
340
+ return ed25519verifyBare(data, b, c);
341
+ };
342
+ const ecdsaVerify = (v, a, b, c, d, e) => {
343
+ const dataBytes = internal.primitives.BytesCls.fromCompat(a);
344
+ const sigRBytes = internal.primitives.BytesCls.fromCompat(b);
345
+ const sigSBytes = internal.primitives.BytesCls.fromCompat(c);
346
+ const pubkeyXBytes = internal.primitives.BytesCls.fromCompat(d);
347
+ const pubkeyYBytes = internal.primitives.BytesCls.fromCompat(e);
348
+ const publicKey = internal.primitives.BytesCls.fromCompat(new Uint8Array([0x04]))
349
+ .concat(pubkeyXBytes)
350
+ .concat(pubkeyYBytes);
351
+ const ecdsa = new ec(curveMap[v]);
352
+ const keyPair = ecdsa.keyFromPublic(publicKey.asUint8Array());
353
+ return keyPair.verify(dataBytes.asUint8Array(), { r: sigRBytes.asUint8Array(), s: sigSBytes.asUint8Array() });
354
+ };
355
+ const ecdsaPkRecover = (v, a, b, c, d) => {
356
+ if (v !== internal.opTypes.Ecdsa.Secp256k1) {
357
+ internal.errors.internalError(`Unsupported ECDSA curve: ${v}`);
358
+ }
359
+ const dataBytes = internal.primitives.BytesCls.fromCompat(a);
360
+ const rBytes = internal.primitives.BytesCls.fromCompat(c);
361
+ const sBytes = internal.primitives.BytesCls.fromCompat(d);
362
+ const recoveryId = internal.primitives.Uint64Cls.fromCompat(b);
363
+ const ecdsa = new ec(curveMap[v]);
364
+ const pubKey = ecdsa.recoverPubKey(dataBytes.asUint8Array(), { r: rBytes.asUint8Array(), s: sBytes.asUint8Array() }, recoveryId.asNumber());
365
+ const x = pubKey.getX().toArray('be');
366
+ const y = pubKey.getY().toArray('be');
367
+ return [Bytes(new Uint8Array(x)), Bytes(new Uint8Array(y))];
368
+ };
369
+ const ecdsaPkDecompress = (v, a) => {
370
+ const bytesA = internal.primitives.BytesCls.fromCompat(a);
371
+ const ecdsa = new ec(curveMap[v]);
372
+ const keyPair = ecdsa.keyFromPublic(bytesA.asUint8Array());
373
+ const pubKey = keyPair.getPublic();
374
+ const x = pubKey.getX().toArray('be');
375
+ const y = pubKey.getY().toArray('be');
376
+ return [Bytes(new Uint8Array(x)), Bytes(new Uint8Array(y))];
377
+ };
378
+ const vrfVerify = (_s, _a, _b, _c) => {
379
+ notImplementedError('vrfVerify');
380
+ };
381
+ const EllipticCurve = new Proxy({}, {
382
+ get: (_target, prop) => {
383
+ notImplementedError(`EllipticCurve.${prop.toString()}`);
384
+ },
385
+ });
386
+ const curveMap = {
387
+ [internal.opTypes.Ecdsa.Secp256k1]: 'secp256k1',
388
+ [internal.opTypes.Ecdsa.Secp256r1]: 'p256',
389
+ };
390
+
391
+ class GlobalData {
392
+ minTxnFee;
393
+ minBalance;
394
+ maxTxnLife;
395
+ zeroAddress;
396
+ logicSigVersion;
397
+ round;
398
+ latestTimestamp;
399
+ groupId;
400
+ callerApplicationId;
401
+ assetCreateMinBalance;
402
+ assetOptInMinBalance;
403
+ genesisHash;
404
+ opcodeBudget;
405
+ constructor() {
406
+ this.minTxnFee = Uint64(MIN_TXN_FEE);
407
+ this.minBalance = Uint64(DEFAULT_ACCOUNT_MIN_BALANCE);
408
+ this.maxTxnLife = Uint64(DEFAULT_MAX_TXN_LIFE);
409
+ this.zeroAddress = Account(ZERO_ADDRESS);
410
+ this.callerApplicationId = Uint64(0);
411
+ this.assetCreateMinBalance = Uint64(DEFAULT_ASSET_CREATE_MIN_BALANCE);
412
+ this.assetOptInMinBalance = Uint64(DEFAULT_ASSET_OPT_IN_MIN_BALANCE);
413
+ this.genesisHash = DEFAULT_GLOBAL_GENESIS_HASH;
414
+ }
415
+ }
416
+ const getGlobalData = () => {
417
+ return lazyContext.ledger.globalData;
418
+ };
419
+ const getMissingValueErrorMessage = (name) => `'algopy.Global' object has no value set for attribute named '${name}'. Use \`context.ledger.patchGlobalData({${name}: your_value})\` to set the value in your test setup."`;
420
+ const Global = {
421
+ /**
422
+ * microalgos
423
+ */
424
+ get minTxnFee() {
425
+ return getGlobalData().minTxnFee;
426
+ },
427
+ /**
428
+ * microalgos
429
+ */
430
+ get minBalance() {
431
+ return getGlobalData().minBalance;
432
+ },
433
+ /**
434
+ * rounds
435
+ */
436
+ get maxTxnLife() {
437
+ return getGlobalData().maxTxnLife;
438
+ },
439
+ /**
440
+ * 32 byte address of all zero bytes
441
+ */
442
+ get zeroAddress() {
443
+ return getGlobalData().zeroAddress;
444
+ },
445
+ /**
446
+ * Number of transactions in this atomic transaction group. At least 1
447
+ */
448
+ get groupSize() {
449
+ const currentTransactionGroup = lazyContext.activeGroup.transactions;
450
+ return Uint64(currentTransactionGroup.length);
451
+ },
452
+ /**
453
+ * Maximum supported version
454
+ */
455
+ get logicSigVersion() {
456
+ const data = getGlobalData();
457
+ if (data.logicSigVersion !== undefined)
458
+ return data.logicSigVersion;
459
+ throw new internal.errors.InternalError(getMissingValueErrorMessage('logicSigVersion'));
460
+ },
461
+ /**
462
+ * Current round number. Application mode only.
463
+ */
464
+ get round() {
465
+ const data = getGlobalData();
466
+ if (data.round !== undefined)
467
+ return data.round;
468
+ return Uint64(lazyContext.txn.groups.length + 1);
469
+ },
470
+ /**
471
+ * Last confirmed block UNIX timestamp. Fails if negative. Application mode only.
472
+ */
473
+ get latestTimestamp() {
474
+ const data = getGlobalData();
475
+ if (data.latestTimestamp !== undefined)
476
+ return data.latestTimestamp;
477
+ return Uint64(lazyContext.activeGroup.latestTimestamp);
478
+ },
479
+ /**
480
+ * ID of current application executing. Application mode only.
481
+ */
482
+ get currentApplicationId() {
483
+ return lazyContext.activeApplication;
484
+ },
485
+ /**
486
+ * Address of the creator of the current application. Application mode only.
487
+ */
488
+ get creatorAddress() {
489
+ const app = lazyContext.activeApplication;
490
+ return app.creator;
491
+ },
492
+ /**
493
+ * Address that the current application controls. Application mode only.
494
+ */
495
+ get currentApplicationAddress() {
496
+ const appAddress = algosdk.getApplicationAddress(asBigInt(this.currentApplicationId.id));
497
+ return Account(Bytes(appAddress));
498
+ },
499
+ /**
500
+ * ID of the transaction group. 32 zero bytes if the transaction is not part of a group.
501
+ */
502
+ get groupId() {
503
+ const data = getGlobalData();
504
+ if (data.groupId !== undefined)
505
+ return data.groupId;
506
+ const reference = getObjectReference(lazyContext.activeGroup);
507
+ const referenceBytes = Bytes(internal.encodingUtil.bigIntToUint8Array(reference));
508
+ return op.sha256(referenceBytes);
509
+ },
510
+ /**
511
+ * The remaining cost that can be spent by opcodes in this program.
512
+ */
513
+ get opcodeBudget() {
514
+ const data = getGlobalData();
515
+ if (data.opcodeBudget !== undefined)
516
+ return data.opcodeBudget;
517
+ throw new internal.errors.InternalError(getMissingValueErrorMessage('opcodeBudget'));
518
+ },
519
+ /**
520
+ * The application ID of the application that called this application. 0 if this application is at the top-level. Application mode only.
521
+ */
522
+ get callerApplicationId() {
523
+ return getGlobalData().callerApplicationId;
524
+ },
525
+ /**
526
+ * The application address of the application that called this application. ZeroAddress if this application is at the top-level. Application mode only.
527
+ */
528
+ get callerApplicationAddress() {
529
+ const appAddress = algosdk.getApplicationAddress(asBigInt(this.callerApplicationId));
530
+ return Account(Bytes(appAddress));
531
+ },
532
+ /**
533
+ * The additional minimum balance required to create (and opt-in to) an asset.
534
+ */
535
+ get assetCreateMinBalance() {
536
+ return getGlobalData().assetCreateMinBalance;
537
+ },
538
+ /**
539
+ * The additional minimum balance required to opt-in to an asset.
540
+ */
541
+ get assetOptInMinBalance() {
542
+ return getGlobalData().assetOptInMinBalance;
543
+ },
544
+ /**
545
+ * The Genesis Hash for the network.
546
+ */
547
+ get genesisHash() {
548
+ return getGlobalData().genesisHash;
549
+ },
550
+ };
551
+
552
+ const GTxn = {
553
+ sender(t) {
554
+ return lazyContext.activeGroup.getTransaction(t).sender;
555
+ },
556
+ fee(t) {
557
+ return lazyContext.activeGroup.getTransaction(t).fee;
558
+ },
559
+ firstValid(t) {
560
+ return lazyContext.activeGroup.getTransaction(t).firstValid;
561
+ },
562
+ firstValidTime(t) {
563
+ return lazyContext.activeGroup.getTransaction(t).firstValidTime;
564
+ },
565
+ lastValid(t) {
566
+ return lazyContext.activeGroup.getTransaction(t).lastValid;
567
+ },
568
+ note(t) {
569
+ return lazyContext.activeGroup.getTransaction(t).note;
570
+ },
571
+ lease(t) {
572
+ return lazyContext.activeGroup.getTransaction(t).lease;
573
+ },
574
+ receiver(t) {
575
+ return lazyContext.activeGroup.getPaymentTransaction(t).receiver;
576
+ },
577
+ amount(t) {
578
+ return lazyContext.activeGroup.getPaymentTransaction(t).amount;
579
+ },
580
+ closeRemainderTo(t) {
581
+ return lazyContext.activeGroup.getPaymentTransaction(t).closeRemainderTo;
582
+ },
583
+ votePk(t) {
584
+ return lazyContext.activeGroup.getKeyRegistrationTransaction(t).voteKey;
585
+ },
586
+ selectionPk(t) {
587
+ return lazyContext.activeGroup.getKeyRegistrationTransaction(t).selectionKey;
588
+ },
589
+ voteFirst(t) {
590
+ return lazyContext.activeGroup.getKeyRegistrationTransaction(t).voteFirst;
591
+ },
592
+ voteLast(t) {
593
+ return lazyContext.activeGroup.getKeyRegistrationTransaction(t).voteLast;
594
+ },
595
+ voteKeyDilution(t) {
596
+ return lazyContext.activeGroup.getKeyRegistrationTransaction(t).voteKeyDilution;
597
+ },
598
+ type(t) {
599
+ return asUint64Cls(lazyContext.activeGroup.getTransaction(t).type).toBytes().asAlgoTs();
600
+ },
601
+ typeEnum(t) {
602
+ return asUint64(lazyContext.activeGroup.getTransaction(t).type);
603
+ },
604
+ xferAsset(t) {
605
+ return lazyContext.activeGroup.getAssetTransferTransaction(t).xferAsset;
606
+ },
607
+ assetAmount(t) {
608
+ return lazyContext.activeGroup.getAssetTransferTransaction(t).assetAmount;
609
+ },
610
+ assetSender(t) {
611
+ return lazyContext.activeGroup.getAssetTransferTransaction(t).assetSender;
612
+ },
613
+ assetReceiver(t) {
614
+ return lazyContext.activeGroup.getAssetTransferTransaction(t).assetReceiver;
615
+ },
616
+ assetCloseTo(t) {
617
+ return lazyContext.activeGroup.getAssetTransferTransaction(t).assetCloseTo;
618
+ },
619
+ groupIndex(t) {
620
+ return lazyContext.activeGroup.getTransaction(t).groupIndex;
621
+ },
622
+ txId(t) {
623
+ return lazyContext.activeGroup.getTransaction(t).txnId;
624
+ },
625
+ applicationId(t) {
626
+ return lazyContext.activeGroup.getApplicationTransaction(t).appId;
627
+ },
628
+ onCompletion(t) {
629
+ const onCompletionStr = lazyContext.activeGroup.getApplicationTransaction(t).onCompletion;
630
+ return asUint64(arc4.OnCompleteAction[onCompletionStr]);
631
+ },
632
+ applicationArgs(a, b) {
633
+ return lazyContext.activeGroup.getApplicationTransaction(a).appArgs(asUint64(b));
634
+ },
635
+ numAppArgs(t) {
636
+ return lazyContext.activeGroup.getApplicationTransaction(t).numAppArgs;
637
+ },
638
+ accounts(a, b) {
639
+ return lazyContext.activeGroup.getApplicationTransaction(a).accounts(asUint64(b));
640
+ },
641
+ numAccounts(t) {
642
+ return lazyContext.activeGroup.getApplicationTransaction(t).numAccounts;
643
+ },
644
+ approvalProgram(t) {
645
+ return lazyContext.activeGroup.getApplicationTransaction(t).approvalProgram;
646
+ },
647
+ clearStateProgram(t) {
648
+ return lazyContext.activeGroup.getApplicationTransaction(t).clearStateProgram;
649
+ },
650
+ rekeyTo(t) {
651
+ return lazyContext.activeGroup.getTransaction(t).rekeyTo;
652
+ },
653
+ configAsset(t) {
654
+ return lazyContext.activeGroup.getAssetConfigTransaction(t).configAsset;
655
+ },
656
+ configAssetTotal(t) {
657
+ return lazyContext.activeGroup.getAssetConfigTransaction(t).total;
658
+ },
659
+ configAssetDecimals(t) {
660
+ return lazyContext.activeGroup.getAssetConfigTransaction(t).decimals;
661
+ },
662
+ configAssetDefaultFrozen(t) {
663
+ return lazyContext.activeGroup.getAssetConfigTransaction(t).defaultFrozen;
664
+ },
665
+ configAssetUnitName(t) {
666
+ return lazyContext.activeGroup.getAssetConfigTransaction(t).unitName;
667
+ },
668
+ configAssetName(t) {
669
+ return lazyContext.activeGroup.getAssetConfigTransaction(t).assetName;
670
+ },
671
+ configAssetUrl(t) {
672
+ return lazyContext.activeGroup.getAssetConfigTransaction(t).url;
673
+ },
674
+ configAssetMetadataHash(t) {
675
+ return lazyContext.activeGroup.getAssetConfigTransaction(t).metadataHash;
676
+ },
677
+ configAssetManager(t) {
678
+ return lazyContext.activeGroup.getAssetConfigTransaction(t).manager;
679
+ },
680
+ configAssetReserve(t) {
681
+ return lazyContext.activeGroup.getAssetConfigTransaction(t).reserve;
682
+ },
683
+ configAssetFreeze(t) {
684
+ return lazyContext.activeGroup.getAssetConfigTransaction(t).freeze;
685
+ },
686
+ configAssetClawback(t) {
687
+ return lazyContext.activeGroup.getAssetConfigTransaction(t).clawback;
688
+ },
689
+ freezeAsset(t) {
690
+ return lazyContext.activeGroup.getAssetFreezeTransaction(t).freezeAsset;
691
+ },
692
+ freezeAssetAccount(t) {
693
+ return lazyContext.activeGroup.getAssetFreezeTransaction(t).freezeAccount;
694
+ },
695
+ freezeAssetFrozen(t) {
696
+ return lazyContext.activeGroup.getAssetFreezeTransaction(t).frozen;
697
+ },
698
+ assets(a, b) {
699
+ return lazyContext.activeGroup.getApplicationTransaction(a).assets(asUint64(b));
700
+ },
701
+ numAssets(t) {
702
+ return lazyContext.activeGroup.getApplicationTransaction(t).numAssets;
703
+ },
704
+ applications(a, b) {
705
+ return lazyContext.activeGroup.getApplicationTransaction(a).apps(asUint64(b));
706
+ },
707
+ numApplications(t) {
708
+ return lazyContext.activeGroup.getApplicationTransaction(t).numApps;
709
+ },
710
+ globalNumUint(t) {
711
+ return lazyContext.activeGroup.getApplicationTransaction(t).globalNumUint;
712
+ },
713
+ globalNumByteSlice(t) {
714
+ return lazyContext.activeGroup.getApplicationTransaction(t).globalNumBytes;
715
+ },
716
+ localNumUint(t) {
717
+ return lazyContext.activeGroup.getApplicationTransaction(t).localNumUint;
718
+ },
719
+ localNumByteSlice(t) {
720
+ return lazyContext.activeGroup.getApplicationTransaction(t).localNumBytes;
721
+ },
722
+ extraProgramPages(t) {
723
+ return lazyContext.activeGroup.getApplicationTransaction(t).extraProgramPages;
724
+ },
725
+ nonparticipation(t) {
726
+ return lazyContext.activeGroup.getKeyRegistrationTransaction(t).nonparticipation;
727
+ },
728
+ logs(a, b) {
729
+ return lazyContext.activeGroup.getApplicationTransaction(a).logs(asUint64(b));
730
+ },
731
+ numLogs(t) {
732
+ return lazyContext.activeGroup.getApplicationTransaction(t).numLogs;
733
+ },
734
+ createdAssetId(t) {
735
+ return lazyContext.activeGroup.getAssetConfigTransaction(t).createdAsset;
736
+ },
737
+ createdApplicationId(t) {
738
+ return lazyContext.activeGroup.getApplicationTransaction(t).createdApp;
739
+ },
740
+ lastLog(t) {
741
+ return lazyContext.activeGroup.getApplicationTransaction(t).lastLog;
742
+ },
743
+ stateProofPk(t) {
744
+ return lazyContext.activeGroup.getKeyRegistrationTransaction(t).stateProofKey;
745
+ },
746
+ approvalProgramPages(a, b) {
747
+ return lazyContext.activeGroup.getApplicationTransaction(a).approvalProgramPages(asUint64(b));
748
+ },
749
+ numApprovalProgramPages(t) {
750
+ return lazyContext.activeGroup.getApplicationTransaction(t).numApprovalProgramPages;
751
+ },
752
+ clearStateProgramPages(a, b) {
753
+ return lazyContext.activeGroup.getApplicationTransaction(a).clearStateProgramPages(asUint64(b));
754
+ },
755
+ numClearStateProgramPages(t) {
756
+ return lazyContext.activeGroup.getApplicationTransaction(t).numClearStateProgramPages;
757
+ },
758
+ };
759
+
760
+ const addw = (a, b) => {
761
+ const uint64A = internal.primitives.Uint64Cls.fromCompat(a);
762
+ const uint64B = internal.primitives.Uint64Cls.fromCompat(b);
763
+ const sum = uint64A.asBigInt() + uint64B.asBigInt();
764
+ return toUint128(sum);
765
+ };
766
+ const base64Decode = (e, a) => {
767
+ const encoding = e === internal.opTypes.Base64.StdEncoding ? 'base64' : 'base64url';
768
+ const bytesValue = internal.primitives.BytesCls.fromCompat(a);
769
+ const stringValue = bytesValue.toString();
770
+ const bufferResult = Buffer.from(stringValue, encoding);
771
+ if (bufferResult.toString(encoding) !== stringValue) {
772
+ internal.errors.avmError('illegal base64 data');
773
+ }
774
+ const uint8ArrayResult = new Uint8Array(bufferResult);
775
+ return asBytes(uint8ArrayResult);
776
+ };
777
+ const bitLength = (a) => {
778
+ const uint64Cls = asMaybeUint64Cls(a);
779
+ const bigUintCls = asMaybeBytesCls(a)?.toBigUint();
780
+ const bigIntValue = (uint64Cls?.asBigInt() ?? bigUintCls?.asBigInt());
781
+ const binaryValue = bigIntValue === 0n ? '' : bigIntValue.toString(2);
782
+ return Uint64(binaryValue.length);
783
+ };
784
+ const bsqrt = (a) => {
785
+ const bigUintClsValue = internal.primitives.BigUintCls.fromCompat(a);
786
+ const bigintValue = internal.primitives.checkBigUint(bigUintClsValue.asBigInt());
787
+ const sqrtValue = squareroot(bigintValue);
788
+ return asBigUint(sqrtValue);
789
+ };
790
+ const btoi = (a) => {
791
+ const bytesValue = internal.primitives.BytesCls.fromCompat(a);
792
+ if (bytesValue.length.asAlgoTs() > BITS_IN_BYTE) {
793
+ internal.errors.avmError(`btoi arg too long, got [${bytesValue.length.valueOf()}]bytes`);
794
+ }
795
+ return bytesValue.toUint64().asAlgoTs();
796
+ };
797
+ const bzero = (a) => {
798
+ const size = internal.primitives.Uint64Cls.fromCompat(a).asBigInt();
799
+ if (size > MAX_BYTES_SIZE) {
800
+ internal.errors.avmError('bzero attempted to create a too large string');
801
+ }
802
+ return Bytes(new Uint8Array(Array(Number(size)).fill(0x00)));
803
+ };
804
+ const concat = (a, b) => {
805
+ const bytesA = internal.primitives.BytesCls.fromCompat(a);
806
+ const bytesB = internal.primitives.BytesCls.fromCompat(b);
807
+ return bytesA.concat(bytesB).asAlgoTs();
808
+ };
809
+ const divmodw = (a, b, c, d) => {
810
+ const i = uint128ToBigInt(a, b);
811
+ const j = uint128ToBigInt(c, d);
812
+ const div = i / j;
813
+ const mod = i % j;
814
+ return [...toUint128(div), ...toUint128(mod)];
815
+ };
816
+ const divw = (a, b, c) => {
817
+ const i = uint128ToBigInt(a, b);
818
+ const j = internal.primitives.Uint64Cls.fromCompat(c).asBigInt();
819
+ return Uint64(i / j);
820
+ };
821
+ const exp = (a, b) => {
822
+ const base = internal.primitives.Uint64Cls.fromCompat(a).asBigInt();
823
+ const exponent = internal.primitives.Uint64Cls.fromCompat(b).asBigInt();
824
+ if (base === 0n && exponent === 0n) {
825
+ throw internal.errors.codeError('0 ** 0 is undefined');
826
+ }
827
+ return Uint64(base ** exponent);
828
+ };
829
+ const expw = (a, b) => {
830
+ const base = internal.primitives.Uint64Cls.fromCompat(a).asBigInt();
831
+ const exponent = internal.primitives.Uint64Cls.fromCompat(b).asBigInt();
832
+ if (base === 0n && exponent === 0n) {
833
+ throw internal.errors.codeError('0 ** 0 is undefined');
834
+ }
835
+ return toUint128(base ** exponent);
836
+ };
837
+ const extract = (a, b, c) => {
838
+ const bytesValue = internal.primitives.BytesCls.fromCompat(a);
839
+ const bytesLength = bytesValue.length.asBigInt();
840
+ const start = internal.primitives.Uint64Cls.fromCompat(b).asBigInt();
841
+ const length = internal.primitives.Uint64Cls.fromCompat(c).asBigInt();
842
+ let end = start + length;
843
+ if ((typeof b === 'number' || typeof b === 'bigint') && (typeof c === 'number' || typeof c === 'bigint') && length === 0n) {
844
+ end = bytesLength;
845
+ }
846
+ if (start > bytesLength) {
847
+ internal.errors.codeError(`extraction start ${start} is beyond length`);
848
+ }
849
+ if (end > bytesLength) {
850
+ internal.errors.codeError(`extraction end ${end} is beyond length`);
851
+ }
852
+ return bytesValue.slice(start, end).asAlgoTs();
853
+ };
854
+ const extractUint16 = (a, b) => {
855
+ const result = extract(a, b, 2);
856
+ const bytesResult = internal.primitives.BytesCls.fromCompat(result);
857
+ return bytesResult.toUint64().asAlgoTs();
858
+ };
859
+ const extractUint32 = (a, b) => {
860
+ const result = extract(a, b, 4);
861
+ const bytesResult = internal.primitives.BytesCls.fromCompat(result);
862
+ return bytesResult.toUint64().asAlgoTs();
863
+ };
864
+ const extractUint64 = (a, b) => {
865
+ const result = extract(a, b, 8);
866
+ const bytesResult = internal.primitives.BytesCls.fromCompat(result);
867
+ return bytesResult.toUint64().asAlgoTs();
868
+ };
869
+ const getBit = (a, b) => {
870
+ const binaryString = toBinaryString(a);
871
+ const index = internal.primitives.Uint64Cls.fromCompat(b).asNumber();
872
+ const adjustedIndex = asMaybeUint64Cls(a) ? binaryString.length - index - 1 : index;
873
+ if (adjustedIndex < 0 || adjustedIndex >= binaryString.length) {
874
+ internal.errors.codeError(`getBit index ${index} is beyond length`);
875
+ }
876
+ return binaryString[adjustedIndex] === '1' ? Uint64(1) : Uint64(0);
877
+ };
878
+ const getByte = (a, b) => {
879
+ const bytesValue = internal.primitives.BytesCls.fromCompat(a);
880
+ const index = internal.primitives.Uint64Cls.fromCompat(b).asNumber();
881
+ if (index >= bytesValue.length.asNumber()) {
882
+ internal.errors.codeError(`getBytes index ${index} is beyond length`);
883
+ }
884
+ return bytesValue.at(index).toUint64().asAlgoTs();
885
+ };
886
+ const itob = (a) => {
887
+ return asUint64Cls(a).toBytes().asAlgoTs();
888
+ };
889
+ const mulw = (a, b) => {
890
+ const uint64A = internal.primitives.Uint64Cls.fromCompat(a);
891
+ const uint64B = internal.primitives.Uint64Cls.fromCompat(b);
892
+ const product = uint64A.asBigInt() * uint64B.asBigInt();
893
+ return toUint128(product);
894
+ };
895
+ const replace = (a, b, c) => {
896
+ const bytesValue = internal.primitives.BytesCls.fromCompat(a);
897
+ const index = internal.primitives.Uint64Cls.fromCompat(b).asNumber();
898
+ const replacement = internal.primitives.BytesCls.fromCompat(c);
899
+ const valueLength = bytesValue.length.asNumber();
900
+ const replacementLength = replacement.length.asNumber();
901
+ if (index + replacementLength > valueLength) {
902
+ internal.errors.codeError(`expected value <= ${valueLength}, got: ${index + replacementLength}`);
903
+ }
904
+ return bytesValue
905
+ .slice(0, index)
906
+ .concat(replacement)
907
+ .concat(bytesValue.slice(index + replacementLength, valueLength))
908
+ .asAlgoTs();
909
+ };
910
+ const select = ((a, b, c) => {
911
+ const uint64A = asMaybeUint64Cls(a);
912
+ const uint64B = asMaybeUint64Cls(b);
913
+ const bytesA = asMaybeBytesCls(a);
914
+ const bytesB = asMaybeBytesCls(b);
915
+ const bigIntC = internal.primitives.Uint64Cls.fromCompat(c).asBigInt();
916
+ return (bigIntC !== 0n ? (uint64B ?? bytesB) : (uint64A ?? bytesA)).asAlgoTs();
917
+ });
918
+ const setBit = ((a, b, c) => {
919
+ const uint64Cls = asMaybeUint64Cls(a);
920
+ const bytesCls = asMaybeBytesCls(a);
921
+ const binaryString = toBinaryString((uint64Cls ?? bytesCls));
922
+ const isUint64 = !!uint64Cls;
923
+ const indexFromLeft = internal.primitives.Uint64Cls.fromCompat(b).asNumber();
924
+ const indexFromRight = binaryString.length - indexFromLeft - 1;
925
+ const bit = internal.primitives.Uint64Cls.fromCompat(c).asNumber();
926
+ const newBytes = doSetBit(binaryString, isUint64 ? indexFromRight : indexFromLeft, bit);
927
+ return isUint64 ? newBytes.toUint64().asAlgoTs() : newBytes.asAlgoTs();
928
+ });
929
+ const setByte = (a, b, c) => {
930
+ const binaryString = toBinaryString(a);
931
+ const byteIndex = internal.primitives.Uint64Cls.fromCompat(b).asNumber();
932
+ const bitIndex = byteIndex * BITS_IN_BYTE;
933
+ const replacementNumber = internal.primitives.Uint64Cls.fromCompat(c).asBigInt();
934
+ const replacement = replacementNumber === 0n ? '0'.repeat(BITS_IN_BYTE) : toBinaryString(c, true);
935
+ if (bitIndex >= binaryString.length) {
936
+ internal.errors.codeError(`setByte index ${byteIndex} is beyond length`);
937
+ }
938
+ if (replacementNumber > MAX_UINT8) {
939
+ internal.errors.codeError(`setByte value ${replacementNumber} > ${MAX_UINT8}`);
940
+ }
941
+ const updatedString = binaryString.slice(0, bitIndex) + replacement + binaryString.slice(bitIndex + replacement.length);
942
+ const updatedBytes = binaryStringToBytes(updatedString);
943
+ return updatedBytes.asAlgoTs();
944
+ };
945
+ const shl = (a, b) => {
946
+ const uint64A = internal.primitives.Uint64Cls.fromCompat(a);
947
+ const uint64B = internal.primitives.Uint64Cls.fromCompat(b);
948
+ const bigIntA = uint64A.asBigInt();
949
+ const bigIntB = uint64B.asBigInt();
950
+ if (bigIntB >= UINT64_SIZE) {
951
+ internal.errors.codeError(`shl value ${bigIntB} >= ${UINT64_SIZE}`);
952
+ }
953
+ const shifted = (bigIntA * 2n ** bigIntB) % 2n ** BigInt(UINT64_SIZE);
954
+ return Uint64(shifted);
955
+ };
956
+ const shr = (a, b) => {
957
+ const uint64A = internal.primitives.Uint64Cls.fromCompat(a);
958
+ const uint64B = internal.primitives.Uint64Cls.fromCompat(b);
959
+ const bigIntA = uint64A.asBigInt();
960
+ const bigIntB = uint64B.asBigInt();
961
+ if (bigIntB >= UINT64_SIZE) {
962
+ internal.errors.codeError(`shr value ${bigIntB} >= ${UINT64_SIZE}`);
963
+ }
964
+ const shifted = bigIntA / 2n ** bigIntB;
965
+ return Uint64(shifted);
966
+ };
967
+ const sqrt = (a) => {
968
+ const bigIntValue = internal.primitives.Uint64Cls.fromCompat(a).asBigInt();
969
+ const sqrtValue = squareroot(bigIntValue);
970
+ return Uint64(sqrtValue);
971
+ };
972
+ const substring = (a, b, c) => {
973
+ const bytesValue = internal.primitives.BytesCls.fromCompat(a);
974
+ const start = internal.primitives.Uint64Cls.fromCompat(b).asBigInt();
975
+ const end = internal.primitives.Uint64Cls.fromCompat(c).asBigInt();
976
+ if (start > end) {
977
+ internal.errors.codeError('substring end before start');
978
+ }
979
+ if (end > bytesValue.length.asNumber()) {
980
+ internal.errors.codeError('substring range beyond length of string');
981
+ }
982
+ return bytesValue.slice(start, end).asAlgoTs();
983
+ };
984
+ const JsonRef = new Proxy({}, {
985
+ get: (_target, prop) => {
986
+ notImplementedError(`JsonRef.${prop.toString()}`);
987
+ },
988
+ });
989
+ const squareroot = (x) => {
990
+ let lo = 0n, hi = x;
991
+ while (lo <= hi) {
992
+ const mid = (lo + hi) / 2n;
993
+ if (mid * mid > x)
994
+ hi = mid - 1n;
995
+ else
996
+ lo = mid + 1n;
997
+ }
998
+ return hi;
999
+ };
1000
+ const toUint128 = (value) => {
1001
+ const cf = value >> 64n;
1002
+ const rest = value & MAX_UINT64;
1003
+ return [Uint64(cf), Uint64(rest)];
1004
+ };
1005
+ const uint128ToBigInt = (a, b) => {
1006
+ const bigIntA = internal.primitives.Uint64Cls.fromCompat(a).asBigInt();
1007
+ const bigIntB = internal.primitives.Uint64Cls.fromCompat(b).asBigInt();
1008
+ return (bigIntA << 64n) + bigIntB;
1009
+ };
1010
+ const toBinaryString = (a, isDynamic = false) => {
1011
+ const uint64Cls = asMaybeUint64Cls(a);
1012
+ const bytesCls = asMaybeBytesCls(a);
1013
+ let binaryString = '';
1014
+ if (uint64Cls) {
1015
+ binaryString = [...uint64Cls.toBytes(isDynamic).asUint8Array()].map((x) => x.toString(2).padStart(BITS_IN_BYTE, '0')).join('');
1016
+ }
1017
+ else if (bytesCls) {
1018
+ binaryString = [...bytesCls.asUint8Array()].map((x) => x.toString(2).padStart(BITS_IN_BYTE, '0')).join('');
1019
+ }
1020
+ else {
1021
+ internal.errors.codeError('unknown type for argument a');
1022
+ }
1023
+ return binaryString;
1024
+ };
1025
+ const doSetBit = (binaryString, index, bit) => {
1026
+ if (index < 0 || index >= binaryString.length) {
1027
+ internal.errors.codeError(`setBit index ${index < 0 ? binaryString.length - index : index} is beyond length`);
1028
+ }
1029
+ if (bit !== 0 && bit !== 1) {
1030
+ internal.errors.codeError(`setBit value > 1`);
1031
+ }
1032
+ const updatedString = binaryString.slice(0, index) + bit.toString() + binaryString.slice(index + 1);
1033
+ return binaryStringToBytes(updatedString);
1034
+ };
1035
+
1036
+ // import {
1037
+ // getApplicationTransaction,
1038
+ // getAssetConfigTransaction,
1039
+ // getAssetFreezeTransaction,
1040
+ // getAssetTransferTransaction,
1041
+ // getKeyRegistrationTransaction,
1042
+ // getPaymentTransaction,
1043
+ // getTransaction,
1044
+ // } from './gtxn'
1045
+ const gaid = (a) => {
1046
+ const group = lazyContext.activeGroup;
1047
+ const transaction = group.transactions[asNumber(a)];
1048
+ if (transaction.type === TransactionType.ApplicationCall) {
1049
+ return transaction.createdApp.id;
1050
+ }
1051
+ else if (transaction.type === TransactionType.AssetConfig) {
1052
+ return transaction.createdAsset.id;
1053
+ }
1054
+ else {
1055
+ throw new internal.errors.InternalError(`transaction at index ${asNumber(a)} is not an Application Call or Asset Config`);
1056
+ }
1057
+ };
1058
+ const Txn = {
1059
+ get sender() {
1060
+ return lazyContext.activeGroup.getTransaction().sender;
1061
+ },
1062
+ /**
1063
+ * microalgos
1064
+ */
1065
+ get fee() {
1066
+ return lazyContext.activeGroup.getTransaction().fee;
1067
+ },
1068
+ /**
1069
+ * round number
1070
+ */
1071
+ get firstValid() {
1072
+ return lazyContext.activeGroup.getTransaction().firstValid;
1073
+ },
1074
+ /**
1075
+ * UNIX timestamp of block before txn.FirstValid. Fails if negative
1076
+ */
1077
+ get firstValidTime() {
1078
+ return lazyContext.activeGroup.getTransaction().firstValidTime;
1079
+ },
1080
+ /**
1081
+ * round number
1082
+ */
1083
+ get lastValid() {
1084
+ return lazyContext.activeGroup.getTransaction().lastValid;
1085
+ },
1086
+ /**
1087
+ * Any data up to 1024 bytes
1088
+ */
1089
+ get note() {
1090
+ return lazyContext.activeGroup.getTransaction().note;
1091
+ },
1092
+ /**
1093
+ * 32 byte lease value
1094
+ */
1095
+ get lease() {
1096
+ return lazyContext.activeGroup.getTransaction().lease;
1097
+ },
1098
+ /**
1099
+ * 32 byte address
1100
+ */
1101
+ get receiver() {
1102
+ return lazyContext.activeGroup.getPaymentTransaction().receiver;
1103
+ },
1104
+ /**
1105
+ * microalgos
1106
+ */
1107
+ get amount() {
1108
+ return lazyContext.activeGroup.getPaymentTransaction().amount;
1109
+ },
1110
+ /**
1111
+ * 32 byte address
1112
+ */
1113
+ get closeRemainderTo() {
1114
+ return lazyContext.activeGroup.getPaymentTransaction().closeRemainderTo;
1115
+ },
1116
+ /**
1117
+ * 32 byte address
1118
+ */
1119
+ get votePk() {
1120
+ return lazyContext.activeGroup.getKeyRegistrationTransaction().voteKey;
1121
+ },
1122
+ /**
1123
+ * 32 byte address
1124
+ */
1125
+ get selectionPk() {
1126
+ return lazyContext.activeGroup.getKeyRegistrationTransaction().selectionKey;
1127
+ },
1128
+ /**
1129
+ * The first round that the participation key is valid.
1130
+ */
1131
+ get voteFirst() {
1132
+ return lazyContext.activeGroup.getKeyRegistrationTransaction().voteFirst;
1133
+ },
1134
+ /**
1135
+ * The last round that the participation key is valid.
1136
+ */
1137
+ get voteLast() {
1138
+ return lazyContext.activeGroup.getKeyRegistrationTransaction().voteLast;
1139
+ },
1140
+ /**
1141
+ * Dilution for the 2-level participation key
1142
+ */
1143
+ get voteKeyDilution() {
1144
+ return lazyContext.activeGroup.getKeyRegistrationTransaction().voteKeyDilution;
1145
+ },
1146
+ /**
1147
+ * Transaction type as bytes
1148
+ */
1149
+ get type() {
1150
+ return asUint64Cls(lazyContext.activeGroup.getTransaction().type).toBytes().asAlgoTs();
1151
+ },
1152
+ /**
1153
+ * Transaction type as integer
1154
+ */
1155
+ get typeEnum() {
1156
+ return asUint64(lazyContext.activeGroup.getTransaction().type);
1157
+ },
1158
+ /**
1159
+ * Asset ID
1160
+ */
1161
+ get xferAsset() {
1162
+ return lazyContext.activeGroup.getAssetTransferTransaction().xferAsset;
1163
+ },
1164
+ /**
1165
+ * value in Asset's units
1166
+ */
1167
+ get assetAmount() {
1168
+ return lazyContext.activeGroup.getAssetTransferTransaction().assetAmount;
1169
+ },
1170
+ /**
1171
+ * 32 byte address. Source of assets if Sender is the Asset's Clawback address.
1172
+ */
1173
+ get assetSender() {
1174
+ return lazyContext.activeGroup.getAssetTransferTransaction().assetSender;
1175
+ },
1176
+ /**
1177
+ * 32 byte address
1178
+ */
1179
+ get assetReceiver() {
1180
+ return lazyContext.activeGroup.getAssetTransferTransaction().assetReceiver;
1181
+ },
1182
+ /**
1183
+ * 32 byte address
1184
+ */
1185
+ get assetCloseTo() {
1186
+ return lazyContext.activeGroup.getAssetTransferTransaction().assetCloseTo;
1187
+ },
1188
+ /**
1189
+ * Position of this transaction within an atomic transaction group. A stand-alone transaction is implicitly element 0 in a group of 1
1190
+ */
1191
+ get groupIndex() {
1192
+ return lazyContext.activeGroup.getTransaction().groupIndex;
1193
+ },
1194
+ /**
1195
+ * The computed ID for this transaction. 32 bytes.
1196
+ */
1197
+ get txId() {
1198
+ return lazyContext.activeGroup.getTransaction().txnId;
1199
+ },
1200
+ /**
1201
+ * ApplicationID from ApplicationCall transaction
1202
+ */
1203
+ get applicationId() {
1204
+ return lazyContext.activeGroup.getApplicationTransaction().appId;
1205
+ },
1206
+ /**
1207
+ * ApplicationCall transaction on completion action
1208
+ */
1209
+ get onCompletion() {
1210
+ const onCompletionStr = lazyContext.activeGroup.getApplicationTransaction().onCompletion;
1211
+ return asUint64(arc4.OnCompleteAction[onCompletionStr]);
1212
+ },
1213
+ /**
1214
+ * Arguments passed to the application in the ApplicationCall transaction
1215
+ */
1216
+ applicationArgs(a) {
1217
+ return lazyContext.activeGroup.getApplicationTransaction().appArgs(asUint64(a));
1218
+ },
1219
+ /**
1220
+ * Number of ApplicationArgs
1221
+ */
1222
+ get numAppArgs() {
1223
+ return lazyContext.activeGroup.getApplicationTransaction().numAppArgs;
1224
+ },
1225
+ /**
1226
+ * Accounts listed in the ApplicationCall transaction
1227
+ */
1228
+ accounts(a) {
1229
+ return lazyContext.activeGroup.getApplicationTransaction().accounts(asUint64(a));
1230
+ },
1231
+ /**
1232
+ * Number of Accounts
1233
+ */
1234
+ get numAccounts() {
1235
+ return lazyContext.activeGroup.getApplicationTransaction().numAccounts;
1236
+ },
1237
+ /**
1238
+ * Approval program
1239
+ */
1240
+ get approvalProgram() {
1241
+ return lazyContext.activeGroup.getApplicationTransaction().approvalProgram;
1242
+ },
1243
+ /**
1244
+ * Clear state program
1245
+ */
1246
+ get clearStateProgram() {
1247
+ return lazyContext.activeGroup.getApplicationTransaction().clearStateProgram;
1248
+ },
1249
+ /**
1250
+ * 32 byte Sender's new AuthAddr
1251
+ */
1252
+ get rekeyTo() {
1253
+ return lazyContext.activeGroup.getTransaction().rekeyTo;
1254
+ },
1255
+ /**
1256
+ * Asset ID in asset config transaction
1257
+ */
1258
+ get configAsset() {
1259
+ return lazyContext.activeGroup.getAssetConfigTransaction().configAsset;
1260
+ },
1261
+ /**
1262
+ * Total number of units of this asset created
1263
+ */
1264
+ get configAssetTotal() {
1265
+ return lazyContext.activeGroup.getAssetConfigTransaction().total;
1266
+ },
1267
+ /**
1268
+ * Number of digits to display after the decimal place when displaying the asset
1269
+ */
1270
+ get configAssetDecimals() {
1271
+ return lazyContext.activeGroup.getAssetConfigTransaction().decimals;
1272
+ },
1273
+ /**
1274
+ * Whether the asset's slots are frozen by default or not, 0 or 1
1275
+ */
1276
+ get configAssetDefaultFrozen() {
1277
+ return lazyContext.activeGroup.getAssetConfigTransaction().defaultFrozen;
1278
+ },
1279
+ /**
1280
+ * Unit name of the asset
1281
+ */
1282
+ get configAssetUnitName() {
1283
+ return lazyContext.activeGroup.getAssetConfigTransaction().unitName;
1284
+ },
1285
+ /**
1286
+ * The asset name
1287
+ */
1288
+ get configAssetName() {
1289
+ return lazyContext.activeGroup.getAssetConfigTransaction().assetName;
1290
+ },
1291
+ /**
1292
+ * URL
1293
+ */
1294
+ get configAssetUrl() {
1295
+ return lazyContext.activeGroup.getAssetConfigTransaction().url;
1296
+ },
1297
+ /**
1298
+ * 32 byte commitment to unspecified asset metadata
1299
+ */
1300
+ get configAssetMetadataHash() {
1301
+ return lazyContext.activeGroup.getAssetConfigTransaction().metadataHash;
1302
+ },
1303
+ /**
1304
+ * 32 byte address
1305
+ */
1306
+ get configAssetManager() {
1307
+ return lazyContext.activeGroup.getAssetConfigTransaction().manager;
1308
+ },
1309
+ /**
1310
+ * 32 byte address
1311
+ */
1312
+ get configAssetReserve() {
1313
+ return lazyContext.activeGroup.getAssetConfigTransaction().reserve;
1314
+ },
1315
+ /**
1316
+ * 32 byte address
1317
+ */
1318
+ get configAssetFreeze() {
1319
+ return lazyContext.activeGroup.getAssetConfigTransaction().freeze;
1320
+ },
1321
+ /**
1322
+ * 32 byte address
1323
+ */
1324
+ get configAssetClawback() {
1325
+ return lazyContext.activeGroup.getAssetConfigTransaction().clawback;
1326
+ },
1327
+ /**
1328
+ * Asset ID being frozen or un-frozen
1329
+ */
1330
+ get freezeAsset() {
1331
+ return lazyContext.activeGroup.getAssetFreezeTransaction().freezeAsset;
1332
+ },
1333
+ /**
1334
+ * 32 byte address of the account whose asset slot is being frozen or un-frozen
1335
+ */
1336
+ get freezeAssetAccount() {
1337
+ return lazyContext.activeGroup.getAssetFreezeTransaction().freezeAccount;
1338
+ },
1339
+ /**
1340
+ * The new frozen value, 0 or 1
1341
+ */
1342
+ get freezeAssetFrozen() {
1343
+ return lazyContext.activeGroup.getAssetFreezeTransaction().frozen;
1344
+ },
1345
+ /**
1346
+ * Foreign Assets listed in the ApplicationCall transaction
1347
+ */
1348
+ assets(a) {
1349
+ return lazyContext.activeGroup.getApplicationTransaction().assets(asUint64(a));
1350
+ },
1351
+ /**
1352
+ * Number of Assets
1353
+ */
1354
+ get numAssets() {
1355
+ return lazyContext.activeGroup.getApplicationTransaction().numAssets;
1356
+ },
1357
+ /**
1358
+ * Foreign Apps listed in the ApplicationCall transaction
1359
+ */
1360
+ applications(a) {
1361
+ return lazyContext.activeGroup.getApplicationTransaction().apps(asUint64(a));
1362
+ },
1363
+ /**
1364
+ * Number of Applications
1365
+ */
1366
+ get numApplications() {
1367
+ return lazyContext.activeGroup.getApplicationTransaction().numApps;
1368
+ },
1369
+ /**
1370
+ * Number of global state integers in ApplicationCall
1371
+ */
1372
+ get globalNumUint() {
1373
+ return lazyContext.activeGroup.getApplicationTransaction().globalNumUint;
1374
+ },
1375
+ /**
1376
+ * Number of global state byteslices in ApplicationCall
1377
+ */
1378
+ get globalNumByteSlice() {
1379
+ return lazyContext.activeGroup.getApplicationTransaction().globalNumBytes;
1380
+ },
1381
+ /**
1382
+ * Number of local state integers in ApplicationCall
1383
+ */
1384
+ get localNumUint() {
1385
+ return lazyContext.activeGroup.getApplicationTransaction().localNumUint;
1386
+ },
1387
+ /**
1388
+ * Number of local state byteslices in ApplicationCall
1389
+ */
1390
+ get localNumByteSlice() {
1391
+ return lazyContext.activeGroup.getApplicationTransaction().localNumBytes;
1392
+ },
1393
+ /**
1394
+ * Number of additional pages for each of the application's approval and clear state programs. An ExtraProgramPages of 1 means 2048 more total bytes, or 1024 for each program.
1395
+ */
1396
+ get extraProgramPages() {
1397
+ return lazyContext.activeGroup.getApplicationTransaction().extraProgramPages;
1398
+ },
1399
+ /**
1400
+ * Marks an account nonparticipating for rewards
1401
+ */
1402
+ get nonparticipation() {
1403
+ return lazyContext.activeGroup.getKeyRegistrationTransaction().nonparticipation;
1404
+ },
1405
+ /**
1406
+ * Log messages emitted by an application call (only with `itxn` in v5). Application mode only
1407
+ */
1408
+ logs(a) {
1409
+ return lazyContext.activeGroup.getApplicationTransaction().logs(asUint64(a));
1410
+ },
1411
+ /**
1412
+ * Number of Logs (only with `itxn` in v5). Application mode only
1413
+ */
1414
+ get numLogs() {
1415
+ return lazyContext.activeGroup.getApplicationTransaction().numLogs;
1416
+ },
1417
+ /**
1418
+ * Asset ID allocated by the creation of an ASA (only with `itxn` in v5). Application mode only
1419
+ */
1420
+ get createdAssetId() {
1421
+ return lazyContext.activeGroup.getAssetConfigTransaction().createdAsset;
1422
+ },
1423
+ /**
1424
+ * ApplicationID allocated by the creation of an application (only with `itxn` in v5). Application mode only
1425
+ */
1426
+ get createdApplicationId() {
1427
+ return lazyContext.activeGroup.getApplicationTransaction().createdApp;
1428
+ },
1429
+ /**
1430
+ * The last message emitted. Empty bytes if none were emitted. Application mode only
1431
+ */
1432
+ get lastLog() {
1433
+ return lazyContext.activeGroup.getApplicationTransaction().lastLog;
1434
+ },
1435
+ /**
1436
+ * 64 byte state proof public key
1437
+ */
1438
+ get stateProofPk() {
1439
+ return lazyContext.activeGroup.getKeyRegistrationTransaction().stateProofKey;
1440
+ },
1441
+ /**
1442
+ * Approval Program as an array of pages
1443
+ */
1444
+ approvalProgramPages(a) {
1445
+ return lazyContext.activeGroup.getApplicationTransaction().approvalProgramPages(asUint64(a));
1446
+ },
1447
+ /**
1448
+ * Number of Approval Program pages
1449
+ */
1450
+ get numApprovalProgramPages() {
1451
+ return lazyContext.activeGroup.getApplicationTransaction().numApprovalProgramPages;
1452
+ },
1453
+ /**
1454
+ * ClearState Program as an array of pages
1455
+ */
1456
+ clearStateProgramPages(a) {
1457
+ return lazyContext.activeGroup.getApplicationTransaction().clearStateProgramPages(asUint64(a));
1458
+ },
1459
+ /**
1460
+ * Number of ClearState Program pages
1461
+ */
1462
+ get numClearStateProgramPages() {
1463
+ return lazyContext.activeGroup.getApplicationTransaction().numClearStateProgramPages;
1464
+ },
1465
+ };
1466
+
1467
+ const GITxn = {
1468
+ sender: function (t) {
1469
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn(t).sender;
1470
+ },
1471
+ fee: function (t) {
1472
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn(t).fee;
1473
+ },
1474
+ firstValid: function (t) {
1475
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn(t).firstValid;
1476
+ },
1477
+ firstValidTime: function (t) {
1478
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn(t).firstValidTime;
1479
+ },
1480
+ lastValid: function (t) {
1481
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn(t).lastValid;
1482
+ },
1483
+ note: function (t) {
1484
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn(t).note;
1485
+ },
1486
+ lease: function (t) {
1487
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn(t).lease;
1488
+ },
1489
+ receiver: function (t) {
1490
+ return lazyContext.activeGroup.getItxnGroup().getPaymentInnerTxn(t).receiver;
1491
+ },
1492
+ amount: function (t) {
1493
+ return lazyContext.activeGroup.getItxnGroup().getPaymentInnerTxn(t).amount;
1494
+ },
1495
+ closeRemainderTo: function (t) {
1496
+ return lazyContext.activeGroup.getItxnGroup().getPaymentInnerTxn(t).closeRemainderTo;
1497
+ },
1498
+ votePk: function (t) {
1499
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn(t).voteKey;
1500
+ },
1501
+ selectionPk: function (t) {
1502
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn(t).selectionKey;
1503
+ },
1504
+ voteFirst: function (t) {
1505
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn(t).voteFirst;
1506
+ },
1507
+ voteLast: function (t) {
1508
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn(t).voteLast;
1509
+ },
1510
+ voteKeyDilution: function (t) {
1511
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn(t).voteKeyDilution;
1512
+ },
1513
+ type: function (t) {
1514
+ return asUint64Cls(lazyContext.activeGroup.getItxnGroup().getInnerTxn(t).type).toBytes().asAlgoTs();
1515
+ },
1516
+ typeEnum: function (t) {
1517
+ return asUint64(lazyContext.activeGroup.getItxnGroup().getInnerTxn(t).type);
1518
+ },
1519
+ xferAsset: function (t) {
1520
+ return lazyContext.activeGroup.getItxnGroup().getAssetTransferInnerTxn(t).xferAsset;
1521
+ },
1522
+ assetAmount: function (t) {
1523
+ return lazyContext.activeGroup.getItxnGroup().getAssetTransferInnerTxn(t).assetAmount;
1524
+ },
1525
+ assetSender: function (t) {
1526
+ return lazyContext.activeGroup.getItxnGroup().getAssetTransferInnerTxn(t).assetSender;
1527
+ },
1528
+ assetReceiver: function (t) {
1529
+ return lazyContext.activeGroup.getItxnGroup().getAssetTransferInnerTxn(t).assetReceiver;
1530
+ },
1531
+ assetCloseTo: function (t) {
1532
+ return lazyContext.activeGroup.getItxnGroup().getAssetTransferInnerTxn(t).assetCloseTo;
1533
+ },
1534
+ groupIndex: function (t) {
1535
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn(t).groupIndex;
1536
+ },
1537
+ txId: function (t) {
1538
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn(t).txnId;
1539
+ },
1540
+ applicationId: function (t) {
1541
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).appId;
1542
+ },
1543
+ onCompletion: function (t) {
1544
+ const onCompletionStr = lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).onCompletion;
1545
+ return asUint64(arc4.OnCompleteAction[onCompletionStr]);
1546
+ },
1547
+ applicationArgs: function (t, a) {
1548
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).appArgs(asUint64(a));
1549
+ },
1550
+ numAppArgs: function (t) {
1551
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).numAppArgs;
1552
+ },
1553
+ accounts: function (t, a) {
1554
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).accounts(asUint64(a));
1555
+ },
1556
+ numAccounts: function (t) {
1557
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).numAccounts;
1558
+ },
1559
+ approvalProgram: function (t) {
1560
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).approvalProgram;
1561
+ },
1562
+ clearStateProgram: function (t) {
1563
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).clearStateProgram;
1564
+ },
1565
+ rekeyTo: function (t) {
1566
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn(t).rekeyTo;
1567
+ },
1568
+ configAsset: function (t) {
1569
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn(t).configAsset;
1570
+ },
1571
+ configAssetTotal: function (t) {
1572
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn(t).total;
1573
+ },
1574
+ configAssetDecimals: function (t) {
1575
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn(t).decimals;
1576
+ },
1577
+ configAssetDefaultFrozen: function (t) {
1578
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn(t).defaultFrozen;
1579
+ },
1580
+ configAssetUnitName: function (t) {
1581
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn(t).unitName;
1582
+ },
1583
+ configAssetName: function (t) {
1584
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn(t).assetName;
1585
+ },
1586
+ configAssetUrl: function (t) {
1587
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn(t).url;
1588
+ },
1589
+ configAssetMetadataHash: function (t) {
1590
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn(t).metadataHash;
1591
+ },
1592
+ configAssetManager: function (t) {
1593
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn(t).manager;
1594
+ },
1595
+ configAssetReserve: function (t) {
1596
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn(t).reserve;
1597
+ },
1598
+ configAssetFreeze: function (t) {
1599
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn(t).freeze;
1600
+ },
1601
+ configAssetClawback: function (t) {
1602
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn(t).clawback;
1603
+ },
1604
+ freezeAsset: function (t) {
1605
+ return lazyContext.activeGroup.getItxnGroup().getAssetFreezeInnerTxn(t).freezeAsset;
1606
+ },
1607
+ freezeAssetAccount: function (t) {
1608
+ return lazyContext.activeGroup.getItxnGroup().getAssetFreezeInnerTxn(t).freezeAccount;
1609
+ },
1610
+ freezeAssetFrozen: function (t) {
1611
+ return lazyContext.activeGroup.getItxnGroup().getAssetFreezeInnerTxn(t).frozen;
1612
+ },
1613
+ assets: function (t, a) {
1614
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).assets(asUint64(a));
1615
+ },
1616
+ numAssets: function (t) {
1617
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).numAssets;
1618
+ },
1619
+ applications: function (t, a) {
1620
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).apps(asUint64(a));
1621
+ },
1622
+ numApplications: function (t) {
1623
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).numApps;
1624
+ },
1625
+ globalNumUint: function (t) {
1626
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).globalNumUint;
1627
+ },
1628
+ globalNumByteSlice: function (t) {
1629
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).globalNumBytes;
1630
+ },
1631
+ localNumUint: function (t) {
1632
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).localNumUint;
1633
+ },
1634
+ localNumByteSlice: function (t) {
1635
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).localNumBytes;
1636
+ },
1637
+ extraProgramPages: function (t) {
1638
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).extraProgramPages;
1639
+ },
1640
+ nonparticipation: function (t) {
1641
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn(t).nonparticipation;
1642
+ },
1643
+ logs: function (t, a) {
1644
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).logs(asUint64(a));
1645
+ },
1646
+ numLogs: function (t) {
1647
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).numLogs;
1648
+ },
1649
+ createdAssetId: function (t) {
1650
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn(t).createdAsset;
1651
+ },
1652
+ createdApplicationId: function (t) {
1653
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).createdApp;
1654
+ },
1655
+ lastLog: function (t) {
1656
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).lastLog;
1657
+ },
1658
+ stateProofPk: function (t) {
1659
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn(t).stateProofKey;
1660
+ },
1661
+ approvalProgramPages: function (t, a) {
1662
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).approvalProgramPages(asUint64(a));
1663
+ },
1664
+ numApprovalProgramPages: function (t) {
1665
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).numApprovalProgramPages;
1666
+ },
1667
+ clearStateProgramPages: function (t, a) {
1668
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).clearStateProgramPages(asUint64(a));
1669
+ },
1670
+ numClearStateProgramPages: function (t) {
1671
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn(t).numClearStateProgramPages;
1672
+ },
1673
+ };
1674
+ const ITxn = {
1675
+ /**
1676
+ * 32 byte address
1677
+ */
1678
+ get sender() {
1679
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn().sender;
1680
+ },
1681
+ /**
1682
+ * microalgos
1683
+ */
1684
+ get fee() {
1685
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn().fee;
1686
+ },
1687
+ /**
1688
+ * round number
1689
+ */
1690
+ get firstValid() {
1691
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn().firstValid;
1692
+ },
1693
+ /**
1694
+ * UNIX timestamp of block before txn.FirstValid. Fails if negative
1695
+ */
1696
+ get firstValidTime() {
1697
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn().firstValidTime;
1698
+ },
1699
+ /**
1700
+ * round number
1701
+ */
1702
+ get lastValid() {
1703
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn().lastValid;
1704
+ },
1705
+ /**
1706
+ * Any data up to 1024 bytes
1707
+ */
1708
+ get note() {
1709
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn().note;
1710
+ },
1711
+ /**
1712
+ * 32 byte lease value
1713
+ */
1714
+ get lease() {
1715
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn().lease;
1716
+ },
1717
+ /**
1718
+ * 32 byte address
1719
+ */
1720
+ get receiver() {
1721
+ return lazyContext.activeGroup.getItxnGroup().getPaymentInnerTxn().receiver;
1722
+ },
1723
+ /**
1724
+ * microalgos
1725
+ */
1726
+ get amount() {
1727
+ return lazyContext.activeGroup.getItxnGroup().getPaymentInnerTxn().amount;
1728
+ },
1729
+ /**
1730
+ * 32 byte address
1731
+ */
1732
+ get closeRemainderTo() {
1733
+ return lazyContext.activeGroup.getItxnGroup().getPaymentInnerTxn().closeRemainderTo;
1734
+ },
1735
+ /**
1736
+ * 32 byte address
1737
+ */
1738
+ get votePk() {
1739
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn().voteKey;
1740
+ },
1741
+ /**
1742
+ * 32 byte address
1743
+ */
1744
+ get selectionPk() {
1745
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn().selectionKey;
1746
+ },
1747
+ /**
1748
+ * The first round that the participation key is valid.
1749
+ */
1750
+ get voteFirst() {
1751
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn().voteFirst;
1752
+ },
1753
+ /**
1754
+ * The last round that the participation key is valid.
1755
+ */
1756
+ get voteLast() {
1757
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn().voteLast;
1758
+ },
1759
+ /**
1760
+ * Dilution for the 2-level participation key
1761
+ */
1762
+ get voteKeyDilution() {
1763
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn().voteKeyDilution;
1764
+ },
1765
+ /**
1766
+ * Transaction type as bytes
1767
+ */
1768
+ get type() {
1769
+ return asUint64Cls(lazyContext.activeGroup.getItxnGroup().getInnerTxn().type).toBytes().asAlgoTs();
1770
+ },
1771
+ /**
1772
+ * Transaction type as integer
1773
+ */
1774
+ get typeEnum() {
1775
+ return asUint64(lazyContext.activeGroup.getItxnGroup().getInnerTxn().type);
1776
+ },
1777
+ /**
1778
+ * Asset ID
1779
+ */
1780
+ get xferAsset() {
1781
+ return lazyContext.activeGroup.getItxnGroup().getAssetTransferInnerTxn().xferAsset;
1782
+ },
1783
+ /**
1784
+ * value in Asset's units
1785
+ */
1786
+ get assetAmount() {
1787
+ return lazyContext.activeGroup.getItxnGroup().getAssetTransferInnerTxn().assetAmount;
1788
+ },
1789
+ /**
1790
+ * 32 byte address. Source of assets if Sender is the Asset's Clawback address.
1791
+ */
1792
+ get assetSender() {
1793
+ return lazyContext.activeGroup.getItxnGroup().getAssetTransferInnerTxn().assetSender;
1794
+ },
1795
+ /**
1796
+ * 32 byte address
1797
+ */
1798
+ get assetReceiver() {
1799
+ return lazyContext.activeGroup.getItxnGroup().getAssetTransferInnerTxn().assetReceiver;
1800
+ },
1801
+ /**
1802
+ * 32 byte address
1803
+ */
1804
+ get assetCloseTo() {
1805
+ return lazyContext.activeGroup.getItxnGroup().getAssetTransferInnerTxn().assetCloseTo;
1806
+ },
1807
+ /**
1808
+ * Position of this transaction within an atomic transaction group. A stand-alone transaction is implicitly element 0 in a group of 1
1809
+ */
1810
+ get groupIndex() {
1811
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn().groupIndex;
1812
+ },
1813
+ /**
1814
+ * The computed ID for this transaction. 32 bytes.
1815
+ */
1816
+ get txId() {
1817
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn().txnId;
1818
+ },
1819
+ /**
1820
+ * ApplicationID from ApplicationCall transaction
1821
+ */
1822
+ get applicationId() {
1823
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().appId;
1824
+ },
1825
+ /**
1826
+ * ApplicationCall transaction on completion action
1827
+ */
1828
+ get onCompletion() {
1829
+ const onCompletionStr = lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().onCompletion;
1830
+ return asUint64(arc4.OnCompleteAction[onCompletionStr]);
1831
+ },
1832
+ /**
1833
+ * Arguments passed to the application in the ApplicationCall transaction
1834
+ */
1835
+ applicationArgs(a) {
1836
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().appArgs(asUint64(a));
1837
+ },
1838
+ /**
1839
+ * Number of ApplicationArgs
1840
+ */
1841
+ get numAppArgs() {
1842
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().numAppArgs;
1843
+ },
1844
+ /**
1845
+ * Accounts listed in the ApplicationCall transaction
1846
+ */
1847
+ accounts(a) {
1848
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().accounts(asUint64(a));
1849
+ },
1850
+ /**
1851
+ * Number of Accounts
1852
+ */
1853
+ get numAccounts() {
1854
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().numAccounts;
1855
+ },
1856
+ /**
1857
+ * Approval program
1858
+ */
1859
+ get approvalProgram() {
1860
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().approvalProgram;
1861
+ },
1862
+ /**
1863
+ * Clear state program
1864
+ */
1865
+ get clearStateProgram() {
1866
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().clearStateProgram;
1867
+ },
1868
+ /**
1869
+ * 32 byte Sender's new AuthAddr
1870
+ */
1871
+ get rekeyTo() {
1872
+ return lazyContext.activeGroup.getItxnGroup().getInnerTxn().rekeyTo;
1873
+ },
1874
+ /**
1875
+ * Asset ID in asset config transaction
1876
+ */
1877
+ get configAsset() {
1878
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn().configAsset;
1879
+ },
1880
+ /**
1881
+ * Total number of units of this asset created
1882
+ */
1883
+ get configAssetTotal() {
1884
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn().total;
1885
+ },
1886
+ /**
1887
+ * Number of digits to display after the decimal place when displaying the asset
1888
+ */
1889
+ get configAssetDecimals() {
1890
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn().decimals;
1891
+ },
1892
+ /**
1893
+ * Whether the asset's slots are frozen by default or not, 0 or 1
1894
+ */
1895
+ get configAssetDefaultFrozen() {
1896
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn().defaultFrozen;
1897
+ },
1898
+ /**
1899
+ * Unit name of the asset
1900
+ */
1901
+ get configAssetUnitName() {
1902
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn().unitName;
1903
+ },
1904
+ /**
1905
+ * The asset name
1906
+ */
1907
+ get configAssetName() {
1908
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn().assetName;
1909
+ },
1910
+ /**
1911
+ * URL
1912
+ */
1913
+ get configAssetUrl() {
1914
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn().url;
1915
+ },
1916
+ /**
1917
+ * 32 byte commitment to unspecified asset metadata
1918
+ */
1919
+ get configAssetMetadataHash() {
1920
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn().metadataHash;
1921
+ },
1922
+ /**
1923
+ * 32 byte address
1924
+ */
1925
+ get configAssetManager() {
1926
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn().manager;
1927
+ },
1928
+ /**
1929
+ * 32 byte address
1930
+ */
1931
+ get configAssetReserve() {
1932
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn().reserve;
1933
+ },
1934
+ /**
1935
+ * 32 byte address
1936
+ */
1937
+ get configAssetFreeze() {
1938
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn().freeze;
1939
+ },
1940
+ /**
1941
+ * 32 byte address
1942
+ */
1943
+ get configAssetClawback() {
1944
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn().clawback;
1945
+ },
1946
+ /**
1947
+ * Asset ID being frozen or un-frozen
1948
+ */
1949
+ get freezeAsset() {
1950
+ return lazyContext.activeGroup.getItxnGroup().getAssetFreezeInnerTxn().freezeAsset;
1951
+ },
1952
+ /**
1953
+ * 32 byte address of the account whose asset slot is being frozen or un-frozen
1954
+ */
1955
+ get freezeAssetAccount() {
1956
+ return lazyContext.activeGroup.getItxnGroup().getAssetFreezeInnerTxn().freezeAccount;
1957
+ },
1958
+ /**
1959
+ * The new frozen value, 0 or 1
1960
+ */
1961
+ get freezeAssetFrozen() {
1962
+ return lazyContext.activeGroup.getItxnGroup().getAssetFreezeInnerTxn().frozen;
1963
+ },
1964
+ /**
1965
+ * Foreign Assets listed in the ApplicationCall transaction
1966
+ */
1967
+ assets(a) {
1968
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().assets(asUint64(a));
1969
+ },
1970
+ /**
1971
+ * Number of Assets
1972
+ */
1973
+ get numAssets() {
1974
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().numAssets;
1975
+ },
1976
+ /**
1977
+ * Foreign Apps listed in the ApplicationCall transaction
1978
+ */
1979
+ applications(a) {
1980
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().apps(asUint64(a));
1981
+ },
1982
+ /**
1983
+ * Number of Applications
1984
+ */
1985
+ get numApplications() {
1986
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().numApps;
1987
+ },
1988
+ /**
1989
+ * Number of global state integers in ApplicationCall
1990
+ */
1991
+ get globalNumUint() {
1992
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().globalNumUint;
1993
+ },
1994
+ /**
1995
+ * Number of global state byteslices in ApplicationCall
1996
+ */
1997
+ get globalNumByteSlice() {
1998
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().globalNumBytes;
1999
+ },
2000
+ /**
2001
+ * Number of local state integers in ApplicationCall
2002
+ */
2003
+ get localNumUint() {
2004
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().localNumUint;
2005
+ },
2006
+ /**
2007
+ * Number of local state byteslices in ApplicationCall
2008
+ */
2009
+ get localNumByteSlice() {
2010
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().localNumBytes;
2011
+ },
2012
+ /**
2013
+ * Number of additional pages for each of the application's approval and clear state programs. An ExtraProgramPages of 1 means 2048 more total bytes, or 1024 for each program.
2014
+ */
2015
+ get extraProgramPages() {
2016
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().extraProgramPages;
2017
+ },
2018
+ /**
2019
+ * Marks an account nonparticipating for rewards
2020
+ */
2021
+ get nonparticipation() {
2022
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn().nonparticipation;
2023
+ },
2024
+ /**
2025
+ * Log messages emitted by an application call (only with `itxn` in v5). Application mode only
2026
+ */
2027
+ logs(a) {
2028
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().logs(asUint64(a));
2029
+ },
2030
+ /**
2031
+ * Number of Logs (only with `itxn` in v5). Application mode only
2032
+ */
2033
+ get numLogs() {
2034
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().numLogs;
2035
+ },
2036
+ /**
2037
+ * Asset ID allocated by the creation of an ASA (only with `itxn` in v5). Application mode only
2038
+ */
2039
+ get createdAssetId() {
2040
+ return lazyContext.activeGroup.getItxnGroup().getAssetConfigInnerTxn().createdAsset;
2041
+ },
2042
+ /**
2043
+ * ApplicationID allocated by the creation of an application (only with `itxn` in v5). Application mode only
2044
+ */
2045
+ get createdApplicationId() {
2046
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().createdApp;
2047
+ },
2048
+ /**
2049
+ * The last message emitted. Empty bytes if none were emitted. Application mode only
2050
+ */
2051
+ get lastLog() {
2052
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().lastLog;
2053
+ },
2054
+ /**
2055
+ * 64 byte state proof public key
2056
+ */
2057
+ get stateProofPk() {
2058
+ return lazyContext.activeGroup.getItxnGroup().getKeyRegistrationInnerTxn().stateProofKey;
2059
+ },
2060
+ /**
2061
+ * Approval Program as an array of pages
2062
+ */
2063
+ approvalProgramPages(a) {
2064
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().approvalProgramPages(asUint64(a));
2065
+ },
2066
+ /**
2067
+ * Number of Approval Program pages
2068
+ */
2069
+ get numApprovalProgramPages() {
2070
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().numApprovalProgramPages;
2071
+ },
2072
+ /**
2073
+ * ClearState Program as an array of pages
2074
+ */
2075
+ clearStateProgramPages(a) {
2076
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().clearStateProgramPages(asUint64(a));
2077
+ },
2078
+ /**
2079
+ * Number of ClearState Program pages
2080
+ */
2081
+ get numClearStateProgramPages() {
2082
+ return lazyContext.activeGroup.getItxnGroup().getApplicationInnerTxn().numClearStateProgramPages;
2083
+ },
2084
+ };
2085
+ const setConstructingItxnField = (fields) => {
2086
+ Object.assign(lazyContext.activeGroup.constructingItxn, fields);
2087
+ };
2088
+ const getConstructingItxn = () => {
2089
+ return lazyContext.activeGroup.constructingItxn;
2090
+ };
2091
+ const ITxnCreate = {
2092
+ begin: function () {
2093
+ lazyContext.activeGroup.beginInnerTransactionGroup();
2094
+ },
2095
+ setSender: function (a) {
2096
+ setConstructingItxnField({ sender: a });
2097
+ },
2098
+ setFee: function (a) {
2099
+ setConstructingItxnField({ fee: asUint64(a) });
2100
+ },
2101
+ setNote: function (a) {
2102
+ setConstructingItxnField({ note: asBytes(a) });
2103
+ },
2104
+ setReceiver: function (a) {
2105
+ setConstructingItxnField({ receiver: a });
2106
+ },
2107
+ setAmount: function (a) {
2108
+ setConstructingItxnField({ amount: asUint64(a) });
2109
+ },
2110
+ setCloseRemainderTo: function (a) {
2111
+ setConstructingItxnField({ closeRemainderTo: a });
2112
+ },
2113
+ setVotePk: function (a) {
2114
+ setConstructingItxnField({ voteKey: asBytes(a) });
2115
+ },
2116
+ setSelectionPk: function (a) {
2117
+ setConstructingItxnField({ selectionKey: asBytes(a) });
2118
+ },
2119
+ setVoteFirst: function (a) {
2120
+ setConstructingItxnField({ voteFirst: asUint64(a) });
2121
+ },
2122
+ setVoteLast: function (a) {
2123
+ setConstructingItxnField({ voteLast: asUint64(a) });
2124
+ },
2125
+ setVoteKeyDilution: function (a) {
2126
+ setConstructingItxnField({ voteKeyDilution: asUint64(a) });
2127
+ },
2128
+ setType: function (a) {
2129
+ setConstructingItxnField({ type: asBytesCls(a).toUint64().asNumber() });
2130
+ },
2131
+ setTypeEnum: function (a) {
2132
+ setConstructingItxnField({ type: asUint64Cls(a).asNumber() });
2133
+ },
2134
+ setXferAsset: function (a) {
2135
+ setConstructingItxnField({ xferAsset: getAsset(a) });
2136
+ },
2137
+ setAssetAmount: function (a) {
2138
+ setConstructingItxnField({ assetAmount: asUint64(a) });
2139
+ },
2140
+ setAssetSender: function (a) {
2141
+ setConstructingItxnField({ assetSender: a });
2142
+ },
2143
+ setAssetReceiver: function (a) {
2144
+ setConstructingItxnField({ assetReceiver: a });
2145
+ },
2146
+ setAssetCloseTo: function (a) {
2147
+ setConstructingItxnField({ assetCloseTo: a });
2148
+ },
2149
+ setApplicationId: function (a) {
2150
+ setConstructingItxnField({ appId: getApp(a) });
2151
+ },
2152
+ setOnCompletion: function (a) {
2153
+ setConstructingItxnField({ onCompletion: asUint64(a) });
2154
+ },
2155
+ setApplicationArgs: function (a) {
2156
+ const appArgs = (getConstructingItxn().appArgs ?? []);
2157
+ appArgs.push(asBytes(a));
2158
+ setConstructingItxnField({ appArgs });
2159
+ },
2160
+ setAccounts: function (a) {
2161
+ const accounts = (getConstructingItxn().accounts ?? []);
2162
+ accounts.push(a);
2163
+ setConstructingItxnField({ accounts });
2164
+ },
2165
+ setApprovalProgram: function (a) {
2166
+ setConstructingItxnField({ approvalProgram: asBytes(a) });
2167
+ },
2168
+ setClearStateProgram: function (a) {
2169
+ setConstructingItxnField({ clearStateProgram: asBytes(a) });
2170
+ },
2171
+ setRekeyTo: function (a) {
2172
+ setConstructingItxnField({ rekeyTo: a });
2173
+ },
2174
+ setConfigAsset: function (a) {
2175
+ setConstructingItxnField({ configAsset: getAsset(a) });
2176
+ },
2177
+ setConfigAssetTotal: function (a) {
2178
+ setConstructingItxnField({ total: asUint64(a) });
2179
+ },
2180
+ setConfigAssetDecimals: function (a) {
2181
+ setConstructingItxnField({ decimals: asUint64(a) });
2182
+ },
2183
+ setConfigAssetDefaultFrozen: function (a) {
2184
+ setConstructingItxnField({ defaultFrozen: a });
2185
+ },
2186
+ setConfigAssetUnitName: function (a) {
2187
+ setConstructingItxnField({ unitName: asBytes(a) });
2188
+ },
2189
+ setConfigAssetName: function (a) {
2190
+ setConstructingItxnField({ assetName: asBytes(a) });
2191
+ },
2192
+ setConfigAssetUrl: function (a) {
2193
+ setConstructingItxnField({ url: asBytes(a) });
2194
+ },
2195
+ setConfigAssetMetadataHash: function (a) {
2196
+ setConstructingItxnField({ metadataHash: asBytes(a) });
2197
+ },
2198
+ setConfigAssetManager: function (a) {
2199
+ setConstructingItxnField({ manager: a });
2200
+ },
2201
+ setConfigAssetReserve: function (a) {
2202
+ setConstructingItxnField({ reserve: a });
2203
+ },
2204
+ setConfigAssetFreeze: function (a) {
2205
+ setConstructingItxnField({ freeze: a });
2206
+ },
2207
+ setConfigAssetClawback: function (a) {
2208
+ setConstructingItxnField({ clawback: a });
2209
+ },
2210
+ setFreezeAsset: function (a) {
2211
+ setConstructingItxnField({ freezeAsset: getAsset(a) });
2212
+ },
2213
+ setFreezeAssetAccount: function (a) {
2214
+ setConstructingItxnField({ freezeAccount: a });
2215
+ },
2216
+ setFreezeAssetFrozen: function (a) {
2217
+ setConstructingItxnField({ frozen: a });
2218
+ },
2219
+ setAssets: function (a) {
2220
+ const asset = getAsset(a);
2221
+ if (asset) {
2222
+ const assets = (getConstructingItxn().assets ?? []);
2223
+ assets.push(asset);
2224
+ setConstructingItxnField({ assets });
2225
+ }
2226
+ },
2227
+ setApplications: function (a) {
2228
+ const app = getApp(a);
2229
+ if (app) {
2230
+ const apps = (getConstructingItxn().apps ?? []);
2231
+ apps.push(app);
2232
+ setConstructingItxnField({ apps });
2233
+ }
2234
+ },
2235
+ setGlobalNumUint: function (a) {
2236
+ setConstructingItxnField({ globalNumUint: asUint64(a) });
2237
+ },
2238
+ setGlobalNumByteSlice: function (a) {
2239
+ setConstructingItxnField({ globalNumBytes: asUint64(a) });
2240
+ },
2241
+ setLocalNumUint: function (a) {
2242
+ setConstructingItxnField({ localNumUint: asUint64(a) });
2243
+ },
2244
+ setLocalNumByteSlice: function (a) {
2245
+ setConstructingItxnField({ localNumBytes: asUint64(a) });
2246
+ },
2247
+ setExtraProgramPages: function (a) {
2248
+ setConstructingItxnField({ extraProgramPages: asUint64(a) });
2249
+ },
2250
+ setNonparticipation: function (a) {
2251
+ setConstructingItxnField({ nonparticipation: a });
2252
+ },
2253
+ setStateProofPk: function (a) {
2254
+ setConstructingItxnField({ stateProofKey: asBytes(a) });
2255
+ },
2256
+ setApprovalProgramPages: function (a) {
2257
+ let pages = (getConstructingItxn().approvalProgram ?? []);
2258
+ if (!Array.isArray(pages)) {
2259
+ pages = [pages];
2260
+ }
2261
+ pages.push(asBytes(a));
2262
+ setConstructingItxnField({ approvalProgram: pages });
2263
+ },
2264
+ setClearStateProgramPages: function (a) {
2265
+ let pages = (getConstructingItxn().clearStateProgram ?? []);
2266
+ if (!Array.isArray(pages)) {
2267
+ pages = [pages];
2268
+ }
2269
+ pages.push(asBytes(a));
2270
+ setConstructingItxnField({ clearStateProgram: pages });
2271
+ },
2272
+ next: function () {
2273
+ lazyContext.activeGroup.appendInnerTransactionGroup();
2274
+ },
2275
+ submit: function () {
2276
+ lazyContext.activeGroup.submitInnerTransactionGroup();
2277
+ },
2278
+ };
2279
+
2280
+ const Scratch = {
2281
+ loadBytes: function (a) {
2282
+ const result = lazyContext.activeGroup.activeTransaction.getScratchSlot(a);
2283
+ if (result instanceof internal.primitives.BytesCls) {
2284
+ return result;
2285
+ }
2286
+ throw new internal.errors.InternalError('Invalid scratch slot type');
2287
+ },
2288
+ loadUint64: function (a) {
2289
+ const result = lazyContext.activeGroup.activeTransaction.getScratchSlot(a);
2290
+ if (result instanceof internal.primitives.Uint64Cls) {
2291
+ return result;
2292
+ }
2293
+ throw new internal.errors.InternalError('Invalid scratch slot type');
2294
+ },
2295
+ store: function (a, b) {
2296
+ lazyContext.activeGroup.activeTransaction.setScratchSlot(a, b);
2297
+ },
2298
+ };
2299
+
2300
+ var ops = /*#__PURE__*/Object.freeze({
2301
+ __proto__: null,
2302
+ AcctParams: AcctParams,
2303
+ AppParams: AppParams,
2304
+ AssetHolding: AssetHolding$1,
2305
+ AssetParams: AssetParams,
2306
+ EllipticCurve: EllipticCurve,
2307
+ GITxn: GITxn,
2308
+ GTxn: GTxn,
2309
+ Global: Global,
2310
+ ITxn: ITxn,
2311
+ ITxnCreate: ITxnCreate,
2312
+ JsonRef: JsonRef,
2313
+ Scratch: Scratch,
2314
+ Txn: Txn,
2315
+ addw: addw,
2316
+ balance: balance,
2317
+ base64Decode: base64Decode,
2318
+ bitLength: bitLength,
2319
+ bsqrt: bsqrt,
2320
+ btoi: btoi,
2321
+ bzero: bzero,
2322
+ concat: concat,
2323
+ divmodw: divmodw,
2324
+ divw: divw,
2325
+ ecdsaPkDecompress: ecdsaPkDecompress,
2326
+ ecdsaPkRecover: ecdsaPkRecover,
2327
+ ecdsaVerify: ecdsaVerify,
2328
+ ed25519verify: ed25519verify,
2329
+ ed25519verifyBare: ed25519verifyBare,
2330
+ exp: exp,
2331
+ expw: expw,
2332
+ extract: extract,
2333
+ extractUint16: extractUint16,
2334
+ extractUint32: extractUint32,
2335
+ extractUint64: extractUint64,
2336
+ gaid: gaid,
2337
+ getBit: getBit,
2338
+ getByte: getByte,
2339
+ itob: itob,
2340
+ keccak256: keccak256,
2341
+ minBalance: minBalance,
2342
+ mulw: mulw,
2343
+ replace: replace,
2344
+ select: select,
2345
+ setBit: setBit,
2346
+ setByte: setByte,
2347
+ sha256: sha256,
2348
+ sha3_256: sha3_256,
2349
+ sha512_256: sha512_256,
2350
+ shl: shl,
2351
+ shr: shr,
2352
+ sqrt: sqrt,
2353
+ substring: substring,
2354
+ vrfVerify: vrfVerify
2355
+ });
2356
+
2357
+ class ApplicationData {
2358
+ application;
2359
+ isCreating = false;
2360
+ get appLogs() {
2361
+ return this.application.appLogs;
2362
+ }
2363
+ constructor() {
2364
+ this.application = {
2365
+ approvalProgram: ALWAYS_APPROVE_TEAL_PROGRAM,
2366
+ clearStateProgram: ALWAYS_APPROVE_TEAL_PROGRAM,
2367
+ globalNumUint: 0,
2368
+ globalNumBytes: 0,
2369
+ localNumUint: 0,
2370
+ localNumBytes: 0,
2371
+ extraProgramPages: 0,
2372
+ creator: lazyContext.defaultSender,
2373
+ appLogs: [],
2374
+ };
2375
+ }
2376
+ }
2377
+ class ApplicationCls {
2378
+ id;
2379
+ constructor(id) {
2380
+ this.id = asUint64(id ?? 0);
2381
+ }
2382
+ get data() {
2383
+ return lazyContext.getApplicationData(this.id);
2384
+ }
2385
+ get approvalProgram() {
2386
+ return this.data.application.approvalProgram;
2387
+ }
2388
+ get clearStateProgram() {
2389
+ return this.data.application.clearStateProgram;
2390
+ }
2391
+ get globalNumUint() {
2392
+ return this.data.application.globalNumUint;
2393
+ }
2394
+ get globalNumBytes() {
2395
+ return this.data.application.globalNumBytes;
2396
+ }
2397
+ get localNumUint() {
2398
+ return this.data.application.localNumUint;
2399
+ }
2400
+ get localNumBytes() {
2401
+ return this.data.application.localNumBytes;
2402
+ }
2403
+ get extraProgramPages() {
2404
+ return this.data.application.extraProgramPages;
2405
+ }
2406
+ get creator() {
2407
+ return this.data.application.creator;
2408
+ }
2409
+ get address() {
2410
+ const addr = algosdk.getApplicationAddress(asBigInt(this.id));
2411
+ return Account(Bytes(addr));
2412
+ }
2413
+ }
2414
+
2415
+ class AssetCls {
2416
+ id;
2417
+ constructor(id) {
2418
+ this.id = asUint64(id ?? 0);
2419
+ }
2420
+ get data() {
2421
+ return lazyContext.getAssetData(this.id);
2422
+ }
2423
+ get total() {
2424
+ return this.data.total;
2425
+ }
2426
+ get decimals() {
2427
+ return this.data.decimals;
2428
+ }
2429
+ get defaultFrozen() {
2430
+ return this.data.defaultFrozen;
2431
+ }
2432
+ get unitName() {
2433
+ return this.data.unitName;
2434
+ }
2435
+ get name() {
2436
+ return this.data.name;
2437
+ }
2438
+ get url() {
2439
+ return this.data.url;
2440
+ }
2441
+ get metadataHash() {
2442
+ return this.data.metadataHash;
2443
+ }
2444
+ get manager() {
2445
+ return this.data.manager;
2446
+ }
2447
+ get reserve() {
2448
+ return this.data.reserve;
2449
+ }
2450
+ get freeze() {
2451
+ return this.data.freeze;
2452
+ }
2453
+ get clawback() {
2454
+ return this.data.clawback;
2455
+ }
2456
+ get creator() {
2457
+ return this.data.creator;
2458
+ }
2459
+ balance(account) {
2460
+ return this.getAssetHolding(account).balance;
2461
+ }
2462
+ frozen(account) {
2463
+ return this.getAssetHolding(account).frozen;
2464
+ }
2465
+ getAssetHolding(account) {
2466
+ const accountData = lazyContext.getAccountData(account.bytes);
2467
+ if (!accountData.optedAssets.has(asBigInt(this.id))) {
2468
+ internal.errors.internalError('The asset is not opted into the account! Use `ctx.any.account(opted_asset_balances={{ASSET_ID: VALUE}})` to set emulated opted asset into the account.');
2469
+ }
2470
+ return accountData.optedAssets.get(asBigInt(this.id));
2471
+ }
2472
+ }
2473
+
2474
+ class AssetHolding {
2475
+ balance;
2476
+ frozen;
2477
+ constructor(balance, frozen) {
2478
+ this.balance = asUint64(balance);
2479
+ this.frozen = frozen;
2480
+ }
2481
+ }
2482
+ class AccountData {
2483
+ optedAssets = new Map();
2484
+ optedApplications = new Map();
2485
+ account;
2486
+ constructor() {
2487
+ this.account = {
2488
+ totalAppsCreated: 0,
2489
+ totalAppsOptedIn: 0,
2490
+ totalAssets: 0,
2491
+ totalAssetsCreated: 0,
2492
+ totalBoxBytes: 0,
2493
+ totalBoxes: 0,
2494
+ totalExtraAppPages: 0,
2495
+ totalNumByteSlice: 0,
2496
+ totalNumUint: 0,
2497
+ minBalance: DEFAULT_ACCOUNT_MIN_BALANCE,
2498
+ balance: 0,
2499
+ authAddress: Account(),
2500
+ };
2501
+ }
2502
+ }
2503
+ class AccountCls {
2504
+ bytes;
2505
+ constructor(address) {
2506
+ this.bytes = asBytes(address ?? ZERO_ADDRESS);
2507
+ }
2508
+ get data() {
2509
+ return lazyContext.getAccountData(this.bytes);
2510
+ }
2511
+ get balance() {
2512
+ return this.data.account.balance;
2513
+ }
2514
+ get minBalance() {
2515
+ return this.data.account.minBalance;
2516
+ }
2517
+ get authAddress() {
2518
+ return this.data.account.authAddress;
2519
+ }
2520
+ get totalNumUint() {
2521
+ return this.data.account.totalNumUint;
2522
+ }
2523
+ get totalNumByteSlice() {
2524
+ return this.data.account.totalNumByteSlice;
2525
+ }
2526
+ get totalExtraAppPages() {
2527
+ return this.data.account.totalExtraAppPages;
2528
+ }
2529
+ get totalAppsCreated() {
2530
+ return this.data.account.totalAppsCreated;
2531
+ }
2532
+ get totalAppsOptedIn() {
2533
+ return this.data.account.totalAppsOptedIn;
2534
+ }
2535
+ get totalAssetsCreated() {
2536
+ return this.data.account.totalAssetsCreated;
2537
+ }
2538
+ get totalAssets() {
2539
+ return this.data.account.totalAssets;
2540
+ }
2541
+ get totalBoxes() {
2542
+ return this.data.account.totalBoxes;
2543
+ }
2544
+ get totalBoxBytes() {
2545
+ return this.data.account.totalBoxBytes;
2546
+ }
2547
+ isOptedIn(assetOrApp) {
2548
+ if (assetOrApp instanceof AssetCls) {
2549
+ return this.data.optedAssets.has(asUint64Cls(assetOrApp.id).asBigInt());
2550
+ }
2551
+ if (assetOrApp instanceof ApplicationCls) {
2552
+ return this.data.optedApplications.has(asUint64Cls(assetOrApp.id).asBigInt());
2553
+ }
2554
+ throw new internal.errors.InternalError('Invalid argument type. Must be an `algopy.Asset` or `algopy.Application` instance.');
2555
+ }
2556
+ }
2557
+
2558
+ const baseDefaultFields = () => ({
2559
+ sender: lazyContext.defaultSender,
2560
+ fee: Uint64(0),
2561
+ firstValid: Uint64(0),
2562
+ firstValidTime: Uint64(0),
2563
+ lastValid: Uint64(0),
2564
+ note: Bytes(),
2565
+ lease: Bytes(),
2566
+ groupIndex: Uint64(0),
2567
+ txnId: getRandomBytes(32).asAlgoTs(),
2568
+ rekeyTo: Account(),
2569
+ });
2570
+ class TransactionBase {
2571
+ constructor(fields) {
2572
+ const baseDefaults = baseDefaultFields();
2573
+ this.sender = fields.sender ?? baseDefaults.sender;
2574
+ this.fee = fields.fee ?? baseDefaults.fee;
2575
+ this.firstValid = fields.firstValid ?? baseDefaults.firstValid;
2576
+ this.firstValidTime = fields.firstValidTime ?? baseDefaults.firstValidTime;
2577
+ this.lastValid = fields.lastValid ?? baseDefaults.lastValid;
2578
+ this.note = fields.note ?? baseDefaults.note;
2579
+ this.lease = fields.lease ?? baseDefaults.lease;
2580
+ this.groupIndex = fields.groupIndex ?? baseDefaults.groupIndex;
2581
+ this.txnId = fields.txnId ?? baseDefaults.txnId;
2582
+ this.rekeyTo = fields.rekeyTo ?? baseDefaults.rekeyTo;
2583
+ this.scratchSpace = Array(256).fill(Uint64(0));
2584
+ }
2585
+ sender;
2586
+ fee;
2587
+ firstValid;
2588
+ firstValidTime;
2589
+ lastValid;
2590
+ note;
2591
+ lease;
2592
+ groupIndex;
2593
+ txnId;
2594
+ rekeyTo;
2595
+ scratchSpace;
2596
+ setScratchSlot(index, value) {
2597
+ const i = asNumber(index);
2598
+ if (i >= this.scratchSpace.length) {
2599
+ throw internal.errors.internalError('invalid scratch slot');
2600
+ }
2601
+ const bytesValue = asMaybeBytesCls(value);
2602
+ const uint64Value = asMaybeUint64Cls(value);
2603
+ this.scratchSpace[i] = bytesValue?.asAlgoTs() ?? uint64Value?.asAlgoTs() ?? Uint64(0);
2604
+ }
2605
+ getScratchSlot(index) {
2606
+ const i = asNumber(index);
2607
+ if (i >= this.scratchSpace.length) {
2608
+ throw internal.errors.internalError('invalid scratch slot');
2609
+ }
2610
+ return this.scratchSpace[i];
2611
+ }
2612
+ }
2613
+ class PaymentTransaction extends TransactionBase {
2614
+ /* @internal */
2615
+ static create(fields) {
2616
+ return new PaymentTransaction(fields);
2617
+ }
2618
+ constructor(fields) {
2619
+ super(fields);
2620
+ this.receiver = fields.receiver ?? Account();
2621
+ this.amount = fields.amount ?? Uint64(0);
2622
+ this.closeRemainderTo = fields.closeRemainderTo ?? Account();
2623
+ }
2624
+ receiver;
2625
+ amount;
2626
+ closeRemainderTo;
2627
+ type = TransactionType.Payment;
2628
+ typeBytes = asUint64Cls(TransactionType.Payment).toBytes().asAlgoTs();
2629
+ }
2630
+ class KeyRegistrationTransaction extends TransactionBase {
2631
+ /* @internal */
2632
+ static create(fields) {
2633
+ return new KeyRegistrationTransaction(fields);
2634
+ }
2635
+ constructor(fields) {
2636
+ super(fields);
2637
+ this.voteKey = fields.voteKey ?? Bytes();
2638
+ this.selectionKey = fields.selectionKey ?? Bytes();
2639
+ this.voteFirst = fields.voteFirst ?? Uint64(0);
2640
+ this.voteLast = fields.voteLast ?? Uint64(0);
2641
+ this.voteKeyDilution = fields.voteKeyDilution ?? Uint64(0);
2642
+ this.nonparticipation = fields.nonparticipation ?? false;
2643
+ this.stateProofKey = fields.stateProofKey ?? Bytes();
2644
+ }
2645
+ voteKey;
2646
+ selectionKey;
2647
+ voteFirst;
2648
+ voteLast;
2649
+ voteKeyDilution;
2650
+ nonparticipation;
2651
+ stateProofKey;
2652
+ type = TransactionType.KeyRegistration;
2653
+ typeBytes = asUint64Cls(TransactionType.KeyRegistration).toBytes().asAlgoTs();
2654
+ }
2655
+ class AssetConfigTransaction extends TransactionBase {
2656
+ /* @internal */
2657
+ static create(fields) {
2658
+ return new AssetConfigTransaction(fields);
2659
+ }
2660
+ constructor(fields) {
2661
+ super(fields);
2662
+ this.configAsset = fields.configAsset ?? Asset();
2663
+ this.total = fields.total ?? Uint64(0);
2664
+ this.decimals = fields.decimals ?? Uint64(0);
2665
+ this.defaultFrozen = fields.defaultFrozen ?? false;
2666
+ this.unitName = fields.unitName ?? Bytes();
2667
+ this.assetName = fields.assetName ?? Bytes();
2668
+ this.url = fields.url ?? Bytes();
2669
+ this.metadataHash = fields.metadataHash ?? Bytes();
2670
+ this.manager = fields.manager ?? Account();
2671
+ this.reserve = fields.reserve ?? Account();
2672
+ this.freeze = fields.freeze ?? Account();
2673
+ this.clawback = fields.clawback ?? Account();
2674
+ this.createdAsset = fields.createdAsset ?? Asset();
2675
+ }
2676
+ configAsset;
2677
+ total;
2678
+ decimals;
2679
+ defaultFrozen;
2680
+ unitName;
2681
+ assetName;
2682
+ url;
2683
+ metadataHash;
2684
+ manager;
2685
+ reserve;
2686
+ freeze;
2687
+ clawback;
2688
+ createdAsset;
2689
+ type = TransactionType.AssetConfig;
2690
+ typeBytes = asUint64Cls(TransactionType.AssetConfig).toBytes().asAlgoTs();
2691
+ }
2692
+ class AssetTransferTransaction extends TransactionBase {
2693
+ /* @internal */
2694
+ static create(fields) {
2695
+ return new AssetTransferTransaction(fields);
2696
+ }
2697
+ constructor(fields) {
2698
+ super(fields);
2699
+ this.xferAsset = fields.xferAsset ?? Asset();
2700
+ this.assetAmount = fields.assetAmount ?? Uint64(0);
2701
+ this.assetSender = fields.assetSender ?? Account();
2702
+ this.assetReceiver = fields.assetReceiver ?? Account();
2703
+ this.assetCloseTo = fields.assetCloseTo ?? Account();
2704
+ }
2705
+ xferAsset;
2706
+ assetAmount;
2707
+ assetSender;
2708
+ assetReceiver;
2709
+ assetCloseTo;
2710
+ type = TransactionType.AssetTransfer;
2711
+ typeBytes = asUint64Cls(TransactionType.AssetTransfer).toBytes().asAlgoTs();
2712
+ }
2713
+ class AssetFreezeTransaction extends TransactionBase {
2714
+ /* @internal */
2715
+ static create(fields) {
2716
+ return new AssetFreezeTransaction(fields);
2717
+ }
2718
+ constructor(fields) {
2719
+ super(fields);
2720
+ this.freezeAsset = fields.freezeAsset ?? Asset();
2721
+ this.freezeAccount = fields.freezeAccount ?? Account();
2722
+ this.frozen = fields.frozen ?? false;
2723
+ }
2724
+ freezeAsset;
2725
+ freezeAccount;
2726
+ frozen;
2727
+ type = TransactionType.AssetFreeze;
2728
+ typeBytes = asUint64Cls(TransactionType.AssetFreeze).toBytes().asAlgoTs();
2729
+ }
2730
+ class ApplicationTransaction extends TransactionBase {
2731
+ /* @internal */
2732
+ static create(fields) {
2733
+ return new ApplicationTransaction(fields);
2734
+ }
2735
+ #appArgs;
2736
+ #accounts;
2737
+ #assets;
2738
+ #apps;
2739
+ #approvalProgramPages;
2740
+ #clearStateProgramPages;
2741
+ #appLogs;
2742
+ constructor(fields) {
2743
+ super(fields);
2744
+ this.appId = fields.appId ?? Application();
2745
+ this.onCompletion = fields.onCompletion ?? 'NoOp';
2746
+ this.globalNumUint = fields.globalNumUint ?? Uint64(0);
2747
+ this.globalNumBytes = fields.globalNumBytes ?? Uint64(0);
2748
+ this.localNumUint = fields.localNumUint ?? Uint64(0);
2749
+ this.localNumBytes = fields.localNumBytes ?? Uint64(0);
2750
+ this.extraProgramPages = fields.extraProgramPages ?? Uint64(0);
2751
+ this.createdApp = fields.createdApp ?? Application();
2752
+ this.#appArgs = fields.appArgs ?? [];
2753
+ this.#appLogs = fields.appLogs ?? [];
2754
+ this.#accounts = fields.accounts ?? [];
2755
+ this.#assets = fields.assets ?? [];
2756
+ this.#apps = fields.apps ?? [];
2757
+ this.#approvalProgramPages = fields.approvalProgramPages ?? (fields.approvalProgram ? [fields.approvalProgram] : []);
2758
+ this.#clearStateProgramPages = fields.clearStateProgramPages ?? (fields.clearStateProgram ? [fields.clearStateProgram] : []);
2759
+ fields.scratchSpace?.forEach((v, i) => this.setScratchSlot(i, v));
2760
+ }
2761
+ appId;
2762
+ onCompletion;
2763
+ globalNumUint;
2764
+ globalNumBytes;
2765
+ localNumUint;
2766
+ localNumBytes;
2767
+ extraProgramPages;
2768
+ createdApp;
2769
+ get approvalProgram() {
2770
+ return this.approvalProgramPages(0);
2771
+ }
2772
+ get clearStateProgram() {
2773
+ return this.clearStateProgramPages(0);
2774
+ }
2775
+ get numAppArgs() {
2776
+ return Uint64(this.#appArgs.length);
2777
+ }
2778
+ get numAccounts() {
2779
+ return Uint64(this.#accounts.length);
2780
+ }
2781
+ get numAssets() {
2782
+ return Uint64(this.#assets.length);
2783
+ }
2784
+ get numApps() {
2785
+ return Uint64(this.#apps.length);
2786
+ }
2787
+ get numApprovalProgramPages() {
2788
+ return Uint64(this.approvalProgramPages.length);
2789
+ }
2790
+ get numClearStateProgramPages() {
2791
+ return Uint64(this.clearStateProgramPages.length);
2792
+ }
2793
+ get numLogs() {
2794
+ return Uint64(this.#appLogs.length || lazyContext.getApplicationData(this.appId.id).appLogs.length);
2795
+ }
2796
+ get lastLog() {
2797
+ return this.#appLogs.at(-1) ?? lazyContext.getApplicationData(this.appId.id).appLogs.at(-1) ?? Bytes();
2798
+ }
2799
+ appArgs(index) {
2800
+ return this.#appArgs[asNumber(index)];
2801
+ }
2802
+ accounts(index) {
2803
+ return this.#accounts[asNumber(index)];
2804
+ }
2805
+ assets(index) {
2806
+ return this.#assets[asNumber(index)];
2807
+ }
2808
+ apps(index) {
2809
+ return this.#apps[asNumber(index)];
2810
+ }
2811
+ approvalProgramPages(index) {
2812
+ return combineIntoMaxBytePages(this.#approvalProgramPages)[asNumber(index)];
2813
+ }
2814
+ clearStateProgramPages(index) {
2815
+ return combineIntoMaxBytePages(this.#clearStateProgramPages)[asNumber(index)];
2816
+ }
2817
+ logs(index) {
2818
+ const i = asNumber(index);
2819
+ return this.#appLogs[i] ?? lazyContext.getApplicationData(this.appId.id).appLogs ?? Bytes();
2820
+ }
2821
+ type = TransactionType.ApplicationCall;
2822
+ typeBytes = asUint64Cls(TransactionType.ApplicationCall).toBytes().asAlgoTs();
2823
+ /* @internal */
2824
+ get appLogs() {
2825
+ return this.#appLogs;
2826
+ }
2827
+ /* @internal */
2828
+ appendLog(value) {
2829
+ if (this.#appLogs.length + 1 > MAX_ITEMS_IN_LOG) {
2830
+ throw internal.errors.internalError(`Too many log calls in program, up to ${MAX_ITEMS_IN_LOG} is allowed`);
2831
+ }
2832
+ this.#appLogs.push(asBytes(value));
2833
+ }
2834
+ }
2835
+
2836
+ const isUint64GenericType = (typeName) => {
2837
+ if (typeName === undefined)
2838
+ return false;
2839
+ const genericTypes = extractGenericTypeArgs(typeName);
2840
+ return genericTypes.some((t) => t.toLocaleLowerCase() === 'uint64');
2841
+ };
2842
+ const extractStates = (contract) => {
2843
+ const stateTotals = { globalNumBytes: 0, globalNumUint: 0, localNumBytes: 0, localNumUint: 0 };
2844
+ const states = {
2845
+ globalStates: new Map(),
2846
+ localStates: new Map(),
2847
+ totals: stateTotals,
2848
+ };
2849
+ Object.entries(contract).forEach(([key, value]) => {
2850
+ const isLocalState = value instanceof Function && value.name === 'localStateInternal';
2851
+ const isGlobalState = value instanceof internal.state.GlobalStateCls;
2852
+ if (isLocalState || isGlobalState) {
2853
+ // set key using property name if not already set
2854
+ if (value.key === undefined)
2855
+ value.key = Bytes(key);
2856
+ // capture state into the context
2857
+ if (isLocalState)
2858
+ states.localStates.set(value.key, value.map);
2859
+ else
2860
+ states.globalStates.set(value.key, value);
2861
+ // populate state totals
2862
+ const isUint64State = isUint64GenericType(getGenericTypeInfo(value));
2863
+ stateTotals.globalNumUint += isGlobalState && isUint64State ? 1 : 0;
2864
+ stateTotals.globalNumBytes += isGlobalState && !isUint64State ? 1 : 0;
2865
+ stateTotals.localNumUint += isLocalState && isUint64State ? 1 : 0;
2866
+ stateTotals.localNumBytes += isLocalState && !isUint64State ? 1 : 0;
2867
+ }
2868
+ });
2869
+ return states;
2870
+ };
2871
+ const extractArraysFromArgs = (args) => {
2872
+ const transactions = [];
2873
+ const accounts = [];
2874
+ const apps = [];
2875
+ const assets = [];
2876
+ for (const arg of args) {
2877
+ if (isTransaction(arg)) {
2878
+ transactions.push(arg);
2879
+ }
2880
+ else if (arg instanceof AccountCls) {
2881
+ accounts.push(arg);
2882
+ }
2883
+ else if (arg instanceof ApplicationCls) {
2884
+ apps.push(arg);
2885
+ }
2886
+ else if (arg instanceof AssetCls) {
2887
+ assets.push(arg);
2888
+ }
2889
+ }
2890
+ return { accounts, apps, assets, transactions };
2891
+ };
2892
+ function isTransaction(obj) {
2893
+ return (obj instanceof PaymentTransaction ||
2894
+ obj instanceof KeyRegistrationTransaction ||
2895
+ obj instanceof AssetConfigTransaction ||
2896
+ obj instanceof AssetTransferTransaction ||
2897
+ obj instanceof AssetFreezeTransaction ||
2898
+ obj instanceof ApplicationTransaction);
2899
+ }
2900
+ class ContractContext {
2901
+ create(type, ...args) {
2902
+ const proxy = new Proxy(type, this.getContractProxyHandler());
2903
+ return new proxy(...args);
2904
+ }
2905
+ getContractProxyHandler() {
2906
+ const onConstructed = (instance) => {
2907
+ const states = extractStates(instance);
2908
+ const application = lazyContext.any.application({
2909
+ ...states.totals,
2910
+ });
2911
+ lazyContext.ledger.addAppIdContractMap(application.id, instance);
2912
+ };
2913
+ return {
2914
+ construct(target, args) {
2915
+ let isArc4 = false;
2916
+ const instance = new Proxy(new target(...args), {
2917
+ get(target, prop, receiver) {
2918
+ const orig = Reflect.get(target, prop, receiver);
2919
+ if (isArc4 || prop === 'approvalProgram' || prop === 'clearStateProgram') {
2920
+ return (...args) => {
2921
+ const app = lazyContext.ledger.getApplicationForContract(receiver);
2922
+ const { transactions, ...appCallArgs } = extractArraysFromArgs(args);
2923
+ const abiMetadata = getAbiMetadata(receiver, prop);
2924
+ const txns = [
2925
+ ...(transactions ?? []),
2926
+ lazyContext.any.txn.applicationCall({
2927
+ appId: app,
2928
+ ...appCallArgs,
2929
+ onCompletion: (abiMetadata?.allowActions ?? [])[0],
2930
+ }),
2931
+ ];
2932
+ return lazyContext.txn.ensureScope(txns).execute(() => orig.apply(target, args));
2933
+ };
2934
+ }
2935
+ return orig;
2936
+ },
2937
+ });
2938
+ onConstructed(instance);
2939
+ isArc4 = hasAbiMetadata(instance);
2940
+ return instance;
2941
+ },
2942
+ };
2943
+ }
2944
+ }
2945
+
2946
+ class LedgerContext {
2947
+ appIdIter = iterBigInt(1001n, MAX_UINT64);
2948
+ assetIdIter = iterBigInt(1001n, MAX_UINT64);
2949
+ applicationDataMap = new Map();
2950
+ appIdContractMap = new Map();
2951
+ accountDataMap = new Map();
2952
+ assetDataMap = new Map();
2953
+ globalData = new GlobalData();
2954
+ addAppIdContractMap(appId, contract) {
2955
+ this.appIdContractMap.set(asBigInt(appId), contract);
2956
+ }
2957
+ getAsset(assetId) {
2958
+ const id = asBigInt(assetId);
2959
+ if (this.assetDataMap.has(id)) {
2960
+ return Asset(asUint64(id));
2961
+ }
2962
+ throw internal.errors.internalError('Unknown asset, check correct testing context is active');
2963
+ }
2964
+ getApplication(applicationId) {
2965
+ const id = asBigInt(applicationId);
2966
+ if (this.applicationDataMap.has(id)) {
2967
+ return Application(asUint64(id));
2968
+ }
2969
+ throw internal.errors.internalError('Unknown application, check correct testing context is active');
2970
+ }
2971
+ getApplicationForContract(contract) {
2972
+ for (const [appId, c] of this.appIdContractMap) {
2973
+ if (c === contract) {
2974
+ if (this.applicationDataMap.has(appId)) {
2975
+ return Application(asUint64(appId));
2976
+ }
2977
+ }
2978
+ }
2979
+ throw internal.errors.internalError('Unknown contract, check correct testing context is active');
2980
+ }
2981
+ /**
2982
+ * Update asset holdings for account, only specified values will be updated.
2983
+ * Account will also be opted-in to asset
2984
+ * @param accountAddress
2985
+ * @param assetId
2986
+ * @param balance
2987
+ * @param frozen
2988
+ */
2989
+ updateAssetHolding(accountAddress, assetId, balance, frozen) {
2990
+ const addr = (asMaybeBytesCls(accountAddress) ?? asBytesCls(accountAddress.bytes)).toString();
2991
+ const id = (asMaybeUint64Cls(assetId) ?? asUint64Cls(assetId.id)).asBigInt();
2992
+ const accountData = this.accountDataMap.get(addr);
2993
+ const asset = this.assetDataMap.get(id);
2994
+ const holding = accountData.optedAssets.get(id) ?? new AssetHolding(0n, asset.defaultFrozen);
2995
+ if (balance !== undefined)
2996
+ holding.balance = asUint64(balance);
2997
+ if (frozen !== undefined)
2998
+ holding.frozen = frozen;
2999
+ accountData.optedAssets.set(id, holding);
3000
+ }
3001
+ patchGlobalData(data) {
3002
+ this.globalData = {
3003
+ ...this.globalData,
3004
+ ...data,
3005
+ };
3006
+ }
3007
+ }
3008
+
3009
+ function decodeLogs(logs, decoding) {
3010
+ return logs.map((log, i) => {
3011
+ switch (decoding[i]) {
3012
+ case 'i':
3013
+ return internal.encodingUtil.uint8ArrayToBigInt(log);
3014
+ case 's':
3015
+ return internal.encodingUtil.uint8ArrayToUtf8(log);
3016
+ default:
3017
+ return log;
3018
+ }
3019
+ });
3020
+ }
3021
+
3022
+ const mapCommonFields = (fields) => {
3023
+ const { sender, note, rekeyTo, ...rest } = fields;
3024
+ return {
3025
+ sender: sender instanceof Account ? sender : typeof sender === 'string' ? Account(asBytes(sender)) : lazyContext.activeApplication.address,
3026
+ note: note !== undefined ? asBytes(note) : undefined,
3027
+ rekeyTo: rekeyTo instanceof Account ? rekeyTo : typeof rekeyTo === 'string' ? Account(asBytes(rekeyTo)) : undefined,
3028
+ ...rest,
3029
+ };
3030
+ };
3031
+ class PaymentInnerTxn extends PaymentTransaction {
3032
+ isItxn;
3033
+ /* @internal */
3034
+ static create(fields) {
3035
+ return new PaymentInnerTxn(fields);
3036
+ }
3037
+ /* @internal */
3038
+ constructor(fields) {
3039
+ super(mapCommonFields(fields));
3040
+ }
3041
+ }
3042
+ class KeyRegistrationInnerTxn extends KeyRegistrationTransaction {
3043
+ isItxn;
3044
+ /* @internal */
3045
+ static create(fields) {
3046
+ return new KeyRegistrationInnerTxn(fields);
3047
+ }
3048
+ /* @internal */
3049
+ constructor(fields) {
3050
+ super(mapCommonFields(fields));
3051
+ }
3052
+ }
3053
+ class AssetConfigInnerTxn extends AssetConfigTransaction {
3054
+ isItxn;
3055
+ /* @internal */
3056
+ static create(fields) {
3057
+ return new AssetConfigInnerTxn(fields);
3058
+ }
3059
+ /* @internal */
3060
+ constructor(fields) {
3061
+ const { assetName, unitName, url, ...rest } = mapCommonFields(fields);
3062
+ const createdAsset = lazyContext.any.asset({
3063
+ name: typeof assetName === 'string' ? asBytes(assetName) : assetName,
3064
+ unitName: typeof unitName === 'string' ? asBytes(unitName) : unitName,
3065
+ url: typeof url === 'string' ? asBytes(url) : url,
3066
+ ...rest,
3067
+ });
3068
+ super({
3069
+ assetName: typeof assetName === 'string' ? asBytes(assetName) : assetName,
3070
+ unitName: typeof unitName === 'string' ? asBytes(unitName) : unitName,
3071
+ url: typeof url === 'string' ? asBytes(url) : url,
3072
+ ...rest,
3073
+ createdAsset: createdAsset,
3074
+ });
3075
+ }
3076
+ }
3077
+ class AssetTransferInnerTxn extends AssetTransferTransaction {
3078
+ isItxn;
3079
+ /* @internal */
3080
+ static create(fields) {
3081
+ if (fields.xferAsset === undefined) {
3082
+ throw new Error('xferAsset is required');
3083
+ }
3084
+ return new AssetTransferInnerTxn(fields);
3085
+ }
3086
+ /* @internal */
3087
+ constructor(fields) {
3088
+ super(mapCommonFields(fields));
3089
+ }
3090
+ }
3091
+ class AssetFreezeInnerTxn extends AssetFreezeTransaction {
3092
+ isItxn;
3093
+ /* @internal */
3094
+ static create(fields) {
3095
+ if (fields.freezeAsset === undefined) {
3096
+ throw new Error('freezeAsset is required');
3097
+ }
3098
+ return new AssetFreezeInnerTxn(fields);
3099
+ }
3100
+ /* @internal */
3101
+ constructor(fields) {
3102
+ const { freezeAsset, freezeAccount, ...rest } = mapCommonFields(fields);
3103
+ const asset = freezeAsset instanceof internal.primitives.Uint64Cls ? getAsset(freezeAsset) : freezeAsset;
3104
+ const account = typeof freezeAccount === 'string' ? Account(asBytes(freezeAccount)) : freezeAccount;
3105
+ super({
3106
+ freezeAsset: asset,
3107
+ freezeAccount: account,
3108
+ ...rest,
3109
+ });
3110
+ }
3111
+ }
3112
+ class ApplicationInnerTxn extends ApplicationTransaction {
3113
+ isItxn;
3114
+ /* @internal */
3115
+ static create(fields) {
3116
+ return new ApplicationInnerTxn(fields);
3117
+ }
3118
+ /* @internal */
3119
+ constructor(fields) {
3120
+ const { appId, approvalProgram, clearStateProgram, onCompletion, appArgs, accounts, assets, apps, ...rest } = mapCommonFields(fields);
3121
+ super({
3122
+ appId: appId instanceof internal.primitives.Uint64Cls ? getApp(appId) : appId,
3123
+ onCompletion: typeof onCompletion === 'string'
3124
+ ? onCompletion
3125
+ : onCompletion !== undefined
3126
+ ? arc4.OnCompleteAction[onCompletion]
3127
+ : undefined,
3128
+ approvalProgram: Array.isArray(approvalProgram) ? undefined : approvalProgram,
3129
+ approvalProgramPages: Array.isArray(approvalProgram) ? approvalProgram : undefined,
3130
+ clearStateProgram: Array.isArray(clearStateProgram) ? undefined : clearStateProgram,
3131
+ clearStateProgramPages: Array.isArray(clearStateProgram) ? clearStateProgram : undefined,
3132
+ appArgs: appArgs?.map((x) => toBytes(x)),
3133
+ accounts: accounts?.map((x) => x),
3134
+ assets: assets?.map((x) => x),
3135
+ apps: apps?.map((x) => x),
3136
+ ...rest,
3137
+ });
3138
+ }
3139
+ }
3140
+ const createInnerTxn = (fields) => {
3141
+ switch (fields.type) {
3142
+ case TransactionType.Payment:
3143
+ return new PaymentInnerTxn(fields);
3144
+ case TransactionType.AssetConfig:
3145
+ return new AssetConfigInnerTxn(fields);
3146
+ case TransactionType.AssetTransfer:
3147
+ return new AssetTransferInnerTxn(fields);
3148
+ case TransactionType.AssetFreeze:
3149
+ return new AssetFreezeInnerTxn(fields);
3150
+ case TransactionType.ApplicationCall:
3151
+ return new ApplicationInnerTxn(fields);
3152
+ case TransactionType.KeyRegistration:
3153
+ return new KeyRegistrationInnerTxn(fields);
3154
+ default:
3155
+ throw new internal.errors.InternalError(`Invalid inner transaction type: ${fields.type}`);
3156
+ }
3157
+ };
3158
+ function submitGroup(...transactionFields) {
3159
+ return transactionFields.map((f) => f.submit());
3160
+ }
3161
+ function payment(fields) {
3162
+ ensureItxnGroupBegin();
3163
+ return new ItxnParams(fields, TransactionType.Payment);
3164
+ }
3165
+ function keyRegistration(fields) {
3166
+ ensureItxnGroupBegin();
3167
+ return new ItxnParams(fields, TransactionType.KeyRegistration);
3168
+ }
3169
+ function assetConfig(fields) {
3170
+ ensureItxnGroupBegin();
3171
+ return new ItxnParams(fields, TransactionType.AssetConfig);
3172
+ }
3173
+ function assetTransfer(fields) {
3174
+ ensureItxnGroupBegin();
3175
+ return new ItxnParams(fields, TransactionType.AssetTransfer);
3176
+ }
3177
+ function assetFreeze(fields) {
3178
+ ensureItxnGroupBegin();
3179
+ return new ItxnParams(fields, TransactionType.AssetFreeze);
3180
+ }
3181
+ function applicationCall(fields) {
3182
+ ensureItxnGroupBegin();
3183
+ return new ItxnParams(fields, TransactionType.ApplicationCall);
3184
+ }
3185
+ class ItxnParams {
3186
+ #fields;
3187
+ constructor(fields, type) {
3188
+ this.#fields = { ...fields, type };
3189
+ }
3190
+ submit() {
3191
+ return createInnerTxn(this.#fields);
3192
+ }
3193
+ set(p) {
3194
+ Object.assign(this.#fields, p);
3195
+ }
3196
+ copy() {
3197
+ return new ItxnParams(this.#fields, this.#fields.type);
3198
+ }
3199
+ }
3200
+ const ensureItxnGroupBegin = () => {
3201
+ if (!lazyContext.activeGroup.constructingItxnGroup.length) {
3202
+ lazyContext.activeGroup.beginInnerTransactionGroup();
3203
+ }
3204
+ };
3205
+
3206
+ function ScopeGenerator(dispose) {
3207
+ function* internal() {
3208
+ try {
3209
+ yield;
3210
+ }
3211
+ finally {
3212
+ dispose();
3213
+ }
3214
+ }
3215
+ const scope = internal();
3216
+ scope.next();
3217
+ return {
3218
+ done: () => {
3219
+ scope.return();
3220
+ },
3221
+ };
3222
+ }
3223
+ class TransactionContext {
3224
+ groups = [];
3225
+ #activeGroup;
3226
+ createScope(group, activeTransactionIndex) {
3227
+ const transactionGroup = new TransactionGroup(group, activeTransactionIndex);
3228
+ const previousGroup = this.#activeGroup;
3229
+ this.#activeGroup = transactionGroup;
3230
+ const scope = ScopeGenerator(() => {
3231
+ if (this.#activeGroup?.transactions?.length) {
3232
+ this.groups.push(this.#activeGroup);
3233
+ }
3234
+ this.#activeGroup = previousGroup;
3235
+ });
3236
+ return {
3237
+ execute: (body) => {
3238
+ const result = body();
3239
+ scope.done();
3240
+ return result;
3241
+ },
3242
+ };
3243
+ }
3244
+ ensureScope(group, activeTransactionIndex) {
3245
+ if (!this.#activeGroup || !this.#activeGroup.transactions.length) {
3246
+ return this.createScope(group, activeTransactionIndex);
3247
+ }
3248
+ return {
3249
+ execute: (body) => {
3250
+ return body();
3251
+ },
3252
+ };
3253
+ }
3254
+ get activeGroup() {
3255
+ if (this.#activeGroup) {
3256
+ return this.#activeGroup;
3257
+ }
3258
+ throw internal.errors.internalError('no active txn group');
3259
+ }
3260
+ get lastGroup() {
3261
+ if (this.groups.length === 0) {
3262
+ internal.errors.internalError('No group transactions found!');
3263
+ }
3264
+ return this.groups.at(-1);
3265
+ }
3266
+ get lastActive() {
3267
+ return this.lastGroup.activeTransaction;
3268
+ }
3269
+ /* internal */
3270
+ appendLog(value) {
3271
+ const activeTransaction = this.activeGroup.activeTransaction;
3272
+ if (activeTransaction.type !== TransactionType.ApplicationCall) {
3273
+ throw internal.errors.internalError('Can only add logs to ApplicationCallTransaction!');
3274
+ }
3275
+ activeTransaction.appendLog(value);
3276
+ }
3277
+ exportLogs(appId, ...decoding) {
3278
+ const transaction = this.groups
3279
+ .flatMap((g) => g.transactions)
3280
+ .filter((t) => t.type === TransactionType.ApplicationCall)
3281
+ .find((t) => asBigInt(t.appId.id) === asBigInt(appId));
3282
+ let logs = [];
3283
+ if (transaction) {
3284
+ logs = transaction.appLogs;
3285
+ }
3286
+ else {
3287
+ logs = lazyContext.getApplicationData(appId).appLogs;
3288
+ }
3289
+ const rawLogs = logs.map((l) => internal.primitives.toExternalValue(l));
3290
+ return decodeLogs(rawLogs, decoding);
3291
+ }
3292
+ }
3293
+ class TransactionGroup {
3294
+ activeTransactionIndex;
3295
+ latestTimestamp;
3296
+ transactions;
3297
+ itxnGroups = [];
3298
+ constructingItxnGroup = [];
3299
+ constructor(transactions, activeTransactionIndex) {
3300
+ this.latestTimestamp = Date.now();
3301
+ if (transactions.length > algosdk.AtomicTransactionComposer.MAX_GROUP_SIZE) {
3302
+ internal.errors.internalError(`Transaction group can have at most ${algosdk.AtomicTransactionComposer.MAX_GROUP_SIZE} transactions, as per AVM limits.`);
3303
+ }
3304
+ transactions.forEach((txn, index) => Object.assign(txn, { groupIndex: asUint64(index) }));
3305
+ this.activeTransactionIndex = activeTransactionIndex === undefined ? transactions.length - 1 : activeTransactionIndex;
3306
+ this.transactions = transactions;
3307
+ }
3308
+ get activeTransaction() {
3309
+ return this.transactions[this.activeTransactionIndex];
3310
+ }
3311
+ get activeApplicationId() {
3312
+ if (this.transactions.length === 0) {
3313
+ internal.errors.internalError('No transactions in the group');
3314
+ }
3315
+ testInvariant(this.activeTransaction.type === TransactionType.ApplicationCall, 'No app_id found in the active transaction');
3316
+ return this.activeTransaction.appId.id;
3317
+ }
3318
+ get constructingItxn() {
3319
+ if (!this.constructingItxnGroup.length) {
3320
+ internal.errors.internalError('itxn field without itxn begin');
3321
+ }
3322
+ return this.constructingItxnGroup.at(-1);
3323
+ }
3324
+ getScratchSlot(index) {
3325
+ return this.activeTransaction.getScratchSlot(index);
3326
+ }
3327
+ patchActiveTransactionFields(fields) {
3328
+ const activeTransaction = this.activeTransaction;
3329
+ const filteredFields = Object.fromEntries(Object.entries(fields).filter(([_, value]) => value !== undefined));
3330
+ Object.assign(activeTransaction, filteredFields);
3331
+ }
3332
+ beginInnerTransactionGroup() {
3333
+ if (this.constructingItxnGroup.length) {
3334
+ internal.errors.internalError('itxn begin without itxn submit');
3335
+ }
3336
+ testInvariant(this.activeTransaction.type === TransactionType.ApplicationCall, 'No active application call transaction');
3337
+ if (this.activeTransaction.onCompletion === 'ClearState') {
3338
+ internal.errors.internalError('Cannot begin inner transaction group in a clear state call');
3339
+ }
3340
+ this.constructingItxnGroup.push({});
3341
+ }
3342
+ appendInnerTransactionGroup() {
3343
+ if (!this.constructingItxnGroup.length) {
3344
+ internal.errors.internalError('itxn next without itxn begin');
3345
+ }
3346
+ this.constructingItxnGroup.push({ type: TransactionType.Payment });
3347
+ }
3348
+ submitInnerTransactionGroup() {
3349
+ if (!this.constructingItxnGroup.length) {
3350
+ internal.errors.internalError('itxn submit without itxn begin');
3351
+ }
3352
+ if (this.constructingItxnGroup.length > algosdk.AtomicTransactionComposer.MAX_GROUP_SIZE) {
3353
+ internal.errors.internalError('Cannot submit more than 16 inner transactions at once');
3354
+ }
3355
+ const itxns = this.constructingItxnGroup.map((t) => createInnerTxn(t));
3356
+ itxns.forEach((itxn, index) => Object.assign(itxn, { groupIndex: asUint64(index) }));
3357
+ this.itxnGroups.push(new ItxnGroup(itxns));
3358
+ this.constructingItxnGroup = [];
3359
+ }
3360
+ getItxnGroup(index) {
3361
+ const i = index !== undefined ? asNumber(index) : undefined;
3362
+ testInvariant(this.itxnGroups.length > 0, 'no previous inner transactions');
3363
+ if (i !== undefined && i >= this.itxnGroups.length) {
3364
+ throw new internal.errors.InternalError('Invalid group index');
3365
+ }
3366
+ const group = i !== undefined ? this.itxnGroups[i] : this.itxnGroups.at(-1);
3367
+ testInvariant(group.itxns.length > 0, 'no previous inner transactions');
3368
+ return group;
3369
+ }
3370
+ getApplicationTransaction(index) {
3371
+ return this.getTransactionImpl({ type: TransactionType.ApplicationCall, index });
3372
+ }
3373
+ getAssetConfigTransaction(index) {
3374
+ return this.getTransactionImpl({ type: TransactionType.AssetConfig, index });
3375
+ }
3376
+ getAssetTransferTransaction(index) {
3377
+ return this.getTransactionImpl({ type: TransactionType.AssetTransfer, index });
3378
+ }
3379
+ getAssetFreezeTransaction(index) {
3380
+ return this.getTransactionImpl({ type: TransactionType.AssetFreeze, index });
3381
+ }
3382
+ getKeyRegistrationTransaction(index) {
3383
+ return this.getTransactionImpl({ type: TransactionType.KeyRegistration, index });
3384
+ }
3385
+ getPaymentTransaction(index) {
3386
+ return this.getTransactionImpl({ type: TransactionType.Payment, index });
3387
+ }
3388
+ getTransaction(index) {
3389
+ return this.getTransactionImpl({ index });
3390
+ }
3391
+ getTransactionImpl({ type, index }) {
3392
+ const i = index !== undefined ? asNumber(index) : undefined;
3393
+ if (i !== undefined && i >= lazyContext.activeGroup.transactions.length) {
3394
+ throw new internal.errors.InternalError('Invalid group index');
3395
+ }
3396
+ const transaction = i !== undefined ? lazyContext.activeGroup.transactions[i] : lazyContext.activeGroup.activeTransaction;
3397
+ if (type === undefined) {
3398
+ return transaction;
3399
+ }
3400
+ if (transaction.type !== type) {
3401
+ throw new internal.errors.InternalError(`Invalid transaction type: ${transaction.type}`);
3402
+ }
3403
+ switch (type) {
3404
+ case TransactionType.ApplicationCall:
3405
+ return transaction;
3406
+ case TransactionType.Payment:
3407
+ return transaction;
3408
+ case TransactionType.AssetConfig:
3409
+ return transaction;
3410
+ case TransactionType.AssetTransfer:
3411
+ return transaction;
3412
+ case TransactionType.AssetFreeze:
3413
+ return transaction;
3414
+ case TransactionType.KeyRegistration:
3415
+ return transaction;
3416
+ default:
3417
+ throw new internal.errors.InternalError(`Invalid transaction type: ${type}`);
3418
+ }
3419
+ }
3420
+ }
3421
+ class ItxnGroup {
3422
+ itxns = [];
3423
+ constructor(itxns) {
3424
+ this.itxns = itxns;
3425
+ }
3426
+ getApplicationInnerTxn(index) {
3427
+ return this.getInnerTxnImpl({ type: TransactionType.ApplicationCall, index });
3428
+ }
3429
+ getAssetConfigInnerTxn(index) {
3430
+ return this.getInnerTxnImpl({ type: TransactionType.AssetConfig, index });
3431
+ }
3432
+ getAssetTransferInnerTxn(index) {
3433
+ return this.getInnerTxnImpl({ type: TransactionType.AssetTransfer, index });
3434
+ }
3435
+ getAssetFreezeInnerTxn(index) {
3436
+ return this.getInnerTxnImpl({ type: TransactionType.AssetFreeze, index });
3437
+ }
3438
+ getKeyRegistrationInnerTxn(index) {
3439
+ return this.getInnerTxnImpl({ type: TransactionType.KeyRegistration, index });
3440
+ }
3441
+ getPaymentInnerTxn(index) {
3442
+ return this.getInnerTxnImpl({ type: TransactionType.Payment, index });
3443
+ }
3444
+ getInnerTxn(index) {
3445
+ return this.getInnerTxnImpl({ index });
3446
+ }
3447
+ getInnerTxnImpl({ type, index }) {
3448
+ testInvariant(this.itxns.length > 0, 'no previous inner transactions');
3449
+ const i = index !== undefined ? asNumber(index) : undefined;
3450
+ if (i !== undefined && i >= this.itxns.length) {
3451
+ throw new internal.errors.InternalError('Invalid group index');
3452
+ }
3453
+ const transaction = i !== undefined ? this.itxns[i] : this.itxns.at(-1);
3454
+ if (type === undefined) {
3455
+ return transaction;
3456
+ }
3457
+ if (transaction.type !== type) {
3458
+ throw new internal.errors.InternalError(`Invalid transaction type: ${transaction.type}`);
3459
+ }
3460
+ switch (type) {
3461
+ case TransactionType.ApplicationCall:
3462
+ return transaction;
3463
+ case TransactionType.Payment:
3464
+ return transaction;
3465
+ case TransactionType.AssetConfig:
3466
+ return transaction;
3467
+ case TransactionType.AssetTransfer:
3468
+ return transaction;
3469
+ case TransactionType.AssetFreeze:
3470
+ return transaction;
3471
+ case TransactionType.KeyRegistration:
3472
+ return transaction;
3473
+ default:
3474
+ throw new internal.errors.InternalError(`Invalid transaction type: ${type}`);
3475
+ }
3476
+ }
3477
+ }
3478
+
3479
+ class AvmValueGenerator {
3480
+ uint64(minValue = 0n, maxValue = MAX_UINT64) {
3481
+ if (maxValue > MAX_UINT64) {
3482
+ internal.errors.internalError('maxValue must be less than or equal to MAX_UINT64');
3483
+ }
3484
+ if (minValue > maxValue) {
3485
+ internal.errors.internalError('minValue must be less than or equal to maxValue');
3486
+ }
3487
+ if (minValue < 0n || maxValue < 0n) {
3488
+ internal.errors.internalError('minValue and maxValue must be greater than or equal to 0');
3489
+ }
3490
+ return Uint64(getRandomBigInt(minValue, maxValue));
3491
+ }
3492
+ bytes(length = MAX_BYTES_SIZE) {
3493
+ return Bytes(new Uint8Array(randomBytes(length)));
3494
+ }
3495
+ account(input) {
3496
+ const addr = input?.address ? asBytesCls(input.address).toString() : undefined;
3497
+ if (addr && lazyContext.ledger.accountDataMap.has(addr)) {
3498
+ internal.errors.internalError('Account with such address already exists in testing context. Use `context.ledger.getAccount(address)` to retrieve the existing account.');
3499
+ }
3500
+ const accountAddress = addr ?? algosdk.generateAccount().addr;
3501
+ const data = new AccountData();
3502
+ const { address, optedAssetBalances, optedApplications, ...account } = input ?? {};
3503
+ data.account = {
3504
+ ...data.account,
3505
+ ...account,
3506
+ };
3507
+ lazyContext.ledger.accountDataMap.set(accountAddress, data);
3508
+ if (input?.optedAssetBalances) {
3509
+ for (const [assetId, balance] of input.optedAssetBalances) {
3510
+ lazyContext.ledger.updateAssetHolding(accountAddress, assetId, balance);
3511
+ }
3512
+ }
3513
+ if (input?.optedApplications) {
3514
+ for (const app of input.optedApplications) {
3515
+ data.optedApplications.set(asBigInt(app.id), app);
3516
+ }
3517
+ }
3518
+ return new AccountCls(Bytes(accountAddress));
3519
+ }
3520
+ asset(input) {
3521
+ const id = input?.assetId;
3522
+ if (id && lazyContext.ledger.assetDataMap.has(asBigInt(id))) {
3523
+ internal.errors.internalError('Asset with such ID already exists in testing context!');
3524
+ }
3525
+ const assetId = asUint64Cls(id ?? lazyContext.ledger.assetIdIter.next().value);
3526
+ const defaultAssetData = {
3527
+ total: lazyContext.any.uint64(),
3528
+ decimals: lazyContext.any.uint64(1, 6),
3529
+ defaultFrozen: false,
3530
+ unitName: lazyContext.any.bytes(4),
3531
+ name: lazyContext.any.bytes(32),
3532
+ url: lazyContext.any.bytes(10),
3533
+ metadataHash: lazyContext.any.bytes(32),
3534
+ manager: Account(ZERO_ADDRESS),
3535
+ freeze: Account(ZERO_ADDRESS),
3536
+ clawback: Account(ZERO_ADDRESS),
3537
+ creator: lazyContext.defaultSender,
3538
+ reserve: Account(ZERO_ADDRESS),
3539
+ };
3540
+ const { assetId: _, ...assetData } = input ?? {};
3541
+ lazyContext.ledger.assetDataMap.set(assetId.asBigInt(), {
3542
+ ...defaultAssetData,
3543
+ ...assetData,
3544
+ });
3545
+ return new AssetCls(assetId.asAlgoTs());
3546
+ }
3547
+ application(input) {
3548
+ const id = input?.applicationId;
3549
+ if (id && lazyContext.ledger.applicationDataMap.has(asBigInt(id))) {
3550
+ internal.errors.internalError('Application with such ID already exists in testing context!');
3551
+ }
3552
+ const applicationId = asUint64Cls(id ?? lazyContext.ledger.appIdIter.next().value);
3553
+ const data = new ApplicationData();
3554
+ const { applicationId: _, ...applicationData } = input ?? {};
3555
+ data.application = {
3556
+ ...data.application,
3557
+ ...applicationData,
3558
+ };
3559
+ lazyContext.ledger.applicationDataMap.set(applicationId.asBigInt(), data);
3560
+ return new ApplicationCls(applicationId.asAlgoTs());
3561
+ }
3562
+ }
3563
+
3564
+ class TxnValueGenerator {
3565
+ applicationCall(fields) {
3566
+ const params = fields ?? {};
3567
+ if (params.appId && !lazyContext.ledger.applicationDataMap.has(asBigInt(params.appId.id))) {
3568
+ internal.errors.internalError(`Application ID ${params.appId.id} not found in test context`);
3569
+ }
3570
+ if (!params.appId) {
3571
+ params.appId = lazyContext.any.application();
3572
+ }
3573
+ return ApplicationTransaction.create(params);
3574
+ }
3575
+ payment(fields) {
3576
+ return PaymentTransaction.create(fields ?? {});
3577
+ }
3578
+ keyRegistration(fields) {
3579
+ return KeyRegistrationTransaction.create(fields ?? {});
3580
+ }
3581
+ assetConfig(fields) {
3582
+ return AssetConfigTransaction.create(fields ?? {});
3583
+ }
3584
+ assetTransfer(fields) {
3585
+ return AssetTransferTransaction.create(fields ?? {});
3586
+ }
3587
+ assetFreeze(fields) {
3588
+ return AssetFreezeTransaction.create(fields ?? {});
3589
+ }
3590
+ }
3591
+
3592
+ class ValueGenerator extends AvmValueGenerator {
3593
+ txn;
3594
+ constructor() {
3595
+ super();
3596
+ this.txn = new TxnValueGenerator();
3597
+ }
3598
+ }
3599
+
3600
+ class TestExecutionContext {
3601
+ #applicationLogs;
3602
+ #contractContext;
3603
+ #ledgerContext;
3604
+ #txnContext;
3605
+ #valueGenerator;
3606
+ #defaultSender;
3607
+ constructor() {
3608
+ internal.ctxMgr.instance = this;
3609
+ this.#applicationLogs = new Map();
3610
+ this.#contractContext = new ContractContext();
3611
+ this.#ledgerContext = new LedgerContext();
3612
+ this.#txnContext = new TransactionContext();
3613
+ this.#valueGenerator = new ValueGenerator();
3614
+ this.#defaultSender = Account(Bytes(algosdk.generateAccount().addr));
3615
+ }
3616
+ account(address) {
3617
+ return new AccountCls(address);
3618
+ }
3619
+ application(id) {
3620
+ return new ApplicationCls(id);
3621
+ }
3622
+ asset(id) {
3623
+ return new AssetCls(id);
3624
+ }
3625
+ log(value) {
3626
+ this.txn.appendLog(value);
3627
+ }
3628
+ exportLogs(appId, ...decoding) {
3629
+ return this.txn.exportLogs(appId, ...decoding);
3630
+ }
3631
+ get op() {
3632
+ return ops;
3633
+ }
3634
+ get contract() {
3635
+ return this.#contractContext;
3636
+ }
3637
+ get ledger() {
3638
+ return this.#ledgerContext;
3639
+ }
3640
+ get txn() {
3641
+ return this.#txnContext;
3642
+ }
3643
+ get any() {
3644
+ return this.#valueGenerator;
3645
+ }
3646
+ get defaultSender() {
3647
+ return this.#defaultSender;
3648
+ }
3649
+ get abiMetadata() {
3650
+ return {
3651
+ captureMethodConfig,
3652
+ };
3653
+ }
3654
+ get gtxn() {
3655
+ return {
3656
+ Transaction: (index) => this.txn.activeGroup.getTransaction(index),
3657
+ PaymentTxn: (index) => this.txn.activeGroup.getPaymentTransaction(index),
3658
+ KeyRegistrationTxn: (index) => this.txn.activeGroup.getKeyRegistrationTransaction(index),
3659
+ AssetConfigTxn: (index) => this.txn.activeGroup.getAssetConfigTransaction(index),
3660
+ AssetTransferTxn: (index) => this.txn.activeGroup.getAssetTransferTransaction(index),
3661
+ AssetFreezeTxn: (index) => this.txn.activeGroup.getAssetFreezeTransaction(index),
3662
+ ApplicationTxn: (index) => this.txn.activeGroup.getApplicationTransaction(index),
3663
+ };
3664
+ }
3665
+ get itxn() {
3666
+ return {
3667
+ submitGroup: submitGroup,
3668
+ payment: payment,
3669
+ keyRegistration: keyRegistration,
3670
+ assetConfig: assetConfig,
3671
+ assetTransfer: assetTransfer,
3672
+ assetFreeze: assetFreeze,
3673
+ applicationCall: applicationCall,
3674
+ };
3675
+ }
3676
+ reset() {
3677
+ this.#applicationLogs.clear();
3678
+ this.#contractContext = new ContractContext();
3679
+ this.#ledgerContext = new LedgerContext();
3680
+ this.#txnContext = new TransactionContext();
3681
+ internal.ctxMgr.reset();
3682
+ internal.ctxMgr.instance = this;
3683
+ }
3684
+ }
3685
+
3686
+ export { TestExecutionContext };
3687
+ //# sourceMappingURL=index.mjs.map