| 
1 |  | -### ServicesPrecompile  | 
2 |  | - | 
3 |  | -The `ServicesPrecompile` is a precompiled contract that facilitates interaction with the Tangle network's services functionality. It provides a comprehensive interface for creating and managing service blueprints, requesting services, and handling service lifecycle operations.  | 
4 |  | - | 
5 |  | -The latest version of the precompile can be found [here](https://github.com/tangle-network/tangle/blob/main/precompiles/services/Services.sol).  | 
6 |  | - | 
7 |  | -#### Address  | 
8 |  | - | 
9 |  | -- **Contract Address**: `0x0000000000000000000000000000000000000900`  | 
10 |  | - | 
11 |  | -This interface is designed to be used by Solidity contracts to interact with the Services pallet, enabling blueprint creation, service orchestration, and operator management on the Tangle network.  | 
12 |  | - | 
13 |  | -#### Interface  | 
14 |  | - | 
15 |  | -```solidity  | 
16 |  | -// SPDX-License-Identifier: GPL-3.0-only  | 
17 |  | -pragma solidity >=0.8.3;  | 
18 |  | -
  | 
19 |  | -/// @dev The Services contract's address.  | 
20 |  | -address constant SERVICES = 0x0000000000000000000000000000000000000900;  | 
21 |  | -
  | 
22 |  | -/// @dev The Services contract's instance.  | 
23 |  | -Services constant SERVICES_CONTRACT = Services(SERVICES);  | 
24 |  | -
  | 
25 |  | -/// @author The Tangle Team  | 
26 |  | -/// @title Pallet Services Interface  | 
27 |  | -/// @title The interface through which solidity contracts will interact with the Services pallet  | 
28 |  | -/// @custom:address 0x0000000000000000000000000000000000000900  | 
29 |  | -interface Services {  | 
30 |  | -    /// @dev Custom errors for service operations  | 
31 |  | -    error InvalidPermittedCallers();  | 
32 |  | -    error InvalidOperatorsList();  | 
33 |  | -    error InvalidRequestArguments();  | 
34 |  | -    error InvalidTTL();  | 
35 |  | -    error InvalidAmount();  | 
36 |  | -    error ValueMustBeZeroForERC20();  | 
37 |  | -    error ValueMustBeZeroForCustomAsset();  | 
38 |  | -    error PaymentAssetShouldBeCustomOrERC20();  | 
39 |  | -
  | 
40 |  | -    /// @dev Create a new service blueprint.  | 
41 |  | -    /// @param blueprint_data The encoded blueprint data including metadata and configuration.  | 
42 |  | -    function createBlueprint(bytes calldata blueprint_data) external returns (uint8);  | 
43 |  | -
  | 
44 |  | -    /// @dev Request a new service instance.  | 
45 |  | -    /// @param blueprint_id The ID of the blueprint to instantiate.  | 
46 |  | -    /// @param security_requirements Security requirements for the service.  | 
47 |  | -    /// @param permitted_callers_data Encoded list of permitted callers.  | 
48 |  | -    /// @param service_providers_data Encoded list of service providers.  | 
49 |  | -    /// @param request_args_data Arguments for the service request.  | 
50 |  | -    /// @param ttl Time-to-live for the service.  | 
51 |  | -    /// @param payment_asset_id Asset ID for payment (0 for ERC20).  | 
52 |  | -    /// @param payment_token_address Token address if using ERC20.  | 
53 |  | -    /// @param payment_amount Amount to pay for the service.  | 
54 |  | -    /// @param min_operators Minimum number of operators required.  | 
55 |  | -    /// @param max_operators Maximum number of operators allowed.  | 
56 |  | -    function requestService(  | 
57 |  | -        uint256 blueprint_id,  | 
58 |  | -        bytes[] calldata security_requirements,  | 
59 |  | -        bytes calldata permitted_callers_data,  | 
60 |  | -        bytes calldata service_providers_data,  | 
61 |  | -        bytes calldata request_args_data,  | 
62 |  | -        uint256 ttl,  | 
63 |  | -        uint256 payment_asset_id,  | 
64 |  | -        address payment_token_address,  | 
65 |  | -        uint256 payment_amount,  | 
66 |  | -        uint32 min_operators,  | 
67 |  | -        uint32 max_operators  | 
68 |  | -    ) external payable returns (uint8);  | 
69 |  | -
  | 
70 |  | -    /// @dev Terminate an active service.  | 
71 |  | -    /// @param service_id The ID of the service to terminate.  | 
72 |  | -    function terminateService(uint256 service_id) external returns (uint8);  | 
73 |  | -
  | 
74 |  | -    /// @dev Call a job within an active service.  | 
75 |  | -    /// @param service_id The ID of the service.  | 
76 |  | -    /// @param job The job index to call.  | 
77 |  | -    /// @param args_data Arguments for the job call.  | 
78 |  | -    function callJob(uint256 service_id, uint8 job, bytes calldata args_data) external returns (uint8);  | 
79 |  | -
  | 
80 |  | -    /// @dev Slash an operator for misbehavior.  | 
81 |  | -    /// @param offender The operator to slash.  | 
82 |  | -    /// @param service_id The service ID related to the offense.  | 
83 |  | -    /// @param percent The percentage to slash (0-100).  | 
84 |  | -    function slash(bytes calldata offender, uint256 service_id, uint8 percent) external returns (uint8);  | 
85 |  | -
  | 
86 |  | -    /// @dev Dispute an unapplied slash.  | 
87 |  | -    /// @param era The era of the slash.  | 
88 |  | -    /// @param index The index of the slash.  | 
89 |  | -    function dispute(uint32 era, uint32 index) external returns (uint8);  | 
90 |  | -}  | 
91 |  | -```  | 
92 |  | - | 
93 |  | -#### Key Concepts  | 
94 |  | - | 
95 |  | -##### Blueprint Creation  | 
96 |  | - | 
97 |  | -Blueprints define reusable service templates that operators can instantiate. They contain:  | 
98 |  | - | 
99 |  | -- Service metadata and configuration  | 
100 |  | -- Job definitions and requirements  | 
101 |  | -- Security and operational parameters  | 
102 |  | - | 
103 |  | -##### Service Lifecycle  | 
104 |  | - | 
105 |  | -1. **Request**: Users request service instances from blueprints  | 
106 |  | -2. **Active**: Services run with assigned operators  | 
107 |  | -3. **Termination**: Services end when TTL expires or manually terminated  | 
108 |  | - | 
109 |  | -##### Payment System  | 
110 |  | - | 
111 |  | -- Supports multiple asset types (native, ERC20, custom)  | 
112 |  | -- Payment amount distributed among operators  | 
113 |  | -- Use `msg.value` for native token payments  | 
114 |  | - | 
115 |  | -#### Example Usage  | 
116 |  | - | 
117 |  | -```solidity  | 
118 |  | -contract ServiceRequestExample {  | 
119 |  | -    address constant SERVICES_ADDRESS = 0x0000000000000000000000000000000000000900;  | 
120 |  | -    IServicesPrecompile services = IServicesPrecompile(SERVICES_ADDRESS);  | 
121 |  | -
  | 
122 |  | -    function requestSimpleService(  | 
123 |  | -        uint256 blueprintId,  | 
124 |  | -        uint256 ttl,  | 
125 |  | -        uint256 paymentAmount  | 
126 |  | -    ) public payable returns (uint8) {  | 
127 |  | -        // Request a service with native token payment  | 
128 |  | -        uint8 statusCode = services.requestService{value: paymentAmount}(  | 
129 |  | -            blueprintId,  | 
130 |  | -            new bytes[](0), // No special security requirements  | 
131 |  | -            "", // No caller restrictions  | 
132 |  | -            "", // Any operators  | 
133 |  | -            "", // No special arguments  | 
134 |  | -            ttl,  | 
135 |  | -            1, // Native asset  | 
136 |  | -            address(0), // Not needed for native  | 
137 |  | -            paymentAmount,  | 
138 |  | -            1, // Min 1 operator  | 
139 |  | -            5  // Max 5 operators  | 
140 |  | -        );  | 
141 |  | -
  | 
142 |  | -        require(statusCode == 0, "Service request failed");  | 
143 |  | -        return statusCode;  | 
144 |  | -    }  | 
145 |  | -
  | 
146 |  | -    function requestERC20Service(  | 
147 |  | -        uint256 blueprintId,  | 
148 |  | -        address tokenAddress,  | 
149 |  | -        uint256 amount  | 
150 |  | -    ) public returns (uint8) {  | 
151 |  | -        // Request a service with ERC20 payment  | 
152 |  | -        // Note: Ensure token approval before calling  | 
153 |  | -        uint8 statusCode = services.requestService(  | 
154 |  | -            blueprintId,  | 
155 |  | -            new bytes[](0),  | 
156 |  | -            "",  | 
157 |  | -            "",  | 
158 |  | -            "",  | 
159 |  | -            86400, // 24 hour TTL  | 
160 |  | -            0, // ERC20 asset  | 
161 |  | -            tokenAddress,  | 
162 |  | -            amount,  | 
163 |  | -            1,  | 
164 |  | -            3  | 
165 |  | -        );  | 
166 |  | -
  | 
167 |  | -        require(statusCode == 0, "Service request failed");  | 
168 |  | -        return statusCode;  | 
169 |  | -    }  | 
170 |  | -}  | 
171 |  | -```  | 
172 |  | - | 
173 |  | -#### Error Handling  | 
174 |  | - | 
175 |  | -The interface includes custom errors for better debugging:  | 
176 |  | - | 
177 |  | -- `InvalidPermittedCallers`: Invalid caller restrictions  | 
178 |  | -- `InvalidOperatorsList`: Invalid operator specification  | 
179 |  | -- `InvalidRequestArguments`: Malformed request parameters  | 
180 |  | -- `InvalidTTL`: TTL value out of bounds  | 
181 |  | -- `InvalidAmount`: Payment amount issues  | 
182 |  | -- `ValueMustBeZeroForERC20`: Incorrect msg.value for ERC20 payments  | 
183 |  | -- `PaymentAssetShouldBeCustomOrERC20`: Invalid payment asset type  | 
 | 1 | +https://github.com/tangle-network/tangle/blob/main/precompiles/services/Services.sol  | 
0 commit comments