MUD Quickstart with Nethereum
MUD is an enhanced EIP-2535 Diamond pattern for building complex smart contract applications. A World contract acts as a diamond proxy — routing calls to system contracts via delegatecall, with all state stored in typed tables. Namespaces group systems and tables into a unified context (like web3.Eth). It's not just for games — any application with complex structured on-chain state benefits.
When to Use This Skill
- User wants to build a MUD application with .NET/C#
- User needs to generate C# code from MUD table definitions
- User is working with MUD World contracts, tables, or systems
- User mentions
defineWorld,mud.config.ts, or MUD namespaces
Required Packages
dotnet add package Nethereum.Mud
dotnet add package Nethereum.Mud.Contracts
For code generation CLI:
dotnet tool install -g Nethereum.Generator.Console
Code Generation Workflow
- Define tables in
mud.config.tsusingdefineWorld()at mud.dev - Compile contracts with Forge
- Create
.nethereum-gen.multisettings:
[
{
"paths": ["path/to/mud.config.ts"],
"generatorConfigs": [
{
"baseNamespace": "MyProject.Contracts",
"basePath": "Generated/Tables",
"codeGenLang": 0,
"generatorType": "MudTables"
}
]
}
]
- Run:
Nethereum.Generator.Console generate from-config
Generator types:
MudTables— generates TableRecord and TableService from mud.config.tsMudExtendedService— generates system service wrappers from compiled JSONContractDefinition— standard ABI-to-C# (non-MUD contracts)
Using Generated Services
var playerService = new PlayerTableService(web3, worldAddress);
// Read
var player = await playerService.GetTableRecordAsync(
new PlayerTableRecord.PlayerKey { Address = addr });
// Write
await playerService.SetRecordRequestAndWaitForReceiptAsync(
new PlayerTableRecord.PlayerKey { Address = addr },
new PlayerTableRecord.PlayerValue { Score = 100, Name = "Alice" });
// Delete
await playerService.DeleteRecordRequestAndWaitForReceiptAsync(
new PlayerTableRecord.PlayerKey { Address = addr });
Namespace Pattern (Production)
Aggregate generated services into namespace classes:
public class AppNamespace : NamespaceBase<AppResource, AppSystems, AppTables>
{
public AppNamespace(IWeb3 web3, string contractAddress) : base(web3, contractAddress)
{
Systems = new AppSystems(web3, contractAddress);
Tables = new AppTables(web3, contractAddress);
}
}
// Usage
var app = new AppNamespace(web3, worldAddress);
var player = await app.Tables.Player.GetTableRecordAsync(key);
See CafeCosmos for a production example with 35+ tables.
Key Classes
| Class | Purpose |
|---|---|
TableRecord<TKey, TValue> |
Base for keyed table records |
TableRecordSingleton<TValue> |
Base for singleton table records (key: []) |
TableService<TRecord, TKey, TValue> |
Typed CRUD for keyed tables |
TableSingletonService<TRecord, TValue> |
Typed CRUD for singleton tables |
ResourceEncoder |
Encode table/system/namespace resource IDs |
NamespaceBase<TResource, TSystems, TTables> |
Base for namespace aggregation |
For full documentation, see: https://docs.nethereum.com/docs/mud-framework/guide-mud-quickstart