@algorandfoundation/algokit-utils 7.0.0-beta.9 → 8.0.0-beta.1
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/LICENSE +21 -0
- package/README.md +21 -3
- package/account/account.d.ts +4 -1
- package/account/account.js +10 -7
- package/account/account.js.map +1 -1
- package/account/account.mjs +11 -8
- package/account/account.mjs.map +1 -1
- package/account/get-account-config-from-environment.js.map +1 -1
- package/account/get-account-config-from-environment.mjs.map +1 -1
- package/account/get-account.js.map +1 -1
- package/account/get-account.mjs.map +1 -1
- package/account/get-dispenser-account.d.ts +1 -1
- package/account/get-dispenser-account.js.map +1 -1
- package/account/get-dispenser-account.mjs.map +1 -1
- package/account/mnemonic-account.js.map +1 -1
- package/account/mnemonic-account.mjs.map +1 -1
- package/amount.d.ts +1 -0
- package/amount.js +3 -2
- package/amount.js.map +1 -1
- package/amount.mjs +3 -3
- package/amount.mjs.map +1 -1
- package/app-client.d.ts +4 -4
- package/app-client.js +4 -4
- package/app-client.js.map +1 -1
- package/app-client.mjs +4 -4
- package/app-client.mjs.map +1 -1
- package/app-deploy.js +25 -15
- package/app-deploy.js.map +1 -1
- package/app-deploy.mjs +27 -17
- package/app-deploy.mjs.map +1 -1
- package/app.d.ts +2 -2
- package/app.js +9 -6
- package/app.js.map +1 -1
- package/app.mjs +9 -6
- package/app.mjs.map +1 -1
- package/asset.js.map +1 -1
- package/asset.mjs.map +1 -1
- package/debugging/debugging.js.map +1 -1
- package/debugging/debugging.mjs.map +1 -1
- package/dispenser-client.js.map +1 -1
- package/dispenser-client.mjs.map +1 -1
- package/index.d.ts +1 -0
- package/index.js +7 -1
- package/index.js.map +1 -1
- package/index.mjs +3 -2
- package/index.mjs.map +1 -1
- package/indexer-lookup.d.ts +10 -8
- package/indexer-lookup.js +14 -10
- package/indexer-lookup.js.map +1 -1
- package/indexer-lookup.mjs +14 -10
- package/indexer-lookup.mjs.map +1 -1
- package/localnet/get-kmd-wallet-account.js.map +1 -1
- package/localnet/get-kmd-wallet-account.mjs.map +1 -1
- package/localnet/get-localnet-dispenser-account.js.map +1 -1
- package/localnet/get-localnet-dispenser-account.mjs.map +1 -1
- package/localnet/get-or-create-kmd-wallet-account.js.map +1 -1
- package/localnet/get-or-create-kmd-wallet-account.mjs.map +1 -1
- package/localnet/is-localnet.js.map +1 -1
- package/localnet/is-localnet.mjs.map +1 -1
- package/network-client.d.ts +1 -7
- package/network-client.js +2 -9
- package/network-client.js.map +1 -1
- package/network-client.mjs +2 -9
- package/network-client.mjs.map +1 -1
- package/package.json +5 -5
- package/testing/_asset.d.ts +2 -1
- package/testing/account.d.ts +4 -3
- package/testing/account.js +8 -2
- package/testing/account.js.map +1 -1
- package/testing/account.mjs +9 -3
- package/testing/account.mjs.map +1 -1
- package/testing/fixtures/algokit-log-capture-fixture.js.map +1 -1
- package/testing/fixtures/algokit-log-capture-fixture.mjs.map +1 -1
- package/testing/fixtures/algorand-fixture.js +5 -6
- package/testing/fixtures/algorand-fixture.js.map +1 -1
- package/testing/fixtures/algorand-fixture.mjs +5 -6
- package/testing/fixtures/algorand-fixture.mjs.map +1 -1
- package/testing/indexer.js.map +1 -1
- package/testing/indexer.mjs.map +1 -1
- package/testing/test-logger.js +7 -1
- package/testing/test-logger.js.map +1 -1
- package/testing/test-logger.mjs +7 -1
- package/testing/test-logger.mjs.map +1 -1
- package/testing/transaction-logger.js.map +1 -1
- package/testing/transaction-logger.mjs.map +1 -1
- package/transaction/legacy-bridge.js +2 -2
- package/transaction/legacy-bridge.js.map +1 -1
- package/transaction/legacy-bridge.mjs +3 -3
- package/transaction/legacy-bridge.mjs.map +1 -1
- package/transaction/perform-atomic-transaction-composer-simulate.d.ts +4 -1
- package/transaction/perform-atomic-transaction-composer-simulate.js +14 -9
- package/transaction/perform-atomic-transaction-composer-simulate.js.map +1 -1
- package/transaction/perform-atomic-transaction-composer-simulate.mjs +15 -10
- package/transaction/perform-atomic-transaction-composer-simulate.mjs.map +1 -1
- package/transaction/transaction.d.ts +13 -22
- package/transaction/transaction.js +164 -110
- package/transaction/transaction.js.map +1 -1
- package/transaction/transaction.mjs +163 -109
- package/transaction/transaction.mjs.map +1 -1
- package/transfer/transfer-algos.js.map +1 -1
- package/transfer/transfer-algos.mjs.map +1 -1
- package/transfer/transfer.js +3 -1
- package/transfer/transfer.js.map +1 -1
- package/transfer/transfer.mjs +3 -1
- package/transfer/transfer.mjs.map +1 -1
- package/types/account-manager.d.ts +20 -20
- package/types/account-manager.js +36 -27
- package/types/account-manager.js.map +1 -1
- package/types/account-manager.mjs +38 -29
- package/types/account-manager.mjs.map +1 -1
- package/types/account.d.ts +17 -8
- package/types/account.js +2 -2
- package/types/account.js.map +1 -1
- package/types/account.mjs +3 -3
- package/types/account.mjs.map +1 -1
- package/types/algo-http-client-with-retry.d.ts +1 -2
- package/types/algo-http-client-with-retry.js +33 -3
- package/types/algo-http-client-with-retry.js.map +1 -1
- package/types/algo-http-client-with-retry.mjs +32 -2
- package/types/algo-http-client-with-retry.mjs.map +1 -1
- package/types/algorand-client-interface.d.ts +2 -2
- package/types/algorand-client-transaction-creator.d.ts +16 -14
- package/types/algorand-client-transaction-creator.js +3 -1
- package/types/algorand-client-transaction-creator.js.map +1 -1
- package/types/algorand-client-transaction-creator.mjs +3 -1
- package/types/algorand-client-transaction-creator.mjs.map +1 -1
- package/types/algorand-client-transaction-sender.d.ts +109 -105
- package/types/algorand-client-transaction-sender.js +6 -2
- package/types/algorand-client-transaction-sender.js.map +1 -1
- package/types/algorand-client-transaction-sender.mjs +6 -2
- package/types/algorand-client-transaction-sender.mjs.map +1 -1
- package/types/algorand-client.d.ts +6 -7
- package/types/algorand-client.js +3 -6
- package/types/algorand-client.js.map +1 -1
- package/types/algorand-client.mjs +5 -5
- package/types/algorand-client.mjs.map +1 -1
- package/types/amount.js.map +1 -1
- package/types/amount.mjs.map +1 -1
- package/types/app-arc56.d.ts +31 -20
- package/types/app-arc56.js.map +1 -1
- package/types/app-arc56.mjs.map +1 -1
- package/types/app-client.d.ts +298 -286
- package/types/app-client.js +148 -41
- package/types/app-client.js.map +1 -1
- package/types/app-client.mjs +147 -40
- package/types/app-client.mjs.map +1 -1
- package/types/app-deployer.d.ts +4 -4
- package/types/app-deployer.js +23 -24
- package/types/app-deployer.js.map +1 -1
- package/types/app-deployer.mjs +25 -26
- package/types/app-deployer.mjs.map +1 -1
- package/types/app-factory.d.ts +130 -130
- package/types/app-factory.js +4 -5
- package/types/app-factory.js.map +1 -1
- package/types/app-factory.mjs +5 -6
- package/types/app-factory.mjs.map +1 -1
- package/types/app-manager.d.ts +5 -5
- package/types/app-manager.js +116 -38
- package/types/app-manager.js.map +1 -1
- package/types/app-manager.mjs +117 -39
- package/types/app-manager.mjs.map +1 -1
- package/types/app-spec.js +8 -2
- package/types/app-spec.js.map +1 -1
- package/types/app-spec.mjs +8 -2
- package/types/app-spec.mjs.map +1 -1
- package/types/app.d.ts +12 -11
- package/types/app.js.map +1 -1
- package/types/app.mjs.map +1 -1
- package/types/asset-manager.d.ts +9 -9
- package/types/asset-manager.js +10 -13
- package/types/asset-manager.js.map +1 -1
- package/types/asset-manager.mjs +10 -13
- package/types/asset-manager.mjs.map +1 -1
- package/types/async-event-emitter.d.ts +1 -10
- package/types/async-event-emitter.js +0 -5
- package/types/async-event-emitter.js.map +1 -1
- package/types/async-event-emitter.mjs +1 -6
- package/types/async-event-emitter.mjs.map +1 -1
- package/types/client-manager.d.ts +2 -9
- package/types/client-manager.js +11 -21
- package/types/client-manager.js.map +1 -1
- package/types/client-manager.mjs +11 -21
- package/types/client-manager.mjs.map +1 -1
- package/types/composer.d.ts +58 -46
- package/types/composer.js +154 -105
- package/types/composer.js.map +1 -1
- package/types/composer.mjs +156 -105
- package/types/composer.mjs.map +1 -1
- package/types/config.js.map +1 -1
- package/types/config.mjs.map +1 -1
- package/types/debugging.d.ts +1 -1
- package/types/debugging.js +1 -1
- package/types/debugging.js.map +1 -1
- package/types/debugging.mjs +1 -1
- package/types/debugging.mjs.map +1 -1
- package/types/dispenser-client.d.ts +2 -1
- package/types/dispenser-client.js +5 -1
- package/types/dispenser-client.js.map +1 -1
- package/types/dispenser-client.mjs +5 -1
- package/types/dispenser-client.mjs.map +1 -1
- package/types/indexer.d.ts +74 -755
- package/types/indexer.js.map +1 -1
- package/types/indexer.mjs.map +1 -1
- package/types/kmd-account-manager.d.ts +2 -2
- package/types/kmd-account-manager.js +1 -1
- package/types/kmd-account-manager.js.map +1 -1
- package/types/kmd-account-manager.mjs +2 -2
- package/types/kmd-account-manager.mjs.map +1 -1
- package/types/lifecycle-events.d.ts +10 -0
- package/types/lifecycle-events.js +8 -0
- package/types/lifecycle-events.js.map +1 -0
- package/types/lifecycle-events.mjs +8 -0
- package/types/lifecycle-events.mjs.map +1 -0
- package/types/logging.js.map +1 -1
- package/types/logging.mjs.map +1 -1
- package/types/logic-error.d.ts +2 -3
- package/types/logic-error.js +3 -3
- package/types/logic-error.js.map +1 -1
- package/types/logic-error.mjs +3 -3
- package/types/logic-error.mjs.map +1 -1
- package/types/network-client.d.ts +1 -1
- package/types/network-client.js.map +1 -1
- package/types/network-client.mjs.map +1 -1
- package/types/testing.d.ts +6 -7
- package/util.js.map +1 -1
- package/util.mjs.map +1 -1
- package/types/urlTokenBaseHTTPClient.d.ts +0 -40
- package/types/urlTokenBaseHTTPClient.js +0 -153
- package/types/urlTokenBaseHTTPClient.js.map +0 -1
- package/types/urlTokenBaseHTTPClient.mjs +0 -151
- package/types/urlTokenBaseHTTPClient.mjs.map +0 -1
package/types/asset-manager.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import algosdk from 'algosdk';
|
|
2
|
-
import { AccountAssetInformation
|
|
3
|
-
import
|
|
1
|
+
import algosdk, { Address } from 'algosdk';
|
|
2
|
+
import { AccountAssetInformation } from './account';
|
|
3
|
+
import { CommonTransactionParams, TransactionComposer } from './composer';
|
|
4
4
|
import { SendParams } from './transaction';
|
|
5
5
|
/** Individual result from performing a bulk opt-in or bulk opt-out for an account against a series of assets. */
|
|
6
6
|
export interface BulkAssetOptInOutResult {
|
|
@@ -121,13 +121,13 @@ export declare class AssetManager {
|
|
|
121
121
|
/**
|
|
122
122
|
* Create a new asset manager.
|
|
123
123
|
* @param algod An algod client
|
|
124
|
-
* @param newGroup A function that creates a new `
|
|
124
|
+
* @param newGroup A function that creates a new `TransactionComposer` transaction group
|
|
125
125
|
* @example Create a new asset manager
|
|
126
126
|
* ```typescript
|
|
127
|
-
* const assetManager = new AssetManager(algod, () => new
|
|
127
|
+
* const assetManager = new AssetManager(algod, () => new TransactionComposer({algod, () => signer, () => suggestedParams}))
|
|
128
128
|
* ```
|
|
129
129
|
*/
|
|
130
|
-
constructor(algod: algosdk.Algodv2, newGroup: () =>
|
|
130
|
+
constructor(algod: algosdk.Algodv2, newGroup: () => TransactionComposer);
|
|
131
131
|
/**
|
|
132
132
|
* Returns the current asset information for the asset with the given ID.
|
|
133
133
|
*
|
|
@@ -155,7 +155,7 @@ export declare class AssetManager {
|
|
|
155
155
|
* @param assetId The ID of the asset to return a holding for
|
|
156
156
|
* @returns The account asset holding information
|
|
157
157
|
*/
|
|
158
|
-
getAccountInformation(sender: string |
|
|
158
|
+
getAccountInformation(sender: string | Address, assetId: bigint): Promise<AccountAssetInformation>;
|
|
159
159
|
/**
|
|
160
160
|
* Opt an account in to a list of Algorand Standard Assets.
|
|
161
161
|
*
|
|
@@ -173,7 +173,7 @@ export declare class AssetManager {
|
|
|
173
173
|
* ```
|
|
174
174
|
* @returns An array of records matching asset ID to transaction ID of the opt in
|
|
175
175
|
*/
|
|
176
|
-
bulkOptIn(account: string |
|
|
176
|
+
bulkOptIn(account: string | Address, assetIds: bigint[], options?: Omit<CommonTransactionParams, 'sender'> & SendParams): Promise<BulkAssetOptInOutResult[]>;
|
|
177
177
|
/**
|
|
178
178
|
* Opt an account out of a list of Algorand Standard Assets.
|
|
179
179
|
*
|
|
@@ -191,7 +191,7 @@ export declare class AssetManager {
|
|
|
191
191
|
* ```
|
|
192
192
|
* @returns An array of records matching asset ID to transaction ID of the opt in
|
|
193
193
|
*/
|
|
194
|
-
bulkOptOut(account: string |
|
|
194
|
+
bulkOptOut(account: string | Address, assetIds: bigint[], options?: Omit<CommonTransactionParams, 'sender'> & SendParams & {
|
|
195
195
|
/** Whether or not to check if the account has a zero balance for each asset first or not.
|
|
196
196
|
*
|
|
197
197
|
* Defaults to `true`.
|
package/types/asset-manager.js
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var algosdk = require('algosdk');
|
|
4
3
|
var config = require('../config.js');
|
|
5
4
|
var util = require('../util.js');
|
|
6
5
|
var types_composer = require('./composer.js');
|
|
7
6
|
|
|
8
|
-
var AssetModel = algosdk.modelsv2.Asset;
|
|
9
7
|
/** Allows management of asset information. */
|
|
10
8
|
class AssetManager {
|
|
11
9
|
/**
|
|
12
10
|
* Create a new asset manager.
|
|
13
11
|
* @param algod An algod client
|
|
14
|
-
* @param newGroup A function that creates a new `
|
|
12
|
+
* @param newGroup A function that creates a new `TransactionComposer` transaction group
|
|
15
13
|
* @example Create a new asset manager
|
|
16
14
|
* ```typescript
|
|
17
|
-
* const assetManager = new AssetManager(algod, () => new
|
|
15
|
+
* const assetManager = new AssetManager(algod, () => new TransactionComposer({algod, () => signer, () => suggestedParams}))
|
|
18
16
|
* ```
|
|
19
17
|
*/
|
|
20
18
|
constructor(algod, newGroup) {
|
|
@@ -33,7 +31,7 @@ class AssetManager {
|
|
|
33
31
|
* @returns The asset information
|
|
34
32
|
*/
|
|
35
33
|
async getById(assetId) {
|
|
36
|
-
const asset =
|
|
34
|
+
const asset = await this._algod.getAssetByID(Number(assetId)).do();
|
|
37
35
|
return {
|
|
38
36
|
assetId: BigInt(asset.index),
|
|
39
37
|
total: BigInt(asset.params.total),
|
|
@@ -69,11 +67,11 @@ class AssetManager {
|
|
|
69
67
|
* @returns The account asset holding information
|
|
70
68
|
*/
|
|
71
69
|
async getAccountInformation(sender, assetId) {
|
|
72
|
-
const info = await this._algod.accountAssetInformation(
|
|
70
|
+
const info = await this._algod.accountAssetInformation(sender, Number(assetId)).do();
|
|
73
71
|
return {
|
|
74
72
|
assetId: BigInt(assetId),
|
|
75
|
-
balance: BigInt(info
|
|
76
|
-
frozen: info
|
|
73
|
+
balance: BigInt(info.assetHolding?.amount ?? 0),
|
|
74
|
+
frozen: info.assetHolding?.isFrozen === true,
|
|
77
75
|
round: BigInt(info['round']),
|
|
78
76
|
};
|
|
79
77
|
}
|
|
@@ -101,7 +99,7 @@ class AssetManager {
|
|
|
101
99
|
for (const assetId of assetGroup) {
|
|
102
100
|
composer.addAssetOptIn({
|
|
103
101
|
...options,
|
|
104
|
-
sender:
|
|
102
|
+
sender: account,
|
|
105
103
|
assetId: BigInt(assetId),
|
|
106
104
|
});
|
|
107
105
|
}
|
|
@@ -133,7 +131,6 @@ class AssetManager {
|
|
|
133
131
|
*/
|
|
134
132
|
async bulkOptOut(account, assetIds, options) {
|
|
135
133
|
const results = [];
|
|
136
|
-
const sender = typeof account === 'string' ? account : account.addr;
|
|
137
134
|
for (const assetGroup of util.chunkArray(assetIds, types_composer.MAX_TRANSACTION_GROUP_SIZE)) {
|
|
138
135
|
const composer = this._newGroup();
|
|
139
136
|
const notOptedInAssetIds = [];
|
|
@@ -141,7 +138,7 @@ class AssetManager {
|
|
|
141
138
|
for (const assetId of assetGroup) {
|
|
142
139
|
if (options?.ensureZeroBalance !== false) {
|
|
143
140
|
try {
|
|
144
|
-
const accountAssetInfo = await this.getAccountInformation(
|
|
141
|
+
const accountAssetInfo = await this.getAccountInformation(account, assetId);
|
|
145
142
|
if (accountAssetInfo.balance !== 0n) {
|
|
146
143
|
nonZeroBalanceAssetIds.push(BigInt(assetId));
|
|
147
144
|
}
|
|
@@ -152,7 +149,7 @@ class AssetManager {
|
|
|
152
149
|
}
|
|
153
150
|
}
|
|
154
151
|
if (notOptedInAssetIds.length > 0 || nonZeroBalanceAssetIds.length > 0) {
|
|
155
|
-
throw new Error(`Account ${
|
|
152
|
+
throw new Error(`Account ${account}${notOptedInAssetIds.length > 0 ? ` is not opted-in to Asset${notOptedInAssetIds.length > 1 ? 's' : ''} ${notOptedInAssetIds.join(', ')}` : ''}${nonZeroBalanceAssetIds.length > 0
|
|
156
153
|
? ` has non-zero balance for Asset${nonZeroBalanceAssetIds.length > 1 ? 's' : ''} ${nonZeroBalanceAssetIds.join(', ')}`
|
|
157
154
|
: ''}; can't opt-out.`);
|
|
158
155
|
}
|
|
@@ -160,7 +157,7 @@ class AssetManager {
|
|
|
160
157
|
composer.addAssetOptOut({
|
|
161
158
|
...options,
|
|
162
159
|
creator: (await this.getById(BigInt(assetId))).creator,
|
|
163
|
-
sender,
|
|
160
|
+
sender: account,
|
|
164
161
|
assetId: BigInt(assetId),
|
|
165
162
|
});
|
|
166
163
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asset-manager.js","sources":["../../src/types/asset-manager.ts"],"sourcesContent":["import algosdk from 'algosdk'\nimport { Config } from '../config'\nimport { chunkArray } from '../util'\nimport { AccountAssetInformation, TransactionSignerAccount } from './account'\nimport AlgoKitComposer, { CommonTransactionParams, MAX_TRANSACTION_GROUP_SIZE } from './composer'\nimport { SendParams } from './transaction'\nimport AssetModel = algosdk.modelsv2.Asset\n\n/** Individual result from performing a bulk opt-in or bulk opt-out for an account against a series of assets. */\nexport interface BulkAssetOptInOutResult {\n /** The ID of the asset opted into / out of */\n assetId: bigint\n /** The transaction ID of the resulting opt in / out */\n transactionId: string\n}\n\n/** Information about an asset. */\nexport interface AssetInformation {\n /** The ID of the asset. */\n assetId: bigint\n\n /** The address of the account that created the asset.\n *\n * This is the address where the parameters for this asset can be found,\n * and also the address where unwanted asset units can be sent when\n * closing out an asset position and opting-out of the asset.\n */\n creator: string\n\n /** The total amount of the smallest divisible (decimal) units that were created of the asset.\n *\n * For example, if `decimals` is, say, 2, then for every 100 `total` there is 1 whole unit.\n */\n total: bigint\n\n /** The amount of decimal places the asset was created with.\n *\n * * If 0, the asset is not divisible;\n * * If 1, the base unit of the asset is in tenths;\n * * If 2, the base unit of the asset is in hundredths;\n * * If 3, the base unit of the asset is in thousandths;\n * * and so on up to 19 decimal places.\n */\n decimals: number\n\n /** Whether the asset was frozen by default for all accounts.\n *\n * If `true` then for anyone apart from the creator to hold the\n * asset it needs to be unfrozen per account using an asset freeze\n * transaction from the `freeze` account.\n */\n defaultFrozen?: boolean\n\n /** The address of the optional account that can manage the configuration of the asset and destroy it.\n *\n * If not set the asset is permanently immutable.\n */\n manager?: string\n\n /**\n * The address of the optional account that holds the reserve (uncirculated supply) units of the asset.\n *\n * This address has no specific authority in the protocol itself and is informational only.\n *\n * Some standards like [ARC-19](https://github.com/algorandfoundation/ARCs/blob/main/ARCs/arc-0019.md)\n * rely on this field to hold meaningful data.\n *\n * It can be used in the case where you want to signal to holders of your asset that the uncirculated units\n * of the asset reside in an account that is different from the default creator account.\n *\n * If not set the field is permanently empty.\n */\n reserve?: string\n\n /**\n * The address of the optional account that can be used to freeze or unfreeze holdings of this asset for any account.\n *\n * If empty, freezing is not permitted.\n *\n * If not set the field is permanently empty.\n */\n freeze?: string\n\n /**\n * The address of the optional account that can clawback holdings of this asset from any account.\n *\n * The clawback account has the ability to **unconditionally take assets from any account**.\n *\n * If empty, clawback is not permitted.\n *\n * If not set the field is permanently empty.\n */\n clawback?: string\n\n /** The optional name of the unit of this asset (e.g. ticker name).\n *\n * Max size is 8 bytes.\n */\n unitName?: string\n\n /** The optional name of the unit of this asset (e.g. ticker name).\n *\n * Max size is 8 bytes.\n */\n unitNameAsBytes?: Uint8Array\n\n /** The optional name of the asset.\n *\n * Max size is 32 bytes.\n */\n assetName?: string\n\n /** The optional name of the asset.\n *\n * Max size is 32 bytes.\n */\n assetNameAsBytes?: Uint8Array\n\n /** Optional URL where more information about the asset can be retrieved (e.g. metadata).\n *\n * Max size is 96 bytes.\n */\n url?: string\n\n /** Optional URL where more information about the asset can be retrieved (e.g. metadata).\n *\n * Max size is 96 bytes.\n */\n urlAsBytes?: Uint8Array\n\n /** 32-byte hash of some metadata that is relevant to the asset and/or asset holders.\n *\n * The format of this metadata is up to the application.\n */\n metadataHash?: Uint8Array\n}\n\n/** Allows management of asset information. */\nexport class AssetManager {\n private _algod: algosdk.Algodv2\n private _newGroup: () => AlgoKitComposer\n\n /**\n * Create a new asset manager.\n * @param algod An algod client\n * @param newGroup A function that creates a new `AlgoKitComposer` transaction group\n * @example Create a new asset manager\n * ```typescript\n * const assetManager = new AssetManager(algod, () => new AlgoKitComposer({algod, () => signer, () => suggestedParams}))\n * ```\n */\n constructor(algod: algosdk.Algodv2, newGroup: () => AlgoKitComposer) {\n this._algod = algod\n this._newGroup = newGroup\n }\n\n /**\n * Returns the current asset information for the asset with the given ID.\n *\n * @example\n * ```typescript\n * const assetInfo = await assetManager.getById(12353n);\n * ```\n *\n * @param assetId The ID of the asset\n * @returns The asset information\n */\n public async getById(assetId: bigint): Promise<AssetInformation> {\n const asset = AssetModel.from_obj_for_encoding(await this._algod.getAssetByID(Number(assetId)).do())\n\n return {\n assetId: BigInt(asset.index),\n total: BigInt(asset.params.total),\n decimals: Number(asset.params.decimals),\n assetName: asset.params.name,\n assetNameAsBytes: asset.params.nameB64,\n unitName: asset.params.unitName,\n unitNameAsBytes: asset.params.unitNameB64,\n url: asset.params.url,\n urlAsBytes: asset.params.urlB64,\n creator: asset.params.creator,\n manager: asset.params.manager,\n clawback: asset.params.clawback,\n freeze: asset.params.freeze,\n reserve: asset.params.reserve,\n defaultFrozen: asset.params.defaultFrozen,\n metadataHash: asset.params.metadataHash,\n }\n }\n\n /**\n * Returns the given sender account's asset holding for a given asset.\n *\n * @example\n * ```typescript\n * const address = \"XBYLS2E6YI6XXL5BWCAMOA4GTWHXWENZMX5UHXMRNWWUQ7BXCY5WC5TEPA\";\n * const assetId = 123345n;\n * const accountInfo = await algorand.asset.getAccountInformation(address, assetId);\n * ```\n *\n * [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/#get-v2accountsaddressassetsasset-id)\n * @param sender The address of the sender/account to look up\n * @param assetId The ID of the asset to return a holding for\n * @returns The account asset holding information\n */\n public async getAccountInformation(sender: string | TransactionSignerAccount, assetId: bigint): Promise<AccountAssetInformation> {\n const info = await this._algod.accountAssetInformation(typeof sender === 'string' ? sender : sender.addr, Number(assetId)).do()\n\n return {\n assetId: BigInt(assetId),\n balance: BigInt(info['asset-holding']['amount']),\n frozen: info['asset-holding']['is-frozen'] === true,\n round: BigInt(info['round']),\n }\n }\n\n /**\n * Opt an account in to a list of Algorand Standard Assets.\n *\n * Transactions will be sent in batches of 16 as transaction groups.\n *\n * @param account The account to opt-in\n * @param assetIds The list of asset IDs to opt-in to\n * @param options Any parameters to control the transaction or execution of the transaction\n * @example Example using AlgorandClient\n * ```typescript\n * // Basic example\n * algorand.asset.bulkOptIn(\"ACCOUNTADDRESS\", [12345n, 67890n])\n * // With configuration\n * algorand.asset.bulkOptIn(\"ACCOUNTADDRESS\", [12345n, 67890n], { maxFee: (1000).microAlgo(), suppressLog: true })\n * ```\n * @returns An array of records matching asset ID to transaction ID of the opt in\n */\n async bulkOptIn(\n account: string | TransactionSignerAccount,\n assetIds: bigint[],\n options?: Omit<CommonTransactionParams, 'sender'> & SendParams,\n ): Promise<BulkAssetOptInOutResult[]> {\n const results: BulkAssetOptInOutResult[] = []\n\n for (const assetGroup of chunkArray(assetIds, MAX_TRANSACTION_GROUP_SIZE)) {\n const composer = this._newGroup()\n\n for (const assetId of assetGroup) {\n composer.addAssetOptIn({\n ...options,\n sender: typeof account === 'string' ? account : account.addr,\n assetId: BigInt(assetId),\n })\n }\n\n const result = await composer.send(options)\n\n Config.getLogger(options?.suppressLog).info(\n `Successfully opted in ${account} for assets ${assetGroup.join(', ')} with transaction IDs ${result.txIds.join(', ')}` +\n `\\n Grouped under ${result.groupId} in round ${result.confirmations?.[0]?.confirmedRound}.`,\n )\n\n assetGroup.forEach((assetId, index) => {\n results.push({ assetId: BigInt(assetId), transactionId: result.txIds[index] })\n })\n }\n\n return results\n }\n\n /**\n * Opt an account out of a list of Algorand Standard Assets.\n *\n * Transactions will be sent in batches of 16 as transaction groups.\n *\n * @param account The account to opt-in\n * @param assetIds The list of asset IDs to opt-out of\n * @param options Any parameters to control the transaction or execution of the transaction\n * @example Example using AlgorandClient\n * ```typescript\n * // Basic example\n * algorand.asset.bulkOptOut(\"ACCOUNTADDRESS\", [12345n, 67890n])\n * // With configuration\n * algorand.asset.bulkOptOut(\"ACCOUNTADDRESS\", [12345n, 67890n], { ensureZeroBalance: true, maxFee: (1000).microAlgo(), suppressLog: true })\n * ```\n * @returns An array of records matching asset ID to transaction ID of the opt in\n */\n async bulkOptOut(\n account: string | TransactionSignerAccount,\n assetIds: bigint[],\n options?: Omit<CommonTransactionParams, 'sender'> &\n SendParams & {\n /** Whether or not to check if the account has a zero balance for each asset first or not.\n *\n * Defaults to `true`.\n *\n * If this is set to `true` and the account has an asset balance it will throw an error.\n *\n * If this is set to `false` and the account has an asset balance it will lose those assets to the asset creator.\n */\n ensureZeroBalance?: boolean\n },\n ): Promise<BulkAssetOptInOutResult[]> {\n const results: BulkAssetOptInOutResult[] = []\n\n const sender = typeof account === 'string' ? account : account.addr\n\n for (const assetGroup of chunkArray(assetIds, MAX_TRANSACTION_GROUP_SIZE)) {\n const composer = this._newGroup()\n\n const notOptedInAssetIds: bigint[] = []\n const nonZeroBalanceAssetIds: bigint[] = []\n for (const assetId of assetGroup) {\n if (options?.ensureZeroBalance !== false) {\n try {\n const accountAssetInfo = await this.getAccountInformation(sender, assetId)\n if (accountAssetInfo.balance !== 0n) {\n nonZeroBalanceAssetIds.push(BigInt(assetId))\n }\n } catch {\n notOptedInAssetIds.push(BigInt(assetId))\n }\n }\n }\n\n if (notOptedInAssetIds.length > 0 || nonZeroBalanceAssetIds.length > 0) {\n throw new Error(\n `Account ${sender}${notOptedInAssetIds.length > 0 ? ` is not opted-in to Asset${notOptedInAssetIds.length > 1 ? 's' : ''} ${notOptedInAssetIds.join(', ')}` : ''}${\n nonZeroBalanceAssetIds.length > 0\n ? ` has non-zero balance for Asset${nonZeroBalanceAssetIds.length > 1 ? 's' : ''} ${nonZeroBalanceAssetIds.join(', ')}`\n : ''\n }; can't opt-out.`,\n )\n }\n\n for (const assetId of assetGroup) {\n composer.addAssetOptOut({\n ...options,\n creator: (await this.getById(BigInt(assetId))).creator,\n sender,\n assetId: BigInt(assetId),\n })\n }\n\n const result = await composer.send(options)\n\n Config.getLogger(options?.suppressLog).info(\n `Successfully opted ${account} out of assets ${assetGroup.join(', ')} with transaction IDs ${result.txIds.join(', ')}` +\n `\\n Grouped under ${result.groupId} in round ${result.confirmations?.[0]?.confirmedRound}.`,\n )\n\n assetGroup.forEach((assetId, index) => {\n results.push({ assetId: BigInt(assetId), transactionId: result.txIds[index] })\n })\n }\n\n return results\n }\n}\n"],"names":["chunkArray","MAX_TRANSACTION_GROUP_SIZE","Config"],"mappings":";;;;;;;AAMA,IAAO,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAA;AAmI1C;MACa,YAAY,CAAA;AAIvB;;;;;;;;AAQG;IACH,WAAY,CAAA,KAAsB,EAAE,QAA+B,EAAA;AACjE,QAAA,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;AACnB,QAAA,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAA;KAC1B;AAED;;;;;;;;;;AAUG;IACI,MAAM,OAAO,CAAC,OAAe,EAAA;QAClC,MAAM,KAAK,GAAG,UAAU,CAAC,qBAAqB,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;QAEpG,OAAO;AACL,YAAA,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;YAC5B,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC;YACj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AU,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,KAAK,KAAI;gBACpC,OAAO,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE,aAAa,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;AAChF,aAAC,CAAC,CAAA;SACH;AAED,QAAA,OAAO,OAAO,CAAA;KACf;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"asset-manager.js","sources":["../../src/types/asset-manager.ts"],"sourcesContent":["import algosdk, { Address } from 'algosdk'\nimport { Config } from '../config'\nimport { chunkArray } from '../util'\nimport { AccountAssetInformation } from './account'\nimport { CommonTransactionParams, MAX_TRANSACTION_GROUP_SIZE, TransactionComposer } from './composer'\nimport { SendParams } from './transaction'\n\n/** Individual result from performing a bulk opt-in or bulk opt-out for an account against a series of assets. */\nexport interface BulkAssetOptInOutResult {\n /** The ID of the asset opted into / out of */\n assetId: bigint\n /** The transaction ID of the resulting opt in / out */\n transactionId: string\n}\n\n/** Information about an asset. */\nexport interface AssetInformation {\n /** The ID of the asset. */\n assetId: bigint\n\n /** The address of the account that created the asset.\n *\n * This is the address where the parameters for this asset can be found,\n * and also the address where unwanted asset units can be sent when\n * closing out an asset position and opting-out of the asset.\n */\n creator: string\n\n /** The total amount of the smallest divisible (decimal) units that were created of the asset.\n *\n * For example, if `decimals` is, say, 2, then for every 100 `total` there is 1 whole unit.\n */\n total: bigint\n\n /** The amount of decimal places the asset was created with.\n *\n * * If 0, the asset is not divisible;\n * * If 1, the base unit of the asset is in tenths;\n * * If 2, the base unit of the asset is in hundredths;\n * * If 3, the base unit of the asset is in thousandths;\n * * and so on up to 19 decimal places.\n */\n decimals: number\n\n /** Whether the asset was frozen by default for all accounts.\n *\n * If `true` then for anyone apart from the creator to hold the\n * asset it needs to be unfrozen per account using an asset freeze\n * transaction from the `freeze` account.\n */\n defaultFrozen?: boolean\n\n /** The address of the optional account that can manage the configuration of the asset and destroy it.\n *\n * If not set the asset is permanently immutable.\n */\n manager?: string\n\n /**\n * The address of the optional account that holds the reserve (uncirculated supply) units of the asset.\n *\n * This address has no specific authority in the protocol itself and is informational only.\n *\n * Some standards like [ARC-19](https://github.com/algorandfoundation/ARCs/blob/main/ARCs/arc-0019.md)\n * rely on this field to hold meaningful data.\n *\n * It can be used in the case where you want to signal to holders of your asset that the uncirculated units\n * of the asset reside in an account that is different from the default creator account.\n *\n * If not set the field is permanently empty.\n */\n reserve?: string\n\n /**\n * The address of the optional account that can be used to freeze or unfreeze holdings of this asset for any account.\n *\n * If empty, freezing is not permitted.\n *\n * If not set the field is permanently empty.\n */\n freeze?: string\n\n /**\n * The address of the optional account that can clawback holdings of this asset from any account.\n *\n * The clawback account has the ability to **unconditionally take assets from any account**.\n *\n * If empty, clawback is not permitted.\n *\n * If not set the field is permanently empty.\n */\n clawback?: string\n\n /** The optional name of the unit of this asset (e.g. ticker name).\n *\n * Max size is 8 bytes.\n */\n unitName?: string\n\n /** The optional name of the unit of this asset (e.g. ticker name).\n *\n * Max size is 8 bytes.\n */\n unitNameAsBytes?: Uint8Array\n\n /** The optional name of the asset.\n *\n * Max size is 32 bytes.\n */\n assetName?: string\n\n /** The optional name of the asset.\n *\n * Max size is 32 bytes.\n */\n assetNameAsBytes?: Uint8Array\n\n /** Optional URL where more information about the asset can be retrieved (e.g. metadata).\n *\n * Max size is 96 bytes.\n */\n url?: string\n\n /** Optional URL where more information about the asset can be retrieved (e.g. metadata).\n *\n * Max size is 96 bytes.\n */\n urlAsBytes?: Uint8Array\n\n /** 32-byte hash of some metadata that is relevant to the asset and/or asset holders.\n *\n * The format of this metadata is up to the application.\n */\n metadataHash?: Uint8Array\n}\n\n/** Allows management of asset information. */\nexport class AssetManager {\n private _algod: algosdk.Algodv2\n private _newGroup: () => TransactionComposer\n\n /**\n * Create a new asset manager.\n * @param algod An algod client\n * @param newGroup A function that creates a new `TransactionComposer` transaction group\n * @example Create a new asset manager\n * ```typescript\n * const assetManager = new AssetManager(algod, () => new TransactionComposer({algod, () => signer, () => suggestedParams}))\n * ```\n */\n constructor(algod: algosdk.Algodv2, newGroup: () => TransactionComposer) {\n this._algod = algod\n this._newGroup = newGroup\n }\n\n /**\n * Returns the current asset information for the asset with the given ID.\n *\n * @example\n * ```typescript\n * const assetInfo = await assetManager.getById(12353n);\n * ```\n *\n * @param assetId The ID of the asset\n * @returns The asset information\n */\n public async getById(assetId: bigint): Promise<AssetInformation> {\n const asset = await this._algod.getAssetByID(Number(assetId)).do()\n\n return {\n assetId: BigInt(asset.index),\n total: BigInt(asset.params.total),\n decimals: Number(asset.params.decimals),\n assetName: asset.params.name,\n assetNameAsBytes: asset.params.nameB64,\n unitName: asset.params.unitName,\n unitNameAsBytes: asset.params.unitNameB64,\n url: asset.params.url,\n urlAsBytes: asset.params.urlB64,\n creator: asset.params.creator,\n manager: asset.params.manager,\n clawback: asset.params.clawback,\n freeze: asset.params.freeze,\n reserve: asset.params.reserve,\n defaultFrozen: asset.params.defaultFrozen,\n metadataHash: asset.params.metadataHash,\n }\n }\n\n /**\n * Returns the given sender account's asset holding for a given asset.\n *\n * @example\n * ```typescript\n * const address = \"XBYLS2E6YI6XXL5BWCAMOA4GTWHXWENZMX5UHXMRNWWUQ7BXCY5WC5TEPA\";\n * const assetId = 123345n;\n * const accountInfo = await algorand.asset.getAccountInformation(address, assetId);\n * ```\n *\n * [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/#get-v2accountsaddressassetsasset-id)\n * @param sender The address of the sender/account to look up\n * @param assetId The ID of the asset to return a holding for\n * @returns The account asset holding information\n */\n public async getAccountInformation(sender: string | Address, assetId: bigint): Promise<AccountAssetInformation> {\n const info = await this._algod.accountAssetInformation(sender, Number(assetId)).do()\n\n return {\n assetId: BigInt(assetId),\n balance: BigInt(info.assetHolding?.amount ?? 0),\n frozen: info.assetHolding?.isFrozen === true,\n round: BigInt(info['round']),\n }\n }\n\n /**\n * Opt an account in to a list of Algorand Standard Assets.\n *\n * Transactions will be sent in batches of 16 as transaction groups.\n *\n * @param account The account to opt-in\n * @param assetIds The list of asset IDs to opt-in to\n * @param options Any parameters to control the transaction or execution of the transaction\n * @example Example using AlgorandClient\n * ```typescript\n * // Basic example\n * algorand.asset.bulkOptIn(\"ACCOUNTADDRESS\", [12345n, 67890n])\n * // With configuration\n * algorand.asset.bulkOptIn(\"ACCOUNTADDRESS\", [12345n, 67890n], { maxFee: (1000).microAlgo(), suppressLog: true })\n * ```\n * @returns An array of records matching asset ID to transaction ID of the opt in\n */\n async bulkOptIn(\n account: string | Address,\n assetIds: bigint[],\n options?: Omit<CommonTransactionParams, 'sender'> & SendParams,\n ): Promise<BulkAssetOptInOutResult[]> {\n const results: BulkAssetOptInOutResult[] = []\n\n for (const assetGroup of chunkArray(assetIds, MAX_TRANSACTION_GROUP_SIZE)) {\n const composer = this._newGroup()\n\n for (const assetId of assetGroup) {\n composer.addAssetOptIn({\n ...options,\n sender: account,\n assetId: BigInt(assetId),\n })\n }\n\n const result = await composer.send(options)\n\n Config.getLogger(options?.suppressLog).info(\n `Successfully opted in ${account} for assets ${assetGroup.join(', ')} with transaction IDs ${result.txIds.join(', ')}` +\n `\\n Grouped under ${result.groupId} in round ${result.confirmations?.[0]?.confirmedRound}.`,\n )\n\n assetGroup.forEach((assetId, index) => {\n results.push({ assetId: BigInt(assetId), transactionId: result.txIds[index] })\n })\n }\n\n return results\n }\n\n /**\n * Opt an account out of a list of Algorand Standard Assets.\n *\n * Transactions will be sent in batches of 16 as transaction groups.\n *\n * @param account The account to opt-in\n * @param assetIds The list of asset IDs to opt-out of\n * @param options Any parameters to control the transaction or execution of the transaction\n * @example Example using AlgorandClient\n * ```typescript\n * // Basic example\n * algorand.asset.bulkOptOut(\"ACCOUNTADDRESS\", [12345n, 67890n])\n * // With configuration\n * algorand.asset.bulkOptOut(\"ACCOUNTADDRESS\", [12345n, 67890n], { ensureZeroBalance: true, maxFee: (1000).microAlgo(), suppressLog: true })\n * ```\n * @returns An array of records matching asset ID to transaction ID of the opt in\n */\n async bulkOptOut(\n account: string | Address,\n assetIds: bigint[],\n options?: Omit<CommonTransactionParams, 'sender'> &\n SendParams & {\n /** Whether or not to check if the account has a zero balance for each asset first or not.\n *\n * Defaults to `true`.\n *\n * If this is set to `true` and the account has an asset balance it will throw an error.\n *\n * If this is set to `false` and the account has an asset balance it will lose those assets to the asset creator.\n */\n ensureZeroBalance?: boolean\n },\n ): Promise<BulkAssetOptInOutResult[]> {\n const results: BulkAssetOptInOutResult[] = []\n\n for (const assetGroup of chunkArray(assetIds, MAX_TRANSACTION_GROUP_SIZE)) {\n const composer = this._newGroup()\n\n const notOptedInAssetIds: bigint[] = []\n const nonZeroBalanceAssetIds: bigint[] = []\n for (const assetId of assetGroup) {\n if (options?.ensureZeroBalance !== false) {\n try {\n const accountAssetInfo = await this.getAccountInformation(account, assetId)\n if (accountAssetInfo.balance !== 0n) {\n nonZeroBalanceAssetIds.push(BigInt(assetId))\n }\n } catch {\n notOptedInAssetIds.push(BigInt(assetId))\n }\n }\n }\n\n if (notOptedInAssetIds.length > 0 || nonZeroBalanceAssetIds.length > 0) {\n throw new Error(\n `Account ${account}${notOptedInAssetIds.length > 0 ? ` is not opted-in to Asset${notOptedInAssetIds.length > 1 ? 's' : ''} ${notOptedInAssetIds.join(', ')}` : ''}${\n nonZeroBalanceAssetIds.length > 0\n ? ` has non-zero balance for Asset${nonZeroBalanceAssetIds.length > 1 ? 's' : ''} ${nonZeroBalanceAssetIds.join(', ')}`\n : ''\n }; can't opt-out.`,\n )\n }\n\n for (const assetId of assetGroup) {\n composer.addAssetOptOut({\n ...options,\n creator: (await this.getById(BigInt(assetId))).creator,\n sender: account,\n assetId: BigInt(assetId),\n })\n }\n\n const result = await composer.send(options)\n\n Config.getLogger(options?.suppressLog).info(\n `Successfully opted ${account} out of assets ${assetGroup.join(', ')} with transaction IDs ${result.txIds.join(', ')}` +\n `\\n Grouped under ${result.groupId} in round ${result.confirmations?.[0]?.confirmedRound}.`,\n )\n\n assetGroup.forEach((assetId, index) => {\n results.push({ assetId: BigInt(assetId), transactionId: result.txIds[index] })\n })\n }\n\n return results\n }\n}\n"],"names":["chunkArray","MAX_TRANSACTION_GROUP_SIZE","Config"],"mappings":";;;;;;AAwIA;MACa,YAAY,CAAA;AAIvB;;;;;;;;AAQG;IACH,WAAY,CAAA,KAAsB,EAAE,QAAmC,EAAA;AACrE,QAAA,IAAI,CAAC,MAAM,GAAG,KAAK;AACnB,QAAA,IAAI,CAAC,SAAS,GAAG,QAAQ;;AAG3B;;;;;;;;;;AAUG;IACI,MAAM,OAAO,CAAC,OAAe,EAAA;AAClC,QAAA,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE;QAElE,OAAO;AACL,YAAA,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;YAC5B,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC;YACjC,QAAQ,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC;AACvC,YAAA,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,IAAI;AAC5B,YAAA,gBAAgB,EAAE,KAAK,CAAC,MAAM,CAAC,OAAO;AACtC,YAAA,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,QAAQ;AAC/B,YAAA,eAAe,EAAE,KAAK,CAAC,MAAM,CAAC,WAAW;AACzC,YAAA,GAAG,EAAE,KAAK,CAAC,MAAM,CAAC,GAAG;AACrB,YAAA,UAAU,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM;AAC/B,YAAA,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,OAAO;AAC7B,YAAA,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,OAAO;AAC7B,YAAA,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,QAAQ;AAC/B,YAAA,M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EAAA,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA,sBAAA,EAAyB,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAE,CAAA;AACpH,gBAAA,CAAA,kBAAA,EAAqB,MAAM,CAAC,OAAO,CAAA,UAAA,EAAa,MAAM,CAAC,aAAa,GAAG,CAAC,CAAC,EAAE,cAAc,CAAA,CAAA,CAAG,CAC/F;YAED,UAAU,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,KAAK,KAAI;gBACpC,OAAO,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE,aAAa,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;AAChF,aAAC,CAAC;;AAGJ,QAAA,OAAO,OAAO;;AAEjB;;;;"}
|
package/types/asset-manager.mjs
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
|
-
import algosdk from 'algosdk';
|
|
2
1
|
import { Config } from '../config.mjs';
|
|
3
2
|
import { chunkArray } from '../util.mjs';
|
|
4
3
|
import { MAX_TRANSACTION_GROUP_SIZE } from './composer.mjs';
|
|
5
4
|
|
|
6
|
-
var AssetModel = algosdk.modelsv2.Asset;
|
|
7
5
|
/** Allows management of asset information. */
|
|
8
6
|
class AssetManager {
|
|
9
7
|
/**
|
|
10
8
|
* Create a new asset manager.
|
|
11
9
|
* @param algod An algod client
|
|
12
|
-
* @param newGroup A function that creates a new `
|
|
10
|
+
* @param newGroup A function that creates a new `TransactionComposer` transaction group
|
|
13
11
|
* @example Create a new asset manager
|
|
14
12
|
* ```typescript
|
|
15
|
-
* const assetManager = new AssetManager(algod, () => new
|
|
13
|
+
* const assetManager = new AssetManager(algod, () => new TransactionComposer({algod, () => signer, () => suggestedParams}))
|
|
16
14
|
* ```
|
|
17
15
|
*/
|
|
18
16
|
constructor(algod, newGroup) {
|
|
@@ -31,7 +29,7 @@ class AssetManager {
|
|
|
31
29
|
* @returns The asset information
|
|
32
30
|
*/
|
|
33
31
|
async getById(assetId) {
|
|
34
|
-
const asset =
|
|
32
|
+
const asset = await this._algod.getAssetByID(Number(assetId)).do();
|
|
35
33
|
return {
|
|
36
34
|
assetId: BigInt(asset.index),
|
|
37
35
|
total: BigInt(asset.params.total),
|
|
@@ -67,11 +65,11 @@ class AssetManager {
|
|
|
67
65
|
* @returns The account asset holding information
|
|
68
66
|
*/
|
|
69
67
|
async getAccountInformation(sender, assetId) {
|
|
70
|
-
const info = await this._algod.accountAssetInformation(
|
|
68
|
+
const info = await this._algod.accountAssetInformation(sender, Number(assetId)).do();
|
|
71
69
|
return {
|
|
72
70
|
assetId: BigInt(assetId),
|
|
73
|
-
balance: BigInt(info
|
|
74
|
-
frozen: info
|
|
71
|
+
balance: BigInt(info.assetHolding?.amount ?? 0),
|
|
72
|
+
frozen: info.assetHolding?.isFrozen === true,
|
|
75
73
|
round: BigInt(info['round']),
|
|
76
74
|
};
|
|
77
75
|
}
|
|
@@ -99,7 +97,7 @@ class AssetManager {
|
|
|
99
97
|
for (const assetId of assetGroup) {
|
|
100
98
|
composer.addAssetOptIn({
|
|
101
99
|
...options,
|
|
102
|
-
sender:
|
|
100
|
+
sender: account,
|
|
103
101
|
assetId: BigInt(assetId),
|
|
104
102
|
});
|
|
105
103
|
}
|
|
@@ -131,7 +129,6 @@ class AssetManager {
|
|
|
131
129
|
*/
|
|
132
130
|
async bulkOptOut(account, assetIds, options) {
|
|
133
131
|
const results = [];
|
|
134
|
-
const sender = typeof account === 'string' ? account : account.addr;
|
|
135
132
|
for (const assetGroup of chunkArray(assetIds, MAX_TRANSACTION_GROUP_SIZE)) {
|
|
136
133
|
const composer = this._newGroup();
|
|
137
134
|
const notOptedInAssetIds = [];
|
|
@@ -139,7 +136,7 @@ class AssetManager {
|
|
|
139
136
|
for (const assetId of assetGroup) {
|
|
140
137
|
if (options?.ensureZeroBalance !== false) {
|
|
141
138
|
try {
|
|
142
|
-
const accountAssetInfo = await this.getAccountInformation(
|
|
139
|
+
const accountAssetInfo = await this.getAccountInformation(account, assetId);
|
|
143
140
|
if (accountAssetInfo.balance !== 0n) {
|
|
144
141
|
nonZeroBalanceAssetIds.push(BigInt(assetId));
|
|
145
142
|
}
|
|
@@ -150,7 +147,7 @@ class AssetManager {
|
|
|
150
147
|
}
|
|
151
148
|
}
|
|
152
149
|
if (notOptedInAssetIds.length > 0 || nonZeroBalanceAssetIds.length > 0) {
|
|
153
|
-
throw new Error(`Account ${
|
|
150
|
+
throw new Error(`Account ${account}${notOptedInAssetIds.length > 0 ? ` is not opted-in to Asset${notOptedInAssetIds.length > 1 ? 's' : ''} ${notOptedInAssetIds.join(', ')}` : ''}${nonZeroBalanceAssetIds.length > 0
|
|
154
151
|
? ` has non-zero balance for Asset${nonZeroBalanceAssetIds.length > 1 ? 's' : ''} ${nonZeroBalanceAssetIds.join(', ')}`
|
|
155
152
|
: ''}; can't opt-out.`);
|
|
156
153
|
}
|
|
@@ -158,7 +155,7 @@ class AssetManager {
|
|
|
158
155
|
composer.addAssetOptOut({
|
|
159
156
|
...options,
|
|
160
157
|
creator: (await this.getById(BigInt(assetId))).creator,
|
|
161
|
-
sender,
|
|
158
|
+
sender: account,
|
|
162
159
|
assetId: BigInt(assetId),
|
|
163
160
|
});
|
|
164
161
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asset-manager.mjs","sources":["../../src/types/asset-manager.ts"],"sourcesContent":["import algosdk from 'algosdk'\nimport { Config } from '../config'\nimport { chunkArray } from '../util'\nimport { AccountAssetInformation, TransactionSignerAccount } from './account'\nimport AlgoKitComposer, { CommonTransactionParams, MAX_TRANSACTION_GROUP_SIZE } from './composer'\nimport { SendParams } from './transaction'\nimport AssetModel = algosdk.modelsv2.Asset\n\n/** Individual result from performing a bulk opt-in or bulk opt-out for an account against a series of assets. */\nexport interface BulkAssetOptInOutResult {\n /** The ID of the asset opted into / out of */\n assetId: bigint\n /** The transaction ID of the resulting opt in / out */\n transactionId: string\n}\n\n/** Information about an asset. */\nexport interface AssetInformation {\n /** The ID of the asset. */\n assetId: bigint\n\n /** The address of the account that created the asset.\n *\n * This is the address where the parameters for this asset can be found,\n * and also the address where unwanted asset units can be sent when\n * closing out an asset position and opting-out of the asset.\n */\n creator: string\n\n /** The total amount of the smallest divisible (decimal) units that were created of the asset.\n *\n * For example, if `decimals` is, say, 2, then for every 100 `total` there is 1 whole unit.\n */\n total: bigint\n\n /** The amount of decimal places the asset was created with.\n *\n * * If 0, the asset is not divisible;\n * * If 1, the base unit of the asset is in tenths;\n * * If 2, the base unit of the asset is in hundredths;\n * * If 3, the base unit of the asset is in thousandths;\n * * and so on up to 19 decimal places.\n */\n decimals: number\n\n /** Whether the asset was frozen by default for all accounts.\n *\n * If `true` then for anyone apart from the creator to hold the\n * asset it needs to be unfrozen per account using an asset freeze\n * transaction from the `freeze` account.\n */\n defaultFrozen?: boolean\n\n /** The address of the optional account that can manage the configuration of the asset and destroy it.\n *\n * If not set the asset is permanently immutable.\n */\n manager?: string\n\n /**\n * The address of the optional account that holds the reserve (uncirculated supply) units of the asset.\n *\n * This address has no specific authority in the protocol itself and is informational only.\n *\n * Some standards like [ARC-19](https://github.com/algorandfoundation/ARCs/blob/main/ARCs/arc-0019.md)\n * rely on this field to hold meaningful data.\n *\n * It can be used in the case where you want to signal to holders of your asset that the uncirculated units\n * of the asset reside in an account that is different from the default creator account.\n *\n * If not set the field is permanently empty.\n */\n reserve?: string\n\n /**\n * The address of the optional account that can be used to freeze or unfreeze holdings of this asset for any account.\n *\n * If empty, freezing is not permitted.\n *\n * If not set the field is permanently empty.\n */\n freeze?: string\n\n /**\n * The address of the optional account that can clawback holdings of this asset from any account.\n *\n * The clawback account has the ability to **unconditionally take assets from any account**.\n *\n * If empty, clawback is not permitted.\n *\n * If not set the field is permanently empty.\n */\n clawback?: string\n\n /** The optional name of the unit of this asset (e.g. ticker name).\n *\n * Max size is 8 bytes.\n */\n unitName?: string\n\n /** The optional name of the unit of this asset (e.g. ticker name).\n *\n * Max size is 8 bytes.\n */\n unitNameAsBytes?: Uint8Array\n\n /** The optional name of the asset.\n *\n * Max size is 32 bytes.\n */\n assetName?: string\n\n /** The optional name of the asset.\n *\n * Max size is 32 bytes.\n */\n assetNameAsBytes?: Uint8Array\n\n /** Optional URL where more information about the asset can be retrieved (e.g. metadata).\n *\n * Max size is 96 bytes.\n */\n url?: string\n\n /** Optional URL where more information about the asset can be retrieved (e.g. metadata).\n *\n * Max size is 96 bytes.\n */\n urlAsBytes?: Uint8Array\n\n /** 32-byte hash of some metadata that is relevant to the asset and/or asset holders.\n *\n * The format of this metadata is up to the application.\n */\n metadataHash?: Uint8Array\n}\n\n/** Allows management of asset information. */\nexport class AssetManager {\n private _algod: algosdk.Algodv2\n private _newGroup: () => AlgoKitComposer\n\n /**\n * Create a new asset manager.\n * @param algod An algod client\n * @param newGroup A function that creates a new `AlgoKitComposer` transaction group\n * @example Create a new asset manager\n * ```typescript\n * const assetManager = new AssetManager(algod, () => new AlgoKitComposer({algod, () => signer, () => suggestedParams}))\n * ```\n */\n constructor(algod: algosdk.Algodv2, newGroup: () => AlgoKitComposer) {\n this._algod = algod\n this._newGroup = newGroup\n }\n\n /**\n * Returns the current asset information for the asset with the given ID.\n *\n * @example\n * ```typescript\n * const assetInfo = await assetManager.getById(12353n);\n * ```\n *\n * @param assetId The ID of the asset\n * @returns The asset information\n */\n public async getById(assetId: bigint): Promise<AssetInformation> {\n const asset = AssetModel.from_obj_for_encoding(await this._algod.getAssetByID(Number(assetId)).do())\n\n return {\n assetId: BigInt(asset.index),\n total: BigInt(asset.params.total),\n decimals: Number(asset.params.decimals),\n assetName: asset.params.name,\n assetNameAsBytes: asset.params.nameB64,\n unitName: asset.params.unitName,\n unitNameAsBytes: asset.params.unitNameB64,\n url: asset.params.url,\n urlAsBytes: asset.params.urlB64,\n creator: asset.params.creator,\n manager: asset.params.manager,\n clawback: asset.params.clawback,\n freeze: asset.params.freeze,\n reserve: asset.params.reserve,\n defaultFrozen: asset.params.defaultFrozen,\n metadataHash: asset.params.metadataHash,\n }\n }\n\n /**\n * Returns the given sender account's asset holding for a given asset.\n *\n * @example\n * ```typescript\n * const address = \"XBYLS2E6YI6XXL5BWCAMOA4GTWHXWENZMX5UHXMRNWWUQ7BXCY5WC5TEPA\";\n * const assetId = 123345n;\n * const accountInfo = await algorand.asset.getAccountInformation(address, assetId);\n * ```\n *\n * [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/#get-v2accountsaddressassetsasset-id)\n * @param sender The address of the sender/account to look up\n * @param assetId The ID of the asset to return a holding for\n * @returns The account asset holding information\n */\n public async getAccountInformation(sender: string | TransactionSignerAccount, assetId: bigint): Promise<AccountAssetInformation> {\n const info = await this._algod.accountAssetInformation(typeof sender === 'string' ? sender : sender.addr, Number(assetId)).do()\n\n return {\n assetId: BigInt(assetId),\n balance: BigInt(info['asset-holding']['amount']),\n frozen: info['asset-holding']['is-frozen'] === true,\n round: BigInt(info['round']),\n }\n }\n\n /**\n * Opt an account in to a list of Algorand Standard Assets.\n *\n * Transactions will be sent in batches of 16 as transaction groups.\n *\n * @param account The account to opt-in\n * @param assetIds The list of asset IDs to opt-in to\n * @param options Any parameters to control the transaction or execution of the transaction\n * @example Example using AlgorandClient\n * ```typescript\n * // Basic example\n * algorand.asset.bulkOptIn(\"ACCOUNTADDRESS\", [12345n, 67890n])\n * // With configuration\n * algorand.asset.bulkOptIn(\"ACCOUNTADDRESS\", [12345n, 67890n], { maxFee: (1000).microAlgo(), suppressLog: true })\n * ```\n * @returns An array of records matching asset ID to transaction ID of the opt in\n */\n async bulkOptIn(\n account: string | TransactionSignerAccount,\n assetIds: bigint[],\n options?: Omit<CommonTransactionParams, 'sender'> & SendParams,\n ): Promise<BulkAssetOptInOutResult[]> {\n const results: BulkAssetOptInOutResult[] = []\n\n for (const assetGroup of chunkArray(assetIds, MAX_TRANSACTION_GROUP_SIZE)) {\n const composer = this._newGroup()\n\n for (const assetId of assetGroup) {\n composer.addAssetOptIn({\n ...options,\n sender: typeof account === 'string' ? account : account.addr,\n assetId: BigInt(assetId),\n })\n }\n\n const result = await composer.send(options)\n\n Config.getLogger(options?.suppressLog).info(\n `Successfully opted in ${account} for assets ${assetGroup.join(', ')} with transaction IDs ${result.txIds.join(', ')}` +\n `\\n Grouped under ${result.groupId} in round ${result.confirmations?.[0]?.confirmedRound}.`,\n )\n\n assetGroup.forEach((assetId, index) => {\n results.push({ assetId: BigInt(assetId), transactionId: result.txIds[index] })\n })\n }\n\n return results\n }\n\n /**\n * Opt an account out of a list of Algorand Standard Assets.\n *\n * Transactions will be sent in batches of 16 as transaction groups.\n *\n * @param account The account to opt-in\n * @param assetIds The list of asset IDs to opt-out of\n * @param options Any parameters to control the transaction or execution of the transaction\n * @example Example using AlgorandClient\n * ```typescript\n * // Basic example\n * algorand.asset.bulkOptOut(\"ACCOUNTADDRESS\", [12345n, 67890n])\n * // With configuration\n * algorand.asset.bulkOptOut(\"ACCOUNTADDRESS\", [12345n, 67890n], { ensureZeroBalance: true, maxFee: (1000).microAlgo(), suppressLog: true })\n * ```\n * @returns An array of records matching asset ID to transaction ID of the opt in\n */\n async bulkOptOut(\n account: string | TransactionSignerAccount,\n assetIds: bigint[],\n options?: Omit<CommonTransactionParams, 'sender'> &\n SendParams & {\n /** Whether or not to check if the account has a zero balance for each asset first or not.\n *\n * Defaults to `true`.\n *\n * If this is set to `true` and the account has an asset balance it will throw an error.\n *\n * If this is set to `false` and the account has an asset balance it will lose those assets to the asset creator.\n */\n ensureZeroBalance?: boolean\n },\n ): Promise<BulkAssetOptInOutResult[]> {\n const results: BulkAssetOptInOutResult[] = []\n\n const sender = typeof account === 'string' ? account : account.addr\n\n for (const assetGroup of chunkArray(assetIds, MAX_TRANSACTION_GROUP_SIZE)) {\n const composer = this._newGroup()\n\n const notOptedInAssetIds: bigint[] = []\n const nonZeroBalanceAssetIds: bigint[] = []\n for (const assetId of assetGroup) {\n if (options?.ensureZeroBalance !== false) {\n try {\n const accountAssetInfo = await this.getAccountInformation(sender, assetId)\n if (accountAssetInfo.balance !== 0n) {\n nonZeroBalanceAssetIds.push(BigInt(assetId))\n }\n } catch {\n notOptedInAssetIds.push(BigInt(assetId))\n }\n }\n }\n\n if (notOptedInAssetIds.length > 0 || nonZeroBalanceAssetIds.length > 0) {\n throw new Error(\n `Account ${sender}${notOptedInAssetIds.length > 0 ? ` is not opted-in to Asset${notOptedInAssetIds.length > 1 ? 's' : ''} ${notOptedInAssetIds.join(', ')}` : ''}${\n nonZeroBalanceAssetIds.length > 0\n ? ` has non-zero balance for Asset${nonZeroBalanceAssetIds.length > 1 ? 's' : ''} ${nonZeroBalanceAssetIds.join(', ')}`\n : ''\n }; can't opt-out.`,\n )\n }\n\n for (const assetId of assetGroup) {\n composer.addAssetOptOut({\n ...options,\n creator: (await this.getById(BigInt(assetId))).creator,\n sender,\n assetId: BigInt(assetId),\n })\n }\n\n const result = await composer.send(options)\n\n Config.getLogger(options?.suppressLog).info(\n `Successfully opted ${account} out of assets ${assetGroup.join(', ')} with transaction IDs ${result.txIds.join(', ')}` +\n `\\n Grouped under ${result.groupId} in round ${result.confirmations?.[0]?.confirmedRound}.`,\n )\n\n assetGroup.forEach((assetId, index) => {\n results.push({ assetId: BigInt(assetId), transactionId: result.txIds[index] })\n })\n }\n\n return results\n }\n}\n"],"names":[],"mappings":";;;;;AAMA,IAAO,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAA;AAmI1C;MACa,YAAY,CAAA;AAIvB;;;;;;;;AAQG;IACH,WAAY,CAAA,KAAsB,EAAE,QAA+B,EAAA;AACjE,QAAA,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;AACnB,QAAA,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAA;KAC1B;AAED;;;;;;;;;;AAUG;IACI,MAAM,OAAO,CAAC,OAAe,EAAA;QAClC,MAAM,KAAK,GAAG,UAAU,CAAC,qBAAqB,CAAC,MAAM,IAAI,CAAC,MAAM,CAA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CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAE,CAAA;AACpH,gBAAA,CAAA,kBAAA,EAAqB,MAAM,CAAC,OAAO,CAAA,UAAA,EAAa,MAAM,CAAC,aAAa,GAAG,CAAC,CAAC,EAAE,cAAc,CAAA,CAAA,CAAG,CAC/F,CAAA;YAED,UAAU,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,KAAK,KAAI;gBACpC,OAAO,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE,aAAa,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;AAChF,aAAC,CAAC,CAAA;SACH;AAED,QAAA,OAAO,OAAO,CAAA;KACf;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"asset-manager.mjs","sources":["../../src/types/asset-manager.ts"],"sourcesContent":["import algosdk, { Address } from 'algosdk'\nimport { Config } from '../config'\nimport { chunkArray } from '../util'\nimport { AccountAssetInformation } from './account'\nimport { CommonTransactionParams, MAX_TRANSACTION_GROUP_SIZE, TransactionComposer } from './composer'\nimport { SendParams } from './transaction'\n\n/** Individual result from performing a bulk opt-in or bulk opt-out for an account against a series of assets. */\nexport interface BulkAssetOptInOutResult {\n /** The ID of the asset opted into / out of */\n assetId: bigint\n /** The transaction ID of the resulting opt in / out */\n transactionId: string\n}\n\n/** Information about an asset. */\nexport interface AssetInformation {\n /** The ID of the asset. */\n assetId: bigint\n\n /** The address of the account that created the asset.\n *\n * This is the address where the parameters for this asset can be found,\n * and also the address where unwanted asset units can be sent when\n * closing out an asset position and opting-out of the asset.\n */\n creator: string\n\n /** The total amount of the smallest divisible (decimal) units that were created of the asset.\n *\n * For example, if `decimals` is, say, 2, then for every 100 `total` there is 1 whole unit.\n */\n total: bigint\n\n /** The amount of decimal places the asset was created with.\n *\n * * If 0, the asset is not divisible;\n * * If 1, the base unit of the asset is in tenths;\n * * If 2, the base unit of the asset is in hundredths;\n * * If 3, the base unit of the asset is in thousandths;\n * * and so on up to 19 decimal places.\n */\n decimals: number\n\n /** Whether the asset was frozen by default for all accounts.\n *\n * If `true` then for anyone apart from the creator to hold the\n * asset it needs to be unfrozen per account using an asset freeze\n * transaction from the `freeze` account.\n */\n defaultFrozen?: boolean\n\n /** The address of the optional account that can manage the configuration of the asset and destroy it.\n *\n * If not set the asset is permanently immutable.\n */\n manager?: string\n\n /**\n * The address of the optional account that holds the reserve (uncirculated supply) units of the asset.\n *\n * This address has no specific authority in the protocol itself and is informational only.\n *\n * Some standards like [ARC-19](https://github.com/algorandfoundation/ARCs/blob/main/ARCs/arc-0019.md)\n * rely on this field to hold meaningful data.\n *\n * It can be used in the case where you want to signal to holders of your asset that the uncirculated units\n * of the asset reside in an account that is different from the default creator account.\n *\n * If not set the field is permanently empty.\n */\n reserve?: string\n\n /**\n * The address of the optional account that can be used to freeze or unfreeze holdings of this asset for any account.\n *\n * If empty, freezing is not permitted.\n *\n * If not set the field is permanently empty.\n */\n freeze?: string\n\n /**\n * The address of the optional account that can clawback holdings of this asset from any account.\n *\n * The clawback account has the ability to **unconditionally take assets from any account**.\n *\n * If empty, clawback is not permitted.\n *\n * If not set the field is permanently empty.\n */\n clawback?: string\n\n /** The optional name of the unit of this asset (e.g. ticker name).\n *\n * Max size is 8 bytes.\n */\n unitName?: string\n\n /** The optional name of the unit of this asset (e.g. ticker name).\n *\n * Max size is 8 bytes.\n */\n unitNameAsBytes?: Uint8Array\n\n /** The optional name of the asset.\n *\n * Max size is 32 bytes.\n */\n assetName?: string\n\n /** The optional name of the asset.\n *\n * Max size is 32 bytes.\n */\n assetNameAsBytes?: Uint8Array\n\n /** Optional URL where more information about the asset can be retrieved (e.g. metadata).\n *\n * Max size is 96 bytes.\n */\n url?: string\n\n /** Optional URL where more information about the asset can be retrieved (e.g. metadata).\n *\n * Max size is 96 bytes.\n */\n urlAsBytes?: Uint8Array\n\n /** 32-byte hash of some metadata that is relevant to the asset and/or asset holders.\n *\n * The format of this metadata is up to the application.\n */\n metadataHash?: Uint8Array\n}\n\n/** Allows management of asset information. */\nexport class AssetManager {\n private _algod: algosdk.Algodv2\n private _newGroup: () => TransactionComposer\n\n /**\n * Create a new asset manager.\n * @param algod An algod client\n * @param newGroup A function that creates a new `TransactionComposer` transaction group\n * @example Create a new asset manager\n * ```typescript\n * const assetManager = new AssetManager(algod, () => new TransactionComposer({algod, () => signer, () => suggestedParams}))\n * ```\n */\n constructor(algod: algosdk.Algodv2, newGroup: () => TransactionComposer) {\n this._algod = algod\n this._newGroup = newGroup\n }\n\n /**\n * Returns the current asset information for the asset with the given ID.\n *\n * @example\n * ```typescript\n * const assetInfo = await assetManager.getById(12353n);\n * ```\n *\n * @param assetId The ID of the asset\n * @returns The asset information\n */\n public async getById(assetId: bigint): Promise<AssetInformation> {\n const asset = await this._algod.getAssetByID(Number(assetId)).do()\n\n return {\n assetId: BigInt(asset.index),\n total: BigInt(asset.params.total),\n decimals: Number(asset.params.decimals),\n assetName: asset.params.name,\n assetNameAsBytes: asset.params.nameB64,\n unitName: asset.params.unitName,\n unitNameAsBytes: asset.params.unitNameB64,\n url: asset.params.url,\n urlAsBytes: asset.params.urlB64,\n creator: asset.params.creator,\n manager: asset.params.manager,\n clawback: asset.params.clawback,\n freeze: asset.params.freeze,\n reserve: asset.params.reserve,\n defaultFrozen: asset.params.defaultFrozen,\n metadataHash: asset.params.metadataHash,\n }\n }\n\n /**\n * Returns the given sender account's asset holding for a given asset.\n *\n * @example\n * ```typescript\n * const address = \"XBYLS2E6YI6XXL5BWCAMOA4GTWHXWENZMX5UHXMRNWWUQ7BXCY5WC5TEPA\";\n * const assetId = 123345n;\n * const accountInfo = await algorand.asset.getAccountInformation(address, assetId);\n * ```\n *\n * [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/#get-v2accountsaddressassetsasset-id)\n * @param sender The address of the sender/account to look up\n * @param assetId The ID of the asset to return a holding for\n * @returns The account asset holding information\n */\n public async getAccountInformation(sender: string | Address, assetId: bigint): Promise<AccountAssetInformation> {\n const info = await this._algod.accountAssetInformation(sender, Number(assetId)).do()\n\n return {\n assetId: BigInt(assetId),\n balance: BigInt(info.assetHolding?.amount ?? 0),\n frozen: info.assetHolding?.isFrozen === true,\n round: BigInt(info['round']),\n }\n }\n\n /**\n * Opt an account in to a list of Algorand Standard Assets.\n *\n * Transactions will be sent in batches of 16 as transaction groups.\n *\n * @param account The account to opt-in\n * @param assetIds The list of asset IDs to opt-in to\n * @param options Any parameters to control the transaction or execution of the transaction\n * @example Example using AlgorandClient\n * ```typescript\n * // Basic example\n * algorand.asset.bulkOptIn(\"ACCOUNTADDRESS\", [12345n, 67890n])\n * // With configuration\n * algorand.asset.bulkOptIn(\"ACCOUNTADDRESS\", [12345n, 67890n], { maxFee: (1000).microAlgo(), suppressLog: true })\n * ```\n * @returns An array of records matching asset ID to transaction ID of the opt in\n */\n async bulkOptIn(\n account: string | Address,\n assetIds: bigint[],\n options?: Omit<CommonTransactionParams, 'sender'> & SendParams,\n ): Promise<BulkAssetOptInOutResult[]> {\n const results: BulkAssetOptInOutResult[] = []\n\n for (const assetGroup of chunkArray(assetIds, MAX_TRANSACTION_GROUP_SIZE)) {\n const composer = this._newGroup()\n\n for (const assetId of assetGroup) {\n composer.addAssetOptIn({\n ...options,\n sender: account,\n assetId: BigInt(assetId),\n })\n }\n\n const result = await composer.send(options)\n\n Config.getLogger(options?.suppressLog).info(\n `Successfully opted in ${account} for assets ${assetGroup.join(', ')} with transaction IDs ${result.txIds.join(', ')}` +\n `\\n Grouped under ${result.groupId} in round ${result.confirmations?.[0]?.confirmedRound}.`,\n )\n\n assetGroup.forEach((assetId, index) => {\n results.push({ assetId: BigInt(assetId), transactionId: result.txIds[index] })\n })\n }\n\n return results\n }\n\n /**\n * Opt an account out of a list of Algorand Standard Assets.\n *\n * Transactions will be sent in batches of 16 as transaction groups.\n *\n * @param account The account to opt-in\n * @param assetIds The list of asset IDs to opt-out of\n * @param options Any parameters to control the transaction or execution of the transaction\n * @example Example using AlgorandClient\n * ```typescript\n * // Basic example\n * algorand.asset.bulkOptOut(\"ACCOUNTADDRESS\", [12345n, 67890n])\n * // With configuration\n * algorand.asset.bulkOptOut(\"ACCOUNTADDRESS\", [12345n, 67890n], { ensureZeroBalance: true, maxFee: (1000).microAlgo(), suppressLog: true })\n * ```\n * @returns An array of records matching asset ID to transaction ID of the opt in\n */\n async bulkOptOut(\n account: string | Address,\n assetIds: bigint[],\n options?: Omit<CommonTransactionParams, 'sender'> &\n SendParams & {\n /** Whether or not to check if the account has a zero balance for each asset first or not.\n *\n * Defaults to `true`.\n *\n * If this is set to `true` and the account has an asset balance it will throw an error.\n *\n * If this is set to `false` and the account has an asset balance it will lose those assets to the asset creator.\n */\n ensureZeroBalance?: boolean\n },\n ): Promise<BulkAssetOptInOutResult[]> {\n const results: BulkAssetOptInOutResult[] = []\n\n for (const assetGroup of chunkArray(assetIds, MAX_TRANSACTION_GROUP_SIZE)) {\n const composer = this._newGroup()\n\n const notOptedInAssetIds: bigint[] = []\n const nonZeroBalanceAssetIds: bigint[] = []\n for (const assetId of assetGroup) {\n if (options?.ensureZeroBalance !== false) {\n try {\n const accountAssetInfo = await this.getAccountInformation(account, assetId)\n if (accountAssetInfo.balance !== 0n) {\n nonZeroBalanceAssetIds.push(BigInt(assetId))\n }\n } catch {\n notOptedInAssetIds.push(BigInt(assetId))\n }\n }\n }\n\n if (notOptedInAssetIds.length > 0 || nonZeroBalanceAssetIds.length > 0) {\n throw new Error(\n `Account ${account}${notOptedInAssetIds.length > 0 ? ` is not opted-in to Asset${notOptedInAssetIds.length > 1 ? 's' : ''} ${notOptedInAssetIds.join(', ')}` : ''}${\n nonZeroBalanceAssetIds.length > 0\n ? ` has non-zero balance for Asset${nonZeroBalanceAssetIds.length > 1 ? 's' : ''} ${nonZeroBalanceAssetIds.join(', ')}`\n : ''\n }; can't opt-out.`,\n )\n }\n\n for (const assetId of assetGroup) {\n composer.addAssetOptOut({\n ...options,\n creator: (await this.getById(BigInt(assetId))).creator,\n sender: account,\n assetId: BigInt(assetId),\n })\n }\n\n const result = await composer.send(options)\n\n Config.getLogger(options?.suppressLog).info(\n `Successfully opted ${account} out of assets ${assetGroup.join(', ')} with transaction IDs ${result.txIds.join(', ')}` +\n `\\n Grouped under ${result.groupId} in round ${result.confirmations?.[0]?.confirmedRound}.`,\n )\n\n assetGroup.forEach((assetId, index) => {\n results.push({ assetId: BigInt(assetId), transactionId: result.txIds[index] })\n })\n }\n\n return results\n }\n}\n"],"names":[],"mappings":";;;;AAwIA;MACa,YAAY,CAAA;AAIvB;;;;;;;;AAQG;IACH,WAAY,CAAA,KAAsB,EAAE,QAAmC,EAAA;AACrE,QAAA,IAAI,CAAC,MAAM,GAAG,KAAK;AACnB,QAAA,IAAI,CAAC,SAAS,GAAG,QAAQ;;AAG3B;;;;;;;;;;AAUG;IACI,MAAM,OAAO,CAAC,OAAe,EAAA;AAClC,QAAA,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE;QAElE,OAAO;AACL,YAAA,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;YAC5B,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC;YACjC,QAAQ,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC;AACvC,YAAA,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,IAAI;AAC5B,YAAA,gBAAgB,EAAE,KAAK,CAAC,MAAM,CAAC,OAAO;AACtC,YAAA,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,QAAQ;AAC/B,YAAA,eAAe,EAAE,KAAK,CAAC,MAAM,CAAC,WAAW;AACzC,YAAA,GAAG,EAAE,KAAK,CAAC,MAAM,CAAC,GAAG;AACrB,YAAA,UAAU,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM;AAC/B,YA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AG,MAAM,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC;AAE3C,YAAA,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC,IAAI,CACzC,CAAsB,mBAAA,EAAA,OAAO,CAAkB,eAAA,EAAA,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA,sBAAA,EAAyB,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAE,CAAA;AACpH,gBAAA,CAAA,kBAAA,EAAqB,MAAM,CAAC,OAAO,CAAA,UAAA,EAAa,MAAM,CAAC,aAAa,GAAG,CAAC,CAAC,EAAE,cAAc,CAAA,CAAA,CAAG,CAC/F;YAED,UAAU,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,KAAK,KAAI;gBACpC,OAAO,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE,aAAa,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;AAChF,aAAC,CAAC;;AAGJ,QAAA,OAAO,OAAO;;AAEjB;;;;"}
|
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare enum EventType {
|
|
3
|
-
TxnGroupSimulated = "TxnGroupSimulated",
|
|
4
|
-
AppCompiled = "AppCompiled"
|
|
5
|
-
}
|
|
6
|
-
export type EventDataMap = {
|
|
7
|
-
[EventType.TxnGroupSimulated]: AVMTracesEventData;
|
|
8
|
-
[EventType.AppCompiled]: TealSourcesDebugEventData;
|
|
9
|
-
[key: string]: unknown;
|
|
10
|
-
};
|
|
1
|
+
import { EventDataMap, EventType } from './lifecycle-events';
|
|
11
2
|
export type AsyncEventListener<T = unknown> = (event: T, eventName: string | symbol) => Promise<void> | void;
|
|
12
3
|
export declare class AsyncEventEmitter {
|
|
13
4
|
private listenerWrapperMap;
|
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
exports.EventType = void 0;
|
|
4
|
-
(function (EventType) {
|
|
5
|
-
EventType["TxnGroupSimulated"] = "TxnGroupSimulated";
|
|
6
|
-
EventType["AppCompiled"] = "AppCompiled";
|
|
7
|
-
})(exports.EventType || (exports.EventType = {}));
|
|
8
3
|
class AsyncEventEmitter {
|
|
9
4
|
constructor() {
|
|
10
5
|
this.listenerWrapperMap = new WeakMap();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async-event-emitter.js","sources":["../../src/types/async-event-emitter.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"async-event-emitter.js","sources":["../../src/types/async-event-emitter.ts"],"sourcesContent":["import { EventDataMap, EventType } from './lifecycle-events'\n\nexport type AsyncEventListener<T = unknown> = (event: T, eventName: string | symbol) => Promise<void> | void\n\nexport class AsyncEventEmitter {\n private listenerWrapperMap = new WeakMap<AsyncEventListener, AsyncEventListener>()\n private listenerMap: Record<string | symbol, AsyncEventListener[]> = {}\n\n async emitAsync<K extends EventType>(eventName: K, event: EventDataMap[K]): Promise<void>\n async emitAsync(eventName: string | symbol, event: unknown): Promise<void>\n async emitAsync(eventName: string | symbol, event: unknown): Promise<void> {\n for (const listener of this.listenerMap[eventName] ?? []) {\n await listener(event, eventName)\n }\n }\n\n on<K extends EventType>(eventName: K, listener: AsyncEventListener<EventDataMap[K]>): AsyncEventEmitter\n on<T = unknown>(eventName: string | symbol, listener: AsyncEventListener<T>): AsyncEventEmitter\n on(eventName: string | symbol, listener: AsyncEventListener): AsyncEventEmitter {\n if (!this.listenerMap[eventName]) this.listenerMap[eventName] = []\n this.listenerMap[eventName].push(listener as AsyncEventListener)\n return this\n }\n\n once<K extends EventType>(eventName: K, listener: AsyncEventListener<EventDataMap[K]>): AsyncEventEmitter\n once<T = unknown>(eventName: string | symbol, listener: AsyncEventListener<T>): AsyncEventEmitter\n once(eventName: string | symbol, listener: AsyncEventListener): AsyncEventEmitter {\n const wrappedListener: AsyncEventListener = async (event, eventName) => {\n try {\n return await listener(event, eventName)\n } finally {\n this.removeListener(eventName, wrappedListener)\n }\n }\n this.listenerWrapperMap.set(listener, wrappedListener)\n return this.on(eventName, wrappedListener)\n }\n\n removeListener(eventName: string | symbol, listener: AsyncEventListener): AsyncEventEmitter {\n const wrappedListener = this.listenerWrapperMap.get(listener)\n if (wrappedListener) {\n this.listenerWrapperMap.delete(listener)\n if (this.listenerMap[eventName]?.indexOf(wrappedListener) !== -1) {\n this.listenerMap[eventName].splice(this.listenerMap[eventName].indexOf(wrappedListener), 1)\n }\n } else {\n if (this.listenerMap[eventName]?.indexOf(listener) !== -1) {\n this.listenerMap[eventName].splice(this.listenerMap[eventName].indexOf(listener), 1)\n }\n }\n\n return this\n }\n\n off = this.removeListener\n}\n"],"names":[],"mappings":";;MAIa,iBAAiB,CAAA;AAA9B,IAAA,WAAA,GAAA;AACU,QAAA,IAAA,CAAA,kBAAkB,GAAG,IAAI,OAAO,EAA0C;QAC1E,IAAW,CAAA,WAAA,GAAkD,EAAE;AAgDvE,QAAA,IAAA,CAAA,GAAG,GAAG,IAAI,CAAC,cAAc;;AA5CzB,IAAA,MAAM,SAAS,CAAC,SAA0B,EAAE,KAAc,EAAA;AACxD,QAAA,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE;AACxD,YAAA,MAAM,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;;;IAMpC,EAAE,CAAC,SAA0B,EAAE,QAA4B,EAAA;AACzD,QAAA,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC;AAAE,YAAA,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,GAAG,EAAE;QAClE,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,QAA8B,CAAC;AAChE,QAAA,OAAO,IAAI;;IAKb,IAAI,CAAC,SAA0B,EAAE,QAA4B,EAAA;QAC3D,MAAM,eAAe,GAAuB,OAAO,K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ppCompiled";
|
|
5
|
-
})(EventType || (EventType = {}));
|
|
6
1
|
class AsyncEventEmitter {
|
|
7
2
|
constructor() {
|
|
8
3
|
this.listenerWrapperMap = new WeakMap();
|
|
@@ -49,5 +44,5 @@ class AsyncEventEmitter {
|
|
|
49
44
|
}
|
|
50
45
|
}
|
|
51
46
|
|
|
52
|
-
export { AsyncEventEmitter
|
|
47
|
+
export { AsyncEventEmitter };
|
|
53
48
|
//# sourceMappingURL=async-event-emitter.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async-event-emitter.mjs","sources":["../../src/types/async-event-emitter.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"async-event-emitter.mjs","sources":["../../src/types/async-event-emitter.ts"],"sourcesContent":["import { EventDataMap, EventType } from './lifecycle-events'\n\nexport type AsyncEventListener<T = unknown> = (event: T, eventName: string | symbol) => Promise<void> | void\n\nexport class AsyncEventEmitter {\n private listenerWrapperMap = new WeakMap<AsyncEventListener, AsyncEventListener>()\n private listenerMap: Record<string | symbol, AsyncEventListener[]> = {}\n\n async emitAsync<K extends EventType>(eventName: K, event: EventDataMap[K]): Promise<void>\n async emitAsync(eventName: string | symbol, event: unknown): Promise<void>\n async emitAsync(eventName: string | symbol, event: unknown): Promise<void> {\n for (const listener of this.listenerMap[eventName] ?? []) {\n await listener(event, eventName)\n }\n }\n\n on<K extends EventType>(eventName: K, listener: AsyncEventListener<EventDataMap[K]>): AsyncEventEmitter\n on<T = unknown>(eventName: string | symbol, listener: AsyncEventListener<T>): AsyncEventEmitter\n on(eventName: string | symbol, listener: AsyncEventListener): AsyncEventEmitter {\n if (!this.listenerMap[eventName]) this.listenerMap[eventName] = []\n this.listenerMap[eventName].push(listener as AsyncEventListener)\n return this\n }\n\n once<K extends EventType>(eventName: K, listener: AsyncEventListener<EventDataMap[K]>): AsyncEventEmitter\n once<T = unknown>(eventName: string | symbol, listener: AsyncEventListener<T>): AsyncEventEmitter\n once(eventName: string | symbol, listener: AsyncEventListener): AsyncEventEmitter {\n const wrappedListener: AsyncEventListener = async (event, eventName) => {\n try {\n return await listener(event, eventName)\n } finally {\n this.removeListener(eventName, wrappedListener)\n }\n }\n this.listenerWrapperMap.set(listener, wrappedListener)\n return this.on(eventName, wrappedListener)\n }\n\n removeListener(eventName: string | symbol, listener: AsyncEventListener): AsyncEventEmitter {\n const wrappedListener = this.listenerWrapperMap.get(listener)\n if (wrappedListener) {\n this.listenerWrapperMap.delete(listener)\n if (this.listenerMap[eventName]?.indexOf(wrappedListener) !== -1) {\n this.listenerMap[eventName].splice(this.listenerMap[eventName].indexOf(wrappedListener), 1)\n }\n } else {\n if (this.listenerMap[eventName]?.indexOf(listener) !== -1) {\n this.listenerMap[eventName].splice(this.listenerMap[eventName].indexOf(listener), 1)\n }\n }\n\n return this\n }\n\n off = this.removeListener\n}\n"],"names":[],"mappings":"MAIa,iBAAiB,CAAA;AAA9B,IAAA,WAAA,GAAA;AACU,QAAA,IAAA,CAAA,kBAAkB,GAAG,IAAI,OAAO,EAA0C;QAC1E,IAAW,CAAA,WAAA,GAAkD,EAAE;AAgDvE,QAAA,IAAA,CAAA,GAAG,GAAG,IAAI,CAAC,cAAc;;AA5CzB,IAAA,MAAM,SAAS,CAAC,SAA0B,EAAE,KAAc,EAAA;AACxD,QAAA,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE;AACxD,YAAA,MAAM,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;;;IAMpC,EAAE,CAAC,SAA0B,EAAE,QAA4B,EAAA;AACzD,QAAA,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC;AAAE,YAAA,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,GAAG,EAAE;QAClE,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,QAA8B,CAAC;AAChE,QAAA,OAAO,IAAI;;IAKb,IAAI,CAA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md = algosdk.Kmd;
|
|
9
9
|
import Indexer = algosdk.Indexer;
|
|
10
10
|
import Algodv2 = algosdk.Algodv2;
|
|
11
|
-
import IntDecoding = algosdk.IntDecoding;
|
|
12
11
|
/** Clients from algosdk that interact with the official Algorand APIs */
|
|
13
12
|
export interface AlgoSdkClients {
|
|
14
13
|
/** Algod client, see https://developer.algorand.org/docs/rest-apis/algod/ */
|
|
@@ -343,7 +342,6 @@ export declare class ClientManager {
|
|
|
343
342
|
* Returns an indexer SDK client that automatically retries on idempotent calls
|
|
344
343
|
*
|
|
345
344
|
* @param config The config of the client
|
|
346
|
-
* @param overrideIntDecoding Override the default int decoding for responses, uses MIXED by default to avoid lost precision for big integers
|
|
347
345
|
* @example AlgoNode (testnet)
|
|
348
346
|
* ```typescript
|
|
349
347
|
* const indexer = ClientManager.getIndexerClient(ClientManager.getAlgoNodeConfig('testnet', 'indexer'))
|
|
@@ -359,16 +357,11 @@ export declare class ClientManager {
|
|
|
359
357
|
* const indexer = ClientManager.getIndexerClient({server: 'http://localhost', port: '8980', token: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'})
|
|
360
358
|
* await indexer.makeHealthCheck().do()
|
|
361
359
|
* ```
|
|
362
|
-
* @example Override int decoding for responses
|
|
363
|
-
* ```typescript
|
|
364
|
-
* const indexer = ClientManager.getIndexerClient(config, IntDecoding.BIGINT)
|
|
365
|
-
* ```
|
|
366
360
|
*/
|
|
367
|
-
static getIndexerClient(config: AlgoClientConfig
|
|
361
|
+
static getIndexerClient(config: AlgoClientConfig): Indexer;
|
|
368
362
|
/**
|
|
369
363
|
* Returns an indexer SDK client that automatically retries on idempotent calls loaded from environment variables (expects to be called from a Node.js environment).
|
|
370
364
|
*
|
|
371
|
-
* @param overrideIntDecoding Override the default int decoding for responses, uses MIXED by default to avoid lost precision for big integers
|
|
372
365
|
* @example
|
|
373
366
|
*
|
|
374
367
|
* ```typescript
|
|
@@ -377,7 +370,7 @@ export declare class ClientManager {
|
|
|
377
370
|
* await indexer.makeHealthCheck().do()
|
|
378
371
|
* ```
|
|
379
372
|
*/
|
|
380
|
-
static getIndexerClientFromEnvironment(
|
|
373
|
+
static getIndexerClientFromEnvironment(): Indexer;
|
|
381
374
|
/**
|
|
382
375
|
* Returns a KMD SDK client.
|
|
383
376
|
*
|
package/types/client-manager.js
CHANGED
|
@@ -9,7 +9,6 @@ var types_networkClient = require('./network-client.js');
|
|
|
9
9
|
|
|
10
10
|
var Kmd = algosdk.Kmd;
|
|
11
11
|
var Indexer = algosdk.Indexer;
|
|
12
|
-
var IntDecoding = algosdk.IntDecoding;
|
|
13
12
|
/** Exposes access to various API clients. */
|
|
14
13
|
class ClientManager {
|
|
15
14
|
/**
|
|
@@ -80,11 +79,11 @@ class ClientManager {
|
|
|
80
79
|
}
|
|
81
80
|
const params = await this._getNetworkPromise;
|
|
82
81
|
return {
|
|
83
|
-
isTestNet: ['testnet-v1.0', 'testnet-v1', 'testnet'].includes(params.genesisID),
|
|
84
|
-
isMainNet: ['mainnet-v1.0', 'mainnet-v1', 'mainnet'].includes(params.genesisID),
|
|
85
|
-
isLocalNet: ClientManager.genesisIdIsLocalNet(params.genesisID),
|
|
86
|
-
genesisId: params.genesisID,
|
|
87
|
-
genesisHash: params.genesisHash,
|
|
82
|
+
isTestNet: ['testnet-v1.0', 'testnet-v1', 'testnet'].includes(params.genesisID ?? 'unknown'),
|
|
83
|
+
isMainNet: ['mainnet-v1.0', 'mainnet-v1', 'mainnet'].includes(params.genesisID ?? 'unknown'),
|
|
84
|
+
isLocalNet: ClientManager.genesisIdIsLocalNet(params.genesisID ?? 'unknown'),
|
|
85
|
+
genesisId: params.genesisID ?? 'unknown',
|
|
86
|
+
genesisHash: params.genesisHash ? Buffer.from(params.genesisHash).toString('base64') : 'unknown',
|
|
88
87
|
};
|
|
89
88
|
}
|
|
90
89
|
/**
|
|
@@ -448,7 +447,7 @@ class ClientManager {
|
|
|
448
447
|
*/
|
|
449
448
|
static getAlgodClient(config) {
|
|
450
449
|
const { token, server, port } = config;
|
|
451
|
-
const tokenHeader = typeof token === 'string' ? { 'X-Algo-API-Token': token } : token ?? {};
|
|
450
|
+
const tokenHeader = typeof token === 'string' ? { 'X-Algo-API-Token': token } : (token ?? {});
|
|
452
451
|
const httpClientWithRetry = new types_algoHttpClientWithRetry.AlgoHttpClientWithRetry(tokenHeader, server, port);
|
|
453
452
|
return new algosdk.Algodv2(httpClientWithRetry, server);
|
|
454
453
|
}
|
|
@@ -469,7 +468,6 @@ class ClientManager {
|
|
|
469
468
|
* Returns an indexer SDK client that automatically retries on idempotent calls
|
|
470
469
|
*
|
|
471
470
|
* @param config The config of the client
|
|
472
|
-
* @param overrideIntDecoding Override the default int decoding for responses, uses MIXED by default to avoid lost precision for big integers
|
|
473
471
|
* @example AlgoNode (testnet)
|
|
474
472
|
* ```typescript
|
|
475
473
|
* const indexer = ClientManager.getIndexerClient(ClientManager.getAlgoNodeConfig('testnet', 'indexer'))
|
|
@@ -485,24 +483,16 @@ class ClientManager {
|
|
|
485
483
|
* const indexer = ClientManager.getIndexerClient({server: 'http://localhost', port: '8980', token: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'})
|
|
486
484
|
* await indexer.makeHealthCheck().do()
|
|
487
485
|
* ```
|
|
488
|
-
* @example Override int decoding for responses
|
|
489
|
-
* ```typescript
|
|
490
|
-
* const indexer = ClientManager.getIndexerClient(config, IntDecoding.BIGINT)
|
|
491
|
-
* ```
|
|
492
486
|
*/
|
|
493
|
-
static getIndexerClient(config
|
|
487
|
+
static getIndexerClient(config) {
|
|
494
488
|
const { token, server, port } = config;
|
|
495
|
-
const tokenHeader = typeof token === 'string' ? { 'X-Indexer-API-Token': token } : token ?? {};
|
|
489
|
+
const tokenHeader = typeof token === 'string' ? { 'X-Indexer-API-Token': token } : (token ?? {});
|
|
496
490
|
const httpClientWithRetry = new types_algoHttpClientWithRetry.AlgoHttpClientWithRetry(tokenHeader, server, port);
|
|
497
|
-
|
|
498
|
-
// Use mixed int decoding by default so bigints don't have lost precision
|
|
499
|
-
indexer.setIntEncoding(overrideIntDecoding ?? IntDecoding.MIXED);
|
|
500
|
-
return indexer;
|
|
491
|
+
return new Indexer(httpClientWithRetry);
|
|
501
492
|
}
|
|
502
493
|
/**
|
|
503
494
|
* Returns an indexer SDK client that automatically retries on idempotent calls loaded from environment variables (expects to be called from a Node.js environment).
|
|
504
495
|
*
|
|
505
|
-
* @param overrideIntDecoding Override the default int decoding for responses, uses MIXED by default to avoid lost precision for big integers
|
|
506
496
|
* @example
|
|
507
497
|
*
|
|
508
498
|
* ```typescript
|
|
@@ -511,8 +501,8 @@ class ClientManager {
|
|
|
511
501
|
* await indexer.makeHealthCheck().do()
|
|
512
502
|
* ```
|
|
513
503
|
*/
|
|
514
|
-
static getIndexerClientFromEnvironment(
|
|
515
|
-
return ClientManager.getIndexerClient(ClientManager.getIndexerConfigFromEnvironment()
|
|
504
|
+
static getIndexerClientFromEnvironment() {
|
|
505
|
+
return ClientManager.getIndexerClient(ClientManager.getIndexerConfigFromEnvironment());
|
|
516
506
|
}
|
|
517
507
|
/**
|
|
518
508
|
* Returns a KMD SDK client.
|