-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: "How MUD models data" guide (#2243)
- Loading branch information
Showing
15 changed files
with
208 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.table { | ||
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace; | ||
background-color: hsl(var(--nextra-primary-hue) 100% 66%/0.1); | ||
margin-top: 1em; | ||
text-align: center; | ||
} | ||
|
||
.table th, | ||
.table td { | ||
padding: 1em 1.5em; | ||
border-bottom: 1px solid rgba(0, 0, 0, 0.2); | ||
} | ||
|
||
.table th { | ||
color: white; | ||
background: hsl(var(--nextra-primary-hue) 100% 45%/0.5); | ||
} | ||
.table thead th { | ||
background: hsl(var(--nextra-primary-hue) 100% 45%/0.75); | ||
} | ||
|
||
.table tbody tr:hover th, | ||
.table tbody tr:hover td { | ||
backdrop-filter: brightness(1.5); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React, { ReactNode } from "react"; | ||
import styles from "./MUDTable.module.css"; | ||
|
||
type Props = { | ||
children: ReactNode; | ||
}; | ||
|
||
export function MUDTable({ children }: Props) { | ||
return <table className={styles.table}>{children}</table>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export default { | ||
"replicating-onchain-state": "Replicating onchain state", | ||
"hello-world": "Hello World", | ||
"extending-world": "Extending World", | ||
"extending-a-world": "Extending a World", | ||
emojimon: "Emojimon", | ||
}; |
2 changes: 1 addition & 1 deletion
2
docs/pages/guides/extending-world.mdx → docs/pages/guides/extending-a-world.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
export default { | ||
introduction: "Introduction", | ||
"data-model": "Data model", | ||
tables: "Tables", | ||
"table-libraries": "Table Libraries", | ||
"table-libraries": "Table libraries", | ||
encoding: "Encoding", | ||
"store-hooks": "Store Hooks", | ||
"store-hooks": "Store hooks", | ||
reference: "Reference", | ||
"table-intro": { display: "hidden" }, // imported | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { CollapseCode } from "../../components/CollapseCode"; | ||
import { MUDTable } from "../../components/MUDTable"; | ||
|
||
# How MUD models data | ||
|
||
The MUD framework helps you to model your onchain state in a way that enables building applications with familiar tooling, like relational databases. | ||
|
||
In your MUD config, you define data as a set of tables. Like relational databases, these tables have “columns” by way of two MUD concepts: a key schema and a value schema. Each one has a set of fields that map to Solidity types. | ||
|
||
## Defining tables | ||
|
||
Let's say we're making a game where players can move around a map. The definition for a `Position` table might look something like: | ||
|
||
<CollapseCode> | ||
|
||
```tsx filename="mud.config.ts" {3-11} showLineNumbers | ||
export default mudConfig({ | ||
tables: { | ||
Position: { | ||
keySchema: { | ||
player: "address", | ||
}, | ||
valueSchema: { | ||
x: "int32", | ||
y: "int32", | ||
}, | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
</CollapseCode> | ||
|
||
Translating the table definition above would look like this in a relational database: | ||
|
||
<MUDTable> | ||
<thead> | ||
<tr> | ||
<th>player</th> | ||
<th>x</th> | ||
<th>y</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<th>0x1234</th> | ||
<td>-5</td> | ||
<td>1</td> | ||
</tr> | ||
<tr> | ||
<th>0x5678</th> | ||
<td>3</td> | ||
<td>6</td> | ||
</tr> | ||
</tbody> | ||
</MUDTable> | ||
|
||
Because `player` in part of the key schema, we would have a unique/primary key constraint on `player`. | ||
|
||
Let's add another table for terrain at a given position on the map. MUD allows for multiple keys in the key schema and, like databases, we call these composite keys. | ||
|
||
<CollapseCode> | ||
|
||
```tsx filename="mud.config.ts" {12-20} showLineNumbers | ||
export default mudConfig({ | ||
tables: { | ||
Position: { | ||
keySchema: { | ||
player: "address", | ||
}, | ||
valueSchema: { | ||
x: "int32", | ||
y: "int32", | ||
}, | ||
}, | ||
Terrain: { | ||
keySchema: { | ||
x: "int32", | ||
y: "int32", | ||
}, | ||
valueSchema: { | ||
terrainType: "string", | ||
}, | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
</CollapseCode> | ||
|
||
Similarly, the relational database representation of that table would look like: | ||
|
||
<MUDTable> | ||
<thead> | ||
<tr> | ||
<th>x</th> | ||
<th>y</th> | ||
<th>terrainType</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<th>-1</th> | ||
<th>0</th> | ||
<td>grass</td> | ||
</tr> | ||
<tr> | ||
<th>0</th> | ||
<th>1</th> | ||
<td>tree</td> | ||
</tr> | ||
<tr> | ||
<th>1</th> | ||
<th>0</th> | ||
<td>grass</td> | ||
</tr> | ||
</tbody> | ||
</MUDTable> | ||
|
||
Because we have a composite key of `x` and `y`, we would have a unique/primary key constraint on the tuple of `(x, y)`. | ||
|
||
## Tables on chain | ||
|
||
Solidity and the EVM have much more limited data structures, so how can we express a relational database onchain? MUD takes the concept of tables and does a few things with them: | ||
|
||
- [encodes each table record as a key/value pair](./encoding) before storing onchain | ||
- [emits an event for each mutation](/guides/replicating-onchain-state) | ||
- [provides a typed Solidity libraries](./table-libraries) to abstract this away | ||
|
||
## Field types | ||
|
||
Key schema fields can use all of Solidity's static-length primitive types like `uint256`, `address`, `bool`. | ||
|
||
Value schema fields can use all of Solidity's static-length primitive types, arrays of those static-length primitive types, as well as and `string` and `bytes`. | ||
|
||
Enums and user types are also supported and automatically map down to their Solidity primitive types. | ||
|
||
More complex types like structs, `string[]`, and `bytes[]` are not yet supported. We'll cover the reasons why in our encoding/decoding guide. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters