@verify-network/contracts 0.1.0 → 0.1.1

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 (2) hide show
  1. package/README.md +115 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # @verify-network/contracts
2
+
3
+ What a consumer of the Verify Network router builds against. It holds the Solidity interfaces a consumer imports, the viem-ready ABIs of the router's facets, and the creation bytecode and diamond cut helpers for deploying the router yourself.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @verify-network/contracts viem
9
+ ```
10
+
11
+ `viem` (`^2.56.3`) is a peer dependency. Node 22 or later.
12
+
13
+ ## Solidity interfaces
14
+
15
+ | Import | Contents |
16
+ | -------------------------------------------------------------- | ------------------------------------------------------------------- |
17
+ | `@verify-network/contracts/contracts/IVerifyNetworkRouter.sol` | The router functions a consumer calls: the requests and the balance |
18
+ | `@verify-network/contracts/contracts/IVrfReceiver.sol` | The callback a consumer implements to receive randomness |
19
+ | `@verify-network/contracts/contracts/IDirectVrfReceiver.sol` | The callback a consumer implements to receive direct requests |
20
+
21
+ Hardhat 3 resolves these paths from `node_modules` as they are. Foundry needs one remapping:
22
+
23
+ ```
24
+ @verify-network/contracts/=node_modules/@verify-network/contracts/
25
+ ```
26
+
27
+ A minimal consumer:
28
+
29
+ ```solidity
30
+ // SPDX-License-Identifier: MIT
31
+ pragma solidity ^0.8.30;
32
+
33
+ import {IVerifyNetworkRouter} from "@verify-network/contracts/contracts/IVerifyNetworkRouter.sol";
34
+ import {IVrfReceiver} from "@verify-network/contracts/contracts/IVrfReceiver.sol";
35
+
36
+ contract Lottery is IVrfReceiver {
37
+ IVerifyNetworkRouter public immutable router;
38
+ address public immutable owner;
39
+
40
+ error OnlyRouter(address caller);
41
+
42
+ constructor(IVerifyNetworkRouter router_) {
43
+ router = router_;
44
+ owner = msg.sender;
45
+ }
46
+
47
+ /// Requests from this contract's balance on the router. The round travels as callback data.
48
+ function draw(uint256 round) external returns (bytes32 requestId) {
49
+ requestId = router.requestRandomness(abi.encode(round), 200_000);
50
+ }
51
+
52
+ /// The router calls this with exactly the callback gas limit of the request.
53
+ function onRandomnessFulfilled(bytes32, bytes32 randomness, bytes calldata callbackData) external {
54
+ if (msg.sender != address(router)) revert OnlyRouter(msg.sender);
55
+ uint256 round = abi.decode(callbackData, (uint256));
56
+ // settle `round` with `randomness`
57
+ }
58
+
59
+ /// Refunds of stale requests land on this contract's balance on the router.
60
+ function withdrawBalance(address recipient, uint256 amount) external {
61
+ require(msg.sender == owner);
62
+ router.withdraw(recipient, amount);
63
+ }
64
+ }
65
+ ```
66
+
67
+ Fund it once with `router.deposit(address(lottery))`. It pays for each request from that balance. Two rules apply to every consumer:
68
+
69
+ - Only the router may call the callback. Nothing else authenticates the randomness.
70
+ - Give the consumer a way to withdraw. A stale request's refund goes to the consumer's balance on the router, even when the request was paid with `msg.value`.
71
+
72
+ The router stores only the request id. Your callback data is hashed into it and returned to your callback verbatim, so a consumer may keep its whole pending state there, up to 16 KiB.
73
+
74
+ ## ABIs
75
+
76
+ Each ABI is the default export of `@verify-network/contracts/abi/<Contract>`, typed `as const` for viem:
77
+
78
+ | ABI | Use |
79
+ | ---------------------- | -------------------------------------------------------------------------- |
80
+ | `VrfFacet` | Requests, fulfillments, stale reports, challenges, and the router's events |
81
+ | `BalancesFacet` | `deposit`, `withdraw`, `balanceOf` |
82
+ | `ConfigFacet` | `pricing()`, the stake, and the admin's settings |
83
+ | `NodesFacet` | The active set, node keys (`publicKeyOf`), and endpoints (`endpointOf`) |
84
+ | `IVerifyNetworkRouter` | The consumer's subset of the router |
85
+ | `IVrfReceiver` | The on-chain request callback |
86
+ | `IDirectVrfReceiver` | The direct request callback |
87
+
88
+ The router is an EIP-2535 diamond, so every facet lives at the router's address.
89
+
90
+ ```ts
91
+ import BalancesFacet from "@verify-network/contracts/abi/BalancesFacet";
92
+ import { createPublicClient, http } from "viem";
93
+ import { base } from "viem/chains";
94
+
95
+ const publicClient = createPublicClient({ chain: base, transport: http() });
96
+ const balance = await publicClient.readContract({
97
+ address: "0x…", // the router
98
+ abi: BalancesFacet,
99
+ functionName: "balanceOf",
100
+ args: ["0x…"], // the consumer
101
+ });
102
+ console.log(balance);
103
+ ```
104
+
105
+ ## Deploy the router yourself
106
+
107
+ `@verify-network/contracts/artifacts` holds the ABI and creation bytecode of every contract a router deployment needs: `Router`, `RouterInit`, `ECVRFVerifier`, `TrustedNodeEnclaveKeyVerifier`, and the facets in `FACETS`. Each one is in the shape viem's `deployContract` takes.
108
+
109
+ `@verify-network/contracts/diamond` holds the EIP-2535 helpers. `DIAMOND_ACTIONS` and `facetSelectors` build the first `diamondCut`. `planFacetCut` and `facetCuts` plan an in-place upgrade of one facet from the diamond loupe. They refuse a selector that another facet already serves.
110
+
111
+ To test a consumer against a local router, use `@verify-network/local`. It deploys these artifacts and runs a node that fulfills requests.
112
+
113
+ ## License
114
+
115
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verify-network/contracts",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "The Verify Network router a consumer builds against: viem-ready ABIs, the creation bytecode a local deploy of the router diamond takes, diamond cut helpers, and the Solidity consumer interfaces.",
5
5
  "license": "MIT",
6
6
  "files": [