🟢Warden

Creating a Simple Rule

The following example demonstrates how to create a rule that triggers an action based on a condition:

rule CheckThreshold {
  when (balanceOf(user_address) > 1000) {
    execute(action SendReward);
  }
}

Explanation:

  • rule CheckThreshold: Defines a new rule.

  • when (balanceOf(user_address) > 1000): This condition checks if the user’s balance exceeds 1000 tokens.

  • execute(action SendReward): Executes the predefined action SendReward if the condition is met.

Defining Actions

Actions specify the operations triggered when rules are satisfied. For instance, transferring tokens or interacting with another contract.

action SendReward {
  transfer(from: contractAddress, to: user_address, amount: 50);
}

This action transfers 50 tokens from contractAddress to user_address.

Using Approvals

Some actions might require user approvals to ensure that only authorized users can trigger them.

action SecureAction {
  approval(user_address); // Requires approval from the specific user
  call(contractAddress.methodName(), params);
}

Explanation:

  • approval(user_address): Ensures that the user has approved the action before it can be executed.

  • call(contractAddress.methodName(), params): Calls a function on a contract with specific parameters.

Integration with Other Modules

The x/act module can integrate with other modules, such as managing permissions or storing complex data. Here's an example of linking it to an on-chain data store:

rule FetchData {
  when (dataStore.get("key") == "expectedValue") {
    execute(action ProcessData);
  }
}

action ProcessData {
  call(contract.processData(), [data]);
}

Last updated