@rootzero/contracts 0.2.0 → 0.5.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.
Files changed (106) hide show
  1. package/README.md +59 -59
  2. package/contracts/Commands.sol +7 -2
  3. package/contracts/Core.sol +7 -1
  4. package/contracts/{Blocks.sol → Cursors.sol} +8 -2
  5. package/contracts/Events.sol +6 -0
  6. package/contracts/Utils.sol +8 -2
  7. package/contracts/blocks/Cursors.sol +818 -0
  8. package/contracts/blocks/Keys.sol +27 -0
  9. package/contracts/blocks/Schema.sol +68 -15
  10. package/contracts/blocks/Writers.sol +177 -82
  11. package/contracts/commands/Base.sol +31 -6
  12. package/contracts/commands/Borrow.sol +40 -41
  13. package/contracts/commands/Burn.sol +25 -13
  14. package/contracts/commands/Create.sol +24 -15
  15. package/contracts/commands/Credit.sol +26 -13
  16. package/contracts/commands/Debit.sol +28 -16
  17. package/contracts/commands/Deposit.sol +26 -14
  18. package/contracts/commands/Liquidate.sol +46 -49
  19. package/contracts/commands/Liquidity.sol +85 -85
  20. package/contracts/commands/Mint.sol +27 -16
  21. package/contracts/commands/Pipe.sol +28 -12
  22. package/contracts/commands/Provision.sol +36 -27
  23. package/contracts/commands/Reclaim.sol +27 -21
  24. package/contracts/commands/Redeem.sol +46 -49
  25. package/contracts/commands/Remove.sol +24 -15
  26. package/contracts/commands/Repay.sol +46 -49
  27. package/contracts/commands/Settle.sol +22 -17
  28. package/contracts/commands/Stake.sol +61 -53
  29. package/contracts/commands/Supply.sol +19 -12
  30. package/contracts/commands/Swap.sol +42 -41
  31. package/contracts/commands/Transfer.sol +40 -22
  32. package/contracts/commands/Unstake.sol +28 -20
  33. package/contracts/commands/Withdraw.sol +26 -14
  34. package/contracts/commands/admin/Allocate.sol +20 -12
  35. package/contracts/commands/admin/AllowAssets.sol +19 -11
  36. package/contracts/commands/admin/Authorize.sol +19 -11
  37. package/contracts/commands/admin/DenyAssets.sol +19 -11
  38. package/contracts/commands/admin/Destroy.sol +21 -10
  39. package/contracts/commands/admin/Init.sol +21 -9
  40. package/contracts/commands/admin/Relocate.sol +20 -11
  41. package/contracts/commands/admin/Unauthorize.sol +19 -11
  42. package/contracts/core/Access.sol +32 -3
  43. package/contracts/core/Balances.sol +23 -6
  44. package/contracts/core/Host.sol +19 -0
  45. package/contracts/core/Operation.sol +44 -7
  46. package/contracts/core/Validator.sol +16 -1
  47. package/contracts/events/Access.sol +7 -0
  48. package/contracts/events/Asset.sol +9 -2
  49. package/contracts/events/Balance.sol +10 -0
  50. package/contracts/events/Collateral.sol +10 -1
  51. package/contracts/events/Command.sol +10 -0
  52. package/contracts/events/Debt.sol +11 -1
  53. package/contracts/events/Deposit.sol +9 -0
  54. package/contracts/events/Emitter.sol +8 -1
  55. package/contracts/events/Governed.sol +7 -0
  56. package/contracts/events/HostAnnounced.sol +8 -0
  57. package/contracts/events/Listing.sol +9 -0
  58. package/contracts/events/Peer.sol +8 -0
  59. package/contracts/events/Quote.sol +7 -0
  60. package/contracts/events/RootZero.sol +6 -2
  61. package/contracts/events/Withdraw.sol +9 -0
  62. package/contracts/interfaces/IHostDiscovery.sol +10 -0
  63. package/contracts/peer/AllowAssets.sol +23 -9
  64. package/contracts/peer/Base.sol +8 -0
  65. package/contracts/peer/DenyAssets.sol +23 -9
  66. package/contracts/peer/Pull.sol +24 -12
  67. package/contracts/peer/Push.sol +24 -12
  68. package/contracts/peer/Settle.sol +34 -0
  69. package/contracts/test/TestBorrowHost.sol +32 -7
  70. package/contracts/test/TestBurnHost.sol +4 -1
  71. package/contracts/test/TestCreateHost.sol +19 -5
  72. package/contracts/test/TestCursorHelper.sol +191 -0
  73. package/contracts/test/TestDiscovery.sol +3 -0
  74. package/contracts/test/TestECDSA.sol +3 -0
  75. package/contracts/test/TestHost.sol +36 -17
  76. package/contracts/test/TestLiquidityHost.sol +75 -53
  77. package/contracts/test/TestMintHost.sol +15 -5
  78. package/contracts/test/TestOperation.sol +21 -0
  79. package/contracts/test/TestPeerHost.sol +33 -10
  80. package/contracts/test/TestReclaimHost.sol +16 -9
  81. package/contracts/test/TestRejectEther.sol +3 -0
  82. package/contracts/test/TestRemoveHost.sol +19 -5
  83. package/contracts/test/TestSupplyHost.sol +25 -0
  84. package/contracts/test/TestSwapHost.sol +33 -10
  85. package/contracts/test/TestUtils.sol +17 -2
  86. package/contracts/test/TestValidator.sol +3 -0
  87. package/contracts/utils/Accounts.sol +61 -0
  88. package/contracts/utils/Assets.sol +65 -3
  89. package/contracts/utils/ECDSA.sol +22 -0
  90. package/contracts/utils/Ids.sol +54 -0
  91. package/contracts/utils/Layout.sol +44 -0
  92. package/contracts/utils/State.sol +22 -0
  93. package/contracts/utils/Utils.sol +69 -1
  94. package/contracts/utils/Value.sol +12 -0
  95. package/package.json +2 -2
  96. package/contracts/blocks/Blocks.sol +0 -818
  97. package/contracts/blocks/Mem.sol +0 -129
  98. package/contracts/combinators/AmountToBalance.sol +0 -25
  99. package/contracts/combinators/AmountToCustody.sol +0 -36
  100. package/contracts/combinators/CustodyToBalance.sol +0 -25
  101. package/contracts/combinators/EachRoute.sol +0 -18
  102. package/contracts/combinators/MapBalance.sol +0 -25
  103. package/contracts/combinators/MapCustody.sol +0 -25
  104. package/contracts/combinators/RouteToBalance.sol +0 -27
  105. package/contracts/test/TestBlockHelper.sol +0 -261
  106. package/contracts/utils/Channels.sol +0 -11
