Last active
June 2, 2021 16:43
-
-
Save thrilok209/7a3b64941c724e0bfd27d385ce72f798 to your computer and use it in GitHub Desktop.
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
pragma solidity ^0.6.12; | |
interface TokenInterface { | |
function approve(address, uint256) external; | |
function transfer(address, uint) external; | |
function transferFrom(address, address, uint) external; | |
function deposit() external payable; | |
function withdraw(uint) external; | |
function balanceOf(address) external view returns (uint); | |
function decimals() external view returns (uint); | |
} | |
interface MemoryInterface { | |
function getUint(uint id) external returns (uint num); | |
function setUint(uint id, uint val) external; | |
} | |
interface InstaMapping { | |
function cTokenMapping(address) external view returns (address); | |
function gemJoinMapping(bytes32) external view returns (address); | |
} | |
interface AccountInterface { | |
function enable(address) external; | |
function disable(address) external; | |
function isAuth(address) external view returns (bool); | |
} | |
abstract contract Stores { | |
/** | |
* @dev Return ethereum address | |
*/ | |
address constant internal ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; | |
/** | |
* @dev Return Wrapped ETH address | |
*/ | |
address constant internal wethAddr = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; | |
/** | |
* @dev Return memory variable address | |
*/ | |
MemoryInterface constant internal instaMemory = MemoryInterface(0x8a5419CfC711B2343c17a6ABf4B2bAFaBb06957F); | |
/** | |
* @dev Return InstaDApp Mapping Addresses | |
*/ | |
InstaMapping constant internal instaMapping = InstaMapping(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); | |
/** | |
* @dev Get Uint value from InstaMemory Contract. | |
*/ | |
function getUint(uint getId, uint val) internal returns (uint returnVal) { | |
returnVal = getId == 0 ? val : instaMemory.getUint(getId); | |
} | |
/** | |
* @dev Set Uint value in InstaMemory Contract. | |
*/ | |
function setUint(uint setId, uint val) virtual internal { | |
if (setId != 0) instaMemory.setUint(setId, val); | |
} | |
} | |
/** | |
* @dev Wrappers over Solidity's arithmetic operations with added overflow | |
* checks. | |
* | |
* Arithmetic operations in Solidity wrap on overflow. This can easily result | |
* in bugs, because programmers usually assume that an overflow raises an | |
* error, which is the standard behavior in high level programming languages. | |
* `SafeMath` restores this intuition by reverting the transaction when an | |
* operation overflows. | |
* | |
* Using this library instead of the unchecked operations eliminates an entire | |
* class of bugs, so it's recommended to use it always. | |
*/ | |
library SafeMath { | |
/** | |
* @dev Returns the addition of two unsigned integers, with an overflow flag. | |
* | |
* _Available since v3.4._ | |
*/ | |
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) { | |
uint256 c = a + b; | |
if (c < a) return (false, 0); | |
return (true, c); | |
} | |
/** | |
* @dev Returns the substraction of two unsigned integers, with an overflow flag. | |
* | |
* _Available since v3.4._ | |
*/ | |
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) { | |
if (b > a) return (false, 0); | |
return (true, a - b); | |
} | |
/** | |
* @dev Returns the multiplication of two unsigned integers, with an overflow flag. | |
* | |
* _Available since v3.4._ | |
*/ | |
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) { | |
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the | |
// benefit is lost if 'b' is also tested. | |
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 | |
if (a == 0) return (true, 0); | |
uint256 c = a * b; | |
if (c / a != b) return (false, 0); | |
return (true, c); | |
} | |
/** | |
* @dev Returns the division of two unsigned integers, with a division by zero flag. | |
* | |
* _Available since v3.4._ | |
*/ | |
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) { | |
if (b == 0) return (false, 0); | |
return (true, a / b); | |
} | |
/** | |
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag. | |
* | |
* _Available since v3.4._ | |
*/ | |
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) { | |
if (b == 0) return (false, 0); | |
return (true, a % b); | |
} | |
/** | |
* @dev Returns the addition of two unsigned integers, reverting on | |
* overflow. | |
* | |
* Counterpart to Solidity's `+` operator. | |
* | |
* Requirements: | |
* | |
* - Addition cannot overflow. | |
*/ | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
require(c >= a, "SafeMath: addition overflow"); | |
return c; | |
} | |
/** | |
* @dev Returns the subtraction of two unsigned integers, reverting on | |
* overflow (when the result is negative). | |
* | |
* Counterpart to Solidity's `-` operator. | |
* | |
* Requirements: | |
* | |
* - Subtraction cannot overflow. | |
*/ | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
require(b <= a, "SafeMath: subtraction overflow"); | |
return a - b; | |
} | |
/** | |
* @dev Returns the multiplication of two unsigned integers, reverting on | |
* overflow. | |
* | |
* Counterpart to Solidity's `*` operator. | |
* | |
* Requirements: | |
* | |
* - Multiplication cannot overflow. | |
*/ | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
if (a == 0) return 0; | |
uint256 c = a * b; | |
require(c / a == b, "SafeMath: multiplication overflow"); | |
return c; | |
} | |
/** | |
* @dev Returns the integer division of two unsigned integers, reverting on | |
* division by zero. The result is rounded towards zero. | |
* | |
* Counterpart to Solidity's `/` operator. Note: this function uses a | |
* `revert` opcode (which leaves remaining gas untouched) while Solidity | |
* uses an invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* | |
* - The divisor cannot be zero. | |
*/ | |
function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
require(b > 0, "SafeMath: division by zero"); | |
return a / b; | |
} | |
/** | |
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), | |
* reverting when dividing by zero. | |
* | |
* Counterpart to Solidity's `%` operator. This function uses a `revert` | |
* opcode (which leaves remaining gas untouched) while Solidity uses an | |
* invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* | |
* - The divisor cannot be zero. | |
*/ | |
function mod(uint256 a, uint256 b) internal pure returns (uint256) { | |
require(b > 0, "SafeMath: modulo by zero"); | |
return a % b; | |
} | |
/** | |
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on | |
* overflow (when the result is negative). | |
* | |
* CAUTION: This function is deprecated because it requires allocating memory for the error | |
* message unnecessarily. For custom revert reasons use {trySub}. | |
* | |
* Counterpart to Solidity's `-` operator. | |
* | |
* Requirements: | |
* | |
* - Subtraction cannot overflow. | |
*/ | |
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
require(b <= a, errorMessage); | |
return a - b; | |
} | |
/** | |
* @dev Returns the integer division of two unsigned integers, reverting with custom message on | |
* division by zero. The result is rounded towards zero. | |
* | |
* CAUTION: This function is deprecated because it requires allocating memory for the error | |
* message unnecessarily. For custom revert reasons use {tryDiv}. | |
* | |
* Counterpart to Solidity's `/` operator. Note: this function uses a | |
* `revert` opcode (which leaves remaining gas untouched) while Solidity | |
* uses an invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* | |
* - The divisor cannot be zero. | |
*/ | |
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
require(b > 0, errorMessage); | |
return a / b; | |
} | |
/** | |
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), | |
* reverting with custom message when dividing by zero. | |
* | |
* CAUTION: This function is deprecated because it requires allocating memory for the error | |
* message unnecessarily. For custom revert reasons use {tryMod}. | |
* | |
* Counterpart to Solidity's `%` operator. This function uses a `revert` | |
* opcode (which leaves remaining gas untouched) while Solidity uses an | |
* invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* | |
* - The divisor cannot be zero. | |
*/ | |
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
require(b > 0, errorMessage); | |
return a % b; | |
} | |
} | |
contract DSMath { | |
uint constant WAD = 10 ** 18; | |
uint constant RAY = 10 ** 27; | |
function add(uint x, uint y) internal pure returns (uint z) { | |
z = SafeMath.add(x, y); | |
} | |
function sub(uint x, uint y) internal virtual pure returns (uint z) { | |
z = SafeMath.sub(x, y); | |
} | |
function mul(uint x, uint y) internal pure returns (uint z) { | |
z = SafeMath.mul(x, y); | |
} | |
function div(uint x, uint y) internal pure returns (uint z) { | |
z = SafeMath.div(x, y); | |
} | |
function wmul(uint x, uint y) internal pure returns (uint z) { | |
z = SafeMath.add(SafeMath.mul(x, y), WAD / 2) / WAD; | |
} | |
function wdiv(uint x, uint y) internal pure returns (uint z) { | |
z = SafeMath.add(SafeMath.mul(x, WAD), y / 2) / y; | |
} | |
function rdiv(uint x, uint y) internal pure returns (uint z) { | |
z = SafeMath.add(SafeMath.mul(x, RAY), y / 2) / y; | |
} | |
function rmul(uint x, uint y) internal pure returns (uint z) { | |
z = SafeMath.add(SafeMath.mul(x, y), RAY / 2) / RAY; | |
} | |
function toInt(uint x) internal pure returns (int y) { | |
y = int(x); | |
require(y >= 0, "int-overflow"); | |
} | |
function toRad(uint wad) internal pure returns (uint rad) { | |
rad = mul(wad, 10 ** 27); | |
} | |
} | |
abstract contract Basic is DSMath, Stores { | |
function convert18ToDec(uint _dec, uint256 _amt) internal pure returns (uint256 amt) { | |
amt = (_amt / 10 ** (18 - _dec)); | |
} | |
function convertTo18(uint _dec, uint256 _amt) internal pure returns (uint256 amt) { | |
amt = mul(_amt, 10 ** (18 - _dec)); | |
} | |
function getTokenBal(TokenInterface token) internal view returns(uint _amt) { | |
_amt = address(token) == ethAddr ? address(this).balance : token.balanceOf(address(this)); | |
} | |
function getTokensDec(TokenInterface buyAddr, TokenInterface sellAddr) internal view returns(uint buyDec, uint sellDec) { | |
buyDec = address(buyAddr) == ethAddr ? 18 : buyAddr.decimals(); | |
sellDec = address(sellAddr) == ethAddr ? 18 : sellAddr.decimals(); | |
} | |
function encodeEvent(string memory eventName, bytes memory eventParam) internal pure returns (bytes memory) { | |
return abi.encode(eventName, eventParam); | |
} | |
function changeEthAddress(address buy, address sell) internal pure returns(TokenInterface _buy, TokenInterface _sell){ | |
_buy = buy == ethAddr ? TokenInterface(wethAddr) : TokenInterface(buy); | |
_sell = sell == ethAddr ? TokenInterface(wethAddr) : TokenInterface(sell); | |
} | |
function convertEthToWeth(bool isEth, TokenInterface token, uint amount) internal { | |
if(isEth) token.deposit{value: amount}(); | |
} | |
function convertWethToEth(bool isEth, TokenInterface token, uint amount) internal { | |
if(isEth) { | |
token.approve(address(token), amount); | |
token.withdraw(amount); | |
} | |
} | |
} | |
interface RootChainManagerInterface { | |
function rootToChildToken(address user) external view returns(address); | |
function depositEtherFor(address user) external payable; | |
function depositFor( | |
address user, | |
address rootToken, | |
bytes calldata depositData | |
) external; | |
function exit(bytes calldata inputData) external; | |
} | |
interface DepositManagerProxyInterface { | |
function depositERC20ForUser( | |
address _token, | |
address _user, | |
uint256 _amount | |
) external; | |
} | |
abstract contract Helpers is DSMath, Basic { | |
/** | |
* @dev Polygon POS Bridge ERC20 Predicate | |
*/ | |
address internal constant erc20Predicate = 0x40ec5B33f54e0E8A33A975908C5BA1c14e5BbbDf; | |
/** | |
* @dev Polygon POS Bridge Manager | |
*/ | |
RootChainManagerInterface internal constant migrator = RootChainManagerInterface(0xA0c68C638235ee32657e8f720a23ceC1bFc77C77); | |
/** | |
* @dev Polygon Plasma Bridge Manager | |
*/ | |
DepositManagerProxyInterface internal constant migratorPlasma = DepositManagerProxyInterface(0x401F6c983eA34274ec46f84D70b31C151321188b); | |
} | |
contract Events { | |
event LogDeposit( | |
address targetDsa, | |
address token, | |
uint256 amt, | |
uint256 getId, | |
uint256 setId | |
); | |
event LogWithdraw(bytes proof); | |
} | |
abstract contract PolygonBridgeResolver is Events, Helpers { | |
/** | |
* @dev Deposit assets to the bridge. | |
* @notice Deposit assets to the bridge. | |
* @param targetDsa The address to receive the token on Polygon | |
* @param token The address of the token to deposit. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) | |
* @param amt The amount of tokens to deposit. (For max: `uint256(-1)`) | |
* @param getId ID to retrieve amt. | |
* @param setId ID stores the amount of tokens deposit. | |
*/ | |
function deposit( | |
address targetDsa, | |
address token, | |
uint256 amt, | |
uint256 getId, | |
uint256 setId | |
) external payable { | |
uint _amt = getUint(getId, amt); | |
if (token == ethAddr) { | |
_amt = _amt == uint(-1) ? address(this).balance : _amt; | |
migrator.depositEtherFor{value: _amt}(targetDsa); | |
} else { | |
TokenInterface _token = TokenInterface(token); | |
_amt = _amt == uint(-1) ? _token.balanceOf(address(this)) : _amt; | |
if (migrator.rootToChildToken(token) != address(0)) { | |
_token.approve(erc20Predicate, _amt); | |
migrator.depositFor(targetDsa, token, abi.encode(_amt)); | |
} else { | |
_token.approve(address(migratorPlasma), _amt); | |
migratorPlasma.depositERC20ForUser(token, targetDsa, _amt); | |
} | |
} | |
setUint(setId, _amt); | |
emit LogDeposit(targetDsa, token, _amt, getId, setId); | |
} | |
} | |
contract ConnectPolygonBridge is PolygonBridgeResolver { | |
/** | |
* @dev Connector Details | |
*/ | |
function connectorID() public pure returns(uint _type, uint _id) { | |
(_type, _id) = (1, 100); | |
} | |
string public constant name = "Polygon-Bridge-v1.1"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment