Basic Usage
Simple examples demonstrating how to create policies, intents, and validate/execute them using ERC-1066-x402.
Creating a Policy
Policies define the rules for intent validation. Create a policy using the PolicyRegistry:
create_policy.sol
PolicyRegistry registry = PolicyRegistry(registryAddress);
bytes32 policyId = keccak256("my-policy");
Policy memory policy = Policy({
owner: msg.sender,
allowedTargets: [targetAddress],
allowedSelectors: [],
maxValuePerTx: 1 ether,
maxAggregateValue: 0,
validAfter: 0,
validBefore: 0,
allowedChains: []
});
registry.setPolicy(policyId, policy);Creating an Intent
An intent represents a desired action that will be validated and executed:
create_intent.sol
Intent memory intent = Intent({
sender: userAddress,
target: targetContract,
data: callData,
value: 0.5 ether,
nonce: 0,
validAfter: block.timestamp,
validBefore: block.timestamp + 1 days,
policyId: policyId
});Validating an Intent
Before executing, validate the intent to check if it will succeed:
validate_intent.sol
// Validate intent
bytes1 status = validator.canExecute(intent);
// Check if validation succeeded
require(status == StatusCodes.STATUS_SUCCESS, "Intent validation failed");Pre-flight Validation
Always validate intents before executing them. This saves gas and provides clear feedback about why an intent might fail.
Executing an Intent
Once validated, execute the intent:
execute_intent.sol
// Execute intent with value
executor.execute{value: intent.value}(intent);