@stacks/clarinet-sdk-browser 3.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -0
- package/dist/esm/browser/src/defaultVfs.d.ts +4 -0
- package/dist/esm/browser/src/defaultVfs.js +63 -0
- package/dist/esm/browser/src/defaultVfs.js.map +1 -0
- package/dist/esm/browser/src/index.d.ts +6 -0
- package/dist/esm/browser/src/index.js +19 -0
- package/dist/esm/browser/src/index.js.map +1 -0
- package/dist/esm/browser/src/sdkProxy.d.ts +11 -0
- package/dist/esm/browser/src/sdkProxy.js +95 -0
- package/dist/esm/browser/src/sdkProxy.js.map +1 -0
- package/dist/esm/browser/tsconfig.tsbuildinfo +1 -0
- package/dist/esm/common/src/sdkProxyHelpers.d.ts +86 -0
- package/dist/esm/common/src/sdkProxyHelpers.js +61 -0
- package/dist/esm/common/src/sdkProxyHelpers.js.map +1 -0
- package/package.json +34 -0
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Clarinet SDK for the Web
|
|
2
|
+
|
|
3
|
+
The Clarinet SDK can be used to interact with the simnet from web browsers.
|
|
4
|
+
|
|
5
|
+
If you want to use the Clarinet SDK in Node.js, try [@stacks/clarinet-sdk](https://www.npmjs.com/package/@stacks/clarinet-sdk).
|
|
6
|
+
|
|
7
|
+
Find the API references of the SDK in [our documentation](https://docs.stacks.co/reference/clarinet-js-sdk/overview).
|
|
8
|
+
|
|
9
|
+
You can use this SDK to:
|
|
10
|
+
|
|
11
|
+
- Interact with a clarinet project as you would with the Clarinet CLI
|
|
12
|
+
- Call public, read-only, and private functions from smart contracts
|
|
13
|
+
- Get clarity maps or data-var values
|
|
14
|
+
- Get contract interfaces (available functions and data)
|
|
15
|
+
- Write unit tests for Clarity smart contracts
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm install @stacks/clarinet-sdk-browser
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Usage
|
|
24
|
+
|
|
25
|
+
There are two ways to use the sdk in the browser:
|
|
26
|
+
|
|
27
|
+
- With an empty clarinet session:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const simnet = await initSimnet();
|
|
31
|
+
await simnet.initEmptySession();
|
|
32
|
+
simnet.runSnippet("(+ 1 2)");
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- With a clarinet project (ie: with a Clarinet.toml)
|
|
36
|
+
💡 It requires to use a virtual file system. More documentation and examples soon.
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
const simnet = await initSimnet();
|
|
40
|
+
await simnet.initSession("/project", "Clarinet.toml");
|
|
41
|
+
```
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export const defaultFileStore = new Map();
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
globalThis.fsStore = defaultFileStore;
|
|
4
|
+
function fileArrayToString(bufferArray) {
|
|
5
|
+
return Array.from(bufferArray)
|
|
6
|
+
.map((item) => String.fromCharCode(item))
|
|
7
|
+
.join("");
|
|
8
|
+
}
|
|
9
|
+
function isValidReadEvent(e) {
|
|
10
|
+
return typeof e?.path === "string";
|
|
11
|
+
}
|
|
12
|
+
function isValidReadManyEvent(e) {
|
|
13
|
+
return Array.isArray(e?.paths) && e.paths.every((s) => typeof s === "string");
|
|
14
|
+
}
|
|
15
|
+
function isValidWriteEvent(e) {
|
|
16
|
+
return (typeof e?.path === "string" && (Array.isArray(e?.content) || e?.content instanceof Uint8Array));
|
|
17
|
+
}
|
|
18
|
+
function exists(event) {
|
|
19
|
+
if (!isValidReadEvent(event))
|
|
20
|
+
throw new Error("invalid read event");
|
|
21
|
+
return defaultFileStore.has(event.path);
|
|
22
|
+
}
|
|
23
|
+
function readFile(event) {
|
|
24
|
+
if (!isValidReadEvent(event))
|
|
25
|
+
throw new Error("invalid read event");
|
|
26
|
+
const content = defaultFileStore.get(event.path) ?? null;
|
|
27
|
+
return content;
|
|
28
|
+
}
|
|
29
|
+
function readFiles(event) {
|
|
30
|
+
if (!isValidReadManyEvent(event))
|
|
31
|
+
throw new Error("invalid read event");
|
|
32
|
+
const files = event.paths.map((p) => {
|
|
33
|
+
try {
|
|
34
|
+
return defaultFileStore.get(p);
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
console.warn(err);
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return Object.fromEntries(files.reduce((acc, f, i) => {
|
|
42
|
+
if (f === null || f === undefined)
|
|
43
|
+
return acc;
|
|
44
|
+
return acc.concat([[event.paths[i], f]]);
|
|
45
|
+
}, []));
|
|
46
|
+
}
|
|
47
|
+
function writeFile(event) {
|
|
48
|
+
if (!isValidWriteEvent(event))
|
|
49
|
+
throw new Error("invalid write event");
|
|
50
|
+
return defaultFileStore.set(event.path, fileArrayToString(Uint8Array.from(event.content)));
|
|
51
|
+
}
|
|
52
|
+
export function defaultVfs(action, data) {
|
|
53
|
+
if (action === "vfs/exists")
|
|
54
|
+
return exists(data);
|
|
55
|
+
if (action === "vfs/readFile")
|
|
56
|
+
return readFile(data);
|
|
57
|
+
if (action === "vfs/readFiles")
|
|
58
|
+
return readFiles(data);
|
|
59
|
+
if (action === "vfs/writeFile")
|
|
60
|
+
return writeFile(data);
|
|
61
|
+
throw new Error("invalid vfs action");
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=defaultVfs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaultVfs.js","sourceRoot":"","sources":["../../../../src/defaultVfs.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,GAAG,EAAkB,CAAC;AAE1D,aAAa;AACb,UAAU,CAAC,OAAO,GAAG,gBAAgB,CAAC;AAEtC,SAAS,iBAAiB,CAAC,WAAuB;IAChD,OAAO,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC;SAC3B,GAA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MAAM,KAAK,eAAe;QAAE,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC;IACvD,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;AACxC,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import init, { SDK } from "@stacks/clarinet-sdk-wasm-browser";
|
|
2
|
+
import { Simnet, getSessionProxy } from "./sdkProxy.js";
|
|
3
|
+
export { tx, type ClarityEvent, type ParsedTransactionResult, type DeployContractOptions, type Tx, type TransferSTX, } from "../../common/src/sdkProxyHelpers.js";
|
|
4
|
+
export { init, SDK, getSessionProxy, type Simnet };
|
|
5
|
+
export { defaultVfs, defaultFileStore } from "./defaultVfs.js";
|
|
6
|
+
export declare const initSimnet: (virtualFileSystem?: Function) => Promise<Simnet>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import init, { SDK } from "@stacks/clarinet-sdk-wasm-browser";
|
|
2
|
+
import { getSessionProxy } from "./sdkProxy.js";
|
|
3
|
+
import { defaultVfs } from "./defaultVfs.js";
|
|
4
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
BigInt.prototype.toJSON = function () {
|
|
7
|
+
return this.toString();
|
|
8
|
+
};
|
|
9
|
+
export { tx, } from "../../common/src/sdkProxyHelpers.js";
|
|
10
|
+
export { init, SDK, getSessionProxy };
|
|
11
|
+
export { defaultVfs, defaultFileStore } from "./defaultVfs.js";
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
globalThis.vfs = defaultVfs;
|
|
14
|
+
export const initSimnet = async (virtualFileSystem) => {
|
|
15
|
+
await init();
|
|
16
|
+
const vfs = virtualFileSystem ? virtualFileSystem : defaultVfs;
|
|
17
|
+
return new Proxy(new SDK(vfs), getSessionProxy());
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,EAAE,EAAE,GAAG,EAAE,MAAM,mCAAmC,CAAC;AAE9D,OAAO,EAAU,eAAe,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,0GAA0G;AAC1G,aAAa;AACb,MAAM,CAAC,SAAS,CAAC,MAAM,GAAG;IACxB,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;AACzB,CAAC,CAAC;AAEF,OAAO,EACL,EAAE,GAMH,MAAM,qCAAqC,CAAC;AAE7C,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,eAAe,EAAe,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAE/D,aAAa;AACb,UAAU,CAAC,GAAG,GAAG,UAAU,CAAC;AAE5B,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAAE,iBAA4B,EAAE,EAAE;IAC/D,MAAM,IAAI,EAAE,CAAC;IACb,MAAM,GAAG,GAAG,iBAAiB,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,UAAU,CAAC;IAC/D,OAAO,IAAI,KAAK,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,EAAE,eAAe,EAAE,CAAsB,CAAC;AACzE,CAAC,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type SDK } from "@stacks/clarinet-sdk-wasm-browser";
|
|
2
|
+
import { type CallFn, type DeployContract, type GetDataVar, type GetMapEntry, type MineBlock, type Execute, type TransferSTX } from "../../common/src/sdkProxyHelpers.js";
|
|
3
|
+
/** @deprecated use `simnet.execute(command)` instead */
|
|
4
|
+
type RunSnippet = SDK["runSnippet"];
|
|
5
|
+
export type Simnet = {
|
|
6
|
+
[K in keyof SDK]: K extends "callReadOnlyFn" | "callPublicFn" | "callPrivateFn" ? CallFn : K extends "execute" ? Execute : K extends "runSnippet" ? RunSnippet : K extends "deployContract" ? DeployContract : K extends "transferSTX" ? TransferSTX : K extends "mineBlock" ? MineBlock : K extends "getDataVar" ? GetDataVar : K extends "getMapEntry" ? GetMapEntry : SDK[K];
|
|
7
|
+
};
|
|
8
|
+
export declare function getSessionProxy(): {
|
|
9
|
+
get(session: SDK, prop: keyof SDK, receiver: any): string | number | CallFn | DeployContract | TransferSTX | MineBlock | GetMapEntry | ((cwd: string, manifest_path: string) => Promise<void>) | ((epoch: import("@stacks/clarinet-sdk-wasm-browser").EpochString) => void) | ((count?: number | null) => number) | ((addresses: string[]) => void) | ((recipient: string, amount: bigint) => string) | ((include_boot_contracts: boolean, boot_contracts_path: string) => import("@stacks/clarinet-sdk-wasm-browser").SessionReport);
|
|
10
|
+
};
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Cl, serializeCVBytes } from "@stacks/transactions";
|
|
2
|
+
import { CallFnArgs, DeployContractArgs, TransferSTXArgs, ContractOptions, } from "@stacks/clarinet-sdk-wasm-browser";
|
|
3
|
+
import { parseEvents, parseCosts, } from "../../common/src/sdkProxyHelpers.js";
|
|
4
|
+
function parseTxResponse(response) {
|
|
5
|
+
return {
|
|
6
|
+
result: Cl.deserialize(response.result),
|
|
7
|
+
events: parseEvents(response.events),
|
|
8
|
+
costs: parseCosts(response.costs),
|
|
9
|
+
performance: response.performance,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export function getSessionProxy() {
|
|
13
|
+
return {
|
|
14
|
+
get(session, prop, receiver) {
|
|
15
|
+
// some of the WASM methods are proxied here to:
|
|
16
|
+
// - serialize clarity values input argument
|
|
17
|
+
// - deserialize output into clarity values
|
|
18
|
+
if (prop === "callReadOnlyFn" || prop === "callPublicFn" || prop === "callPrivateFn") {
|
|
19
|
+
const callFn = (contract, method, args, sender) => {
|
|
20
|
+
const response = session[prop](new CallFnArgs(contract, method, args.map(serializeCVBytes), sender));
|
|
21
|
+
return parseTxResponse(response);
|
|
22
|
+
};
|
|
23
|
+
return callFn;
|
|
24
|
+
}
|
|
25
|
+
if (prop === "execute") {
|
|
26
|
+
const execute = (snippet) => {
|
|
27
|
+
const response = session.execute(snippet);
|
|
28
|
+
return parseTxResponse(response);
|
|
29
|
+
};
|
|
30
|
+
return execute;
|
|
31
|
+
}
|
|
32
|
+
if (prop === "deployContract") {
|
|
33
|
+
const callDeployContract = (name, content, options, sender) => {
|
|
34
|
+
const rustOptions = options
|
|
35
|
+
? new ContractOptions(options.clarityVersion)
|
|
36
|
+
: new ContractOptions();
|
|
37
|
+
const response = session.deployContract(new DeployContractArgs(name, content, rustOptions, sender));
|
|
38
|
+
return parseTxResponse(response);
|
|
39
|
+
};
|
|
40
|
+
return callDeployContract;
|
|
41
|
+
}
|
|
42
|
+
if (prop === "transferSTX") {
|
|
43
|
+
const callTransferSTX = (amount, ...args) => {
|
|
44
|
+
const response = session.transferSTX(new TransferSTXArgs(BigInt(amount), ...args));
|
|
45
|
+
return parseTxResponse(response);
|
|
46
|
+
};
|
|
47
|
+
return callTransferSTX;
|
|
48
|
+
}
|
|
49
|
+
if (prop === "mineBlock") {
|
|
50
|
+
const callMineBlock = (txs) => {
|
|
51
|
+
const serializedTxs = txs.map((tx) => {
|
|
52
|
+
if (tx.callPublicFn) {
|
|
53
|
+
return {
|
|
54
|
+
callPublicFn: {
|
|
55
|
+
...tx.callPublicFn,
|
|
56
|
+
args_maps: tx.callPublicFn.args.map(serializeCVBytes),
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (tx.callPrivateFn) {
|
|
61
|
+
return {
|
|
62
|
+
callPrivateFn: {
|
|
63
|
+
...tx.callPrivateFn,
|
|
64
|
+
args_maps: tx.callPrivateFn.args.map(serializeCVBytes),
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
return tx;
|
|
69
|
+
});
|
|
70
|
+
const responses = session.mineBlock(serializedTxs);
|
|
71
|
+
return responses.map(parseTxResponse);
|
|
72
|
+
};
|
|
73
|
+
return callMineBlock;
|
|
74
|
+
}
|
|
75
|
+
if (prop === "getDataVar") {
|
|
76
|
+
const getDataVar = (...args) => {
|
|
77
|
+
const response = session.getDataVar(...args);
|
|
78
|
+
const result = Cl.deserialize(response);
|
|
79
|
+
return result;
|
|
80
|
+
};
|
|
81
|
+
return getDataVar;
|
|
82
|
+
}
|
|
83
|
+
if (prop === "getMapEntry") {
|
|
84
|
+
const getMapEntry = (contract, mapName, mapKey) => {
|
|
85
|
+
const response = session.getMapEntry(contract, mapName, serializeCVBytes(mapKey));
|
|
86
|
+
const result = Cl.deserialize(response);
|
|
87
|
+
return result;
|
|
88
|
+
};
|
|
89
|
+
return getMapEntry;
|
|
90
|
+
}
|
|
91
|
+
return Reflect.get(session, prop, receiver);
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=sdkProxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sdkProxy.js","sourceRoot":"","sources":["../../../../src/sdkProxy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EACL,UAAU,EACV,kBAAkB,EAClB,eAAe,EACf,eAAe,GAGhB,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EACL,WAAW,EASX,UAAU,GACX,MAAM,qCAAqC,CAAC;AA0B7C,SAAS,eAAe,CAAC,QAAwB;IAC/C,OAAO;QACL,MAAM,EAAE,EAAE,CAAC,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC;QACvC,MAAM,EAAE,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC;QACpC,KAAK,EAAE,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC;QACjC,WAAW,EAAE,QAAQ,CAAC,WAAW;KAClC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,eAA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OAAO,aAAa,CAAC;YACvB,CAAC;YAED,IAAI,IAAI,KAAK,YAAY,EAAE,CAAC;gBAC1B,MAAM,UAAU,GAAe,CAAC,GAAG,IAAI,EAAE,EAAE;oBACzC,MAAM,QAAQ,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC,CAAC;oBAC7C,MAAM,MAAM,GAAG,EAAE,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;oBACxC,OAAO,MAAM,CAAC;gBAChB,CAAC,CAAC;gBACF,OAAO,UAAU,CAAC;YACpB,CAAC;YAED,IAAI,IAAI,KAAK,aAAa,EAAE,CAAC;gBAC3B,MAAM,WAAW,GAAgB,CAAC,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE;oBAC7D,MAAM,QAAQ,GAAG,OAAO,CAAC,WAAW,CAAC,QAAQ,EAAE,OAAO,EAAE,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC;oBAClF,MAAM,MAAM,GAAG,EAAE,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;oBACxC,OAAO,MAAM,CAAC;gBAChB,CAAC,CAAC;gBACF,OAAO,WAAW,CAAC;YACrB,CAAC;YAED,OAAO,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;QAC9C,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../../../src/defaultvfs.ts","../../../src/index.ts","../../../src/sdkproxy.ts","../../../../common/src/sdkproxyhelpers.ts"],"version":"5.9.2"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { ClarityValue } from "@stacks/transactions";
|
|
2
|
+
export type ClarityEvent = {
|
|
3
|
+
event: string;
|
|
4
|
+
data: {
|
|
5
|
+
raw_value?: string;
|
|
6
|
+
value?: ClarityValue;
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export type ExecutionCost = {
|
|
11
|
+
writeLength: number;
|
|
12
|
+
writeCount: number;
|
|
13
|
+
readLength: number;
|
|
14
|
+
readCount: number;
|
|
15
|
+
runtime: number;
|
|
16
|
+
};
|
|
17
|
+
export type ClarityCosts = {
|
|
18
|
+
total: ExecutionCost;
|
|
19
|
+
limit: ExecutionCost;
|
|
20
|
+
memory: number;
|
|
21
|
+
memory_limit: number;
|
|
22
|
+
};
|
|
23
|
+
export type ParsedTransactionResult = {
|
|
24
|
+
result: ClarityValue;
|
|
25
|
+
events: ClarityEvent[];
|
|
26
|
+
costs: ClarityCosts | null;
|
|
27
|
+
performance: string | undefined;
|
|
28
|
+
};
|
|
29
|
+
export type CallFn = (contract: string, method: string, args: ClarityValue[], sender: string) => ParsedTransactionResult;
|
|
30
|
+
export type DeployContractOptions = {
|
|
31
|
+
clarityVersion: 1 | 2 | 3 | 4;
|
|
32
|
+
};
|
|
33
|
+
export type DeployContract = (name: string, content: string, options: DeployContractOptions | null, sender: string) => ParsedTransactionResult;
|
|
34
|
+
export type TransferSTX = (amount: number | bigint, recipient: string, sender: string) => ParsedTransactionResult;
|
|
35
|
+
export type Tx = {
|
|
36
|
+
callPublicFn: {
|
|
37
|
+
contract: string;
|
|
38
|
+
method: string;
|
|
39
|
+
args: ClarityValue[];
|
|
40
|
+
sender: string;
|
|
41
|
+
};
|
|
42
|
+
callPrivateFn?: never;
|
|
43
|
+
deployContract?: never;
|
|
44
|
+
transferSTX?: never;
|
|
45
|
+
} | {
|
|
46
|
+
callPublicFn?: never;
|
|
47
|
+
callPrivateFn: {
|
|
48
|
+
contract: string;
|
|
49
|
+
method: string;
|
|
50
|
+
args: ClarityValue[];
|
|
51
|
+
sender: string;
|
|
52
|
+
};
|
|
53
|
+
deployContract?: never;
|
|
54
|
+
transferSTX?: never;
|
|
55
|
+
} | {
|
|
56
|
+
callPublicFn?: never;
|
|
57
|
+
callPrivateFn?: never;
|
|
58
|
+
deployContract: {
|
|
59
|
+
name: string;
|
|
60
|
+
content: string;
|
|
61
|
+
options: DeployContractOptions | null;
|
|
62
|
+
sender: string;
|
|
63
|
+
};
|
|
64
|
+
transferSTX?: never;
|
|
65
|
+
} | {
|
|
66
|
+
callPublicFn?: never;
|
|
67
|
+
callPrivateFn?: never;
|
|
68
|
+
deployContradct?: never;
|
|
69
|
+
transferSTX: {
|
|
70
|
+
amount: number;
|
|
71
|
+
recipient: string;
|
|
72
|
+
sender: string;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
export declare const tx: {
|
|
76
|
+
callPublicFn: (contract: string, method: string, args: ClarityValue[], sender: string) => Tx;
|
|
77
|
+
callPrivateFn: (contract: string, method: string, args: ClarityValue[], sender: string) => Tx;
|
|
78
|
+
deployContract: (name: string, content: string, options: DeployContractOptions | null, sender: string) => Tx;
|
|
79
|
+
transferSTX: (amount: number, recipient: string, sender: string) => Tx;
|
|
80
|
+
};
|
|
81
|
+
export declare function parseEvents(events: string): ClarityEvent[];
|
|
82
|
+
export declare function parseCosts(costs: string): ClarityCosts | null;
|
|
83
|
+
export type MineBlock = (txs: Array<Tx>) => ParsedTransactionResult[];
|
|
84
|
+
export type Execute = (snippet: string) => ParsedTransactionResult;
|
|
85
|
+
export type GetDataVar = (contract: string, dataVar: string) => ClarityValue;
|
|
86
|
+
export type GetMapEntry = (contract: string, mapName: string, mapKey: ClarityValue) => ClarityValue;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Cl } from "@stacks/transactions";
|
|
2
|
+
export const tx = {
|
|
3
|
+
callPublicFn: (contract, method, args, sender) => ({
|
|
4
|
+
callPublicFn: { contract, method, args, sender },
|
|
5
|
+
}),
|
|
6
|
+
callPrivateFn: (contract, method, args, sender) => ({
|
|
7
|
+
callPrivateFn: { contract, method, args, sender },
|
|
8
|
+
}),
|
|
9
|
+
deployContract: (name, content, options, sender) => ({
|
|
10
|
+
deployContract: { name, content, options, sender },
|
|
11
|
+
}),
|
|
12
|
+
transferSTX: (amount, recipient, sender) => ({
|
|
13
|
+
transferSTX: { amount, recipient, sender },
|
|
14
|
+
}),
|
|
15
|
+
};
|
|
16
|
+
export function parseEvents(events) {
|
|
17
|
+
try {
|
|
18
|
+
// @todo: improve type safety
|
|
19
|
+
return JSON.parse(events).map((e) => {
|
|
20
|
+
const { event, data } = JSON.parse(e);
|
|
21
|
+
if ("raw_value" in data) {
|
|
22
|
+
data.value = Cl.deserialize(data.raw_value);
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
event: event,
|
|
26
|
+
data: data,
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
console.error(`Fail to parse events: ${e}`);
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export function parseCosts(costs) {
|
|
36
|
+
try {
|
|
37
|
+
let { memory, memory_limit, total, limit } = JSON.parse(costs);
|
|
38
|
+
return {
|
|
39
|
+
memory: memory,
|
|
40
|
+
memory_limit: memory_limit,
|
|
41
|
+
total: {
|
|
42
|
+
writeLength: total.write_length,
|
|
43
|
+
writeCount: total.write_count,
|
|
44
|
+
readLength: total.read_length,
|
|
45
|
+
readCount: total.read_count,
|
|
46
|
+
runtime: total.runtime,
|
|
47
|
+
},
|
|
48
|
+
limit: {
|
|
49
|
+
writeLength: limit.write_length,
|
|
50
|
+
writeCount: limit.write_count,
|
|
51
|
+
readLength: limit.read_length,
|
|
52
|
+
readCount: limit.read_count,
|
|
53
|
+
runtime: limit.runtime,
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
catch (_e) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=sdkProxyHelpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sdkProxyHelpers.js","sourceRoot":"","sources":["../../../../../common/src/sdkProxyHelpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,E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C,CAAC;IACL,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,KAAK,CAAC,yBAAyB,CAAC,EAAE,CAAC,CAAC;QAC5C,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,IAAI,CAAC;QACH,IAAI,EAAE,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC/D,OAAO;YACL,MAAM,EAAE,MAAM;YACd,YAAY,EAAE,YAAY;YAC1B,KAAK,EAAE;gBACL,WAAW,EAAE,KAAK,CAAC,YAAY;gBAC/B,UAAU,EAAE,KAAK,CAAC,WAAW;gBAC7B,UAAU,EAAE,KAAK,CAAC,WAAW;gBAC7B,SAAS,EAAE,KAAK,CAAC,UAAU;gBAC3B,OAAO,EAAE,KAAK,CAAC,OAAO;aACvB;YACD,KAAK,EAAE;gBACL,WAAW,EAAE,KAAK,CAAC,YAAY;gBAC/B,UAAU,EAAE,KAAK,CAAC,WAAW;gBAC7B,UAAU,EAAE,KAAK,CAAC,WAAW;gBAC7B,SAAS,EAAE,KAAK,CAAC,UAAU;gBAC3B,OAAO,EAAE,KAAK,CAAC,OAAO;aACvB;SACF,CAAC;IACJ,CAAC;IAAC,OAAO,EAAE,EAAE,CAAC;QACZ,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stacks/clarinet-sdk-browser",
|
|
3
|
+
"version": "3.8.1",
|
|
4
|
+
"description": "A SDK to interact with Clarity Smart Contracts in the browser",
|
|
5
|
+
"homepage": "https://stackslabs.com/clarinet",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/stx-labs/clarinet.git"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"module": "./dist/esm/browser/src/index.js",
|
|
14
|
+
"types": "./dist/esm/browser/src/index.d.ts",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"clean": "rimraf dist",
|
|
17
|
+
"compile": "tsc -b ./tsconfig.json",
|
|
18
|
+
"build": "npm run clean && npm run compile",
|
|
19
|
+
"prepare": "npm run build"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"stacks",
|
|
23
|
+
"clarity",
|
|
24
|
+
"clarinet",
|
|
25
|
+
"tests"
|
|
26
|
+
],
|
|
27
|
+
"author": "stacks-labs",
|
|
28
|
+
"license": "GPL-3.0",
|
|
29
|
+
"readme": "./README.md",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@stacks/clarinet-sdk-wasm-browser": "3.8.1",
|
|
32
|
+
"@stacks/transactions": "^7.0.6"
|
|
33
|
+
}
|
|
34
|
+
}
|