mirror of
https://github.com/Instadapp/dsa-connectors-old.git
synced 2024-07-29 22:47:46 +00:00
done with aave connector
This commit is contained in:
parent
3c63d3369b
commit
96346543b9
|
|
@ -49,22 +49,30 @@ contract AaveHelpers is DSMath, Stores {
|
||||||
/**
|
/**
|
||||||
* @dev get Aave Address
|
* @dev get Aave Address
|
||||||
*/
|
*/
|
||||||
function getAaveProviderAddress() internal pure returns (address) {
|
function getAaveAddress() internal pure returns (address) {
|
||||||
// return 0x24a42fD28C976A61Df5D00D0599C34c4f90748c8; //mainnet
|
// return 0x24a42fD28C976A61Df5D00D0599C34c4f90748c8; //mainnet
|
||||||
// return 0x506B0B2CF20FAA8f38a4E2B524EE43e1f4458Cc5; //kovan
|
return 0x580D4Fdc4BF8f9b5ae2fb9225D584fED4AD5375c; //kovan
|
||||||
return 0x1c8756FD2B28e9426CDBDcC7E3c4d64fa9A54728; //ropsten
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev get Aave Core Address
|
||||||
|
*/
|
||||||
|
function getAaveCoreAddress() internal pure returns (address) {
|
||||||
|
// return 0x24a42fD28C976A61Df5D00D0599C34c4f90748c8; //mainnet
|
||||||
|
return 0x95D1189Ed88B380E319dF73fF00E479fcc4CFa45; //kovan
|
||||||
|
}
|
||||||
|
|
||||||
|
function getIsColl(AaveInterface aave, address token) internal view returns (bool isCol) {
|
||||||
|
(, , , , , , , , , isCol) = aave.getUserReserveData(token, address(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
function getWithdrawBalance(address token) internal view returns (uint bal) {
|
function getWithdrawBalance(address token) internal view returns (uint bal) {
|
||||||
AaveProviderInterface lendingProviderPool = AaveProviderInterface(getAaveProviderAddress());
|
AaveInterface aave = AaveInterface(getAaveAddress());
|
||||||
AaveInterface aave = AaveInterface(lendingProviderPool.getLendingPool());
|
(bal, , , , , , , , , ) = aave.getUserReserveData(token, address(this));
|
||||||
(bal, , , , , , , , , ) = aave.getUserReserveData(token, msg.sender);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPaybackBalance(address token) internal view returns (uint bal) {
|
function getPaybackBalance(AaveInterface aave, address token) internal view returns (uint bal) {
|
||||||
AaveProviderInterface lendingProviderPool = AaveProviderInterface(getAaveProviderAddress());
|
(, bal, , , , , , , , ) = aave.getUserReserveData(token, address(this));
|
||||||
AaveInterface aave = AaveInterface(lendingProviderPool.getLendingPool());
|
|
||||||
(, bal, , , , , , , , ) = aave.getUserReserveData(token, msg.sender);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,21 +92,24 @@ contract BasicResolver is AaveHelpers {
|
||||||
*/
|
*/
|
||||||
function deposit(address token, uint amt, uint getId, uint setId) external payable{
|
function deposit(address token, uint amt, uint getId, uint setId) external payable{
|
||||||
uint _amt = getUint(getId, amt);
|
uint _amt = getUint(getId, amt);
|
||||||
AaveProviderInterface lendingProviderPool = AaveProviderInterface(getAaveProviderAddress());
|
AaveInterface aave = AaveInterface(getAaveAddress());
|
||||||
AaveInterface aave = AaveInterface(lendingProviderPool.getLendingPool());
|
|
||||||
|
|
||||||
uint ethAmt;
|
uint ethAmt;
|
||||||
if (token == getEthAddr()) {
|
if (token == getEthAddr()) {
|
||||||
require(_amt == msg.value, "not-enought-eth");
|
require(_amt == msg.value, "not-enought-eth");
|
||||||
|
_amt = _amt == uint(-1) ? address(this).balance : _amt;
|
||||||
ethAmt = _amt;
|
ethAmt = _amt;
|
||||||
} else {
|
} else {
|
||||||
TokenInterface tokenContract = TokenInterface(token);
|
TokenInterface tokenContract = TokenInterface(token);
|
||||||
|
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
|
||||||
require(tokenContract.balanceOf(address(this)) >= _amt, "not-enough-token");
|
require(tokenContract.balanceOf(address(this)) >= _amt, "not-enough-token");
|
||||||
tokenContract.approve(lendingProviderPool.getLendingPoolCore(), _amt);
|
tokenContract.approve(getAaveCoreAddress(), _amt);
|
||||||
}
|
}
|
||||||
|
|
||||||
aave.deposit.value(ethAmt)(token, amt, 0); // TODO - need to set referralCode;
|
aave.deposit.value(ethAmt)(token, _amt, 0); // TODO - need to set referralCode;
|
||||||
aave.setUserUseReserveAsCollateral(token, true);
|
|
||||||
|
if (!getIsColl(aave, token)) aave.setUserUseReserveAsCollateral(token, true);
|
||||||
|
|
||||||
setUint(setId, _amt);
|
setUint(setId, _amt);
|
||||||
|
|
||||||
emit LogDeposit(token, _amt, getId, setId);
|
emit LogDeposit(token, _amt, getId, setId);
|
||||||
|
|
@ -117,19 +128,16 @@ contract BasicResolver is AaveHelpers {
|
||||||
*/
|
*/
|
||||||
function withdraw(address token, uint amt, uint getId, uint setId) external payable{
|
function withdraw(address token, uint amt, uint getId, uint setId) external payable{
|
||||||
uint _amt = getUint(getId, amt);
|
uint _amt = getUint(getId, amt);
|
||||||
AaveProviderInterface lendingProviderPool = AaveProviderInterface(getAaveProviderAddress());
|
AaveCoreInterface aaveCore = AaveCoreInterface(getAaveCoreAddress());
|
||||||
AaveCoreInterface aaveCore = AaveCoreInterface(lendingProviderPool.getLendingPoolCore());
|
|
||||||
ATokenInterface atoken = ATokenInterface(aaveCore.getReserveATokenAddress(token));
|
ATokenInterface atoken = ATokenInterface(aaveCore.getReserveATokenAddress(token));
|
||||||
|
|
||||||
uint totalBal = atoken.balanceOf(address(this));
|
_amt = _amt == uint(-1) ? atoken.balanceOf(address(this)) : _amt;
|
||||||
|
|
||||||
_amt = _amt >= totalBal ? totalBal : _amt;
|
|
||||||
|
|
||||||
uint initialBal = token == getEthAddr() ? address(this).balance : TokenInterface(token).balanceOf(address(this));
|
uint initialBal = token == getEthAddr() ? address(this).balance : TokenInterface(token).balanceOf(address(this));
|
||||||
atoken.redeem(_amt);
|
atoken.redeem(_amt);
|
||||||
uint finialBal = token == getEthAddr() ? address(this).balance : TokenInterface(token).balanceOf(address(this));
|
uint finialBal = token == getEthAddr() ? address(this).balance : TokenInterface(token).balanceOf(address(this));
|
||||||
uint withdrawnAmt = sub(finialBal, initialBal);
|
|
||||||
require(withdrawnAmt >= _amt, "withdraw-error");
|
_amt = sub(finialBal, initialBal);
|
||||||
setUint(setId, _amt);
|
setUint(setId, _amt);
|
||||||
|
|
||||||
emit LogWithdraw(token, _amt, getId, setId);
|
emit LogWithdraw(token, _amt, getId, setId);
|
||||||
|
|
@ -148,9 +156,8 @@ contract BasicResolver is AaveHelpers {
|
||||||
*/
|
*/
|
||||||
function borrow(address token, uint amt, uint getId, uint setId) external payable {
|
function borrow(address token, uint amt, uint getId, uint setId) external payable {
|
||||||
uint _amt = getUint(getId, amt);
|
uint _amt = getUint(getId, amt);
|
||||||
AaveProviderInterface lendingProviderPool = AaveProviderInterface(getAaveProviderAddress());
|
AaveInterface aave = AaveInterface(getAaveAddress());
|
||||||
AaveInterface aave = AaveInterface(lendingProviderPool.getLendingPool());
|
aave.borrow(token, _amt, 2, 0); // TODO - need to set referralCode;
|
||||||
aave.borrow(token, amt, 2, 0); // TODO - need to set referralCode;
|
|
||||||
setUint(setId, _amt);
|
setUint(setId, _amt);
|
||||||
|
|
||||||
emit LogBorrow(token, _amt, getId, setId);
|
emit LogBorrow(token, _amt, getId, setId);
|
||||||
|
|
@ -169,9 +176,9 @@ contract BasicResolver is AaveHelpers {
|
||||||
*/
|
*/
|
||||||
function payback(address token, uint amt, uint getId, uint setId) external payable {
|
function payback(address token, uint amt, uint getId, uint setId) external payable {
|
||||||
uint _amt = getUint(getId, amt);
|
uint _amt = getUint(getId, amt);
|
||||||
AaveProviderInterface lendingProviderPool = AaveProviderInterface(getAaveProviderAddress());
|
AaveInterface aave = AaveInterface(getAaveAddress());
|
||||||
AaveInterface aave = AaveInterface(lendingProviderPool.getLendingPool());
|
|
||||||
_amt = _amt == uint(-1) ? getPaybackBalance(token) : _amt;
|
_amt = _amt == uint(-1) ? getPaybackBalance(aave, token) : _amt;
|
||||||
|
|
||||||
uint ethAmt;
|
uint ethAmt;
|
||||||
if (token == getEthAddr()) {
|
if (token == getEthAddr()) {
|
||||||
|
|
@ -180,10 +187,10 @@ contract BasicResolver is AaveHelpers {
|
||||||
} else {
|
} else {
|
||||||
TokenInterface tokenContract = TokenInterface(token);
|
TokenInterface tokenContract = TokenInterface(token);
|
||||||
require(tokenContract.balanceOf(address(this)) >= _amt, "not-enough-token");
|
require(tokenContract.balanceOf(address(this)) >= _amt, "not-enough-token");
|
||||||
tokenContract.approve(lendingProviderPool.getLendingPoolCore(), _amt);
|
tokenContract.approve(getAaveCoreAddress(), _amt);
|
||||||
}
|
}
|
||||||
|
|
||||||
aave.repay.value(ethAmt)(token, amt, payable(address(this)));
|
aave.repay.value(ethAmt)(token, _amt, payable(address(this)));
|
||||||
|
|
||||||
setUint(setId, _amt);
|
setUint(setId, _amt);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user