testnet
GitHub

IPcl.changeContractPolicies

changeContractPolicies(ContractPolicyConfig calldata policy)

Updates an existing set of compliance policies for a smart contract. This function completely replaces the old policy configuration with the new one provided. To add or remove a single policy, you must read the existing configuration, modify the UnitPolicy array, and submit the entire new configuration. The caller must be the currently registered admin for the target contract.

Parameters

Name Type Required Description
policy ContractPolicyConfig A struct containing the target contract address and the new set of UnitPolicys. The admin field in the struct can be used to transfer admin rights to a new address.

Returns

Type: void

This function does not return any value.

Errors

Code Name Description
Unauthorized Unauthorized Reverts if the `msg.sender` is not the current policy admin for the contract.
PolicyNotRegistered PolicyNotRegistered Reverts if no policies are currently registered for the target contract.

Examples

Add a Volume Limit Policy

This example demonstrates the 'read-modify-write' pattern for updating policies. It first fetches the current policies, appends a new `VOLUME_ABOVE_MAX_LIMIT` policy, and then submits the combined list as a replacement.

import { IPcl, PCL_CONTRACT, UnitPolicy, ContractPolicyConfig } from "./IPcl.sol";

contract MyTokenAdmin {
    address public myTokenAddress;

    function addVolumeLimit(uint256 maxLimit) external {
        // 1. Get the current policy configuration
        ContractPolicyConfig memory currentConfig = PCL_CONTRACT.contractPolicies(myTokenAddress);

        // 2. Create the new policy to add
        bytes memory params = abi.encode(maxLimit);
        UnitPolicy memory volumeLimitPolicy = UnitPolicy({
            templateId: "VOLUME_ABOVE_MAX_LIMIT",
            parameters: params,
            paramNames: new string[](0),
            selector: bytes4(0)
        });

        // 3. Create a new array and combine old and new policies
        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] = volumeLimitPolicy;

        // 4. Create the new full configuration
        ContractPolicyConfig memory newConfig = ContractPolicyConfig({
            _contract: myTokenAddress,
            admin: address(this), // Keep the same admin
            policies: newPolicies
        });

        // 5. Call the precompile to update
        PCL_CONTRACT.changeContractPolicies(newConfig);
    }
}
ESC
Type to search