Skip to content

Commit

Permalink
feat: initialize clarinet-sdk with the test-core js lib
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocaillard committed Jul 13, 2023
1 parent 1ff5c13 commit 69d6449
Show file tree
Hide file tree
Showing 17 changed files with 1,994 additions and 7 deletions.
32 changes: 30 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"components/clarinet-deployments",
"components/clarinet-files",
"components/clarinet-utils",
"components/clarinet-sdk",
"components/clarity-jupyter-kernel",
"components/clarity-lsp",
"components/clarity-repl",
Expand Down
8 changes: 4 additions & 4 deletions components/clarinet-files/src/wasm_fs_accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ struct WFSWriteRequest<'a> {
}

pub struct WASMFileSystemAccessor {
client_request_tx: JsFunction,
client_request: JsFunction,
}

impl WASMFileSystemAccessor {
pub fn new(client_request_tx: JsFunction) -> WASMFileSystemAccessor {
WASMFileSystemAccessor { client_request_tx }
pub fn new(client_request: JsFunction) -> WASMFileSystemAccessor {
WASMFileSystemAccessor { client_request }
}

fn get_request_promise<T: Serialize>(
&self,
action: String,
data: &T,
) -> FileAccessorResult<JsValue> {
let request_promise = self.client_request_tx.call2(
let request_promise = self.client_request.call2(
&JsValue::NULL,
&JsValue::from(action),
&encode_to_js(data).unwrap(),
Expand Down
3 changes: 3 additions & 0 deletions components/clarinet-sdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
pkg/
node_modules/
77 changes: 77 additions & 0 deletions components/clarinet-sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Clarinet SDK

## Core testing lib

This a very early preview.
Expect many breaking changes in the API.

```js
// @ts-check

import { main } from "obscurity-sdk";
import { before, describe, it } from "node:test";
import assert from "node:assert/strict";
import { Cl } from "@stacks/transactions";

describe("test counter", () => {
const contract = "counter";
const cost = 1000000n;
let deployer;
let contract_addr;
let sender;
/** @type import("obscurity-sdk").Session */
let session;

before(async () => {
session = await main();
await session.initSession(process.cwd(), "./Clarinet.toml");

const accounts = session.getAccounts();
deployer = accounts.get("deployer");
contract_addr = `${deployer}.${contract}`;
sender = accounts.get("wallet_1");
});

it("gets counter value", () => {
const res = session.callReadOnlyFn(contract, "get-counter", [], sender);

assert.deepEqual(res.result, Cl.int(0));
assert.equal(session.blockHeight, 1);
});

it("increments counter value", () => {
const stxBefore = session.getAssetsMap().get("STX");

const res = session.callPublicFn(contract, "increment", [], sender);

assert.equal(res.events.length, 2);
const printEvent = res.events[0];
assert.equal(printEvent.event, "print_event");
const stxTransferEvent = res.events[1];
assert.equal(stxTransferEvent.event, "stx_transfer_event");
assert.equal(stxTransferEvent.data.amount, cost.toString());

assert.deepEqual(res.result, Cl.ok(Cl.bool(true)));
assert.equal(session.blockHeight, 2);

let counter = session.callReadOnlyFn(contract, "get-counter", [], sender);
assert.deepEqual(counter.result, Cl.int(1));

const stxAfter = session.getAssetsMap().get("STX");
// @ts-ignore
assert.equal(stxAfter?.get(sender), stxBefore?.get(sender) - cost);
// @ts-ignore
assert.equal(stxAfter?.get(contract_addr), cost);
});

it("add any value", () => {
const addRes = session.callPublicFn(contract, "add", [Cl.int(10)], sender);
assert.deepEqual(addRes.result, Cl.ok(Cl.bool(true)));
assert.equal(session.blockHeight, 3);

let res = session.callReadOnlyFn(contract, "get-counter", [], sender);
assert.deepEqual(res.result, Cl.int(11));
assert.equal(session.blockHeight, 3);
});
});
```
65 changes: 65 additions & 0 deletions components/clarinet-sdk/cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[package]
edition = "2021"
name = "clarinet-sdk"
version = "0.0.1"

[lib]
crate-type = ["cdylib"]
name = "clarinet_sdk"
path = "src/lib.rs"

[dependencies]
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0"
clarinet-files = { path = "../clarinet-files", default-features = false }
clarity-repl = { path = "../clarity-repl", default-features = false, optional = true }
clarinet-deployments = { path = "../clarinet-deployments", default-features = false }

# WASM
console_error_panic_hook = { version = "0.1", optional = true }
js-sys = { version = "0.3", optional = true }
serde-wasm-bindgen = { version = "0.5", optional = true }
wasm-bindgen = { version = "0.2", optional = true }
wasm-bindgen-futures = { version = "0.4", optional = true }
web-sys = { version = "0.3", features = ["console"], optional = true }

[features]
default = ["wasm"]
wasm = [
"wasm-bindgen",
"wasm-bindgen-futures",
"serde-wasm-bindgen",
"js-sys",
"web-sys",
"console_error_panic_hook",
"clarinet-deployments/wasm",
"clarity-repl/wasm",
"clarinet-files/wasm",
]

[package.metadata.wasm-pack.profile.dev]
wasm-opt = ['-O1']

[package.metadata.wasm-pack.profile.dev.wasm-bindgen]
debug-js-glue = true
demangle-name-section = true
dwarf-debug-info = false

[package.metadata.wasm-pack.profile.profiling]
wasm-opt = ['-O']

[package.metadata.wasm-pack.profile.profiling.wasm-bindgen]
debug-js-glue = false
demangle-name-section = true
dwarf-debug-info = false

[package.metadata.wasm-pack.profile.release]
# -04 aggressively optimizes for speed
wasm-opt = false
# -0z aggressively optimizes for size
# wasm-opt = ['-Oz']

[package.metadata.wasm-pack.profile.release.wasm-bindgen]
debug-js-glue = false
demangle-name-section = true
dwarf-debug-info = false
13 changes: 13 additions & 0 deletions components/clarinet-sdk/clarinet-sdk.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"folders": [{ "path": "../../" }],
"settings": {
"rust-analyzer.check.overrideCommand": [
"cargo",
"check",
"--no-default-features",
"--package=clarinet-sdk",
"--features=wasm",
"--message-format=json"
]
}
}
Loading

0 comments on commit 69d6449

Please sign in to comment.