package/README.md CHANGED
@@ -1,30 +1,22 @@
1
1
  # rootzero
2
2
 
3
- `rootzero` is the Solidity library used to build hosts and commands for the RootZero protocol.
3
+ `rootzero` is the Solidity library for building hosts and commands for the rootzero protocol.
4
4
 
5
- It contains the reusable contracts, utilities, and encoding helpers that RootZero applications compose on top of. If you are building a RootZero host, a command contract, or a small protocol extension that needs to speak RootZero's id, asset, and block formats, this repo is the shared foundation.
6
-
7
- ## What You Build With It
8
-
9
- - `Host` contracts that register with RootZero discovery and expose trusted command endpoints
10
- - `Command` contracts that execute protocol actions such as transfer, deposit, withdraw, settlement, and admin flows
11
- - Shared request/response block parsing and writing logic
12
- - Shared id, asset, account, and event encoding used across the protocol
5
+ It contains the reusable contracts, utilities, cursor parsers, and encoding helpers that rootzero applications compose on top of. If you are building a host, a command contract, or protocol tooling that needs to speak the protocol's ID, asset, account, and block formats, this repo is the shared foundation.
13
6
 
14
7
  ## Main Entry Points
15
8
 
16
- Most consumers should start from the package root entrypoints:
9
+ Most consumers should start from the package root entry points:
17
10
 
18
- - `@rootzero/contracts/Core.sol`: core host and validation building blocks
19
- - `@rootzero/contracts/Commands.sol`: base command contract plus standard command mixins
20
- - `@rootzero/contracts/Blocks.sol`: block schema, readers, and writers
21
- - `@rootzero/contracts/Utils.sol`: ids, assets, accounts, layout, strings, and value helpers
22
- - `@rootzero/contracts/Events.sol`: reusable event emitters and event contracts
11
+ - `@rootzero/contracts/Core.sol` host, access control, balances, and validator building blocks
12
+ - `@rootzero/contracts/Commands.sol` command and peer base contracts plus all standard command mixins
13
+ - `@rootzero/contracts/Cursors.sol` — cursor reader (`Cur`), block schemas, key constants, memory refs, and writers
14
+ - `@rootzero/contracts/Utils.sol` — IDs, assets, accounts, state discriminants, layout, and value helpers
15
+ - `@rootzero/contracts/Events.sol` reusable event emitters and event contracts
23
16
 
