Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use ChainId type instead of uint (#823) #825

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions contracts/registry/ChainNft.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.20;
import {ERC721, ERC721Enumerable} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";

import {ChainId, ChainIdLib} from "../type/ChainId.sol";
import {ITransferInterceptor} from "./ITransferInterceptor.sol";

contract ChainNft is ERC721Enumerable {
Expand Down Expand Up @@ -53,7 +54,7 @@ contract ChainNft is ERC721Enumerable {
// NFT contract is deployed by the registry
_registry = registry;

_chainIdDigits = _calculateChainIdDigits(block.chainid);
_chainIdDigits = _calculateChainIdDigits(ChainIdLib.toChainId(block.chainid));
_chainIdMultiplier = 10 ** _chainIdDigits;

// the first object registered through normal registration starts with id 4
Expand Down Expand Up @@ -200,12 +201,12 @@ contract ChainNft is ERC721Enumerable {
* (42 * 10 ** 10 + 9876543210) * 100 + 10
* (index * 10 ** digits + chainid) * 100 + digits (1 < digits < 100)
*/
function calculateTokenId(uint256 idIndex, uint256 chainId) public view returns (uint256 id) {
if(chainId == block.chainid) {
return 100 * (idIndex * _chainIdMultiplier + chainId) + _chainIdDigits;
function calculateTokenId(uint256 idIndex, ChainId chainId) public view returns (uint256 id) {
if(chainId.toInt() == block.chainid) {
return 100 * (idIndex * _chainIdMultiplier + chainId.toInt()) + _chainIdDigits;
} else {
uint256 chainIdDigits = _calculateChainIdDigits(chainId);
return 100 * (idIndex * (10 ** chainIdDigits) + chainId) + chainIdDigits;
return 100 * (idIndex * (10 ** chainIdDigits) + chainId.toInt()) + chainIdDigits;
}
}

Expand All @@ -224,8 +225,8 @@ contract ChainNft is ERC721Enumerable {
_idNext++;
}

function _calculateChainIdDigits(uint256 chainId) private pure returns (uint256) {
uint256 num = chainId;
function _calculateChainIdDigits(ChainId chainId) private pure returns (uint256) {
uint256 num = chainId.toInt();
uint256 digits = 0;
while (num != 0) {
digits++;
Expand Down
17 changes: 9 additions & 8 deletions contracts/registry/IRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";

import {IRelease} from "./IRelease.sol";

import {ChainId} from "../type/ChainId.sol";
import {NftId} from "../type/NftId.sol";
import {ObjectType} from "../type/ObjectType.sol";
import {VersionPart} from "../type/Version.sol";
Expand All @@ -21,7 +22,7 @@ interface IRegistry is

event LogRegistryObjectRegistered(NftId indexed nftId, NftId indexed parentNftId, ObjectType indexed objectType, bool isInterceptor, address objectAddress, address initialOwner);
event LogRegistryServiceRegistered(NftId indexed nftId, VersionPart indexed majorVersion, ObjectType indexed domain);
event LogRegistryChainRegistryRegistered(NftId indexed nftId, uint256 indexed chainId, address indexed chainRegistryAddress);
event LogRegistryChainRegistryRegistered(NftId indexed nftId, ChainId indexed chainId, address indexed chainRegistryAddress);

// initialize
error ErrorRegistryCallerNotDeployer();
Expand All @@ -30,11 +31,11 @@ interface IRegistry is
error ErrorRegistryObjectTypeNotSupported(ObjectType objectType);

// registerRegistry()
error ErrorRegistryNotOnMainnet(uint256 chainId);
error ErrorRegistryNotOnMainnet(ChainId chainId);
error ErrorRegistryChainRegistryChainIdZero(NftId nftId);
error ErrorRegistryChainRegistryAddressZero(NftId nftId, uint256 chainId);
error ErrorRegistryChainRegistryNftIdInvalid(NftId nftId, uint256 chainId);
error ErrorRegistryChainRegistryAlreadyRegistered(NftId nftId, uint256 chainId);
error ErrorRegistryChainRegistryAddressZero(NftId nftId, ChainId chainId);
error ErrorRegistryChainRegistryNftIdInvalid(NftId nftId, ChainId chainId);
error ErrorRegistryChainRegistryAlreadyRegistered(NftId nftId, ChainId chainId);

// registerService()
error ErrorRegistryServiceAddressZero();
Expand Down Expand Up @@ -71,7 +72,7 @@ interface IRegistry is
/// Only one chain registry may be registered per chain
function registerRegistry(
NftId nftId,
uint256 chainId,
ChainId chainId,
address chainRegistryAddress
) external;

Expand Down Expand Up @@ -107,10 +108,10 @@ interface IRegistry is
function chainIds() external view returns (uint256);

/// @dev Returns the chain id at the specified index.
function getChainId(uint256 idx) external view returns (uint256);
function getChainId(uint256 idx) external view returns (ChainId);

/// @dev Returns the NFT ID of the registry for the specified chain.
function getRegistryNftId(uint256 chainId) external returns (NftId nftId);
function getRegistryNftId(ChainId chainId) external returns (NftId nftId);

function getObjectCount() external view returns (uint256);

Expand Down
21 changes: 11 additions & 10 deletions contracts/registry/Registry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {NftId, NftIdLib} from "../type/NftId.sol";
import {VersionPart, VersionPartLib} from "../type/Version.sol";
import {ObjectType, ObjectTypeLib, PROTOCOL, REGISTRY, SERVICE, INSTANCE, STAKE, STAKING, PRODUCT, DISTRIBUTION, DISTRIBUTOR, ORACLE, POOL, POLICY, BUNDLE} from "../type/ObjectType.sol";

import {ChainId, ChainIdLib} from "../type/ChainId.sol";
import {ChainNft} from "./ChainNft.sol";
import {IRegistry} from "./IRegistry.sol";
import {IRelease} from "./IRelease.sol";
Expand Down Expand Up @@ -63,8 +64,8 @@ contract Registry is
string public constant EMPTY_URI = "";

/// @dev keep track of different registries on different chains
mapping(uint256 chainId => NftId registryNftId) private _registryNftIdByChainId;
uint256[] private _chainId;
mapping(ChainId chainId => NftId registryNftId) private _registryNftIdByChainId;
ChainId[] private _chainId;

/// @dev keep track of object info and address reverse lookup
mapping(NftId nftId => ObjectInfo info) private _info;
Expand Down Expand Up @@ -143,19 +144,19 @@ contract Registry is
/// @inheritdoc IRegistry
function registerRegistry(
NftId nftId,
uint256 chainId,
ChainId chainId,
address registryAddress
)
external
restricted()
{
// registration of chain registries only allowed on mainnet
if (block.chainid != 1) {
revert ErrorRegistryNotOnMainnet(block.chainid);
revert ErrorRegistryNotOnMainnet(ChainIdLib.toChainId(block.chainid));
}

// registry chain id is not zero
if(chainId == 0) {
if(chainId.eqz()) {
revert ErrorRegistryChainRegistryChainIdZero(nftId);
}

Expand Down Expand Up @@ -320,11 +321,11 @@ contract Registry is
return _chainId.length;
}

function getChainId(uint256 idx) public view returns (uint256) {
function getChainId(uint256 idx) public view returns (ChainId) {
return _chainId[idx];
}

function getRegistryNftId(uint256 chainId) public view returns (NftId nftId) {
function getRegistryNftId(ChainId chainId) public view returns (NftId nftId) {
return _registryNftIdByChainId[chainId];
}

Expand Down Expand Up @@ -579,7 +580,7 @@ contract Registry is

// register global registry
_registerRegistryForNft(
1, // mainnet chain id
ChainIdLib.toChainId(1), // mainnet chain id
ObjectInfo({
nftId: GLOBAL_REGISTRY_NFT_ID,
parentNftId: PROTOCOL_NFT_ID,
Expand All @@ -598,7 +599,7 @@ contract Registry is
CHAIN_NFT.calculateTokenId(REGISTRY_TOKEN_SEQUENCE_ID));

_registerRegistryForNft(
block.chainid,
ChainIdLib.toChainId(block.chainid),
ObjectInfo({
nftId: registryNftId,
parentNftId: GLOBAL_REGISTRY_NFT_ID,
Expand All @@ -613,7 +614,7 @@ contract Registry is

/// @dev staking registration
function _registerRegistryForNft(
uint256 chainId,
ChainId chainId,
ObjectInfo memory info,
bool updateAddressLookup
)
Expand Down
1 change: 0 additions & 1 deletion contracts/staking/StakingService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ contract StakingService is
virtual
restricted()
{
uint256 chainId = block.chainid;
_getStakingServiceStorage()._staking.registerTarget(
targetNftId,
INSTANCE(),
Expand Down
1 change: 1 addition & 0 deletions scripts/libs/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export async function deployAndInitializeRegistry(owner: Signer, libraries: Libr
[registryAdminAddress, globalRegistry],
{
libraries: {
ChainIdLib: libraries.chainIdLibAddress,
NftIdLib: libraries.nftIdLibAddress,
ObjectTypeLib: libraries.objectTypeLibAddress,
VersionPartLib: libraries.versionPartLibAddress,
Expand Down
17 changes: 17 additions & 0 deletions test/base/GifTestHelper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import {ChainId} from "../../contracts/type/ChainId.sol";
import {StdAssertions} from "forge-std/StdAssertions.sol";

contract GifTestHelper is StdAssertions {

function assertEqChainId(ChainId chainId1, ChainId chainId2) public pure {
assertEq(chainId1.toInt(), chainId2.toInt(), "ChainId not equal");
}

function assertEqChainId(ChainId chainId1, ChainId chainId2, string memory err) public pure {
assertEq(chainId1.toInt(), chainId2.toInt(), err);
}

}
Loading
Loading