@d13co/escreg-sdk 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.increaseBudgetIncrementCost = exports.increaseBudgetBaseCost = exports.fnetNodelyClient = exports.emptySigner = void 0;
4
+ exports.creditBoxRef = creditBoxRef;
5
+ exports.chunk = chunk;
6
+ exports.getIncreaseBudgetBuilder = getIncreaseBudgetBuilder;
7
+ const algosdk_1 = require("algosdk");
8
+ const algokit_utils_1 = require("@algorandfoundation/algokit-utils");
9
+ exports.emptySigner = (0, algosdk_1.makeEmptyTransactionSigner)();
10
+ exports.fnetNodelyClient = algokit_utils_1.AlgorandClient.fromConfig({
11
+ algodConfig: {
12
+ server: "https://fnet-api.4160.nodely.dev",
13
+ port: 443,
14
+ },
15
+ indexerConfig: {
16
+ server: "https://fnet-idx.4160.nodely.dev",
17
+ port: 443,
18
+ },
19
+ });
20
+ /** Prepend the 'c' key prefix to a public key for the userCredits box */
21
+ function creditBoxRef(publicKey) {
22
+ const ref = new Uint8Array(1 + publicKey.length);
23
+ ref[0] = 0x63; // 'c'
24
+ ref.set(publicKey, 1);
25
+ return ref;
26
+ }
27
+ function chunk(array, size) {
28
+ if (size <= 0)
29
+ throw new Error("Chunk size must be greater than 0");
30
+ const result = [];
31
+ for (let i = 0; i < array.length; i += size) {
32
+ result.push(array.slice(i, i + size));
33
+ }
34
+ return result;
35
+ }
36
+ // sync with "increaseBudget opcode cost" contract tests
37
+ exports.increaseBudgetBaseCost = 26;
38
+ exports.increaseBudgetIncrementCost = 22;
39
+ const SIMULATE_PARAMS = {
40
+ allowMoreLogging: true,
41
+ allowUnnamedResources: true,
42
+ extraOpcodeBudget: 130013,
43
+ fixSigners: true,
44
+ allowEmptySignatures: true,
45
+ };
46
+ const simulateRequest = new algosdk_1.modelsv2.SimulateRequest({
47
+ txnGroups: [],
48
+ ...SIMULATE_PARAMS,
49
+ });
50
+ /* Utility to increase the budget of a transaction group if needed.
51
+ * Simulates and returns undefined if we are under budget, otherwise returns a new builder with an increaseBudget call prepended.
52
+ */
53
+ async function getIncreaseBudgetBuilder(builder, newBuilderFactory, sender, signer, algod) {
54
+ // maxFee/coverAppCallInnerTransactionFees does not work with builder.simulate() #algokit
55
+ // increase first txn's fee so we do not fail because of fees
56
+ // get atc & modify the first txn fee (need to clone to make txns mutable)
57
+ const atc = (await (await builder.composer()).build()).atc.clone();
58
+ // @ts-ignore private and readonly
59
+ atc.transactions[0].txn.fee = 543210n;
60
+ // we also need to replace signers with empty signers for simulation
61
+ // otherwise end users would be prompted to sign for this
62
+ // @ts-ignore private and readonly
63
+ atc.transactions = atc.transactions.map((t) => {
64
+ t.signer = (0, algosdk_1.makeEmptyTransactionSigner)();
65
+ return t;
66
+ });
67
+ const { simulateResponse: { txnGroups: [{ txnResults, appBudgetConsumed = 0 }], }, } = await atc.simulate(algod, simulateRequest);
68
+ // intentionally doing opup even if there is a failure
69
+ // we had code here to return early if there was a failureMessage
70
+ // but that meant that in some cases the actual failure would be obscured by out of budget errors
71
+ // get existing budget: count app calls
72
+ // NOTE only goes 1 level deep in itxns
73
+ const numAppCalls = txnResults.reduce((sum, { txnResult }) => {
74
+ if (txnResult?.txn.txn.type !== "appl")
75
+ return sum;
76
+ const innerTxns = txnResult.innerTxns ?? [];
77
+ return sum + 1 + innerTxns.length;
78
+ }, 0);
79
+ let existingBudget = 700 * numAppCalls;
80
+ // budget is OK, returning
81
+ if (appBudgetConsumed <= existingBudget)
82
+ return;
83
+ existingBudget += 700 - exports.increaseBudgetBaseCost; // add 700 for increaseBudget, removing its base cost
84
+ const itxnBudgetNeeded = appBudgetConsumed - existingBudget; // budget to create in itxns
85
+ const itxns = Math.max(0, Math.ceil(itxnBudgetNeeded / (700 - exports.increaseBudgetIncrementCost)));
86
+ const increaseBudgetArgs = {
87
+ args: { itxns },
88
+ extraFee: (itxns * 1000).microAlgo(),
89
+ maxFee: ((itxns + 1) * 1000).microAlgo(),
90
+ note: Math.floor(Math.random() * 100000000).toString(),
91
+ sender,
92
+ signer,
93
+ };
94
+ return newBuilderFactory().increaseBudget(increaseBudgetArgs);
95
+ }
96
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":";;;AAmBA,oCAKC;AAED,sBAUC;AAsBD,4DA4DC;AAtHD,qCAA2F;AAG3F,qEAAmE;AAEtD,QAAA,WAAW,GAAG,IAAA,oCAA0B,GAAE,CAAC;AAE3C,QAAA,gBAAgB,GAAG,8BAAc,CAAC,UAAU,CAAC;IACxD,WAAW,EAAE;QACX,MAAM,EAAE,kCAAkC;QAC1C,IAAI,EAAE,GAAG;KACV;IACD,aAAa,EAAE;QACb,MAAM,EAAE,kCAAkC;QAC1C,IAAI,EAAE,GAAG;KACV;CACF,CAAC,CAAC;AAEH,yEAAyE;AACzE,SAAgB,YAAY,CAAC,SAAqB;IAChD,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,CAAC,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;IACjD,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,MAAM;IACrB,GAAG,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;IACtB,OAAO,GAAG,CAAC;AACb,CAAC;AAED,SAAgB,KAAK,CAAI,KAAU,EAAE,IAAY;IAC/C,IAAI,IAAI,IAAI,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IAEpE,MAAM,MAAM,GAAU,EAAE,CAAC;IAEzB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC;QAC5C,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,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E,CAAC,cAAc,CAAC,kBAAkB,CAAC,CAAC;AAChE,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { ErrorTransformer } from "@algorandfoundation/algokit-utils/types/composer";
2
+ /**
3
+ * Map of error codes to human-readable error messages
4
+ */
5
+ export declare const errorMap: Record<string, string>;
6
+ export declare const errorTransformer: ErrorTransformer;
7
+ export declare function wrapErrorsInternal<T>(promiseOrGenerator: Promise<T> | (() => Promise<T>)): Promise<T>;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.errorTransformer = exports.errorMap = void 0;
4
+ exports.wrapErrorsInternal = wrapErrorsInternal;
5
+ const errors_1 = require("./generated/errors");
6
+ /**
7
+ * Map of error codes to human-readable error messages
8
+ */
9
+ exports.errorMap = errors_1.ErrorMessages;
10
+ const errorTransformer = async (ogError) => {
11
+ const [errCode] = /ERR:[^" ]+/.exec(ogError.message) ?? [];
12
+ if (errCode) {
13
+ const humanMessage = exports.errorMap[errCode] ?? "Unknown error";
14
+ const message = `${errCode.replace("ERR:", "Error ")}: ${humanMessage}`;
15
+ ogError.stack = `${message}\n ${ogError.message}\n${ogError.stack}`;
16
+ ogError.message = message;
17
+ ogError.code = errCode;
18
+ ogError.description = humanMessage;
19
+ return ogError;
20
+ }
21
+ return ogError;
22
+ };
23
+ exports.errorTransformer = errorTransformer;
24
+ async function wrapErrorsInternal(promiseOrGenerator) {
25
+ try {
26
+ if (typeof promiseOrGenerator === "function") {
27
+ return await promiseOrGenerator();
28
+ }
29
+ else {
30
+ return await promiseOrGenerator;
31
+ }
32
+ }
33
+ catch (e) {
34
+ throw await (0, exports.errorTransformer)(e);
35
+ }
36
+ }
37
+ //# sourceMappingURL=wrapErrors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wrapErrors.js","sourceRoot":"","sources":["../../src/wrapErrors.ts"],"names":[],"mappings":";;;AAuBA,gDAUC;AAhCD,+CAAmD;AAEnD;;GAEG;AACU,Q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