@secondlayer/cli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ryan Waits
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the “Software”), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,233 @@
1
+ # @stacks/codegen
2
+
3
+ Generate fully typed contract interfaces, functions, and React hooks for Clarity smart contracts.
4
+
5
+ ## Usage
6
+
7
+ ### 1. Utilize the generated contract interfaces with familiar libraries
8
+ ```typescript
9
+ // Usage with `@stacks/transactions`
10
+ import { mega } from './generated/contracts'
11
+ import { fetchCallReadOnlyFunction, makeContractCall } from '@stacks/transactions'
12
+
13
+ await makeContractCall({
14
+ ...mega.callback({
15
+ sender: "SPKPXQ0X3A4D1KZ4XTP1GABJX1N36VW10D02TK9X",
16
+ memo: "Hello world",
17
+ }),
18
+ network: 'mainnet',
19
+ })
20
+
21
+ await fetchCallReadOnlyFunction({
22
+ ...mega.getBalance(),
23
+ network: 'mainnet'
24
+ })
25
+ ```
26
+
27
+ ### 2. Use built-in read/write helpers
28
+ ```typescript
29
+ // Read helpers
30
+ const balance = await mega.read.getBalance() // {type: 'uint', value: 42000000n}
31
+
32
+ // Write helpers
33
+ const result = await mega.write.transfer(
34
+ {
35
+ amount: 10000n,
36
+ recipient: "SP3D6PV2ACBPEKYJTCMH7HEN02KP87QSP8KTEH335",
37
+ },
38
+ {
39
+ senderKey: "b244296d5907de9864c0b0d51f98a13c52890be0404e83f273144cd5b9960eed01",
40
+ }
41
+ );
42
+ ```
43
+
44
+ ### 3. React integration
45
+ ```typescript
46
+ import { useBnsV2Transfer } from './generated/hooks'
47
+
48
+ function App() {
49
+ const { transfer, isRequestPending } = useBnsV2Transfer()
50
+
51
+ return (
52
+ <button
53
+ onClick={() => transfer({
54
+ id: 1n,
55
+ owner: 'SP...',
56
+ recipient: 'SP...'
57
+ })}
58
+ disabled={isRequestPending}
59
+ >
60
+ Transfer
61
+ </button>
62
+ )
63
+ }
64
+ ```
65
+
66
+ ## Installation
67
+
68
+ > **Note:** This package is not yet published to npm. For now, you can install it locally:
69
+
70
+ ```bash
71
+ # Clone and build
72
+ git clone https://github.com/ryanwaits/codegen.git
73
+ cd codegen
74
+ bun install
75
+ bun run build
76
+
77
+ # Link globally
78
+ bun link
79
+
80
+ # In your project
81
+ cd /path/to/your/clarinet-project
82
+ bun link @stacks/codegen
83
+ ```
84
+ ## Setup
85
+
86
+ To create a `stacks.config.ts` file, run `codegen init` in your Clarinet project:
87
+
88
+ ```bash
89
+ codegen init
90
+ ```
91
+
92
+ ```typescript
93
+ // stacks.config.ts
94
+ import { defineConfig } from '@stacks/codegen'
95
+ import { clarinet } from '@stacks/codegen/plugins'
96
+
97
+ export default defineConfig({
98
+ out: 'src/generated.ts',
99
+ plugins: [clarinet()],
100
+ })
101
+ ```
102
+
103
+ Run `⁠codegen generate` to create fully type-safe interfaces for your contracts.
104
+
105
+ ```bash
106
+ codegen generate
107
+ ✔ Generation complete for 2 contracts
108
+ 📄 ./src/generated/contracts.ts
109
+ ```
110
+
111
+ ## Advanced
112
+
113
+ ### Plugins
114
+
115
+ ```typescript
116
+ import { defineConfig } from '@stacks/codegen'
117
+ import { clarinet, actions, react, hiro } from '@stacks/codegen/plugins'
118
+
119
+ export default defineConfig({
120
+ out: 'src/generated.ts',
121
+ plugins: [
122
+ clarinet(), // Generate contract interfaces from local Clarinet project
123
+ actions(), // Add read/write helper functions
124
+ react(), // Generate React hooks
125
+ hiro({ // Generate contract interfaces using the Hiro API
126
+ apiKey: process.env.HIRO_API_KEY!,
127
+ network: 'mainnet',
128
+ contracts: [
129
+ 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27.miamicoin-core-v1',
130
+ ],
131
+ }),
132
+ ],
133
+ })
134
+ ```
135
+
136
+ ## Future Enhancements
137
+
138
+ #### Add `testing()` plugin for unit testing
139
+
140
+ Currently, Clarinet JS SDK users must manually convert JS values to Clarity types, recall contract signatures, and write boilerplate for common tests.
141
+
142
+ _Solution:_ Enable zero-config Clarinet detection and auto-run code generation within the dev workflow.
143
+
144
+ ```typescript
145
+ import { describe, it, expect, beforeAll } from 'vitest';
146
+ import { initSimnet } from '@hirosystems/clarinet-sdk';
147
+ import { tokenContract } from './generated/helpers';
148
+
149
+ describe('Token Contract', () => {
150
+ let simnet;
151
+
152
+ beforeAll(async () => {
153
+ simnet = await initSimnet();
154
+ });
155
+
156
+ it('transfers tokens', async () => {
157
+ // Clean, focused API for testing
158
+ const result = await tokenContract.transfer({
159
+ amount: 1000n,
160
+ sender: 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM',
161
+ recipient: 'ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG'
162
+ }, 'wallet_1');
163
+
164
+ expect(result).toBeOk();
165
+ });
166
+ });
167
+ ```
168
+
169
+ #### Converting responses using `cvToValue`
170
+
171
+ Currently, when calling read-only functions or receiving blockchain responses, developers must manually extract values from Clarity value objects that include type metadata (e.g., `{ type: "uint", value: 42000n }`). This requires repetitive boilerplate code to access the actual values.
172
+
173
+ _Solution:_ Provide automatic Clarity value conversion with full TypeScript type inference, extracting raw values while preserving complete type safety.
174
+
175
+ ```typescript
176
+ import { daoContract } from './generated/contracts';
177
+
178
+ // Before
179
+ const result = await daoContract.read.getProposal(proposalId);
180
+ // Returns complex nested structure:
181
+ // {
182
+ // type: "tuple",
183
+ // value: {
184
+ // id: { type: "uint", value: 1n },
185
+ // proposer: { type: "principal", value: "SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7" },
186
+ // title: { type: "string-utf8", value: "Increase Treasury Allocation" },
187
+ // votesFor: { type: "uint", value: 150000n },
188
+ // votesAgainst: { type: "uint", value: 50000n },
189
+ // startBlock: { type: "uint", value: 120500n },
190
+ // endBlock: { type: "uint", value: 125500n },
191
+ // executed: { type: "bool", value: false }
192
+ // }
193
+ // }
194
+
195
+ // Tedious extraction needed:
196
+ return {
197
+ id: result.value.id.value,
198
+ proposer: result.value.proposer.value,
199
+ title: result.value.title.value,
200
+ votesFor: result.value.votesFor.value,
201
+ votesAgainst: result.value.votesAgainst.value,
202
+ startBlock: result.value.startBlock.value,
203
+ endBlock: result.value.endBlock.value,
204
+ executed: result.value.executed.value
205
+ };
206
+
207
+ // After
208
+ const proposal = await daoContract.read.getProposal(proposalId);
209
+ // Returns clean TypeScript object directly:
210
+ // {
211
+ // id: 1n,
212
+ // proposer: "SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7",
213
+ // title: "Increase Treasury Allocation",
214
+ // votesFor: 150000n,
215
+ // votesAgainst: 50000n,
216
+ // startBlock: 120500n,
217
+ // endBlock: 125500n,
218
+ // executed: false
219
+ // }
220
+
221
+ // Full type safety and IntelliSense:
222
+ console.log(proposal.endBlock); // ✅ TypeScript knows this is bigint
223
+ console.log(proposal.title); // ✅ TypeScript knows this is string
224
+ console.log(proposal.executed); // ✅ TypeScript knows this is boolean
225
+
226
+ return proposal;
227
+ ```
228
+
229
+ #### More hooks
230
+
231
+ ## License
232
+
233
+ MIT