Skip to content

Commit

Permalink
Merge pull request #76 from theref/xchain
Browse files Browse the repository at this point in the history
Add cross chain messaging for stake info from ETH to Polygon
  • Loading branch information
KPrasch committed Jun 7, 2023
2 parents 5395250 + a6ed59b commit da35b9d
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ node_modules/
.build
contracts/.cache
env
.cache/
.cache/
10 changes: 10 additions & 0 deletions ape-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ dependencies:
- name: openzeppelin-upgradeable
github: OpenZeppelin/openzeppelin-contracts-upgradeable
version: 4.8.1
- name: fx-portal
github: 0xPolygon/fx-portal
version: 1.0.5

solidity:
version: 0.8.20
evm_version: paris
import_remapping:
- "@openzeppelin/contracts=openzeppelin/v4.8.1"
- "@openzeppelin-upgradeable/contracts=openzeppelin-upgradeable/v4.8.1"
- "@fx-portal/contracts=fx-portal/v1.0.5"

deployments:
polygon:
mumbai:
- fx_child: '0xCf73231F28B7331BBe3124B907840A94851f9f11'
- verify: False
ethereum:
local:
- nu_token_supply: 1_000_000_000
Expand Down Expand Up @@ -63,6 +71,8 @@ deployments:
max_dkg_size: 16
ritual_timeout: 86400 # one day in seconds
verify: True
checkpoint_manager: '0x2890bA17EfE978480615e330ecB65333b880928e'
fx_root: '0x3d1d3E34f7fB6D26245E6640E1c50710eFFf15bA'
mainnet:
- nu_token: '0x4fE83213D56308330EC302a8BD641f1d0113A4Cc'
t_staking: '0x01B67b1194C75264d06F808A921228a95C765dd7'
Expand Down
2 changes: 1 addition & 1 deletion contracts/contracts/coordination/StakeInfo.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

Expand Down
34 changes: 34 additions & 0 deletions contracts/xchain/PolygonChild.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@fx-portal/contracts/tunnel/FxBaseChildTunnel.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract PolygonChild is FxBaseChildTunnel, Ownable {

event MessageReceived(
address indexed sender,
bytes data
);

address public stakeInfoAddress;

constructor(address _fxChild) FxBaseChildTunnel(_fxChild) {}

function _processMessageFromRoot(
uint256 /* stateId */,
address sender,
bytes memory data
) internal override validateSender(sender) {
emit MessageReceived(sender, data);
(bool success, /* returnId */ ) = stakeInfoAddress.call(data);
}

function sendMessageToRoot(bytes memory message) public {
_sendMessageToRoot(message);
}

function setStakeInfoAddress(address _stakeInfoAddress) public onlyOwner {
stakeInfoAddress = _stakeInfoAddress;
}
}
30 changes: 30 additions & 0 deletions contracts/xchain/PolygonRoot.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@fx-portal/contracts/tunnel/FxBaseRootTunnel.sol";


contract PolygonRoot is FxBaseRootTunnel {
bytes public latestData;

constructor(address _checkpointManager, address _fxRoot) FxBaseRootTunnel(_checkpointManager, _fxRoot) {}

function _processMessageFromChild(bytes memory data) internal override {
latestData = data;
}

function updateOperator(address stakingProvider, address operator) public {
bytes memory message = abi.encodeWithSignature("updateOperator(address,address)", stakingProvider, operator);
_sendMessageToChild(message);
}

function updateAmount(address stakingProvider, uint96 amount) public {
bytes memory message = abi.encodeWithSignature("updateAmount(address,uint96)", stakingProvider, amount);
_sendMessageToChild(message);
}

function batchUpdate(bytes32[] calldata updateInfo) public {
bytes memory message = abi.encodeWithSignature("batchUpdate(bytes32[])", updateInfo);
_sendMessageToChild(message);
}
}
20 changes: 20 additions & 0 deletions scripts/check_xchain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import time

from ape import networks, project


def main():
print("*******")
print("WARNING: This script will take 40 mins to run to allow messages to sync from L1 to L2")
print("*******")
with networks.ethereum.goerli.use_provider("infura"):
root = project.PolygonRoot.at("0xdc90A337DF9561705EB85B92391ab8F55d114D53")
root.updateOperator(
"0x3B42d26E19FF860bC4dEbB920DD8caA53F93c600",
"0x3B42d26E19FF860bC4dEbB920DD8caA53F93c600",
)
time.sleep(60 * 40)
with networks.polygon.mumbai.use_provider("infura"):
stake_info = project.StakeInfo.at("0x40D0107ACa3503CB345E4117a9F92E8220EEEb3C")
print(stake_info.operatorToProvider("0xAe87D865F3A507185656aD0ef52a8E0B9f3d58f8"))
print(stake_info.operatorToProvider("0x3B42d26E19FF860bC4dEbB920DD8caA53F93c600"))
53 changes: 53 additions & 0 deletions scripts/deploy_xchain_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from ape import accounts, config, networks, project


def deploy_eth_contracts(deployer):
# Connect to the Ethereum network
with networks.ethereum.goerli.use_provider("infura"):
DEPLOYMENTS_CONFIG = config.get_config("deployments")["ethereum"]["goerli"][0]

# Deploy the FxStateRootTunnel contract
polygon_root = project.PolygonRoot.deploy(
DEPLOYMENTS_CONFIG.get("checkpoint_manager"),
DEPLOYMENTS_CONFIG.get("fx_root"),
sender=deployer,
publish=DEPLOYMENTS_CONFIG.get("verify"),
)

return polygon_root


def deploy_polygon_contracts(deployer):
# Connect to the Polygon network
with networks.polygon.mumbai.use_provider("infura"):
DEPLOYMENTS_CONFIG = config.get_config("deployments")["polygon"]["mumbai"][0]

# Deploy the FxStateChildTunnel contract
polygon_child = project.PolygonChild.deploy(
DEPLOYMENTS_CONFIG.get("fx_child"),
sender=deployer,
publish=DEPLOYMENTS_CONFIG.get("verify"),
)
stake_info = project.StakeInfo.deploy(
[deployer.address, polygon_child.address],
sender=deployer,
publish=DEPLOYMENTS_CONFIG.get("verify"),
)

return polygon_child, stake_info


def main(account_id=None):
deployer = accounts.load("TGoerli")
with accounts.use_sender(deployer):
root = deploy_eth_contracts(deployer)
child, stake_info = deploy_polygon_contracts(deployer)

# Set the root contract address in the child contract
with networks.polygon.mumbai.use_provider("infura"):
child.setFxRootTunnel(root.address)
child.setStakeInfoAddress(stake_info.address)

# Set the child contract address in the root contract
with networks.ethereum.goerli.use_provider("infura"):
root.setFxChildTunnel(child.address)
Empty file added tests/test_xchain.py
Empty file.

0 comments on commit da35b9d

Please sign in to comment.