mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
Merge pull request #88 from Benqi-fi/feature/avalanche-benqi
Add Avalanche, Benqi connectors
This commit is contained in:
commit
5389b7e1a2
6
contracts/avalanche/connectors/Qi/events.sol
Normal file
6
contracts/avalanche/connectors/Qi/events.sol
Normal file
|
@ -0,0 +1,6 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogClaimedReward(uint256 rewardAmt, uint256 setId);
|
||||
event LogDelegate(address delegatee);
|
||||
}
|
59
contracts/avalanche/connectors/Qi/helpers.sol
Normal file
59
contracts/avalanche/connectors/Qi/helpers.sol
Normal file
|
@ -0,0 +1,59 @@
|
|||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import { DSMath } from "../../common/math.sol";
|
||||
import { Basic } from "../../common/basic.sol";
|
||||
import { ComptrollerInterface, QiInterface, BenqiMappingInterface } from "./interface.sol";
|
||||
|
||||
abstract contract Helpers is DSMath, Basic {
|
||||
/**
|
||||
* @dev Benqi Comptroller
|
||||
*/
|
||||
ComptrollerInterface internal constant troller = ComptrollerInterface(0x486Af39519B4Dc9a7fCcd318217352830E8AD9b4);
|
||||
|
||||
/**
|
||||
* @dev Reward Token
|
||||
*/
|
||||
QiInterface internal constant benqiToken = QiInterface(0x8729438EB15e2C8B576fCc6AeCdA6A148776C0F5);
|
||||
|
||||
/**
|
||||
* @dev Benqi Mapping
|
||||
*/
|
||||
BenqiMappingInterface internal constant qiMapping = BenqiMappingInterface(0xe19Fba29ac9BAACc1F584aEcD9C98B4F6fC58ba6);
|
||||
|
||||
/**
|
||||
* @dev Benqi reward token type to show BENQI or AVAX
|
||||
*/
|
||||
uint8 internal constant rewardQi = 0;
|
||||
uint8 internal constant rewardAvax = 1;
|
||||
|
||||
function getMergedQiTokens(
|
||||
string[] calldata supplyIds,
|
||||
string[] calldata borrowIds
|
||||
) internal view returns (address[] memory qitokens, bool isBorrow, bool isSupply) {
|
||||
uint _supplyLen = supplyIds.length;
|
||||
uint _borrowLen = borrowIds.length;
|
||||
uint _totalLen = add(_supplyLen, _borrowLen);
|
||||
qitokens = new address[](_totalLen);
|
||||
|
||||
if(_supplyLen > 0) {
|
||||
isSupply = true;
|
||||
for (uint i = 0; i < _supplyLen; i++) {
|
||||
(address token, address qiToken) = qiMapping.getMapping(supplyIds[i]);
|
||||
require(token != address(0) && qiToken != address(0), "invalid token/qitoken address");
|
||||
|
||||
qitokens[i] = qiToken;
|
||||
}
|
||||
}
|
||||
|
||||
if(_borrowLen > 0) {
|
||||
isBorrow = true;
|
||||
for (uint i = 0; i < _borrowLen; i++) {
|
||||
(address token, address qiToken) = qiMapping.getMapping(borrowIds[i]);
|
||||
require(token != address(0) && qiToken != address(0), "invalid token/qitoken address");
|
||||
|
||||
qitokens[_supplyLen + i] = qiToken;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
17
contracts/avalanche/connectors/Qi/interface.sol
Normal file
17
contracts/avalanche/connectors/Qi/interface.sol
Normal file
|
@ -0,0 +1,17 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
interface ComptrollerInterface {
|
||||
function claimReward(uint8 rewardType, address holder) external;
|
||||
function claimReward(uint8 rewardType, address holder, address[] calldata) external;
|
||||
function claimReward(uint8 rewardType, address[] calldata holders, address[] calldata qiTokens, bool borrowers, bool suppliers) external;
|
||||
}
|
||||
|
||||
interface QiInterface {
|
||||
function delegate(address delegatee) external;
|
||||
function delegates(address) external view returns(address);
|
||||
}
|
||||
|
||||
interface BenqiMappingInterface {
|
||||
function qiTokenMapping(string calldata tokenId) external view returns (address);
|
||||
function getMapping(string calldata tokenId) external view returns (address, address);
|
||||
}
|
103
contracts/avalanche/connectors/Qi/main.sol
Normal file
103
contracts/avalanche/connectors/Qi/main.sol
Normal file
|
@ -0,0 +1,103 @@
|
|||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
/**
|
||||
* @title Reward.
|
||||
* @dev Claim Reward.
|
||||
*/
|
||||
import { TokenInterface } from "../../common/interfaces.sol";
|
||||
import { Stores } from "../../common/stores.sol";
|
||||
import { Helpers } from "./helpers.sol";
|
||||
import { Events } from "./events.sol";
|
||||
|
||||
abstract contract BenqiResolver is Events, Helpers {
|
||||
|
||||
/**
|
||||
* @dev Claim Accrued Qi Token.
|
||||
* @notice Claim Accrued Qi Token.
|
||||
* @param setId ID stores the amount of Reward claimed.
|
||||
*/
|
||||
function ClaimReward(uint256 setId) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
TokenInterface _benqiToken = TokenInterface(address(benqiToken));
|
||||
uint intialBal = _benqiToken.balanceOf(address(this));
|
||||
troller.claimReward(rewardQi, address(this));
|
||||
uint finalBal = _benqiToken.balanceOf(address(this));
|
||||
uint amt = sub(finalBal, intialBal);
|
||||
|
||||
setUint(setId, amt);
|
||||
|
||||
_eventName = "LogClaimedReward(uint256,uint256)";
|
||||
_eventParam = abi.encode(amt, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Claim Accrued Qi Token.
|
||||
* @notice Claim Accrued Qi Token.
|
||||
* @param tokenIds Array of supplied and borrowed token IDs.
|
||||
* @param setId ID stores the amount of Reward claimed.
|
||||
*/
|
||||
function ClaimRewardTwo(string[] calldata tokenIds, uint256 setId) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint _len = tokenIds.length;
|
||||
address[] memory qitokens = new address[](_len);
|
||||
for (uint i = 0; i < _len; i++) {
|
||||
(address token, address qiToken) = qiMapping.getMapping(tokenIds[i]);
|
||||
require(token != address(0) && qiToken != address(0), "invalid token/qitoken address");
|
||||
|
||||
qitokens[i] = qiToken;
|
||||
}
|
||||
|
||||
TokenInterface _benqiToken = TokenInterface(address(benqiToken));
|
||||
uint intialBal = _benqiToken.balanceOf(address(this));
|
||||
troller.claimReward(rewardQi, address(this), qitokens);
|
||||
uint finalBal = _benqiToken.balanceOf(address(this));
|
||||
uint amt = sub(finalBal, intialBal);
|
||||
|
||||
setUint(setId, amt);
|
||||
|
||||
_eventName = "LogClaimedReward(uint256,uint256)";
|
||||
_eventParam = abi.encode(amt, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Claim Accrued Qi Token.
|
||||
* @notice Claim Accrued Qi Token.
|
||||
* @param supplyTokenIds Array of supplied tokenIds.
|
||||
* @param borrowTokenIds Array of borrowed tokenIds.
|
||||
* @param setId ID stores the amount of Reward claimed.
|
||||
*/
|
||||
function ClaimRewardThree(string[] calldata supplyTokenIds, string[] calldata borrowTokenIds, uint256 setId) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
(address[] memory qitokens, bool isBorrow, bool isSupply) = getMergedQiTokens(supplyTokenIds, borrowTokenIds);
|
||||
|
||||
address[] memory holders = new address[](1);
|
||||
holders[0] = address(this);
|
||||
|
||||
TokenInterface _benqiToken = TokenInterface(address(benqiToken));
|
||||
uint intialBal = _benqiToken.balanceOf(address(this));
|
||||
troller.claimReward(rewardQi, holders, qitokens, isBorrow, isSupply);
|
||||
uint finalBal = _benqiToken.balanceOf(address(this));
|
||||
uint amt = sub(finalBal, intialBal);
|
||||
|
||||
setUint(setId, amt);
|
||||
|
||||
_eventName = "LogClaimedReward(uint256,uint256)";
|
||||
_eventParam = abi.encode(amt, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Delegate votes.
|
||||
* @notice Delegate votes.
|
||||
* @param delegatee The address to delegate the votes to.
|
||||
*/
|
||||
function delegate(address delegatee) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
require(benqiToken.delegates(address(this)) != delegatee, "Already delegated to same delegatee.");
|
||||
|
||||
benqiToken.delegate(delegatee);
|
||||
|
||||
_eventName = "LogDelegate(address)";
|
||||
_eventParam = abi.encode(delegatee);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectV2QiAvalanche is BenqiResolver {
|
||||
string public constant name = "Benqi-v1";
|
||||
}
|
62
contracts/avalanche/connectors/benqi/events.sol
Normal file
62
contracts/avalanche/connectors/benqi/events.sol
Normal file
|
@ -0,0 +1,62 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogDeposit(
|
||||
address indexed token,
|
||||
address qiToken,
|
||||
uint256 tokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogWithdraw(
|
||||
address indexed token,
|
||||
address qiToken,
|
||||
uint256 tokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogBorrow(
|
||||
address indexed token,
|
||||
address qiToken,
|
||||
uint256 tokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogPayback(
|
||||
address indexed token,
|
||||
address qiToken,
|
||||
uint256 tokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogDepositQiToken(
|
||||
address indexed token,
|
||||
address qiToken,
|
||||
uint256 tokenAmt,
|
||||
uint256 qiTokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogWithdrawQiToken(
|
||||
address indexed token,
|
||||
address qiToken,
|
||||
uint256 tokenAmt,
|
||||
uint256 qiTokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
|
||||
event LogLiquidate(
|
||||
address indexed borrower,
|
||||
address indexed tokenToPay,
|
||||
address indexed tokenInReturn,
|
||||
uint256 tokenAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
}
|
35
contracts/avalanche/connectors/benqi/helpers.sol
Normal file
35
contracts/avalanche/connectors/benqi/helpers.sol
Normal file
|
@ -0,0 +1,35 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import { DSMath } from "../../common/math.sol";
|
||||
import { Basic } from "../../common/basic.sol";
|
||||
import { ComptrollerInterface, BenqiMappingInterface } from "./interface.sol";
|
||||
|
||||
abstract contract Helpers is DSMath, Basic {
|
||||
/**
|
||||
* @dev Benqi Comptroller
|
||||
*/
|
||||
ComptrollerInterface internal constant troller = ComptrollerInterface(0x486Af39519B4Dc9a7fCcd318217352830E8AD9b4);
|
||||
|
||||
/**
|
||||
* @dev Benqi Mapping
|
||||
*/
|
||||
BenqiMappingInterface internal constant qiMapping = BenqiMappingInterface(0xe19Fba29ac9BAACc1F584aEcD9C98B4F6fC58ba6);
|
||||
|
||||
/**
|
||||
* @dev enter benqi market
|
||||
*/
|
||||
function enterMarket(address qiToken) internal {
|
||||
address[] memory markets = troller.getAssetsIn(address(this));
|
||||
bool isEntered = false;
|
||||
for (uint i = 0; i < markets.length; i++) {
|
||||
if (markets[i] == qiToken) {
|
||||
isEntered = true;
|
||||
}
|
||||
}
|
||||
if (!isEntered) {
|
||||
address[] memory toEnter = new address[](1);
|
||||
toEnter[0] = qiToken;
|
||||
troller.enterMarkets(toEnter);
|
||||
}
|
||||
}
|
||||
}
|
36
contracts/avalanche/connectors/benqi/interface.sol
Normal file
36
contracts/avalanche/connectors/benqi/interface.sol
Normal file
|
@ -0,0 +1,36 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
interface QiTokenInterface {
|
||||
function mint(uint mintAmount) external returns (uint);
|
||||
function redeem(uint redeemTokens) external returns (uint);
|
||||
function borrow(uint borrowAmount) external returns (uint);
|
||||
function repayBorrow(uint repayAmount) external returns (uint);
|
||||
function repayBorrowBehalf(address borrower, uint repayAmount) external returns (uint); // For ARC20
|
||||
function liquidateBorrow(address borrower, uint repayAmount, address qiTokenCollateral) external returns (uint);
|
||||
|
||||
function borrowBalanceCurrent(address account) external returns (uint);
|
||||
function redeemUnderlying(uint redeemAmount) external returns (uint);
|
||||
function exchangeRateCurrent() external returns (uint);
|
||||
|
||||
function balanceOf(address owner) external view returns (uint256 balance);
|
||||
}
|
||||
|
||||
interface QiAVAXInterface {
|
||||
function mint() external payable;
|
||||
function repayBorrow() external payable;
|
||||
function repayBorrowBehalf(address borrower) external payable;
|
||||
function liquidateBorrow(address borrower, address qiTokenCollateral) external payable;
|
||||
}
|
||||
|
||||
interface ComptrollerInterface {
|
||||
function enterMarkets(address[] calldata qiTokens) external returns (uint[] memory);
|
||||
function exitMarket(address qiTokenAddress) external returns (uint);
|
||||
function getAssetsIn(address account) external view returns (address[] memory);
|
||||
function getAccountLiquidity(address account) external view returns (uint, uint, uint);
|
||||
function claimReward(uint8 rewardType, address) external;
|
||||
}
|
||||
|
||||
interface BenqiMappingInterface {
|
||||
function qiTokenMapping(string calldata tokenId) external view returns (address);
|
||||
function getMapping(string calldata tokenId) external view returns (address, address);
|
||||
}
|
441
contracts/avalanche/connectors/benqi/main.sol
Normal file
441
contracts/avalanche/connectors/benqi/main.sol
Normal file
|
@ -0,0 +1,441 @@
|
|||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
/**
|
||||
* @title Benqi.
|
||||
* @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 { QiAVAXInterface, QiTokenInterface } from "./interface.sol";
|
||||
|
||||
abstract contract BenqiResolver is Events, Helpers {
|
||||
/**
|
||||
* @dev Deposit AVAX/ARC20_Token.
|
||||
* @notice Deposit a token to Benqi for lending / collaterization.
|
||||
* @param token The address of the token to deposit. (For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param qiToken The address of the corresponding qiToken.
|
||||
* @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 depositRaw(
|
||||
address token,
|
||||
address qiToken,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) public payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint _amt = getUint(getId, amt);
|
||||
|
||||
require(token != address(0) && qiToken != address(0), "invalid token/qitoken address");
|
||||
|
||||
enterMarket(qiToken);
|
||||
if (token == avaxAddr) {
|
||||
_amt = _amt == uint(-1) ? address(this).balance : _amt;
|
||||
QiAVAXInterface(qiToken).mint{value: _amt}();
|
||||
} else {
|
||||
TokenInterface tokenContract = TokenInterface(token);
|
||||
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
|
||||
approve(tokenContract, qiToken, _amt);
|
||||
require(QiTokenInterface(qiToken).mint(_amt) == 0, "deposit-failed");
|
||||
}
|
||||
setUint(setId, _amt);
|
||||
|
||||
_eventName = "LogDeposit(address,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(token, qiToken, _amt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Deposit AVAX/ARC20_Token using the Mapping.
|
||||
* @notice Deposit a token to Benqi for lending / collaterization.
|
||||
* @param tokenId The token id of the token to deposit.(For eg: AVAX-A)
|
||||
* @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(
|
||||
string calldata tokenId,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
(address token, address qiToken) = qiMapping.getMapping(tokenId);
|
||||
(_eventName, _eventParam) = depositRaw(token, qiToken, amt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Withdraw AVAX/ARC20_Token.
|
||||
* @notice Withdraw deposited token from Benqi
|
||||
* @param token The address of the token to withdraw. (For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param qiToken The address of the corresponding qiToken.
|
||||
* @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 withdrawRaw(
|
||||
address token,
|
||||
address qiToken,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) public payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint _amt = getUint(getId, amt);
|
||||
|
||||
require(token != address(0) && qiToken != address(0), "invalid token/qitoken address");
|
||||
|
||||
QiTokenInterface qiTokenContract = QiTokenInterface(qiToken);
|
||||
if (_amt == uint(-1)) {
|
||||
TokenInterface tokenContract = TokenInterface(token);
|
||||
uint initialBal = token == avaxAddr ? address(this).balance : tokenContract.balanceOf(address(this));
|
||||
require(qiTokenContract.redeem(qiTokenContract.balanceOf(address(this))) == 0, "full-withdraw-failed");
|
||||
uint finalBal = token == avaxAddr ? address(this).balance : tokenContract.balanceOf(address(this));
|
||||
_amt = finalBal - initialBal;
|
||||
} else {
|
||||
require(qiTokenContract.redeemUnderlying(_amt) == 0, "withdraw-failed");
|
||||
}
|
||||
setUint(setId, _amt);
|
||||
|
||||
_eventName = "LogWithdraw(address,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(token, qiToken, _amt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Withdraw AVAX/ARC20_Token using the Mapping.
|
||||
* @notice Withdraw deposited token from Benqi
|
||||
* @param tokenId The token id of the token to withdraw.(For eg: AVAX-A)
|
||||
* @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(
|
||||
string calldata tokenId,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
(address token, address qiToken) = qiMapping.getMapping(tokenId);
|
||||
(_eventName, _eventParam) = withdrawRaw(token, qiToken, amt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Borrow AVAX/ARC20_Token.
|
||||
* @notice Borrow a token using Benqi
|
||||
* @param token The address of the token to borrow. (For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param qiToken The address of the corresponding qiToken.
|
||||
* @param amt The amount of the token to borrow.
|
||||
* @param getId ID to retrieve amt.
|
||||
* @param setId ID stores the amount of tokens borrowed.
|
||||
*/
|
||||
function borrowRaw(
|
||||
address token,
|
||||
address qiToken,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) public payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint _amt = getUint(getId, amt);
|
||||
|
||||
require(token != address(0) && qiToken != address(0), "invalid token/qitoken address");
|
||||
|
||||
enterMarket(qiToken);
|
||||
require(QiTokenInterface(qiToken).borrow(_amt) == 0, "borrow-failed");
|
||||
setUint(setId, _amt);
|
||||
|
||||
_eventName = "LogBorrow(address,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(token, qiToken, _amt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Borrow AVAX/ARC20_Token using the Mapping.
|
||||
* @notice Borrow a token using Benqi
|
||||
* @param tokenId The token id of the token to borrow.(For eg: DAI-A)
|
||||
* @param amt The amount of the token to borrow.
|
||||
* @param getId ID to retrieve amt.
|
||||
* @param setId ID stores the amount of tokens borrowed.
|
||||
*/
|
||||
function borrow(
|
||||
string calldata tokenId,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
(address token, address qiToken) = qiMapping.getMapping(tokenId);
|
||||
(_eventName, _eventParam) = borrowRaw(token, qiToken, amt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Payback borrowed AVAX/ARC20_Token.
|
||||
* @notice Payback debt owed.
|
||||
* @param token The address of the token to payback. (For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param qiToken The address of the corresponding qiToken.
|
||||
* @param amt The amount of the token to payback. (For max: `uint256(-1)`)
|
||||
* @param getId ID to retrieve amt.
|
||||
* @param setId ID stores the amount of tokens paid back.
|
||||
*/
|
||||
function paybackRaw(
|
||||
address token,
|
||||
address qiToken,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) public payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint _amt = getUint(getId, amt);
|
||||
|
||||
require(token != address(0) && qiToken != address(0), "invalid token/qitoken address");
|
||||
|
||||
QiTokenInterface qiTokenContract = QiTokenInterface(qiToken);
|
||||
_amt = _amt == uint(-1) ? qiTokenContract.borrowBalanceCurrent(address(this)) : _amt;
|
||||
|
||||
if (token == avaxAddr) {
|
||||
require(address(this).balance >= _amt, "not-enough-avax");
|
||||
QiAVAXInterface(qiToken).repayBorrow{value: _amt}();
|
||||
} else {
|
||||
TokenInterface tokenContract = TokenInterface(token);
|
||||
require(tokenContract.balanceOf(address(this)) >= _amt, "not-enough-token");
|
||||
approve(tokenContract, qiToken, _amt);
|
||||
require(qiTokenContract.repayBorrow(_amt) == 0, "repay-failed.");
|
||||
}
|
||||
setUint(setId, _amt);
|
||||
|
||||
_eventName = "LogPayback(address,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(token, qiToken, _amt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Payback borrowed AVAX/ARC20_Token using the Mapping.
|
||||
* @notice Payback debt owed.
|
||||
* @param tokenId The token id of the token to payback.(For eg: BENQI-A)
|
||||
* @param amt The amount of the token to payback. (For max: `uint256(-1)`)
|
||||
* @param getId ID to retrieve amt.
|
||||
* @param setId ID stores the amount of tokens paid back.
|
||||
*/
|
||||
function payback(
|
||||
string calldata tokenId,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
(address token, address qiToken) = qiMapping.getMapping(tokenId);
|
||||
(_eventName, _eventParam) = paybackRaw(token, qiToken, amt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Deposit AVAX/ARC20_Token.
|
||||
* @notice Same as depositRaw. The only difference is this method stores qiToken amount in set ID.
|
||||
* @param token The address of the token to deposit. (For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param qiToken The address of the corresponding qiToken.
|
||||
* @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 qiTokens received.
|
||||
*/
|
||||
function depositQiTokenRaw(
|
||||
address token,
|
||||
address qiToken,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) public payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint _amt = getUint(getId, amt);
|
||||
|
||||
require(token != address(0) && qiToken != address(0), "invalid token/qitoken address");
|
||||
|
||||
enterMarket(qiToken);
|
||||
|
||||
QiTokenInterface qitokenContract = QiTokenInterface(qiToken);
|
||||
uint initialBal = qitokenContract.balanceOf(address(this));
|
||||
|
||||
if (token == avaxAddr) {
|
||||
_amt = _amt == uint(-1) ? address(this).balance : _amt;
|
||||
QiAVAXInterface(qiToken).mint{value: _amt}();
|
||||
} else {
|
||||
TokenInterface tokenContract = TokenInterface(token);
|
||||
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
|
||||
approve(tokenContract, qiToken, _amt);
|
||||
require(qitokenContract.mint(_amt) == 0, "deposit-qitoken-failed.");
|
||||
}
|
||||
|
||||
uint _cAmt;
|
||||
|
||||
{
|
||||
uint finalBal = qitokenContract.balanceOf(address(this));
|
||||
_cAmt = sub(finalBal, initialBal);
|
||||
|
||||
setUint(setId, _cAmt);
|
||||
}
|
||||
|
||||
_eventName = "LogDepositQiToken(address,address,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(token, qiToken, _amt, _cAmt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Deposit AVAX/ARC20_Token using the Mapping.
|
||||
* @notice Same as deposit. The only difference is this method stores qiToken amount in set ID.
|
||||
* @param tokenId The token id of the token to depositQiToken.(For eg: DAI-A)
|
||||
* @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 qiTokens received.
|
||||
*/
|
||||
function depositQiToken(
|
||||
string calldata tokenId,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
(address token, address qiToken) = qiMapping.getMapping(tokenId);
|
||||
(_eventName, _eventParam) = depositQiTokenRaw(token, qiToken, amt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Withdraw QiAVAX/QiARC20_Token using qiToken Amt.
|
||||
* @notice Same as withdrawRaw. The only difference is this method fetch qiToken amount in get ID.
|
||||
* @param token The address of the token to withdraw. (For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param qiToken The address of the corresponding qiToken.
|
||||
* @param qiTokenAmt The amount of qiTokens to withdraw
|
||||
* @param getId ID to retrieve qiTokenAmt
|
||||
* @param setId ID stores the amount of tokens withdrawn.
|
||||
*/
|
||||
function withdrawQiTokenRaw(
|
||||
address token,
|
||||
address qiToken,
|
||||
uint qiTokenAmt,
|
||||
uint getId,
|
||||
uint setId
|
||||
) public payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint _cAmt = getUint(getId, qiTokenAmt);
|
||||
require(token != address(0) && qiToken != address(0), "invalid token/qitoken address");
|
||||
|
||||
QiTokenInterface qiTokenContract = QiTokenInterface(qiToken);
|
||||
TokenInterface tokenContract = TokenInterface(token);
|
||||
_cAmt = _cAmt == uint(-1) ? qiTokenContract.balanceOf(address(this)) : _cAmt;
|
||||
|
||||
uint withdrawAmt;
|
||||
{
|
||||
uint initialBal = token != avaxAddr ? tokenContract.balanceOf(address(this)) : address(this).balance;
|
||||
require(qiTokenContract.redeem(_cAmt) == 0, "redeem-failed");
|
||||
uint finalBal = token != avaxAddr ? tokenContract.balanceOf(address(this)) : address(this).balance;
|
||||
|
||||
withdrawAmt = sub(finalBal, initialBal);
|
||||
}
|
||||
|
||||
setUint(setId, withdrawAmt);
|
||||
|
||||
_eventName = "LogWithdrawQiToken(address,address,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(token, qiToken, withdrawAmt, _cAmt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Withdraw QiAVAX/QiARC20_Token using qiToken Amt & the Mapping.
|
||||
* @notice Same as withdraw. The only difference is this method fetch qiToken amount in get ID.
|
||||
* @param tokenId The token id of the token to withdraw QiToken.(For eg: AVAX-A)
|
||||
* @param qiTokenAmt The amount of qiTokens to withdraw
|
||||
* @param getId ID to retrieve qiTokenAmt
|
||||
* @param setId ID stores the amount of tokens withdrawn.
|
||||
*/
|
||||
function withdrawQiToken(
|
||||
string calldata tokenId,
|
||||
uint qiTokenAmt,
|
||||
uint getId,
|
||||
uint setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
(address token, address qiToken) = qiMapping.getMapping(tokenId);
|
||||
(_eventName, _eventParam) = withdrawQiTokenRaw(token, qiToken, qiTokenAmt, getId, setId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Liquidate a position.
|
||||
* @notice Liquidate a position.
|
||||
* @param borrower Borrower's Address.
|
||||
* @param tokenToPay The address of the token to pay for liquidation.(For AVAX: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param qiTokenPay Corresponding qiToken address.
|
||||
* @param tokenInReturn The address of the token to return for liquidation.
|
||||
* @param qiTokenColl Corresponding qiToken address.
|
||||
* @param amt The token amount to pay for liquidation.
|
||||
* @param getId ID to retrieve amt.
|
||||
* @param setId ID stores the amount of paid for liquidation.
|
||||
*/
|
||||
function liquidateRaw(
|
||||
address borrower,
|
||||
address tokenToPay,
|
||||
address qiTokenPay,
|
||||
address tokenInReturn,
|
||||
address qiTokenColl,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) public payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
uint _amt = getUint(getId, amt);
|
||||
require(tokenToPay != address(0) && qiTokenPay != address(0), "invalid token/qitoken address");
|
||||
require(tokenInReturn != address(0) && qiTokenColl != address(0), "invalid token/qitoken address");
|
||||
|
||||
QiTokenInterface qiTokenContract = QiTokenInterface(qiTokenPay);
|
||||
|
||||
{
|
||||
(,, uint shortfal) = troller.getAccountLiquidity(borrower);
|
||||
require(shortfal != 0, "account-cannot-be-liquidated");
|
||||
_amt = _amt == uint(-1) ? qiTokenContract.borrowBalanceCurrent(borrower) : _amt;
|
||||
}
|
||||
|
||||
if (tokenToPay == avaxAddr) {
|
||||
require(address(this).balance >= _amt, "not-enough-avax");
|
||||
QiAVAXInterface(qiTokenPay).liquidateBorrow{value: _amt}(borrower, qiTokenColl);
|
||||
} else {
|
||||
TokenInterface tokenContract = TokenInterface(tokenToPay);
|
||||
require(tokenContract.balanceOf(address(this)) >= _amt, "not-enough-token");
|
||||
approve(tokenContract, qiTokenPay, _amt);
|
||||
require(qiTokenContract.liquidateBorrow(borrower, _amt, qiTokenColl) == 0, "liquidate-failed");
|
||||
}
|
||||
|
||||
setUint(setId, _amt);
|
||||
|
||||
_eventName = "LogLiquidate(address,address,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
address(this),
|
||||
tokenToPay,
|
||||
tokenInReturn,
|
||||
_amt,
|
||||
getId,
|
||||
setId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Liquidate a position using the mapping.
|
||||
* @notice Liquidate a position using the mapping.
|
||||
* @param borrower Borrower's Address.
|
||||
* @param tokenIdToPay token id of the token to pay for liquidation.(For eg: AVAX-A)
|
||||
* @param tokenIdInReturn token id of the token to return for liquidation.(For eg: USDC-A)
|
||||
* @param amt token amount to pay for liquidation.
|
||||
* @param getId ID to retrieve amt.
|
||||
* @param setId ID stores the amount of paid for liquidation.
|
||||
*/
|
||||
function liquidate(
|
||||
address borrower,
|
||||
string calldata tokenIdToPay,
|
||||
string calldata tokenIdInReturn,
|
||||
uint256 amt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
(address tokenToPay, address qiTokenToPay) = qiMapping.getMapping(tokenIdToPay);
|
||||
(address tokenInReturn, address qiTokenColl) = qiMapping.getMapping(tokenIdInReturn);
|
||||
|
||||
(_eventName, _eventParam) = liquidateRaw(
|
||||
borrower,
|
||||
tokenToPay,
|
||||
qiTokenToPay,
|
||||
tokenInReturn,
|
||||
qiTokenColl,
|
||||
amt,
|
||||
getId,
|
||||
setId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectV2BenqiAvalanche is BenqiResolver {
|
||||
string public name = "Benqi-v1";
|
||||
}
|
148
contracts/avalanche/mapping/benqi.sol
Normal file
148
contracts/avalanche/mapping/benqi.sol
Normal file
|
@ -0,0 +1,148 @@
|
|||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
interface IndexInterface {
|
||||
function master() external view returns (address);
|
||||
}
|
||||
|
||||
interface ConnectorsInterface {
|
||||
function chief(address) external view returns (bool);
|
||||
}
|
||||
|
||||
interface QiTokenInterface {
|
||||
function isQiToken() external view returns (bool);
|
||||
function underlying() external view returns (address);
|
||||
}
|
||||
|
||||
interface MappingControllerInterface {
|
||||
function hasRole(address,address) external view returns (bool);
|
||||
}
|
||||
|
||||
abstract contract Helpers {
|
||||
|
||||
struct TokenMap {
|
||||
address qitoken;
|
||||
address token;
|
||||
}
|
||||
|
||||
event LogQiTokenAdded(string indexed name, address indexed token, address indexed qitoken);
|
||||
event LogQiTokenUpdated(string indexed name, address indexed token, address indexed qitoken);
|
||||
|
||||
// InstaConnectorsV2
|
||||
ConnectorsInterface public constant connectors = ConnectorsInterface(0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14);
|
||||
// InstaIndex Address.
|
||||
IndexInterface public constant instaIndex = IndexInterface(0x6CE3e607C808b4f4C26B7F6aDAeB619e49CAbb25);
|
||||
|
||||
// InstaMappingController Address.
|
||||
MappingControllerInterface public constant mappingController = MappingControllerInterface(0xF2113d0c99f36D7D6F6c6FAf05E0863892255999);
|
||||
|
||||
address public constant avaxAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||
|
||||
mapping (string => TokenMap) public qiTokenMapping;
|
||||
|
||||
modifier isChief {
|
||||
require(msg.sender == instaIndex.master() || connectors.chief(msg.sender), "not-an-chief");
|
||||
_;
|
||||
}
|
||||
|
||||
modifier hasRoleOrIsChief {
|
||||
require(
|
||||
msg.sender == instaIndex.master() ||
|
||||
connectors.chief(msg.sender) ||
|
||||
mappingController.hasRole(address(this), msg.sender),
|
||||
"not-an-chief/controller"
|
||||
);
|
||||
_;
|
||||
}
|
||||
|
||||
function _addQitokenMapping(
|
||||
string[] memory _names,
|
||||
address[] memory _tokens,
|
||||
address[] memory _qitokens
|
||||
) internal {
|
||||
require(_names.length == _tokens.length, "addQitokenMapping: not same length");
|
||||
require(_names.length == _qitokens.length, "addQitokenMapping: not same length");
|
||||
|
||||
for (uint i = 0; i < _qitokens.length; i++) {
|
||||
TokenMap memory _data = qiTokenMapping[_names[i]];
|
||||
|
||||
require(_data.qitoken == address(0), "addQitokenMapping: mapping added already");
|
||||
require(_data.token == address(0), "addQitokenMapping: mapping added already");
|
||||
|
||||
require(_tokens[i] != address(0), "addQitokenMapping: _tokens address not vaild");
|
||||
require(_qitokens[i] != address(0), "addQitokenMapping: _qitokens address not vaild");
|
||||
|
||||
QiTokenInterface _qitokenContract = QiTokenInterface(_qitokens[i]);
|
||||
|
||||
require(_qitokenContract.isQiToken(), "addQitokenMapping: not a qiToken");
|
||||
if (_tokens[i] != avaxAddr) {
|
||||
require(_qitokenContract.underlying() == _tokens[i], "addQitokenMapping: mapping mismatch");
|
||||
}
|
||||
|
||||
qiTokenMapping[_names[i]] = TokenMap(
|
||||
_qitokens[i],
|
||||
_tokens[i]
|
||||
);
|
||||
emit LogQiTokenAdded(_names[i], _tokens[i], _qitokens[i]);
|
||||
}
|
||||
}
|
||||
|
||||
function updateQitokenMapping(
|
||||
string[] calldata _names,
|
||||
address[] memory _tokens,
|
||||
address[] calldata _qitokens
|
||||
) external isChief {
|
||||
|
||||
require(_names.length == _tokens.length, "updateQitokenMapping: not same length");
|
||||
require(_names.length == _qitokens.length, "updateQitokenMapping: not same length");
|
||||
|
||||
for (uint i = 0; i < _qitokens.length; i++) {
|
||||
TokenMap memory _data = qiTokenMapping[_names[i]];
|
||||
|
||||
require(_data.qitoken != address(0), "updateQitokenMapping: mapping does not exist");
|
||||
require(_data.token != address(0), "updateQitokenMapping: mapping does not exist");
|
||||
|
||||
require(_tokens[i] != address(0), "updateQitokenMapping: _tokens address not vaild");
|
||||
require(_qitokens[i] != address(0), "updateQitokenMapping: _qitokens address not vaild");
|
||||
|
||||
QiTokenInterface _qitokenContract = QiTokenInterface(_qitokens[i]);
|
||||
|
||||
require(_qitokenContract.isQiToken(), "updateQitokenMapping: not a qiToken");
|
||||
if (_tokens[i] != avaxAddr) {
|
||||
require(_qitokenContract.underlying() == _tokens[i], "addQitokenMapping: mapping mismatch");
|
||||
}
|
||||
|
||||
qiTokenMapping[_names[i]] = TokenMap(
|
||||
_qitokens[i],
|
||||
_tokens[i]
|
||||
);
|
||||
emit LogQiTokenUpdated(_names[i], _tokens[i], _qitokens[i]);
|
||||
}
|
||||
}
|
||||
|
||||
function addQitokenMapping(
|
||||
string[] memory _names,
|
||||
address[] memory _tokens,
|
||||
address[] memory _qitokens
|
||||
) external hasRoleOrIsChief {
|
||||
_addQitokenMapping(_names, _tokens, _qitokens);
|
||||
}
|
||||
|
||||
function getMapping(string memory _tokenId) external view returns (address, address) {
|
||||
TokenMap memory _data = qiTokenMapping[_tokenId];
|
||||
return (_data.token, _data.qitoken);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
contract InstaBenqiMappingAvalanche is Helpers {
|
||||
string constant public name = "Benqi-Mapping-v1.0";
|
||||
|
||||
constructor(
|
||||
string[] memory _qitokenNames,
|
||||
address[] memory _tokens,
|
||||
address[] memory _qitokens
|
||||
) {
|
||||
_addQitokenMapping(_qitokenNames, _tokens, _qitokens);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user