Skip to content

Commit ad76a14

Browse files
authored
feat: make UniversalContract abstract (#492)
1 parent aeb8d44 commit ad76a14

File tree

37 files changed

+3687
-317
lines changed

37 files changed

+3687
-317
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity 0.8.26;
3+
4+
import "../../helpers/interfaces/IBaseRegistry.sol";
5+
6+
interface ICoreRegistry is IBaseRegistry {
7+
function gatewayZEVM() external returns (address);
8+
}

contracts/zevm/interfaces/UniversalContract.sol

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
pragma solidity 0.8.26;
33

44
import { RevertContext } from "../../../contracts/Revert.sol";
5+
import "../interfaces/IGatewayZEVM.sol";
6+
import "./ICoreRegistry.sol";
57

68
/// @custom:deprecated should be removed once v2 SystemContract is not used anymore.
79
/// MessageContext should be used
@@ -38,9 +40,43 @@ struct MessageContext {
3840
}
3941

4042
/// @title UniversalContract
41-
/// @notice Interface for contracts that can receive cross-chain calls on ZetaChain.
42-
/// @dev Contracts implementing this interface can handle incoming cross-chain messages
43+
/// @notice Abstract contract for contracts that can receive cross-chain calls on ZetaChain.
44+
/// @dev Contracts extending this abstract contract can handle incoming cross-chain messages
4345
/// and execute logic based on the provided context, token, and message payload.
44-
interface UniversalContract {
45-
function onCall(MessageContext calldata context, address zrc20, uint256 amount, bytes calldata message) external;
46+
abstract contract UniversalContract {
47+
// TODO: replace with real CoreRegistry address that will be deployed across all envs
48+
/// @notice Reference to the ZetaChain Registry contract
49+
ICoreRegistry public constant registry = ICoreRegistry(0x7c591652f159496b14e15616F0948a6d63b585E8);
50+
/// @notice Reference to the ZetaChain Gateway contract
51+
IGatewayZEVM public immutable gateway;
52+
53+
/// @notice Error thrown when a function is called by an unauthorized address
54+
error Unauthorized();
55+
56+
/// @notice Restricts function access to only the gateway contract
57+
/// @dev Used on functions that process cross-chain messages to ensure they're only called through the Gateway,
58+
/// where message validation occurs.
59+
/// Important for security in functions like `onCall()` and `onRevert()` that handle incoming cross-chain
60+
/// operations.
61+
modifier onlyGateway() {
62+
if (msg.sender != address(gateway)) revert Unauthorized();
63+
_;
64+
}
65+
66+
/// @notice Initializes the contract by retrieving the gateway address from the registry
67+
/// @dev Fetches the gateway contract address for the current chain from the registry.
68+
/// If the gateway is not active or not found, the gateway will remain uninitialized (address(0)).
69+
constructor() {
70+
gateway = IGatewayZEVM(registry.gatewayZEVM());
71+
}
72+
73+
/// @notice Function to handle cross-chain calls
74+
function onCall(
75+
MessageContext calldata context,
76+
address zrc20,
77+
uint256 amount,
78+
bytes calldata message
79+
)
80+
external
81+
virtual;
4682
}

docs/src/SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- [BaseRegistry](contracts/helpers/BaseRegistry.sol/abstract.BaseRegistry.md)
4949
- [❱ zevm](contracts/zevm/README.md)
5050
- [❱ interfaces](contracts/zevm/interfaces/README.md)
51+
- [ICoreRegistry](contracts/zevm/interfaces/ICoreRegistry.sol/interface.ICoreRegistry.md)
5152
- [IGatewayZEVMEvents](contracts/zevm/interfaces/IGatewayZEVM.sol/interface.IGatewayZEVMEvents.md)
5253
- [IGatewayZEVMErrors](contracts/zevm/interfaces/IGatewayZEVM.sol/interface.IGatewayZEVMErrors.md)
5354
- [IGatewayZEVM](contracts/zevm/interfaces/IGatewayZEVM.sol/interface.IGatewayZEVM.md)
@@ -61,7 +62,7 @@
6162
- [zContext](contracts/zevm/interfaces/UniversalContract.sol/struct.zContext.md)
6263
- [zContract](contracts/zevm/interfaces/UniversalContract.sol/interface.zContract.md)
6364
- [MessageContext](contracts/zevm/interfaces/UniversalContract.sol/struct.MessageContext.md)
64-
- [UniversalContract](contracts/zevm/interfaces/UniversalContract.sol/interface.UniversalContract.md)
65+
- [UniversalContract](contracts/zevm/interfaces/UniversalContract.sol/abstract.UniversalContract.md)
6566
- [❱ legacy](contracts/zevm/legacy/README.md)
6667
- [ZetaInterfaces](contracts/zevm/legacy/ZetaConnectorZEVM.sol/interface.ZetaInterfaces.md)
6768
- [ZetaReceiver](contracts/zevm/legacy/ZetaConnectorZEVM.sol/interface.ZetaReceiver.md)
Lines changed: 4 additions & 286 deletions
Original file line numberDiff line numberDiff line change
@@ -1,297 +1,15 @@
11
# ICoreRegistry
2-
[Git Source](https://github.com/zeta-chain/protocol-contracts/blob/main/zevm/interfaces/ICoreRegistry.sol)
2+
[Git Source](https://github.com/zeta-chain/protocol-contracts/blob/main/contracts/zevm/interfaces/ICoreRegistry.sol)
33

44
**Inherits:**
5-
[ICoreRegistryErrors](/contracts/zevm/interfaces/ICoreRegistry.sol/interface.ICoreRegistryErrors.md), [ICoreRegistryEvents](/contracts/zevm/interfaces/ICoreRegistry.sol/interface.ICoreRegistryEvents.md)
6-
7-
Primary interface for the CoreRegistry contract, the central registry for ZetaChain ecosystem.
5+
[IBaseRegistry](/contracts/helpers/interfaces/IBaseRegistry.sol/interface.IBaseRegistry.md)
86

97

108
## Functions
11-
### changeChainStatus
12-
13-
Changes status of the chain to activated/deactivated.
14-
15-
16-
```solidity
17-
function changeChainStatus(uint256 chainId, address gasZRC20, bytes calldata registry, bool activation) external;
18-
```
19-
**Parameters**
20-
21-
|Name|Type|Description|
22-
|----|----|-----------|
23-
|`chainId`|`uint256`|The ID of the chain to activate.|
24-
|`gasZRC20`|`address`|The address of the ZRC20 token that represents gas token for the chain.|
25-
|`registry`|`bytes`||
26-
|`activation`|`bool`|Whether activate or deactivate a chain|
27-
28-
29-
### updateChainMetadata
30-
31-
Updates chain metadata.
32-
33-
34-
```solidity
35-
function updateChainMetadata(uint256 chainId, string calldata key, bytes calldata value) external;
36-
```
37-
**Parameters**
38-
39-
|Name|Type|Description|
40-
|----|----|-----------|
41-
|`chainId`|`uint256`|The ID of the chain.|
42-
|`key`|`string`|The metadata key to update.|
43-
|`value`|`bytes`|The new value for the metadata.|
44-
45-
46-
### registerContract
47-
48-
Registers a new contract address for a specific chain.
49-
50-
51-
```solidity
52-
function registerContract(uint256 chainId, string calldata contractType, bytes calldata addressBytes) external;
53-
```
54-
**Parameters**
55-
56-
|Name|Type|Description|
57-
|----|----|-----------|
58-
|`chainId`|`uint256`|The ID of the chain where the contract is deployed.|
59-
|`contractType`|`string`|The type of the contract (e.g., "connector", "gateway").|
60-
|`addressBytes`|`bytes`|The bytes representation of the non-EVM address.|
61-
62-
63-
### updateContractConfiguration
64-
65-
Updates contract configuration.
66-
67-
68-
```solidity
69-
function updateContractConfiguration(
70-
uint256 chainId,
71-
string calldata contractType,
72-
string calldata key,
73-
bytes calldata value
74-
)
75-
external;
76-
```
77-
**Parameters**
78-
79-
|Name|Type|Description|
80-
|----|----|-----------|
81-
|`chainId`|`uint256`|The ID of the chain where the contract is deployed.|
82-
|`contractType`|`string`|The type of the contract.|
83-
|`key`|`string`|The configuration key to update.|
84-
|`value`|`bytes`|The new value for the configuration.|
85-
86-
87-
### setContractActive
88-
89-
90-
```solidity
91-
function setContractActive(uint256 chainId, string calldata contractType, bool active) external;
92-
```
93-
94-
### registerZRC20Token
95-
96-
Registers a new ZRC20 token in the registry.
9+
### gatewayZEVM
9710

9811

9912
```solidity
100-
function registerZRC20Token(
101-
address address_,
102-
string calldata symbol,
103-
uint256 originChainId,
104-
bytes calldata originAddress,
105-
string calldata coinType,
106-
uint8 decimals
107-
)
108-
external;
13+
function gatewayZEVM() external returns (address);
10914
```
110-
**Parameters**
111-
112-
|Name|Type|Description|
113-
|----|----|-----------|
114-
|`address_`|`address`|The address of the ZRC20 token on ZetaChain.|
115-
|`symbol`|`string`|The symbol of the token.|
116-
|`originChainId`|`uint256`|The ID of the foreign chain where the original asset exists.|
117-
|`originAddress`|`bytes`|The address or identifier of the asset on its native chain.|
118-
|`coinType`|`string`||
119-
|`decimals`|`uint8`||
120-
121-
122-
### setZRC20TokenActive
123-
124-
Updates ZRC20 token information.
125-
126-
127-
```solidity
128-
function setZRC20TokenActive(address address_, bool active) external;
129-
```
130-
**Parameters**
131-
132-
|Name|Type|Description|
133-
|----|----|-----------|
134-
|`address_`|`address`|The address of the ZRC20 token.|
135-
|`active`|`bool`|Whether the token should be active.|
136-
137-
138-
### getChainMetadata
139-
140-
Gets chain-specific metadata.
141-
142-
143-
```solidity
144-
function getChainMetadata(uint256 chainId, string calldata key) external view returns (bytes memory);
145-
```
146-
**Parameters**
147-
148-
|Name|Type|Description|
149-
|----|----|-----------|
150-
|`chainId`|`uint256`|The ID of the chain.|
151-
|`key`|`string`|The metadata key to retrieve.|
152-
153-
**Returns**
154-
155-
|Name|Type|Description|
156-
|----|----|-----------|
157-
|`<none>`|`bytes`|The value of the requested metadata.|
158-
159-
160-
### getContractInfo
161-
162-
Gets information about a specific contract.
163-
164-
165-
```solidity
166-
function getContractInfo(
167-
uint256 chainId,
168-
string calldata contractType
169-
)
170-
external
171-
view
172-
returns (bool active, bytes memory addressBytes);
173-
```
174-
**Parameters**
175-
176-
|Name|Type|Description|
177-
|----|----|-----------|
178-
|`chainId`|`uint256`|The ID of the chain where the contract is deployed.|
179-
|`contractType`|`string`|The type of the contract.|
180-
181-
**Returns**
182-
183-
|Name|Type|Description|
184-
|----|----|-----------|
185-
|`active`|`bool`|Whether the contract is active.|
186-
|`addressBytes`|`bytes`|The address of the contract.|
187-
188-
189-
### getContractConfiguration
190-
191-
Gets contract-specific configuration.
192-
193-
194-
```solidity
195-
function getContractConfiguration(
196-
uint256 chainId,
197-
string calldata contractType,
198-
string calldata key
199-
)
200-
external
201-
view
202-
returns (bytes memory);
203-
```
204-
**Parameters**
205-
206-
|Name|Type|Description|
207-
|----|----|-----------|
208-
|`chainId`|`uint256`|The ID of the chain where the contract is deployed.|
209-
|`contractType`|`string`|The type of the contract.|
210-
|`key`|`string`|The configuration key to retrieve.|
211-
212-
**Returns**
213-
214-
|Name|Type|Description|
215-
|----|----|-----------|
216-
|`<none>`|`bytes`|The value of the requested configuration.|
217-
218-
219-
### getZRC20TokenInfo
220-
221-
Gets information about a specific ZRC20 token.
222-
223-
224-
```solidity
225-
function getZRC20TokenInfo(address address_)
226-
external
227-
view
228-
returns (
229-
bool active,
230-
string memory symbol,
231-
uint256 originChainId,
232-
bytes memory originAddress,
233-
string memory coinType,
234-
uint8 decimals
235-
);
236-
```
237-
**Parameters**
238-
239-
|Name|Type|Description|
240-
|----|----|-----------|
241-
|`address_`|`address`|The address of the ZRC20 token.|
242-
243-
**Returns**
244-
245-
|Name|Type|Description|
246-
|----|----|-----------|
247-
|`active`|`bool`|Whether the token is active.|
248-
|`symbol`|`string`|The symbol of the token|
249-
|`originChainId`|`uint256`|The ID of the foreign chain where the original asset exists.|
250-
|`originAddress`|`bytes`|The address or identifier of the asset on its native chain.|
251-
|`coinType`|`string`|The type of the original coin.|
252-
|`decimals`|`uint8`|The number of decimals the token uses.|
253-
254-
255-
### getZRC20AddressByForeignAsset
256-
257-
Gets the ZRC20 token address for a specific asset on a foreign chain.
258-
259-
260-
```solidity
261-
function getZRC20AddressByForeignAsset(
262-
uint256 originChainId,
263-
bytes calldata originAddress
264-
)
265-
external
266-
view
267-
returns (address);
268-
```
269-
**Parameters**
270-
271-
|Name|Type|Description|
272-
|----|----|-----------|
273-
|`originChainId`|`uint256`|The ID of the foreign chain.|
274-
|`originAddress`|`bytes`|The address or identifier of the asset on its native chain.|
275-
276-
**Returns**
277-
278-
|Name|Type|Description|
279-
|----|----|-----------|
280-
|`<none>`|`address`|The address of the corresponding ZRC20 token on ZetaChain.|
281-
282-
283-
### getActiveChains
284-
285-
Gets all active chains in the registry.
286-
287-
288-
```solidity
289-
function getActiveChains() external view returns (uint256[] memory);
290-
```
291-
**Returns**
292-
293-
|Name|Type|Description|
294-
|----|----|-----------|
295-
|`<none>`|`uint256[]`|Array of chain IDs for all active chains.|
296-
29715

docs/src/contracts/zevm/interfaces/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22

33
# Contents
4+
- [ICoreRegistry](ICoreRegistry.sol/interface.ICoreRegistry.md)
45
- [IGatewayZEVMEvents](IGatewayZEVM.sol/interface.IGatewayZEVMEvents.md)
56
- [IGatewayZEVMErrors](IGatewayZEVM.sol/interface.IGatewayZEVMErrors.md)
67
- [IGatewayZEVM](IGatewayZEVM.sol/interface.IGatewayZEVM.md)
@@ -14,4 +15,4 @@
1415
- [zContext](UniversalContract.sol/struct.zContext.md)
1516
- [zContract](UniversalContract.sol/interface.zContract.md)
1617
- [MessageContext](UniversalContract.sol/struct.MessageContext.md)
17-
- [UniversalContract](UniversalContract.sol/interface.UniversalContract.md)
18+
- [UniversalContract](UniversalContract.sol/abstract.UniversalContract.md)

0 commit comments

Comments
 (0)