@rootzero/contracts 0.9.3 → 0.9.4

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.
@@ -1,294 +0,0 @@
1
- # Getting Started With rootzero
2
-
3
- This guide is for developers who want to build on rootzero without reading the whole codebase first.
4
-
5
- If you remember only one thing, remember this:
6
-
7
- - A `Host` is your application contract.
8
- - A command is an entrypoint the rootzero runtime is allowed to call.
9
- - Requests and responses are passed around as typed byte blocks.
10
-
11
- ## The Mental Model
12
-
13
- rootzero moves data through a small command context:
14
-
15
- ```solidity
16
- struct CommandContext {
17
- bytes32 account;
18
- bytes state;
19
- bytes request;
20
- }
21
- ```
22
-
23
- In practice:
24
-
25
- - `account` is the user account the command is acting for.
26
- - `request` is the new input for this command.
27
- - `state` is live pipeline state produced by an earlier command.
28
-
29
- Most built-in commands follow a simple pattern:
30
-
31
- - read blocks from `request` or `state`
32
- - apply your host logic
33
- - return new blocks
34
-
35
- ## Step 1: Start With A Host
36
-
37
- The smallest useful rootzero app is a host contract.
38
-
39
- ```solidity
40
- // SPDX-License-Identifier: MIT
41
- pragma solidity ^0.8.33;
42
-
43
- import {Host} from "@rootzero/contracts/Core.sol";
44
-
45
- contract ExampleHost is Host {
46
- constructor(address rootzero)
47
- Host(rootzero, 1, "example")
48
- {}
49
- }
50
- ```
51
-
52
- What the constructor arguments mean:
53
-
54
- - `rootzero`: the trusted rootzero runtime allowed to call commands
55
- - `1`: your host version
56
- - `"example"`: your host namespace
57
-
58
- If `rootzero` is a contract, the host announces itself there during deployment. If you pass `address(0)`, the host becomes self-managed and does not auto-register.
59
-
60
- ## Step 2: Reuse A Built-In Command
61
-
62
- The easiest way to integrate is to inherit a built-in command module and implement its hook.
63
-
64
- This example adds `DebitAccount`, which turns `AMOUNT` blocks in `request` into `BALANCE` blocks in the response:
65
-
66
- ```solidity
67
- // SPDX-License-Identifier: MIT
68
- pragma solidity ^0.8.33;
69
-
70
- import {Host} from "@rootzero/contracts/Core.sol";
71
- import {DebitAccount} from "@rootzero/contracts/Commands.sol";
72
- import {Assets} from "@rootzero/contracts/Utils.sol";
73
-
74
- contract ExampleHost is Host, DebitAccount {
75
- mapping(bytes32 account => mapping(bytes32 assetKey => uint amount)) internal balances;
76
-
77
- constructor(address rootzero)
78
- Host(rootzero, 1, "example")
79
- {}
80
-
81
- function debitAccount(
82
- bytes32 account,
83
- bytes32 asset,
84
- bytes32 meta,
85
- uint amount
86
- ) internal override {
87
- bytes32 key = Assets.slot(asset, meta);
88
- balances[account][key] -= amount;
89
- }
90
- }
91
- ```
92
-
93
- Why this is a good first step:
94
-
95
- - you do not need to write block parsing yourself
96
- - you get the standard rootzero command surface
97
- - you only implement the business rule that is unique to your app
98
-
99
- ## Step 3: Understand What Built-In Commands Consume
100
-
101
- The built-in commands are easiest to use when you know which blocks they expect.
102
-
103
- ### Commands That Read `request`
104
-
105
- - `deposit`: reads `AMOUNT` blocks, returns `BALANCE`
106
- - `transfer`: reads `PAYOUT` blocks
107
- - `debitAccount`: reads `AMOUNT`, returns `BALANCE`
108
- - `provision`: reads `ALLOCATION`, returns `CUSTODY` state
109
- - `pipePayable`: reads `STEP` blocks and runs them in order
110
-
111
- ### Commands That Read `state`
112
-
113
- - `withdraw`: reads `BALANCE`, optionally reads `ACCOUNT` from `request`
114
- - `creditAccount`: reads `BALANCE`, optionally reads `ACCOUNT` from `request`
115
-
116
- This is the main pattern to keep in mind:
117
-
118
- - use `request` for the command's direct input
119
- - use `state` for live value threaded through a pipeline, currently `BALANCE` and `CUSTODY`
120
- - use peer commands such as `peerSettle` for settlement messages such as `TRANSACTION`
121
-
122
- ## Step 4: Send A Simple Request
123
-
124
- For a host that supports `deposit`, a request with one `AMOUNT` block is enough.
125
-
126
- TypeScript helper example:
127
-
128
- ```ts
129
- import { ethers } from "ethers";
130
- import { encodeAmountBlock } from "../test/helpers/blocks.js";
131
-
132
- const asset = ethers.zeroPadValue("0x01", 32);
133
- const meta = ethers.ZeroHash;
134
- const amount = 100n;
135
-
136
- const ctx = {
137
- account: "0x...", // 32-byte rootzero account id
138
- state: "0x",
139
- request: encodeAmountBlock(asset, meta, amount),
140
- };
141
-
142
- await host.deposit(ctx);
143
- ```
144
-
145
- What happens:
146
-
147
- 1. `deposit` reads the `AMOUNT` block from `ctx.request`.
148
- 2. Your host applies its deposit logic.
149
- 3. The command returns one `BALANCE` block for each deposited amount.
150
-
151
- ## Step 5: Create A Custom Command
152
-
153
- When the built-in modules are not enough, add your own command entrypoint.
154
-
155
- ```solidity
156
- // SPDX-License-Identifier: MIT
157
- pragma solidity ^0.8.33;
158
-
159
- import {CommandBase, CommandContext, Keys} from "@rootzero/contracts/Commands.sol";
160
- import {Cursors, Cur, Schemas} from "@rootzero/contracts/Cursors.sol";
161
-
162
- using Cursors for Cur;
163
-
164
- string constant NAME = "myCommand";
165
- string constant ROUTE = "route(uint foo, uint bar)";
166
- string constant INPUT = string.concat(ROUTE, "&", Schemas.Amount);
167
-
168
- abstract contract MyCommand is CommandBase {
169
- uint internal immutable myCommandId = commandId(NAME);
170
-
171
- constructor() {
172
- emit Command(host, myCommandId, NAME, INPUT, Keys.Empty, Keys.Balance, false);
173
- }
174
-
175
- function myCommand(
176
- CommandContext calldata c
177
- ) external onlyCommand(c.account) returns (bytes memory) {
178
- Cur memory input = cursor(c.request);
179
- uint next = input.bundle();
180
-
181
- bytes calldata route = input.unpackRaw(Keys.Route);
182
- (bytes32 asset, bytes32 meta, uint amount) = input.unpackAmount();
183
- input.ensure(next);
184
-
185
- route;
186
- return Cursors.toBalanceBlock(asset, meta, amount);
187
- }
188
- }
189
- ```
190
-
191
- There are three important ideas here:
192
-
193
- - every custom command gets a deterministic command id
194
- - you announce it with the `Command` event
195
- - `onlyCommand(c.account)` ensures the caller is trusted and the calldata account matches `c.account`
196
-
197
- ## Step 6: Read Input With A Cursor
198
-
199
- Cursor parsing is the nicest way to read structured command input.
200
-
201
- If your request contains a bundled input like:
202
-
203
- - `route(uint foo) & amount(bytes32 asset, bytes32 meta, uint amount)`
204
-
205
- your command can:
206
-
207
- - open it with `cursor(c.request)` or `Cursors.open(...)`
208
- - consume the route first
209
- - then consume the amount
210
- - keep parsing in bundle/member order without indexing helpers
211
-
212
- For simple projects, it is perfectly fine to:
213
-
214
- - publish the full input schema string in the `Command` event
215
- - encode bundled input blocks off-chain
216
- - decode them sequentially with cursor helpers inside the command
217
-
218
- ## Step 7: Return State With Writers
219
-
220
- When your command needs to build response blocks manually, use `Writers`.
221
-
222
- ```solidity
223
- // SPDX-License-Identifier: MIT
224
- pragma solidity ^0.8.33;
225
-
226
- import {Writers, Writer} from "@rootzero/contracts/Cursors.sol";
227
-
228
- using Writers for Writer;
229
-
230
- function buildBalances() internal pure returns (bytes memory) {
231
- Writer memory writer = Writers.allocBalances(2);
232
- writer.appendBalance(bytes32(uint256(1)), bytes32(0), 50);
233
- writer.appendBalance(bytes32(uint256(2)), bytes32(0), 75);
234
- return writer.finish();
235
- }
236
- ```
237
-
238
- Use this when your command needs to return:
239
-
240
- - balances
241
- - custody state
242
-
243
- If you are only consuming built-in commands, you often will not need to touch writers directly.
244
-
245
- ## Query Forms
246
-
247
- Queries use reusable `Forms` as their input and output schema vocabulary. For example, `getBalances` accepts `Forms.AccountAsset` blocks and returns `Forms.AccountAmount` blocks:
248
-
249
- ```solidity
250
- emit Query(host, NAME, Forms.AccountAsset, Forms.AccountAmount, getBalancesId);
251
- ```
252
-
253
- This keeps query payloads structural: the query name describes what is being asked, while the form describes the fields carried by each block.
254
-
255
- ## A Tiny End-To-End Example
256
-
257
- Imagine you want a host that keeps internal balances and lets rootzero debit them.
258
-
259
- 1. Deploy a host that inherits `Host` and `DebitAccount`.
260
- 2. Store balances in your own mapping.
261
- 3. Implement `debitAccount(account, asset, meta, amount)`.
262
- 4. Send `debitAccount` a request containing one or more `AMOUNT` blocks.
263
- 5. rootzero returns `BALANCE` blocks representing the debited amounts.
264
-
265
- That is already a valid and useful integration.
266
-
267
- ## Which Files To Open Next
268
-
269
- If you want to learn by example, these are the best files to read next:
270
-
271
- - `examples/1-Host.sol`: smallest host
272
- - `examples/2-Basic.sol`: host plus a built-in command hook
273
- - `examples/3-Command.sol`: custom command id and command event
274
- - `examples/4-Batch.sol`: batching request input and building balance output
275
- - `examples/5-Route.sol`: bundled route input plus protocol blocks
276
- - `test/commands.test.ts`: concrete request and response examples
277
- - `test/helpers/blocks.ts`: block encoders you can reuse in off-chain tooling
278
-
279
- ## Common Mistakes
280
-
281
- - Passing data in `state` when the command expects it in `request`
282
- - Forgetting to emit a `Command` event for a custom command
283
- - Using an admin account with user-only command flows such as `pipePayable`
284
- - Trying to parse raw bytes manually when a built-in reader already exists
285
- - Starting with a custom command when a built-in module already matches the job
286
-
287
- ## Recommended Learning Order
288
-
289
- 1. Deploy a plain `Host`.
290
- 2. Add one built-in command such as `DebitAccount` or `Deposit`.
291
- 3. Use the TypeScript block helpers to build requests.
292
- 4. Only then add a custom command with bundled input and cursor parsing.
293
-
294
- That path keeps the first integration small and easy to debug.