Skip to content

Commit

Permalink
refactor: improve sync-request setup
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocaillard committed Jan 22, 2025
1 parent 027b387 commit 25f095e
Show file tree
Hide file tree
Showing 11 changed files with 1,308 additions and 2,081 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion components/clarinet-sdk-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "clarinet-sdk-wasm"
# version.workspace = true
version = "2.13.0-beta1"
version = "2.13.0-beta3"
edition = "2021"
license = "GPL-3.0"
repository = "https://github.com/hirosystems/clarinet"
Expand Down
40 changes: 33 additions & 7 deletions components/clarinet-sdk-wasm/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ import path from "node:path";
const rootDir = new URL(".", import.meta.url).pathname;

/**
* build
* build sdk js script
*/
async function build() {
async function build_wasm_js_scripts() {
const dir = path.join(rootDir, "../clarity-repl/js");
await execCommand("npm", ["install"], dir);
}

/**
* build clarinet-sdk-wasm
*/
async function build_wasm_sdk() {
console.log("Deleting pkg-node");
await rmIfExists(path.join(rootDir, "pkg-node"));
console.log("Deleting pkg-browser");
Expand Down Expand Up @@ -40,6 +48,8 @@ async function build() {
]);

await updatePackageName();
await updatePackageJson("pkg-node/package.json");
await updatePackageJson("pkg-browser/package.json");
}

/**
Expand All @@ -48,11 +58,10 @@ async function build() {
* @param {string[]} args
* @returns
*/
export const execCommand = async (command, args) => {
console.log(`Building ${args[5]}`);
export const execCommand = async (command, args, cwd = rootDir) => {
return new Promise((resolve, reject) => {
const childProcess = spawn(command, args, {
cwd: rootDir,
cwd,
});
childProcess.stdout.on("data", (data) => {
process.stdout.write(data.toString());
Expand Down Expand Up @@ -99,11 +108,28 @@ async function updatePackageName() {
'"name": "@hirosystems/clarinet-sdk-wasm-browser"',
);
await fs.writeFile(filePath, updatedData, "utf-8");
console.log("✅ Package name updated successfully.");
console.log("✅ pkg-browser/package.json name updated");
}

/**
* updatePackagesIncludedFiles
* Include snippets/ files and add the sync-request dependency
* @param {string} path
*/
async function updatePackageJson(file) {
const filePath = path.join(rootDir, file);

const fileData = JSON.parse(await fs.readFile(filePath, "utf-8"));
fileData.files.push("snippets/");

fileData.dependencies = { "sync-request": "6.1.0" };
await fs.writeFile(filePath, JSON.stringify(fileData, null, 2), "utf-8");
console.log(`✅ ${file} updated`);
}

try {
await build();
await build_wasm_js_scripts();
await build_wasm_sdk();
console.log("\n✅ Project successfully built.\n🚀 Ready to publish.");
console.log("Run the following commands to publish");
console.log("\n```");
Expand Down
47 changes: 32 additions & 15 deletions components/clarinet-sdk-wasm/src/test_wasm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use super::core::DeployContractArgs;

use crate::core::{CallFnArgs, ContractOptions, EpochString, TransactionRes, SDK};

use clarity::vm::Value as ClarityValue;
use clarity_repl::{
repl::settings::{ApiUrl, RemoteDataSettings},
uprint,
};
use gloo_utils::format::JsValueSerdeExt;
use js_sys::Function as JsFunction;
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
Expand Down Expand Up @@ -63,18 +70,28 @@ async fn it_can_call_a_private_function() {
assert_eq!(tx.result, expected);
}

// #[wasm_bindgen_test]
// async fn it_can_call_remote_data() {
// let mut sdk = init_sdk().await;
// let _ = deploy_basic_contract(&mut sdk);
// let tx = sdk
// .call_private_fn(&CallFnArgs::new(
// "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.basic-contract".into(),
// "two".into(),
// vec![],
// "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM".into(),
// ))
// .unwrap();
// let expected = format!("0x{}", ClarityValue::UInt(2).serialize_to_hex().unwrap());
// assert_eq!(tx.result, expected);
// }
#[wasm_bindgen_test]
async fn it_can_call_remote_data() {
let js_noop = JsFunction::new_no_args("return");
let mut sdk = SDK::new(js_noop, None);
let options = RemoteDataSettings {
enabled: true,
api_url: ApiUrl("https://api.testnet.hiro.so".to_string()),
initial_height: Some(42000),
};
let _ = sdk
.init_empty_session(JsValue::from_serde(&options).unwrap())
.await;
sdk.set_epoch(EpochString::new("3.0"));

let tx = sdk.call_public_fn(&CallFnArgs::new(
"STJCAB2T9TR2EJM7YS4DM2CGBBVTF7BV237Y8KNV.counter".into(),
"get-count".into(),
vec![],
"ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM".into(),
));

uprint!("tx: {:?}", tx);

// let expected = format!("0x{}", ClarityValue::UInt(0).serialize_to_hex().unwrap());
}
7 changes: 3 additions & 4 deletions components/clarinet-sdk/browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hirosystems/clarinet-sdk-browser",
"version": "2.13.0-beta1",
"version": "2.13.0-beta3",
"description": "A SDK to interact with Clarity Smart Contracts in the browser",
"homepage": "https://www.hiro.so/clarinet",
"repository": {
Expand Down Expand Up @@ -28,8 +28,7 @@
"license": "GPL-3.0",
"readme": "./README.md",
"dependencies": {
"@hirosystems/clarinet-sdk-wasm-browser": "2.13.0-beta1",
"@stacks/transactions": "^6.13.0",
"sync-request": "^6.1.0"
"@hirosystems/clarinet-sdk-wasm-browser": "2.13.0-beta3",
"@stacks/transactions": "^6.13.0"
}
}
5 changes: 2 additions & 3 deletions components/clarinet-sdk/node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hirosystems/clarinet-sdk",
"version": "2.13.0-beta1",
"version": "2.13.0-beta3",
"description": "A SDK to interact with Clarity Smart Contracts in node.js",
"homepage": "https://www.hiro.so/clarinet",
"repository": {
Expand Down Expand Up @@ -61,11 +61,10 @@
"license": "GPL-3.0",
"readme": "./README.md",
"dependencies": {
"@hirosystems/clarinet-sdk-wasm": "2.13.0-beta1",
"@hirosystems/clarinet-sdk-wasm": "2.13.0-beta3",
"@stacks/transactions": "^6.13.0",
"kolorist": "^1.8.0",
"prompts": "^2.4.2",
"sync-request": "^6.1.0",
"vitest": "^1.6.0",
"yargs": "^17.7.2"
},
Expand Down
Loading

0 comments on commit 25f095e

Please sign in to comment.