---
eip: 2746
label: ERC-2746
title: Rules Engine Standard
status: Stagnant
type: Standards Track
category: ERC
created: 2020-06-20
official: https://eips.ethereum.org/EIPS/eip-2746
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-2746.md
canonical: https://eips.huatuo.xyz/zh/eip-2746/
lang: zh
tldr: "一个\"一次部署、长期使用\"的规则引擎合约接口：用户注册数据属性并构建规则树，无需编写新的 Solidity 即可在链上执行。"
tags: [rules-engine, low-code, interface, erc]
---

# ERC-2746: Rules Engine Standard

> An interface for using a smart contract as a rules engine. A single deployed contract can register a data domain, create sets of rules that perform actions on that domain, and then invoke a set as an atomic transaction.

**TL;DR:** 一个"一次部署、长期使用"的规则引擎合约接口：用户注册数据属性并构建规则树，无需编写新的 Solidity 即可在链上执行。

## 它解决什么问题？

添加任何新的链上逻辑，通常都意味着编写、审计和部署一个新合约。对于"如果代币数量小于 10，就做 X"这类简单的业务逻辑，这个门槛太高，也把技术能力较弱的团队挡在门外。

ERC-2746 提出了另一种模式：部署一个规则引擎合约，具有已知的数据域和固定的规则类型目录。之后，用户通过函数调用添加规则来配置行为，无代码前端可以驱动整个流程。

## 它如何运作

该标准定义了四个概念：

- **Attribute（属性）**：引擎数据域中一个有名字的数据点（例如 `TokenAmt`）。它的值可以存放在引擎内部，也可以从其他合约获取。
- **Rule（规则）**：对某个属性的一个预定义操作，并与右侧的值比较。例如：`TokenAmt < 10`。规则可以用 NOT 标志取反。
- **RuleSet**：用 AND 或 OR 组合的一组规则。其布尔结果决定其子 RuleSet 是否运行。
- **RuleTree**：由一个地址拥有的 RuleSet 层级结构，作为一笔交易执行。

接口 `ERCRulesEngine` 包括：

| 函数 | 用途 |
| --- | --- |
| `addAttribute(...)` | 注册一个数据点（仅所有者） |
| `addRuleTree(owner, name, desc)` | 为某个所有者创建一棵树 |
| `addRuleSet(owner, name, desc, parent, severeFailFlag, useAndOp, failQuickFlag)` | 向树中添加一个节点 |
| `addRule(owner, ruleSet, ruleName, attr, ruleType, rightHandValue, notFlag)` | 向集合中添加一条规则 |
| `executeRuleTree(owner)` | 运行整棵树，返回 `bool` |
| `getRuleProps`、`getRuleSetProps`、`getRuleTreeProps` | 查看配置 |
| `removeRuleTree(owner)` | 删除一棵树 |

`CallRuleTree`、`CallRuleSet`、`CallRule` 和 `RuleSetError` 事件让调用方能够追踪运行了哪些路径、哪些集合失败。右侧的值以 `string` 传入，因此一个函数即可同时服务数值规则和文本规则。

## 示例代码

配置并运行一棵简单的树（ethers v6；规则类型 ID 取决于具体实现）：

```ts
import { Contract, encodeBytes32String as b32 } from "ethers";

const engine = new Contract(engineAddress, [
  "function addRuleTree(address,bytes32,string)",
  "function addRuleSet(address,bytes32,string,bytes32,bool,bool,bool)",
  "function addRule(address,bytes32,bytes32,bytes32,uint256,string,bool)",
  "function executeRuleTree(address) returns (bool)",
], signer);

const me = await signer.getAddress();
await engine.addRuleTree(me, b32("Checks"), "Validate an order");
await engine.addRuleSet(me, b32("Root"), "Amount check", b32(""), true, true, false);
const LESS_THAN = 1; // implementation-defined rule type
await engine.addRule(me, b32("Root"), b32("AmtUnder10"), b32("TokenAmt"), LESS_THAN, "10", false);

const ok = await engine.executeRuleTree.staticCall(me);
```

## 需要注意

- 状态为 **Stagnant**。参考实现是 Nethereum 的 Wonka 引擎；没有更广泛的生态采用。
- 该接口是为 Solidity `^0.6.0` 编写的；用于当前编译器时签名可能需要更新。
- `executeRuleTree` 的 gas 成本取决于实际运行了多少条规则，因此难以预测。规范建议（但并未标准化）提供报告最小/最大成本的函数。
- 只应允许树的所有者或合约管理员执行树。如果属性是从其他合约读取的，实现必须谨慎处理访问权限。
- 规范提到 Diamond 模式（[ERC-2535](/eip-2535/)）可作为属性数据的来源之一。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-2746
