diff --git a/contracts/mainnet/connectors/polygon-bridge/main.sol b/contracts/mainnet/connectors/polygon-bridge/main.sol index a188be8d..2acb29e0 100644 --- a/contracts/mainnet/connectors/polygon-bridge/main.sol +++ b/contracts/mainnet/connectors/polygon-bridge/main.sol @@ -6,6 +6,15 @@ import { Helpers } from "./helpers.sol"; import { Events } from "./events.sol"; 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, diff --git a/contracts/polygon/common/basic.sol b/contracts/polygon/common/basic.sol new file mode 100644 index 00000000..fea2b1ec --- /dev/null +++ b/contracts/polygon/common/basic.sol @@ -0,0 +1,45 @@ +pragma solidity ^0.7.0; + +import { TokenInterface } from "./interfaces.sol"; +import { Stores } from "./stores.sol"; +import { DSMath } from "./math.sol"; + +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) == maticAddr ? address(this).balance : token.balanceOf(address(this)); + } + + function getTokensDec(TokenInterface buyAddr, TokenInterface sellAddr) internal view returns(uint buyDec, uint sellDec) { + buyDec = address(buyAddr) == maticAddr ? 18 : buyAddr.decimals(); + sellDec = address(sellAddr) == maticAddr ? 18 : sellAddr.decimals(); + } + + function encodeEvent(string memory eventName, bytes memory eventParam) internal pure returns (bytes memory) { + return abi.encode(eventName, eventParam); + } + + function changeMaticAddress(address buy, address sell) internal pure returns(TokenInterface _buy, TokenInterface _sell){ + _buy = buy == maticAddr ? TokenInterface(wmaticAddr) : TokenInterface(buy); + _sell = sell == maticAddr ? TokenInterface(wmaticAddr) : TokenInterface(sell); + } + + function convertMaticToWmatic(bool isMatic, TokenInterface token, uint amount) internal { + if(isMatic) token.deposit{value: amount}(); + } + + function convertWmaticToMatic(bool isMatic, TokenInterface token, uint amount) internal { + if(isMatic) { + token.approve(address(token), amount); + token.withdraw(amount); + } + } +} diff --git a/contracts/polygon/common/interfaces.sol b/contracts/polygon/common/interfaces.sol new file mode 100644 index 00000000..24a4eb47 --- /dev/null +++ b/contracts/polygon/common/interfaces.sol @@ -0,0 +1,27 @@ +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); +} diff --git a/contracts/polygon/common/math.sol b/contracts/polygon/common/math.sol new file mode 100644 index 00000000..f6e2e6cd --- /dev/null +++ b/contracts/polygon/common/math.sol @@ -0,0 +1,50 @@ +pragma solidity ^0.7.0; + +import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol"; + +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); + } + +} diff --git a/contracts/polygon/common/stores.sol b/contracts/polygon/common/stores.sol new file mode 100644 index 00000000..3afef234 --- /dev/null +++ b/contracts/polygon/common/stores.sol @@ -0,0 +1,37 @@ +pragma solidity ^0.7.0; + +import { MemoryInterface, InstaMapping } from "./interfaces.sol"; + + +abstract contract Stores { + + /** + * @dev Return ethereum address + */ + address constant internal maticAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + + /** + * @dev Return Wrapped ETH address + */ + address constant internal wmaticAddr = 0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270; + + /** + * @dev Return memory variable address + */ + MemoryInterface constant internal instaMemory = MemoryInterface(0x6C7256cf7C003dD85683339F75DdE9971f98f2FD); + + /** + * @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); + } + +} diff --git a/contracts/polygon/connectors/mainnet-bridge/events.sol b/contracts/polygon/connectors/mainnet-bridge/events.sol new file mode 100644 index 00000000..0212e59d --- /dev/null +++ b/contracts/polygon/connectors/mainnet-bridge/events.sol @@ -0,0 +1,10 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogWithdraw( + address token, + uint256 amt, + uint256 getId, + uint256 setId + ); +} \ No newline at end of file diff --git a/contracts/polygon/connectors/mainnet-bridge/main.sol b/contracts/polygon/connectors/mainnet-bridge/main.sol new file mode 100644 index 00000000..030fa679 --- /dev/null +++ b/contracts/polygon/connectors/mainnet-bridge/main.sol @@ -0,0 +1,41 @@ +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import { Events } from "./events.sol"; + +abstract contract MainnetBridgeResolver is Stores, Events { + /** + * @dev Withdraw assets to mainnet. + * @notice Withdraw assets to mainnet by burning the tokens. + * @param token The address of the token to withdraw. (For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param amt The amount of tokens to withdraw. (For max: `uint256(-1)`) + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of tokens withdrawn. + */ + function withdraw( + address token, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + + if (token == maticAddr) { + _amt = _amt == uint(-1) ? address(this).balance : _amt; + TokenInterface(address(0)).withdraw(_amt); + } else { + TokenInterface _token = TokenInterface(token); + _amt = _amt == uint(-1) ? _token.balanceOf(address(this)) : _amt; + _token.withdraw(_amt); + if (token == wmaticAddr) { + TokenInterface(address(0)).withdraw(_amt); + } + } + + setUint(setId, _amt); + + _eventName = "LogWithdraw(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, getId, setId); + } +} \ No newline at end of file