Skip to main content
After you read this page, you can change any rule document to one normal text, edit one rule of a document with many rules, read which tokens and addresses a document names, and compute its on-chain commitment.

The normal layout

The SDK writes one text for a document, whatever layout its author typed. Two documents are the same document when their normal texts are equal. Comments, blank lines, indent width, quote style, flow style, and the key order inside a rule make no difference to the normal text. The engine also reads a rule by its parsed content, not by its bytes. The commitment covers the exact bytes that you commit. Normalize a document before you stage it, so the text that you show, compare, store and commit is one text. The normal layout:
  • prints maps in block style, and lists of scalars in flow style ([mon, tue]);
  • prints the keys inside a rule in this order: type, trigger, actions, account, to, value, recipients, in, out, channel, message, timestamps, day_of_week, day_of_month, time_of_day, timezone, start, expiry, then any other key in document order;
  • keeps the top-level keys, and the keys outside rules, in document order;
  • prints a string plain when it is an identifier that the engine reads as a string, and in double quotes in all other cases;
  • prints a number from its source text, a null as null, and a boolean as true or false;
  • has no comments.

normalizeDocument

Returns the normal text, or the reason why the text has none. reason is a sentence for the author. The function never throws for a bad document. It refuses:
  • YAML with any parser error or warning: a duplicate key, a tab in an indent, an unknown tag, two documents in one text;
  • a raw U+000D with no U+000A after it, and a raw U+0085, U+2028 or U+2029, anywhere;
  • a raw U+FEFF anywhere except as the first character;
  • a %YAML version other than 1.2, and a %TAG directive;
  • an anchor or an alias;
  • any tag except !!str;
  • a double-quoted value that escapes a line break;
  • a | or > block value with no - in its header and no line break after its last line;
  • a | or > block value with an indent digit in its header;
  • a top level that is not a map, and a rules value that is not a map;
  • a rule name that is not a string or a 64-bit integer, and two rules with one name.
The engine refuses almost all of these too. The SDK refuses the rest because its YAML reader and the engine reader read them differently.

printRuleDocument

Prints a one-rule document in the normal layout, with an optional global: map before rules:. Every builder of rule builders prints through it.

PlainScalarKind

How the engine reads a plain (unquoted) scalar. The normal layout uses it to decide which strings need quotes.

normalTextWithGlobalMap

Returns the text in the normal layout with the map value under global.<key>. A null value removes the key, and removes global: when it becomes empty. A document with no global: gets one as its first key. It throws an Error with the refusal reason for a text with no normal layout, and for a global that is not a map.

Split and assemble

A document can hold many rules. The functions below let an editor own one rule at a time and still commit one document. The non-rule parts of the document survive: the preamble (the top-level keys above rules, such as global:) and the trailer (the top-level keys after it).

splitDocument

Returns null for a text that normalizeDocument refuses. For a document with no rules key, rules is empty and hasRulesKey is false.

assembleDocument

The inverse of splitDocument: the preamble, then rules: and the rule bodies, then the trailer. With no rules and hasRulesKey: true, it prints rules: {}, a valid document with nothing to run. With nothing at all, it prints {}.

Single-rule helpers

Fee token

readFeesMethod

Returns global.fees_method when it is exactly { type: fixed_token, token: <RoutingToken> }. Returns null for no fee method, any other shape, an unknown token, and text with no normal layout.

documentWithFeesMethod

Returns the normal text with global.fees_method set to method, or removed for null. It throws for text with no normal layout.

What a document names

documentActionTokens

Reads the tokens that the actions spend, rule by rule in document order. It reads the value of each send_one, the value of each send_batch row, and the in of each exchange:
  • A literal "AMOUNT TOKEN" names TOKEN.
  • "trigger.value" and "trigger.value * N%" name the token at the end of the value range of the incoming_tx or outgoing_tx trigger of the rule.
  • Any other value names no token, and the answer is unnamed with the rule and the action kind.
Other action types are not read. spentTokens holds each token one time, in first-seen order. feeToken is the token of global.fees_method, or null. The ERC20Allow leg computes tokens(D) from this answer (see ERC20Allow). For the Builder example of rule builders:
For a Routing rule with an anyTx condition and a percent payee, the trigger has no range, so the share names no token:

documentDestinations

Returns each address value of a to key inside the actions of each rule: the to of a send_one and of each send_batch row. The bank details of an off_ramp are a map, not an address, and are not listed. The to filter of an outgoing_tx trigger is not listed: it pays nothing. A to: inside a string is text and is not listed. A number is listed as its decimal text. Returns null for text with no normal layout. Use it to check every payee before the owner signs.

documentTransferFilterAddresses

Returns each address filter of the transfer triggers, in document order: the account and from of an incoming_tx, and the account and to of an outgoing_tx. Returns null for text with no normal layout.

documentHasOffRampAction

true when any rule has an off_ramp action. false for text with no normal layout.

documentNotificationEmail and documentWithNotificationEmail

documentNotificationEmail returns global.notification_email as the document spells it, or null. documentWithNotificationEmail returns the normal text with global.notification_email set. canonicalEmail must already be canonical (canonicalNotificationEmail), else it throws Error("notification email must already be canonical"). A document whose email has the same canonical form keeps its own spelling. It throws an Error with the refusal reason for text with no normal layout.
The engine refuses a stage of a document whose notification_email is not the verified inbox of the account. See notification email.

Commitment

dslCommitment

The on-chain commitment of a document. salt must be exactly 0x and 64 hex characters, else it throws. AnafiDslRegistry.commitments(aa).hash holds this value after a commit. The stage and the commit methods check the engine echo against it.
The framing of the sealed stage payload (dslUploadPayload, dslStagePayload) is on low-level helpers.