testnet
GitHub

Managing Contract Policies via Precompile

integration intermediate

A step-by-step guide to the complete lifecycle of managing compliance policies for a smart contract using the PCL precompile functions.

Prerequisites

  • An already deployed smart contract on Maroo.
  • An admin account or contract authorized to manage policies.

1. Registering Initial Policies

The first step is to set the initial policy configuration. This can only be done once per contract. Use the registerContractPolicies function. You must define the target contract, the admin for future changes, and the array of policies.
UnitPolicy[] memory policies = new UnitPolicy[](1);
// ... populate policies array ...

ContractPolicyConfig memory config = ContractPolicyConfig({
    _contract: myTargetContract,
    admin: msg.sender,
    policies: policies
});

PCL_CONTRACT.registerContractPolicies(config);

2. Reading Current Policies

To verify the current configuration or before making an update, you can fetch the active policies for any contract using contractPolicies. This function returns the full ContractPolicyConfig struct.
ContractPolicyConfig memory currentConfig = PCL_CONTRACT.contractPolicies(myTargetContract);

// Now you can inspect currentConfig.policies, currentConfig.admin, etc.
address currentAdmin = currentConfig.admin;

3. Changing Existing Policies

To update policies, use changeContractPolicies. This function replaces the entire existing configuration. The common workflow is to first read the current policies, modify the array in memory (add, remove, or edit a UnitPolicy), and then submit the new ContractPolicyConfig.
// Assume 'currentConfig' is fetched as in Step 2
// Assume 'newPolicy' is a newly created UnitPolicy

UnitPolicy[] memory newPolicies = new UnitPolicy[](currentConfig.policies.length + 1);
for (uint i = 0; i < currentConfig.policies.length; i++) {
    newPolicies[i] = currentConfig.policies[i];
}
newPolicies[currentConfig.policies.length] = newPolicy;

ContractPolicyConfig memory updatedConfig = ContractPolicyConfig({
    _contract: myTargetContract,
    admin: currentConfig.admin, // or a new admin address
    policies: newPolicies
});

PCL_CONTRACT.changeContractPolicies(updatedConfig);
Warning: Be careful: `changeContractPolicies` is a full replacement. If you submit a config with an empty policies array, you will effectively remove all policies.

4. Removing All Policies

To completely remove PCL enforcement from a contract, use removeContractPolicies. This deletes the entire configuration. The caller must be the current admin for the contract's policies.
// The caller must be the registered admin for myTargetContract
PCL_CONTRACT.removeContractPolicies(myTargetContract);
Source: maroo
ESC
Type to search