Skip to content

Commit

Permalink
fix: fix wasm build
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocaillard committed Jan 26, 2024
1 parent b3feafb commit fb794c1
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 119 deletions.
85 changes: 25 additions & 60 deletions Cargo.lock

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

32 changes: 19 additions & 13 deletions components/clarinet-sdk-wasm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,10 @@ pub struct SessionReport {
pub fn execution_result_to_transaction_res(execution: &ExecutionResult) -> TransactionRes {
let result = match &execution.result {
EvaluationResult::Snippet(result) => clarity_values::to_raw_value(&result.result),
EvaluationResult::Contract(ref contract) => {
// contract.;
match contract.result {
Some(ref result) => clarity_values::to_raw_value(result),
_ => "0x03".into(),
}
}
EvaluationResult::Contract(ref contract) => match contract.result {
Some(ref result) => clarity_values::to_raw_value(result),
_ => "0x03".into(),
},
};
let events_as_strings = execution
.events
Expand Down Expand Up @@ -698,14 +695,23 @@ impl SDK {
}

#[wasm_bindgen(js_name=runSnippet)]
pub fn run_snippet(&mut self, snippet: String) -> js_sys::Array {
pub fn run_snippet(&mut self, snippet: String) -> String {
let session = self.get_session_mut();
let (_, output) = session.handle_command(&snippet);
let output_as_array = js_sys::Array::new_with_length(output.len() as u32);
for string in output {
output_as_array.push(&JsValue::from_str(&string));
match session.eval(snippet.clone(), None, false) {
Ok(res) => match res.result {
EvaluationResult::Snippet(result) => clarity_values::to_raw_value(&result.result),
EvaluationResult::Contract(_) => unreachable!(
"Contract evaluation result should not be returned from eval_snippet",
),
},
Err(diagnostics) => {
let mut message = "error:".to_string();
diagnostics.iter().for_each(|d| {
message = format!("{message}\n{}", d.message);
});
message
}
}
output_as_array
}

#[wasm_bindgen(js_name=setCurrentTestName)]
Expand Down
43 changes: 28 additions & 15 deletions components/clarinet-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,23 @@ export type GetContractsInterfaces = () => Map<string, ContractInterface>;
export type Simnet = {
[K in keyof SDK]: K extends "callReadOnlyFn" | "callPublicFn"
? CallFn
: K extends "deployContract"
? DeployContract
: K extends "transferSTX"
? TransferSTX
: K extends "mineBlock"
? MineBlock
: K extends "getDataVar"
? GetDataVar
: K extends "getMapEntry"
? GetMapEntry
: K extends "getContractAST"
? GetContractAST
: K extends "getContractsInterfaces"
? GetContractsInterfaces
: SDK[K];
: K extends "runSnippet"
? ClarityValue
: K extends "deployContract"
? DeployContract
: K extends "transferSTX"
? TransferSTX
: K extends "mineBlock"
? MineBlock
: K extends "getDataVar"
? GetDataVar
: K extends "getMapEntry"
? GetMapEntry
: K extends "getContractAST"
? GetContractAST
: K extends "getContractsInterfaces"
? GetContractsInterfaces
: SDK[K];
};

function parseEvents(events: string): ClarityEvent[] {
Expand Down Expand Up @@ -171,6 +173,17 @@ const getSessionProxy = () => ({
return callFn;
}

if (prop === "runSnippet") {
return (snippet: string) => {
const response = session[prop](snippet);
if (response.startsWith("0x")) {
return Cl.deserialize(response);
} else {
return response;
}
};
}

if (prop === "deployContract") {
const callDeployContract: DeployContract = (name, content, options, sender) => {
const rustOptions = options
Expand Down
67 changes: 40 additions & 27 deletions components/clarinet-sdk/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,53 @@ describe("basic simnet interactions", async () => {
it("initialize simnet", async () => {
expect(simnet.blockHeight).toBe(1);
});
});

it("can mine empty blocks", async () => {
simnet.mineEmptyBlock();
expect(simnet.blockHeight).toBe(2);
simnet.mineEmptyBlocks(4);
expect(simnet.blockHeight).toBe(6);
});
it("can mine empty blocks", async () => {
simnet.mineEmptyBlock();
expect(simnet.blockHeight).toBe(2);
simnet.mineEmptyBlocks(4);
expect(simnet.blockHeight).toBe(6);
});

it("exposes devnet stacks accounts", async () => {
const accounts = simnet.getAccounts();
it("exposes devnet stacks accounts", async () => {
const accounts = simnet.getAccounts();

expect(accounts).toHaveLength(4);
expect(accounts.get("deployer")).toBe(deployerAddr);
expect(accounts.get("wallet_1")).toBe(address1);
});
expect(accounts).toHaveLength(4);
expect(accounts.get("deployer")).toBe(deployerAddr);
expect(accounts.get("wallet_1")).toBe(address1);
});

it("expose assets maps", async () => {
const assets = simnet.getAssetsMap();
expect(assets.get("STX")).toHaveLength(4);
expect(assets.get("STX")?.get(address1)).toBe(100000000000000n);
});
it("expose assets maps", async () => {
const assets = simnet.getAssetsMap();
expect(assets.get("STX")).toHaveLength(4);
expect(assets.get("STX")?.get(address1)).toBe(100000000000000n);
});

it("can get and set epoch", async () => {
// should be 2.4 by default
expect(simnet.currentEpoch).toBe("2.4");
it("can get and set epoch", async () => {
// should be 2.4 by default
expect(simnet.currentEpoch).toBe("2.4");

simnet.setEpoch("2.0");
expect(simnet.currentEpoch).toBe("2.0");
simnet.setEpoch("2.0");
expect(simnet.currentEpoch).toBe("2.0");

// @ts-ignore
// "0" is an invalid epoch
// it logs that 0 is invalid and defaults to 2.4
simnet.setEpoch("0");
expect(simnet.currentEpoch).toBe("2.4");
});

describe("simnet can run arbitrary snippets", async () => {
it("can run simple snippets", () => {
const res = simnet.runSnippet("(+ 1 2)");
expect(res).toStrictEqual(Cl.int(3));
});

// @ts-ignore
// "0" is an invalid epoch
// it logs that 0 is invalid and defaults to 2.4
simnet.setEpoch("0");
expect(simnet.currentEpoch).toBe("2.4");
it("show diagnostic in case of error", () => {
const res = simnet.runSnippet("(+ 1 u2)");
console.log("res", res);
expect(res).toBe("error:\nexpecting expression of type 'int', found 'uint'");
});
});

Expand Down
6 changes: 4 additions & 2 deletions components/clarity-repl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ serde_derive = "1.0"
integer-sqrt = "0.1.3"
getrandom = { version = "0.2.3", features = ["js"] }
atty = "0.2.14"
wsts = "5.0.0"
# clarity-vm = "2"
clarity = { version = "2", default-features = false, optional = true, git = "https://github.com/stacks-network/stacks-core.git", branch = "feat/clarity-wasm-next", package = "clarity" }
# clarity = { version = "2", default-features = false, optional = true, path ="../../../clarity-wasm/stacks-blockchain/clarity" }
# clarity = { version = "2", default-features = false, optional = true, path ="../../../clarity-wasm/stacks-core/clarity" }
clar2wasm = { git = "https://github.com/stacks-network/clarity-wasm.git", optional = true }
# clar2wasm = { path="../../../clarity-wasm/clar2wasm", optional = true }

# DAP Debugger
tokio = { version = "1.35.1", features = ["full"], optional = true }
Expand All @@ -57,6 +57,7 @@ reqwest = { version = "0.11", default-features = false, features = [
"json",
"rustls-tls",
] }
wsts = { path= "../../../wsts", optional = true }

# WASM
wasm-bindgen = { version = "0.2", optional = true }
Expand Down Expand Up @@ -85,6 +86,7 @@ cli = [
"clarity/log",
"hiro_system_kit/tokio_helpers",
"clar2wasm",
"wsts"
]
sdk = [
"clarity/canonical",
Expand Down
1 change: 1 addition & 0 deletions components/clarity-repl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern crate hiro_system_kit;
mod macros;

pub mod analysis;
#[cfg(feature = "cli")]
pub mod codec;
pub mod repl;
pub mod utils;
Expand Down
Loading

0 comments on commit fb794c1

Please sign in to comment.