Skip to content

Commit

Permalink
wrapper without truffle
Browse files Browse the repository at this point in the history
  • Loading branch information
KSlashh committed Aug 6, 2021
1 parent 85a57ee commit a21299e
Show file tree
Hide file tree
Showing 19 changed files with 2,065 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ src/eth/build
src/eth/abigen
src/eth/package-lock.json
src/eth/contracts/@*
artifacts
.DS_Store
96 changes: 96 additions & 0 deletions src/eth-no-truffle/PolyWrapper_v1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.6.0;

import "./libs/token/ERC20/SafeERC20.sol";
import "./libs/token/ERC20/IERC20.sol";
import "./libs/access/Ownable.sol";
import "./libs/utils/ReentrancyGuard.sol";
import "./libs/math/SafeMath.sol";
import "./libs/lifecycle/Pausable.sol";

import "./interfaces/ILockProxy.sol";

contract PolyWrapper is Ownable, Pausable, ReentrancyGuard {
using SafeMath for uint;
using SafeERC20 for IERC20;

uint public chainId;
address public feeCollector;

ILockProxy public lockProxy;

constructor(address _owner, uint _chainId) public {
require(_chainId != 0, "!legal");
transferOwnership(_owner);
chainId = _chainId;
}

function setFeeCollector(address collector) external onlyOwner {
require(collector != address(0), "emtpy address");
feeCollector = collector;
}


function setLockProxy(address _lockProxy) external onlyOwner {
require(_lockProxy != address(0));
lockProxy = ILockProxy(_lockProxy);
require(lockProxy.managerProxyContract() != address(0), "not lockproxy");
}

function pause() external onlyOwner {
_pause();
}

function unpause() external onlyOwner {
_unpause();
}


function extractFee(address token) external {
require(msg.sender == feeCollector, "!feeCollector");
if (token == address(0)) {
payable(msg.sender).transfer(address(this).balance);
} else {
IERC20(token).safeTransfer(feeCollector, IERC20(token).balanceOf(address(this)));
}
}

function lock(address fromAsset, uint64 toChainId, bytes memory toAddress, uint amount, uint fee, uint id) external payable nonReentrant whenNotPaused {

require(toChainId != chainId && toChainId != 0, "!toChainId");
require(amount > fee, "amount less than fee");

_pull(fromAsset, amount);

_push(fromAsset, toChainId, toAddress, amount.sub(fee));

emit PolyWrapperLock(fromAsset, msg.sender, toChainId, toAddress, amount.sub(fee), fee, id);
}

function speedUp(address fromAsset, bytes memory txHash, uint fee) external payable nonReentrant whenNotPaused {
_pull(fromAsset, fee);
emit PolyWrapperSpeedUp(fromAsset, txHash, msg.sender, fee);
}

function _pull(address fromAsset, uint amount) internal {
if (fromAsset == address(0)) {
require(msg.value == amount, "insufficient ether");
} else {
IERC20(fromAsset).safeTransferFrom(msg.sender, address(this), amount);
}
}

function _push(address fromAsset, uint64 toChainId, bytes memory toAddress, uint amount) internal {
if (fromAsset == address(0)) {
require(lockProxy.lock{value: amount}(fromAsset, toChainId, toAddress, amount), "lock ether fail");
} else {
IERC20(fromAsset).safeApprove(address(lockProxy), 0);
IERC20(fromAsset).safeApprove(address(lockProxy), amount);
require(lockProxy.lock(fromAsset, toChainId, toAddress, amount), "lock erc20 fail");
}
}

event PolyWrapperLock(address indexed fromAsset, address indexed sender, uint64 toChainId, bytes toAddress, uint net, uint fee, uint id);
event PolyWrapperSpeedUp(address indexed fromAsset, bytes indexed txHash, address indexed sender, uint efee);

}
109 changes: 109 additions & 0 deletions src/eth-no-truffle/PolyWrapper_v2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.6.0;

import "./libs/token/ERC20/SafeERC20.sol";
import "./libs/token/ERC20/IERC20.sol";
import "./libs/access/Ownable.sol";
import "./libs/utils/ReentrancyGuard.sol";
import "./libs/math/SafeMath.sol";
import "./libs/lifecycle/Pausable.sol";

import "./interfaces/ILockProxy.sol";

