@algorandfoundation/algokit-utils 9.1.1-beta.2 → 9.1.1-beta.3
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.
- package/package.json +1 -1
- package/transaction/transaction.d.ts +2 -2
- package/transaction/transaction.js +7 -5
- package/transaction/transaction.js.map +1 -1
- package/transaction/transaction.mjs +8 -6
- package/transaction/transaction.mjs.map +1 -1
- package/types/algorand-client-transaction-sender.d.ts +0 -78
- package/types/app-arc56.js +12 -3
- package/types/app-arc56.js.map +1 -1
- package/types/app-arc56.mjs +12 -3
- package/types/app-arc56.mjs.map +1 -1
- package/types/app-client.js +2 -0
- package/types/app-client.js.map +1 -1
- package/types/app-client.mjs +2 -0
- package/types/app-client.mjs.map +1 -1
- package/types/app-manager.js +1 -1
- package/types/app-manager.js.map +1 -1
- package/types/app-manager.mjs +1 -1
- package/types/app-manager.mjs.map +1 -1
- package/types/composer.js +2 -1
- package/types/composer.js.map +1 -1
- package/types/composer.mjs +2 -1
- package/types/composer.mjs.map +1 -1
- package/util.d.ts +3 -0
- package/util.js +33 -0
- package/util.js.map +1 -1
- package/util.mjs +33 -1
- package/util.mjs.map +1 -1
package/util.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var types_app = require('./types/app.js');
|
|
4
|
+
var algosdk = require('algosdk');
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Converts a value which might be a number or a bigint into a number to be used with apis that don't support bigint.
|
|
@@ -87,6 +88,37 @@ const asJson = (value, replacer = defaultJsonValueReplacer, space) => {
|
|
|
87
88
|
const calculateExtraProgramPages = (approvalProgram, clearStateProgram) => {
|
|
88
89
|
return Math.floor((approvalProgram.length + (clearStateProgram?.length ?? 0) - 1) / types_app.APP_PAGE_MAX_SIZE);
|
|
89
90
|
};
|
|
91
|
+
/** Take a decoded ABI value and convert all byte arrays (including nested ones) from number[] to Uint8Arrays */
|
|
92
|
+
function convertAbiByteArrays(value, type) {
|
|
93
|
+
// Return value as is if the type doesn't have any bytes or if it's already an Uint8Array
|
|
94
|
+
if (!type.toString().includes('byte') || value instanceof Uint8Array) {
|
|
95
|
+
return value;
|
|
96
|
+
}
|
|
97
|
+
// Handle byte arrays (byte[N] or byte[])
|
|
98
|
+
if ((type instanceof algosdk.ABIArrayStaticType || type instanceof algosdk.ABIArrayDynamicType) &&
|
|
99
|
+
type.childType instanceof algosdk.ABIByteType &&
|
|
100
|
+
Array.isArray(value)) {
|
|
101
|
+
return new Uint8Array(value);
|
|
102
|
+
}
|
|
103
|
+
// Handle other arrays (for nested structures)
|
|
104
|
+
if ((type instanceof algosdk.ABIArrayStaticType || type instanceof algosdk.ABIArrayDynamicType) && Array.isArray(value)) {
|
|
105
|
+
const result = [];
|
|
106
|
+
for (let i = 0; i < value.length; i++) {
|
|
107
|
+
result.push(convertAbiByteArrays(value[i], type.childType));
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
// Handle tuples (for nested structures)
|
|
112
|
+
if (type instanceof algosdk.ABITupleType && Array.isArray(value)) {
|
|
113
|
+
const result = [];
|
|
114
|
+
for (let i = 0; i < value.length && i < type.childTypes.length; i++) {
|
|
115
|
+
result.push(convertAbiByteArrays(value[i], type.childTypes[i]));
|
|
116
|
+
}
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
// For other types, return the value as is
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
90
122
|
|
|
91
123
|
exports.UnsafeConversionError = UnsafeConversionError;
|
|
92
124
|
exports.asJson = asJson;
|
|
@@ -94,6 +126,7 @@ exports.binaryStartsWith = binaryStartsWith;
|
|
|
94
126
|
exports.calculateExtraProgramPages = calculateExtraProgramPages;
|
|
95
127
|
exports.calculateFundAmount = calculateFundAmount;
|
|
96
128
|
exports.chunkArray = chunkArray;
|
|
129
|
+
exports.convertAbiByteArrays = convertAbiByteArrays;
|
|
97
130
|
exports.defaultJsonValueReplacer = defaultJsonValueReplacer;
|
|
98
131
|
exports.memoize = memoize;
|
|
99
132
|
exports.toNumber = toNumber;
|
package/util.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.js","sources":["../src/util.ts"],"sourcesContent":["import { APP_PAGE_MAX_SIZE } from './types/app'\n\n/**\n * Converts a value which might be a number or a bigint into a number to be used with apis that don't support bigint.\n *\n * Throws an UnsafeConversionError if the conversion would result in an unsafe integer for the Number type\n * @param value\n */\nexport const toNumber = (value: number | bigint) => {\n if (typeof value === 'number') return value\n\n if (value > BigInt(Number.MAX_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is larger than the maximum safe integer the Number type can hold.`,\n )\n } else if (value < BigInt(Number.MIN_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is smaller than the minimum safe integer the Number type can hold.`,\n )\n }\n return Number(value)\n}\n\nexport class UnsafeConversionError extends Error {}\n\n/**\n * Calculates the amount of funds to add to a wallet to bring it up to the minimum spending balance.\n * @param minSpendingBalance The minimum spending balance for the wallet\n * @param currentSpendingBalance The current spending balance for the wallet\n * @param minFundingIncrement The minimum amount of funds that can be added to the wallet\n * @returns The amount of funds to add to the wallet or null if the wallet is already above the minimum spending balance\n */\nexport const calculateFundAmount = (\n minSpendingBalance: bigint,\n currentSpendingBalance: bigint,\n minFundingIncrement: bigint,\n): bigint | null => {\n if (minSpendingBalance > currentSpendingBalance) {\n const minFundAmount = minSpendingBalance - currentSpendingBalance\n return BigInt(Math.max(Number(minFundAmount), Number(minFundingIncrement)))\n } else {\n return null\n }\n}\n\n/**\n * Checks if the current environment is Node.js\n *\n * @returns A boolean indicating whether the current environment is Node.js\n */\nexport const isNode = () => {\n return typeof process !== 'undefined' && process.versions != null && process.versions.node != null\n}\n\n/**\n * Returns the given array split into chunks of `batchSize` batches.\n * @param array The array to chunk\n * @param batchSize The size of batches to split the array into\n * @returns A generator that yields the array split into chunks of `batchSize` batches\n */\nexport function* chunkArray<T>(array: T[], batchSize: number): Generator<T[], void> {\n for (let i = 0; i < array.length; i += batchSize) yield array.slice(i, i + batchSize)\n}\n\n/**\n * Memoize calls to the given function in an in-memory map.\n * @param fn The function to memoize\n * @returns The memoized function\n */\nexport const memoize = <T = unknown, R = unknown>(fn: (val: T) => R) => {\n const cache = new Map()\n const cached = function (this: unknown, val: T) {\n return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val)\n }\n cached.cache = cache\n return cached as (val: T) => R\n}\n\nexport const binaryStartsWith = (base: Uint8Array, startsWith: Uint8Array): boolean => {\n if (startsWith.length > base.length) return false\n for (let i = 0; i < startsWith.length; i++) {\n if (base[i] !== startsWith[i]) return false\n }\n return true\n}\n\nexport const defaultJsonValueReplacer = (key: string, value: unknown) => {\n if (typeof value === 'bigint') {\n try {\n return toNumber(value)\n } catch {\n return value.toString()\n }\n }\n return value\n}\nexport const asJson = (\n value: unknown,\n replacer: (key: string, value: unknown) => unknown = defaultJsonValueReplacer,\n space?: string | number,\n) => {\n return JSON.stringify(value, replacer, space)\n}\n\n/** Calculate minimum number of extra program pages required for provided approval and clear state programs */\nexport const calculateExtraProgramPages = (approvalProgram: Uint8Array, clearStateProgram?: Uint8Array): number => {\n return Math.floor((approvalProgram.length + (clearStateProgram?.length ?? 0) - 1) / APP_PAGE_MAX_SIZE)\n}\n"],"names":["APP_PAGE_MAX_SIZE"],"mappings":"
|
|
1
|
+
{"version":3,"file":"util.js","sources":["../src/util.ts"],"sourcesContent":["import { APP_PAGE_MAX_SIZE } from './types/app'\nimport { ABIArrayDynamicType, ABIArrayStaticType, ABIByteType, ABIReturnType, ABITupleType, ABIValue } from 'algosdk'\n\n/**\n * Converts a value which might be a number or a bigint into a number to be used with apis that don't support bigint.\n *\n * Throws an UnsafeConversionError if the conversion would result in an unsafe integer for the Number type\n * @param value\n */\nexport const toNumber = (value: number | bigint) => {\n if (typeof value === 'number') return value\n\n if (value > BigInt(Number.MAX_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is larger than the maximum safe integer the Number type can hold.`,\n )\n } else if (value < BigInt(Number.MIN_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is smaller than the minimum safe integer the Number type can hold.`,\n )\n }\n return Number(value)\n}\n\nexport class UnsafeConversionError extends Error {}\n\n/**\n * Calculates the amount of funds to add to a wallet to bring it up to the minimum spending balance.\n * @param minSpendingBalance The minimum spending balance for the wallet\n * @param currentSpendingBalance The current spending balance for the wallet\n * @param minFundingIncrement The minimum amount of funds that can be added to the wallet\n * @returns The amount of funds to add to the wallet or null if the wallet is already above the minimum spending balance\n */\nexport const calculateFundAmount = (\n minSpendingBalance: bigint,\n currentSpendingBalance: bigint,\n minFundingIncrement: bigint,\n): bigint | null => {\n if (minSpendingBalance > currentSpendingBalance) {\n const minFundAmount = minSpendingBalance - currentSpendingBalance\n return BigInt(Math.max(Number(minFundAmount), Number(minFundingIncrement)))\n } else {\n return null\n }\n}\n\n/**\n * Checks if the current environment is Node.js\n *\n * @returns A boolean indicating whether the current environment is Node.js\n */\nexport const isNode = () => {\n return typeof process !== 'undefined' && process.versions != null && process.versions.node != null\n}\n\n/**\n * Returns the given array split into chunks of `batchSize` batches.\n * @param array The array to chunk\n * @param batchSize The size of batches to split the array into\n * @returns A generator that yields the array split into chunks of `batchSize` batches\n */\nexport function* chunkArray<T>(array: T[], batchSize: number): Generator<T[], void> {\n for (let i = 0; i < array.length; i += batchSize) yield array.slice(i, i + batchSize)\n}\n\n/**\n * Memoize calls to the given function in an in-memory map.\n * @param fn The function to memoize\n * @returns The memoized function\n */\nexport const memoize = <T = unknown, R = unknown>(fn: (val: T) => R) => {\n const cache = new Map()\n const cached = function (this: unknown, val: T) {\n return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val)\n }\n cached.cache = cache\n return cached as (val: T) => R\n}\n\nexport const binaryStartsWith = (base: Uint8Array, startsWith: Uint8Array): boolean => {\n if (startsWith.length > base.length) return false\n for (let i = 0; i < startsWith.length; i++) {\n if (base[i] !== startsWith[i]) return false\n }\n return true\n}\n\nexport const defaultJsonValueReplacer = (key: string, value: unknown) => {\n if (typeof value === 'bigint') {\n try {\n return toNumber(value)\n } catch {\n return value.toString()\n }\n }\n return value\n}\nexport const asJson = (\n value: unknown,\n replacer: (key: string, value: unknown) => unknown = defaultJsonValueReplacer,\n space?: string | number,\n) => {\n return JSON.stringify(value, replacer, space)\n}\n\n/** Calculate minimum number of extra program pages required for provided approval and clear state programs */\nexport const calculateExtraProgramPages = (approvalProgram: Uint8Array, clearStateProgram?: Uint8Array): number => {\n return Math.floor((approvalProgram.length + (clearStateProgram?.length ?? 0) - 1) / APP_PAGE_MAX_SIZE)\n}\n\n/** Take a decoded ABI value and convert all byte arrays (including nested ones) from number[] to Uint8Arrays */\nexport function convertAbiByteArrays(value: ABIValue, type: ABIReturnType): ABIValue {\n // Return value as is if the type doesn't have any bytes or if it's already an Uint8Array\n if (!type.toString().includes('byte') || value instanceof Uint8Array) {\n return value\n }\n\n // Handle byte arrays (byte[N] or byte[])\n if (\n (type instanceof ABIArrayStaticType || type instanceof ABIArrayDynamicType) &&\n type.childType instanceof ABIByteType &&\n Array.isArray(value)\n ) {\n return new Uint8Array(value as number[])\n }\n\n // Handle other arrays (for nested structures)\n if ((type instanceof ABIArrayStaticType || type instanceof ABIArrayDynamicType) && Array.isArray(value)) {\n const result = []\n for (let i = 0; i < value.length; i++) {\n result.push(convertAbiByteArrays(value[i], type.childType))\n }\n return result\n }\n\n // Handle tuples (for nested structures)\n if (type instanceof ABITupleType && Array.isArray(value)) {\n const result = []\n for (let i = 0; i < value.length && i < type.childTypes.length; i++) {\n result.push(convertAbiByteArrays(value[i], type.childTypes[i]))\n }\n return result\n }\n\n // For other types, return the value as is\n return value\n}\n"],"names":["APP_PAGE_MAX_SIZE","ABIArrayStaticType","ABIArray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AC,oBAAoB,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;;AAEjE,QAAA,OAAO,MAAM;;;AAIf,IAAA,OAAO,KAAK;AACd;;;;;;;;;;;;;"}
|
package/util.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { APP_PAGE_MAX_SIZE } from './types/app.mjs';
|
|
2
|
+
import { ABIArrayStaticType, ABIArrayDynamicType, ABIByteType, ABITupleType } from 'algosdk';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Converts a value which might be a number or a bigint into a number to be used with apis that don't support bigint.
|
|
@@ -85,6 +86,37 @@ const asJson = (value, replacer = defaultJsonValueReplacer, space) => {
|
|
|
85
86
|
const calculateExtraProgramPages = (approvalProgram, clearStateProgram) => {
|
|
86
87
|
return Math.floor((approvalProgram.length + (clearStateProgram?.length ?? 0) - 1) / APP_PAGE_MAX_SIZE);
|
|
87
88
|
};
|
|
89
|
+
/** Take a decoded ABI value and convert all byte arrays (including nested ones) from number[] to Uint8Arrays */
|
|
90
|
+
function convertAbiByteArrays(value, type) {
|
|
91
|
+
// Return value as is if the type doesn't have any bytes or if it's already an Uint8Array
|
|
92
|
+
if (!type.toString().includes('byte') || value instanceof Uint8Array) {
|
|
93
|
+
return value;
|
|
94
|
+
}
|
|
95
|
+
// Handle byte arrays (byte[N] or byte[])
|
|
96
|
+
if ((type instanceof ABIArrayStaticType || type instanceof ABIArrayDynamicType) &&
|
|
97
|
+
type.childType instanceof ABIByteType &&
|
|
98
|
+
Array.isArray(value)) {
|
|
99
|
+
return new Uint8Array(value);
|
|
100
|
+
}
|
|
101
|
+
// Handle other arrays (for nested structures)
|
|
102
|
+
if ((type instanceof ABIArrayStaticType || type instanceof ABIArrayDynamicType) && Array.isArray(value)) {
|
|
103
|
+
const result = [];
|
|
104
|
+
for (let i = 0; i < value.length; i++) {
|
|
105
|
+
result.push(convertAbiByteArrays(value[i], type.childType));
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
// Handle tuples (for nested structures)
|
|
110
|
+
if (type instanceof ABITupleType && Array.isArray(value)) {
|
|
111
|
+
const result = [];
|
|
112
|
+
for (let i = 0; i < value.length && i < type.childTypes.length; i++) {
|
|
113
|
+
result.push(convertAbiByteArrays(value[i], type.childTypes[i]));
|
|
114
|
+
}
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
// For other types, return the value as is
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
88
120
|
|
|
89
|
-
export { UnsafeConversionError, asJson, binaryStartsWith, calculateExtraProgramPages, calculateFundAmount, chunkArray, defaultJsonValueReplacer, memoize, toNumber };
|
|
121
|
+
export { UnsafeConversionError, asJson, binaryStartsWith, calculateExtraProgramPages, calculateFundAmount, chunkArray, convertAbiByteArrays, defaultJsonValueReplacer, memoize, toNumber };
|
|
90
122
|
//# sourceMappingURL=util.mjs.map
|
package/util.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.mjs","sources":["../src/util.ts"],"sourcesContent":["import { APP_PAGE_MAX_SIZE } from './types/app'\n\n/**\n * Converts a value which might be a number or a bigint into a number to be used with apis that don't support bigint.\n *\n * Throws an UnsafeConversionError if the conversion would result in an unsafe integer for the Number type\n * @param value\n */\nexport const toNumber = (value: number | bigint) => {\n if (typeof value === 'number') return value\n\n if (value > BigInt(Number.MAX_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is larger than the maximum safe integer the Number type can hold.`,\n )\n } else if (value < BigInt(Number.MIN_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is smaller than the minimum safe integer the Number type can hold.`,\n )\n }\n return Number(value)\n}\n\nexport class UnsafeConversionError extends Error {}\n\n/**\n * Calculates the amount of funds to add to a wallet to bring it up to the minimum spending balance.\n * @param minSpendingBalance The minimum spending balance for the wallet\n * @param currentSpendingBalance The current spending balance for the wallet\n * @param minFundingIncrement The minimum amount of funds that can be added to the wallet\n * @returns The amount of funds to add to the wallet or null if the wallet is already above the minimum spending balance\n */\nexport const calculateFundAmount = (\n minSpendingBalance: bigint,\n currentSpendingBalance: bigint,\n minFundingIncrement: bigint,\n): bigint | null => {\n if (minSpendingBalance > currentSpendingBalance) {\n const minFundAmount = minSpendingBalance - currentSpendingBalance\n return BigInt(Math.max(Number(minFundAmount), Number(minFundingIncrement)))\n } else {\n return null\n }\n}\n\n/**\n * Checks if the current environment is Node.js\n *\n * @returns A boolean indicating whether the current environment is Node.js\n */\nexport const isNode = () => {\n return typeof process !== 'undefined' && process.versions != null && process.versions.node != null\n}\n\n/**\n * Returns the given array split into chunks of `batchSize` batches.\n * @param array The array to chunk\n * @param batchSize The size of batches to split the array into\n * @returns A generator that yields the array split into chunks of `batchSize` batches\n */\nexport function* chunkArray<T>(array: T[], batchSize: number): Generator<T[], void> {\n for (let i = 0; i < array.length; i += batchSize) yield array.slice(i, i + batchSize)\n}\n\n/**\n * Memoize calls to the given function in an in-memory map.\n * @param fn The function to memoize\n * @returns The memoized function\n */\nexport const memoize = <T = unknown, R = unknown>(fn: (val: T) => R) => {\n const cache = new Map()\n const cached = function (this: unknown, val: T) {\n return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val)\n }\n cached.cache = cache\n return cached as (val: T) => R\n}\n\nexport const binaryStartsWith = (base: Uint8Array, startsWith: Uint8Array): boolean => {\n if (startsWith.length > base.length) return false\n for (let i = 0; i < startsWith.length; i++) {\n if (base[i] !== startsWith[i]) return false\n }\n return true\n}\n\nexport const defaultJsonValueReplacer = (key: string, value: unknown) => {\n if (typeof value === 'bigint') {\n try {\n return toNumber(value)\n } catch {\n return value.toString()\n }\n }\n return value\n}\nexport const asJson = (\n value: unknown,\n replacer: (key: string, value: unknown) => unknown = defaultJsonValueReplacer,\n space?: string | number,\n) => {\n return JSON.stringify(value, replacer, space)\n}\n\n/** Calculate minimum number of extra program pages required for provided approval and clear state programs */\nexport const calculateExtraProgramPages = (approvalProgram: Uint8Array, clearStateProgram?: Uint8Array): number => {\n return Math.floor((approvalProgram.length + (clearStateProgram?.length ?? 0) - 1) / APP_PAGE_MAX_SIZE)\n}\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"util.mjs","sources":["../src/util.ts"],"sourcesContent":["import { APP_PAGE_MAX_SIZE } from './types/app'\nimport { ABIArrayDynamicType, ABIArrayStaticType, ABIByteType, ABIReturnType, ABITupleType, ABIValue } from 'algosdk'\n\n/**\n * Converts a value which might be a number or a bigint into a number to be used with apis that don't support bigint.\n *\n * Throws an UnsafeConversionError if the conversion would result in an unsafe integer for the Number type\n * @param value\n */\nexport const toNumber = (value: number | bigint) => {\n if (typeof value === 'number') return value\n\n if (value > BigInt(Number.MAX_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is larger than the maximum safe integer the Number type can hold.`,\n )\n } else if (value < BigInt(Number.MIN_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is smaller than the minimum safe integer the Number type can hold.`,\n )\n }\n return Number(value)\n}\n\nexport class UnsafeConversionError extends Error {}\n\n/**\n * Calculates the amount of funds to add to a wallet to bring it up to the minimum spending balance.\n * @param minSpendingBalance The minimum spending balance for the wallet\n * @param currentSpendingBalance The current spending balance for the wallet\n * @param minFundingIncrement The minimum amount of funds that can be added to the wallet\n * @returns The amount of funds to add to the wallet or null if the wallet is already above the minimum spending balance\n */\nexport const calculateFundAmount = (\n minSpendingBalance: bigint,\n currentSpendingBalance: bigint,\n minFundingIncrement: bigint,\n): bigint | null => {\n if (minSpendingBalance > currentSpendingBalance) {\n const minFundAmount = minSpendingBalance - currentSpendingBalance\n return BigInt(Math.max(Number(minFundAmount), Number(minFundingIncrement)))\n } else {\n return null\n }\n}\n\n/**\n * Checks if the current environment is Node.js\n *\n * @returns A boolean indicating whether the current environment is Node.js\n */\nexport const isNode = () => {\n return typeof process !== 'undefined' && process.versions != null && process.versions.node != null\n}\n\n/**\n * Returns the given array split into chunks of `batchSize` batches.\n * @param array The array to chunk\n * @param batchSize The size of batches to split the array into\n * @returns A generator that yields the array split into chunks of `batchSize` batches\n */\nexport function* chunkArray<T>(array: T[], batchSize: number): Generator<T[], void> {\n for (let i = 0; i < array.length; i += batchSize) yield array.slice(i, i + batchSize)\n}\n\n/**\n * Memoize calls to the given function in an in-memory map.\n * @param fn The function to memoize\n * @returns The memoized function\n */\nexport const memoize = <T = unknown, R = unknown>(fn: (val: T) => R) => {\n const cache = new Map()\n const cached = function (this: unknown, val: T) {\n return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val)\n }\n cached.cache = cache\n return cached as (val: T) => R\n}\n\nexport const binaryStartsWith = (base: Uint8Array, startsWith: Uint8Array): boolean => {\n if (startsWith.length > base.length) return false\n for (let i = 0; i < startsWith.length; i++) {\n if (base[i] !== startsWith[i]) return false\n }\n return true\n}\n\nexport const defaultJsonValueReplacer = (key: string, value: unknown) => {\n if (typeof value === 'bigint') {\n try {\n return toNumber(value)\n } catch {\n return value.toString()\n }\n }\n return value\n}\nexport const asJson = (\n value: unknown,\n replacer: (key: string, value: unknown) => unknown = defaultJsonValueReplacer,\n space?: string | number,\n) => {\n return JSON.stringify(value, replacer, space)\n}\n\n/** Calculate minimum number of extra program pages required for provided approval and clear state programs */\nexport const calculateExtraProgramPages = (approvalProgram: Uint8Array, clearStateProgram?: Uint8Array): number => {\n return Math.floor((approvalProgram.length + (clearStateProgram?.length ?? 0) - 1) / APP_PAGE_MAX_SIZE)\n}\n\n/** Take a decoded ABI value and convert all byte arrays (including nested ones) from number[] to Uint8Arrays */\nexport function convertAbiByteArrays(value: ABIValue, type: ABIReturnType): ABIValue {\n // Return value as is if the type doesn't have any bytes or if it's already an Uint8Array\n if (!type.toString().includes('byte') || value instanceof Uint8Array) {\n return value\n }\n\n // Handle byte arrays (byte[N] or byte[])\n if (\n (type instanceof ABIArrayStaticType || type instanceof ABIArrayDynamicType) &&\n type.childType instanceof ABIByteType &&\n Array.isArray(value)\n ) {\n return new Uint8Array(value as number[])\n }\n\n // Handle other arrays (for nested structures)\n if ((type instanceof ABIArrayStaticType || type instanceof ABIArrayDynamicType) && Array.isArray(value)) {\n const result = []\n for (let i = 0; i < value.length; i++) {\n result.push(convertAbiByteArrays(value[i], type.childType))\n }\n return result\n }\n\n // Handle tuples (for nested structures)\n if (type instanceof ABITupleType && Array.isArray(value)) {\n const result = []\n for (let i = 0; i < value.length && i < type.childTypes.length; i++) {\n result.push(convertAbiByteArrays(value[i], type.childTypes[i]))\n }\n return result\n }\n\n // For other types, return the value as is\n return value\n}\n"],"names":[],"mappings":";;;AAGA;;;;;AAKG;AACU,MAAA,QAAQ,GAAG,CAAC,KAAsB,KAAI;IACjD,IAAI,OAAO,KAAK,KAAK,QAAQ;AAAE,QAAA,OAAO,KAAK;IAE3C,IAAI,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE;AAC3C,QAAA,MAAM,IAAI,qBAAqB,CAC7B,kBAAkB,KAAK,CAAA,oFAAA,CAAsF,CAC9G;;SACI,IAAI,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE;AAClD,QAAA,MAAM,IAAI,qBAAqB,CAC7B,kBAAkB,KAAK,CAAA,qFAAA,CAAuF,CAC/G;;AAEH,IAAA,OAAO,MAAM,CAAC,KAAK,CAAC;AACtB;AAEM,MAAO,qBAAsB,SAAQ,KAAK,CAAA;AAAG;AAEnD;;;;;;AAMG;AACU,MAAA,mBAAmB,GAAG,CACjC,kBAA0B,EAC1B,sBAA8B,EAC9B,mBAA2B,KACV;AACjB,IAAA,IAAI,kBAAkB,GAAG,sBAAsB,EAAE;AAC/C,QAAA,MAAM,aAAa,GAAG,kBAAkB,GAAG,sBAAsB;AACjE,QAAA,OAAO,MAAM,CAAC,IAAI,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