forked from polynetwork/wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
2,065 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,5 @@ src/eth/build | |
src/eth/abigen | ||
src/eth/package-lock.json | ||
src/eth/contracts/@* | ||
artifacts | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.