Skip to content

Commit

Permalink
add contracts v0.4 support, NFT support and eccm with chain-id
Browse files Browse the repository at this point in the history
  • Loading branch information
zouxyan committed Jan 13, 2021
1 parent c9212e4 commit 4f1ec11
Show file tree
Hide file tree
Showing 71 changed files with 16,070 additions and 45 deletions.
8 changes: 4 additions & 4 deletions contracts/core/assets/usdt/USDT.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// /**
// *Submitted for verification at Etherscan.io on 2017-11-28
// */
/**
*Submitted for verification at Etherscan.io on 2017-11-28
*/

// pragma solidity ^0.4.17;
pragma solidity ^0.5.0;

// /**
// * @title SafeMath
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity >=0.5.0;

/**
* @dev Interface of the EthCrossChainManager contract for business contract like LockProxy to request cross chain transaction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity >=0.5.0;

/**
* @dev Interface of the EthCrossChainManagerProxy for business contract like LockProxy to obtain the reliable EthCrossChainManager contract hash.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ interface IUpgradableECCM {
function paused() external view returns (bool);
function upgradeToNew(address) external returns (bool);
function isOwner() external view returns (bool);
function setChainId(uint64 _newChainId) external returns (bool);
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ library ECCUtils {
function verifySig(bytes memory _rawHeader, bytes memory _sigList, address[] memory _keepers, uint _m) internal pure returns (bool){
bytes32 hash = getHeaderHash(_rawHeader);

uint signed = 0;
uint sigCount = _sigList.length / POLYCHAIN_SIGNATURE_LEN;
address[] memory signers = new address[](sigCount);
bytes32 r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import "./../upgrade/UpgradableECCM.sol";
import "./../libs/EthCrossChainUtils.sol";
import "./../interface/IEthCrossChainManager.sol";
import "./../interface/IEthCrossChainData.sol";

contract EthCrossChainManager is IEthCrossChainManager, UpgradableECCM {
using SafeMath for uint256;

event InitGenesisBlockEvent(uint256 height, bytes rawHeader);
event ChangeBookKeeperEvent(uint256 height, bytes rawHeader);
event CrossChainEvent(address indexed sender, bytes txId, address proxyOrAssetContract, uint64 toChainId, bytes toContract, bytes rawdata);
event VerifyHeaderAndExecuteTxEvent(uint64 fromChainID, bytes toContract, bytes crossChainTxHash, bytes fromChainTxHash);
constructor(address _eccd) UpgradableECCM(_eccd) public {}

constructor(address _eccd, uint64 _chainId) UpgradableECCM(_eccd, _chainId) public {}

/* @notice sync Poly chain genesis block header to smart contrat
* @dev this function can only be called once, nextbookkeeper of rawHeader can't be empty
Expand Down Expand Up @@ -157,7 +159,7 @@ contract EthCrossChainManager is IEthCrossChainManager, UpgradableECCM {
require(eccd.markFromChainTxExist(toMerkleValue.fromChainID, Utils.bytesToBytes32(toMerkleValue.txHash)), "Save crosschain tx exist failed!");

// Ethereum ChainId is 2, we need to check the transaction is for Ethereum network
require(toMerkleValue.makeTxParam.toChainId == uint64(2), "This Tx is not aiming at Ethereum network!");
require(toMerkleValue.makeTxParam.toChainId == chainId, "This Tx is not aiming at Ethereum network!");

// Obtain the targeting contract, so that Ethereum cross chain manager contract can trigger the executation of cross chain tx on Ethereum side
address toContract = Utils.bytesToAddress(toMerkleValue.makeTxParam.toContract);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract NewEthCrossChainManager is IEthCrossChainManager, UpgradableECCM {
event ChangeBookKeeperEvent(uint256 height, bytes rawHeader);
event CrossChainEvent(address indexed sender, bytes txId, address proxyOrAssetContract, uint64 toChainId, bytes toContract, bytes rawdata);
event VerifyHeaderAndExecuteTxEvent(uint64 fromChainID, bytes toContract, bytes crossChainTxHash, bytes fromChainTxHash);
constructor(address _eccd) UpgradableECCM(_eccd) public {}
constructor(address _eccd, uint64 _chainId) UpgradableECCM(_eccd, _chainId) public {}

/* @notice sync Poly chain genesis block header to smart contrat
* @dev this function can only be called once, nextbookkeeper of rawHeader can't be empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ contract EthCrossChainManagerProxy is IEthCrossChainManagerProxy, Ownable, Pausa
function getEthCrossChainManager() whenNotPaused public view returns (address) {
return EthCrossChainManagerAddr_;
}
function changeManagerChainID(uint64 _newChainId) onlyOwner whenPaused public {
IUpgradableECCM eccm = IUpgradableECCM(EthCrossChainManagerAddr_);
if (!eccm.paused()) {
require(eccm.pause(), "Pause old EthCrossChainManager contract failed!");
}
require(eccm.setChainId(_newChainId), "set chain ID failed. ");
}
}
10 changes: 9 additions & 1 deletion contracts/core/cross_chain_manager/upgrade/UpgradableECCM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import "./../../../libs/ownership/Ownable.sol";

contract UpgradableECCM is IUpgradableECCM, Ownable, Pausable {
address public EthCrossChainDataAddress;
constructor (address ethCrossChainDataAddr) Pausable() Ownable() public {
uint64 public chainId;

constructor (address ethCrossChainDataAddr, uint64 _chainId) Pausable() Ownable() public {
EthCrossChainDataAddress = ethCrossChainDataAddr;
chainId = _chainId;
}
function pause() onlyOwner public returns (bool) {
if (!paused()) {
Expand Down Expand Up @@ -38,4 +41,9 @@ contract UpgradableECCM is IUpgradableECCM, Ownable, Pausable {
eccd.transferOwnership(newEthCrossChainManagerAddress);
return true;
}

function setChainId(uint64 _newChainId) whenPaused onlyOwner public returns (bool) {
chainId = _newChainId;
return true;
}
}
14 changes: 7 additions & 7 deletions contracts/core/lock_proxy/LockProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,18 @@ contract LockProxy is Ownable {
return true;
}


function _transferERC20ToContract(address fromAssetHash, address fromAddress, address toAddress, uint256 amount) internal returns (bool) {
IERC20 erc20Token = IERC20(fromAssetHash);
IERC20 erc20Token = IERC20(fromAssetHash);
// require(erc20Token.transferFrom(fromAddress, toAddress, amount), "trasnfer ERC20 Token failed!");
erc20Token.safeTransferFrom(fromAddress, toAddress, amount);
return true;
erc20Token.safeTransferFrom(fromAddress, toAddress, amount);
return true;
}

function _transferERC20FromContract(address toAssetHash, address toAddress, uint256 amount) internal returns (bool) {
IERC20 erc20Token = IERC20(toAssetHash);
IERC20 erc20Token = IERC20(toAssetHash);
// require(erc20Token.transfer(toAddress, amount), "trasnfer ERC20 Token failed!");
erc20Token.safeTransfer(toAddress, amount);
return true;
erc20Token.safeTransfer(toAddress, amount);
return true;
}

function _serializeTxArgs(TxArgs memory args) internal pure returns (bytes memory) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/libs/GSN/Context.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity >=0.5.0;

/*
* @dev Provides information about the current execution context, including the
Expand Down
28 changes: 27 additions & 1 deletion contracts/libs/common/ZeroCopySink.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity >=0.5.0;

/**
* @dev Wrappers over encoding and serialization operation into bytes from bassic types in Solidity for PolyNetwork cross chain utility.
Expand Down Expand Up @@ -162,6 +162,32 @@ library ZeroCopySink {
return buff;
}

/* @notice Convert limited uint256 value into bytes
* @param v The uint256 value
* @return Converted bytes array
*/
function WriteUint256(uint256 v) internal pure returns (bytes memory) {
require(v <= uint256(-1), "Value exceeds uint256 range");
bytes memory buff;

assembly{
buff := mload(0x40)
let byteLen := 0x20
mstore(buff, byteLen)
for {
let mindex := 0x00
let vindex := 0x1f
} lt(mindex, byteLen) {
mindex := add(mindex, 0x01)
vindex := sub(vindex, 0x01)
}{
mstore8(add(add(buff, 0x20), mindex), byte(vindex, v))
}
mstore(0x40, add(buff, 0x40))
}
return buff;
}

/* @notice Encode bytes format data into bytes
* @param data The bytes array data
* @return Encoded bytes array
Expand Down
26 changes: 25 additions & 1 deletion contracts/libs/common/ZeroCopySource.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity >=0.5.0;

/**
* @dev Wrappers over decoding and deserialization operation from bytes into bassic types in Solidity for PolyNetwork cross chain utility.
Expand Down Expand Up @@ -173,6 +173,30 @@ library ZeroCopySource {
require(v <= 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, "Value exceeds the range");
return (v, offset + 32);
}

function NextUint256(bytes memory buff, uint256 offset) internal pure returns (uint256, uint256) {
require(offset + 32 <= buff.length && offset < offset + 32, "NextUint256, offset exceeds maximum");
uint256 v;
assembly {
let tmpbytes := mload(0x40)
let byteLen := 0x20
for {
let tindex := 0x00
let bindex := sub(byteLen, 0x01)
let bvalue := mload(add(add(buff, 0x20), offset))
} lt(tindex, byteLen) {
tindex := add(tindex, 0x01)
bindex := sub(bindex, 0x01)
}{
mstore8(add(tmpbytes, tindex), byte(bindex, bvalue))
}
mstore(0x40, add(tmpbytes, byteLen))
v := mload(tmpbytes)
}
require(v <= uint256(-1), "Value exceeds the range");
return (v, offset + 32);
}

/* @notice Read next variable bytes starting from offset,
the decoding rule coming from multi-chain
* @param buff Source bytes array
Expand Down
2 changes: 1 addition & 1 deletion contracts/libs/math/SafeMath.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity >=0.5.0;

/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
Expand Down
2 changes: 1 addition & 1 deletion contracts/libs/ownership/Ownable.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.5.0;
pragma solidity >=0.5.0;

import "../GSN/Context.sol";
/**
Expand Down
Loading

0 comments on commit 4f1ec11

Please sign in to comment.