Skip to main content

Manage Rules

Overview

The IAct precompile allows calling the x/act module from EVM smart contracts.

This article explains how to use x/act to manage Rules. You'll learn how to call the corresponding functions of the precompile and interact with them after deploying your contract.

To understand how to set up and deploy your project, see Get started.

tip

For an overview of x/act functions, refer to Precompiles: x/warden

Create a new Rule

To create a new Rule, use the following code in your contract. It calls the newTemplate() function of the precompile.

contract WardenRule {
IAct constant ACT = IAct(0x0000000000000000000000000000000000000901);

function createRule(
string calldata name,
string calldata definition
) external returns (uint64) {
return ACT.newTemplate(name, definition);
}
}

After deploying your contract, you can interact with it by calling the createRule() function:

cast send $CONTRACT_ADDRESS "createRule(string,string)" \
"MyRule" "quorum(2, [\"0x123...\", \"0x456...\"])" \
--rpc-url $RPC_URL --private-key $PRIVATE_KEY

Update a Rule

To update a Rule, use the following code in your contract. It calls the updateTemplate() function of the precompile.

contract WardenRule {
IAct constant ACT = IAct(0x0000000000000000000000000000000000000901);

function updateRule(
uint64 templateId,
string calldata name,
string calldata definition
) external returns (bool) {
return ACT.updateTemplate(templateId, name, definition);
}
}

After deploying your contract, you can interact with it by calling the updateRule() function:

cast send $CONTRACT_ADDRESS "updateRule(uint64,string,string)" \
1 "UpdatedRule" "quorum(3, [\"0x123...\", \"0x456...\", \"0x789...\"])" \
--private-key $PRIVATE_KEY --rpc-url $RPC_URL

Query Rules

To get a list of all Rules, use the following code in your contract. It calls the templates() function of the precompile.

contract WardenRule {
IAct constant ACT = IAct(0x0000000000000000000000000000000000000901);

function getAllRules(
Types.PageRequest calldata pagination,
address creator
) external view returns (IAct.TemplatesResponse memory) {
return ACT.templates(pagination, creator);
}
}

After deploying your contract, you can interact with it by calling the getAllRules() function:

# Get first 10 rules for a creator
cast call $CONTRACT_ADDRESS "getAllRules((bytes,uint64,uint64,bool,bool),address)" \
"(0x,0,10,true,false)" $CREATOR_ADDRESS --rpc-url $RPC_URL

Query a Rule by ID

To get a Rule by ID, use the following code in your contract. It calls the templateById() function of the precompile.

contract WardenRule {
IAct constant ACT = IAct(0x0000000000000000000000000000000000000901);

function getRuleById(uint64 templateId) external view returns (
IAct.TemplateByIdResponse memory
) {
return ACT.templateById(templateId);
}
}

After deploying your contract, you can interact with it by calling the getRuleById() function:

cast call $CONTRACT_ADDRESS "getRuleById(uint64)" 1 --rpc-url $RPC_URL