@algorandfoundation/algokit-utils 7.0.0-beta.2 → 7.0.0-beta.21
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 +3 -25
- package/account/account.d.ts +1 -2
- package/account/account.js +6 -4
- package/account/account.js.map +1 -1
- package/account/account.mjs +6 -4
- package/account/account.mjs.map +1 -1
- package/account/get-account-config-from-environment.d.ts +0 -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.d.ts +0 -1
- package/account/get-account.js.map +1 -1
- package/account/get-account.mjs.map +1 -1
- package/account/get-dispenser-account.d.ts +0 -1
- package/account/get-dispenser-account.js.map +1 -1
- package/account/get-dispenser-account.mjs.map +1 -1
- package/account/index.d.ts +0 -1
- package/account/mnemonic-account.d.ts +0 -1
- package/account/mnemonic-account.js.map +1 -1
- package/account/mnemonic-account.mjs.map +1 -1
- package/amount.d.ts +14 -5
- package/amount.js +6 -0
- package/amount.js.map +1 -1
- package/amount.mjs +6 -0
- package/amount.mjs.map +1 -1
- package/app-client.d.ts +15 -1
- package/app-client.js +15 -0
- package/app-client.js.map +1 -1
- package/app-client.mjs +15 -0
- package/app-client.mjs.map +1 -1
- package/app-deploy.d.ts +0 -1
- package/app-deploy.js +4 -6
- package/app-deploy.js.map +1 -1
- package/app-deploy.mjs +7 -9
- package/app-deploy.mjs.map +1 -1
- package/app.d.ts +10 -11
- package/app.js +15 -11
- package/app.js.map +1 -1
- package/app.mjs +15 -11
- package/app.mjs.map +1 -1
- package/asset.d.ts +3 -4
- package/asset.js +3 -6
- package/asset.js.map +1 -1
- package/asset.mjs +3 -6
- package/asset.mjs.map +1 -1
- package/config.d.ts +0 -1
- package/config.js.map +1 -1
- package/config.mjs.map +1 -1
- package/debugging/debugging.d.ts +3 -5
- package/debugging/debugging.js +4 -128
- package/debugging/debugging.js.map +1 -1
- package/debugging/debugging.mjs +4 -109
- package/debugging/debugging.mjs.map +1 -1
- package/debugging/index.d.ts +0 -2
- package/dispenser-client.d.ts +0 -1
- package/dispenser-client.js.map +1 -1
- package/dispenser-client.mjs.map +1 -1
- package/index.d.ts +6 -5
- package/index.js +48 -38
- package/index.js.map +1 -1
- package/index.mjs +6 -5
- package/index.mjs.map +1 -1
- package/indexer-lookup.d.ts +0 -1
- package/indexer-lookup.js.map +1 -1
- package/indexer-lookup.mjs.map +1 -1
- package/localnet/get-kmd-wallet-account.d.ts +0 -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.d.ts +0 -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.d.ts +0 -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/index.d.ts +0 -1
- package/localnet/is-localnet.d.ts +0 -1
- package/localnet/is-localnet.js.map +1 -1
- package/localnet/is-localnet.mjs.map +1 -1
- package/network-client.d.ts +0 -1
- package/network-client.js.map +1 -1
- package/network-client.mjs.map +1 -1
- package/package.json +2 -2
- package/testing/_asset.d.ts +0 -1
- package/testing/account.d.ts +0 -1
- package/testing/account.js.map +1 -1
- package/testing/account.mjs.map +1 -1
- package/testing/fixtures/algokit-log-capture-fixture.d.ts +0 -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.d.ts +0 -1
- package/testing/fixtures/algorand-fixture.js +1 -1
- package/testing/fixtures/algorand-fixture.js.map +1 -1
- package/testing/fixtures/algorand-fixture.mjs +1 -1
- package/testing/fixtures/algorand-fixture.mjs.map +1 -1
- package/testing/fixtures/index.d.ts +0 -1
- package/testing/index.d.ts +0 -1
- package/testing/indexer.d.ts +0 -1
- package/testing/indexer.js.map +1 -1
- package/testing/indexer.mjs.map +1 -1
- package/testing/test-logger.d.ts +0 -1
- package/testing/test-logger.js +1 -3
- package/testing/test-logger.js.map +1 -1
- package/testing/test-logger.mjs +1 -3
- package/testing/test-logger.mjs.map +1 -1
- package/testing/transaction-logger.d.ts +0 -1
- package/testing/transaction-logger.js.map +1 -1
- package/testing/transaction-logger.mjs.map +1 -1
- package/transaction/index.d.ts +0 -1
- package/transaction/legacy-bridge.d.ts +5 -6
- package/transaction/legacy-bridge.js +1 -1
- package/transaction/legacy-bridge.js.map +1 -1
- package/transaction/legacy-bridge.mjs +4 -4
- package/transaction/legacy-bridge.mjs.map +1 -1
- package/transaction/perform-atomic-transaction-composer-simulate.d.ts +0 -1
- package/transaction/perform-atomic-transaction-composer-simulate.js +1 -0
- package/transaction/perform-atomic-transaction-composer-simulate.js.map +1 -1
- package/transaction/perform-atomic-transaction-composer-simulate.mjs +1 -0
- package/transaction/perform-atomic-transaction-composer-simulate.mjs.map +1 -1
- package/transaction/transaction.d.ts +41 -6
- package/transaction/transaction.js +113 -61
- package/transaction/transaction.js.map +1 -1
- package/transaction/transaction.mjs +113 -62
- package/transaction/transaction.mjs.map +1 -1
- package/transfer/index.d.ts +0 -1
- package/transfer/transfer-algos.d.ts +1 -2
- package/transfer/transfer-algos.js +1 -1
- package/transfer/transfer-algos.js.map +1 -1
- package/transfer/transfer-algos.mjs +1 -1
- package/transfer/transfer-algos.mjs.map +1 -1
- package/transfer/transfer.d.ts +1 -2
- package/transfer/transfer.js +3 -3
- package/transfer/transfer.js.map +1 -1
- package/transfer/transfer.mjs +3 -3
- package/transfer/transfer.mjs.map +1 -1
- package/types/account-manager.d.ts +15 -7
- package/types/account-manager.js +27 -11
- package/types/account-manager.js.map +1 -1
- package/types/account-manager.mjs +29 -13
- package/types/account-manager.mjs.map +1 -1
- package/types/account.d.ts +9 -1
- package/types/account.js.map +1 -1
- package/types/account.mjs.map +1 -1
- package/types/algo-http-client-with-retry.d.ts +0 -1
- package/types/algo-http-client-with-retry.js.map +1 -1
- package/types/algo-http-client-with-retry.mjs.map +1 -1
- package/types/algorand-client-interface.d.ts +28 -0
- package/types/algorand-client-interface.js +3 -0
- package/types/algorand-client-interface.js.map +1 -0
- package/types/algorand-client-interface.mjs +2 -0
- package/types/algorand-client-interface.mjs.map +1 -0
- package/types/algorand-client-transaction-creator.d.ts +33 -34
- package/types/algorand-client-transaction-creator.js +31 -31
- package/types/algorand-client-transaction-creator.js.map +1 -1
- package/types/algorand-client-transaction-creator.mjs +31 -31
- package/types/algorand-client-transaction-creator.mjs.map +1 -1
- package/types/algorand-client-transaction-sender.d.ts +31 -32
- package/types/algorand-client-transaction-sender.js +25 -9
- package/types/algorand-client-transaction-sender.js.map +1 -1
- package/types/algorand-client-transaction-sender.mjs +25 -9
- package/types/algorand-client-transaction-sender.mjs.map +1 -1
- package/types/algorand-client.d.ts +13 -13
- package/types/algorand-client.js +10 -10
- package/types/algorand-client.js.map +1 -1
- package/types/algorand-client.mjs +12 -12
- package/types/algorand-client.mjs.map +1 -1
- package/types/amount.d.ts +10 -11
- package/types/amount.js +7 -7
- package/types/amount.js.map +1 -1
- package/types/amount.mjs +7 -7
- package/types/amount.mjs.map +1 -1
- package/types/app-arc56.d.ts +359 -0
- package/types/app-arc56.js +184 -0
- package/types/app-arc56.js.map +1 -0
- package/types/app-arc56.mjs +175 -0
- package/types/app-arc56.mjs.map +1 -0
- package/types/app-client.d.ts +1494 -8
- package/types/app-client.js +1009 -19
- package/types/app-client.js.map +1 -1
- package/types/app-client.mjs +1009 -20
- package/types/app-client.mjs.map +1 -1
- package/types/app-deployer.d.ts +19 -14
- package/types/app-deployer.js +25 -25
- package/types/app-deployer.js.map +1 -1
- package/types/app-deployer.mjs +27 -27
- package/types/app-deployer.mjs.map +1 -1
- package/types/app-factory.d.ts +846 -0
- package/types/app-factory.js +409 -0
- package/types/app-factory.js.map +1 -0
- package/types/app-factory.mjs +407 -0
- package/types/app-factory.mjs.map +1 -0
- package/types/app-manager.d.ts +12 -3
- package/types/app-manager.js +13 -15
- package/types/app-manager.js.map +1 -1
- package/types/app-manager.mjs +13 -15
- package/types/app-manager.mjs.map +1 -1
- package/types/app-spec.d.ts +2 -1
- package/types/app-spec.js +127 -0
- package/types/app-spec.js.map +1 -1
- package/types/app-spec.mjs +126 -0
- package/types/app-spec.mjs.map +1 -1
- package/types/app.d.ts +14 -9
- package/types/app.js.map +1 -1
- package/types/app.mjs.map +1 -1
- package/types/asset-manager.d.ts +7 -7
- package/types/asset-manager.js +5 -6
- package/types/asset-manager.js.map +1 -1
- package/types/asset-manager.mjs +5 -6
- package/types/asset-manager.mjs.map +1 -1
- package/types/asset.d.ts +0 -1
- package/types/async-event-emitter.d.ts +14 -0
- package/types/async-event-emitter.js +50 -0
- package/types/async-event-emitter.js.map +1 -0
- package/types/async-event-emitter.mjs +48 -0
- package/types/async-event-emitter.mjs.map +1 -0
- package/types/client-manager.d.ts +131 -51
- package/types/client-manager.js +143 -23
- package/types/client-manager.js.map +1 -1
- package/types/client-manager.mjs +144 -24
- package/types/client-manager.mjs.map +1 -1
- package/types/composer.d.ts +110 -37
- package/types/composer.js +139 -26
- package/types/composer.js.map +1 -1
- package/types/composer.mjs +140 -27
- package/types/composer.mjs.map +1 -1
- package/types/config.d.ts +3 -6
- package/types/config.js +5 -44
- package/types/config.js.map +1 -1
- package/types/config.mjs +5 -27
- package/types/config.mjs.map +1 -1
- package/types/debugging.d.ts +29 -101
- package/types/debugging.js +16 -109
- package/types/debugging.js.map +1 -1
- package/types/debugging.mjs +12 -107
- package/types/debugging.mjs.map +1 -1
- package/types/dispenser-client.d.ts +1 -2
- package/types/dispenser-client.js +6 -5
- package/types/dispenser-client.js.map +1 -1
- package/types/dispenser-client.mjs +6 -5
- package/types/dispenser-client.mjs.map +1 -1
- package/types/expand.d.ts +0 -1
- package/types/indexer.d.ts +0 -1
- package/types/indexer.js.map +1 -1
- package/types/indexer.mjs.map +1 -1
- package/types/kmd-account-manager.d.ts +0 -1
- package/types/kmd-account-manager.js +2 -2
- package/types/kmd-account-manager.js.map +1 -1
- package/types/kmd-account-manager.mjs +4 -4
- 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.d.ts +0 -1
- package/types/logging.js.map +1 -1
- package/types/logging.mjs.map +1 -1
- package/types/logic-error.d.ts +2 -4
- 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 +13 -1
- package/types/network-client.js.map +1 -1
- package/types/network-client.mjs.map +1 -1
- package/types/testing.d.ts +3 -4
- package/types/transaction.d.ts +14 -4
- package/types/transfer.d.ts +0 -1
- package/types/urlTokenBaseHTTPClient.d.ts +0 -1
- package/types/urlTokenBaseHTTPClient.js +1 -1
- package/types/urlTokenBaseHTTPClient.js.map +1 -1
- package/types/urlTokenBaseHTTPClient.mjs +1 -1
- package/types/urlTokenBaseHTTPClient.mjs.map +1 -1
- package/util.d.ts +2 -2
- package/util.js +11 -10
- package/util.js.map +1 -1
- package/util.mjs +11 -10
- package/util.mjs.map +1 -1
- package/account/account.d.ts.map +0 -1
- package/account/get-account-config-from-environment.d.ts.map +0 -1
- package/account/get-account.d.ts.map +0 -1
- package/account/get-dispenser-account.d.ts.map +0 -1
- package/account/index.d.ts.map +0 -1
- package/account/mnemonic-account.d.ts.map +0 -1
- package/amount.d.ts.map +0 -1
- package/app-client.d.ts.map +0 -1
- package/app-deploy.d.ts.map +0 -1
- package/app.d.ts.map +0 -1
- package/asset.d.ts.map +0 -1
- package/config.d.ts.map +0 -1
- package/debugging/debugging.d.ts.map +0 -1
- package/debugging/index.d.ts.map +0 -1
- package/debugging/simulate-and-persist-response.d.ts +0 -20
- package/debugging/simulate-and-persist-response.d.ts.map +0 -1
- package/debugging/simulate-and-persist-response.js +0 -108
- package/debugging/simulate-and-persist-response.js.map +0 -1
- package/debugging/simulate-and-persist-response.mjs +0 -89
- package/debugging/simulate-and-persist-response.mjs.map +0 -1
- package/dispenser-client.d.ts.map +0 -1
- package/index.d.ts.map +0 -1
- package/indexer-lookup.d.ts.map +0 -1
- package/localnet/get-kmd-wallet-account.d.ts.map +0 -1
- package/localnet/get-localnet-dispenser-account.d.ts.map +0 -1
- package/localnet/get-or-create-kmd-wallet-account.d.ts.map +0 -1
- package/localnet/index.d.ts.map +0 -1
- package/localnet/is-localnet.d.ts.map +0 -1
- package/network-client.d.ts.map +0 -1
- package/testing/_asset.d.ts.map +0 -1
- package/testing/account.d.ts.map +0 -1
- package/testing/fixtures/algokit-log-capture-fixture.d.ts.map +0 -1
- package/testing/fixtures/algorand-fixture.d.ts.map +0 -1
- package/testing/fixtures/index.d.ts.map +0 -1
- package/testing/index.d.ts.map +0 -1
- package/testing/indexer.d.ts.map +0 -1
- package/testing/test-logger.d.ts.map +0 -1
- package/testing/transaction-logger.d.ts.map +0 -1
- package/transaction/index.d.ts.map +0 -1
- package/transaction/legacy-bridge.d.ts.map +0 -1
- package/transaction/perform-atomic-transaction-composer-simulate.d.ts.map +0 -1
- package/transaction/transaction.d.ts.map +0 -1
- package/transfer/index.d.ts.map +0 -1
- package/transfer/transfer-algos.d.ts.map +0 -1
- package/transfer/transfer.d.ts.map +0 -1
- package/types/account-manager.d.ts.map +0 -1
- package/types/account.d.ts.map +0 -1
- package/types/algo-http-client-with-retry.d.ts.map +0 -1
- package/types/algorand-client-transaction-creator.d.ts.map +0 -1
- package/types/algorand-client-transaction-sender.d.ts.map +0 -1
- package/types/algorand-client.d.ts.map +0 -1
- package/types/amount.d.ts.map +0 -1
- package/types/app-client.d.ts.map +0 -1
- package/types/app-deployer.d.ts.map +0 -1
- package/types/app-manager.d.ts.map +0 -1
- package/types/app-spec.d.ts.map +0 -1
- package/types/app.d.ts.map +0 -1
- package/types/asset-manager.d.ts.map +0 -1
- package/types/asset.d.ts.map +0 -1
- package/types/client-manager.d.ts.map +0 -1
- package/types/composer.d.ts.map +0 -1
- package/types/config.d.ts.map +0 -1
- package/types/debugging.d.ts.map +0 -1
- package/types/dispenser-client.d.ts.map +0 -1
- package/types/expand.d.ts.map +0 -1
- package/types/indexer.d.ts.map +0 -1
- package/types/kmd-account-manager.d.ts.map +0 -1
- package/types/logging.d.ts.map +0 -1
- package/types/logic-error.d.ts.map +0 -1
- package/types/network-client.d.ts.map +0 -1
- package/types/testing.d.ts.map +0 -1
- package/types/transaction.d.ts.map +0 -1
- package/types/transfer.d.ts.map +0 -1
- package/types/urlTokenBaseHTTPClient.d.ts.map +0 -1
- package/util.d.ts.map +0 -1
package/types/app-client.mjs
CHANGED
|
@@ -3,17 +3,20 @@ import { Buffer } from 'buffer';
|
|
|
3
3
|
import { compileTeal, createApp, updateApp, callApp, getAppGlobalState, getAppLocalState, getAppBoxNames, getAppBoxValue, getAppBoxValueFromABIType } from '../app.mjs';
|
|
4
4
|
import { replaceDeployTimeControlParams, performTemplateSubstitution, deployApp, getCreatorAppsByName } from '../app-deploy.mjs';
|
|
5
5
|
import { Config } from '../config.mjs';
|
|
6
|
-
import { persistSourceMaps } from '../debugging/debugging.mjs';
|
|
7
6
|
import { legacySendTransactionBridge } from '../transaction/legacy-bridge.mjs';
|
|
8
7
|
import { getSenderAddress, encodeTransactionNote } from '../transaction/transaction.mjs';
|
|
8
|
+
import { binaryStartsWith } from '../util.mjs';
|
|
9
9
|
import { UPDATABLE_TEMPLATE_NAME, DELETABLE_TEMPLATE_NAME } from './app.mjs';
|
|
10
|
-
import {
|
|
10
|
+
import { getArc56Method, getArc56ReturnValue, getABITupleFromABIStruct, getABIDecodedValue, getABIEncodedValue } from './app-arc56.mjs';
|
|
11
|
+
import { arc32ToArc56 } from './app-spec.mjs';
|
|
12
|
+
import { EventType } from './lifecycle-events.mjs';
|
|
11
13
|
import { LogicError } from './logic-error.mjs';
|
|
12
14
|
|
|
13
15
|
var ABIMethod = algosdk.ABIMethod;
|
|
14
16
|
var AtomicTransactionComposer = algosdk.AtomicTransactionComposer;
|
|
15
17
|
var getApplicationAddress = algosdk.getApplicationAddress;
|
|
16
18
|
var Indexer = algosdk.Indexer;
|
|
19
|
+
var OnApplicationComplete = algosdk.OnApplicationComplete;
|
|
17
20
|
var SourceMap = algosdk.SourceMap;
|
|
18
21
|
/**
|
|
19
22
|
* Determines deploy time control (UPDATABLE, DELETABLE) value by inspecting application specification
|
|
@@ -37,13 +40,976 @@ function getDeployTimeControl(approval, appSpec, templateVariableName, callConfi
|
|
|
37
40
|
return !!abiCallConfig && abiCallConfig !== 'NEVER';
|
|
38
41
|
});
|
|
39
42
|
}
|
|
40
|
-
|
|
43
|
+
const BYTE_CBLOCK = 38;
|
|
44
|
+
const INT_CBLOCK = 32;
|
|
45
|
+
/**
|
|
46
|
+
* Get the offset of the last constant block at the beginning of the program
|
|
47
|
+
* This value is used to calculate the program counter for an ARC56 program that has a pcOffsetMethod of "cblocks"
|
|
48
|
+
*
|
|
49
|
+
* @param program The program to parse
|
|
50
|
+
* @returns The PC value of the opcode after the last constant block
|
|
51
|
+
*/
|
|
52
|
+
function getConstantBlockOffset(program) {
|
|
53
|
+
const bytes = [...program];
|
|
54
|
+
const programSize = bytes.length;
|
|
55
|
+
bytes.shift(); // remove version
|
|
56
|
+
/** The PC of the opcode after the bytecblock */
|
|
57
|
+
let bytecblockOffset;
|
|
58
|
+
/** The PC of the opcode after the intcblock */
|
|
59
|
+
let intcblockOffset;
|
|
60
|
+
while (bytes.length > 0) {
|
|
61
|
+
/** The current byte from the beginning of the byte array */
|
|
62
|
+
const byte = bytes.shift();
|
|
63
|
+
// If the byte is a constant block...
|
|
64
|
+
if (byte === BYTE_CBLOCK || byte === INT_CBLOCK) {
|
|
65
|
+
const isBytecblock = byte === BYTE_CBLOCK;
|
|
66
|
+
/** The byte following the opcode is the number of values in the constant block */
|
|
67
|
+
const valuesRemaining = bytes.shift();
|
|
68
|
+
// Iterate over all the values in the constant block
|
|
69
|
+
for (let i = 0; i < valuesRemaining; i++) {
|
|
70
|
+
if (isBytecblock) {
|
|
71
|
+
/** The byte following the opcode is the length of the next element */
|
|
72
|
+
const length = bytes.shift();
|
|
73
|
+
bytes.splice(0, length);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
// intcblock is a uvarint, so we need to keep reading until we find the end (MSB is not set)
|
|
77
|
+
while ((bytes.shift() & 0x80) !== 0) {
|
|
78
|
+
// Do nothing...
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (isBytecblock)
|
|
83
|
+
bytecblockOffset = programSize - bytes.length - 1;
|
|
84
|
+
else
|
|
85
|
+
intcblockOffset = programSize - bytes.length - 1;
|
|
86
|
+
if (bytes[0] !== BYTE_CBLOCK && bytes[0] !== INT_CBLOCK) {
|
|
87
|
+
// if the next opcode isn't a constant block, we're done
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return Math.max(bytecblockOffset ?? 0, intcblockOffset ?? 0);
|
|
93
|
+
}
|
|
94
|
+
/** ARC-56/ARC-32 application client that allows you to manage calls and
|
|
95
|
+
* state for a specific deployed instance of an app (with a known app ID). */
|
|
96
|
+
class AppClient {
|
|
97
|
+
constructor(params) {
|
|
98
|
+
this._appId = params.appId;
|
|
99
|
+
this._appAddress = algosdk.getApplicationAddress(this._appId);
|
|
100
|
+
this._appSpec = AppClient.normaliseAppSpec(params.appSpec);
|
|
101
|
+
this._appName = params.appName ?? this._appSpec.name;
|
|
102
|
+
this._algorand = params.algorand;
|
|
103
|
+
this._defaultSender = params.defaultSender;
|
|
104
|
+
this._defaultSigner = params.defaultSigner;
|
|
105
|
+
this._approvalSourceMap = params.approvalSourceMap;
|
|
106
|
+
this._clearSourceMap = params.clearSourceMap;
|
|
107
|
+
this._localStateMethods = (address) => this.getStateMethods(() => this.getLocalState(address), () => this._appSpec.state.keys.local, () => this._appSpec.state.maps.local);
|
|
108
|
+
this._globalStateMethods = this.getStateMethods(() => this.getGlobalState(), () => this._appSpec.state.keys.global, () => this._appSpec.state.maps.global);
|
|
109
|
+
this._boxStateMethods = this.getBoxMethods();
|
|
110
|
+
this._paramsMethods = {
|
|
111
|
+
...this.getMethodCallParamsMethods(),
|
|
112
|
+
/** Get parameters to define bare (raw) transactions to the current app */
|
|
113
|
+
bare: this.getBareParamsMethods(),
|
|
114
|
+
};
|
|
115
|
+
this._createTransactionsMethods = {
|
|
116
|
+
...this.getMethodCallCreateTransactionMethods(),
|
|
117
|
+
/** Get transactions for bare (raw) calls to the current app */
|
|
118
|
+
bare: this.getBareCreateTransactionMethods(),
|
|
119
|
+
};
|
|
120
|
+
this._sendMethods = {
|
|
121
|
+
...this.getMethodCallSendMethods(),
|
|
122
|
+
/** Send bare (raw) transactions to the current app */
|
|
123
|
+
bare: this.getBareSendMethods(),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Clone this app client with different params
|
|
128
|
+
*
|
|
129
|
+
* @param params The params to use for the the cloned app client. Omit a param to keep the original value. Set a param to override the original value. Setting to undefined will clear the original value.
|
|
130
|
+
* @returns A new app client with the altered params
|
|
131
|
+
*/
|
|
132
|
+
clone(params) {
|
|
133
|
+
return new AppClient({
|
|
134
|
+
appId: this._appId,
|
|
135
|
+
appSpec: this._appSpec,
|
|
136
|
+
algorand: this._algorand,
|
|
137
|
+
appName: this._appName,
|
|
138
|
+
defaultSender: this._defaultSender,
|
|
139
|
+
defaultSigner: this._defaultSigner,
|
|
140
|
+
approvalSourceMap: this._approvalSourceMap,
|
|
141
|
+
clearSourceMap: this._clearSourceMap,
|
|
142
|
+
...params,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/** Start a new `TransactionComposer` transaction group */
|
|
146
|
+
newGroup() {
|
|
147
|
+
return this._algorand.newGroup();
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Returns a new `AppClient` client, resolving the app by creator address and name
|
|
151
|
+
* using AlgoKit app deployment semantics (i.e. looking for the app creation transaction note).
|
|
152
|
+
* @param params The parameters to create the app client
|
|
153
|
+
*/
|
|
154
|
+
static async fromCreatorAndName(params) {
|
|
155
|
+
const appSpec = AppClient.normaliseAppSpec(params.appSpec);
|
|
156
|
+
const appLookup = params.appLookupCache ?? (await params.algorand.appDeployer.getCreatorAppsByName(params.creatorAddress, params.ignoreCache));
|
|
157
|
+
const appMetadata = appLookup.apps[params.appName ?? appSpec.name];
|
|
158
|
+
if (!appMetadata) {
|
|
159
|
+
throw new Error(`App not found for creator ${params.creatorAddress} and name ${params.appName ?? appSpec.name}`);
|
|
160
|
+
}
|
|
161
|
+
return new AppClient({
|
|
162
|
+
...params,
|
|
163
|
+
algorand: params.algorand,
|
|
164
|
+
appId: appMetadata.appId,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Returns an `AppClient` instance for the current network based on
|
|
169
|
+
* pre-determined network-specific app IDs specified in the ARC-56 app spec.
|
|
170
|
+
*
|
|
171
|
+
* If no IDs are in the app spec or the network isn't recognised, an error is thrown.
|
|
172
|
+
* @param params The parameters to create the app client
|
|
173
|
+
*/
|
|
174
|
+
static async fromNetwork(params) {
|
|
175
|
+
const network = await params.algorand.client.network();
|
|
176
|
+
const appSpec = AppClient.normaliseAppSpec(params.appSpec);
|
|
177
|
+
const networkNames = [network.genesisHash];
|
|
178
|
+
if (network.isLocalNet)
|
|
179
|
+
networkNames.push('localnet');
|
|
180
|
+
if (network.isTestNet)
|
|
181
|
+
networkNames.push('testnet');
|
|
182
|
+
if (network.isMainNet)
|
|
183
|
+
networkNames.push('mainnet');
|
|
184
|
+
const availableAppSpecNetworks = Object.keys(appSpec.networks ?? {});
|
|
185
|
+
const networkIndex = availableAppSpecNetworks.findIndex((n) => networkNames.includes(n));
|
|
186
|
+
if (networkIndex === -1) {
|
|
187
|
+
throw new Error(`No app ID found for network ${JSON.stringify(networkNames)} in the app spec`);
|
|
188
|
+
}
|
|
189
|
+
const appId = BigInt(appSpec.networks[networkIndex].appID);
|
|
190
|
+
return new AppClient({ ...params, appId, appSpec });
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Takes a string or parsed JSON object that could be ARC-32 or ARC-56 format and
|
|
194
|
+
* normalises it into a parsed ARC-56 contract object.
|
|
195
|
+
* @param spec The spec to normalise
|
|
196
|
+
* @returns The normalised ARC-56 contract object
|
|
197
|
+
*/
|
|
198
|
+
static normaliseAppSpec(spec) {
|
|
199
|
+
const parsedSpec = typeof spec === 'string' ? JSON.parse(spec) : spec;
|
|
200
|
+
const appSpec = 'hints' in parsedSpec ? arc32ToArc56(parsedSpec) : parsedSpec;
|
|
201
|
+
return appSpec;
|
|
202
|
+
}
|
|
203
|
+
/** The ID of the app instance this client is linked to. */
|
|
204
|
+
get appId() {
|
|
205
|
+
return this._appId;
|
|
206
|
+
}
|
|
207
|
+
/** The app address of the app instance this client is linked to. */
|
|
208
|
+
get appAddress() {
|
|
209
|
+
return this._appAddress;
|
|
210
|
+
}
|
|
211
|
+
/** The name of the app (from the ARC-32 / ARC-56 app spec). */
|
|
212
|
+
get appName() {
|
|
213
|
+
return this._appName;
|
|
214
|
+
}
|
|
215
|
+
/** The ARC-56 app spec being used */
|
|
216
|
+
get appSpec() {
|
|
217
|
+
return this._appSpec;
|
|
218
|
+
}
|
|
219
|
+
/** A reference to the underlying `AlgorandClient` this app client is using. */
|
|
220
|
+
get algorand() {
|
|
221
|
+
return this._algorand;
|
|
222
|
+
}
|
|
223
|
+
/** Get parameters to create transactions for the current app.
|
|
224
|
+
*
|
|
225
|
+
* A good mental model for this is that these parameters represent a deferred transaction creation.
|
|
226
|
+
* @example Create a transaction in the future using Algorand Client
|
|
227
|
+
* ```typescript
|
|
228
|
+
* const myMethodCall = appClient.params.call({method: 'my_method', args: [123, 'hello']})
|
|
229
|
+
* // ...
|
|
230
|
+
* await algorand.send.AppMethodCall(myMethodCall)
|
|
231
|
+
* ```
|
|
232
|
+
* @example Define a nested transaction as an ABI argument
|
|
233
|
+
* ```typescript
|
|
234
|
+
* const myMethodCall = appClient.params.call({method: 'my_method', args: [123, 'hello']})
|
|
235
|
+
* await appClient.send.call({method: 'my_method2', args: [myMethodCall]})
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
get params() {
|
|
239
|
+
return this._paramsMethods;
|
|
240
|
+
}
|
|
241
|
+
/** Create transactions for the current app */
|
|
242
|
+
get createTransaction() {
|
|
243
|
+
return this._createTransactionsMethods;
|
|
244
|
+
}
|
|
245
|
+
/** Send transactions to the current app */
|
|
246
|
+
get send() {
|
|
247
|
+
return this._sendMethods;
|
|
248
|
+
}
|
|
249
|
+
/** Get state (local, global, box) from the current app */
|
|
250
|
+
get state() {
|
|
251
|
+
return {
|
|
252
|
+
/**
|
|
253
|
+
* Methods to access local state for the current app
|
|
254
|
+
* @param address The address of the account to get the local state for
|
|
255
|
+
*/
|
|
256
|
+
local: this._localStateMethods,
|
|
257
|
+
/**
|
|
258
|
+
* Methods to access global state for the current app
|
|
259
|
+
*/
|
|
260
|
+
global: this._globalStateMethods,
|
|
261
|
+
/**
|
|
262
|
+
* Methods to access box storage for the current app
|
|
263
|
+
*/
|
|
264
|
+
box: this._boxStateMethods,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Funds Algo into the app account for this app.
|
|
269
|
+
*
|
|
270
|
+
* An alias for `appClient.send.fundAppAccount(params)`.
|
|
271
|
+
* @param params The parameters for the funding transaction
|
|
272
|
+
* @returns The result of the funding
|
|
273
|
+
*/
|
|
274
|
+
async fundAppAccount(params) {
|
|
275
|
+
return this.send.fundAppAccount(params);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Returns raw global state for the current app.
|
|
279
|
+
* @returns The global state
|
|
280
|
+
*/
|
|
281
|
+
async getGlobalState() {
|
|
282
|
+
return await this._algorand.app.getGlobalState(this.appId);
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Returns raw local state for the given account address.
|
|
286
|
+
* @param address The address of the account to get the local state for
|
|
287
|
+
* @returns The local state
|
|
288
|
+
*/
|
|
289
|
+
async getLocalState(address) {
|
|
290
|
+
return await this._algorand.app.getLocalState(this.appId, address);
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Returns the names of all current boxes for the current app.
|
|
294
|
+
* @returns The names of the boxes
|
|
295
|
+
*/
|
|
296
|
+
async getBoxNames() {
|
|
297
|
+
return await this._algorand.app.getBoxNames(this.appId);
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Returns the value of the given box for the current app.
|
|
301
|
+
* @param name The identifier of the box to return
|
|
302
|
+
* @returns The current box value as a byte array
|
|
303
|
+
*/
|
|
304
|
+
async getBoxValue(name) {
|
|
305
|
+
return await this._algorand.app.getBoxValue(this.appId, name);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Returns the value of the given box for the current app.
|
|
309
|
+
* @param name The identifier of the box to return
|
|
310
|
+
* @param type
|
|
311
|
+
* @returns The current box value as a byte array
|
|
312
|
+
*/
|
|
313
|
+
async getBoxValueFromABIType(name, type) {
|
|
314
|
+
return await this._algorand.app.getBoxValueFromABIType({
|
|
315
|
+
appId: this.appId,
|
|
316
|
+
boxName: name,
|
|
317
|
+
type,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Returns the values of all current boxes for the current app.
|
|
322
|
+
* Note: This will issue multiple HTTP requests (one per box) and it's not an atomic operation so values may be out of sync.
|
|
323
|
+
* @param filter Optional filter to filter which boxes' values are returned
|
|
324
|
+
* @returns The (name, value) pair of the boxes with values as raw byte arrays
|
|
325
|
+
*/
|
|
326
|
+
async getBoxValues(filter) {
|
|
327
|
+
const names = (await this.getBoxNames()).filter(filter ?? ((_) => true));
|
|
328
|
+
const values = await this._algorand.app.getBoxValues(this.appId, names.map((name) => name.nameRaw));
|
|
329
|
+
return names.map((name, i) => ({ name, value: values[i] }));
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Returns the values of all current boxes for the current app decoded using an ABI Type.
|
|
333
|
+
* Note: This will issue multiple HTTP requests (one per box) and it's not an atomic operation so values may be out of sync.
|
|
334
|
+
* @param type The ABI type to decode the values with
|
|
335
|
+
* @param filter Optional filter to filter which boxes' values are returned
|
|
336
|
+
* @returns The (name, value) pair of the boxes with values as the ABI Value
|
|
337
|
+
*/
|
|
338
|
+
async getBoxValuesFromABIType(type, filter) {
|
|
339
|
+
const names = (await this.getBoxNames()).filter(filter ?? ((_) => true));
|
|
340
|
+
const values = await this._algorand.app.getBoxValuesFromABIType({
|
|
341
|
+
appId: this.appId,
|
|
342
|
+
boxNames: names.map((name) => name.nameRaw),
|
|
343
|
+
type,
|
|
344
|
+
});
|
|
345
|
+
return names.map((name, i) => ({ name, value: values[i] }));
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Takes an error that may include a logic error from a call to the current app and re-exposes the
|
|
349
|
+
* error to include source code information via the source map and ARC-56 spec.
|
|
350
|
+
* @param e The error to parse
|
|
351
|
+
* @param isClearStateProgram Whether or not the code was running the clear state program (defaults to approval program)
|
|
352
|
+
* @returns The new error, or if there was no logic error or source map then the wrapped error with source details
|
|
353
|
+
*/
|
|
354
|
+
async exposeLogicError(e, isClearStateProgram) {
|
|
355
|
+
const pcOffsetMethod = this._appSpec.sourceInfo?.[isClearStateProgram ? 'clear' : 'approval']?.pcOffsetMethod;
|
|
356
|
+
let program;
|
|
357
|
+
if (pcOffsetMethod === 'cblocks') {
|
|
358
|
+
// TODO: Cache this if we deploy the app and it's not updateable
|
|
359
|
+
const appInfo = await this._algorand.app.getById(this.appId);
|
|
360
|
+
program = isClearStateProgram ? appInfo.clearStateProgram : appInfo.approvalProgram;
|
|
361
|
+
}
|
|
362
|
+
return AppClient.exposeLogicError(e, this._appSpec, {
|
|
363
|
+
isClearStateProgram,
|
|
364
|
+
approvalSourceMap: this._approvalSourceMap,
|
|
365
|
+
clearSourceMap: this._clearSourceMap,
|
|
366
|
+
program,
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Export the current source maps for the app.
|
|
371
|
+
* @returns The source maps
|
|
372
|
+
*/
|
|
373
|
+
exportSourceMaps() {
|
|
374
|
+
if (!this._approvalSourceMap || !this._clearSourceMap) {
|
|
375
|
+
throw new Error("Unable to export source maps; they haven't been loaded into this client - you need to call create, update, or deploy first");
|
|
376
|
+
}
|
|
377
|
+
return {
|
|
378
|
+
approvalSourceMap: this._approvalSourceMap,
|
|
379
|
+
clearSourceMap: this._clearSourceMap,
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Import source maps for the app.
|
|
384
|
+
* @param sourceMaps The source maps to import
|
|
385
|
+
*/
|
|
386
|
+
importSourceMaps(sourceMaps) {
|
|
387
|
+
this._approvalSourceMap = new SourceMap(sourceMaps.approvalSourceMap);
|
|
388
|
+
this._clearSourceMap = new SourceMap(sourceMaps.clearSourceMap);
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Returns the ABI Method spec for the given method string for the app represented by this application client instance
|
|
392
|
+
* @param methodNameOrSignature The method name or method signature to call if an ABI call is being emitted.
|
|
393
|
+
* e.g. `my_method` or `my_method(unit64,string)bytes`
|
|
394
|
+
* @returns A tuple with: [ARC-56 `Method`, algosdk `ABIMethod`]
|
|
395
|
+
*/
|
|
396
|
+
getABIMethod(methodNameOrSignature) {
|
|
397
|
+
return getArc56Method(methodNameOrSignature, this._appSpec);
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Checks for decode errors on the SendAppTransactionResult and maps the return value to the specified type
|
|
401
|
+
* on the ARC-56 method, replacing the `return` property with the decoded type.
|
|
402
|
+
*
|
|
403
|
+
* If the return type is an ARC-56 struct then the struct will be returned.
|
|
404
|
+
*
|
|
405
|
+
* @param result The SendAppTransactionResult to be mapped
|
|
406
|
+
* @param method The method that was called
|
|
407
|
+
* @returns The smart contract response with an updated return value
|
|
408
|
+
*/
|
|
409
|
+
async processMethodCallReturn(result, method) {
|
|
410
|
+
const resultValue = await result;
|
|
411
|
+
return { ...resultValue, return: getArc56ReturnValue(resultValue.return, method, this._appSpec.structs) };
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Compiles the approval and clear state programs (if TEAL templates provided),
|
|
415
|
+
* performing any provided deploy-time parameter replacement and stores
|
|
416
|
+
* the source maps.
|
|
417
|
+
*
|
|
418
|
+
* If no TEAL templates provided it will use any byte code provided in the app spec.
|
|
419
|
+
*
|
|
420
|
+
* Will store any generated source maps for later use in debugging.
|
|
421
|
+
*/
|
|
422
|
+
async compile(compilation) {
|
|
423
|
+
const result = await AppClient.compile(this._appSpec, this._algorand.app, compilation);
|
|
424
|
+
if (result.compiledApproval) {
|
|
425
|
+
this._approvalSourceMap = result.compiledApproval.sourceMap;
|
|
426
|
+
}
|
|
427
|
+
if (result.compiledClear) {
|
|
428
|
+
this._clearSourceMap = result.compiledClear.sourceMap;
|
|
429
|
+
}
|
|
430
|
+
return result;
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Takes an error that may include a logic error from a call to the current app and re-exposes the
|
|
434
|
+
* error to include source code information via the source map and ARC-56 spec.
|
|
435
|
+
* @param e The error to parse
|
|
436
|
+
* @param appSpec The app spec for the app
|
|
437
|
+
* @param details Additional information to inform the error
|
|
438
|
+
* @returns The new error, or if there was no logic error or source map then the wrapped error with source details
|
|
439
|
+
*/
|
|
440
|
+
static exposeLogicError(e, appSpec, details) {
|
|
441
|
+
const { isClearStateProgram, approvalSourceMap, clearSourceMap, program } = details;
|
|
442
|
+
const sourceMap = isClearStateProgram ? clearSourceMap : approvalSourceMap;
|
|
443
|
+
const errorDetails = LogicError.parseLogicError(e);
|
|
444
|
+
// Return the error if we don't have a PC
|
|
445
|
+
if (errorDetails === undefined || errorDetails?.pc === undefined)
|
|
446
|
+
return e;
|
|
447
|
+
/** The PC value to find in the ARC56 SourceInfo */
|
|
448
|
+
let arc56Pc = errorDetails?.pc;
|
|
449
|
+
const programSourceInfo = isClearStateProgram ? appSpec.sourceInfo?.clear : appSpec.sourceInfo?.approval;
|
|
450
|
+
/** The offset to apply to the PC if using the cblocks pc offset method */
|
|
451
|
+
let cblocksOffset = 0;
|
|
452
|
+
// If the program uses cblocks offset, then we need to adjust the PC accordingly
|
|
453
|
+
if (programSourceInfo?.pcOffsetMethod === 'cblocks') {
|
|
454
|
+
if (program === undefined)
|
|
455
|
+
throw new Error('Program bytes are required to calculate the ARC56 cblocks PC offset');
|
|
456
|
+
cblocksOffset = getConstantBlockOffset(program);
|
|
457
|
+
arc56Pc = errorDetails.pc - cblocksOffset;
|
|
458
|
+
}
|
|
459
|
+
// Find the source info for this PC and get the error message
|
|
460
|
+
const sourceInfo = programSourceInfo?.sourceInfo.find((s) => s.pc.includes(arc56Pc));
|
|
461
|
+
const errorMessage = sourceInfo?.errorMessage;
|
|
462
|
+
// If we have the source we can display the TEAL in the error message
|
|
463
|
+
if (appSpec.source) {
|
|
464
|
+
let getLineForPc = (inputPc) => sourceMap?.getLineForPc?.(inputPc);
|
|
465
|
+
// If the SourceMap is not defined, we need to provide our own function for going from a PC to TEAL based on ARC56 SourceInfo[]
|
|
466
|
+
if (sourceMap === undefined) {
|
|
467
|
+
getLineForPc = (inputPc) => {
|
|
468
|
+
const teal = programSourceInfo?.sourceInfo.find((s) => s.pc.includes(inputPc - cblocksOffset))?.teal;
|
|
469
|
+
if (teal === undefined)
|
|
470
|
+
return undefined;
|
|
471
|
+
return teal - 1;
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
e = new LogicError(errorDetails, Buffer.from(isClearStateProgram ? appSpec.source.clear : appSpec.source.approval, 'base64')
|
|
475
|
+
.toString()
|
|
476
|
+
.split('\n'),
|
|
477
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
478
|
+
getLineForPc);
|
|
479
|
+
}
|
|
480
|
+
if (errorMessage) {
|
|
481
|
+
const appId = JSON.stringify(e).match(/(?<=app=)\d+/)?.[0] || '';
|
|
482
|
+
const txId = JSON.stringify(e).match(/(?<=transaction )\S+(?=:)/)?.[0];
|
|
483
|
+
const error = new Error(`Runtime error when executing ${appSpec.name} (appId: ${appId}) in transaction ${txId}: ${errorMessage}`);
|
|
484
|
+
error.cause = e;
|
|
485
|
+
return error;
|
|
486
|
+
}
|
|
487
|
+
return e;
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Compiles the approval and clear state programs (if TEAL templates provided),
|
|
491
|
+
* performing any provided deploy-time parameter replacement and returns
|
|
492
|
+
* the compiled code and any compilation results (including source maps).
|
|
493
|
+
*
|
|
494
|
+
* If no TEAL templates provided it will use any byte code provided in the app spec.
|
|
495
|
+
*
|
|
496
|
+
* Will store any generated source maps for later use in debugging.
|
|
497
|
+
* @param appSpec The app spec for the app
|
|
498
|
+
* @param compilation Any compilation parameters to use
|
|
499
|
+
*/
|
|
500
|
+
static async compile(appSpec, appManager, compilation) {
|
|
501
|
+
const { deployTimeParams, updatable, deletable } = compilation ?? {};
|
|
502
|
+
if (!appSpec.source) {
|
|
503
|
+
if (!appSpec.byteCode?.approval || !appSpec.byteCode?.clear) {
|
|
504
|
+
throw new Error(`Attempt to compile app ${appSpec.name} without source or byteCode`);
|
|
505
|
+
}
|
|
506
|
+
return {
|
|
507
|
+
approvalProgram: Buffer.from(appSpec.byteCode.approval, 'base64'),
|
|
508
|
+
clearStateProgram: Buffer.from(appSpec.byteCode.clear, 'base64'),
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
const approvalTemplate = Buffer.from(appSpec.source.approval, 'base64').toString('utf-8');
|
|
512
|
+
const compiledApproval = await appManager.compileTealTemplate(approvalTemplate, deployTimeParams, {
|
|
513
|
+
updatable,
|
|
514
|
+
deletable,
|
|
515
|
+
});
|
|
516
|
+
const clearTemplate = Buffer.from(appSpec.source.clear, 'base64').toString('utf-8');
|
|
517
|
+
const compiledClear = await appManager.compileTealTemplate(clearTemplate, deployTimeParams);
|
|
518
|
+
if (Config.debug) {
|
|
519
|
+
await Config.events.emitAsync(EventType.AppCompiled, {
|
|
520
|
+
sources: [
|
|
521
|
+
{ compiledTeal: compiledApproval, appName: appSpec.name, fileName: 'approval' },
|
|
522
|
+
{ compiledTeal: compiledClear, appName: appSpec.name, fileName: 'clear' },
|
|
523
|
+
],
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
return {
|
|
527
|
+
approvalProgram: compiledApproval.compiledBase64ToBytes,
|
|
528
|
+
compiledApproval,
|
|
529
|
+
clearStateProgram: compiledClear.compiledBase64ToBytes,
|
|
530
|
+
compiledClear,
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Returns ABI method arguments ready for a method call params object with default values populated
|
|
535
|
+
* and structs replaced with tuples.
|
|
536
|
+
*
|
|
537
|
+
* It does this by replacing any `undefined` values with the equivalent default value from the given ARC-56 app spec.
|
|
538
|
+
* @param methodNameOrSignature The method name or method signature to call if an ABI call is being emitted.
|
|
539
|
+
* e.g. `my_method` or `my_method(unit64,string)bytes`
|
|
540
|
+
* @param args The arguments to the method with `undefined` for any that should be populated with a default value
|
|
541
|
+
*/
|
|
542
|
+
async getABIArgsWithDefaultValues(methodNameOrSignature, args, sender) {
|
|
543
|
+
const m = getArc56Method(methodNameOrSignature, this._appSpec);
|
|
544
|
+
return await Promise.all(args?.map(async (a, i) => {
|
|
545
|
+
const arg = m.args[i];
|
|
546
|
+
if (a !== undefined) {
|
|
547
|
+
// If a struct then convert to tuple for the underlying call
|
|
548
|
+
return arg.struct && typeof a === 'object' && !Array.isArray(a)
|
|
549
|
+
? getABITupleFromABIStruct(a, this._appSpec.structs[arg.struct], this._appSpec.structs)
|
|
550
|
+
: a;
|
|
551
|
+
}
|
|
552
|
+
const defaultValue = arg.defaultValue;
|
|
553
|
+
if (defaultValue) {
|
|
554
|
+
switch (defaultValue.source) {
|
|
555
|
+
case 'literal':
|
|
556
|
+
return getABIDecodedValue(Buffer.from(defaultValue.data, 'base64'), m.method.args[i].defaultValue?.type ?? m.method.args[i].type, this._appSpec.structs);
|
|
557
|
+
case 'method': {
|
|
558
|
+
const method = this.getABIMethod(defaultValue.data);
|
|
559
|
+
const result = await this.send.call({
|
|
560
|
+
method: defaultValue.data,
|
|
561
|
+
args: method.args.map(() => undefined),
|
|
562
|
+
sender,
|
|
563
|
+
});
|
|
564
|
+
if (result.return === undefined) {
|
|
565
|
+
throw new Error('Default value method call did not return a value');
|
|
566
|
+
}
|
|
567
|
+
if (typeof result.return === 'object' && !(result.return instanceof Uint8Array) && !Array.isArray(result.return)) {
|
|
568
|
+
return getABITupleFromABIStruct(result.return, this._appSpec.structs[method.returns.struct], this._appSpec.structs);
|
|
569
|
+
}
|
|
570
|
+
return result.return;
|
|
571
|
+
}
|
|
572
|
+
case 'local':
|
|
573
|
+
case 'global': {
|
|
574
|
+
const state = defaultValue.source === 'global' ? await this.getGlobalState() : await this.getLocalState(sender);
|
|
575
|
+
const value = Object.values(state).find((s) => s.keyBase64 === defaultValue.data);
|
|
576
|
+
if (!value) {
|
|
577
|
+
throw new Error(`Preparing default value for argument ${arg.name ?? `arg${i + 1}`} resulted in the failure: The key '${defaultValue.data}' could not be found in ${defaultValue.source} storage`);
|
|
578
|
+
}
|
|
579
|
+
return 'valueRaw' in value
|
|
580
|
+
? getABIDecodedValue(value.valueRaw, m.method.args[i].defaultValue?.type ?? m.method.args[i].type, this._appSpec.structs)
|
|
581
|
+
: value.value;
|
|
582
|
+
}
|
|
583
|
+
case 'box': {
|
|
584
|
+
const value = await this.getBoxValue(Buffer.from(defaultValue.data, 'base64'));
|
|
585
|
+
return getABIDecodedValue(value, m.method.args[i].defaultValue?.type ?? m.method.args[i].type, this._appSpec.structs);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
if (!algosdk.abiTypeIsTransaction(arg.type)) {
|
|
590
|
+
throw new Error(`No value provided for required argument ${arg.name ?? `arg${i + 1}`} in call to method ${m.name}`);
|
|
591
|
+
}
|
|
592
|
+
}) ?? []);
|
|
593
|
+
}
|
|
594
|
+
getBareParamsMethods() {
|
|
595
|
+
return {
|
|
596
|
+
/** Return params for an update call, including deploy-time TEAL template replacements and compilation if provided */
|
|
597
|
+
update: async (params) => {
|
|
598
|
+
return this.getBareParams({
|
|
599
|
+
...params,
|
|
600
|
+
...(await this.compile(params)),
|
|
601
|
+
}, OnApplicationComplete.UpdateApplicationOC);
|
|
602
|
+
},
|
|
603
|
+
/** Return params for an opt-in call */
|
|
604
|
+
optIn: (params) => {
|
|
605
|
+
return this.getBareParams(params, OnApplicationComplete.OptInOC);
|
|
606
|
+
},
|
|
607
|
+
/** Return params for a delete call */
|
|
608
|
+
delete: (params) => {
|
|
609
|
+
return this.getBareParams(params, OnApplicationComplete.DeleteApplicationOC);
|
|
610
|
+
},
|
|
611
|
+
/** Return params for a clear state call */
|
|
612
|
+
clearState: (params) => {
|
|
613
|
+
return this.getBareParams(params, OnApplicationComplete.ClearStateOC);
|
|
614
|
+
},
|
|
615
|
+
/** Return params for a close out call */
|
|
616
|
+
closeOut: (params) => {
|
|
617
|
+
return this.getBareParams(params, OnApplicationComplete.CloseOutOC);
|
|
618
|
+
},
|
|
619
|
+
/** Return params for a call (defaults to no-op) */
|
|
620
|
+
call: (params) => {
|
|
621
|
+
return this.getBareParams(params, params?.onComplete ?? OnApplicationComplete.NoOpOC);
|
|
622
|
+
},
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
getBareCreateTransactionMethods() {
|
|
626
|
+
return {
|
|
627
|
+
/** Returns a transaction for an update call, including deploy-time TEAL template replacements and compilation if provided */
|
|
628
|
+
update: async (params) => {
|
|
629
|
+
return this._algorand.createTransaction.appUpdate(await this.params.bare.update(params));
|
|
630
|
+
},
|
|
631
|
+
/** Returns a transaction for an opt-in call */
|
|
632
|
+
optIn: (params) => {
|
|
633
|
+
return this._algorand.createTransaction.appCall(this.params.bare.optIn(params));
|
|
634
|
+
},
|
|
635
|
+
/** Returns a transaction for a delete call */
|
|
636
|
+
delete: (params) => {
|
|
637
|
+
return this._algorand.createTransaction.appDelete(this.params.bare.delete(params));
|
|
638
|
+
},
|
|
639
|
+
/** Returns a transaction for a clear state call */
|
|
640
|
+
clearState: (params) => {
|
|
641
|
+
return this._algorand.createTransaction.appCall(this.params.bare.clearState(params));
|
|
642
|
+
},
|
|
643
|
+
/** Returns a transaction for a close out call */
|
|
644
|
+
closeOut: (params) => {
|
|
645
|
+
return this._algorand.createTransaction.appCall(this.params.bare.closeOut(params));
|
|
646
|
+
},
|
|
647
|
+
/** Returns a transaction for a call (defaults to no-op) */
|
|
648
|
+
call: (params) => {
|
|
649
|
+
return this._algorand.createTransaction.appCall(this.params.bare.call(params));
|
|
650
|
+
},
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
getBareSendMethods() {
|
|
654
|
+
return {
|
|
655
|
+
/** Signs and sends an update call, including deploy-time TEAL template replacements and compilation if provided */
|
|
656
|
+
update: async (params) => {
|
|
657
|
+
const compiled = await this.compile(params);
|
|
658
|
+
return {
|
|
659
|
+
...(await this.handleCallErrors(async () => this._algorand.send.appUpdate(await this.params.bare.update(params)))),
|
|
660
|
+
...compiled,
|
|
661
|
+
};
|
|
662
|
+
},
|
|
663
|
+
/** Signs and sends an opt-in call */
|
|
664
|
+
optIn: (params) => {
|
|
665
|
+
return this.handleCallErrors(() => this._algorand.send.appCall(this.params.bare.optIn(params)));
|
|
666
|
+
},
|
|
667
|
+
/** Signs and sends a delete call */
|
|
668
|
+
delete: (params) => {
|
|
669
|
+
return this.handleCallErrors(() => this._algorand.send.appDelete(this.params.bare.delete(params)));
|
|
670
|
+
},
|
|
671
|
+
/** Signs and sends a clear state call */
|
|
672
|
+
clearState: (params) => {
|
|
673
|
+
return this.handleCallErrors(() => this._algorand.send.appCall(this.params.bare.clearState(params)));
|
|
674
|
+
},
|
|
675
|
+
/** Signs and sends a close out call */
|
|
676
|
+
closeOut: (params) => {
|
|
677
|
+
return this.handleCallErrors(() => this._algorand.send.appCall(this.params.bare.closeOut(params)));
|
|
678
|
+
},
|
|
679
|
+
/** Signs and sends a call (defaults to no-op) */
|
|
680
|
+
call: (params) => {
|
|
681
|
+
return this.handleCallErrors(() => this._algorand.send.appCall(this.params.bare.call(params)));
|
|
682
|
+
},
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
getMethodCallParamsMethods() {
|
|
686
|
+
return {
|
|
687
|
+
/** Return params for a payment transaction to fund the app account */
|
|
688
|
+
fundAppAccount: (params) => {
|
|
689
|
+
return {
|
|
690
|
+
...params,
|
|
691
|
+
sender: this.getSender(params.sender),
|
|
692
|
+
signer: this.getSigner(params.sender, params.signer),
|
|
693
|
+
receiver: this.appAddress,
|
|
694
|
+
};
|
|
695
|
+
},
|
|
696
|
+
/** Return params for an update ABI call, including deploy-time TEAL template replacements and compilation if provided */
|
|
697
|
+
update: async (params) => {
|
|
698
|
+
return (await this.getABIParams({
|
|
699
|
+
...params,
|
|
700
|
+
...(await this.compile(params)),
|
|
701
|
+
}, OnApplicationComplete.UpdateApplicationOC));
|
|
702
|
+
},
|
|
703
|
+
/** Return params for an opt-in ABI call */
|
|
704
|
+
optIn: async (params) => {
|
|
705
|
+
return (await this.getABIParams(params, OnApplicationComplete.OptInOC));
|
|
706
|
+
},
|
|
707
|
+
/** Return params for an delete ABI call */
|
|
708
|
+
delete: async (params) => {
|
|
709
|
+
return (await this.getABIParams(params, OnApplicationComplete.DeleteApplicationOC));
|
|
710
|
+
},
|
|
711
|
+
/** Return params for an close out ABI call */
|
|
712
|
+
closeOut: async (params) => {
|
|
713
|
+
return (await this.getABIParams(params, OnApplicationComplete.CloseOutOC));
|
|
714
|
+
},
|
|
715
|
+
/** Return params for an ABI call */
|
|
716
|
+
call: async (params) => {
|
|
717
|
+
return (await this.getABIParams(params, params.onComplete ?? OnApplicationComplete.NoOpOC));
|
|
718
|
+
},
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
getMethodCallSendMethods() {
|
|
722
|
+
return {
|
|
723
|
+
/** Sign and send transactions for a payment transaction to fund the app account */
|
|
724
|
+
fundAppAccount: (params) => {
|
|
725
|
+
return this._algorand.send.payment(this.params.fundAppAccount(params));
|
|
726
|
+
},
|
|
727
|
+
/**
|
|
728
|
+
* Sign and send transactions for an update ABI call, including deploy-time TEAL template replacements and compilation if provided
|
|
729
|
+
*/
|
|
730
|
+
update: async (params) => {
|
|
731
|
+
const compiled = await this.compile(params);
|
|
732
|
+
return {
|
|
733
|
+
...(await this.handleCallErrors(async () => this.processMethodCallReturn(this._algorand.send.appUpdateMethodCall(await this.params.update({ ...params })), getArc56Method(params.method, this._appSpec)))),
|
|
734
|
+
...compiled,
|
|
735
|
+
};
|
|
736
|
+
},
|
|
737
|
+
/**
|
|
738
|
+
* Sign and send transactions for an opt-in ABI call
|
|
739
|
+
*/
|
|
740
|
+
optIn: (params) => {
|
|
741
|
+
return this.handleCallErrors(async () => this.processMethodCallReturn(this._algorand.send.appCallMethodCall(await this.params.optIn(params)), getArc56Method(params.method, this._appSpec)));
|
|
742
|
+
},
|
|
743
|
+
/**
|
|
744
|
+
* Sign and send transactions for a delete ABI call
|
|
745
|
+
*/
|
|
746
|
+
delete: (params) => {
|
|
747
|
+
return this.handleCallErrors(async () => this.processMethodCallReturn(this._algorand.send.appDeleteMethodCall(await this.params.delete(params)), getArc56Method(params.method, this._appSpec)));
|
|
748
|
+
},
|
|
749
|
+
/**
|
|
750
|
+
* Sign and send transactions for a close out ABI call
|
|
751
|
+
*/
|
|
752
|
+
closeOut: (params) => {
|
|
753
|
+
return this.handleCallErrors(async () => this.processMethodCallReturn(this._algorand.send.appCallMethodCall(await this.params.closeOut(params)), getArc56Method(params.method, this._appSpec)));
|
|
754
|
+
},
|
|
755
|
+
/**
|
|
756
|
+
* Sign and send transactions for a call (defaults to no-op)
|
|
757
|
+
*/
|
|
758
|
+
call: async (params) => {
|
|
759
|
+
// Read-only call - do it via simulate
|
|
760
|
+
if (params.onComplete === OnApplicationComplete.NoOpOC ||
|
|
761
|
+
(!params.onComplete && getArc56Method(params.method, this._appSpec).method.readonly)) {
|
|
762
|
+
const result = await this._algorand
|
|
763
|
+
.newGroup()
|
|
764
|
+
.addAppCallMethodCall(await this.params.call(params))
|
|
765
|
+
.simulate({
|
|
766
|
+
allowUnnamedResources: params.populateAppCallResources ?? true,
|
|
767
|
+
// Simulate calls for a readonly method shouldn't invoke signing
|
|
768
|
+
skipSignatures: true,
|
|
769
|
+
});
|
|
770
|
+
return this.processMethodCallReturn({
|
|
771
|
+
...result,
|
|
772
|
+
transaction: result.transactions.at(-1),
|
|
773
|
+
confirmation: result.confirmations.at(-1),
|
|
774
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
|
|
775
|
+
return: (result.returns?.length ?? 0 > 0) ? result.returns?.at(-1) : undefined,
|
|
776
|
+
}, getArc56Method(params.method, this._appSpec));
|
|
777
|
+
}
|
|
778
|
+
return this.handleCallErrors(async () => this.processMethodCallReturn(this._algorand.send.appCallMethodCall(await this.params.call(params)), getArc56Method(params.method, this._appSpec)));
|
|
779
|
+
},
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
getMethodCallCreateTransactionMethods() {
|
|
783
|
+
return {
|
|
784
|
+
/** Return transaction for a payment transaction to fund the app account */
|
|
785
|
+
fundAppAccount: (params) => {
|
|
786
|
+
return this._algorand.createTransaction.payment(this.params.fundAppAccount(params));
|
|
787
|
+
},
|
|
788
|
+
/**
|
|
789
|
+
* Return transactions for an update ABI call, including deploy-time TEAL template replacements and compilation if provided
|
|
790
|
+
*/
|
|
791
|
+
update: async (params) => {
|
|
792
|
+
return this._algorand.createTransaction.appUpdateMethodCall(await this.params.update(params));
|
|
793
|
+
},
|
|
794
|
+
/**
|
|
795
|
+
* Return transactions for an opt-in ABI call
|
|
796
|
+
*/
|
|
797
|
+
optIn: async (params) => {
|
|
798
|
+
return this._algorand.createTransaction.appCallMethodCall(await this.params.optIn(params));
|
|
799
|
+
},
|
|
800
|
+
/**
|
|
801
|
+
* Return transactions for a delete ABI call
|
|
802
|
+
*/
|
|
803
|
+
delete: async (params) => {
|
|
804
|
+
return this._algorand.createTransaction.appDeleteMethodCall(await this.params.delete(params));
|
|
805
|
+
},
|
|
806
|
+
/**
|
|
807
|
+
* Return transactions for a close out ABI call
|
|
808
|
+
*/
|
|
809
|
+
closeOut: async (params) => {
|
|
810
|
+
return this._algorand.createTransaction.appCallMethodCall(await this.params.closeOut(params));
|
|
811
|
+
},
|
|
812
|
+
/**
|
|
813
|
+
* Return transactions for an ABI call (defaults to no-op)
|
|
814
|
+
*/
|
|
815
|
+
call: async (params) => {
|
|
816
|
+
return this._algorand.createTransaction.appCallMethodCall(await this.params.call(params));
|
|
817
|
+
},
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
/** Returns the sender for a call, using the provided sender or using the `defaultSender`
|
|
821
|
+
* if none provided and throws an error if neither provided */
|
|
822
|
+
getSender(sender) {
|
|
823
|
+
if (!sender && !this._defaultSender) {
|
|
824
|
+
throw new Error(`No sender provided and no default sender present in app client for call to app ${this._appName}`);
|
|
825
|
+
}
|
|
826
|
+
return sender ?? this._defaultSender;
|
|
827
|
+
}
|
|
828
|
+
/** Returns the signer for a call, using the provided signer or the `defaultSigner`
|
|
829
|
+
* if no signer was provided and the call will use default sender
|
|
830
|
+
* or `undefined` otherwise (so the signer is resolved from `AlgorandClient`) */
|
|
831
|
+
getSigner(sender, signer) {
|
|
832
|
+
return signer ?? (!sender ? this._defaultSigner : undefined);
|
|
833
|
+
}
|
|
834
|
+
getBareParams(params, onComplete) {
|
|
835
|
+
return {
|
|
836
|
+
...params,
|
|
837
|
+
appId: this._appId,
|
|
838
|
+
sender: this.getSender(params?.sender),
|
|
839
|
+
signer: this.getSigner(params?.sender, params?.signer),
|
|
840
|
+
onComplete,
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
async getABIParams(params, onComplete) {
|
|
844
|
+
const sender = this.getSender(params.sender);
|
|
845
|
+
const method = getArc56Method(params.method, this._appSpec);
|
|
846
|
+
const args = await this.getABIArgsWithDefaultValues(params.method, params.args, sender);
|
|
847
|
+
return {
|
|
848
|
+
...params,
|
|
849
|
+
appId: this._appId,
|
|
850
|
+
sender: sender,
|
|
851
|
+
signer: this.getSigner(params.sender, params.signer),
|
|
852
|
+
method,
|
|
853
|
+
onComplete,
|
|
854
|
+
args,
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
/** Make the given call and catch any errors, augmenting with debugging information before re-throwing. */
|
|
858
|
+
async handleCallErrors(call) {
|
|
859
|
+
try {
|
|
860
|
+
return await call();
|
|
861
|
+
}
|
|
862
|
+
catch (e) {
|
|
863
|
+
throw await this.exposeLogicError(e);
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
getBoxMethods() {
|
|
867
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
868
|
+
const that = this;
|
|
869
|
+
const stateMethods = {
|
|
870
|
+
/**
|
|
871
|
+
* Returns all single-key state values in a record keyed by the key name and the value a decoded ABI value.
|
|
872
|
+
*/
|
|
873
|
+
getAll: async () => {
|
|
874
|
+
return Object.fromEntries(await Promise.all(Object.keys(that._appSpec.state.keys.box).map(async (key) => [key, await stateMethods.getValue(key)])));
|
|
875
|
+
},
|
|
876
|
+
/**
|
|
877
|
+
* Returns a single state value for the current app with the value a decoded ABI value.
|
|
878
|
+
* @param name The name of the state value to retrieve the value for
|
|
879
|
+
* @returns
|
|
880
|
+
*/
|
|
881
|
+
getValue: async (name) => {
|
|
882
|
+
const metadata = that._appSpec.state.keys.box[name];
|
|
883
|
+
const value = await that.getBoxValue(Buffer.from(metadata.key, 'base64'));
|
|
884
|
+
return getABIDecodedValue(value, metadata.valueType, that._appSpec.structs);
|
|
885
|
+
},
|
|
886
|
+
/**
|
|
887
|
+
*
|
|
888
|
+
* @param mapName The name of the map to read from
|
|
889
|
+
* @param key The key within the map (without any map prefix) as either a Buffer with the bytes or a value
|
|
890
|
+
* that will be converted to bytes by encoding it using the specified ABI key type
|
|
891
|
+
* in the ARC-56 spec
|
|
892
|
+
*/
|
|
893
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
894
|
+
getMapValue: async (mapName, key) => {
|
|
895
|
+
const metadata = that._appSpec.state.maps.box[mapName];
|
|
896
|
+
const prefix = Buffer.from(metadata.prefix ?? '', 'base64');
|
|
897
|
+
const encodedKey = Buffer.concat([prefix, getABIEncodedValue(key, metadata.keyType, that._appSpec.structs)]);
|
|
898
|
+
const base64Key = Buffer.from(encodedKey).toString('base64');
|
|
899
|
+
const value = await that.getBoxValue(Buffer.from(base64Key, 'base64'));
|
|
900
|
+
return getABIDecodedValue(value, metadata.valueType, that._appSpec.structs);
|
|
901
|
+
},
|
|
902
|
+
/**
|
|
903
|
+
*
|
|
904
|
+
* @param mapName The name of the map to read from
|
|
905
|
+
* @param key The key within the map as either a Buffer with the bytes or a value
|
|
906
|
+
* that will be converted to bytes by encoding it using the specified ABI key type
|
|
907
|
+
* in the ARC-56 spec
|
|
908
|
+
* @param appState
|
|
909
|
+
*/
|
|
910
|
+
getMap: async (mapName) => {
|
|
911
|
+
const metadata = that._appSpec.state.maps.box[mapName];
|
|
912
|
+
const prefix = Buffer.from(metadata.prefix ?? '', 'base64');
|
|
913
|
+
const boxNames = await that.getBoxNames();
|
|
914
|
+
return new Map(await Promise.all(boxNames
|
|
915
|
+
.filter((b) => binaryStartsWith(b.nameRaw, prefix))
|
|
916
|
+
.map(async (b) => {
|
|
917
|
+
const encodedKey = Buffer.concat([prefix, b.nameRaw]);
|
|
918
|
+
const base64Key = Buffer.from(encodedKey).toString('base64');
|
|
919
|
+
return [
|
|
920
|
+
getABIDecodedValue(b.nameRaw.slice(prefix.length), metadata.keyType, that._appSpec.structs),
|
|
921
|
+
getABIDecodedValue(await that.getBoxValue(Buffer.from(base64Key, 'base64')), metadata.valueType, that._appSpec.structs),
|
|
922
|
+
];
|
|
923
|
+
})));
|
|
924
|
+
},
|
|
925
|
+
};
|
|
926
|
+
return stateMethods;
|
|
927
|
+
}
|
|
928
|
+
getStateMethods(stateGetter, keyGetter, mapGetter) {
|
|
929
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
930
|
+
const that = this;
|
|
931
|
+
const stateMethods = {
|
|
932
|
+
/**
|
|
933
|
+
* Returns all single-key state values in a record keyed by the key name and the value a decoded ABI value.
|
|
934
|
+
*/
|
|
935
|
+
getAll: async () => {
|
|
936
|
+
const appState = await stateGetter();
|
|
937
|
+
return Object.fromEntries(await Promise.all(Object.keys(keyGetter()).map(async (key) => [key, await stateMethods.getValue(key, appState)])));
|
|
938
|
+
},
|
|
939
|
+
/**
|
|
940
|
+
* Returns a single state value for the current app with the value a decoded ABI value.
|
|
941
|
+
* @param name The name of the state value to retrieve the value for
|
|
942
|
+
* @param appState Optional cached value of the current state
|
|
943
|
+
* @returns
|
|
944
|
+
*/
|
|
945
|
+
getValue: async (name, appState) => {
|
|
946
|
+
const state = Object.values(appState ?? (await stateGetter()));
|
|
947
|
+
const metadata = keyGetter()[name];
|
|
948
|
+
const value = state.find((s) => s.keyBase64 === metadata.key);
|
|
949
|
+
if (value && 'valueRaw' in value) {
|
|
950
|
+
return getABIDecodedValue(value.valueRaw, metadata.valueType, that._appSpec.structs);
|
|
951
|
+
}
|
|
952
|
+
return value?.value;
|
|
953
|
+
},
|
|
954
|
+
/**
|
|
955
|
+
* Returns a single value from the given map for the current app with the value a decoded ABI value.
|
|
956
|
+
* @param mapName The name of the map to read from
|
|
957
|
+
* @param key The key within the map (without any map prefix) as either a Buffer with the bytes or a value
|
|
958
|
+
* that will be converted to bytes by encoding it using the specified ABI key type
|
|
959
|
+
* in the ARC-56 spec
|
|
960
|
+
* @param appState Optional cached value of the current state
|
|
961
|
+
*/
|
|
962
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
963
|
+
getMapValue: async (mapName, key, appState) => {
|
|
964
|
+
const state = Object.values(appState ?? (await stateGetter()));
|
|
965
|
+
const metadata = mapGetter()[mapName];
|
|
966
|
+
const prefix = Buffer.from(metadata.prefix ?? '', 'base64');
|
|
967
|
+
const encodedKey = Buffer.concat([prefix, getABIEncodedValue(key, metadata.keyType, that._appSpec.structs)]);
|
|
968
|
+
const base64Key = Buffer.from(encodedKey).toString('base64');
|
|
969
|
+
const value = state.find((s) => s.keyBase64 === base64Key);
|
|
970
|
+
if (value && 'valueRaw' in value) {
|
|
971
|
+
return getABIDecodedValue(value.valueRaw, metadata.valueType, that._appSpec.structs);
|
|
972
|
+
}
|
|
973
|
+
return value?.value;
|
|
974
|
+
},
|
|
975
|
+
/**
|
|
976
|
+
* Returns all map values for the given map.
|
|
977
|
+
* @param mapName The name of the map to read from
|
|
978
|
+
* @param appState Optional cached value of the current state
|
|
979
|
+
* @returns A map of all key-value pairs in the map as a `Record<string, ABIValue>`
|
|
980
|
+
*/
|
|
981
|
+
getMap: async (mapName) => {
|
|
982
|
+
const state = Object.values(await stateGetter());
|
|
983
|
+
const metadata = mapGetter()[mapName];
|
|
984
|
+
const prefix = Buffer.from(metadata.prefix ?? '', 'base64');
|
|
985
|
+
return new Map(state
|
|
986
|
+
.filter((s) => binaryStartsWith(s.keyRaw, prefix))
|
|
987
|
+
.map((s) => {
|
|
988
|
+
const key = s.keyRaw.slice(prefix.length);
|
|
989
|
+
return [
|
|
990
|
+
getABIDecodedValue(key, metadata.keyType, this._appSpec.structs),
|
|
991
|
+
getABIDecodedValue('valueRaw' in s ? s.valueRaw : s.value, metadata.valueType, this._appSpec.structs),
|
|
992
|
+
];
|
|
993
|
+
}));
|
|
994
|
+
},
|
|
995
|
+
};
|
|
996
|
+
return stateMethods;
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* @deprecated Use `AppClient` instead e.g. via `algorand.client.getAppClientById` or
|
|
1001
|
+
* `algorand.client.getAppClientByCreatorAndName`.
|
|
1002
|
+
* If you want to `create` or `deploy` then use `AppFactory` e.g. via `algorand.client.getAppFactory`,
|
|
1003
|
+
* which will in turn give you an `AppClient` instance against the created/deployed app to make other calls.
|
|
1004
|
+
*
|
|
1005
|
+
* Application client - a class that wraps an ARC-0032 app spec and provides high productivity methods to deploy and call the app */
|
|
41
1006
|
class ApplicationClient {
|
|
42
|
-
// todo: process ABI args as needed to make them nicer to deal with like beaker-ts
|
|
43
|
-
// todo: support readonly, noop method calls
|
|
44
|
-
// todo: find create, update, delete, etc. methods from app spec and call them by default
|
|
45
|
-
// todo: intelligent version management when deploying
|
|
46
1007
|
/**
|
|
1008
|
+
* @deprecated Use `AppClient` instead e.g. via `algorand.client.getAppClientById` or
|
|
1009
|
+
* `algorand.client.getAppClientByCreatorAndName`.
|
|
1010
|
+
* If you want to `create` or `deploy` then use `AppFactory` e.g. via `algorand.client.getAppFactory`,
|
|
1011
|
+
* which will in turn give you an `AppClient` instance against the created/deployed app to make other calls.
|
|
1012
|
+
*
|
|
47
1013
|
* Create a new ApplicationClient instance
|
|
48
1014
|
* @param appDetails The details of the app
|
|
49
1015
|
* @param algod An algod instance
|
|
@@ -78,9 +1044,11 @@ class ApplicationClient {
|
|
|
78
1044
|
this.params = params;
|
|
79
1045
|
}
|
|
80
1046
|
/**
|
|
81
|
-
*
|
|
1047
|
+
* @deprecated Use `AppClient.compile()` instead.
|
|
1048
|
+
*
|
|
1049
|
+
* Compiles the approval and clear state programs and sets up the source map.
|
|
82
1050
|
* @param compilation The deploy-time parameters for the compilation
|
|
83
|
-
* @returns The compiled approval and clear programs
|
|
1051
|
+
* @returns The compiled approval and clear state programs
|
|
84
1052
|
*/
|
|
85
1053
|
async compile(compilation) {
|
|
86
1054
|
const { deployTimeParams, updatable, deletable } = compilation ?? {};
|
|
@@ -95,15 +1063,12 @@ class ApplicationClient {
|
|
|
95
1063
|
const clear = performTemplateSubstitution(clearTemplate, deployTimeParams ?? this.deployTimeParams);
|
|
96
1064
|
const clearCompiled = await compileTeal(clear, this.algod);
|
|
97
1065
|
this._clearSourceMap = clearCompiled?.sourceMap;
|
|
98
|
-
if (Config.debug
|
|
99
|
-
|
|
1066
|
+
if (Config.debug) {
|
|
1067
|
+
await Config.events.emitAsync(EventType.AppCompiled, {
|
|
100
1068
|
sources: [
|
|
101
|
-
|
|
102
|
-
|
|
1069
|
+
{ compiledTeal: approvalCompiled, appName: this._appName, fileName: 'approval' },
|
|
1070
|
+
{ compiledTeal: clearCompiled, appName: this._appName, fileName: 'clear' },
|
|
103
1071
|
],
|
|
104
|
-
projectRoot: Config.projectRoot,
|
|
105
|
-
client: this.algod,
|
|
106
|
-
withSources: true,
|
|
107
1072
|
});
|
|
108
1073
|
}
|
|
109
1074
|
return { approvalCompiled, clearCompiled };
|
|
@@ -130,6 +1095,8 @@ class ApplicationClient {
|
|
|
130
1095
|
this._clearSourceMap = new SourceMap(sourceMaps.clearSourceMap);
|
|
131
1096
|
}
|
|
132
1097
|
/**
|
|
1098
|
+
* @deprecated Use `deploy` from an `AppFactory` instance instead.
|
|
1099
|
+
*
|
|
133
1100
|
* Idempotently deploy (create, update/delete if changed) an app against the given name via the given creator account, including deploy-time template placeholder substitutions.
|
|
134
1101
|
*
|
|
135
1102
|
* To understand the architecture decisions behind this functionality please see https://github.com/algorandfoundation/algokit-cli/blob/main/docs/architecture-decisions/2023-01-12_smart-contract-deployment.md
|
|
@@ -215,6 +1182,8 @@ class ApplicationClient {
|
|
|
215
1182
|
}
|
|
216
1183
|
}
|
|
217
1184
|
/**
|
|
1185
|
+
* @deprecated Use `create` from an `AppFactory` instance instead.
|
|
1186
|
+
*
|
|
218
1187
|
* Creates a smart contract app, returns the details of the created app.
|
|
219
1188
|
* @param create The parameters to create the app with
|
|
220
1189
|
* @returns The details of the created app, or the transaction to create it if `skipSending` and the compilation result
|
|
@@ -255,10 +1224,12 @@ class ApplicationClient {
|
|
|
255
1224
|
return { ...result, ...{ compiledApproval: approvalCompiled, compiledClear: clearCompiled } };
|
|
256
1225
|
}
|
|
257
1226
|
catch (e) {
|
|
258
|
-
throw this.exposeLogicError(e);
|
|
1227
|
+
throw await this.exposeLogicError(e);
|
|
259
1228
|
}
|
|
260
1229
|
}
|
|
261
1230
|
/**
|
|
1231
|
+
* @deprecated Use `appClient.send.update` or `appClient.createTransaction.update` from an `AppClient` instance instead.
|
|
1232
|
+
*
|
|
262
1233
|
* Updates the smart contract app.
|
|
263
1234
|
* @param update The parameters to update the app with
|
|
264
1235
|
* @returns The transaction send result and the compilation result
|
|
@@ -287,10 +1258,12 @@ class ApplicationClient {
|
|
|
287
1258
|
return { ...result, ...{ compiledApproval: approvalCompiled, compiledClear: clearCompiled } };
|
|
288
1259
|
}
|
|
289
1260
|
catch (e) {
|
|
290
|
-
throw this.exposeLogicError(e);
|
|
1261
|
+
throw await this.exposeLogicError(e);
|
|
291
1262
|
}
|
|
292
1263
|
}
|
|
293
1264
|
/**
|
|
1265
|
+
* @deprecated Use `appClient.send.call` or `appClient.createTransaction.call` from an `AppClient` instance instead.
|
|
1266
|
+
*
|
|
294
1267
|
* Issues a no_op (normal) call to the app.
|
|
295
1268
|
* @param call The call details.
|
|
296
1269
|
* @returns The result of the call
|
|
@@ -324,6 +1297,8 @@ class ApplicationClient {
|
|
|
324
1297
|
return await this.callOfType(call, 'no_op');
|
|
325
1298
|
}
|
|
326
1299
|
/**
|
|
1300
|
+
* @deprecated Use `appClient.send.optIn` or `appClient.createTransaction.optIn` from an `AppClient` instance instead.
|
|
1301
|
+
*
|
|
327
1302
|
* Issues a opt_in call to the app.
|
|
328
1303
|
* @param call The call details.
|
|
329
1304
|
* @returns The result of the call
|
|
@@ -332,6 +1307,8 @@ class ApplicationClient {
|
|
|
332
1307
|
return await this.callOfType(call, 'opt_in');
|
|
333
1308
|
}
|
|
334
1309
|
/**
|
|
1310
|
+
* @deprecated Use `appClient.send.closeOut` or `appClient.createTransaction.closeOut` from an `AppClient` instance instead.
|
|
1311
|
+
*
|
|
335
1312
|
* Issues a close_out call to the app.
|
|
336
1313
|
* @param call The call details.
|
|
337
1314
|
* @returns The result of the call
|
|
@@ -340,6 +1317,8 @@ class ApplicationClient {
|
|
|
340
1317
|
return await this.callOfType(call, 'close_out');
|
|
341
1318
|
}
|
|
342
1319
|
/**
|
|
1320
|
+
* @deprecated Use `appClient.send.clearState` or `appClient.createTransaction.clearState` from an `AppClient` instance instead.
|
|
1321
|
+
*
|
|
343
1322
|
* Issues a clear_state call to the app.
|
|
344
1323
|
* @param call The call details.
|
|
345
1324
|
* @returns The result of the call
|
|
@@ -348,6 +1327,8 @@ class ApplicationClient {
|
|
|
348
1327
|
return await this.callOfType(call, 'clear_state');
|
|
349
1328
|
}
|
|
350
1329
|
/**
|
|
1330
|
+
* @deprecated Use `appClient.send.delete` or `appClient.createTransaction.delete` from an `AppClient` instance instead.
|
|
1331
|
+
*
|
|
351
1332
|
* Issues a delete_application call to the app.
|
|
352
1333
|
* @param call The call details.
|
|
353
1334
|
* @returns The result of the call
|
|
@@ -356,6 +1337,8 @@ class ApplicationClient {
|
|
|
356
1337
|
return await this.callOfType(call, 'delete_application');
|
|
357
1338
|
}
|
|
358
1339
|
/**
|
|
1340
|
+
* @deprecated Use `appClient.send.call` or `appClient.createTransaction.call` from an `AppClient` instance instead.
|
|
1341
|
+
*
|
|
359
1342
|
* Issues a call to the app with the given call type.
|
|
360
1343
|
* @param call The call details.
|
|
361
1344
|
* @param callType The call type
|
|
@@ -498,6 +1481,8 @@ class ApplicationClient {
|
|
|
498
1481
|
})));
|
|
499
1482
|
}
|
|
500
1483
|
/**
|
|
1484
|
+
* @deprecated Use `appClient.params.*` from an `AppClient` instance instead.
|
|
1485
|
+
*
|
|
501
1486
|
* Returns the arguments for an app call for the given ABI method or raw method specification.
|
|
502
1487
|
* @param args The call args specific to this application client
|
|
503
1488
|
* @param sender The sender of this call. Will be used to fetch any default argument values if applicable
|
|
@@ -555,6 +1540,8 @@ class ApplicationClient {
|
|
|
555
1540
|
}
|
|
556
1541
|
}
|
|
557
1542
|
/**
|
|
1543
|
+
* @deprecated Use `appClient.getABIMethod` instead.
|
|
1544
|
+
*
|
|
558
1545
|
* Returns the ABI Method parameters for the given method name string for the app represented by this application client instance
|
|
559
1546
|
* @param method Either the name of the method or the ABI method spec definition string
|
|
560
1547
|
* @returns The ABI method params for the given method
|
|
@@ -581,6 +1568,8 @@ class ApplicationClient {
|
|
|
581
1568
|
return methodParams ? new ABIMethod(methodParams) : undefined;
|
|
582
1569
|
}
|
|
583
1570
|
/**
|
|
1571
|
+
* @deprecated Use `appClient.appId` and `appClient.appAddress` from an `AppClient` instance instead.
|
|
1572
|
+
*
|
|
584
1573
|
* Gets the reference information for the current application instance.
|
|
585
1574
|
* `appId` will be 0 if it can't find an app.
|
|
586
1575
|
* @returns The app reference, or if deployed using the `deploy` method, the app metadata too
|
|
@@ -622,7 +1611,7 @@ class ApplicationClient {
|
|
|
622
1611
|
.toString()
|
|
623
1612
|
.split('\n'),
|
|
624
1613
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
625
|
-
isClear ? this._clearSourceMap : this._approvalSourceMap);
|
|
1614
|
+
(pc) => (isClear ? this._clearSourceMap : this._approvalSourceMap).getLineForPc(pc));
|
|
626
1615
|
else
|
|
627
1616
|
return e;
|
|
628
1617
|
}
|
|
@@ -631,5 +1620,5 @@ class ApplicationClient {
|
|
|
631
1620
|
}
|
|
632
1621
|
}
|
|
633
1622
|
|
|
634
|
-
export { ApplicationClient };
|
|
1623
|
+
export { AppClient, ApplicationClient };
|
|
635
1624
|
//# sourceMappingURL=app-client.mjs.map
|