# 智能合约架构

## 概述

MemeDice 的智能合约体系由多个合约组成，采用工厂模式部署，使用 CREATE2 实现确定性地址，并通过 Vault 模式实现高效的股东收益分配。

**当前只公布部分技术架构，更多完整技术架构讲逐步公开...**

***

## Vault 份额模式

MemeDice 的股东系统使用 Vault 风格的全局累加器模式，实现 O(1) gas 的收益分配。

### 为什么高效？

| 操作     | Gas 复杂度 | 说明                      |
| ------ | ------- | ----------------------- |
| 玩家下注结算 | O(1)    | 仅更新 poolBalance         |
| 股东存款   | O(1)    | 更新 shares 和 totalShares |
| 股东提款   | O(1)    | 更新 shares 和 totalShares |

**关键洞察**：游戏结算时不需要遍历股东列表，只需更新资金池余额。每个股东的实际价值在查询时动态计算。

详细说明请参阅 [股东系统详解](/ti-gong-liu-dong-xing-lp/gu-dong-xi-tong-xiang-jie.md)。

***

## 游戏合约状态

更多技术细节将逐步公开和更新...

***

## 合约交互流程

更多技术细节将逐步公开和更新...

***

## 安全设计总结

| 设计                      | 目的                    |
| ----------------------- | --------------------- |
| **不可变量**                | 关键参数部署后不可修改           |
| **ReentrancyGuard**     | 防止重入攻击                |
| **SafeERC20**           | 安全的代币操作               |
| **pendingLockedAmount** | 防止资金池超额支付             |
| **BET\_TIMEOUT**        | 防止资金永久锁定              |
| **MAX\_FEE\_BPS**       | 费率上限保护                |
| **onlyEOA**             | 防止合约攻击（Prevrandao 模式） |
| **CREATE2 验证**          | 确保地址后缀正确              |
| **事件日志**                | 完全透明的操作记录             |

***

## 相关链接

* ⬅️ 返回 [技术深度概览](/ji-shu-gai-lan/ji-shu-shen-du-gai-lan.md)
* 🔒 了解 [游戏安全性](/ji-shu-gai-lan/you-xi-an-quan-xing.md)
* 💸 了解 [费用体系](/ji-shu-gai-lan/fei-yong-ti-xi-xiang-jie.md)
* 📐 了解 [股东系统](/ti-gong-liu-dong-xing-lp/gu-dong-xi-tong-xiang-jie.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/ji-shu-gai-lan/zhi-neng-he-yue-jia-gou.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.
