Skip to content

Commit

Permalink
feat: init ts-gen project
Browse files Browse the repository at this point in the history
Co-authored-by: janniks <[email protected]>
  • Loading branch information
hugocaillard and janniks committed Aug 8, 2023
1 parent 4121c02 commit 0da97c2
Show file tree
Hide file tree
Showing 15 changed files with 1,169 additions and 12 deletions.
4 changes: 2 additions & 2 deletions components/clarinet-sdk/package-lock.json

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

17 changes: 14 additions & 3 deletions components/clarinet-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obscurity-sdk",
"version": "0.1.1",
"version": "0.1.3",
"description": "clarity testing lib",
"repository": {
"type": "git",
Expand All @@ -9,13 +9,24 @@
"files": [
"dist"
],
"main": "./dist/index.js",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./ts-gen": {
"types": "./dist/ts-gen/index.d.ts",
"import": "./dist/ts-gen/index.js"
}
},
"scripts": {
"dev:build-wasm": "rimraf ./src-ts/sdk/*; wasm-pack build --target=web --dev --out-dir ./src-ts/sdk",
"dev:build-ts": "rimraf ./dist/*; rollup -c ./rollup.config.mjs",
"dev:build": "npm run dev:build-wasm; npm run dev:build-ts",
"build-wasm": "rimraf ./src-ts/sdk/*; wasm-pack build --target=web --out-dir ./src-ts/sdk",
"build-ts": "rimraf ./dist/*; rollup -c ./rollup.config.mjs --environment INCLUDE_DEPS,BUILD:production",
"build-ts": "rimraf ./dist/*; rollup -c ./rollup.config.mjs",
"build-ts-watch": "rimraf ./dist/*; rollup --watch -c ./rollup.config.mjs",
"build": "npm run build-wasm; npm run build-ts",
"prepare": "npm run build"
},
Expand Down
8 changes: 5 additions & 3 deletions components/clarinet-sdk/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import { wasm } from "@rollup/plugin-wasm";
import typescript from "@rollup/plugin-typescript";
import copy from "rollup-plugin-copy";

/** @type import("rollup").RollupOptions */
export default {
input: {
index: "src-ts/index.ts",
"ts-gen/index": "src-ts/ts-gen/index.ts",
},
plugins: [
wasm(),
wasm({ targetEnv: "auto-inline" }),
typescript({ tsconfig: "./tsconfig.json" }),
copy({ targets: [{ src: "./src-ts/sdk/clarinet_sdk.d.ts", dest: "dist/sdk" }] }),
],
module: "esnext",
output: {
dir: "dist",
format: "cjs",
format: "esm",
sourcemap: "true",
},
};
14 changes: 10 additions & 4 deletions components/clarinet-sdk/src-ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// core -> import from "clarity-sdk"
// test -> import from "clarity-sdk/test"
// ts-gen -> import from "@hirosystems/clarity-sdk/ts-gen"

import { Cl, ClarityValue } from "@stacks/transactions";

import init, { CallContractArgs, SDK } from "./sdk/clarinet_sdk";
import init, { CallContractArgs, SDK } from "./sdk/clarinet_sdk.js";
// @ts-ignore
import wasm from "./sdk/clarinet_sdk_bg.wasm";
import { vfs } from "./vfs";
import type { ContractInterface } from "./contractInterface";
import { vfs } from "./vfs.js";
import type { ContractInterface } from "./contractInterface.js";

type CallFn = (
contract: string,
Expand Down Expand Up @@ -94,10 +99,11 @@ const sessionProxy = {
// load wasm only once and memoize it
function memoizedInit() {
//@ts-ignore
let vm = null;
let vm: ClarityVM | null = null;
return async (manifestPath = "./Clarinet.toml") => {
//@ts-ignore
if (!vm) {
console.log("init clarity vm");
// @ts-ignore
await init(wasm());
vm = new Proxy(new SDK(vfs), sessionProxy) as unknown as ClarityVM;
Expand Down
51 changes: 51 additions & 0 deletions components/clarinet-sdk/src-ts/ts-gen/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ContractInterface } from "../contractInterface.js";

// @todo: fix the type, the key is a contract identifier not a string
export function generateTSLib(contractInterfaces: Map<string, ContractInterface>) {
contractInterfaces.forEach((contractInterface, identifier) => {
// @ts-ignore
const name: string = identifier.name;

console.log(name, JSON.stringify(contractInterface, null, 2));
});
}

// input: contractInterface

// output:
// name-of-the-project.ts

// export const countract1 = {
// increment: () => {
// // call increment function
// },
// decrement: () => {
// // call decrement function
// }
// }

// export const countract2 = {
// increment: () => {
// // call increment function
// },
// decrement: () => {
// // call decrement function
// }
// }

// import contracts from "/ts-gen/proxy"

// const {counter }= contracts;

// const {increment } = counter

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

// callReadOnlyFunction.bind(null, "ASDKLJASLDKJ", )

// contracts.counter.increment()

// const res: = await counter.increment(arg1, arg2)

// usage
// counter.increment()
5 changes: 5 additions & 0 deletions examples/counter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

**/settings/Mainnet.toml
**/settings/Testnet.toml
.cache/**
history.txt
5 changes: 5 additions & 0 deletions examples/counter/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

{
"deno.enable": true,
"files.eol": "\n"
}
18 changes: 18 additions & 0 deletions examples/counter/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

{
"version": "2.0.0",
"tasks": [
{
"label": "check contracts",
"group": "test",
"type": "shell",
"command": "clarinet check"
},
{
"label": "test contracts",
"group": "test",
"type": "shell",
"command": "clarinet test"
}
]
}
21 changes: 21 additions & 0 deletions examples/counter/Clarinet.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[project]
name = 'counter'
description = ''
authors = []
telemetry = false
cache_dir = './.cache'
requirements = []

[contracts.counter]
path = 'contracts/counter.clar'
clarity_version = 2
epoch = 2.4

[repl.analysis]
passes = []

[repl.analysis.check_checker]
strict = false
trusted_sender = false
trusted_caller = false
callee_filter = false
24 changes: 24 additions & 0 deletions examples/counter/contracts/counter.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

;; counter
;; let's get started with smart contracts
(define-data-var counter uint u1)

(define-public (increment (step uint))
(let ((new-val (+ step (var-get counter))))
(var-set counter new-val)
(print { object: "counter", action: "incremented", value: new-val })
(ok new-val)
)
)

(define-public (decrement (step uint))
(let ((new-val (- step (var-get counter))))
(var-set counter new-val)
(print { object: "counter", action: "decremented", value: new-val })
(ok new-val)
)
)

(define-read-only (read-counter)
(var-get counter)
)
Loading

0 comments on commit 0da97c2

Please sign in to comment.