@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,90 @@
1
+ import { makeEmptyTransactionSigner, modelsv2 } from "algosdk";
2
+ import { AlgorandClient } from "@algorandfoundation/algokit-utils";
3
+ export const emptySigner = makeEmptyTransactionSigner();
4
+ export const fnetNodelyClient = AlgorandClient.fromConfig({
5
+ algodConfig: {
6
+ server: "https://fnet-api.4160.nodely.dev",
7
+ port: 443,
8
+ },
9
+ indexerConfig: {
10
+ server: "https://fnet-idx.4160.nodely.dev",
11
+ port: 443,
12
+ },
13
+ });
14
+ /** Prepend the 'c' key prefix to a public key for the userCredits box */
15
+ export function creditBoxRef(publicKey) {
16
+ const ref = new Uint8Array(1 + publicKey.length);
17
+ ref[0] = 0x63; // 'c'
18
+ ref.set(publicKey, 1);
19
+ return ref;
20
+ }
21
+ export function chunk(array, size) {
22
+ if (size <= 0)
23
+ throw new Error("Chunk size must be greater than 0");
24
+ const result = [];
25
+ for (let i = 0; i < array.length; i += size) {
26
+ result.push(array.slice(i, i + size));
27
+ }
28
+ return result;
29
+ }
30
+ // sync with "increaseBudget opcode cost" contract tests
31
+ export const increaseBudgetBaseCost = 26;
32
+ export const increaseBudgetIncrementCost = 22;
33
+ const SIMULATE_PARAMS = {
34
+ allowMoreLogging: true,
35
+ allowUnnamedResources: true,
36
+ extraOpcodeBudget: 130013,
37
+ fixSigners: true,
38
+ allowEmptySignatures: true,
39
+ };
40
+ const simulateRequest = new modelsv2.SimulateRequest({
41
+ txnGroups: [],
42
+ ...SIMULATE_PARAMS,
43
+ });
44
+ /* Utility to increase the budget of a transaction group if needed.
45
+ * Simulates and returns undefined if we are under budget, otherwise returns a new builder with an increaseBudget call prepended.
46
+ */
47
+ export async function getIncreaseBudgetBuilder(builder, newBuilderFactory, sender, signer, algod) {
48
+ // maxFee/coverAppCallInnerTransactionFees does not work with builder.simulate() #algokit
49
+ // increase first txn's fee so we do not fail because of fees
50
+ // get atc & modify the first txn fee (need to clone to make txns mutable)
51
+ const atc = (await (await builder.composer()).build()).atc.clone();
52
+ // @ts-ignore private and readonly
53
+ atc.transactions[0].txn.fee = 543210n;
54
+ // we also need to replace signers with empty signers for simulation
55
+ // otherwise end users would be prompted to sign for this
56
+ // @ts-ignore private and readonly
57
+ atc.transactions = atc.transactions.map((t) => {
58
+ t.signer = makeEmptyTransactionSigner();
59
+ return t;
60
+ });
61
+ const { simulateResponse: { txnGroups: [{ txnResults, appBudgetConsumed = 0 }], }, } = await atc.simulate(algod, simulateRequest);
62
+ // intentionally doing opup even if there is a failure
63
+ // we had code here to return early if there was a failureMessage
64
+ // but that meant that in some cases the actual failure would be obscured by out of budget errors
65
+ // get existing budget: count app calls
66
+ // NOTE only goes 1 level deep in itxns
67
+ const numAppCalls = txnResults.reduce((sum, { txnResult }) => {
68
+ if (txnResult?.txn.txn.type !== "appl")
69
+ return sum;
70
+ const innerTxns = txnResult.innerTxns ?? [];
71
+ return sum + 1 + innerTxns.length;
72
+ }, 0);
73
+ let existingBudget = 700 * numAppCalls;
74
+ // budget is OK, returning
75
+ if (appBudgetConsumed <= existingBudget)
76
+ return;
77
+ existingBudget += 700 - increaseBudgetBaseCost; // add 700 for increaseBudget, removing its base cost
78
+ const itxnBudgetNeeded = appBudgetConsumed - existingBudget; // budget to create in itxns
79
+ const itxns = Math.max(0, Math.ceil(itxnBudgetNeeded / (700 - increaseBudgetIncrementCost)));
80
+ const increaseBudgetArgs = {
81
+ args: { itxns },
82
+ extraFee: (itxns * 1000).microAlgo(),
83
+ maxFee: ((itxns + 1) * 1000).microAlgo(),
84
+ note: Math.floor(Math.random() * 100000000).toString(),
85
+ sender,
86
+ signer,
87
+ };
88
+ return newBuilderFactory().increaseBudget(increaseBudgetArgs);
89
+ }
90
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/util.ts"],"names":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
+ 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,32 @@
1
+ import { ErrorMessages } from "./generated/errors";
2
+ /**
3
+ * Map of error codes to human-readable error messages
4
+ */
5
+ export const errorMap = ErrorMessages;
6
+ export const errorTransformer = async (ogError) => {
7
+ const [errCode] = /ERR:[^" ]+/.exec(ogError.message) ?? [];
8
+ if (errCode) {
9
+ const humanMessage = errorMap[errCode] ?? "Unknown error";
10
+ const message = `${errCode.replace("ERR:", "Error ")}: ${humanMessage}`;
11
+ ogError.stack = `${message}\n ${ogError.message}\n${ogError.stack}`;
12
+ ogError.message = message;
13
+ ogError.code = errCode;
14
+ ogError.description = humanMessage;
15
+ return ogError;
16
+ }
17
+ return ogError;
18
+ };
19
+ export async function wrapErrorsInternal(promiseOrGenerator) {
20
+ try {
21
+ if (typeof promiseOrGenerator === "function") {
22
+ return await promiseOrGenerator();
23
+ }
24
+ else {
25
+ return await promiseOrGenerator;
26
+ }
27
+ }
28
+ catch (e) {
29
+ throw await errorTransformer(e);
30
+ }
31
+ }
32
+ //# sourceMappingURL=wrapErrors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wrapErrors.js","sourceRoot":"","sources":["../../src/wrapErrors.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD;;GAEG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,aAAa,CAAC;AAEtC,MAAM,CAAC,MAAM,gBAAgB,GAAqB,KAAK,EAAE,OAAO,EAAE,EAAE;IAClE,MAAM,CAAC,OAAO,CAAC,GAAG,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;IAC3D,IAAI,OAAO,EAAE,CAAC;QACZ,MAAM,YAAY,GAAG,QAAQ,CAAC,OAAO,CAAC,IAAI,eAAe,CAAC;QAC1D,MAAM,OAAO,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC,KAAK,YAAY,EAAE,CAAA;QAEvE,OAAO,CAAC,KAAK,GAAG,GAAG,OAAO,SAAS,OAAO,CAAC,OAAO,KAAK,OAAO,CAAC,KAAK,EAAE,CAAC;QACvE,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC;QACzB,OAAe,CAAC,IAAI,GAAG,OAAO,CAAC;QAC/B,OAAe,CAAC,WAAW,GAAG,YAAY,CAAC;QAC5C,OAAO,OAAO,CAAC;IACjB,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC;AAEF,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAI,kBAAmD;IAC7F,IAAI,CAAC;QACH,IAAI,OAAO,kBAAkB,KAAK,UAAU,EAAE,CAAC;YAC7C,OAAO,MAAM,kBAAkB,EAAE,CAAC;QACpC,CAAC;aAAM,CAAC;YACN,OAAO,MAAM,kBAAkB,CAAC;QAClC,CAAC;IACH,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,MAAM,MAAM,gBAAgB,CAAC,CAAU,CAAC,CAAC;IAC3C,CAAC;AACH,CAAC"}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@d13co/escreg-sdk",
3
+ "version": "0.0.1",
4
+ "main": "dist/cjs/index.js",
5
+ "module": "dist/esm/index.js",
6
+ "types": "dist/esm/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "require": "./dist/cjs/index.js",
10
+ "import": "./dist/esm/index.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist/",
15
+ "README.md"
16
+ ],
17
+ "scripts": {
18
+ "clean": "rm -rf dist/ src/generated/",
19
+ "prebuild": "npm run clean && npm run generate && npm run generate:errors",
20
+ "generate": "npx @algorandfoundation/algokit-client-generator generate -m minimal -o src/generated/EscregGenerated.ts -a ../contract/smart_contracts/artifacts/escreg/Escreg.arc56.json",
21
+ "generate:errors": "tsx scripts/generate-errors.ts",
22
+ "build:cjs": "tsc --project tsconfig.cjs.json",
23
+ "build:esm": "tsc --project tsconfig.esm.json",
24
+ "build": "concurrently 'npm run build:cjs' 'npm run build:esm'"
25
+ },
26
+ "author": "Tasos Bitsios",
27
+ "license": "ISC",
28
+ "description": "TypeScript SDK for the Escreg on-chain escrow registry on Algorand",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/d13co/escreg.git",
32
+ "directory": "projects/ts-sdk"
33
+ },
34
+ "homepage": "https://github.com/d13co/escreg#sdk",
35
+ "bugs": {
36
+ "url": "https://github.com/d13co/escreg/issues"
37
+ },
38
+ "keywords": [
39
+ "algorand",
40
+ "escrow",
41
+ "registry",
42
+ "sdk"
43
+ ],
44
+ "devDependencies": {
45
+ "@algorandfoundation/algokit-client-generator": "^6.0.0",
46
+ "@types/node": "^24.0.14",
47
+ "concurrently": "^9.2.0",
48
+ "tsx": "^4.21.0",
49
+ "typescript": "^5.8.3"
50
+ },
51
+ "peerDependencies": {
52
+ "@algorandfoundation/algokit-utils": "^9.0.0",
53
+ "algosdk": "^3.0.0"
54
+ },
55
+ "dependencies": {
56
+ "p-map": "^7.0.3"
57
+ }
58
+ }