Skip to content

Commit

Permalink
Add core tests to SavingsContract
Browse files Browse the repository at this point in the history
  • Loading branch information
superduck35 committed Jan 2, 2021
1 parent b0cafc2 commit ced99c4
Show file tree
Hide file tree
Showing 4 changed files with 714 additions and 268 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ module.exports = {
}
},
"rules": {
"@typescript-eslint/no-use-before-define": 1
"@typescript-eslint/no-use-before-define": 1,
"no-nested-ternary": 0
},
"overrides": [
{
Expand Down
101 changes: 50 additions & 51 deletions contracts/savings/SavingsContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,45 @@ contract SavingsContract is
}


/** @dev Enable or disable the automation of fee collection during deposit process */
function automateInterestCollectionFlag(bool _enabled)
external
onlyGovernor
{
automateInterestCollection = _enabled;
emit AutomaticInterestCollectionSwitched(_enabled);
/***************************************
VIEW - E
****************************************/

/**
* @dev Returns the underlying balance of a given user
* @param _user Address of the user to check
* @return balance Units of underlying owned by the user
*/
function balanceOfUnderlying(address _user) external view returns (uint256 balance) {
(balance,) = _creditsToUnderlying(balanceOf(_user));
}

/**
* @dev Converts a given underlying amount into credits
* @param _underlying Units of underlying
* @return credits Credit units (a.k.a imUSD)
*/
function underlyingToCredits(uint256 _underlying) external view returns (uint256 credits) {
(credits,) = _underlyingToCredits(_underlying);
}

/**
* @dev Converts a given credit amount into underlying
* @param _credits Units of credits
* @return amount Corresponding underlying amount
*/
function creditsToUnderlying(uint256 _credits) external view returns (uint256 amount) {
(amount,) = _creditsToUnderlying(_credits);
}

// Deprecated in favour of `balanceOf(address)`
// Maintained for backwards compatibility
// Returns the credit balance of a given user
function creditBalances(address _user) external view returns (uint256) {
return balanceOf(_user);
}


/***************************************
INTEREST
****************************************/
Expand All @@ -146,14 +176,22 @@ contract SavingsContract is
// new exchange rate is relationship between _totalCredits & totalSavings
// _totalCredits * exchangeRate = totalSavings
// exchangeRate = totalSavings/_totalCredits
uint256 amountPerCredit = _calcExchangeRate(_amount, totalCredits);
uint256 newExchangeRate = exchangeRate.add(amountPerCredit);
(uint256 totalCollat, ) = _creditsToUnderlying(totalCredits);
uint256 newExchangeRate = _calcExchangeRate(totalCollat.add(_amount), totalCredits);
exchangeRate = newExchangeRate;

emit ExchangeRateUpdated(newExchangeRate, _amount);
}
}

/** @dev Enable or disable the automation of fee collection during deposit process */
function automateInterestCollectionFlag(bool _enabled)
external
onlyGovernor
{
automateInterestCollection = _enabled;
emit AutomaticInterestCollectionSwitched(_enabled);
}

/***************************************
DEPOSIT
Expand Down Expand Up @@ -598,45 +636,6 @@ contract SavingsContract is
}


/***************************************
VIEW - E
****************************************/

/**
* @dev Returns the underlying balance of a given user
* @param _user Address of the user to check
* @return balance Units of underlying owned by the user
*/
function balanceOfUnderlying(address _user) external view returns (uint256 balance) {
(balance,) = _creditsToUnderlying(balanceOf(_user));
}

/**
* @dev Converts a given underlying amount into credits
* @param _underlying Units of underlying
* @return credits Credit units (a.k.a imUSD)
*/
function underlyingToCredits(uint256 _underlying) external view returns (uint256 credits) {
(credits,) = _underlyingToCredits(_underlying);
}

/**
* @dev Converts a given credit amount into underlying
* @param _credits Units of credits
* @return amount Corresponding underlying amount
*/
function creditsToUnderlying(uint256 _credits) external view returns (uint256 amount) {
(amount,) = _creditsToUnderlying(_credits);
}

// Deprecated in favour of `balanceOf(address)`
// Maintained for backwards compatibility
// Returns the credit balance of a given user
function creditBalances(address _user) external view returns (uint256) {
return balanceOf(_user);
}


/***************************************
VIEW - I
****************************************/
Expand All @@ -661,7 +660,7 @@ contract SavingsContract is

/**
* @dev Converts masset amount into credits based on exchange rate
* c = masset / exchangeRate
* c = (masset / exchangeRate) + 1
*/
function _underlyingToCredits(uint256 _underlying)
internal
Expand All @@ -677,14 +676,14 @@ contract SavingsContract is

/**
* @dev Works out a new exchange rate, given an amount of collateral and total credits
* e = underlying / credits
* e = underlying / (credits-1)
*/
function _calcExchangeRate(uint256 _totalCollateral, uint256 _totalCredits)
internal
pure
returns (uint256 _exchangeRate)
{
return _totalCollateral.divPrecisely(_totalCredits);
return _totalCollateral.divPrecisely(_totalCredits.sub(1));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test-utils/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const assertBnGte = (actual: BN, comparison: BN): void => {
export const assertBNSlightlyGT = (
actual: BN,
equator: BN,
maxActualShouldExceedExpected = new BN(100),
maxActualShouldExceedExpected: number | BN = new BN(100),
mustBeGreater = false,
): void => {
const actualDelta = actual.gt(equator) ? actual.sub(equator) : equator.sub(actual);
Expand All @@ -81,7 +81,7 @@ export const assertBNSlightlyGT = (
`Actual value should be greater than the expected value`,
);
assert.ok(
actual.lte(equator.add(maxActualShouldExceedExpected)),
actual.lte(equator.add(new BN(maxActualShouldExceedExpected))),
`Actual value should not exceed ${maxActualShouldExceedExpected.toString()} units greater than expected. Variance was ${actualDelta.toString()}`,
);
};
Expand Down
Loading

0 comments on commit ced99c4

Please sign in to comment.