Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit 9a2cd2b

Browse files
manual mirror done (#126)
1 parent 0495c85 commit 9a2cd2b

File tree

265 files changed

+48907
-15972
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

265 files changed

+48907
-15972
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@
1818
npm-debug.log*
1919
yarn-debug.log*
2020
yarn-error.log*
21+
22+
/.github
23+
/.husky

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ There exist four separate sets of docs contained in the following folders.
4141
* `./nil`
4242
* `./crypto3`
4343

44-
All four sets of docs are rendered as a single React app configured in `docusaurus.config.js`.
44+
Note that only =nil; docs are rendered when running the Docusaurus instance. The rest only exist as collections of raw `.md` files.
4545

4646
# Contributing
4747

babel.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
2+
presets: [require.resolve('@docusaurus/core/lib/babel/preset'), "@babel/preset-env", "@babel/preset-typescript"],
33
};

docs/api/index.md

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
11
# Documentation Repository for =nil;
22

3-
This repository is the host of the Docusaurus project that handles documentation for =nil; Foundation.
4-
5-
# Structure
6-
7-
The project follows the monorepo pattern to simplify dependency management.
8-
9-
There exist four separate sets of docs in the following folders.
10-
11-
* `./zkllvm`
12-
* `./proof-market`
13-
* `./zksharding`
14-
* `./crypto3`
15-
16-
Each set of docs is connected to a single React app which is configured in the `docusaurus.config.js` file.
3+
This repository is the host of the Docusaurus project that handles documentation for =nil;.

docs/api/typedoc-sidebar.cjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// @ts-check
22
/** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */
3-
const typedocSidebar = { items: []};
4-
module.exports = typedocSidebar.items;
3+
const typedocSidebar = { items: [] };
4+
module.exports = typedocSidebar.items;

docusaurus.config.js

+40-43
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { themes as prismThemes } from 'prism-react-renderer';
88

99
import remarkMath from 'remark-math';
1010
import rehypeKatex from 'rehype-katex';
11+
import remarkCodeSnippets from 'nil-remark-code-snippets';
1112

