Update mapping

This commit is contained in:
Mubaris NK 2021-03-17 12:56:51 +05:30
parent 579b081d79
commit 5c460884b7
No known key found for this signature in database
GPG Key ID: 9AC09AD0F8D68561

View File

@ -40,7 +40,7 @@ interface ComptrollerInterface {
}
interface InstaMappingV2 {
function cTokenMapping(string calldata) external view returns (address);
function getMapping(string calldata tokenId) external view returns (address, address);
}
interface MemoryInterface {
@ -172,14 +172,14 @@ contract BasicResolver is CompoundHelpers {
/**
* @dev Deposit ETH/ERC20_Token.
* @param token token address to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param tokenId token id of the token to deposit.(For eg: ETH-A)
* @param amt token amount to deposit.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function deposit(address token, string calldata tokenId, uint amt, uint getId, uint setId) external payable{
function deposit(string calldata tokenId, uint amt, uint getId, uint setId) external payable{
uint _amt = getUint(getId, amt);
address cToken = InstaMappingV2(getMappingAddr()).cTokenMapping(tokenId);
(address token, address cToken) = InstaMappingV2(getMappingAddr()).getMapping(tokenId);
enterMarket(cToken);
if (token == getAddressETH()) {
_amt = _amt == uint(-1) ? address(this).balance : _amt;
@ -197,14 +197,14 @@ contract BasicResolver is CompoundHelpers {
/**
* @dev Withdraw ETH/ERC20_Token.
* @param token token address to withdraw.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param tokenId token id of the token to withdraw.(For eg: ETH-A)
* @param amt token amount to withdraw.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function withdraw(address token, string calldata tokenId, uint amt, uint getId, uint setId) external payable{
function withdraw(string calldata tokenId, uint amt, uint getId, uint setId) external payable{
uint _amt = getUint(getId, amt);
address cToken = InstaMappingV2(getMappingAddr()).cTokenMapping(tokenId);
(address token, address cToken) = InstaMappingV2(getMappingAddr()).getMapping(tokenId);
CTokenInterface cTokenContract = CTokenInterface(cToken);
if (_amt == uint(-1)) {
TokenInterface tokenContract = TokenInterface(token);
@ -222,14 +222,14 @@ contract BasicResolver is CompoundHelpers {
/**
* @dev Borrow ETH/ERC20_Token.
* @param token token address to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param tokenId token id of the token to borrow.(For eg: DAI-A)
* @param amt token amount to borrow.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function borrow(address token, string calldata tokenId, uint amt, uint getId, uint setId) external payable {
function borrow(string calldata tokenId, uint amt, uint getId, uint setId) external payable {
uint _amt = getUint(getId, amt);
address cToken = InstaMappingV2(getMappingAddr()).cTokenMapping(tokenId);
(address token, address cToken) = InstaMappingV2(getMappingAddr()).getMapping(tokenId);
enterMarket(cToken);
require(CTokenInterface(cToken).borrow(_amt) == 0, "borrow-failed");
setUint(setId, _amt);
@ -239,14 +239,14 @@ contract BasicResolver is CompoundHelpers {
/**
* @dev Payback borrowed ETH/ERC20_Token.
* @param token token address to payback.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param tokenId token id of the token to payback.(For eg: COMP-A)
* @param amt token amount to payback.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function payback(address token, string calldata tokenId, uint amt, uint getId, uint setId) external payable {
function payback(string calldata tokenId, uint amt, uint getId, uint setId) external payable {
uint _amt = getUint(getId, amt);
address cToken = InstaMappingV2(getMappingAddr()).cTokenMapping(tokenId);
(address token, address cToken) = InstaMappingV2(getMappingAddr()).getMapping(tokenId);
CTokenInterface cTokenContract = CTokenInterface(cToken);
_amt = _amt == uint(-1) ? cTokenContract.borrowBalanceCurrent(address(this)) : _amt;
@ -295,24 +295,22 @@ contract ExtraResolver is BasicResolver {
);
struct LiquidateData {
address borrower;
address tokenToPay;
string tokenPayId;
address tokenInReturn;
string tokenReturnId;
uint amt;
}
address cTokenPay;
address cTokenColl;
}
/**
* @dev Deposit ETH/ERC20_Token.
* @param token token address to depositCToken.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param tokenId token id of the token to depositCToken.(For eg: DAI-A)
* @param amt token amount to depositCToken.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set ctoken amount at this ID in `InstaMemory` Contract.
*/
function depositCToken(address token, string calldata tokenId, uint amt, uint getId, uint setId) external payable{
function depositCToken(string calldata tokenId, uint amt, uint getId, uint setId) external payable{
uint _amt = getUint(getId, amt);
address cToken = InstaMappingV2(getMappingAddr()).cTokenMapping(tokenId);
(address token, address cToken) = InstaMappingV2(getMappingAddr()).getMapping(tokenId);
enterMarket(cToken);
CTokenInterface ctokenContract = CTokenInterface(cToken);
@ -337,23 +335,28 @@ contract ExtraResolver is BasicResolver {
/**
* @dev Withdraw CETH/CERC20_Token using cToken Amt.
* @param token token address to withdraw CToken.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param tokenId token id of the token to withdraw CToken.(For eg: ETH-A)
* @param cTokenAmt ctoken amount to withdrawCToken.
* @param getId Get ctoken amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function withdrawCToken(address token, string calldata tokenId, uint cTokenAmt, uint getId, uint setId) external payable {
function withdrawCToken(string calldata tokenId, uint cTokenAmt, uint getId, uint setId) external payable {
uint _cAmt = getUint(getId, cTokenAmt);
address cToken = InstaMappingV2(getMappingAddr()).cTokenMapping(tokenId);
(address token, address cToken) = InstaMappingV2(getMappingAddr()).getMapping(tokenId);
CTokenInterface cTokenContract = CTokenInterface(cToken);
TokenInterface tokenContract = TokenInterface(token);
_cAmt = _cAmt == uint(-1) ? cTokenContract.balanceOf(address(this)) : _cAmt;
uint initialBal = token != getAddressETH() ? tokenContract.balanceOf(address(this)) : address(this).balance;
require(cTokenContract.redeem(_cAmt) == 0, "redeem-failed");
uint finalBal = token != getAddressETH() ? tokenContract.balanceOf(address(this)) : address(this).balance;
uint withdrawAmt;
{
uint initialBal = token != getAddressETH() ? tokenContract.balanceOf(address(this)) : address(this).balance;
require(cTokenContract.redeem(_cAmt) == 0, "redeem-failed");
uint finalBal = token != getAddressETH() ? tokenContract.balanceOf(address(this)) : address(this).balance;
withdrawAmt = sub(finalBal, initialBal);
}
uint withdrawAmt = sub(finalBal, initialBal);
setUint(setId, withdrawAmt);
emit LogWithdrawCToken(token, tokenId, cToken, withdrawAmt, _cAmt, getId, setId);
@ -361,34 +364,44 @@ contract ExtraResolver is BasicResolver {
/**
* @dev Liquidate a position.
* @param data Liquidation data.
* @param borrower Borrower's Address.
* @param tokenIdToPay token id of the token to pay for liquidation.(For eg: ETH-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 Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function liquidate(
LiquidateData calldata data,
address borrower,
string calldata tokenIdToPay,
string calldata tokenIdInReturn,
uint amt,
uint getId,
uint setId
) external payable {
uint _amt = getUint(getId, data.amt);
address cTokenPay = InstaMappingV2(getMappingAddr()).cTokenMapping(data.tokenPayId);
address cTokenColl = InstaMappingV2(getMappingAddr()).cTokenMapping(data.tokenReturnId);
CTokenInterface cTokenContract = CTokenInterface(cTokenPay);
) external payable
{
uint _amt = getUint(getId, amt);
LiquidateData memory data;
(data.tokenToPay, data.cTokenPay) = InstaMappingV2(getMappingAddr()).getMapping(tokenIdToPay);
(data.tokenInReturn, data.cTokenColl) = InstaMappingV2(getMappingAddr()).getMapping(tokenIdInReturn);
CTokenInterface cTokenContract = CTokenInterface(data.cTokenPay);
{
(,, uint shortfal) = ComptrollerInterface(getComptrollerAddress()).getAccountLiquidity(data.borrower);
(,, uint shortfal) = ComptrollerInterface(getComptrollerAddress()).getAccountLiquidity(borrower);
require(shortfal != 0, "account-cannot-be-liquidated");
}
_amt = _amt == uint(-1) ? cTokenContract.borrowBalanceCurrent(data.borrower) : _amt;
_amt = _amt == uint(-1) ? cTokenContract.borrowBalanceCurrent(borrower) : _amt;
if (data.tokenToPay == getAddressETH()) {
require(address(this).balance >= _amt, "not-enought-eth");
CETHInterface(cTokenPay).liquidateBorrow.value(_amt)(data.borrower, cTokenColl);
CETHInterface(data.cTokenPay).liquidateBorrow.value(_amt)(borrower, data.cTokenColl);
} else {
TokenInterface tokenContract = TokenInterface(data.tokenToPay);
require(tokenContract.balanceOf(address(this)) >= _amt, "not-enough-token");
tokenContract.approve(cTokenPay, _amt);
require(cTokenContract.liquidateBorrow(data.borrower, _amt, cTokenColl) == 0, "liquidate-failed");
tokenContract.approve(data.cTokenPay, _amt);
require(cTokenContract.liquidateBorrow(borrower, _amt, data.cTokenColl) == 0, "liquidate-failed");
}
setUint(setId, _amt);