Skip to content

Commit

Permalink
Merge pull request #136 from marginswap/werg-interest-handling
Browse files Browse the repository at this point in the history
bettter interest handling when extinguishing
  • Loading branch information
werg committed May 3, 2021
2 parents 76cdc83 + 5284ee8 commit b009658
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
3 changes: 0 additions & 3 deletions contracts/CrossMarginAccounts.sol
Expand Up @@ -36,8 +36,6 @@ abstract contract CrossMarginAccounts is RoleAware, PriceAware {
mapping(address => CrossMarginAccount) internal marginAccounts;
/// @dev total token caps
mapping(address => uint256) public tokenCaps;
/// @dev tracks total of short positions per token
mapping(address => uint256) public totalShort;
/// @dev tracks total of long positions per token
mapping(address => uint256) public totalLong;
uint256 public coolingOffPeriod = 10;
Expand Down Expand Up @@ -285,7 +283,6 @@ abstract contract CrossMarginAccounts is RoleAware, PriceAware {
uint256 len = account.borrowTokens.length;
for (uint256 borrowIdx; len > borrowIdx; borrowIdx++) {
address borrowToken = account.borrowTokens[borrowIdx];
totalShort[borrowToken] -= account.borrowed[borrowToken];
account.borrowed[borrowToken] = 0;
account.borrowedYieldQuotientsFP[borrowToken] = 0;
}
Expand Down
35 changes: 22 additions & 13 deletions contracts/CrossMarginTrading.sol
Expand Up @@ -57,10 +57,18 @@ contract CrossMarginTrading is CrossMarginLiquidation, IMarginTrading {
CrossMarginAccount storage account = marginAccounts[trader];
account.lastDepositBlock = block.number;

if (account.borrowed[token] > 0) {
extinguishableDebt = min(depositAmount, account.borrowed[token]);
uint256 currentBorrowed = account.borrowed[token];
if (currentBorrowed > 0) {
(uint256 borrowAmount, uint256 yieldQuotientFP) =
Lending(lending()).applyBorrowInterest(
currentBorrowed,
token,
account.borrowedYieldQuotientsFP[token]
);
account.borrowed[token] = borrowAmount;
account.borrowedYieldQuotientsFP[token] = yieldQuotientFP;
extinguishableDebt = min(depositAmount, borrowAmount);
extinguishDebt(account, token, extinguishableDebt);
totalShort[token] -= extinguishableDebt;
}

// no overflow because depositAmount >= extinguishableDebt
Expand Down Expand Up @@ -99,11 +107,9 @@ contract CrossMarginTrading is CrossMarginLiquidation, IMarginTrading {
address borrowToken,
uint256 borrowAmount
) internal {
totalShort[borrowToken] += borrowAmount;
totalLong[borrowToken] += borrowAmount;
require(
tokenCaps[borrowToken] >= totalShort[borrowToken] &&
tokenCaps[borrowToken] >= totalLong[borrowToken],
tokenCaps[borrowToken] >= totalLong[borrowToken],
"Exceeds global token cap"
);

Expand Down Expand Up @@ -175,9 +181,17 @@ contract CrossMarginTrading is CrossMarginLiquidation, IMarginTrading {
CrossMarginAccount storage account = marginAccounts[trader];

if (account.borrowed[tokenTo] > 0) {
extinguishableDebt = min(outAmount, account.borrowed[tokenTo]);
(uint256 extantBorrow, uint256 yieldQuotientFP) =
Lending(lending()).applyBorrowInterest(
account.borrowed[tokenTo],
tokenTo,
account.borrowedYieldQuotientsFP[tokenTo]
);
account.borrowed[tokenTo] = extantBorrow;
account.borrowedYieldQuotientsFP[tokenTo] = yieldQuotientFP;

extinguishableDebt = min(outAmount, extantBorrow);
extinguishDebt(account, tokenTo, extinguishableDebt);
totalShort[tokenTo] -= extinguishableDebt;
}

uint256 sellAmount = inAmount;
Expand Down Expand Up @@ -208,11 +222,6 @@ contract CrossMarginTrading is CrossMarginLiquidation, IMarginTrading {
);

if (borrowAmount > 0) {
totalShort[tokenFrom] += borrowAmount;
require(
tokenCaps[tokenFrom] >= totalShort[tokenFrom],
"Exceeds global token cap"
);
borrow(account, tokenFrom, borrowAmount);
}
}
Expand Down
6 changes: 3 additions & 3 deletions contracts/PriceAware.sol
Expand Up @@ -39,8 +39,8 @@ struct PairPrice {
/// of front-running and other price manipulation.
abstract contract PriceAware is RoleAware {
uint256 constant FP112 = 2**112;
uint256 constant FP8 = 2 ** 8;
uint256 constant FP96 = 2 ** (112 - 2 * 8);
uint256 constant FP8 = 2**8;
uint256 constant FP96 = 2**(112 - 2 * 8);

address public immutable peg;

Expand Down Expand Up @@ -292,7 +292,7 @@ abstract contract PriceAware is RoleAware {
}

function scaleMul(uint256 a, uint256 b) internal pure returns (uint256) {
return (a / FP8) * (b / FP8) / FP96;
return ((a / FP8) * (b / FP8)) / FP96;
}

function initPairPrice(
Expand Down

0 comments on commit b009658

Please sign in to comment.