[WIP] Aave v2 state sender + connector

This commit is contained in:
Mubaris NK 2021-04-01 15:32:46 +05:30
parent 6cfd94a1f6
commit d9fdd17e3d
No known key found for this signature in database
GPG Key ID: 9AC09AD0F8D68561
9 changed files with 385 additions and 0 deletions

View File

@ -0,0 +1,28 @@
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
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);
function cast(
string[] calldata _targets,
bytes[] calldata _datas,
address _origin
) external payable returns (bytes32);
}

49
contracts/common/math.sol Normal file
View File

@ -0,0 +1,49 @@
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);
}
}

View File

@ -0,0 +1,39 @@
pragma solidity ^0.7.0;
import { MemoryInterface } from "./interfaces.sol";
abstract contract Stores {
/**
* @dev Return ethereum address
* ETH on Mainnet
* MATIC on Polygon
*/
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 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);
}
}

View File

@ -0,0 +1,12 @@
pragma solidity ^0.7.0;
contract Events {
event LogAaveV2Migrate(
address indexed user,
address[] supplyTokens,
address[] borrowTokens,
uint[] supplyAmts,
uint[] stableBorrowAmts,
uint[] variableBorrowAmts
);
}

View File

@ -0,0 +1,35 @@
pragma solidity ^0.7.0;
import { DSMath } from "../../common/math.sol";
import { Stores } from "../../common/stores.sol";
import { AaveLendingPoolProviderInterface, AaveDataProviderInterface, StateSenderInterface } from "./interfaces.sol";
abstract contract Helpers is DSMath, Stores {
/**
* @dev Aave referal code
*/
uint16 constant internal referalCode = 3228;
address constant internal fundLocker = address(1); // Replace this
address constant internal polygonReceiver = address(2); // Replace this
/**
* @dev Aave Provider
*/
AaveLendingPoolProviderInterface constant internal aaveProvider = AaveLendingPoolProviderInterface(0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5);
/**
* @dev Aave Data Provider
*/
AaveDataProviderInterface constant internal aaveData = AaveDataProviderInterface(0x057835Ad21a177dbdd3090bB1CAE03EaCF78Fc6d);
/**
* @dev Polygon State Sender
*/
StateSenderInterface constant internal stateSender = StateSenderInterface(0x28e4F3a7f651294B9564800b2D01f35189A5bFbE);
function getIsColl(address token, address user) internal view returns (bool isCol) {
(, , , , , , , , isCol) = aaveData.getUserReserveData(token, user);
}
}

View File

@ -0,0 +1,76 @@
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
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 getUserAccountData(address user) external view returns (
uint256 totalCollateralETH,
uint256 totalDebtETH,
uint256 availableBorrowsETH,
uint256 currentLiquidationThreshold,
uint256 ltv,
uint256 healthFactor
);
}
interface AaveLendingPoolProviderInterface {
function getLendingPool() external view returns (address);
}
// Aave Protocol Data Provider
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
);
function getReserveConfigurationData(address asset) external view returns (
uint256 decimals,
uint256 ltv,
uint256 liquidationThreshold,
uint256 liquidationBonus,
uint256 reserveFactor,
bool usageAsCollateralEnabled,
bool borrowingEnabled,
bool stableBorrowRateEnabled,
bool isActive,
bool isFrozen
);
}
interface AaveAddressProviderRegistryInterface {
function getAddressesProvidersList() external view returns (address[] memory);
}
interface ATokenInterface {
function scaledBalanceOf(address _user) external view returns (uint256);
function isTransferAllowed(address _user, uint256 _amount) external view returns (bool);
function balanceOf(address _user) external view returns(uint256);
function transferFrom(address, address, uint) external returns (bool);
}
interface StateSenderInterface {
function syncState(address receiver, bytes calldata data) external;
function register(address sender, address receiver) external;
}

View File

@ -0,0 +1,130 @@
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
import { TokenInterface, AccountInterface } from "../../common/interfaces.sol";
import { AaveInterface, ATokenInterface } from "./interfaces.sol";
import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol";
abstract contract AaveResolver is Helpers, Events {
function _paybackBehalfOne(AaveInterface aave, address token, uint amt, uint rateMode, address user) private {
aave.repay(token, amt, rateMode, user);
}
function _PaybackStable(
uint _length,
AaveInterface aave,
address[] memory tokens,
uint256[] memory amts,
address user
) internal {
for (uint i = 0; i < _length; i++) {
if (amts[i] > 0) {
_paybackBehalfOne(aave, tokens[i], amts[i], 1, user);
}
}
}
function _PaybackVariable(
uint _length,
AaveInterface aave,
address[] memory tokens,
uint256[] memory amts,
address user
) internal {
for (uint i = 0; i < _length; i++) {
if (amts[i] > 0) {
_paybackBehalfOne(aave, tokens[i], amts[i], 2, user);
}
}
}
function _Withdraw(
uint _length,
AaveInterface aave,
address[] memory tokens,
uint256[] memory amts
) internal {
for (uint i = 0; i < _length; i++) {
if (amts[i] > 0) {
aave.withdraw(tokens[i], amts[i], fundLocker);
}
}
}
}
contract AaveImportResolver is AaveResolver {
struct AaveData {
bool isFinal;
uint[] supplyAmts;
uint[] variableBorrowAmts;
uint[] stableBorrowAmts;
address[] supplyTokens;
address[] borrowTokens;
}
mapping (address => AaveData) public records;
function migrate(
address[] calldata supplyTokens,
address[] calldata borrowTokens
) external payable returns (string memory _eventName, bytes memory _eventParam) {
require(supplyTokens.length > 0, "0-length-not-allowed");
AaveData memory data;
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
data.supplyAmts = new uint[](supplyTokens.length);
data.supplyTokens = new address[](supplyTokens.length);
for (uint i = 0; i < supplyTokens.length; i++) {
address _token = supplyTokens[i] == ethAddr ? wethAddr : supplyTokens[i];
(address _aToken, ,) = aaveData.getReserveTokensAddresses(_token);
data.supplyTokens[i] = _token;
data.supplyAmts[i] = ATokenInterface(_aToken).balanceOf(address(this));
}
if (borrowTokens.length > 0) {
data.variableBorrowAmts = new uint[](borrowTokens.length);
data.stableBorrowAmts = new uint[](borrowTokens.length);
for (uint i = 0; i < borrowTokens.length; i++) {
address _token = borrowTokens[i] == ethAddr ? wethAddr : borrowTokens[i];
data.borrowTokens[i] = _token;
(
,
data.stableBorrowAmts[i],
data.variableBorrowAmts[i],
,,,,,
) = aaveData.getUserReserveData(_token, address(this));
uint totalBorrowAmt = add(data.stableBorrowAmts[i], data.variableBorrowAmts[i]);
if (totalBorrowAmt > 0) {
TokenInterface(_token).approve(address(aave), totalBorrowAmt);
}
}
_PaybackStable(borrowTokens.length, aave, data.borrowTokens, data.stableBorrowAmts, address(this));
_PaybackVariable(borrowTokens.length, aave, data.borrowTokens, data.variableBorrowAmts, address(this));
}
_Withdraw(supplyTokens.length, aave, data.supplyTokens, data.supplyAmts);
records[msg.sender] = data;
bytes memory positionData = abi.encode(msg.sender, data);
stateSender.syncState(polygonReceiver, positionData);
_eventName = "LogAaveV2Migrate(address,address[],address[],uint256[],uint256[],uint256[])";
_eventParam = abi.encode(
msg.sender,
supplyTokens,
borrowTokens,
data.supplyAmts,
data.stableBorrowAmts,
data.variableBorrowAmts
);
}
}

13
package-lock.json generated
View File

@ -7,6 +7,9 @@
"": {
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"@openzeppelin/contracts": "^3.4.0-solc-0.7"
},
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-waffle": "^2.0.1",
@ -1141,6 +1144,11 @@
"hardhat": "^2.0.0"
}
},
"node_modules/@openzeppelin/contracts": {
"version": "3.4.0-solc-0.7",
"resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-3.4.0-solc-0.7.tgz",
"integrity": "sha512-bJz1YgmeKRYVnYZedYSiy5/YmaYTxOhb76ftCseN1z2r4DK7PwCGvRCF2fRavTxAmGkIC/Q5zSy/Dr3OerPw0w=="
},
"node_modules/@resolver-engine/core": {
"version": "0.3.3",
"resolved": "https://registry.npmjs.org/@resolver-engine/core/-/core-0.3.3.tgz",
@ -16550,6 +16558,11 @@
"@types/web3": "1.0.19"
}
},
"@openzeppelin/contracts": {
"version": "3.4.0-solc-0.7",
"resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-3.4.0-solc-0.7.tgz",
"integrity": "sha512-bJz1YgmeKRYVnYZedYSiy5/YmaYTxOhb76ftCseN1z2r4DK7PwCGvRCF2fRavTxAmGkIC/Q5zSy/Dr3OerPw0w=="
},
"@resolver-engine/core": {
"version": "0.3.3",
"resolved": "https://registry.npmjs.org/@resolver-engine/core/-/core-0.3.3.tgz",

View File

@ -16,5 +16,8 @@
"ethereum-waffle": "^3.3.0",
"ethers": "^5.1.0",
"hardhat": "^2.1.2"
},
"dependencies": {
"@openzeppelin/contracts": "^3.4.0-solc-0.7"
}
}