# Smart Contract Architecture

## Overview

MemeDice’s smart contract system consists of multiple contracts, deployed using the factory pattern, uses CREATE2 to achieve deterministic addresses, and uses the Vault pattern to enable efficient revenue distribution to shareholders.

**Only part of the technical architecture is currently disclosed; more complete technical architecture will be gradually published...**

***

## Vault Share Mode

MemeDice’s shareholder system uses a Vault-style global accumulator pattern to achieve O(1) gas revenue distribution.

### Why is it efficient?

| Operation              | Gas Complexity | Description                    |
| ---------------------- | -------------- | ------------------------------ |
| Player bet settlement  | O(1)           | Only updates poolBalance       |
| Shareholder deposit    | O(1)           | Updates shares and totalShares |
| Shareholder withdrawal | O(1)           | Updates shares and totalShares |

**Key insight**: When the game settles, there is no need to iterate through the shareholder list; only the pool balance needs to be updated. The actual value of each shareholder is calculated dynamically when queried.

For details, please refer to [Shareholder system explained in detail](/dapp.meme-en/provide-liquidity-lp/detailed-explanation-of-the-shareholder-system.md).

***

## Game contract state

More technical details will be gradually disclosed and updated...

***

## Contract interaction flow

More technical details will be gradually disclosed and updated...

***

## Security design summary

| Design                   | Purpose                                            |
| ------------------------ | -------------------------------------------------- |
| **Invariants**           | Key parameters cannot be modified after deployment |
| **ReentrancyGuard**      | Prevents reentrancy attacks                        |
| **SafeERC20**            | Secure token operations                            |
| **pendingLockedAmount**  | Prevents the pool from overpaying                  |
| **BET\_TIMEOUT**         | Prevents funds from being permanently locked       |
| **MAX\_FEE\_BPS**        | Fee cap protection                                 |
| **onlyEOA**              | Prevents contract attacks (Prevrandao pattern)     |
| **CREATE2 verification** | Ensures the address suffix is correct              |
| **Event logs**           | Fully transparent operation records                |

***

## Related links

* ⬅️ Back [Technical Deep Dive Overview](/dapp.meme-en/technical-overview/technical-deep-dive-overview.md)
* 🔒 Learn about [Game Security](/dapp.meme-en/technical-overview/game-security.md)
* 💸 Learn [Fee System](/dapp.meme-en/technical-overview/detailed-explanation-of-the-fee-system.md)
* 📐 Learn [Shareholder system](/dapp.meme-en/provide-liquidity-lp/detailed-explanation-of-the-shareholder-system.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.dapp.meme/dapp.meme-en/technical-overview/smart-contract-architecture.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