1213
/** @type {import('@docusaurus/types').Config} */
1314
const config = {
@@ -26,8 +27,9 @@ const config = {
2627
organizationName: '=nil; Foundation', // Usually your GitHub org/user name.
2728
projectName: 'docs.nil.foundation', // Usually your repo name.
2829

29-
onBrokenLinks: 'throw',
30+
onBrokenLinks: 'log',
3031
onBrokenMarkdownLinks: 'warn',
32+
onBrokenAnchors: 'log',
3133

3234
// Even if you don't use internationalization, you can use this field to set
3335
// useful metadata like html lang. For example, if your site is Chinese, you
@@ -46,10 +48,10 @@ const config = {
4648
path: "nil",
4749
routeBasePath: "nil",
4850
sidebarPath: require.resolve("./sidebar-nil.js"),
49-
remarkPlugins: [remarkMath],
51+
remarkPlugins: [remarkMath, remarkCodeSnippets],
5052
rehypePlugins: [rehypeKatex],
5153
openrpc: {
52-
openrpcDocument: "https://api.devnet.nil.foundation/api/docs/openrpc.json",
54+
openrpcDocument: process.env.OPENRPC_JSON,
5355
path: "references",
5456
sidebarLabel: "JSON-RPC API",
5557
},
@@ -68,43 +70,17 @@ const config = {
6870
'docusaurus-plugin-goatcounter'
6971
,
7072
[
71-
'@docusaurus/plugin-content-docs',
72-
{
73-
id: 'zkllvm',
74-
path: 'zkllvm',
75-
routeBasePath: 'zkllvm',
76-
sidebarPath: './sidebar-zkllvm.js'
77-
}
78-
],
79-
[
80-
'@docusaurus/plugin-content-docs',
81-
{
82-
id: 'proof-market',
83-
path: 'proof-market',
84-
routeBasePath: 'proof-market',
85-
sidebarPath: './sidebar-proof-market.js'
86-
}
87-
],
88-
[
89-
'@docusaurus/plugin-content-docs',
90-
{
91-
id: 'crypto3',
92-
path: 'crypto3',
93-
routeBasePath: 'crypto3',
94-
sidebarPath: './sidebar-crypto-3.js'
95-
}
96-
],
97-
[
98-
'docusaurus-plugin-typedoc',
73+
'nil-docusaurus-plugin-typedoc',
9974
{
10075
out: "./nil/reference/client",
10176
outputFileStrategy: "members",
10277
fileExtension: ".mdx",
10378
useCodeBlocks: true,
10479
parametersFormat: "htmlTable",
10580
entryPoints: [
106-
"./nil/nil.js/src"
81+
'node_modules/@nilfoundation/niljs/dist/niljs.cjs'
10782
],
83+
tsconfig: `tsconfig.json`,
10884
skipErrorChecking: true,
10985
sidebar: {
11086
"autoConfiguration": true,
@@ -160,26 +136,47 @@ const config = {
160136
},
161137
items: [
162138
{
139+
label: 'Documentation',
163140
position: 'left',
164-
label: '=nil;',
165141
to: '/nil/intro'
166142
},
167143
{
144+
label: 'Cookbook',
168145
position: 'left',
169-
label: 'zkLLVM',
170-
to: '/zkllvm/overview/what-is-zkllvm'
171-
146+
to: '/nil/cookbook'
172147
},
173148
{
149+
label: 'Migration guides',
174150
position: 'left',
175-
label: 'Proof Market',
176-
to: '/proof-market/intro'
151+
to: 'nil/migration-guides/september-1709-2024-release'
177152
},
178153
{
179-
position: 'left',
180-
label: 'Crypto3',
181-
to: '/crypto3/intro'
182-
},
154+
type: 'dropdown',
155+
label: 'Developer tools',
156+
position: 'right',
157+
items: [
158+
{
159+
label: 'Block explorer',
160+
href: 'https://explore.nil.foundation/'
161+
},
162+
{
163+
label: 'Solidity extension',
164+
href: 'https://github.com/NilFoundation/nil_cli/blob/master/Nil.sol'
165+
},
166+
{
167+
label: '=nil; CLI',
168+
href: 'https://github.com/NilFoundation/nil_cli/tree/master'
169+
},
170+
{
171+
label: 'Client library',
172+
href: 'https://www.npmjs.com/package/@nilfoundation/niljs'
173+
},
174+
{
175+
label: 'Hardhat plugin',
176+
href: 'https://github.com/NilFoundation/nil-hardhat-example'
177+
}
178+
]
179+
}
183180
],
184181
},
185182
footer: {
@@ -214,4 +211,4 @@ const config = {
214211
}),
215212
};
216213

217-
export default config;
214+
export default config;

nil/cookbook/index.mdx

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {CookbookCard, CookbookCardSection, CookbookBadges} from '@site/src/components/CookbookCardSection';
2+
3+
# Create a swap contract and custom tokens
4+
5+
This series of recipes show how to create a swap contract that can reliably exchange one custom currency with another even if the contracts representing these currencies are located on different shards.
6+
7+
The swap contract adopts order book-like mechanics by matching swap requests from different wallets. When requests are matched, the contract processes them and takes care of any excess tokens by sending them back to the request originators.
8+
9+
<CookbookCardSection>
10+
<CookbookCard badges={[CookbookBadges[0]]} tag={{
11+
label: "8 min read",
12+
}} id='swap1' title='Design the swap contract' description='' to='./cookbook/swap/create-swap-contract'
13+
/>
14+
<CookbookCard badges={[CookbookBadges[2]]} tag={{
15+
label: "7 min read",
16+
}} id='swap2' title='Deploy and test the swap contract' description='' to='./cookbook/swap/deploy-test-swap'/>
17+
</CookbookCardSection>
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Create the swap contract
2+
3+
The swap contract needs to do the following:
4+
5+
* Accept swap requests
6+
* Match swap requests and exchange tokens
7+
* Handle excess tokens and send them back to wallets located on different shards
8+
9+
## Contract definition
10+
11+
```solidity showLineNumbers
12+
13+
pragma solidity ^0.8.26;
14+
15+
import "@nilfoundation/smart-contracts/contracts/Nil.sol";
16+
17+
/**
18+
* @title SwapMatch
19+
* @author =nil; Foundation
20+
* @notice The contract matches swap requests and performs token swaps.
21+
* On a successful match, the desired tokens are sent to the swap originators.
22+
* The contract also calculates token excesses and sends them back on a successful match.
23+
*/
24+
contract SwapMatch is NilBase {}
25+
```
26+
27+
## Structs and properties
28+
29+
The contract contains the following structs and properties:
30+
31+
```solidity showLineNumbers file=../../../tests/SwapMatch.sol start=startContractStructsAndProperties end=endContractStructsAndProperties
32+
```
33+
34+
* The `Swap` struct stores data about a token exchange that has been processed successfuly. It shows both parties that participated in the swap as well as information about the associated tokens
35+
* The `SwapRequest` struct represents a single request for a token exchange coming from one wallet (`initiator`). It contains the token the wallet wants to exchange as well as the ID of the desired token and its amount
36+
* The `swaps` and `swapRequests` maps store recorded swaps and swap requests using the current counter values as keys
37+
38+
## Contract events
39+
40+
The contract emits the following events
41+
42+
```solidity showLineNumbers file=../../../tests/SwapMatch.sol start=startContractEvents end=endContractEvents
43+
```
44+
45+
## Placing swap requests
46+
47+
Wallets can place new swap requests by calling this function:
48+
49+
```solidity showLineNumbers file=../../../tests/SwapMatch.sol start=startPlaceSwapRequest end=endPlaceSwapRequest
50+
```
51+
52+
The function initializes a new `SwapRequest` and increases the swap request counter. It also starts the matching process.
53+
54+
## Matching swap requests
55+
56+
The contract matches swap requests using this function:
57+
58+
```solidity showLineNumbers file=../../../tests/SwapMatch.sol start=startMatchSwapRequests end=endMatchSwapRequests
59+
```
60+
61+
The function iterates over all recorded swap requests and finds a suitable partner for the new swap request. This is done based on four conditions:
62+
63+
* The 'candidate' swap request must not be matched yet
64+
* The 'candidate' swap request must contain at least the same amount of tokens as desired in the swap request being matched
65+
* The 'candidate' swap request is asking for the same token as provided in the swap request being matched
66+
* The swap request being matched can provide at least the same amount of tokens as desired by the 'candidate' swap request
67+
68+
If all conditions are satisfied, the contract begins sending tokens. It also initializes a new `Swap` object and updates the relevant map and counter.
69+
70+
## Sending tokens and handling excesses
71+
72+
The contract sends tokens and makes sure that both parties in a swap receive back any 'excess' tokens that could not be exchanged:
73+
74+
```solidity showLineNumbers file=../../../tests/SwapMatch.sol start=startSendTokensAndProcessExcess end=endSendTokensAndProcessExcess
75+
```
76+
77+
The function calculates the possible token excesses:
78+
79+
* The difference between the amount of tokens desired by the matched swap request and the new swap request
80+
* The difference between the amount of tokens desired by the new swap request and the matched swap request
81+
82+
The tokens are transformed into token arrays and sent to the originators of swap requests using `Nil.asyncCall()`. All refunds are also sent to these addresses.
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Deploy and test the swap contract
2+
3+
This test of the `SwapMatch` contract does the follows:
4+
5+
* Creates two new wallets
6+
* Deploys the contract
7+
* Mints two currencies for these wallets
8+
* Places two swap requests (one from Wallet 1 and another from Wallet 2)
9+
* Evaluates whether the swap requests were matched and if excesses were returned correctly
10+
11+
## Create two new wallets
12+
13+
```js showLineNumbers file=../../../tests/cookbook-swap.test.mjs start=startTwoNewWalletsDeploy end=endTwoNewWalletsDeploy
14+
```
15+
16+
## Deploy the contract
17+
18+
```js showLineNumbers file=../../../tests/cookbook-swap.test.mjs start=startDeploymentOfSwapMatch end=endDeploymentOfSwapMatch
19+
```
20+
21+
## Mint two new currencies
22+
23+
```js showLineNumbers file=../../../tests/cookbook-swap.test.mjs start=startCurrencyCreation end=endCurrencyCreation
24+
```
25+
26+
## Place the first swap request
27+
28+
```js showLineNumbers file=../../../tests/cookbook-swap.test.mjs start=startFirstSendRequest end=endFirstSendRequest
29+
```
30+
31+
## Place the second swap request
32+
33+
```js showLineNumbers file=../../../tests/cookbook-swap.test.mjs start=startSecondSendRequest end=endSecondSendRequest
34+
```
35+
36+
## Check the token amounts
37+
38+
```js showLineNumbers file=../../../tests/cookbook-swap.test.mjs start=startFinalChecks end=endFinalChecks
39+
```
40+
41+
Expected output:
42+
43+
```
44+
Wallet 1 tokens: {
45+
TOKEN_ONE_ID: 90000000n,
46+
TOKEN_TWO_ID: 20000000n
47+
}
48+
Wallet 2 tokens: {
49+
TOKEN_ONE_ID: 10000000n,
50+
TOKEN_TWO_ID: 80000000n
51+
}
52+
```

0 commit comments

Comments
 (0)