24
17
  ## Start Here
25
18
 
26
- If you are new to RootZero, start with the getting started guide in the repository:
27
- https://github.com/lastqubit/rootzero-contracts/blob/main/docs/GETTING_STARTED.md
19
+ If you are new to rootzero, read [`docs/GETTING_STARTED.md`](docs/GETTING_STARTED.md) first.
28
20
 
29
21
  It walks through:
30
22
 
@@ -35,17 +27,40 @@ It walks through:
35
27
  - a custom command example
36
28
  - simple TypeScript request encoding
37
29
 
30
+ ## Block Wire Format
31
+
32
+ All request and response data is encoded as a binary block stream. Each block is:
33
+
34
+ ```
35
+ [bytes4 key][bytes4 payloadLen][payload]
36
+ ```
37
+
38
+ `key` is `bytes4(keccak256(schemaString))` — see `Keys` for the full set. `Cursors` parses calldata streams zero-copy via the `Cur` struct; `Writers` builds response streams into pre-allocated memory; `Mem` parses in-memory streams for composed responses.
39
+
40
+ ## State Discriminants
41
+
42
+ Every command declares its input and output state shape using constants from the `State` library:
43
+
44
+ | Constant | Meaning |
45
+ |----------------------|-----------------------------------|
46
+ | `State.Empty` | No state (empty stream) |
47
+ | `State.Steps` | STEP blocks (sub-command pipeline)|
48
+ | `State.Balances` | BALANCE blocks |
49
+ | `State.Transactions` | TRANSACTION blocks |
50
+ | `State.Custodies` | CUSTODY blocks |
51
+ | `State.Claims` | Claim records |
52
+
38
53
  ## Typical Usage
39
54
 
40
55
  ### Build a Host
41
56
 
42
- Extend `Host` when you want a RootZero host contract with admin command support and optional discovery registration.
57
+ Extend `Host` when you want a rootzero host contract with admin command support and optional discovery registration.
43
58
 
44
59
  ```solidity
45
60
  // SPDX-License-Identifier: GPL-3.0-only
46
61
  pragma solidity ^0.8.33;
47
62
 
48
- import {Host} from "@rootzero/contracts/Core.sol";
63
+ import { Host } from "@rootzero/contracts/Core.sol";
49
64
 
50
65
  contract ExampleHost is Host {
51
66
  constructor(address rootzero)
@@ -54,82 +69,67 @@ contract ExampleHost is Host {
54
69
  }
55
70
  ```
56
71
 
57
- `rootzero` is the trusted RootZero runtime. If it is a contract, the host also announces itself there during deployment. Use `address(0)` for a self-managed host that does not auto-register.
58
-
59
- `Host` already layers in the standard admin command flows used by RootZero hosts:
60
-
61
- - `Authorize`
62
- - `Unauthorize`
63
- - `Relocate`
72
+ `rootzero` is the trusted runtime address. If it implements `IHostDiscovery`, the host announces itself there during deployment. Use `address(0)` for a self-managed host that does not auto-register.
64
73
 
65
74
  ### Build a Command
66
75
 
67
- Extend `CommandBase` when you want a RootZero command mixin that runs inside the protocol's trusted call model. Commands are abstract contracts mixed into a host or composed as a standalone module.
76
+ Extend `CommandBase` to define a command mixin that runs inside the protocol's trusted call model. Commands are abstract contracts mixed into a host.
68
77
 
69
78
  ```solidity
70
79
  // SPDX-License-Identifier: GPL-3.0-only
71
80
  pragma solidity ^0.8.33;
72
81
 
73
- import {CommandBase, CommandContext} from "@rootzero/contracts/Commands.sol";
82
+ import { CommandBase, CommandContext } from "@rootzero/contracts/Commands.sol";
83
+ import { Cursors, Cur, Schemas } from "@rootzero/contracts/Cursors.sol";
84
+ import { State } from "@rootzero/contracts/Utils.sol";
85
+
86
+ using Cursors for Cur;
74
87
 
75
88
  string constant NAME = "myCommand";
76
- string constant ROUTE = "route(uint foo, uint bar)";
77
89
 
78
90
  abstract contract ExampleCommand is CommandBase {
79
91
  uint internal immutable myCommandId = commandId(NAME);
80
92
 
81
93
  constructor() {
82
- emit Command(host, NAME, ROUTE, myCommandId, 0, 0);
94
+ emit Command(host, NAME, Schemas.Amount, myCommandId, State.Empty, State.Balances);
83
95
  }
84
96
 
85
97
  function myCommand(
86
98
  CommandContext calldata c
87
99
  ) external payable onlyCommand(myCommandId, c.target) returns (bytes memory) {
88
- return "";
100
+ (Cur memory input, , ) = cursor(c.request, 1);
101
+ (bytes32 asset, bytes32 meta, uint amount) = input.unpackAmount();
102
+ input.complete();
103
+ return Cursors.toBalanceBlock(asset, meta, amount);
89
104
  }
90
105
  }
91
106
  ```
