diff --git a/contracts/avalanche/common/basic.sol b/contracts/avalanche/common/basic.sol new file mode 100644 index 00000000..ff2c8fe9 --- /dev/null +++ b/contracts/avalanche/common/basic.sol @@ -0,0 +1,54 @@ +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) == avaxAddr ? address(this).balance : token.balanceOf(address(this)); + } + + function getTokensDec(TokenInterface buyAddr, TokenInterface sellAddr) internal view returns(uint buyDec, uint sellDec) { + buyDec = address(buyAddr) == avaxAddr ? 18 : buyAddr.decimals(); + sellDec = address(sellAddr) == avaxAddr ? 18 : sellAddr.decimals(); + } + + function encodeEvent(string memory eventName, bytes memory eventParam) internal pure returns (bytes memory) { + return abi.encode(eventName, eventParam); + } + + function approve(TokenInterface token, address spender, uint256 amount) internal { + try token.approve(spender, amount) { + + } catch { + token.approve(spender, 0); + token.approve(spender, amount); + } + } + + function changeAvaxAddress(address buy, address sell) internal pure returns(TokenInterface _buy, TokenInterface _sell){ + _buy = buy == avaxAddr ? TokenInterface(wavaxAddr) : TokenInterface(buy); + _sell = sell == avaxAddr ? TokenInterface(wavaxAddr) : TokenInterface(sell); + } + + function convertAvaxToWavax(bool isAvax, TokenInterface token, uint amount) internal { + if(isAvax) token.deposit{value: amount}(); + } + + function convertWavaxToAvax(bool isAvax, TokenInterface token, uint amount) internal { + if(isAvax) { + approve(token, address(token), amount); + token.withdraw(amount); + } + } +} diff --git a/contracts/avalanche/common/interfaces.sol b/contracts/avalanche/common/interfaces.sol new file mode 100644 index 00000000..ea9c362d --- /dev/null +++ b/contracts/avalanche/common/interfaces.sol @@ -0,0 +1,22 @@ +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 AccountInterface { + function enable(address) external; + function disable(address) external; + function isAuth(address) external view returns (bool); +} diff --git a/contracts/avalanche/common/math.sol b/contracts/avalanche/common/math.sol new file mode 100644 index 00000000..f6e2e6cd --- /dev/null +++ b/contracts/avalanche/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/avalanche/common/stores.sol b/contracts/avalanche/common/stores.sol new file mode 100644 index 00000000..aed57250 --- /dev/null +++ b/contracts/avalanche/common/stores.sol @@ -0,0 +1,37 @@ +pragma solidity ^0.7.0; + +import { MemoryInterface } from "./interfaces.sol"; + + +abstract contract Stores { + + /** + * @dev Return avax address + */ + address constant internal avaxAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + + /** + * @dev Return Wrapped AVAX address + */ + address constant internal wavaxAddr = 0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7; + + /** + * @dev Return memory variable address + */ + MemoryInterface constant internal instaMemory = MemoryInterface(0x3254Ce8f5b1c82431B8f21Df01918342215825C2); + + /** + * @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/avalanche/connectors/aave/aave-rewards/events.sol b/contracts/avalanche/connectors/aave/aave-rewards/events.sol new file mode 100644 index 00000000..7fb14419 --- /dev/null +++ b/contracts/avalanche/connectors/aave/aave-rewards/events.sol @@ -0,0 +1,10 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogClaimed( + address[] assets, + uint256 amt, + uint256 getId, + uint256 setId + ); +} diff --git a/contracts/avalanche/connectors/aave/aave-rewards/helpers.sol b/contracts/avalanche/connectors/aave/aave-rewards/helpers.sol new file mode 100644 index 00000000..f3ea8e66 --- /dev/null +++ b/contracts/avalanche/connectors/aave/aave-rewards/helpers.sol @@ -0,0 +1,12 @@ +pragma solidity ^0.7.0; + +import { DSMath } from "../../../common/math.sol"; +import { Basic } from "../../../common/basic.sol"; +import { AaveIncentivesInterface } from "./interface.sol"; + +abstract contract Helpers is DSMath, Basic { + /** + * @dev Aave Incentives + */ + AaveIncentivesInterface internal constant incentives = AaveIncentivesInterface(0x01D83Fe6A10D2f2B7AF17034343746188272cAc9); +} diff --git a/contracts/avalanche/connectors/aave/aave-rewards/interface.sol b/contracts/avalanche/connectors/aave/aave-rewards/interface.sol new file mode 100644 index 00000000..d50d1860 --- /dev/null +++ b/contracts/avalanche/connectors/aave/aave-rewards/interface.sol @@ -0,0 +1,9 @@ +pragma solidity ^0.7.0; + +interface AaveIncentivesInterface { + function claimRewards( + address[] calldata assets, + uint256 amount, + address to + ) external returns (uint256); +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/aave/aave-rewards/main.sol b/contracts/avalanche/connectors/aave/aave-rewards/main.sol new file mode 100644 index 00000000..e982793e --- /dev/null +++ b/contracts/avalanche/connectors/aave/aave-rewards/main.sol @@ -0,0 +1,50 @@ +pragma solidity ^0.7.0; + + +/** + * @title Aave Rewards. + * @dev Claim Aave rewards. + */ + +import { TokenInterface } from "../../../common/interfaces.sol"; +import { Stores } from "../../../common/stores.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract IncentivesResolver is Helpers, Events { + + /** + * @dev Claim Pending Rewards. + * @notice Claim Pending Rewards from Aave incentives contract. + * @param assets The list of assets supplied and borrowed. + * @param amt The amount of reward to claim. (uint(-1) for max) + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of rewards claimed. + */ + function claim( + address[] calldata assets, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + + require(assets.length > 0, "invalid-assets"); + + _amt = incentives.claimRewards(assets, _amt, address(this)); + + + TokenInterface wavax = TokenInterface(wavaxAddr); + uint256 wavaxAmount = wavax.balanceOf(address(this)); + convertWavaxToAvax(wavaxAmount > 0, wavax, wavaxAmount); + + setUint(setId, _amt); + + _eventName = "LogClaimed(address[],uint256,uint256,uint256)"; + _eventParam = abi.encode(assets, _amt, getId, setId); + } +} + +contract ConnectV2AaveIncentivesAvalanche is IncentivesResolver { + string public constant name = "Aave-Incentives-v1"; +} diff --git a/contracts/avalanche/connectors/aave/v2/events.sol b/contracts/avalanche/connectors/aave/v2/events.sol new file mode 100644 index 00000000..805b2209 --- /dev/null +++ b/contracts/avalanche/connectors/aave/v2/events.sol @@ -0,0 +1,10 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogDeposit(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogWithdraw(address indexed token, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogBorrow(address indexed token, uint256 tokenAmt, uint256 indexed rateMode, uint256 getId, uint256 setId); + event LogPayback(address indexed token, uint256 tokenAmt, uint256 indexed rateMode, uint256 getId, uint256 setId); + event LogEnableCollateral(address[] tokens); + event LogSwapRateMode(address indexed token, uint256 rateMode); +} diff --git a/contracts/avalanche/connectors/aave/v2/helpers.sol b/contracts/avalanche/connectors/aave/v2/helpers.sol new file mode 100644 index 00000000..cb1f13ef --- /dev/null +++ b/contracts/avalanche/connectors/aave/v2/helpers.sol @@ -0,0 +1,49 @@ +pragma solidity ^0.7.0; + +import { DSMath } from "../../../common/math.sol"; +import { Basic } from "../../../common/basic.sol"; +import { AaveLendingPoolProviderInterface, AaveDataProviderInterface } from "./interface.sol"; + +abstract contract Helpers is DSMath, Basic { + + /** + * @dev Aave Lending Pool Provider + */ + AaveLendingPoolProviderInterface constant internal aaveProvider = AaveLendingPoolProviderInterface(0xb6A86025F0FE1862B372cb0ca18CE3EDe02A318f); + + /** + * @dev Aave Protocol Data Provider + */ + AaveDataProviderInterface constant internal aaveData = AaveDataProviderInterface(0x65285E9dfab318f57051ab2b139ccCf232945451); + + /** + * @dev Aave Referral Code + */ + uint16 constant internal referralCode = 3228; + + /** + * @dev Checks if collateral is enabled for an asset + * @param token token address of the asset.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + */ + function getIsColl(address token) internal view returns (bool isCol) { + (, , , , , , , , isCol) = aaveData.getUserReserveData(token, address(this)); + } + + /** + * @dev Get total debt balance & fee for an asset + * @param token token address of the debt.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param rateMode Borrow rate mode (Stable = 1, Variable = 2) + */ + function getPaybackBalance(address token, uint rateMode) internal view returns (uint) { + (, uint stableDebt, uint variableDebt, , , , , , ) = aaveData.getUserReserveData(token, address(this)); + return rateMode == 1 ? stableDebt : variableDebt; + } + + /** + * @dev Get total collateral balance for an asset + * @param token token address of the collateral.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + */ + function getCollateralBalance(address token) internal view returns (uint bal) { + (bal, , , , , , , ,) = aaveData.getUserReserveData(token, address(this)); + } +} diff --git a/contracts/avalanche/connectors/aave/v2/interface.sol b/contracts/avalanche/connectors/aave/v2/interface.sol new file mode 100644 index 00000000..ab17fd22 --- /dev/null +++ b/contracts/avalanche/connectors/aave/v2/interface.sol @@ -0,0 +1,47 @@ +pragma solidity ^0.7.0; + +interface AaveInterface { + function deposit(address _asset, uint256 _amount, address _onBehalfOf, uint16 _referralCode) external; + function withdraw(address _asset, uint256 _amount, address _to) external; + function borrow( + address _asset, + uint256 _amount, + uint256 _interestRateMode, + uint16 _referralCode, + address _onBehalfOf + ) external; + function repay(address _asset, uint256 _amount, uint256 _rateMode, address _onBehalfOf) external; + function setUserUseReserveAsCollateral(address _asset, bool _useAsCollateral) external; + function swapBorrowRateMode(address _asset, uint256 _rateMode) external; +} + +interface AaveLendingPoolProviderInterface { + function getLendingPool() external view returns (address); +} + +interface AaveDataProviderInterface { + function getReserveTokensAddresses(address _asset) external view returns ( + address aTokenAddress, + address stableDebtTokenAddress, + address variableDebtTokenAddress + ); + function getUserReserveData(address _asset, address _user) external view returns ( + uint256 currentATokenBalance, + uint256 currentStableDebt, + uint256 currentVariableDebt, + uint256 principalStableDebt, + uint256 scaledVariableDebt, + uint256 stableBorrowRate, + uint256 liquidityRate, + uint40 stableRateLastUpdated, + bool usageAsCollateralEnabled + ); +} + +interface AaveAddressProviderRegistryInterface { + function getAddressesProvidersList() external view returns (address[] memory); +} + +interface ATokenInterface { + function balanceOf(address _user) external view returns(uint256); +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/aave/v2/main.sol b/contracts/avalanche/connectors/aave/v2/main.sol new file mode 100644 index 00000000..3dd6239a --- /dev/null +++ b/contracts/avalanche/connectors/aave/v2/main.sol @@ -0,0 +1,216 @@ +pragma solidity ^0.7.0; + +/** + * @title Aave v2. + * @dev Lending & Borrowing. + */ + + +import { TokenInterface } from "../../../common/interfaces.sol"; +import { Stores } from "../../../common/stores.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; +import { AaveInterface } from "./interface.sol"; + +abstract contract AaveResolver is Events, Helpers { + /** + * @dev Deposit AVAX/ERC20_Token. + * @notice Deposit a token to Aave v2 for lending / collaterization. + * @param token The address of the token to deposit.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param amt The amount of the token to deposit. (For max: `uint256(-1)`) + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of tokens deposited. + */ + function deposit( + address token, + uint256 amt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + + AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); + + bool isAVAX = token == avaxAddr; + address _token = isAVAX ? wavaxAddr : token; + + TokenInterface tokenContract = TokenInterface(_token); + + if (isAVAX) { + _amt = _amt == uint(-1) ? address(this).balance : _amt; + convertAvaxToWavax(isAVAX, tokenContract, _amt); + } else { + _amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt; + } + + approve(tokenContract, address(aave), _amt); + + aave.deposit(_token, _amt, address(this), referralCode); + + if (!getIsColl(_token)) { + aave.setUserUseReserveAsCollateral(_token, true); + } + + setUint(setId, _amt); + + _eventName = "LogDeposit(address,uint256,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, getId, setId); + } + + /** + * @dev Withdraw AVAX/ERC20_Token. + * @notice Withdraw deposited token from Aave v2 + * @param token The address of the token to withdraw.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param amt The amount of the token 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); + + AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); + bool isAVAX = token == avaxAddr; + address _token = isAVAX ? wavaxAddr : token; + + TokenInterface tokenContract = TokenInterface(_token); + + uint initialBal = tokenContract.balanceOf(address(this)); + aave.withdraw(_token, _amt, address(this)); + uint finalBal = tokenContract.balanceOf(address(this)); + + _amt = sub(finalBal, initialBal); + + convertWavaxToAvax(isAVAX, tokenContract, _amt); + + setUint(setId, _amt); + + _eventName = "LogWithdraw(address,uint256,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, getId, setId); + } + + /** + * @dev Borrow AVAX/ERC20_Token. + * @notice Borrow a token using Aave v2 + * @param token The address of the token to borrow.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param amt The amount of the token to borrow. + * @param rateMode The type of borrow debt. (For Stable: 1, Variable: 2) + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of tokens borrowed. + */ + function borrow( + address token, + uint256 amt, + uint256 rateMode, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + + AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); + + bool isAVAX = token == avaxAddr; + address _token = isAVAX ? wavaxAddr : token; + + aave.borrow(_token, _amt, rateMode, referralCode, address(this)); + convertWavaxToAvax(isAVAX, TokenInterface(_token), _amt); + + setUint(setId, _amt); + + _eventName = "LogBorrow(address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, rateMode, getId, setId); + } + + /** + * @dev Payback borrowed AVAX/ERC20_Token. + * @notice Payback debt owed. + * @param token The address of the token to payback.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param amt The amount of the token to payback. (For max: `uint256(-1)`) + * @param rateMode The type of debt paying back. (For Stable: 1, Variable: 2) + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of tokens paid back. + */ + function payback( + address token, + uint256 amt, + uint256 rateMode, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + + AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); + + bool isAVAX = token == avaxAddr; + address _token = isAVAX ? wavaxAddr : token; + + TokenInterface tokenContract = TokenInterface(_token); + + _amt = _amt == uint(-1) ? getPaybackBalance(_token, rateMode) : _amt; + + if (isAVAX) convertAvaxToWavax(isAVAX, tokenContract, _amt); + + approve(tokenContract, address(aave), _amt); + + aave.repay(_token, _amt, rateMode, address(this)); + + setUint(setId, _amt); + + _eventName = "LogPayback(address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, rateMode, getId, setId); + } + + /** + * @dev Enable collateral + * @notice Enable an array of tokens as collateral + * @param tokens Array of tokens to enable collateral + */ + function enableCollateral( + address[] calldata tokens + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _length = tokens.length; + require(_length > 0, "0-tokens-not-allowed"); + + AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); + + for (uint i = 0; i < _length; i++) { + address token = tokens[i]; + if (getCollateralBalance(token) > 0 && !getIsColl(token)) { + aave.setUserUseReserveAsCollateral(token, true); + } + } + + _eventName = "LogEnableCollateral(address[])"; + _eventParam = abi.encode(tokens); + } + + /** + * @dev Swap borrow rate mode + * @notice Swaps user borrow rate mode between variable and stable + * @param token The address of the token to swap borrow rate.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param rateMode Desired borrow rate mode. (Stable = 1, Variable = 2) + */ + function swapBorrowRateMode( + address token, + uint rateMode + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); + + uint currentRateMode = rateMode == 1 ? 2 : 1; + + if (getPaybackBalance(token, currentRateMode) > 0) { + aave.swapBorrowRateMode(token, rateMode); + } + + _eventName = "LogSwapRateMode(address,uint256)"; + _eventParam = abi.encode(token, rateMode); + } +} + +contract ConnectV2AaveV2Avalanche is AaveResolver { + string constant public name = "AaveV2-v1"; +} diff --git a/contracts/avalanche/connectors/authority/events.sol b/contracts/avalanche/connectors/authority/events.sol new file mode 100644 index 00000000..1ad38da7 --- /dev/null +++ b/contracts/avalanche/connectors/authority/events.sol @@ -0,0 +1,6 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogAddAuth(address indexed _msgSender, address indexed _authority); + event LogRemoveAuth(address indexed _msgSender, address indexed _authority); +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/authority/helpers.sol b/contracts/avalanche/connectors/authority/helpers.sol new file mode 100644 index 00000000..8e4d1371 --- /dev/null +++ b/contracts/avalanche/connectors/authority/helpers.sol @@ -0,0 +1,15 @@ +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +import { DSMath } from "../../common/math.sol"; +import { Basic } from "../../common/basic.sol"; +import { ListInterface } from "./interface.sol"; + +abstract contract Helpers is DSMath, Basic { + ListInterface internal constant listContract = ListInterface(0x9926955e0Dd681Dc303370C52f4Ad0a4dd061687); + + function checkAuthCount() internal view returns (uint count) { + uint64 accountId = listContract.accountID(address(this)); + count = listContract.accountLink(accountId).count; + } +} diff --git a/contracts/avalanche/connectors/authority/interface.sol b/contracts/avalanche/connectors/authority/interface.sol new file mode 100644 index 00000000..5b7fde6d --- /dev/null +++ b/contracts/avalanche/connectors/authority/interface.sol @@ -0,0 +1,13 @@ +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +interface ListInterface { + struct AccountLink { + address first; + address last; + uint64 count; + } + + function accountID(address) external view returns (uint64); + function accountLink(uint64) external view returns (AccountLink memory); +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/authority/main.sol b/contracts/avalanche/connectors/authority/main.sol new file mode 100644 index 00000000..921627eb --- /dev/null +++ b/contracts/avalanche/connectors/authority/main.sol @@ -0,0 +1,45 @@ +pragma solidity ^0.7.0; + +/** + * @title Authority. + * @dev Manage Authorities to DSA. + */ + +import { AccountInterface } from "../../common/interfaces.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract AuthorityResolver is Events, Helpers { + /** + * @dev Add New authority + * @notice Add an address as account authority + * @param authority The authority Address. + */ + function add( + address authority + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + AccountInterface(address(this)).enable(authority); + + _eventName = "LogAddAuth(address,address)"; + _eventParam = abi.encode(msg.sender, authority); + } + + /** + * @dev Remove authority + * @notice Remove an address as account authority + * @param authority The authority Address. + */ + function remove( + address authority + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + require(checkAuthCount() > 1, "Removing-all-authorities"); + AccountInterface(address(this)).disable(authority); + + _eventName = "LogRemoveAuth(address,address)"; + _eventParam = abi.encode(msg.sender, authority); + } +} + +contract ConnectV2AuthAvalanche is AuthorityResolver { + string public constant name = "Auth-v1"; +} diff --git a/contracts/avalanche/connectors/basic/events.sol b/contracts/avalanche/connectors/basic/events.sol new file mode 100644 index 00000000..b68f3801 --- /dev/null +++ b/contracts/avalanche/connectors/basic/events.sol @@ -0,0 +1,6 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogDeposit(address indexed erc20, uint256 tokenAmt, uint256 getId, uint256 setId); + event LogWithdraw(address indexed erc20, uint256 tokenAmt, address indexed to, uint256 getId, uint256 setId); +} diff --git a/contracts/avalanche/connectors/basic/main.sol b/contracts/avalanche/connectors/basic/main.sol new file mode 100644 index 00000000..c674103e --- /dev/null +++ b/contracts/avalanche/connectors/basic/main.sol @@ -0,0 +1,82 @@ +pragma solidity ^0.7.0; + + +/** + * @title Basic. + * @dev Deposit & Withdraw from DSA. + */ + +import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; + +import { DSMath } from "../../common/math.sol"; +import { Basic } from "../../common/basic.sol"; +import { Events } from "./events.sol"; + +abstract contract BasicResolver is Events, DSMath, Basic { + using SafeERC20 for IERC20; + + /** + * @dev Deposit Assets To Smart Account. + * @notice Deposit a token to DSA + * @param token The address of the token to deposit. (For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param amt The amount of tokens to deposit. (For max: `uint256(-1)` (Not valid for AVAX)) + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of tokens deposited. + */ + function deposit( + address token, + uint256 amt, + uint256 getId, + uint256 setId + ) public payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + if (token != avaxAddr) { + IERC20 tokenContract = IERC20(token); + _amt = _amt == uint(-1) ? tokenContract.balanceOf(msg.sender) : _amt; + tokenContract.safeTransferFrom(msg.sender, address(this), _amt); + } else { + require(msg.value == _amt || _amt == uint(-1), "invalid-ether-amount"); + _amt = msg.value; + } + setUint(setId, _amt); + + _eventName = "LogDeposit(address,uint256,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, getId, setId); + } + + /** + * @dev Withdraw Assets from Smart Account + * @notice Withdraw a token from DSA + * @param token The address of the token to withdraw. (For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param amt The amount of tokens to withdraw. (For max: `uint256(-1)`) + * @param to The address to receive the token upon withdrawal + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of tokens withdrawn. + */ + function withdraw( + address token, + uint amt, + address payable to, + uint getId, + uint setId + ) public payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + if (token == avaxAddr) { + _amt = _amt == uint(-1) ? address(this).balance : _amt; + to.call{value: _amt}(""); + } else { + IERC20 tokenContract = IERC20(token); + _amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt; + tokenContract.safeTransfer(to, _amt); + } + setUint(setId, _amt); + + _eventName = "LogWithdraw(address,uint256,address,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, to, getId, setId); + } +} + +contract ConnectV2BasicAvalanche is BasicResolver { + string constant public name = "Basic-v1"; +} diff --git a/contracts/avalanche/connectors/wavax/events.sol b/contracts/avalanche/connectors/wavax/events.sol new file mode 100644 index 00000000..f8ec366a --- /dev/null +++ b/contracts/avalanche/connectors/wavax/events.sol @@ -0,0 +1,6 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogDeposit(uint256 tokenAmt, uint256 getId, uint256 setId); + event LogWithdraw(uint256 tokenAmt, uint256 getId, uint256 setId); +} diff --git a/contracts/avalanche/connectors/wavax/helpers.sol b/contracts/avalanche/connectors/wavax/helpers.sol new file mode 100644 index 00000000..d995c62e --- /dev/null +++ b/contracts/avalanche/connectors/wavax/helpers.sol @@ -0,0 +1,8 @@ +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; + + +abstract contract Helpers { + TokenInterface constant internal wavaxContract = TokenInterface(0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7); +} diff --git a/contracts/avalanche/connectors/wavax/main.sol b/contracts/avalanche/connectors/wavax/main.sol new file mode 100644 index 00000000..d1c72cab --- /dev/null +++ b/contracts/avalanche/connectors/wavax/main.sol @@ -0,0 +1,65 @@ +pragma solidity ^0.7.0; + +/** + * @title WAVAX. + * @dev Wrap and Unwrap WAVAX. + */ + +import { DSMath } from "../../common/math.sol"; +import { Basic } from "../../common/basic.sol"; +import { Events } from "./events.sol"; +import { Helpers } from "./helpers.sol"; + +abstract contract Resolver is Events, DSMath, Basic, Helpers { + + /** + * @dev Deposit AVAX into WAVAX. + * @notice Wrap AVAX into WAVAX + * @param amt The amount of AVAX to deposit. (For max: `uint256(-1)`) + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of AVAX deposited. + */ + function deposit( + uint256 amt, + uint256 getId, + uint256 setId + ) public payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + + _amt = _amt == uint(-1) ? address(this).balance : _amt; + wavaxContract.deposit{value: _amt}(); + + setUint(setId, _amt); + + _eventName = "LogDeposit(uint256,uint256,uint256)"; + _eventParam = abi.encode(_amt, getId, setId); + } + + /** + * @dev Withdraw AVAX from WAVAX from Smart Account + * @notice Unwrap AVAX from WAVAX + * @param amt The amount of wavax to withdraw. (For max: `uint256(-1)`) + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of AVAX withdrawn. + */ + function withdraw( + uint amt, + uint getId, + uint setId + ) public payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + + _amt = _amt == uint(-1) ? wavaxContract.balanceOf(address(this)) : _amt; + approve(wavaxContract, wavaxAddr, _amt); + wavaxContract.withdraw(_amt); + + setUint(setId, _amt); + + _eventName = "LogWithdraw(uint256,uint256,uint256)"; + _eventParam = abi.encode(_amt, getId, setId); + } +} + +contract ConnectV2WAVAXAvalanche is Resolver { + string constant public name = "WAVAX-v1.0"; +} diff --git a/hardhat.config.js b/hardhat.config.js index e4a64184..1103f232 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -74,6 +74,13 @@ module.exports = { timeout: 150000, gasPrice: parseInt(utils.parseUnits("2", "gwei")), }, + avax: { + url: 'https://api.avax.network/ext/bc/C/rpc', + chainId: 43114, + accounts: [`0x${PRIVATE_KEY}`], + timeout: 150000, + gasPrice: parseInt(utils.parseUnits("225", "gwei")) + } }, etherscan: { apiKey: process.env.ETHERSCAN_API_KEY,