Skip to content

Instantly share code, notes, and snippets.

@jdkanani
Last active November 30, 2021 06:23
Show Gist options
  • Save jdkanani/90054eef7b9977e67367770483b4adda to your computer and use it in GitHub Desktop.
Save jdkanani/90054eef7b9977e67367770483b4adda to your computer and use it in GitHub Desktop.
Cross PoS Portal ERC20 token transfer from Polygon PoS chain to Ethereum chain through contract
// SPDX-License-Identifier: Unlicensed
pragma solidity ^0.8.9;
interface IERC20 {
function withdraw(uint256 amount) external payable;
function transfer(address to, uint256 value) external returns (bool);
function balanceOf(address who) external view returns (uint256);
}
interface IRootChainManager {
function exit(bytes memory _data) external;
}
abstract contract Ownable {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor () {
address msgSender = msg.sender;
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(owner() == msg.sender, "Ownable: caller is not the owner");
_;
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
contract CrossTransfer is Ownable {
IERC20 public immutable rootToken;
IERC20 public immutable childToken;
uint24 public immutable rootChainId;
uint24 public immutable childChainId;
IRootChainManager public immutable rootChainManager;
constructor(
IERC20 _rootToken,
IERC20 _childToken,
IRootChainManager _rootChainManager,
uint24 _rootChainId,
uint24 _childChainId
) {
rootToken = _rootToken;
childToken = _childToken;
rootChainManager = _rootChainManager;
rootChainId = _rootChainId;
childChainId = _childChainId;
}
modifier onlyRootChain() {
require(block.chainid == rootChainId, "ONLY_ROOT");
_;
}
modifier onlyChildChain() {
require(block.chainid == childChainId, "ONLY_CHILD");
_;
}
function withdrawOnPolygon(address token, uint256 amount) public onlyOwner onlyChildChain {
// Calling withdraw method in ChildERC20
// https://github.com/maticnetwork/pos-portal/blob/master/contracts/child/ChildToken/UpgradeableChildERC20/UChildERC20.sol#L81
IERC20(token).withdraw(amount);
}
function withdrawOnEthereum(bytes memory data) public onlyOwner onlyRootChain {
// Calling exit method in bridge's RootChainManager on Ethereum
// https://github.com/maticnetwork/pos-portal/blob/master/contracts/root/RootChainManager/RootChainManager.sol#L336
rootChainManager.exit(data);
//
// TODO process received tokens
//
}
}
@jdkanani
Copy link
Author

jdkanani commented Nov 30, 2021

This test contract allows anyone to move PoS Portal ERC20 tokens from Polygon to Ethereum chain through the contract to contract. It doesn't require EOA to take control to move funds.

The same contract must be deployed on Polygon and Ethereum chains at the same address. You can use Create2 or the same deployer address with the same nonce to deploy.

Once deployed, transfer the desired tokens to this contract on Polygon and call the withdrawOnPolygon function to withdraw tokens to Ethereum. Once this transaction is included in the checkpoint, call the withdrawOnEthereum function on the Ethereum chain to receive the tokens and process them.

Note that this contract is not tested on testnet or mainnet. The deployer must modify as per their needs and test the whole flow before using it in production.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment