Skip to content

Commit

Permalink
refactor CrossChain into core-modules (#1756)
Browse files Browse the repository at this point in the history
* refactor CrossChain into core-modules

* fix tests

* fix comment

* fix ccip tests
  • Loading branch information
barrasso authored Aug 15, 2023
1 parent 698ffd2 commit 7c92934
Show file tree
Hide file tree
Showing 22 changed files with 257 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "@synthetixio/core-contracts/contracts/errors/ParameterError.sol";
import "@synthetixio/core-contracts/contracts/initializable/InitializableMixin.sol";
import "@synthetixio/core-contracts/contracts/utils/SafeCast.sol";
import "@synthetixio/core-contracts/contracts/ownership/OwnableStorage.sol";
import "@synthetixio/main/contracts/storage/CrossChain.sol";
import "@synthetixio/core-modules/contracts/storage/CrossChain.sol";
import "@synthetixio/core-contracts/contracts/proxy/ProxyStorage.sol";
import "../../interfaces/IElectionModule.sol";
import "../../submodules/election/ElectionCredentials.sol";
Expand Down
12 changes: 12 additions & 0 deletions protocol/governance/contracts/modules/core/CrossChainModule.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

import {CrossChainModule as BaseCrossChainModule} from "@synthetixio/core-modules/contracts/modules/CrossChainModule.sol";

/**
* @title Module that handles anything related to cross-chain.
*/
// solhint-disable-next-line no-empty-blocks
contract CrossChainModule is BaseCrossChainModule {

}
40 changes: 24 additions & 16 deletions protocol/governance/storage.dump.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ library SetUtil {
}
}

// @custom:artifact @synthetixio/core-modules/contracts/modules/CrossChainModule.sol:CrossChainModule
contract CrossChainModule {
bytes32 private constant _USD_TOKEN = "USDToken";
bytes32 private constant _CCIP_CHAINLINK_SEND = "ccipChainlinkSend";
bytes32 private constant _CCIP_CHAINLINK_RECV = "ccipChainlinkRecv";
bytes32 private constant _CCIP_CHAINLINK_TOKEN_POOL = "ccipChainlinkTokenPool";
}

// @custom:artifact @synthetixio/core-modules/contracts/modules/NftModule.sol:NftModule
contract NftModule {
bytes32 internal constant _INITIALIZED_NAME = "NftModule";
Expand All @@ -117,22 +125,9 @@ library AssociatedSystem {
}
}

// @custom:artifact @synthetixio/core-modules/contracts/storage/Initialized.sol:Initialized
library Initialized {
struct Data {
bool initialized;
}
function load(bytes32 id) internal pure returns (Data storage store) {
bytes32 s = keccak256(abi.encode("io.synthetix.code-modules.Initialized", id));
assembly {
store.slot := s
}
}
}

