ERC-1592 Transfer rule engine standard (Address and ERC20-compliant transfer rules)

Stagnant · Tokens · created 2018-11-09

An interface that lets you attach reusable modules of transfer rules, such as whitelisting, freezing, or KYC, to an ERC20 token, and query in advance whether a transfer would pass before sending it.

Why it is needed

ERC20 does not tell you why a transfer failed. When each token implements its own transfer restriction logic in its own way, it becomes harder for exchanges, wallets, and other contracts to figure out the reason, and when the rules are mixed into the token code, reusing or swapping them is cumbersome. Also, if there is no easy way to detect when a rule has changed, counterparties cannot adjust their expectations to the new conditions. This proposal aims to separate transfer rules into individual contracts apart from the token code and unify them under a common interface to make pre-validation and reuse easy.

What to implement

In depth

IRule: two check functions

IRule defines only two functions: isAddressValid(address) and isTransferValid(address _from, address _to, uint256 _amount). Both are external view, so they can be queried at no gas cost. If one of them is irrelevant to a given rule, it can simply be implemented to return true. There is a convention of commenting out the names of unused parameters in isTransferValid with /* */.

WithRules: the rule engine interface

IWithRules provides ruleLength() to get the number of registered rules, rule(_ruleId) to look up a specific rule contract, validateAddress and validateTransfer to check whether all registered rules are passed, and defineRules(IRule[] _rules) to replace the entire rule list. Calling defineRules emits a RulesDefined(count) event.

Designing rule order

When registering multiple rules, their order should be chosen carefully. Placing rules that are cheap to validate or likely to fail (be rejected) first reduces overall gas consumption, and the remaining order should follow the business logic. Because of this, the rules within one context must be designed as a whole rather than individually.

Reference implementation and modifiers

The source provides, via an external link, a reference implementation of WithRules optimized to minimize gas and deployment cost. This implementation defines two modifiers, whenAddressRulesAreValid and whenTransferRulesAreValid, which can be applied to any function in a token contract, such as transfer() or transferFrom(), to enforce anything from a simple whitelist to complex transfer rules.

How to integrate into a token

Integration is complete once the token inherits WithRules, each rule is written to conform to the IRule interface and deployed individually, and the token owner calls defineRules() once to connect all the rules in the chosen order. The source also provides a TemplateRule code skeleton to fill in with state variables, a constructor, isAddressValid, and isTransferValid.

Example rules

The source introduces four example rules via links to an external repository. The YesNo rule is a simple demo showing how the rule and engine behave. The Freeze rule is a blacklist-type rule that blocks both sending to and receiving from a designated address. The Lock rule is a whitelist-type rule that prohibits sending or receiving for a certain period while allowing an administrator to grant exceptions to certain addresses. UserKycRule is an example rule that delegates verification entirely to an existing whitelist.

Current status

This proposal was written on November 9, 2018, and it is recorded that at the time of writing, Mt Pelerin's MPS token actually used an early version of it. However, this ERC is in Stagnant status, meaning it is a proposal where discussion has stopped and is no longer progressing.

Original ERC · ← ERCs