92
107
 
93
- `CommandBase` gives you the common RootZero command context:
94
-
95
- - trusted caller enforcement
96
- - admin checks
97
- - expiry checks
98
- - command-to-command or command-to-host calls through encoded RootZero node ids
99
- - shared command events
100
-
101
108
  ## Repo Layout
102
109
 
103
- - `contracts/core`: host, access control, balances, and validation primitives
104
- - `contracts/commands`: standard command building blocks and admin commands
105
- - `contracts/peer`: peer protocol surfaces for inter-host asset flows
106
- - `contracts/blocks`: request/response block encoding and decoding
107
- - `contracts/utils`: shared protocol encoding helpers
108
- - `contracts/events`: protocol event contracts and emitters
109
- - `contracts/interfaces`: discovery interfaces and shared external protocol surfaces
110
+ - `contracts/core` host, access control, balances, operation base, and signature validation
111
+ - `contracts/commands` standard command building blocks and admin commands
112
+ - `contracts/peer` peer protocol surfaces for inter-host asset flows and asset allow/deny
113
+ - `contracts/blocks` block stream schema (`Schema`), cursor parsing (`Cursors`), memory refs (`Mem`), and writers (`Writers`)
114
+ - `contracts/utils` shared encoding helpers: IDs, assets, accounts, state discriminants, layout, ECDSA
115
+ - `contracts/events` protocol event contracts and emitters
116
+ - `contracts/interfaces` discovery interfaces and shared external protocol surfaces
117
+ - `docs` — introductory documentation
110
118
 
111
- ## Install And Compile
119
+ ## Install and Compile
112
120
 
113
121
  ```bash
114
122
  npm install @rootzero/contracts
115
123
  npm run compile
116
124
  ```
117
125
 
118
- The stable import surface for consumers is:
119
-
120
- - `@rootzero/contracts/Core.sol`
121
- - `@rootzero/contracts/Commands.sol`
122
- - `@rootzero/contracts/Blocks.sol`
123
- - `@rootzero/contracts/Utils.sol`
124
- - `@rootzero/contracts/Events.sol`
125
-
126
126
  ## When To Use This Repo
127
127
 
128
128
  Use `rootzero` if you want to:
129
129
 
130
- - create a new RootZero host
131
- - implement a new RootZero command
132
- - reuse RootZero's block format and wire encoding
133
- - share protocol-level Solidity code across multiple RootZero applications
130
+ - create a new rootzero host
131
+ - implement a new rootzero command
132
+ - reuse the protocol's block format and wire encoding
133
+ - share protocol-level Solidity code across multiple rootzero applications
134
134
 
135
135
  If you are looking for a full end-user app or deployment repo, this library is the lower-level protocol package rather than the full product surface.
@@ -1,9 +1,11 @@
1
1
  // SPDX-License-Identifier: GPL-3.0-only
2
2
  pragma solidity ^0.8.33;
3
3
 
4
+ // Aggregator: re-exports all command and peer abstractions.
5
+ // Import this file to inherit from any command or peer base contract without managing individual paths.
6
+
4
7
  import { CommandBase, CommandContext } from "./commands/Base.sol";
5
- import { NoOperation } from "./core/Operation.sol";
6
- import { Channels } from "./utils/Channels.sol";
8
+ import { State } from "./utils/State.sol";
7
9
  import { BorrowAgainstBalanceToBalance, BorrowAgainstCustodyToBalance } from "./commands/Borrow.sol";
8
10
  import { Burn } from "./commands/Burn.sol";
9
11
  import { Create } from "./commands/Create.sol";
@@ -39,3 +41,6 @@ import { PeerAllowAssets } from "./peer/AllowAssets.sol";
39
41
  import { PeerDenyAssets } from "./peer/DenyAssets.sol";
40
42
  import { PeerPull } from "./peer/Pull.sol";
41
43
  import { PeerPush } from "./peer/Push.sol";