// @custom:artifact @synthetixio/main/contracts/storage/CrossChain.sol:CrossChain
// @custom:artifact @synthetixio/core-modules/contracts/storage/CrossChain.sol:CrossChain
library CrossChain {
bytes32 private constant _SLOT_CROSS_CHAIN = keccak256(abi.encode("io.synthetix.synthetix.CrossChain"));
bytes32 private constant _SLOT_CROSS_CHAIN = keccak256(abi.encode("io.synthetix.core-modules.CrossChain"));
struct Data {
address ccipRouter;
SetUtil.UintSet supportedNetworks;
Expand All @@ -147,7 +142,20 @@ library CrossChain {
}
}

// @custom:artifact @synthetixio/main/contracts/utils/CcipClient.sol:CcipClient
// @custom:artifact @synthetixio/core-modules/contracts/storage/Initialized.sol:Initialized
library Initialized {
struct Data {
bool initialized;
}
function load(bytes32 id) internal pure returns (Data storage store) {
bytes32 s = keccak256(abi.encode("io.synthetix.code-modules.Initialized", id));
assembly {
store.slot := s
}
}
}

// @custom:artifact @synthetixio/core-modules/contracts/utils/CcipClient.sol:CcipClient
library CcipClient {
bytes4 public constant EVM_EXTRA_ARGS_V1_TAG = 0x97a657c9;
struct EVMTokenAmount {
Expand Down
7 changes: 2 additions & 5 deletions protocol/synthetix/cannonfile.test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ artifact = "contracts/generated/test/TestableCollateralConfigurationStorage.sol:
[contract.TestableCollateralLockStorage]
artifact = "contracts/generated/test/TestableCollateralLockStorage.sol:TestableCollateralLockStorage"

[contract.TestableCrossChainStorage]
artifact = "contracts/generated/test/TestableCrossChainStorage.sol:TestableCrossChainStorage"

[contract.TestableDistributionStorage]
artifact = "contracts/generated/test/TestableDistributionStorage.sol:TestableDistributionStorage"

Expand Down Expand Up @@ -72,6 +69,7 @@ contracts = [
"CcipReceiverModule",
"CollateralModule",
"CollateralConfigurationModule",
"CrossChainModule",
"CrossChainUSDModule",
"IssueUSDModule",
"LiquidationModule",
Expand All @@ -88,7 +86,6 @@ contracts = [
"TestableCollateralStorage",
"TestableCollateralConfigurationStorage",
"TestableCollateralLockStorage",
"TestableCrossChainStorage",
"TestableDistributionStorage",
"TestableDistributionActorStorage",
"TestableMarketStorage",
Expand All @@ -111,6 +108,7 @@ depends = [
"contract.CcipReceiverModule",
"contract.CollateralModule",
"contract.CollateralConfigurationModule",
"contract.CrossChainModule",
"contract.CrossChainUSDModule",
"contract.IssueUSDModule",
"contract.LiquidationModule",
Expand All @@ -127,7 +125,6 @@ depends = [
"contract.TestableCollateralStorage",
"contract.TestableCollateralConfigurationStorage",
"contract.TestableCollateralLockStorage",
"contract.TestableCrossChainStorage",
"contract.TestableDistributionStorage",
"contract.TestableDistributionActorStorage",
"contract.TestableMarketStorage",
Expand Down
5 changes: 5 additions & 0 deletions protocol/synthetix/cannonfile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ artifact = "contracts/modules/core/CollateralModule.sol:CollateralModule"
[contract.CollateralConfigurationModule]
artifact = "contracts/modules/core/CollateralConfigurationModule.sol:CollateralConfigurationModule"

[contract.CrossChainModule]
artifact = "contracts/modules/core/CrossChainModule.sol:CrossChainModule"

[contract.CrossChainUSDModule]
artifact = "contracts/modules/core/CrossChainUSDModule.sol:CrossChainUSDModule"

Expand Down Expand Up @@ -99,6 +102,7 @@ contracts = [
"CcipReceiverModule",
"CollateralModule",
"CollateralConfigurationModule",
"CrossChainModule",
"CrossChainUSDModule",
"IssueUSDModule",
"LiquidationModule",
Expand All @@ -121,6 +125,7 @@ depends = [
"contract.CcipReceiverModule",
"contract.CollateralModule",
"contract.CollateralConfigurationModule",
"contract.CrossChainModule",
"contract.CrossChainUSDModule",
"contract.IssueUSDModule",
"contract.LiquidationModule",
Expand Down
25 changes: 0 additions & 25 deletions protocol/synthetix/contracts/interfaces/IUtilsModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,6 @@ import {IERC165} from "@synthetixio/core-contracts/contracts/interfaces/IERC165.
* @title Module with assorted utility functions.
*/
interface IUtilsModule is IERC165 {
/**
* @notice Emitted when a new cross chain network becomes supported by the protocol
*/
event NewSupportedCrossChainNetwork(uint64 newChainId);

/**
* @notice Configure CCIP addresses on the stablecoin.
* @param ccipRouter The address on this chain to which CCIP messages will be sent or received.
* @param ccipTokenPool The address where CCIP fees will be sent to when sending and receiving cross chain messages.
*/
function configureChainlinkCrossChain(address ccipRouter, address ccipTokenPool) external;

/**
* @notice Used to add new cross chain networks to the protocol
* Ignores a network if it matches the current chain id
* Ignores a network if it has already been added
* @param supportedNetworks array of all networks that are supported by the protocol
* @param ccipSelectors the ccip "selector" which maps to the chain id on the same index. must be same length as `supportedNetworks`
* @return numRegistered the number of networks that were actually registered
*/
function setSupportedCrossChainNetworks(
uint64[] memory supportedNetworks,
uint64[] memory ccipSelectors
) external returns (uint256 numRegistered);

/**
* @notice Configure the system's single oracle manager address.
* @param oracleManagerAddress The address of the oracle manager.
Expand Down
3 changes: 1 addition & 2 deletions protocol/synthetix/contracts/mocks/CcipRouterMock.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.4;

import "../interfaces/external/ICcipRouterClient.sol";
import "@synthetixio/core-modules/contracts/interfaces/external/ICcipRouterClient.sol";

contract CcipRouterMock {
// solhint-disable no-empty-blocks
Expand Down
20 changes: 5 additions & 15 deletions protocol/synthetix/contracts/modules/core/CcipReceiverModule.sol
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

import "@synthetixio/core-modules/contracts/interfaces/IAssociatedSystemsModule.sol";
import "@synthetixio/core-modules/contracts/storage/AssociatedSystem.sol";
import "@synthetixio/core-contracts/contracts/ownership/OwnableStorage.sol";

import "../../interfaces/external/IAny2EVMMessageReceiver.sol";

import "../../storage/OracleManager.sol";
import "../../storage/Config.sol";
import "../../storage/CrossChain.sol";
import {CcipReceiverModule as BaseCcipReceiverModule} from "@synthetixio/core-modules/contracts/modules/CcipReceiverModule.sol";

/**
* @title Module with assorted utility functions.
* @dev See IUtilsModule.
* @title Module that handles receiving ccip messages.
*/
contract CcipReceiverModule is IAny2EVMMessageReceiver {
function ccipReceive(CcipClient.Any2EVMMessage memory message) external {
CrossChain.processCcipReceive(CrossChain.load(), message);
}
// solhint-disable-next-line no-empty-blocks
contract CcipReceiverModule is BaseCcipReceiverModule {

}
12 changes: 12 additions & 0 deletions protocol/synthetix/contracts/modules/core/CrossChainModule.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

import {CrossChainModule as BaseCrossChainModule} from "@synthetixio/core-modules/contracts/modules/CrossChainModule.sol";

/**
* @title Module that handles anything related to cross-chain.
*/
// solhint-disable-next-line no-empty-blocks
contract CrossChainModule is BaseCrossChainModule {

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ pragma solidity >=0.8.11 <0.9.0;
import "../../interfaces/ICrossChainUSDModule.sol";
import "@synthetixio/core-modules/contracts/interfaces/ITokenModule.sol";

import "../../storage/CrossChain.sol";

import "@synthetixio/core-modules/contracts/storage/AssociatedSystem.sol";
import "@synthetixio/core-modules/contracts/storage/FeatureFlag.sol";
import "@synthetixio/core-modules/contracts/storage/CrossChain.sol";

/**
* @title Module for the cross-chain transfers of stablecoins.
Expand Down
70 changes: 1 addition & 69 deletions protocol/synthetix/contracts/modules/core/UtilsModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,18 @@ pragma solidity >=0.8.11 <0.9.0;
import "@synthetixio/core-modules/contracts/interfaces/IAssociatedSystemsModule.sol";
import "@synthetixio/core-modules/contracts/storage/AssociatedSystem.sol";
import "@synthetixio/core-contracts/contracts/ownership/OwnableStorage.sol";
import "@synthetixio/core-contracts/contracts/errors/ParameterError.sol";
import "@synthetixio/core-contracts/contracts/utils/SafeCast.sol";

import "../../interfaces/IUtilsModule.sol";

import "../../storage/CrossChain.sol";
import "../../storage/OracleManager.sol";
import "../../storage/Config.sol";

import "../../interfaces/external/IAny2EVMMessageReceiver.sol";

/**
* @title Module with assorted utility functions.
* @dev See IUtilsModule.
*/
contract UtilsModule is IUtilsModule {
using AssociatedSystem for AssociatedSystem.Data;
using SetUtil for SetUtil.UintSet;
using SafeCastU256 for uint256;

bytes32 private constant _USD_TOKEN = "USDToken";
bytes32 private constant _CCIP_CHAINLINK_SEND = "ccipChainlinkSend";
bytes32 private constant _CCIP_CHAINLINK_RECV = "ccipChainlinkRecv";
bytes32 private constant _CCIP_CHAINLINK_TOKEN_POOL = "ccipChainlinkTokenPool";

/**
* @inheritdoc IUtilsModule
*/
function configureChainlinkCrossChain(
address ccipRouter,
address ccipTokenPool
) external override {
OwnableStorage.onlyOwner();

CrossChain.Data storage cc = CrossChain.load();

cc.ccipRouter = ICcipRouterClient(ccipRouter);

IAssociatedSystemsModule usdToken = IAssociatedSystemsModule(
AssociatedSystem.load(_USD_TOKEN).proxy
);

usdToken.registerUnmanagedSystem(_CCIP_CHAINLINK_SEND, ccipRouter);
usdToken.registerUnmanagedSystem(_CCIP_CHAINLINK_RECV, ccipRouter);
usdToken.registerUnmanagedSystem(_CCIP_CHAINLINK_TOKEN_POOL, ccipTokenPool);
}

/**
* @inheritdoc IUtilsModule
*/
function setSupportedCrossChainNetworks(
uint64[] memory supportedNetworks,
uint64[] memory ccipSelectors
) external returns (uint256 numRegistered) {
OwnableStorage.onlyOwner();

uint64 myChainId = block.chainid.to64();

if (ccipSelectors.length != supportedNetworks.length) {
revert ParameterError.InvalidParameter("ccipSelectors", "must match length");
}

CrossChain.Data storage cc = CrossChain.load();
for (uint i = 0; i < supportedNetworks.length; i++) {
if (supportedNetworks[i] == myChainId) continue;
if (
supportedNetworks[i] != myChainId &&
!cc.supportedNetworks.contains(supportedNetworks[i])
) {
numRegistered++;
cc.supportedNetworks.add(supportedNetworks[i]);
emit NewSupportedCrossChainNetwork(supportedNetworks[i]);
}

cc.ccipChainIdToSelector[supportedNetworks[i]] = ccipSelectors[i];
cc.ccipSelectorToChainId[ccipSelectors[i]] = supportedNetworks[i];
}
}

/**
* @inheritdoc IUtilsModule
Expand Down Expand Up @@ -113,8 +47,6 @@ contract UtilsModule is IUtilsModule {
function supportsInterface(
bytes4 interfaceId
) public view virtual override(IERC165) returns (bool) {
return
interfaceId == type(IAny2EVMMessageReceiver).interfaceId ||
interfaceId == this.supportsInterface.selector;
return interfaceId == this.supportsInterface.selector;
}
}
3 changes: 1 addition & 2 deletions protocol/synthetix/contracts/modules/usd/USDTokenModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
pragma solidity ^0.8.7;

import "../../interfaces/IUSDTokenModule.sol";
import "../../storage/CrossChain.sol";

import "@synthetixio/core-modules/contracts/storage/AssociatedSystem.sol";
import "@synthetixio/core-modules/contracts/storage/CrossChain.sol";
import "@synthetixio/core-contracts/contracts/token/ERC20.sol";
import "@synthetixio/core-modules/contracts/storage/FeatureFlag.sol";
import "@synthetixio/core-contracts/contracts/initializable/InitializableMixin.sol";
Expand Down
Loading

0 comments on commit 7c92934

Please sign in to comment.