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 11, 2023
1 parent bccd4e3 commit d898ba9
Show file tree
Hide file tree
Showing 13 changed files with 1,051 additions and 5 deletions.
14 changes: 12 additions & 2 deletions components/clarinet-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"files": [
"dist"
],
"main": "dist/cjs/index.js",
"module": "dist/esm/index.mjs",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.mjs",
"types": "./dist/esm/index.d.ts",
"exports": {
".": {
Expand All @@ -22,6 +22,16 @@
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.js"
}
},
"./ts-gen": {
"import": {
"types": "./dist/esm/ts-gen/index.d.ts",
"default": "./dist/esm/ts-gen/index.mjs"
},
"require": {
"types": "./dist/cjs/ts-gen/index.d.ts",
"default": "./dist/cjs/ts-gen/index.js"
}
}
},
"scripts": {
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()
9 changes: 6 additions & 3 deletions components/clarinet-sdk/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const CopyPlugin = require("copy-webpack-plugin");
/** @type WebpackConfig */
const configBase = {
mode: "production",
entry: "./src-ts/index.ts",
entry: {
index: "./src-ts/index.ts",
"ts-gen/index": "./src-ts/ts-gen/index.ts",
},
resolve: { extensions: [".ts", ".js"] },
optimization: {
minimize: false,
Expand All @@ -19,7 +22,7 @@ const configESM = {
...configBase,
target: "node20",
output: {
filename: "index.mjs",
filename: "[name].mjs",
path: path.resolve(__dirname, "dist/esm"),
library: {
type: "module",
Expand Down Expand Up @@ -56,7 +59,7 @@ const configCJS = {
...configBase,
target: "node20",
output: {
filename: "index.js",
filename: "[name].js",
path: path.resolve(__dirname, "dist/cjs"),
library: {
type: "commonjs",
Expand Down
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 d898ba9

Please sign in to comment.