contract PolyWrapper is Ownable, Pausable, ReentrancyGuard {
using SafeMath for uint;
using SafeERC20 for IERC20;

uint public chainId;
address public feeCollector;

ILockProxy public lockProxy;

constructor(address _owner, uint _chainId) public {
require(_chainId != 0, "!legal");
transferOwnership(_owner);
chainId = _chainId;
}

function setFeeCollector(address collector) external onlyOwner {
require(collector != address(0), "emtpy address");
feeCollector = collector;
}


function setLockProxy(address _lockProxy) external onlyOwner {
require(_lockProxy != address(0));
lockProxy = ILockProxy(_lockProxy);
require(lockProxy.managerProxyContract() != address(0), "not lockproxy");
}

function pause() external onlyOwner {
_pause();
}

function unpause() external onlyOwner {
_unpause();
}


function extractFee(address token) external {
require(msg.sender == feeCollector, "!feeCollector");
if (token == address(0)) {
payable(msg.sender).transfer(address(this).balance);
} else {
IERC20(token).safeTransfer(feeCollector, IERC20(token).balanceOf(address(this)));
}
}

function lock(address fromAsset, uint64 toChainId, bytes memory toAddress, uint amount, uint fee, uint id) external payable nonReentrant whenNotPaused {

require(toChainId != chainId && toChainId != 0, "!toChainId");

_pull(fromAsset, amount);

amount = _checkoutFee(fromAsset, amount, fee);

_push(fromAsset, toChainId, toAddress, amount);

emit PolyWrapperLock(fromAsset, msg.sender, toChainId, toAddress, amount, fee, id);
}

function speedUp(address fromAsset, bytes memory txHash, uint fee) external payable nonReentrant whenNotPaused {
_pull(fromAsset, fee);
emit PolyWrapperSpeedUp(fromAsset, txHash, msg.sender, fee);
}

function _pull(address fromAsset, uint amount) internal {
if (fromAsset == address(0)) {
require(msg.value == amount, "insufficient ether");
} else {
IERC20(fromAsset).safeTransferFrom(msg.sender, address(this), amount);
}
}

// take fee in the form of ether
function _checkoutFee(address fromAsset, uint amount, uint fee) internal view returns (uint) {
if (fromAsset == address(0)) {
require(msg.value >= amount, "insufficient ether");
require(amount > fee, "amount less than fee");
return amount.sub(fee);
} else {
require(msg.value >= fee, "insufficient ether");
return amount;
}
}

function _push(address fromAsset, uint64 toChainId, bytes memory toAddress, uint amount) internal {
if (fromAsset == address(0)) {
require(lockProxy.lock{value: amount}(fromAsset, toChainId, toAddress, amount), "lock ether fail");
} else {
IERC20(fromAsset).safeApprove(address(lockProxy), 0);
IERC20(fromAsset).safeApprove(address(lockProxy), amount);
require(lockProxy.lock(fromAsset, toChainId, toAddress, amount), "lock erc20 fail");
}
}

event PolyWrapperLock(address indexed fromAsset, address indexed sender, uint64 toChainId, bytes toAddress, uint net, uint fee, uint id);
event PolyWrapperSpeedUp(address indexed fromAsset, bytes indexed txHash, address indexed sender, uint efee);

}
108 changes: 108 additions & 0 deletions src/eth-no-truffle/contracts/PolyWrapper_v2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
pragma solidity >=0.6.0;

import "./libs/token/ERC20/SafeERC20.sol";
import "./libs/token/ERC20/IERC20.sol";
import "./libs/access/Ownable.sol";
import "./libs/utils/ReentrancyGuard.sol";
import "./libs/math/SafeMath.sol";
import "./libs/lifecycle/Pausable.sol";

import "./interfaces/ILockProxy.sol";

