-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(guides/dictionary): first version #3044
Open
qbzzt
wants to merge
4
commits into
main
Choose a base branch
from
240816-dictionary
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,275 @@ | ||
import { Callout } from "nextra/components"; | ||
|
||
# MUD for Solidity Programmers | ||
|
||
To properly use MUD you need to do some things differently than the way you do them normally in Solidity. | ||
|
||
## State variables | ||
|
||
A `System` contract should not store any internal state, but only interact with `World` state via `Tables`. | ||
|
||
- By concentrating all the state information in a single contract (the `World`) we make it easier to apply [access control](/world/namespaces-access-control), [execute hooks when data changes](/store/store-hooks), etc. | ||
- It is a lot easier to [upgrade functionality](/cli/deploy#upgrading-a-world) when you do not need to deal with storage because the contract does not have a state. | ||
- The same `System` can be used by multiple `World`s. | ||
|
||
Instead of using [state variables](https://docs.soliditylang.org/en/latest/internals/layout_in_storage.html) you can achieve the same functionality with [tables](/store/tables). | ||
|
||
### Value-type (`string`, `uint`, etc.) | ||
|
||
[Value-type](https://docs.soliditylang.org/en/latest/types.html#value-types) state variables such as `bool`, `uint256`, and `string` are represented by a singleton table, a table with no key that only contains a single record. | ||
|
||
<details> | ||
|
||
<summary>Code example</summary> | ||
|
||
```solidity filename="Solidity" {2} | ||
contract Example { | ||
string name; | ||
} | ||
``` | ||
|
||
```typescript filename="MUD" {6-11} | ||
import { defineWorld } from "@latticexyz/world"; | ||
|
||
export default defineWorld({ | ||
namespace: "app", | ||
tables: { | ||
Name: { | ||
schema: { | ||
name: "string", | ||
}, | ||
key: [], | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
</details> | ||
|
||
### Structures | ||
|
||
A [`struct`](https://docs.soliditylang.org/en/latest/types.html#structs) state variable wraps multiple variables together. You can create a singleton table with multiple value fields. | ||
|
||
<details> | ||
|
||
<summary>Code example</summary> | ||
|
||
```solidity filename="Solidity" {2-7} | ||
contract Example { | ||
struct Person { | ||
string name; | ||
address personalWallet; | ||
uint balance; | ||
} | ||
Person deployer; | ||
} | ||
``` | ||
|
||
```typescript filename="MUD" {6-13} | ||
import { defineWorld } from "@latticexyz/world"; | ||
|
||
export default defineWorld({ | ||
namespace: "app", | ||
tables: { | ||
Deployer: { | ||
schema: { | ||
personalWallet: "address", | ||
balance: "uint256", | ||
name: "string", | ||
}, | ||
key: [], | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
</details> | ||
|
||
### Mappings | ||
|
||
A [mapping type](https://docs.soliditylang.org/en/latest/types.html#mapping-types) maps between a key type and a value type, which is pretty much the same as what MUD tables do. | ||
|
||
<details> | ||
|
||
<summary>Single key mapping</summary> | ||
|
||
```solidity filename="Solidity" {2} | ||
contract Example { | ||
mapping(address => uint) public balances; | ||
} | ||
``` | ||
|
||
```typescript filename="MUD" {6-12} | ||
import { defineWorld } from "@latticexyz/world"; | ||
|
||
export default defineWorld({ | ||
namespace: "app", | ||
tables: { | ||
Balances: { | ||
schema: { | ||
owner: "address", | ||
balance: "uint256", | ||
}, | ||
key: ["owner"], | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
</details> | ||
|
||
<details> | ||
|
||
<summary>Multiple key mapping</summary> | ||
|
||
```solidity filename="Solidity" {2} | ||
contract Example { | ||
mapping(address => mapping(address => uint256)) public allowances; | ||
} | ||
``` | ||
|
||
```typescript filename="MUD" {6-13} | ||
import { defineWorld } from "@latticexyz/world"; | ||
|
||
export default defineWorld({ | ||
namespace: "app", | ||
tables: { | ||
Allowances: { | ||
schema: { | ||
owner: "address", | ||
spender: "address", | ||
balance: "uint256", | ||
}, | ||
key: ["owner", "spender"], | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
</details> | ||
|
||
<Callout type="info" emoji="📝"> | ||
MUD key fields have to be fixed-length. | ||
This means that some mappings, for example `mapping(string => uint) balances;`, cannot be directly translated to a MUD table. | ||
|
||
An easy workaround is to use the hash of the string as the key instead of the string itself (which is exactly what happens under the hood of vanilla Solidity with the keys of a mapping). | ||
|
||
</Callout> | ||
|
||
### Arrays | ||
|
||
There are two ways in which MUD supports arrays. | ||
|
||
- Arrays of fixed-length value types, such as `uint24[]` or `address[]`, [are supported as table fields](https://github.com/latticexyz/mud/blob/main/packages/schema-type/src/solidity/SchemaType.sol#L108-L205). | ||
|
||
<details> | ||
|
||
{" "} | ||
|
||
<summary>Example</summary> | ||
|
||
```solidity filename="Solidity" {2} | ||
contract Example { | ||
bool[] listOfBooleans; | ||
} | ||
``` | ||
|
||
```typescript filename="MUD" {6-11} | ||
import { defineWorld } from "@latticexyz/world"; | ||
|
||
export default defineWorld({ | ||
namespace: "app", | ||
tables: { | ||
ListOfBooleans: { | ||
schema: { | ||
values: "bool[]", | ||
}, | ||
key: [], | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
</details> | ||
|
||
- An array is a mapping between an unsigned integer and the value type of the array. | ||
We can use a table with `index` as the key. That way, we can get around the limitation that arrays of variable-length types such as `string` or `bytes` are not supported. | ||
|
||
<details> | ||
|
||
{" "} | ||
|
||
<summary>Example</summary> | ||
|
||
```solidity filename="Solidity" {2} | ||
contract Example { | ||
string[] listOfNames; | ||
} | ||
``` | ||
|
||
```typescript filename="MUD" {6-12} | ||
import { defineWorld } from "@latticexyz/world"; | ||
|
||
export default defineWorld({ | ||
namespace: "app", | ||
tables: { | ||
ListOfNames: { | ||
schema: { | ||
index: "uint256", | ||
name: "string", | ||
}, | ||
key: ["index"], | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
</details> | ||
|
||
## Special variables | ||
|
||
Applications never call a `System` directly. | ||
All calls go through a `World` for state, access control, [balance management](/world/balance), etc. | ||
As a result, some of the [Solidity special variables](https://docs.soliditylang.org/en/latest/units-and-global-variables.html#special-variables-and-functions) have to replaced with functions that obtain the relevant information. | ||
|
||
| Meaning | Solidity | MUD | | ||
| ------------------------ | ------------ | -------------- | | ||
| Caller | `msg.sender` | `_msgSender()` | | ||
| Value sent with the call | `msg.value` | `_msgValue()` | | ||
|
||
{ | ||
|
||
// ## Events | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we delete this if we're not using it yet? |
||
|
||
// Normally we expect onchain-to-client communication to happen using [table synchronization](/guides/replicating-onchain-state), but sometimes it is useful to have a mechanism that is typical event handling, with events triggering code. | ||
|
||
// You shouldn't [`emit`](https://docs.soliditylang.org/en/latest/contracts.html#events) events from your `System`s. | ||
// Unless your `System` is in the [highly discouraged](/guides/best-practices/system-best-practices#avoid-the-root-namespace-if-possible) root namespace, events will come from the `System`'s address. | ||
// When you upgrade the `System` that address changes. | ||
// Instead, we recommend you use an offchain singleton table | ||
|
||
// To do this: | ||
|
||
// 1. In the `mud.config.ts` file create an [offchain](/store/tables#types-of-tables) singleton table. | ||
|
||
// `typescript | ||
// Event: { | ||
// schema: { | ||
// id: "bytes32", | ||
// message: "string", | ||
// }, | ||
// key: [], | ||
// type: "offchain", | ||
// }, | ||
// ` | ||
|
||
// 1. Add code to emit the event to any `System` call that needs it. | ||
|
||
// `solidity | ||
// Event.set(id, "new task"); | ||
// ` | ||
|
||
// 1. `Event` gets updated, same as all the other tables. However, here we cannot rely on these updates. | ||
// `Event` might end up being updated multiple times in the same block, and | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's with these? the space and indentation and formatting etc. feels off compared to the examples above