@algorandfoundation/algokit-utils 2.3.3-beta.3 → 2.3.3-beta.5
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/cjs/account.d.ts.map +1 -0
- package/cjs/account.js.map +1 -0
- package/cjs/amount.d.ts.map +1 -0
- package/cjs/amount.js.map +1 -0
- package/cjs/app-client.d.ts.map +1 -0
- package/cjs/app-client.js.map +1 -0
- package/cjs/app-deploy.d.ts.map +1 -0
- package/cjs/app-deploy.js.map +1 -0
- package/cjs/app.d.ts.map +1 -0
- package/cjs/app.js.map +1 -0
- package/cjs/index.d.ts.map +1 -0
- package/cjs/index.js.map +1 -0
- package/cjs/indexer-lookup.d.ts.map +1 -0
- package/cjs/indexer-lookup.js.map +1 -0
- package/cjs/localnet.d.ts.map +1 -0
- package/{localnet.js → cjs/localnet.js} +1 -1
- package/cjs/localnet.js.map +1 -0
- package/cjs/network-client.d.ts.map +1 -0
- package/cjs/network-client.js.map +1 -0
- package/cjs/testing/account.d.ts.map +1 -0
- package/cjs/testing/account.js.map +1 -0
- package/cjs/testing/fixtures/algokit-log-capture-fixture.d.ts.map +1 -0
- package/cjs/testing/fixtures/algokit-log-capture-fixture.js.map +1 -0
- package/cjs/testing/fixtures/algorand-fixture.d.ts.map +1 -0
- package/cjs/testing/fixtures/algorand-fixture.js.map +1 -0
- package/cjs/testing/fixtures/index.d.ts.map +1 -0
- package/cjs/testing/fixtures/index.js.map +1 -0
- package/cjs/testing/index.d.ts.map +1 -0
- package/cjs/testing/index.js.map +1 -0
- package/cjs/testing/indexer.d.ts.map +1 -0
- package/cjs/testing/indexer.js.map +1 -0
- package/cjs/testing/test-logger.d.ts.map +1 -0
- package/{testing → cjs/testing}/test-logger.js +2 -0
- package/cjs/testing/test-logger.js.map +1 -0
- package/cjs/testing/transaction-logger.d.ts.map +1 -0
- package/{testing → cjs/testing}/transaction-logger.js +2 -3
- package/cjs/testing/transaction-logger.js.map +1 -0
- package/cjs/transaction.d.ts.map +1 -0
- package/cjs/transaction.js.map +1 -0
- package/cjs/transfer.d.ts.map +1 -0
- package/cjs/transfer.js.map +1 -0
- package/cjs/types/account.d.ts +66 -0
- package/cjs/types/account.d.ts.map +1 -0
- package/{types → cjs/types}/account.js +7 -0
- package/cjs/types/account.js.map +1 -0
- package/cjs/types/algo-http-client-with-retry.d.ts.map +1 -0
- package/{types → cjs/types}/algo-http-client-with-retry.js +16 -16
- package/cjs/types/algo-http-client-with-retry.js.map +1 -0
- package/cjs/types/amount.d.ts +24 -0
- package/cjs/types/amount.d.ts.map +1 -0
- package/{types → cjs/types}/amount.js +1 -0
- package/cjs/types/amount.js.map +1 -0
- package/cjs/types/app-client.d.ts +408 -0
- package/cjs/types/app-client.d.ts.map +1 -0
- package/{types → cjs/types}/app-client.js +13 -0
- package/cjs/types/app-client.js.map +1 -0
- package/cjs/types/app-spec.d.ts.map +1 -0
- package/{types → cjs/types}/app-spec.js.map +1 -1
- package/cjs/types/app.d.ts +308 -0
- package/cjs/types/app.d.ts.map +1 -0
- package/cjs/types/app.js.map +1 -0
- package/cjs/types/config.d.ts.map +1 -0
- package/{types → cjs/types}/config.js +1 -0
- package/cjs/types/config.js.map +1 -0
- package/cjs/types/indexer.d.ts.map +1 -0
- package/cjs/types/indexer.js.map +1 -0
- package/cjs/types/logging.d.ts.map +1 -0
- package/cjs/types/logging.js.map +1 -0
- package/cjs/types/logic-error.d.ts.map +1 -0
- package/{types → cjs/types}/logic-error.js +5 -2
- package/cjs/types/logic-error.js.map +1 -0
- package/cjs/types/network-client.d.ts +20 -0
- package/cjs/types/network-client.d.ts.map +1 -0
- package/cjs/types/network-client.js.map +1 -0
- package/cjs/types/testing.d.ts.map +1 -0
- package/{types → cjs/types}/testing.js.map +1 -1
- package/cjs/types/transaction.d.ts +111 -0
- package/cjs/types/transaction.d.ts.map +1 -0
- package/{types → cjs/types}/transaction.js.map +1 -1
- package/cjs/types/transfer.d.ts +32 -0
- package/cjs/types/transfer.d.ts.map +1 -0
- package/{types → cjs/types}/transfer.js.map +1 -1
- package/cjs/types/urlTokenBaseHTTPClient.d.ts.map +1 -0
- package/{types → cjs/types}/urlTokenBaseHTTPClient.js +4 -0
- package/cjs/types/urlTokenBaseHTTPClient.js.map +1 -0
- package/cjs/util.d.ts.map +1 -0
- package/cjs/util.js.map +1 -0
- package/esm/account.js +170 -0
- package/esm/account.js.map +1 -0
- package/esm/amount.js +27 -0
- package/esm/amount.js.map +1 -0
- package/esm/app-client.js +76 -0
- package/esm/app-client.js.map +1 -0
- package/esm/app-deploy.js +446 -0
- package/esm/app-deploy.js.map +1 -0
- package/esm/app.js +572 -0
- package/esm/app.js.map +1 -0
- package/esm/index.js +14 -0
- package/esm/index.js.map +1 -0
- package/esm/indexer-lookup.js +98 -0
- package/esm/indexer-lookup.js.map +1 -0
- package/esm/localnet.js +111 -0
- package/esm/localnet.js.map +1 -0
- package/esm/network-client.js +179 -0
- package/esm/network-client.js.map +1 -0
- package/esm/testing/account.js +24 -0
- package/esm/testing/account.js.map +1 -0
- package/esm/testing/fixtures/algokit-log-capture-fixture.js +39 -0
- package/esm/testing/fixtures/algokit-log-capture-fixture.js.map +1 -0
- package/esm/testing/fixtures/algorand-fixture.js +53 -0
- package/esm/testing/fixtures/algorand-fixture.js.map +1 -0
- package/esm/testing/fixtures/index.js +3 -0
- package/esm/testing/fixtures/index.js.map +1 -0
- package/esm/testing/index.js +6 -0
- package/esm/testing/index.js.map +1 -0
- package/esm/testing/indexer.js +36 -0
- package/esm/testing/indexer.js.map +1 -0
- package/esm/testing/test-logger.js +69 -0
- package/esm/testing/test-logger.js.map +1 -0
- package/esm/testing/transaction-logger.js +66 -0
- package/esm/testing/transaction-logger.js.map +1 -0
- package/esm/transaction.js +365 -0
- package/esm/transaction.js.map +1 -0
- package/esm/transfer.js +59 -0
- package/esm/transfer.js.map +1 -0
- package/esm/types/account.js +90 -0
- package/esm/types/account.js.map +1 -0
- package/esm/types/algo-http-client-with-retry.js +58 -0
- package/esm/types/algo-http-client-with-retry.js.map +1 -0
- package/esm/types/amount.js +35 -0
- package/esm/types/amount.js.map +1 -0
- package/esm/types/app-client.js +624 -0
- package/esm/types/app-client.js.map +1 -0
- package/esm/types/app-spec.js +2 -0
- package/esm/types/app-spec.js.map +1 -0
- package/esm/types/app.js +33 -0
- package/esm/types/app.js.map +1 -0
- package/esm/types/config.js +50 -0
- package/esm/types/config.js.map +1 -0
- package/esm/types/indexer.js +31 -0
- package/esm/types/indexer.js.map +1 -0
- package/esm/types/logging.js +24 -0
- package/esm/types/logging.js.map +1 -0
- package/esm/types/logic-error.js +49 -0
- package/esm/types/logic-error.js.map +1 -0
- package/esm/types/network-client.js +2 -0
- package/esm/types/network-client.js.map +1 -0
- package/esm/types/testing.js +2 -0
- package/esm/types/testing.js.map +1 -0
- package/esm/types/transaction.js +2 -0
- package/esm/types/transaction.js.map +1 -0
- package/esm/types/transfer.js +2 -0
- package/esm/types/transfer.js.map +1 -0
- package/esm/types/urlTokenBaseHTTPClient.js +151 -0
- package/esm/types/urlTokenBaseHTTPClient.js.map +1 -0
- package/esm/util.js +20 -0
- package/esm/util.js.map +1 -0
- package/package.json +42 -6
- package/types/account.d.ts +125 -63
- package/types/account.d.ts.map +1 -1
- package/types/amount.d.ts +18 -22
- package/types/amount.d.ts.map +1 -1
- package/types/app-client.d.ts +70 -407
- package/types/app-client.d.ts.map +1 -1
- package/types/app-deploy.d.ts +101 -0
- package/types/app-deploy.d.ts.map +1 -0
- package/types/app.d.ts +145 -290
- package/types/app.d.ts.map +1 -1
- package/types/index.d.ts +14 -0
- package/types/index.d.ts.map +1 -0
- package/types/indexer-lookup.d.ts +38 -0
- package/types/indexer-lookup.d.ts.map +1 -0
- package/types/localnet.d.ts +54 -0
- package/types/localnet.d.ts.map +1 -0
- package/types/network-client.d.ts +96 -19
- package/types/network-client.d.ts.map +1 -1
- package/types/testing/account.d.ts +14 -0
- package/types/testing/account.d.ts.map +1 -0
- package/types/testing/fixtures/algokit-log-capture-fixture.d.ts +19 -0
- package/types/testing/fixtures/algokit-log-capture-fixture.d.ts.map +1 -0
- package/types/testing/fixtures/algorand-fixture.d.ts +48 -0
- package/types/testing/fixtures/algorand-fixture.d.ts.map +1 -0
- package/types/testing/fixtures/index.d.ts +3 -0
- package/types/testing/fixtures/index.d.ts.map +1 -0
- package/types/testing/index.d.ts +6 -0
- package/types/testing/index.d.ts.map +1 -0
- package/types/testing/indexer.d.ts +12 -0
- package/types/testing/indexer.d.ts.map +1 -0
- package/types/testing/test-logger.d.ts +41 -0
- package/types/testing/test-logger.d.ts.map +1 -0
- package/types/testing/transaction-logger.d.ts +29 -0
- package/types/testing/transaction-logger.d.ts.map +1 -0
- package/types/transaction.d.ts +133 -106
- package/types/transaction.d.ts.map +1 -1
- package/types/transfer.d.ts +21 -31
- package/types/transfer.d.ts.map +1 -1
- package/types/types/account.d.ts +66 -0
- package/types/types/account.d.ts.map +1 -0
- package/types/types/algo-http-client-with-retry.d.ts +14 -0
- package/types/types/algo-http-client-with-retry.d.ts.map +1 -0
- package/types/types/amount.d.ts +24 -0
- package/types/types/amount.d.ts.map +1 -0
- package/types/types/app-client.d.ts +408 -0
- package/types/types/app-client.d.ts.map +1 -0
- package/types/types/app-spec.d.ts +154 -0
- package/types/types/app-spec.d.ts.map +1 -0
- package/types/types/app.d.ts +308 -0
- package/types/types/app.d.ts.map +1 -0
- package/types/types/config.d.ts +32 -0
- package/types/types/config.d.ts.map +1 -0
- package/types/types/indexer.d.ts +623 -0
- package/types/types/indexer.d.ts.map +1 -0
- package/types/types/logging.d.ts +13 -0
- package/types/types/logging.d.ts.map +1 -0
- package/types/types/logic-error.d.ts +37 -0
- package/types/types/logic-error.d.ts.map +1 -0
- package/types/types/network-client.d.ts +20 -0
- package/types/types/network-client.d.ts.map +1 -0
- package/types/types/testing.d.ts +89 -0
- package/types/types/testing.d.ts.map +1 -0
- package/types/types/transaction.d.ts +111 -0
- package/types/types/transaction.d.ts.map +1 -0
- package/types/types/transfer.d.ts +32 -0
- package/types/types/transfer.d.ts.map +1 -0
- package/types/types/urlTokenBaseHTTPClient.d.ts +41 -0
- package/types/types/urlTokenBaseHTTPClient.d.ts.map +1 -0
- package/types/util.d.ts +10 -0
- package/types/util.d.ts.map +1 -0
- package/account.d.ts.map +0 -1
- package/account.js.map +0 -1
- package/amount.d.ts.map +0 -1
- package/amount.js.map +0 -1
- package/app-client.d.ts.map +0 -1
- package/app-client.js.map +0 -1
- package/app-deploy.d.ts.map +0 -1
- package/app-deploy.js.map +0 -1
- package/app.d.ts.map +0 -1
- package/app.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/indexer-lookup.d.ts.map +0 -1
- package/indexer-lookup.js.map +0 -1
- package/localnet.d.ts.map +0 -1
- package/localnet.js.map +0 -1
- package/network-client.d.ts.map +0 -1
- package/network-client.js.map +0 -1
- package/testing/account.d.ts.map +0 -1
- package/testing/account.js.map +0 -1
- package/testing/fixtures/algokit-log-capture-fixture.d.ts.map +0 -1
- package/testing/fixtures/algokit-log-capture-fixture.js.map +0 -1
- package/testing/fixtures/algorand-fixture.d.ts.map +0 -1
- package/testing/fixtures/algorand-fixture.js.map +0 -1
- package/testing/fixtures/index.d.ts.map +0 -1
- package/testing/fixtures/index.js.map +0 -1
- package/testing/index.d.ts.map +0 -1
- package/testing/index.js.map +0 -1
- package/testing/indexer.d.ts.map +0 -1
- package/testing/indexer.js.map +0 -1
- package/testing/test-logger.d.ts.map +0 -1
- package/testing/test-logger.js.map +0 -1
- package/testing/transaction-logger.d.ts.map +0 -1
- package/testing/transaction-logger.js.map +0 -1
- package/transaction.d.ts.map +0 -1
- package/transaction.js.map +0 -1
- package/transfer.d.ts.map +0 -1
- package/transfer.js.map +0 -1
- package/types/account.js.map +0 -1
- package/types/algo-http-client-with-retry.d.ts.map +0 -1
- package/types/algo-http-client-with-retry.js.map +0 -1
- package/types/amount.js.map +0 -1
- package/types/app-client.js.map +0 -1
- package/types/app-spec.d.ts.map +0 -1
- package/types/app.js.map +0 -1
- package/types/config.d.ts.map +0 -1
- package/types/config.js.map +0 -1
- package/types/indexer.d.ts.map +0 -1
- package/types/indexer.js.map +0 -1
- package/types/logging.d.ts.map +0 -1
- package/types/logging.js.map +0 -1
- package/types/logic-error.d.ts.map +0 -1
- package/types/logic-error.js.map +0 -1
- package/types/network-client.js.map +0 -1
- package/types/testing.d.ts.map +0 -1
- package/types/urlTokenBaseHTTPClient.d.ts.map +0 -1
- package/types/urlTokenBaseHTTPClient.js.map +0 -1
- package/util.d.ts.map +0 -1
- package/util.js.map +0 -1
- /package/{account.d.ts → cjs/account.d.ts} +0 -0
- /package/{account.js → cjs/account.js} +0 -0
- /package/{amount.d.ts → cjs/amount.d.ts} +0 -0
- /package/{amount.js → cjs/amount.js} +0 -0
- /package/{app-client.d.ts → cjs/app-client.d.ts} +0 -0
- /package/{app-client.js → cjs/app-client.js} +0 -0
- /package/{app-deploy.d.ts → cjs/app-deploy.d.ts} +0 -0
- /package/{app-deploy.js → cjs/app-deploy.js} +0 -0
- /package/{app.d.ts → cjs/app.d.ts} +0 -0
- /package/{app.js → cjs/app.js} +0 -0
- /package/{index.d.ts → cjs/index.d.ts} +0 -0
- /package/{index.js → cjs/index.js} +0 -0
- /package/{indexer-lookup.d.ts → cjs/indexer-lookup.d.ts} +0 -0
- /package/{indexer-lookup.js → cjs/indexer-lookup.js} +0 -0
- /package/{localnet.d.ts → cjs/localnet.d.ts} +0 -0
- /package/{network-client.d.ts → cjs/network-client.d.ts} +0 -0
- /package/{network-client.js → cjs/network-client.js} +0 -0
- /package/{testing → cjs/testing}/account.d.ts +0 -0
- /package/{testing → cjs/testing}/account.js +0 -0
- /package/{testing → cjs/testing}/fixtures/algokit-log-capture-fixture.d.ts +0 -0
- /package/{testing → cjs/testing}/fixtures/algokit-log-capture-fixture.js +0 -0
- /package/{testing → cjs/testing}/fixtures/algorand-fixture.d.ts +0 -0
- /package/{testing → cjs/testing}/fixtures/algorand-fixture.js +0 -0
- /package/{testing → cjs/testing}/fixtures/index.d.ts +0 -0
- /package/{testing → cjs/testing}/fixtures/index.js +0 -0
- /package/{testing → cjs/testing}/index.d.ts +0 -0
- /package/{testing → cjs/testing}/index.js +0 -0
- /package/{testing → cjs/testing}/indexer.d.ts +0 -0
- /package/{testing → cjs/testing}/indexer.js +0 -0
- /package/{testing → cjs/testing}/test-logger.d.ts +0 -0
- /package/{testing → cjs/testing}/transaction-logger.d.ts +0 -0
- /package/{transaction.d.ts → cjs/transaction.d.ts} +0 -0
- /package/{transaction.js → cjs/transaction.js} +0 -0
- /package/{transfer.d.ts → cjs/transfer.d.ts} +0 -0
- /package/{transfer.js → cjs/transfer.js} +0 -0
- /package/{types → cjs/types}/algo-http-client-with-retry.d.ts +0 -0
- /package/{types → cjs/types}/app-spec.d.ts +0 -0
- /package/{types → cjs/types}/app-spec.js +0 -0
- /package/{types → cjs/types}/app.js +0 -0
- /package/{types → cjs/types}/config.d.ts +0 -0
- /package/{types → cjs/types}/indexer.d.ts +0 -0
- /package/{types → cjs/types}/indexer.js +0 -0
- /package/{types → cjs/types}/logging.d.ts +0 -0
- /package/{types → cjs/types}/logging.js +0 -0
- /package/{types → cjs/types}/logic-error.d.ts +0 -0
- /package/{types → cjs/types}/network-client.js +0 -0
- /package/{types → cjs/types}/testing.d.ts +0 -0
- /package/{types → cjs/types}/testing.js +0 -0
- /package/{types → cjs/types}/transaction.js +0 -0
- /package/{types → cjs/types}/transfer.js +0 -0
- /package/{types → cjs/types}/urlTokenBaseHTTPClient.d.ts +0 -0
- /package/{util.d.ts → cjs/util.d.ts} +0 -0
- /package/{util.js → cjs/util.js} +0 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { getSenderAddress } from '../';
|
|
2
|
+
/** Exposes an AlgoKit logger which captures log messages, while wrapping an original logger.
|
|
3
|
+
* This is useful for automated testing.
|
|
4
|
+
*/
|
|
5
|
+
export class TestLogger {
|
|
6
|
+
originalLogger;
|
|
7
|
+
logs;
|
|
8
|
+
/**
|
|
9
|
+
* Create a new test logger that wraps the given logger if provided.
|
|
10
|
+
* @param originalLogger The optional original logger to wrap.
|
|
11
|
+
*/
|
|
12
|
+
constructor(originalLogger) {
|
|
13
|
+
this.originalLogger = originalLogger;
|
|
14
|
+
this.logs = [];
|
|
15
|
+
}
|
|
16
|
+
/** Returns all logs captured thus far. */
|
|
17
|
+
get capturedLogs() {
|
|
18
|
+
return this.logs;
|
|
19
|
+
}
|
|
20
|
+
/** Clears all logs captured so far. */
|
|
21
|
+
clear() {
|
|
22
|
+
this.logs = [];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Returns a captured log snapshot.
|
|
26
|
+
* This helps ensure that the provided configuration items won't appear
|
|
27
|
+
* with random values in the log snapshot, but rather will get substituted with predictable ids.
|
|
28
|
+
*
|
|
29
|
+
* https://jestjs.io/docs/snapshot-testing#2-tests-should-be-deterministic
|
|
30
|
+
*
|
|
31
|
+
* @example Jest Example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const logger = new TestLogger()
|
|
34
|
+
* ...
|
|
35
|
+
* expect(logger.getLogSnapshot()).toMatchSnapshot()
|
|
36
|
+
* ```
|
|
37
|
+
* @param config The snapshot configuration
|
|
38
|
+
* @returns The snapshotted logs.
|
|
39
|
+
*/
|
|
40
|
+
getLogSnapshot(config) {
|
|
41
|
+
const { transactions: transactionIds, accounts, apps } = config ?? {};
|
|
42
|
+
let snapshot = this.capturedLogs.join('\n');
|
|
43
|
+
transactionIds?.forEach((txn, id) => (snapshot = snapshot.replace(new RegExp(typeof txn === 'string' ? txn : txn.txID(), 'g'), `TXID_${id + 1}`)));
|
|
44
|
+
accounts?.forEach((sender, id) => (snapshot = snapshot.replace(new RegExp(typeof sender === 'string' ? sender : getSenderAddress(sender), 'g'), `ACCOUNT_${id + 1}`)));
|
|
45
|
+
apps?.forEach((app, id) => (snapshot = snapshot.replace(new RegExp(`\\b${app.toString()}\\b(?! bytes)`, 'g'), `APP_${id + 1}`)));
|
|
46
|
+
return snapshot;
|
|
47
|
+
}
|
|
48
|
+
error(message, ...optionalParams) {
|
|
49
|
+
this.originalLogger?.error(message, ...optionalParams);
|
|
50
|
+
this.logs.push(`ERROR: ${message}${optionalParams.length ? ` | ${JSON.stringify(optionalParams)}` : ''}`);
|
|
51
|
+
}
|
|
52
|
+
warn(message, ...optionalParams) {
|
|
53
|
+
this.originalLogger?.warn(message, ...optionalParams);
|
|
54
|
+
this.logs.push(`WARN: ${message}${optionalParams.length ? ` | ${JSON.stringify(optionalParams)}` : ''}`);
|
|
55
|
+
}
|
|
56
|
+
info(message, ...optionalParams) {
|
|
57
|
+
this.originalLogger?.info(message, ...optionalParams);
|
|
58
|
+
this.logs.push(`INFO: ${message}${optionalParams.length ? ` | ${JSON.stringify(optionalParams)}` : ''}`);
|
|
59
|
+
}
|
|
60
|
+
verbose(message, ...optionalParams) {
|
|
61
|
+
this.originalLogger?.verbose(message, ...optionalParams);
|
|
62
|
+
this.logs.push(`VERBOSE: ${message}${optionalParams.length ? ` | ${JSON.stringify(optionalParams)}` : ''}`);
|
|
63
|
+
}
|
|
64
|
+
debug(message, ...optionalParams) {
|
|
65
|
+
this.originalLogger?.debug(message, ...optionalParams);
|
|
66
|
+
this.logs.push(`DEBUG: ${message}${optionalParams.length ? ` | ${JSON.stringify(optionalParams)}` : ''}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=test-logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-logger.js","sourceRoot":"","sources":["../../../src/testing/test-logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,KAAK,CAAA;AAItC;;GAEG;AACH,MAAM,OAAO,UAAU;IACb,cAAc,CAAoB;IAClC,IAAI,CAAU;IAEtB;;;OAGG;IACH,YAAY,cAAuB;QACjC,IAAI,CAAC,cAAc,GAAG,cAAc,CAAA;QACpC,IAAI,CAAC,IAAI,GAAG,E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M,CAAC,CAAC,CAAC,MAAM,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;IAC7G,CAAC;IACD,KAAK,CAAC,OAAe,EAAE,GAAG,cAAyB;QACjD,IAAI,CAAC,cAAc,EAAE,KAAK,CAAC,OAAO,EAAE,GAAG,cAAc,CAAC,CAAA;QACtD,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,OAAO,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;IAC3G,CAAC;CACF"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { decodeSignedTransaction } from 'algosdk';
|
|
2
|
+
import { runWhenIndexerCaughtUp } from './';
|
|
3
|
+
/**
|
|
4
|
+
* Allows you to keep track of Algorand transaction IDs by wrapping an `Algodv2` in a proxy.
|
|
5
|
+
* Useful for automated tests.
|
|
6
|
+
*/
|
|
7
|
+
export class TransactionLogger {
|
|
8
|
+
_sentTransactionIds = [];
|
|
9
|
+
/**
|
|
10
|
+
* The list of transaction IDs that has been logged thus far.
|
|
11
|
+
*/
|
|
12
|
+
get sentTransactionIds() {
|
|
13
|
+
return this._sentTransactionIds;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Clear all logged IDs.
|
|
17
|
+
*/
|
|
18
|
+
clear() {
|
|
19
|
+
this._sentTransactionIds = [];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The method that captures raw transactions and stores the transaction IDs.
|
|
23
|
+
*/
|
|
24
|
+
logRawTransaction(signedTransactions) {
|
|
25
|
+
if (Array.isArray(signedTransactions)) {
|
|
26
|
+
for (const stxn of signedTransactions) {
|
|
27
|
+
const decoded = decodeSignedTransaction(stxn);
|
|
28
|
+
this._sentTransactionIds.push(decoded.txn.txID());
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const decoded = decodeSignedTransaction(signedTransactions);
|
|
33
|
+
this._sentTransactionIds.push(decoded.txn.txID());
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Return a proxy that wraps the given Algodv2 with this transaction logger.
|
|
37
|
+
*
|
|
38
|
+
* @param algod The `Algodv2` to wrap
|
|
39
|
+
* @returns The wrapped `Algodv2`, any transactions sent using this algod instance will be logged by this transaction logger
|
|
40
|
+
*/
|
|
41
|
+
capture(algod) {
|
|
42
|
+
return new Proxy(algod, new TransactionLoggingAlgodv2ProxyHandler(this));
|
|
43
|
+
}
|
|
44
|
+
/** Wait until all logged transactions IDs appear in the given `Indexer`. */
|
|
45
|
+
async waitForIndexer(indexer) {
|
|
46
|
+
await Promise.all(this._sentTransactionIds.map((txnId) => runWhenIndexerCaughtUp(() => indexer.lookupTransactionByID(txnId).do())));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
class TransactionLoggingAlgodv2ProxyHandler {
|
|
50
|
+
transactionLogger;
|
|
51
|
+
constructor(transactionLogger) {
|
|
52
|
+
this.transactionLogger = transactionLogger;
|
|
53
|
+
}
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
55
|
+
get(target, property, receiver) {
|
|
56
|
+
if (property === 'sendRawTransaction') {
|
|
57
|
+
return (stxOrStxs) => {
|
|
58
|
+
this.transactionLogger.logRawTransaction(stxOrStxs);
|
|
59
|
+
return target[property].call(receiver, stxOrStxs);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
63
|
+
return target[property];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=transaction-logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction-logger.js","sourceRoot":"","sources":["../../../src/testing/transaction-logger.ts"],"names":[],"mappings":"AAA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CACF;AAED,MAAM,qCAAqC;IACjC,iBAAiB,CAAmB;IAE5C,YAAY,iBAAoC;QAC9C,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAA;IAC5C,CAAC;IAED,8DAA8D;IAC9D,GAAG,CAAC,MAAe,EAAE,QAAyB,EAAE,QAAa;QAC3D,IAAI,QAAQ,KAAK,oBAAoB,EAAE;YACrC,OAAO,CAAC,SAAoC,EAAE,EAAE;gBAC9C,IAAI,CAAC,iBAAiB,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAA;gBACnD,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAA;YACnD,CAAC,CAAA;SACF;QACD,8DAA8D;QAC9D,OAAQ,MAAc,CAAC,QAAQ,CAAC,CAAA;IAClC,CAAC;CACF"}
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import algosdk, { AtomicTransactionComposer, modelsv2, } from 'algosdk';
|
|
2
|
+
import { Buffer } from 'buffer';
|
|
3
|
+
import { Config } from './';
|
|
4
|
+
import { toNumber } from './util';
|
|
5
|
+
/** Encodes a transaction note into a byte array ready to be included in an Algorand transaction.
|
|
6
|
+
*
|
|
7
|
+
* @param note The transaction note
|
|
8
|
+
* @returns the transaction note ready for inclusion in a transaction
|
|
9
|
+
*
|
|
10
|
+
* Case on the value of `data` this either be:
|
|
11
|
+
* * `null` | `undefined`: `undefined`
|
|
12
|
+
* * `string`: The string value
|
|
13
|
+
* * Uint8Array: passthrough
|
|
14
|
+
* * Arc2TransactionNote object: ARC-0002 compatible transaction note
|
|
15
|
+
* * Else: The object/value converted into a JSON string representation
|
|
16
|
+
*/
|
|
17
|
+
export function encodeTransactionNote(note) {
|
|
18
|
+
if (note == null || typeof note === 'undefined') {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
else if (typeof note === 'object' && note.constructor === Uint8Array) {
|
|
22
|
+
return note;
|
|
23
|
+
}
|
|
24
|
+
else if (typeof note === 'object' && 'dAppName' in note) {
|
|
25
|
+
const arc2Payload = `${note.dAppName}:${note.format}${typeof note.data === 'string' ? note.data : JSON.stringify(note.data)}`;
|
|
26
|
+
const encoder = new TextEncoder();
|
|
27
|
+
return encoder.encode(arc2Payload);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
const n = typeof note === 'string' ? note : JSON.stringify(note);
|
|
31
|
+
const encoder = new TextEncoder();
|
|
32
|
+
return encoder.encode(n);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns the public address of the given transaction sender.
|
|
37
|
+
* @param sender A transaction sender
|
|
38
|
+
* @returns The public address
|
|
39
|
+
*/
|
|
40
|
+
export const getSenderAddress = function (sender) {
|
|
41
|
+
return 'addr' in sender ? sender.addr : sender.address();
|
|
42
|
+
};
|
|
43
|
+
const memoize = (fn) => {
|
|
44
|
+
const cache = new Map();
|
|
45
|
+
const cached = function (val) {
|
|
46
|
+
return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val);
|
|
47
|
+
};
|
|
48
|
+
cached.cache = cache;
|
|
49
|
+
return cached;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Given a transaction in a variety of supported formats, returns a TransactionWithSigner object ready to be passed to an
|
|
53
|
+
* AtomicTransactionComposer's addTransaction method.
|
|
54
|
+
* @param transaction One of: A TransactionWithSigner object (returned as is), a TransactionToSign object (signer is obtained from the
|
|
55
|
+
* signer property), a Transaction object (signer is extracted from the defaultSender parameter), an async SendTransactionResult returned by
|
|
56
|
+
* one of algokit utils' helpers (signer is obtained from the defaultSender parameter)
|
|
57
|
+
* @param defaultSender The default sender to be used to obtain a signer where the object provided to the transaction parameter does not
|
|
58
|
+
* include a signer.
|
|
59
|
+
* @returns A TransactionWithSigner object.
|
|
60
|
+
*/
|
|
61
|
+
export const getTransactionWithSigner = async (transaction, defaultSender) => {
|
|
62
|
+
if ('txn' in transaction)
|
|
63
|
+
return transaction;
|
|
64
|
+
if (defaultSender === undefined)
|
|
65
|
+
throw new Error('Default sender must be provided when passing in a transaction object that does not contain its own signer');
|
|
66
|
+
return transaction instanceof Promise
|
|
67
|
+
? {
|
|
68
|
+
txn: (await transaction).transaction,
|
|
69
|
+
signer: getSenderTransactionSigner(defaultSender),
|
|
70
|
+
}
|
|
71
|
+
: 'transaction' in transaction
|
|
72
|
+
? {
|
|
73
|
+
txn: transaction.transaction,
|
|
74
|
+
signer: getSenderTransactionSigner(transaction.signer),
|
|
75
|
+
}
|
|
76
|
+
: {
|
|
77
|
+
txn: transaction,
|
|
78
|
+
signer: getSenderTransactionSigner(defaultSender),
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Returns a `TransactionSigner` for the given transaction sender.
|
|
83
|
+
* This function has memoization, so will return the same transaction signer for a given sender.
|
|
84
|
+
* @param sender A transaction sender
|
|
85
|
+
* @returns A transaction signer
|
|
86
|
+
*/
|
|
87
|
+
export const getSenderTransactionSigner = memoize(function (sender) {
|
|
88
|
+
return 'signer' in sender
|
|
89
|
+
? sender.signer
|
|
90
|
+
: 'lsig' in sender
|
|
91
|
+
? algosdk.makeLogicSigAccountTransactionSigner(sender)
|
|
92
|
+
: algosdk.makeBasicAccountTransactionSigner(sender);
|
|
93
|
+
});
|
|
94
|
+
/**
|
|
95
|
+
* Signs a single transaction by the given signer.
|
|
96
|
+
* @param transaction The transaction to sign
|
|
97
|
+
* @param signer The signer to sign
|
|
98
|
+
* @returns The signed transaction as a `Uint8Array`
|
|
99
|
+
*/
|
|
100
|
+
export const signTransaction = async (transaction, signer) => {
|
|
101
|
+
return 'sk' in signer
|
|
102
|
+
? transaction.signTxn(signer.sk)
|
|
103
|
+
: 'lsig' in signer
|
|
104
|
+
? algosdk.signLogicSigTransactionObject(transaction, signer).blob
|
|
105
|
+
: 'sign' in signer
|
|
106
|
+
? signer.sign(transaction)
|
|
107
|
+
: (await signer.signer([transaction], [0]))[0];
|
|
108
|
+
};
|
|
109
|
+
/** Prepares a transaction for sending and then (if instructed) signs and sends the given transaction to the chain.
|
|
110
|
+
*
|
|
111
|
+
* @param send The details for the transaction to prepare/send, including:
|
|
112
|
+
* * `transaction`: The unsigned transaction
|
|
113
|
+
* * `from`: The account to sign the transaction with: either an account with private key loaded or a logic signature account
|
|
114
|
+
* * `config`: The sending configuration for this transaction
|
|
115
|
+
* @param algod An algod client
|
|
116
|
+
*
|
|
117
|
+
* @returns An object with transaction (`transaction`) and (if `skipWaiting` is `false` or `undefined`) confirmation (`confirmation`)
|
|
118
|
+
*/
|
|
119
|
+
export const sendTransaction = async function (send, algod) {
|
|
120
|
+
const { transaction, from, sendParams } = send;
|
|
121
|
+
const { skipSending, skipWaiting, fee, maxFee, suppressLog, maxRoundsToWaitForConfirmation, atc } = sendParams ?? {};
|
|
122
|
+
controlFees(transaction, { fee, maxFee });
|
|
123
|
+
if (atc) {
|
|
124
|
+
atc.addTransaction({ txn: transaction, signer: getSenderTransactionSigner(from) });
|
|
125
|
+
return { transaction };
|
|
126
|
+
}
|
|
127
|
+
if (skipSending) {
|
|
128
|
+
return { transaction };
|
|
129
|
+
}
|
|
130
|
+
const signedTransaction = await signTransaction(transaction, from);
|
|
131
|
+
await algod.sendRawTransaction(signedTransaction).do();
|
|
132
|
+
Config.getLogger(suppressLog).info(`Sent transaction ID ${transaction.txID()} ${transaction.type} from ${getSenderAddress(from)}`);
|
|
133
|
+
let confirmation = undefined;
|
|
134
|
+
if (!skipWaiting) {
|
|
135
|
+
confirmation = await waitForConfirmation(transaction.txID(), maxRoundsToWaitForConfirmation ?? 5, algod);
|
|
136
|
+
}
|
|
137
|
+
return { transaction, confirmation };
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Signs and sends transactions that have been collected by an `AtomicTransactionComposer`.
|
|
141
|
+
* @param atcSend The parameters controlling the send, including:
|
|
142
|
+
* * `atc` The `AtomicTransactionComposer`
|
|
143
|
+
* * `sendParams` The parameters to control the send behaviour
|
|
144
|
+
* @param algod An algod client
|
|
145
|
+
* @returns An object with transaction IDs, transactions, group transaction ID (`groupTransactionId`) if more than 1 transaction sent, and (if `skipWaiting` is `false` or unset) confirmation (`confirmation`)
|
|
146
|
+
*/
|
|
147
|
+
export const sendAtomicTransactionComposer = async function (atcSend, algod) {
|
|
148
|
+
const { atc, sendParams } = atcSend;
|
|
149
|
+
const transactionsWithSigner = atc.buildGroup();
|
|
150
|
+
const transactionsToSend = transactionsWithSigner.map((t) => {
|
|
151
|
+
return t.txn;
|
|
152
|
+
});
|
|
153
|
+
let groupId = undefined;
|
|
154
|
+
if (transactionsToSend.length > 1) {
|
|
155
|
+
groupId = transactionsToSend[0].group ? Buffer.from(transactionsToSend[0].group).toString('base64') : '';
|
|
156
|
+
Config.getLogger(sendParams?.suppressLog).info(`Sending group of ${transactionsToSend.length} transactions (${groupId})`, {
|
|
157
|
+
transactionsToSend,
|
|
158
|
+
});
|
|
159
|
+
Config.getLogger(sendParams?.suppressLog).debug(`Transaction IDs (${groupId})`, transactionsToSend.map((t) => t.txID()));
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
const result = await atc.execute(algod, sendParams?.maxRoundsToWaitForConfirmation ?? 5);
|
|
163
|
+
if (transactionsToSend.length > 1) {
|
|
164
|
+
Config.getLogger(sendParams?.suppressLog).info(`Group transaction (${groupId}) sent with ${transactionsToSend.length} transactions`);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
Config.getLogger(sendParams?.suppressLog).info(`Sent transaction ID ${transactionsToSend[0].txID()} ${transactionsToSend[0].type} from ${algosdk.encodeAddress(transactionsToSend[0].from.publicKey)}`);
|
|
168
|
+
}
|
|
169
|
+
let confirmations = undefined;
|
|
170
|
+
if (!sendParams?.skipWaiting) {
|
|
171
|
+
confirmations = await Promise.all(transactionsToSend.map(async (t) => modelsv2.PendingTransactionResponse.from_obj_for_encoding(await algod.pendingTransactionInformation(t.txID()).do())));
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
groupId,
|
|
175
|
+
confirmations,
|
|
176
|
+
txIds: transactionsToSend.map((t) => t.txID()),
|
|
177
|
+
transactions: transactionsToSend,
|
|
178
|
+
returns: result.methodResults.map((r) => ({
|
|
179
|
+
decodeError: r.decodeError,
|
|
180
|
+
returnValue: r.returnValue,
|
|
181
|
+
rawReturnValue: r.rawReturnValue,
|
|
182
|
+
})),
|
|
183
|
+
};
|
|
184
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
185
|
+
}
|
|
186
|
+
catch (e) {
|
|
187
|
+
if (Config.debug && typeof e === 'object') {
|
|
188
|
+
e.traces = [];
|
|
189
|
+
Config.logger.debug('Received error executing Atomic Transaction Composer and debug flag enabled; attempting dry run to get more information');
|
|
190
|
+
const dryrun = await performAtomicTransactionComposerDryrun(atc, algod);
|
|
191
|
+
for (const txn of dryrun.txns) {
|
|
192
|
+
if (txn.appCallRejected()) {
|
|
193
|
+
e.traces.push({
|
|
194
|
+
trace: txn.appTrace(),
|
|
195
|
+
cost: txn.cost,
|
|
196
|
+
logs: txn.logs,
|
|
197
|
+
messages: txn.appCallMessages,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
throw e;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* Performs a dry run of the transactions loaded into the given AtomicTransactionComposer`
|
|
207
|
+
* @param atc The AtomicTransactionComposer` with transaction(s) loaded
|
|
208
|
+
* @param algod An Algod client
|
|
209
|
+
* @returns The dryrun result
|
|
210
|
+
*/
|
|
211
|
+
export async function performAtomicTransactionComposerDryrun(atc, algod) {
|
|
212
|
+
const signedTransactions = await atc.gatherSignatures();
|
|
213
|
+
const txns = signedTransactions.map((t) => {
|
|
214
|
+
return algosdk.decodeSignedTransaction(t);
|
|
215
|
+
});
|
|
216
|
+
const dryrun = await algosdk.createDryrun({ client: algod, txns });
|
|
217
|
+
return new algosdk.DryrunResult(await algod.dryrun(dryrun).do());
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Signs and sends a group of [up to 16](https://developer.algorand.org/docs/get-details/atomic_transfers/#create-transactions) transactions to the chain
|
|
221
|
+
*
|
|
222
|
+
* @param groupSend The group details to send, with:
|
|
223
|
+
* * `transactions`: The array of transactions to send along with their signing account
|
|
224
|
+
* * `sendParams`: The parameters to dictate how the group is sent
|
|
225
|
+
* @param algod An algod client
|
|
226
|
+
* @returns An object with transaction IDs, transactions, group transaction ID (`groupTransactionId`) if more than 1 transaction sent, and (if `skipWaiting` is `false` or unset) confirmation (`confirmation`)
|
|
227
|
+
*/
|
|
228
|
+
export const sendGroupOfTransactions = async function (groupSend, algod) {
|
|
229
|
+
const { transactions, signer, sendParams } = groupSend;
|
|
230
|
+
const defaultTransactionSigner = signer ? getSenderTransactionSigner(signer) : undefined;
|
|
231
|
+
const transactionsWithSigner = await Promise.all(transactions.map(async (t) => {
|
|
232
|
+
if ('signer' in t)
|
|
233
|
+
return {
|
|
234
|
+
txn: t.transaction,
|
|
235
|
+
signer: getSenderTransactionSigner(t.signer),
|
|
236
|
+
sender: t.signer,
|
|
237
|
+
};
|
|
238
|
+
const txn = 'then' in t ? (await t).transaction : t;
|
|
239
|
+
if (!signer) {
|
|
240
|
+
throw new Error(`Attempt to send transaction ${txn.txID()} as part of a group transaction, but no signer parameter was provided.`);
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
txn,
|
|
244
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
245
|
+
signer: defaultTransactionSigner,
|
|
246
|
+
sender: signer,
|
|
247
|
+
};
|
|
248
|
+
}));
|
|
249
|
+
const atc = new AtomicTransactionComposer();
|
|
250
|
+
transactionsWithSigner.forEach((txn) => atc.addTransaction(txn));
|
|
251
|
+
return (await sendAtomicTransactionComposer({ atc, sendParams }, algod));
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* Wait until the transaction is confirmed or rejected, or until `timeout`
|
|
255
|
+
* number of rounds have passed.
|
|
256
|
+
*
|
|
257
|
+
* @param algod An algod client
|
|
258
|
+
* @param transactionId The transaction ID to wait for
|
|
259
|
+
* @param maxRoundsToWait Maximum number of rounds to wait
|
|
260
|
+
*
|
|
261
|
+
* @return Pending transaction information
|
|
262
|
+
* @throws Throws an error if the transaction is not confirmed or rejected in the next `timeout` rounds
|
|
263
|
+
*/
|
|
264
|
+
export const waitForConfirmation = async function (transactionId, maxRoundsToWait, algod) {
|
|
265
|
+
if (maxRoundsToWait < 0) {
|
|
266
|
+
throw new Error(`Invalid timeout, received ${maxRoundsToWait}, expected > 0`);
|
|
267
|
+
}
|
|
268
|
+
// Get current round
|
|
269
|
+
const status = modelsv2.NodeStatusResponse.from_obj_for_encoding(await algod.status().do());
|
|
270
|
+
if (status === undefined) {
|
|
271
|
+
throw new Error('Unable to get node status');
|
|
272
|
+
}
|
|
273
|
+
// Loop for up to `timeout` rounds looking for a confirmed transaction
|
|
274
|
+
const startRound = BigInt(status.lastRound) + 1n;
|
|
275
|
+
let currentRound = startRound;
|
|
276
|
+
while (currentRound < startRound + BigInt(maxRoundsToWait)) {
|
|
277
|
+
try {
|
|
278
|
+
const pendingInfo = modelsv2.PendingTransactionResponse.from_obj_for_encoding(await algod.pendingTransactionInformation(transactionId).do());
|
|
279
|
+
if (pendingInfo !== undefined) {
|
|
280
|
+
const confirmedRound = pendingInfo.confirmedRound;
|
|
281
|
+
if (confirmedRound && confirmedRound > 0) {
|
|
282
|
+
return pendingInfo;
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
const poolError = pendingInfo.poolError;
|
|
286
|
+
if (poolError != null && poolError.length > 0) {
|
|
287
|
+
// If there was a pool error, then the transaction has been rejected!
|
|
288
|
+
throw new Error(`Transaction ${transactionId} was rejected; pool error: ${poolError}`);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
293
|
+
}
|
|
294
|
+
catch (e) {
|
|
295
|
+
if (e.name === 'URLTokenBaseHTTPError') {
|
|
296
|
+
currentRound++;
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
await algod.statusAfterBlock(toNumber(currentRound)).do();
|
|
301
|
+
currentRound++;
|
|
302
|
+
}
|
|
303
|
+
throw new Error(`Transaction ${transactionId} not confirmed after ${maxRoundsToWait} rounds`);
|
|
304
|
+
};
|
|
305
|
+
/**
|
|
306
|
+
* Limit the acceptable fee to a defined amount of µALGOs.
|
|
307
|
+
* This also sets the transaction to be flatFee to ensure the transaction only succeeds at
|
|
308
|
+
* the estimated rate.
|
|
309
|
+
* @param transaction The transaction to cap or suggested params object about to be used to create a transaction
|
|
310
|
+
* @param maxAcceptableFee The maximum acceptable fee to pay
|
|
311
|
+
*/
|
|
312
|
+
export function capTransactionFee(transaction, maxAcceptableFee) {
|
|
313
|
+
// If a flat fee hasn't already been defined
|
|
314
|
+
if (!transaction.flatFee) {
|
|
315
|
+
// Once a transaction has been constructed by algosdk, transaction.fee indicates what the total transaction fee
|
|
316
|
+
// Will be based on the current suggested fee-per-byte value.
|
|
317
|
+
if (transaction.fee > maxAcceptableFee.microAlgos) {
|
|
318
|
+
throw new Error(`Cancelled transaction due to high network congestion fees. Algorand suggested fees would cause this transaction to cost ${transaction.fee} µALGOs. Cap for this transaction is ${maxAcceptableFee.microAlgos} µALGOs.`);
|
|
319
|
+
}
|
|
320
|
+
else if (transaction.fee > algosdk.ALGORAND_MIN_TX_FEE) {
|
|
321
|
+
Config.logger.warn(`Algorand network congestion fees are in effect. This transaction will incur a fee of ${transaction.fee} µALGOs.`);
|
|
322
|
+
}
|
|
323
|
+
// Now set the flat on the transaction. Otherwise the network may increase the fee above our cap and perform the transaction.
|
|
324
|
+
transaction.flatFee = true;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Allows for control of fees on a `Transaction` or `SuggestedParams` object
|
|
329
|
+
* @param transaction The transaction or suggested params
|
|
330
|
+
* @param feeControl The fee control parameters
|
|
331
|
+
*/
|
|
332
|
+
export function controlFees(transaction, feeControl) {
|
|
333
|
+
const { fee, maxFee } = feeControl;
|
|
334
|
+
if (fee) {
|
|
335
|
+
transaction.fee = fee.microAlgos;
|
|
336
|
+
transaction.flatFee = true;
|
|
337
|
+
}
|
|
338
|
+
if (maxFee !== undefined) {
|
|
339
|
+
capTransactionFee(transaction, maxFee);
|
|
340
|
+
}
|
|
341
|
+
return transaction;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Returns suggested transaction parameters from algod unless some are already provided.
|
|
345
|
+
* @param params Optionally provide parameters to use
|
|
346
|
+
* @param algod Algod algod
|
|
347
|
+
* @returns The suggested transaction parameters
|
|
348
|
+
*/
|
|
349
|
+
export async function getTransactionParams(params, algod) {
|
|
350
|
+
return params ? { ...params } : await algod.getTransactionParams().do();
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Returns the array of transactions currently present in the given `AtomicTransactionComposer`
|
|
354
|
+
* @param atc The atomic transaction composer
|
|
355
|
+
* @returns The array of transactions with signers
|
|
356
|
+
*/
|
|
357
|
+
export function getAtomicTransactionComposerTransactions(atc) {
|
|
358
|
+
try {
|
|
359
|
+
return atc.clone().buildGroup();
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
return [];
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
//# sourceMappingURL=transaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction.js","sourceRoot":"","sources":["../../src/transaction.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,EAAE,EAEd,yBAAyB,EACzB,QAAQ,GAKT,MAAM,SAAS,CAAA;AAChB,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAA;AAa3B,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AAC,mBAAmB,EAAE;YACxD,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,wFAAwF,WAAW,CAAC,GAAG,UAAU,CAAC,CAAA;SACtI;QAED,6HAA6H;QAC7H,WAAW,CAAC,OAAO,GAAG,IAAI,CAAA;KAC3B;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CACzB,WAAc,EACd,UAAqD;IAErD,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,UAAU,CAAA;IAClC,IAAI,GAAG,EAAE;QACP,WAAW,CAAC,GAAG,GAAG,GAAG,CAAC,UAAU,CAAA;QAChC,WAAW,CAAC,OAAO,GAAG,IAAI,CAAA;KAC3B;IAED,IAAI,MAAM,KAAK,SAAS,EAAE;QACxB,iBAAiB,CAAC,WAAW,EAAE,MAAM,CAAC,CAAA;KACvC;IAED,OAAO,WAAW,CAAA;AACpB,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,MAAmC,EAAE,KAAc;IAC5F,OAAO,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC,oBAAoB,EAAE,CAAC,EAAE,EAAE,CAAA;AACzE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,wCAAwC,CAAC,GAA8B;IACrF,IAAI;QACF,OAAO,GAAG,CAAC,KAAK,EAAE,CAAC,UAAU,EAAE,CAAA;KAChC;IAAC,MAAM;QACN,OAAO,EAAE,CAAA;KACV;AACH,CAAC"}
|
package/esm/transfer.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import algosdk from 'algosdk';
|
|
2
|
+
import { Config, getDispenserAccount, microAlgos } from './';
|
|
3
|
+
import { encodeTransactionNote, getSenderAddress, getTransactionParams, sendTransaction } from './transaction';
|
|
4
|
+
/**
|
|
5
|
+
* Transfer ALGOs between two accounts.
|
|
6
|
+
* @param transfer The transfer definition
|
|
7
|
+
* @param algod An algod client
|
|
8
|
+
* @returns The transaction object and optionally the confirmation if it was sent to the chain (`skipSending` is `false` or unset)
|
|
9
|
+
*/
|
|
10
|
+
export async function transferAlgos(transfer, algod) {
|
|
11
|
+
const { from, to, amount, note, transactionParams, ...sendParams } = transfer;
|
|
12
|
+
const transaction = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
|
|
13
|
+
from: getSenderAddress(from),
|
|
14
|
+
to: typeof to === 'string' ? to : getSenderAddress(to),
|
|
15
|
+
amount: amount.microAlgos,
|
|
16
|
+
note: encodeTransactionNote(note),
|
|
17
|
+
suggestedParams: await getTransactionParams(transactionParams, algod),
|
|
18
|
+
closeRemainderTo: undefined,
|
|
19
|
+
rekeyTo: undefined,
|
|
20
|
+
});
|
|
21
|
+
if (!sendParams.skipSending) {
|
|
22
|
+
Config.getLogger(sendParams.suppressLog).debug(`Transferring ${amount.microAlgos}µALGOs from ${getSenderAddress(from)} to ${to}`);
|
|
23
|
+
}
|
|
24
|
+
return sendTransaction({ transaction, from, sendParams }, algod);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Funds a given account using a funding source such that it has a certain amount of algos free to spend (accounting for ALGOs locked in minimum balance requirement).
|
|
28
|
+
*
|
|
29
|
+
* https://developer.algorand.org/docs/get-details/accounts/#minimum-balance
|
|
30
|
+
*
|
|
31
|
+
* @param funding The funding configuration
|
|
32
|
+
* @param algod An algod client
|
|
33
|
+
* @param kmd An optional kmd client
|
|
34
|
+
* @returns undefined if nothing was needed or the transaction send result
|
|
35
|
+
*/
|
|
36
|
+
export async function ensureFunded(funding, algod, kmd) {
|
|
37
|
+
const { accountToFund, fundingSource, minSpendingBalance, minFundingIncrement, transactionParams, note, ...sendParams } = funding;
|
|
38
|
+
const addressToFund = typeof accountToFund === 'string' ? accountToFund : getSenderAddress(accountToFund);
|
|
39
|
+
const accountInfo = await algod.accountInformation(addressToFund).do();
|
|
40
|
+
const balance = Number(accountInfo.amount);
|
|
41
|
+
const minimumBalanceRequirement = microAlgos(Number(accountInfo['min-balance']));
|
|
42
|
+
const currentSpendingBalance = microAlgos(balance - minimumBalanceRequirement.microAlgos);
|
|
43
|
+
if (minSpendingBalance > currentSpendingBalance) {
|
|
44
|
+
const from = fundingSource ?? (await getDispenserAccount(algod, kmd));
|
|
45
|
+
const minFundAmount = microAlgos(minSpendingBalance.microAlgos - currentSpendingBalance.microAlgos);
|
|
46
|
+
const fundAmount = microAlgos(Math.max(minFundAmount.microAlgos, minFundingIncrement?.microAlgos ?? 0));
|
|
47
|
+
Config.getLogger(sendParams.suppressLog).info(`Funding ${addressToFund} ${fundAmount} from ${getSenderAddress(from)} to reach minimum spend amount of ${minSpendingBalance} (balance = ${balance}, min_balance_req = ${minimumBalanceRequirement})`);
|
|
48
|
+
return await transferAlgos({
|
|
49
|
+
from,
|
|
50
|
+
to: addressToFund,
|
|
51
|
+
note: note ?? 'Funding account to meet minimum requirement',
|
|
52
|
+
amount: fundAmount,
|
|
53
|
+
transactionParams,
|
|
54
|
+
...sendParams,
|
|
55
|
+
}, algod);
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=transfer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transfer.js","sourceRoot":"","sources":["../../src/transfer.ts"],"names":[],"mappings":"AAAA,OAAO,OAAyB,MAAM,SAAS,CAAA;AAC/C,OAAO,EAAE,MAAM,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,IAAI,CAAA;AAC5D,OAAO,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAA;AAI9G;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,QAA4B,EAAE,KAAc;IAC9E,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,iBAAiB,EAAE,GAAG,UAAU,EAAE,GAAG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