contract PolyWrapper is Ownable, Pausable, ReentrancyGuard {
using SafeMath for uint;
using SafeERC20 for IERC20;

uint public chainId;
address public feeCollector;

ILockProxy public lockProxy;

constructor(address _owner, uint _chainId) public {
require(_chainId != 0, "!legal");
transferOwnership(_owner);
chainId = _chainId;
}

function setFeeCollector(address collector) external onlyOwner {
require(collector != address(0), "emtpy address");
feeCollector = collector;
}


function setLockProxy(address _lockProxy) external onlyOwner {
require(_lockProxy != address(0));
lockProxy = ILockProxy(_lockProxy);
require(lockProxy.managerProxyContract() != address(0), "not lockproxy");
}

function pause() external onlyOwner {
_pause();
}

function unpause() external onlyOwner {
_unpause();
}


function extractFee(address token) external {
require(msg.sender == feeCollector, "!feeCollector");
if (token == address(0)) {
payable(msg.sender).transfer(address(this).balance);
} else {
IERC20(token).safeTransfer(feeCollector, IERC20(token).balanceOf(address(this)));
}
}

function lock(address fromAsset, uint64 toChainId, bytes memory toAddress, uint amount, uint fee, uint id) external payable nonReentrant whenNotPaused {

require(toChainId != chainId && toChainId != 0, "!toChainId");

_pull(fromAsset, amount);

amount = _checkoutFee(fromAsset, amount, fee);

_push(fromAsset, toChainId, toAddress, amount);

emit PolyWrapperLock(fromAsset, msg.sender, toChainId, toAddress, amount, fee, id);
}

function speedUp(address fromAsset, bytes memory txHash, uint fee) external payable nonReentrant whenNotPaused {
_pull(fromAsset, fee);
emit PolyWrapperSpeedUp(fromAsset, txHash, msg.sender, fee);
}

function _pull(address fromAsset, uint amount) internal {
if (fromAsset == address(0)) {
require(msg.value == amount, "insufficient ether");
} else {
IERC20(fromAsset).safeTransferFrom(msg.sender, address(this), amount);
}
}

// take fee in the form of ether
function _checkoutFee(address fromAsset, uint amount, uint fee) internal view returns (uint) {
if (fromAsset == address(0)) {
require(msg.value >= amount, "insufficient ether");
require(amount > fee, "amount less than fee");
return amount.sub(fee);
} else {
require(msg.value >= fee, "insufficient ether");
return amount;
}
}

function _push(address fromAsset, uint64 toChainId, bytes memory toAddress, uint amount) internal {
if (fromAsset == address(0)) {
require(lockProxy.lock{value: amount}(fromAsset, toChainId, toAddress, amount), "lock ether fail");
} else {
IERC20(fromAsset).safeApprove(address(lockProxy), 0);
IERC20(fromAsset).safeApprove(address(lockProxy), amount);
require(lockProxy.lock(fromAsset, toChainId, toAddress, amount), "lock erc20 fail");
}
}

event PolyWrapperLock(address indexed fromAsset, address indexed sender, uint64 toChainId, bytes toAddress, uint net, uint fee, uint id);
event PolyWrapperSpeedUp(address indexed fromAsset, bytes indexed txHash, address indexed sender, uint efee);

}
30 changes: 30 additions & 0 deletions src/eth-no-truffle/interfaces/ILockProxy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.6.0;

interface ILockProxy {
function managerProxyContract() external view returns (address);
function proxyHashMap(uint64) external view returns (bytes memory);
function assetHashMap(address, uint64) external view returns (bytes memory);
function getBalanceFor(address) external view returns (uint256);
function setManagerProxy(
address eccmpAddr
) external;

function bindProxyHash(
uint64 toChainId,
bytes calldata targetProxyHash
) external returns (bool);

function bindAssetHash(
address fromAssetHash,
uint64 toChainId,
bytes calldata toAssetHash
) external returns (bool);

function lock(
address fromAssetHash,
uint64 toChainId,
bytes calldata toAddress,
uint256 amount
) external payable returns (bool);
}
33 changes: 33 additions & 0 deletions src/eth-no-truffle/interfaces/IPolyWrapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pragma solidity >=0.6.0;

interface IPolyWrapper {
function feeCollector() external view returns (address);
function lockProxy() external view returns (address);
function paused() external view returns (bool);
function chainId() external view returns (uint);
function owner() external view returns (address);

function lock(
address fromAsset,
uint64 toChainId,
bytes calldata toAddress,
uint amount,
uint fee,
uint id
) external payable;

function speedUp(
address fromAsset,
bytes calldata txHash,
uint fee
) external payable;

function setFeeCollector(address collector) external;
function setLockProxy(address _lockProxy) external;
function extractFee(address token) external;
function pause() external;
function unpause() external;

event PolyWrapperLock(address indexed fromAsset, address indexed sender, uint64 toChainId, bytes toAddress, uint net, uint fee, uint id);
event PolyWrapperSpeedUp(address indexed fromAsset, bytes indexed txHash, address indexed sender, uint efee);
}
Loading

0 comments on commit a21299e

Please sign in to comment.