44
+
45
+
46
+
@@ -1,10 +1,16 @@
1
1
  // SPDX-License-Identifier: GPL-3.0-only
2
2
  pragma solidity ^0.8.33;
3
3
 
4
+ // Aggregator: re-exports all core protocol contracts (Access, Balances, Host, Operation, Validator).
5
+ // Import this file to bring the full host base layer into scope.
6
+
4
7
  import { AccessControl } from "./core/Access.sol";
5
8
  import { Balances } from "./core/Balances.sol";
6
9
  import { Host } from "./core/Host.sol";
7
- import { FailedCall, NoOperation, OperationBase } from "./core/Operation.sol";
10
+ import { FailedCall, OperationBase } from "./core/Operation.sol";
8
11
  import { Validator } from "./core/Validator.sol";
9
12
  import { HostDiscovery } from "./core/Host.sol";
10
13
  import { IHostDiscovery } from "./interfaces/IHostDiscovery.sol";
14
+
15
+
16
+
@@ -1,9 +1,15 @@
1
1
  // SPDX-License-Identifier: GPL-3.0-only
2
2
  pragma solidity ^0.8.33;
3
3
 
4
+ // Aggregator: re-exports all block stream primitives (Cursors, Writers, Schema, Keys).
5
+ // Import this file to get access to the full block encoding/decoding surface in one import.
6
+
4
7
  import { HostAmount, UserAmount, HostAsset, Tx, AssetAmount } from "./blocks/Schema.sol";
5
8
  import { Keys } from "./blocks/Keys.sol";
6
9
  import { Schemas } from "./blocks/Schema.sol";
7
- import { Block, BlockPair, Blocks } from "./blocks/Blocks.sol";
8
- import { Mem, MemRef } from "./blocks/Mem.sol";
10
+ import { Cursors, Cur } from "./blocks/Cursors.sol";
9
11
  import { Writer, Writers } from "./blocks/Writers.sol";
12
+
13
+
14
+
15
+
@@ -1,6 +1,9 @@
1
1
  // SPDX-License-Identifier: GPL-3.0-only
2
2
  pragma solidity ^0.8.33;
3
3
 
4
+ // Aggregator: re-exports all event contracts.
5
+ // Import this file to get access to every event emitter in one import.
6
+
4
7
  import { AccessEvent } from "./events/Access.sol";
5
8
  import { AssetEvent } from "./events/Asset.sol";
6
9
  import { BalanceEvent } from "./events/Balance.sol";
@@ -16,3 +19,6 @@ import { PeerEvent } from "./events/Peer.sol";
16
19
  import { QuoteEvent } from "./events/Quote.sol";
17
20
  import { RootZeroEvent } from "./events/RootZero.sol";
18
21
  import { WithdrawalEvent } from "./events/Withdraw.sol";
22
+
23
+
24
+
@@ -1,12 +1,18 @@
1
1
  // SPDX-License-Identifier: GPL-3.0-only
2
2
  pragma solidity ^0.8.33;
3
3
 
4
- import { Channels } from "./utils/Channels.sol";
4
+ // Aggregator: re-exports all utility libraries (State, Accounts, Assets, ECDSA, Ids, Layout, Utils, Value).
5
+ // Import this file to access the full utility surface without managing individual paths.
6
+
7
+ import { State } from "./utils/State.sol";
5
8
  import { Accounts } from "./utils/Accounts.sol";
6
9
  import { Amounts, Assets } from "./utils/Assets.sol";
7
10
  import { ECDSA } from "./utils/ECDSA.sol";
8
11
  import { Ids, Selectors } from "./utils/Ids.sol";
9
12
  import { Layout } from "./utils/Layout.sol";
10
13
  import { Schemas } from "./blocks/Schema.sol";
11
- import { addrOr, applyBps, beforeBps, bytes32ToString, isFamily, isLocal, isLocalFamily, matchesBase, MAX_BPS, max8, max16, max24, max32, max40, max64, max96, max128, max160, toLocalBase, toLocalFamily, toUnspecifiedBase, ValueOverflow } from "./utils/Utils.sol";
14
+ import { addrOr, applyBps, beforeBps, bytes32ToString, divisible, isFamily, isLocal, isLocalFamily, matchesBase, MAX_BPS, max8, max16, max24, max32, max40, max64, max96, max128, max160, NotDivisible, toLocalBase, toLocalFamily, toUnspecifiedBase, ValueOverflow } from "./utils/Utils.sol";
12
15
  import { Values } from "./utils/Value.sol";
16
+
17
+
18
+