Initialize a Tracker

As outlined in the policy configuration guide, you define your trackers in the policy.json file for your project. Below is an example policy to illustrate the examples that will follow.

policy.json
{
  "Policy": "Tracker Example Policy",
  "ForeignCalls": [],
  "Trackers": [
    {
      "name": "TradingVolume",
      "type": "uint256",
      "initialValue": 0
    }
  ],
  "Rules": [
    // purposefully empty for now
  ]
}

There are two ways that you can utilize trackers in your project.

  1. Trackers can be used as input values in rule expressions.
  2. Trackers can be updated as effects of rules.

Let’s see how to accomplish these two usage options.

Usage in Rules

First, we need to add at least one rule to the policy that will use the named tracker. This rule condition ensures that the total volume so far and the new amount does not exceed 1 billion tokens.

policy.json
{
  "Policy": "Tracker Example Policy",
  "ForeignCalls": [],
  "Trackers": [
    {
      "name": "TradingVolume",
      "type": "uint256",
      "initialValue": 0
    }
  ],
  "Rules": [
    {
      "condition": "(TR:TradingVolume + amount) < 1_000_000_000",
      "positiveEffects": [],
      "negativeEffects": ["revert(\"Trading Volume Max Reached\")"],
      "callingFunction": "_update(address from, address to, uint256 amount)",
      "encodedValues": "address from, address to, uint256 amount"
    }
  ]
}

Note that this rule is applied to the internal _update function. This ensures the policy is applied to both the transfer and transferFrom function calls.

Tracker Update Effects

As of now, this rule will always succeed because the tracker value is never updated. Below we add a positiveEffect that updates the tracker value whenever a transfer call is successful.

policy.json
//...
    "Rules": [
      {
        "condition": "(TR:TradingVolume + amount) < 1_000_000_000",
        "positiveEffects": [
          "TRU:TradingVolume += amount"
        ],
        "negativeEffects": ["revert(\"Trading Volume Max Reached\")"],
        "callingFunction": "_update(address from, address to, uint256 amount)",
        "encodedValues": "address from, address to, uint256 amount"
      }
    ]
//...

Notice that when referencing a tracker value in a rule condition, you precede the name with TR as in TR:TradingVolume.

When referencing in an update you must precede it with TRU as in TRU:TradingVolume

Taking it Further

The rule defined thus far is a good start, but needs additional configuration to acheive the desired outcome.

First, as soon as 1_000_000_000 in trading volume is reached the token will no longer be transferrable. Fixing this will requires resetting the TradingVolume tracker when the defined duration is reached. To do this we need another tracker to record the timestamp and mark the beginning of a rolling 24 hour period. Then we test for that condition in a new rule and reset the tracker values when the condition is true.

policy.json
{
  "Policy": "Tracker Example Policy",
  "ForeignCalls": [],
  "Trackers": [
    {
      "name": "TimeStamp",
      "type": "uint256",
      "initialValue": 0
    },
    {
      "name": "TradingVolume",
      "type": "uint256",
      "initialValue": 0
    }
  ],
  "Rules": [
    {
      "condition": "(block.timestamp - TR:TimeStamp) >= 86400",
      "positiveEffects": ["TRU:TimeStamp = block.timestamp", "TRU:TradingVolume = 0"],
      "negativeEffects": [],
      "callingFunction": "_update(address from, address to, uint256 amount)",
      "encodedValues": "address from, address to, uint256 amount"
    },
    {
      "condition": "(TR:TradingVolume + amount) < 1_000_000_000",
      "positiveEffects": [],
      "negativeEffects": ["revert(\"Trading Volume Max Reached\")"],
      "callingFunction": "_update(address from, address to, uint256 amount)",
      "encodedValues": "address from, address to, uint256 amount"
    }
  ]
}

The new rule above is leveraging the block.timestamp directly within the rule condition. This is not supported in the current version of the Rules Engine, but is anticipated. In the meantime, you’d need to directly pass the timestamp value as an extra argument to the modifier added to the _update function.

Find more info about how to do this in the contract integration guide.