Created
May 9, 2025 07:31
-
-
Save wenakita/041e2f0e52f4802fc5610a4615c58fe7 to your computer and use it in GitHub Desktop.
ChainRegistry Contract for Verification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity 0.8.24; | |
contract ChainRegistry { | |
// Storage variables | |
address private _owner; | |
mapping(uint16 => ChainConfig) private chainConfigs; | |
uint16[] private supportedChains; | |
uint16 private currentChainId; | |
// Events | |
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); | |
event ChainRegistered(uint16 indexed chainId, string chainName); | |
event ChainUpdated(uint16 indexed chainId); | |
event ChainStatusChanged(uint16 indexed chainId, bool isActive); | |
event CurrentChainSet(uint16 indexed chainId); | |
// Main data structure | |
struct ChainConfig { | |
uint16 chainId; | |
string chainName; | |
address wrappedNativeToken; | |
address swapTrigger; | |
address vrfConsumer; | |
address dragonToken; | |
bool isActive; | |
} | |
constructor() { | |
_owner = msg.sender; | |
emit OwnershipTransferred(address(0), _owner); | |
currentChainId = 332; | |
} | |
modifier onlyOwner() { | |
require(_owner == msg.sender, "Ownable: caller is not the owner"); | |
_; | |
} | |
function owner() public view returns (address) { | |
return _owner; | |
} | |
function transferOwnership(address newOwner) public onlyOwner { | |
require(newOwner != address(0), "Ownable: new owner is the zero address"); | |
address oldOwner = _owner; | |
_owner = newOwner; | |
emit OwnershipTransferred(oldOwner, newOwner); | |
} | |
function renounceOwnership() public onlyOwner { | |
address oldOwner = _owner; | |
_owner = address(0); | |
emit OwnershipTransferred(oldOwner, address(0)); | |
} | |
function setCurrentChainId(uint16 _chainId) external onlyOwner { | |
currentChainId = _chainId; | |
emit CurrentChainSet(_chainId); | |
} | |
function registerChain( | |
uint16 _chainId, | |
string calldata _chainName, | |
address _wrappedNativeToken, | |
address _swapTrigger, | |
address _vrfConsumer, | |
address _dragonToken | |
) external onlyOwner { | |
require(chainConfigs[_chainId].chainId != _chainId, "ChainAlreadyRegistered"); | |
require(_wrappedNativeToken != address(0), "ZeroAddress"); | |
chainConfigs[_chainId] = ChainConfig({ | |
chainId: _chainId, | |
chainName: _chainName, | |
wrappedNativeToken: _wrappedNativeToken, | |
swapTrigger: _swapTrigger, | |
vrfConsumer: _vrfConsumer, | |
dragonToken: _dragonToken, | |
isActive: true | |
}); | |
supportedChains.push(_chainId); | |
if (supportedChains.length == 1) { | |
currentChainId = _chainId; | |
emit CurrentChainSet(_chainId); | |
} | |
emit ChainRegistered(_chainId, _chainName); | |
} | |
function updateChain( | |
uint16 _chainId, | |
address _wrappedNativeToken, | |
address _swapTrigger, | |
address _vrfConsumer, | |
address _dragonToken | |
) external onlyOwner { | |
require(chainConfigs[_chainId].chainId == _chainId, "ChainNotRegistered"); | |
require(_wrappedNativeToken != address(0), "ZeroAddress"); | |
ChainConfig storage config = chainConfigs[_chainId]; | |
config.wrappedNativeToken = _wrappedNativeToken; | |
if (_swapTrigger != address(0)) { | |
config.swapTrigger = _swapTrigger; | |
} | |
if (_vrfConsumer != address(0)) { | |
config.vrfConsumer = _vrfConsumer; | |
} | |
if (_dragonToken != address(0)) { | |
config.dragonToken = _dragonToken; | |
} | |
emit ChainUpdated(_chainId); | |
} | |
function setChainActive(uint16 _chainId, bool _isActive) external onlyOwner { | |
require(chainConfigs[_chainId].chainId == _chainId, "ChainNotRegistered"); | |
chainConfigs[_chainId].isActive = _isActive; | |
emit ChainStatusChanged(_chainId, _isActive); | |
} | |
function getChainConfig(uint16 _chainId) external view returns (ChainConfig memory) { | |
require(chainConfigs[_chainId].chainId == _chainId, "ChainNotRegistered"); | |
return chainConfigs[_chainId]; | |
} | |
function getCurrentChainId() external view returns (uint16) { | |
return currentChainId; | |
} | |
function getWrappedNativeToken(uint16 chainId) external view returns (address) { | |
require(chainConfigs[chainId].chainId == chainId, "ChainNotRegistered"); | |
return chainConfigs[chainId].wrappedNativeToken; | |
} | |
function getSwapTrigger(uint16 _chainId) external view returns (address) { | |
require(chainConfigs[_chainId].chainId == _chainId, "ChainNotRegistered"); | |
return chainConfigs[_chainId].swapTrigger; | |
} | |
function getVRFConsumer(uint16 _chainId) external view returns (address) { | |
require(chainConfigs[_chainId].chainId == _chainId, "ChainNotRegistered"); | |
return chainConfigs[_chainId].vrfConsumer; | |
} | |
function getDragonToken(uint16 _chainId) external view returns (address) { | |
require(chainConfigs[_chainId].chainId == _chainId, "ChainNotRegistered"); | |
return chainConfigs[_chainId].dragonToken; | |
} | |
function getSupportedChains() external view returns (uint16[] memory) { | |
return supportedChains; | |
} | |
function isChainSupported(uint16 chainId) external view returns (bool) { | |
return chainConfigs[chainId].chainId == chainId; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment