Skip to main content
After you read this page, you can turn a typed form into a rule document, read a committed document back into its form, and add an email notice or a fee token. Each form has a builder and a reader: For the meaning of each trigger and action, see the rule language.

The contract of every builder and reader

  • One rule per document. Each builder prints one rule. Combine rules with splitDocument and assembleDocument (see documents).
  • Normal layout. Each builder prints through printRuleDocument, so the output is in the normal layout. Each reader reads normal text only. Normalize other text with normalizeDocument first.
  • Byte-for-byte readers. A reader returns a form only when the builder of that form reproduces the input byte for byte from the form. Else it returns null. A field that the form cannot show, such as enabled: or a from: filter in a Routing rule, makes the reader return null. So a form never hides a part of a committed rule.
  • Builders refuse nothing. A builder prints an unfinished form as it is. The engine checks the document at the stage. For example, a share under a fixed-time trigger prints, and the engine refuses it.
  • Fee token. Each builder takes an optional feesMethod. With it, the document starts with global.fees_method. An ERC20Allow document needs it. Each reader reads it back with readFeesMethod and passes it to the builder for the byte check.
  • Email notice. With notifyByEmail: true, the actions end with notify on the email channel and the fixed message of the form (FORM_NOTIFY_MESSAGES). The document names no inbox. Each notify mails global.notification_email, which must be the verified inbox of the account. See notification email.

FeesMethod

One fixed token for all fees of a document. It prints as:

Routing

RoutingForm

RoutingRecipient

One on-chain payee prints send_one. Two or more on-chain payees print one send_batch, which pays all or none. Each bank payee prints one off_ramp after the send. The all-or-none rule covers only the on-chain payees.

Output

An anyTx condition with one payee:
A bank payee (holder: { kind: "person", firstName: "Ada", lastName: "Lovelace" }, memo: "Rent payment"):
A fixed instant:

Reader

parseRoutingDsl(yaml, native) returns the form, or null. native is the native coin of the network. It fills token for anyTx and atTime, which name no token. On-chain payees come before bank payees in the result. The reader of the first example returns:

Swap

The exchange action sells at market through the swap aggregator of the network. The engine refuses every exchange on polygon-amoy, where no aggregator is available.
parseSwapDsl puts placeholderWatchedToken in watchedToken under anyTx and atTime. It returns null for a share under at_fixed_time.

DCA

Every calendar cadence also prints the rule field start: (the first fire) and, with an end, expiry:. Both print in UTC through toISOString(). A monthly rule on day 29, 30 or 31 skips the months without that day. Weekly, from 2027-01-08 09:00 UTC (10:00 in Berlin), to the end of 2027:
The same form with cadence: "hourly" and no end:
With cadence: "once":
parseDcaDsl returns null for a share, for the same token on both sides, for a time_of_day that does not agree with start:, and for any other change. A once rule reads back with timezone: "UTC".

Limit order

The form of this output had priceUsd: "4,000.50". The builder prints the canonical level 4000.5. A level that isPriceUsdText refuses prints as 0, which the reader also refuses.

Standing order

A fixed payment on a schedule. The schedule fields mean what they mean on DcaForm. Each payee must have a literal amount: a scheduled fire has no trigger.value. The reader returns null for a share, a once or hourly schedule, and any other action. notifyByEmail adds "Your Standing order automation fired.". Rent of 1200 USDC on the first of each month at 09:00 in Berlin:

Reimburse

Each time account sends a transfer that condition matches, the rule pays percent of that transfer back to account.

Builder

One trigger, then any number of action blocks in order, then the optional email notice. trigger.source chooses which part of trigger prints. The other parts are kept, so a switch back restores them. The engine runs each action on its own, so the actions of a Builder rule are not atomic as a group. notifyByEmail adds "Your Builder automation fired.".
parseBuilderDsl fills the parts that the rule does not use with placeholders: an incoming transfer on this account with the any condition and native, a once schedule in UTC with no instant, and a price trigger on native with no level, direction above, and the one-shot cooldown. Two adjacent send blocks that print the same bytes as one block read back as one.

Price trigger

The rule fires when the USD price of token crosses priceUsd in direction and stays on that side for holdSeconds. A cross less than cooldownSeconds after the last fire starts no hold. isPriceUsdText(text) accepts a level of the form ^(0|[1-9]\d*)(\.\d{1,8})?$ with no final zero in the fraction, at most 15 significant digits, and a value from 0.0001 up to, but not including, 1,000,000,000,000. priceUsdCanonicalText(text) makes typed text canonical: it trims, drops leading zeros of the whole part and final zeros of the fraction, and drops commas only where they must be thousands separators. priceCrossedTriggerMap returns the trigger as a DslMap for printRuleDocument. It prints the canonical level, or 0 when the level is not valid. parsePriceCrossedTrigger reads the lines of a rule up to actions:. It needs exactly the keys type, token, direction, price_usd, hold_seconds, cooldown_seconds, in that order.

Email notify action

actionLinesWithoutNotify removes the first - type: notify item and its lines from the lines under actions:. The readers use it to read notifyByEmail.

Form parts

These functions are the parts that the builders and readers share. Use them to build a form of your own on the same normal layout.

Generate a document from text

generateDsl calls POST /api/dsl/generate and returns the text of a draft document. It commits, signs and stages nothing. The owner still stages and commits the draft. The request times out after 45 seconds. siweSessionMessage renders the sign-in for this route with the statement Sign in to Anafi to write rules from a description. and no expiry line. The API derives the end of the session from Issued At. Sign once, and reuse the pair while the API accepts it. The API accepts the sign-in only when domain is the domain of the Anafi web app, and chainId is the chain of the host.
On a failure it throws GenerateDslError with failure: