EIPs Explained

ERC-2746 · Stagnant · ERC · 2020-06-20

ERC-2746: Rules Engine Standard

An interface for a deploy-once 'rules engine' contract where users register data attributes and build trees of rules that can be executed on-chain without writing new Solidity.

What problem does it solve?

Adding any new on-chain logic normally means writing, auditing and deploying a new contract. That is a high bar for simple business logic like "if the token amount is under 10, do X", and it shuts out less technical teams.

ERC-2746 proposes a different model: deploy one rules engine contract with a known data domain and a fixed catalog of rule types. After that, users configure behaviour by adding rules through function calls, and a no-code front end can drive the whole thing.

How it works

The standard defines four concepts:

  • Attribute: a named data point in the engine's domain (for example TokenAmt). Its value can live inside the engine or be fetched from another contract.
  • Rule: one predefined operation on one attribute, compared against a right-hand value. Example: TokenAmt < 10. Rules can be negated with a NOT flag.
  • RuleSet: a group of rules combined with AND or OR. Its boolean result decides whether its child RuleSets run.
  • RuleTree: a hierarchy of RuleSets owned by one address, executed as a single transaction.

The interface ERCRulesEngine includes:

Function Purpose
addAttribute(...) Register a data point (owner only)
addRuleTree(owner, name, desc) Create a tree for an owner
addRuleSet(owner, name, desc, parent, severeFailFlag, useAndOp, failQuickFlag) Add a node to the tree
addRule(owner, ruleSet, ruleName, attr, ruleType, rightHandValue, notFlag) Add a rule to a set
executeRuleTree(owner) Run the whole tree, returns bool
getRuleProps, getRuleSetProps, getRuleTreeProps Inspect configuration
removeRuleTree(owner) Delete a tree

Events CallRuleTree, CallRuleSet, CallRule and RuleSetError let callers trace which paths ran and which sets failed. The right-hand value is passed as a string so one function can serve numeric and text rules.

Sample code

Configuring and running a simple tree (ethers v6; the rule type ID depends on the implementation):

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);

Things to know

  • Status is Stagnant. The reference implementation is Nethereum's Wonka engine; there is no wider ecosystem adoption.
  • The interface was written for Solidity ^0.6.0; signatures may need updating for current compilers.
  • Gas cost of executeRuleTree depends on how many rules actually run, so it is hard to predict. The spec suggests (but does not standardise) functions that report min/max cost.
  • Only the tree owner or the contract admin should be allowed to execute a tree. If attributes read from other contracts, the implementation must handle access rights carefully.
  • The spec mentions the Diamond pattern (ERC-2535) as one possible source for attribute data.

#rules-engine #low-code #interface #erc

Details

Authors
Aaron Kendall (@jaerith), Juan Blanco (@juanfranblanco)
Status
Stagnant
Type
Standards Track / ERC
Created
2020-06-20

Machine-readable: Markdown · JSON