Fixing build

This commit is contained in:
Tianjie Wei 2021-11-29 16:55:26 -08:00
parent 25eadd9920
commit 0cf73a7258
2 changed files with 57 additions and 66 deletions

View File

@ -12,7 +12,7 @@ contract Helpers is Basic {
int256 private constant _INT256_MIN = type(int256).min; int256 private constant _INT256_MIN = type(int256).min;
NotionalInterface internal constant notional = NotionalInterface internal constant notional =
NotionalInterface(0x1344a36a1b56144c3bc62e7757377d288fde0369); NotionalInterface(0x1344A36A1B56144C3Bc62E7757377D288fDE0369);
function getUnderlyingToken(uint16 currencyId) internal returns (address) { function getUnderlyingToken(uint16 currencyId) internal returns (address) {
(, Token memory underlyingToken) = notional.getCurrency(currencyId); (, Token memory underlyingToken) = notional.getCurrency(currencyId);
@ -46,7 +46,6 @@ contract Helpers is Basic {
function convertToInternal(uint16 currencyId, int256 amount) function convertToInternal(uint16 currencyId, int256 amount)
internal internal
pure
returns (int256) returns (int256)
{ {
// If token decimals is greater than INTERNAL_TOKEN_PRECISION then this will truncate // If token decimals is greater than INTERNAL_TOKEN_PRECISION then this will truncate
@ -64,13 +63,10 @@ contract Helpers is Basic {
uint32 minLendRate uint32 minLendRate
) internal returns (bytes32) { ) internal returns (bytes32) {
return return
abi.encodePacked( (bytes32(uint256(LEND_TRADE)) << 248) |
LEND_TRADE, (bytes32(uint256(marketIndex)) << 240) |
marketIndex, (bytes32(uint256(fCashAmount)) << 152) |
fCashAmount, (bytes32(uint256(minLendRate)) << 120);
minLendRate,
uint120(0)
);
} }
function encodeBorrowTrade( function encodeBorrowTrade(
@ -79,13 +75,10 @@ contract Helpers is Basic {
uint32 maxBorrowRate uint32 maxBorrowRate
) internal returns (bytes32) { ) internal returns (bytes32) {
return return
abi.encodePacked( (bytes32(uint256(BORROW_TRADE)) << 248) |
BORROW_TRADE, (bytes32(uint256(marketIndex)) << 240) |
marketIndex, (bytes32(uint256(fCashAmount)) << 152) |
fCashAmount, (bytes32(uint256(maxBorrowRate)) << 120);
maxBorrowRate,
uint120(0)
);
} }
function mul(int256 a, int256 b) internal pure returns (int256 c) { function mul(int256 a, int256 b) internal pure returns (int256 c) {

View File

@ -5,7 +5,6 @@ import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol"; import { Events } from "./events.sol";
import { DepositActionType, BalanceActionWithTrades, BalanceAction } from "./interface.sol"; import { DepositActionType, BalanceActionWithTrades, BalanceAction } from "./interface.sol";
import { TokenInterface } from "../../common/interfaces.sol"; import { TokenInterface } from "../../common/interfaces.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
/** /**
* @title Notional * @title Notional
@ -32,9 +31,9 @@ abstract contract NotionalResolver is Events, Helpers {
uint assetCashDeposited; uint assetCashDeposited;
address tokenAddress = useUnderlying ? getUnderlyingToken(currencyId) : getAssetToken(currencyId); address tokenAddress = useUnderlying ? getUnderlyingToken(currencyId) : getAssetToken(currencyId);
depositAmount = getUint(getId, depositAmount); depositAmount = getUint(getId, depositAmount);
if (depositAmount == uint(-1)) depositAmount = IERC20(tokenAddress).balanceOf(address(this)); if (depositAmount == uint(-1)) depositAmount = TokenInterface(tokenAddress).balanceOf(address(this));
approve(tokenAddress, address(notional), depositAmount); approve(TokenInterface(tokenAddress), address(notional), depositAmount);
if (useUnderlying && currencyId == ETH_CURRENCY_ID) { if (useUnderlying && currencyId == ETH_CURRENCY_ID) {
assetCashDeposited = notional.depositUnderlyingToken{value: depositAmount}(address(this), currencyId, depositAmount); assetCashDeposited = notional.depositUnderlyingToken{value: depositAmount}(address(this), currencyId, depositAmount);
@ -68,9 +67,9 @@ abstract contract NotionalResolver is Events, Helpers {
uint setId uint setId
) external returns (string memory _eventName, bytes memory _eventParam) { ) external returns (string memory _eventName, bytes memory _eventParam) {
withdrawAmount = getUint(getId, withdrawAmount); withdrawAmount = getUint(getId, withdrawAmount);
uint amountInternalPrecision = withdrawAmount == uint(-1) ? uint88 amountInternalPrecision = withdrawAmount == uint(-1) ?
getCashBalance(currencyId) : uint88(getCashBalance(currencyId)) :
convertToInternal(currencyId, withdrawAmount); uint88(convertToInternal(currencyId, int256(withdrawAmount)));
uint amountWithdrawn = notional.withdraw(currencyId, amountInternalPrecision, redeemToUnderlying); uint amountWithdrawn = notional.withdraw(currencyId, amountInternalPrecision, redeemToUnderlying);
// Sets the amount of tokens withdrawn to address(this) // Sets the amount of tokens withdrawn to address(this)
@ -108,12 +107,12 @@ abstract contract NotionalResolver is Events, Helpers {
function redeemNTokenRaw( function redeemNTokenRaw(
uint16 currencyId, uint16 currencyId,
bool sellTokenAssets, bool sellTokenAssets,
uint tokensToRedeem, uint96 tokensToRedeem,
uint getId, uint getId,
uint setId uint setId
) external returns (string memory _eventName, bytes memory _eventParam) { ) external returns (string memory _eventName, bytes memory _eventParam) {
tokensToRedeem = getUint(getId, tokensToRedeem); tokensToRedeem = uint96(getUint(getId, tokensToRedeem));
if (tokensToRedeem == uint(-1)) tokensToRedeem = getNTokenBalance(currencyId); if (tokensToRedeem == uint96(-1)) tokensToRedeem = uint96(getNTokenBalance(currencyId));
int _assetCashChange = notional.nTokenRedeem(address(this), currencyId, tokensToRedeem, sellTokenAssets); int _assetCashChange = notional.nTokenRedeem(address(this), currencyId, tokensToRedeem, sellTokenAssets);
// Floor asset cash change at zero in order to properly set the uint. If the asset cash change is negative // Floor asset cash change at zero in order to properly set the uint. If the asset cash change is negative
@ -147,9 +146,9 @@ abstract contract NotionalResolver is Events, Helpers {
uint setId uint setId
) external returns (string memory _eventName, bytes memory _eventParam) { ) external returns (string memory _eventName, bytes memory _eventParam) {
tokensToRedeem = getUint(getId, tokensToRedeem); tokensToRedeem = getUint(getId, tokensToRedeem);
if (tokensToRedeem == uint(-1)) tokensToRedeem = getNTokenBalance(currencyId); if (tokensToRedeem == uint(-1)) tokensToRedeem = uint(getNTokenBalance(currencyId));
BalanceAction[] memory action = new BalanceAction[1]; BalanceAction[] memory action = new BalanceAction[](1);
action[0].actionType = DepositActionType.RedeemNToken; action[0].actionType = DepositActionType.RedeemNToken;
action[0].currencyId = currencyId; action[0].currencyId = currencyId;
action[0].depositActionAmount = tokensToRedeem; action[0].depositActionAmount = tokensToRedeem;
@ -169,14 +168,14 @@ abstract contract NotionalResolver is Events, Helpers {
getAssetToken(currencyId); getAssetToken(currencyId);
// TODO: handle ETH // TODO: handle ETH
balanceBefore = IERC20(tokenAddress).balanceOf(address(this)); balanceBefore = TokenInterface(tokenAddress).balanceOf(address(this));
} }
notional.batchBalanceAction(address(this), action); notional.batchBalanceAction(address(this), action);
if (setId != 0) { if (setId != 0) {
// TODO: handle ETH // TODO: handle ETH
uint netBalance = sub(balanceBefore, IERC20(tokenAddress).balanceOf(address(this))); uint netBalance = sub(balanceBefore, TokenInterface(tokenAddress).balanceOf(address(this)));
// This can be used to determine the exact amount withdrawn // This can be used to determine the exact amount withdrawn
setUint(setId, netBalance); setUint(setId, netBalance);
} }
@ -200,17 +199,17 @@ abstract contract NotionalResolver is Events, Helpers {
*/ */
function redeemNTokenAndDeleverage( function redeemNTokenAndDeleverage(
uint16 currencyId, uint16 currencyId,
uint tokensToRedeem, uint96 tokensToRedeem,
uint8 marketIndex, uint8 marketIndex,
uint fCashAmount, uint88 fCashAmount,
uint32 minLendRate, uint32 minLendRate,
uint getId uint getId
) external returns (string memory _eventName, bytes memory _eventParam) { ) external returns (string memory _eventName, bytes memory _eventParam) {
tokensToRedeem = getUint(getId, tokensToRedeem); tokensToRedeem = uint96(getUint(getId, tokensToRedeem));
if (tokensToRedeem == uint(-1)) tokensToRedeem = getNTokenBalance(currencyId); if (tokensToRedeem == uint96(-1)) tokensToRedeem = uint96(getNTokenBalance(currencyId));
notional.nTokenRedeem(currencyId, tokensToRedeem, true); notional.nTokenRedeem(address(this), currencyId, tokensToRedeem, true);
BalanceActionWithTrades[] memory action = new BalanceActionWithTrades[1]; BalanceActionWithTrades[] memory action = new BalanceActionWithTrades[](1);
action[0].actionType = DepositActionType.RedeemNToken; action[0].actionType = DepositActionType.RedeemNToken;
action[0].currencyId = currencyId; action[0].currencyId = currencyId;
action[0].depositActionAmount = tokensToRedeem; action[0].depositActionAmount = tokensToRedeem;
@ -245,26 +244,26 @@ abstract contract NotionalResolver is Events, Helpers {
) external payable returns (string memory _eventName, bytes memory _eventParam) { ) external payable returns (string memory _eventName, bytes memory _eventParam) {
address tokenAddress = useUnderlying ? getUnderlyingToken(currencyId) : getAssetToken(currencyId); address tokenAddress = useUnderlying ? getUnderlyingToken(currencyId) : getAssetToken(currencyId);
depositAmount = getUint(getId, depositAmount); depositAmount = getUint(getId, depositAmount);
if (depositAmount == uint(-1)) depositAmount = IERC20(tokenAddress).balanceOf(address(this)); if (depositAmount == uint(-1)) depositAmount = TokenInterface(tokenAddress).balanceOf(address(this));
approve(tokenAddress, address(notional), depositAmount); approve(TokenInterface(tokenAddress), address(notional), depositAmount);
BalanceAction[] memory action = new BalanceAction[1]; BalanceAction[] memory action = new BalanceAction[](1);
action[0].actionType = useUnderlying ? DepositActionType.DepositUnderlyingAndMintNToken : DepositActionType.DepositAssetAndMintNToken; action[0].actionType = useUnderlying ? DepositActionType.DepositUnderlyingAndMintNToken : DepositActionType.DepositAssetAndMintNToken;
action[0].currencyId = currencyId; action[0].currencyId = currencyId;
action[0].depositActionAmount = depositAmount; action[0].depositActionAmount = depositAmount;
// withdraw amount, withdraw cash and redeem to underlying are all 0 and false // withdraw amount, withdraw cash and redeem to underlying are all 0 and false
uint nTokenBefore; int256 nTokenBefore;
if (setId != 0) { if (setId != 0) {
nTokenBefore = getNTokenBalance(currencyId); nTokenBefore = getNTokenBalance(currencyId);
} }
uint msgValue = currencyId == ETH_CURRENCY_ID ? depositAmount : 0; uint msgValue = currencyId == ETH_CURRENCY_ID ? depositAmount : 0;
notional.batchBalanceAndTradeAction{value: msgValue}(address(this), action); notional.batchBalanceAction{value: msgValue}(address(this), action);
if (setId != 0) { if (setId != 0) {
// Set the amount of nTokens minted // Set the amount of nTokens minted
setUint(setId, sub(getNTokenBalance(currencyId), nTokenBefore)); setUint(setId, uint(sub(getNTokenBalance(currencyId), nTokenBefore)));
} }
// todo: events // todo: events
@ -277,24 +276,24 @@ abstract contract NotionalResolver is Events, Helpers {
uint setId uint setId
) external payable returns (string memory _eventName, bytes memory _eventParam) { ) external payable returns (string memory _eventName, bytes memory _eventParam) {
cashBalanceToMint = getUint(getId, cashBalanceToMint); cashBalanceToMint = getUint(getId, cashBalanceToMint);
if (cashBalanceToMint == uint(-1)) cashBalanceToMint = getCashBalance(currencyId); if (cashBalanceToMint == uint(-1)) cashBalanceToMint = uint(getCashBalance(currencyId));
BalanceAction[] memory action = new BalanceAction[1]; BalanceAction[] memory action = new BalanceAction[](1);
action[0].actionType = DepositActionType.ConvertCashToNToken; action[0].actionType = DepositActionType.ConvertCashToNToken;
action[0].currencyId = currencyId; action[0].currencyId = currencyId;
action[0].depositActionAmount = cashBalanceToMint; action[0].depositActionAmount = cashBalanceToMint;
// NOTE: withdraw amount, withdraw cash and redeem to underlying are all 0 and false // NOTE: withdraw amount, withdraw cash and redeem to underlying are all 0 and false
uint nTokenBefore; int256 nTokenBefore;
if (setId != 0) { if (setId != 0) {
nTokenBefore = getNTokenBalance(currencyId); nTokenBefore = getNTokenBalance(currencyId);
} }
notional.batchBalanceActionWithTrades(address(this), action); notional.batchBalanceAction(address(this), action);
if (setId != 0) { if (setId != 0) {
// Set the amount of nTokens minted // Set the amount of nTokens minted
setUint(setId, getNTokenBalance(currencyId).sub(nTokenBefore)); setUint(setId, uint(sub(getNTokenBalance(currencyId), nTokenBefore)));
} }
// todo: events // todo: events
@ -305,16 +304,16 @@ abstract contract NotionalResolver is Events, Helpers {
uint depositAmount, uint depositAmount,
bool useUnderlying, bool useUnderlying,
uint8 marketIndex, uint8 marketIndex,
uint fCashAmount, uint88 fCashAmount,
uint minLendRate, uint32 minLendRate,
uint getId uint getId
) external payable returns (string memory _eventName, bytes memory _eventParam) { ) external payable returns (string memory _eventName, bytes memory _eventParam) {
address tokenAddress = useUnderlying ? getUnderlyingToken(currencyId) : getAssetToken(currencyId); address tokenAddress = useUnderlying ? getUnderlyingToken(currencyId) : getAssetToken(currencyId);
depositAmount = getUint(getId, depositAmount); depositAmount = getUint(getId, depositAmount);
if (depositAmount == uint(-1)) depositAmount = IERC20(tokenAddress).balanceOf(address(this)); if (depositAmount == uint(-1)) depositAmount = TokenInterface(tokenAddress).balanceOf(address(this));
approve(tokenAddress, address(notional), depositAmount); approve(TokenInterface(tokenAddress), address(notional), depositAmount);
BalanceAction[] memory action = new BalanceAction[1]; BalanceActionWithTrades[] memory action = new BalanceActionWithTrades[](1);
action[0].actionType = useUnderlying ? DepositActionType.DepositUnderlying : DepositActionType.DepositAsset; action[0].actionType = useUnderlying ? DepositActionType.DepositUnderlying : DepositActionType.DepositAsset;
action[0].currencyId = currencyId; action[0].currencyId = currencyId;
action[0].depositActionAmount = depositAmount; action[0].depositActionAmount = depositAmount;
@ -328,7 +327,7 @@ abstract contract NotionalResolver is Events, Helpers {
action[0].trades = trades; action[0].trades = trades;
uint msgValue = currencyId == ETH_CURRENCY_ID ? depositAmount : 0; uint msgValue = currencyId == ETH_CURRENCY_ID ? depositAmount : 0;
notional.batchBalanceActionWithTrades{value: msgValue}(address(this), action); notional.batchBalanceAndTradeAction{value: msgValue}(address(this), action);
// todo: events // todo: events
} }
@ -339,8 +338,8 @@ abstract contract NotionalResolver is Events, Helpers {
uint depositAmount, uint depositAmount,
uint16 borrowCurrencyId, uint16 borrowCurrencyId,
uint8 marketIndex, uint8 marketIndex,
uint fCashAmount, uint88 fCashAmount,
uint maxBorrowRate, uint32 maxBorrowRate,
bool redeemToUnderlying, bool redeemToUnderlying,
uint getId, uint getId,
uint setId uint setId
@ -348,9 +347,9 @@ abstract contract NotionalResolver is Events, Helpers {
require(depositCurrencyId != borrowCurrencyId); require(depositCurrencyId != borrowCurrencyId);
address tokenAddress = useUnderlying ? getUnderlyingToken(depositCurrencyId) : getAssetToken(depositCurrencyId); address tokenAddress = useUnderlying ? getUnderlyingToken(depositCurrencyId) : getAssetToken(depositCurrencyId);
depositAmount = getUint(getId, depositAmount); depositAmount = getUint(getId, depositAmount);
if (depositAmount == uint(-1)) depositAmount = IERC20(tokenAddress).balanceOf(address(this)); if (depositAmount == uint(-1)) depositAmount = TokenInterface(tokenAddress).balanceOf(address(this));
approve(tokenAddress, address(notional), depositAmount); approve(TokenInterface(tokenAddress), address(notional), depositAmount);
BalanceActionWithTrades[] memory action = new BalanceActionWithTrades[](2); BalanceActionWithTrades[] memory action = new BalanceActionWithTrades[](2);
uint256 depositIndex; uint256 depositIndex;
@ -383,13 +382,13 @@ abstract contract NotionalResolver is Events, Helpers {
uint balanceBefore; uint balanceBefore;
if (setId != 0) { if (setId != 0) {
address borrowToken = useUnderlying ? getUnderlyingToken(borrowCurrencyId) : getAssetToken(borrowCurrencyId); address borrowToken = useUnderlying ? getUnderlyingToken(borrowCurrencyId) : getAssetToken(borrowCurrencyId);
balanceBefore = IERC20(borrowToken).balanceOf(address(this)); balanceBefore = TokenInterface(borrowToken).balanceOf(address(this));
} }
notional.batchBalanceActionWithTrades{value: msgValue}(address(this), action); notional.batchBalanceAndTradeAction{value: msgValue}(address(this), action);
if (setId != 0) { if (setId != 0) {
setUint(setId, IERC20(borrowToken).balanceOf(address(this)).sub(balanceBefore)); setUint(setId, sub(TokenInterface(borrowToken).balanceOf(address(this)), balanceBefore));
} }
// todo: events // todo: events
@ -398,13 +397,13 @@ abstract contract NotionalResolver is Events, Helpers {
function withdrawLend( function withdrawLend(
uint16 currencyId, uint16 currencyId,
uint8 marketIndex, uint8 marketIndex,
uint fCashAmount, uint88 fCashAmount,
uint maxBorrowRate, uint32 maxBorrowRate,
uint getId, uint getId,
uint setId uint setId
) external payable returns (string memory _eventName, bytes memory _eventParam) { ) external payable returns (string memory _eventName, bytes memory _eventParam) {
bool useUnderlying = currencyId != ETH_CURRENCY_ID; bool useUnderlying = currencyId != ETH_CURRENCY_ID;
BalanceActionWithTrades[] memory action = new BalanceActionWithTrades[](); BalanceActionWithTrades[] memory action = new BalanceActionWithTrades[](1);
action[0].actionType = DepositActionType.None; action[0].actionType = DepositActionType.None;
action[0].currencyId = currencyId; action[0].currencyId = currencyId;
// Withdraw borrowed amount to wallet // Withdraw borrowed amount to wallet
@ -420,13 +419,12 @@ abstract contract NotionalResolver is Events, Helpers {
uint balanceBefore; uint balanceBefore;
if (setId != 0) { if (setId != 0) {
address tokenAddress = useUnderlying ? getUnderlyingToken(currencyId) : getAssetToken(currencyId); address tokenAddress = useUnderlying ? getUnderlyingToken(currencyId) : getAssetToken(currencyId);
balanceBefore = IERC20(tokenAddress).balanceOf(address(this)); balanceBefore = TokenInterface(tokenAddress).balanceOf(address(this));
} }
notional.batchBalanceAndTradeAction(address(this), action);
notional.batchBalanceActionWithTrades{value: msg.value}(address(this), action);
if (setId != 0) { if (setId != 0) {
setUint(setId, IERC20(tokenAddress).balanceOf(address(this)).sub(balanceBefore)); setUint(setId, sub(TokenInterface(tokenAddress).balanceOf(address(this)), balanceBefore));
} }
} }