diff --git a/contracts/mainnet/connectors/polygon-bridge/helpers.sol b/contracts/mainnet/connectors/polygon-bridge/helpers.sol index 6bbdd5c3..262ca81a 100644 --- a/contracts/mainnet/connectors/polygon-bridge/helpers.sol +++ b/contracts/mainnet/connectors/polygon-bridge/helpers.sol @@ -2,7 +2,7 @@ pragma solidity ^0.7.0; import { DSMath } from "../../common/math.sol"; import { Basic } from "../../common/basic.sol"; -import { RootChainManagerInterface } from "./interface.sol"; +import { RootChainManagerInterface, DepositManagerProxyInterface } from "./interface.sol"; abstract contract Helpers is DSMath, Basic { /** @@ -14,4 +14,9 @@ abstract contract Helpers is DSMath, Basic { * @dev Polygon POS Bridge Manager */ RootChainManagerInterface internal constant migrator = RootChainManagerInterface(0xA0c68C638235ee32657e8f720a23ceC1bFc77C77); + + /** + * @dev Polygon Plasma Bridge Manager + */ + DepositManagerProxyInterface internal constant migratorPlasma = DepositManagerProxyInterface(0x401F6c983eA34274ec46f84D70b31C151321188b); } \ No newline at end of file diff --git a/contracts/mainnet/connectors/polygon-bridge/interface.sol b/contracts/mainnet/connectors/polygon-bridge/interface.sol index 6043e9a7..266b2a2f 100644 --- a/contracts/mainnet/connectors/polygon-bridge/interface.sol +++ b/contracts/mainnet/connectors/polygon-bridge/interface.sol @@ -2,10 +2,19 @@ pragma solidity ^0.7.0; interface RootChainManagerInterface { function depositEtherFor(address user) external payable; + function rootToChildToken(address user) external view returns(address); 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; } \ No newline at end of file diff --git a/contracts/mainnet/connectors_old/polygon-bridge.sol b/contracts/mainnet/connectors_old/polygon-bridge.sol new file mode 100644 index 00000000..137b4584 --- /dev/null +++ b/contracts/mainnet/connectors_old/polygon-bridge.sol @@ -0,0 +1,462 @@ +pragma solidity ^0.7.0; + +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"; +} \ No newline at end of file diff --git a/docs/connectors.json b/docs/connectors.json index 6bbcbea2..8a75fb05 100644 --- a/docs/connectors.json +++ b/docs/connectors.json @@ -1,6 +1,6 @@ { "1" : { - "1INCH-A": "0x6C7256cf7C003dD85683339F75DdE9971f98f2FD", + "1INCH-A": "0x2A6d6d4EE84015F7D64B4d1F66a409bA3f2BAC00", "AAVE-V1-A": "0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14", "AAVE-V2-A": "0x497Bc53507DF17e60F731e9534cff74E8BC9DBb8", "AUTHORITY-A": "0x6CE3e607C808b4f4C26B7F6aDAeB619e49CAbb25", @@ -9,7 +9,7 @@ "COMPOUND-A": "0x911F4e4e762AeFA6F2Fc1b24e6B1A928200a88a8", "MAKERDAO-A": "0x839c2D3aDe63DF5b0b8F3E57D5e145057Ab41556", "UNISWAP-A": "0xA4BF319968986D2352FA1c550D781bBFCCE3FcaB", - "POLYGON-BRIDGE-A": "0x697860CeE594c577F18f71cAf3d8B68D913c7366", + "POLYGON-BRIDGE-A": "0x1b79B302132370B434fb7807b36CB72FB0510aD5", "AAVE-CLAIM-A": "0x611C1FA59Aa1d6352c4C8bD44882063c6aEE85E0", "AAVE-STAKE-A": "0xf73c94402bc24148b744083ed02654eec2c37d5b" }, @@ -19,4 +19,5 @@ "BASIC-A": "0x1cAF5EC802ca602E98139AD96A8f2B7BC524264E", "AAVE-CLAIM-A": "0xC7Cb1dE2721BFC0E0DA1b9D526bCdC54eF1C0eFC", "PARASWAP-A": "0xFb3a1D56eD56F046721B9aCa749895100754578b" + } } diff --git a/hardhat.config.js b/hardhat.config.js index b48aa2f3..d592c5f7 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -13,6 +13,10 @@ const { utils } = require("ethers"); const PRIVATE_KEY = process.env.PRIVATE_KEY; const ALCHEMY_ID = process.env.ALCHEMY_ID; +if (!process.env.ALCHEMY_ID) { + throw new Error("ENV Variable ALCHEMY_ID not set!"); +} + /** * @type import('hardhat/config').HardhatUserConfig */ @@ -43,7 +47,7 @@ module.exports = { url: `https://eth-mainnet.alchemyapi.io/v2/${ALCHEMY_ID}`, accounts: [`0x${PRIVATE_KEY}`], timeout: 150000, - gasPrice: parseInt(utils.parseUnits("132", "gwei")) + gasPrice: parseInt(utils.parseUnits("34", "gwei")) }, hardhat: { forking: {