Skip to content

Commit

Permalink
modify _setCreditlimit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ibsunhub committed Aug 22, 2022
1 parent a06c995 commit 66ca604
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
33 changes: 24 additions & 9 deletions contracts/Comptroller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,7 @@ contract Comptroller is ComptrollerV1Storage, ComptrollerInterface, ComptrollerE

/**
* @notice Sets protocol's credit limit by market
* @dev Setting credit limit to 0 would change the protocol to a normal account
* @param protocol The address of the protocol
* @param market The market
* @param creditLimit The credit limit
Expand All @@ -1304,16 +1305,30 @@ contract Comptroller is ComptrollerV1Storage, ComptrollerInterface, ComptrollerE
address market,
uint256 creditLimit
) public {
require(
msg.sender == admin || msg.sender == creditLimitManager || msg.sender == pauseGuardian,
"admin or credit limit manager or pause guardian only"
);
require(isMarketListed(market), "market not listed");
require(msg.sender == admin || msg.sender == creditLimitManager, "admin or credit limit manager only");

if (_creditLimits[protocol][market] == 0 && creditLimit != 0) {
// Only admin or credit limit manager could set a new credit limit.
require(msg.sender == admin || msg.sender == creditLimitManager, "admin or credit limit manager only");
}
_setCreditLimitInternal(protocol, market, creditLimit);
}

/**
* @notice Pause protocol's credit limit by market
* @param protocol The address of the protocol
* @param market The market
*/
function _pauseCreditLimit(address protocol, address market) public {
require(msg.sender == pauseGuardian, "pause guardian only");

// We set the credit limit to a very small amount (1 Wei) to avoid the protocol becoming a normal account.
// Normal account could be liquidated or repaid, which might cause some additional problem.
_setCreditLimitInternal(protocol, market, 1);
}

function _setCreditLimitInternal(
address protocol,
address market,
uint256 creditLimit
) internal {
require(isMarketListed(market), "market not listed");

_creditLimits[protocol][market] = creditLimit;
emit CreditLimitChanged(protocol, market, creditLimit);
Expand Down
43 changes: 41 additions & 2 deletions tests/Comptroller/comptrollerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ describe('Comptroller', () => {

it("fails if not called by admin", async () => {
const cToken = await makeCToken({supportMarket: true});
await expect(send(cToken.comptroller, '_setCreditLimit', [accounts[0], cToken._address, creditLimit], {from: accounts[1]})).rejects.toRevert("revert admin or credit limit manager or pause guardian only");
await expect(send(cToken.comptroller, '_setCreditLimit', [accounts[0], cToken._address, creditLimit], {from: accounts[1]})).rejects.toRevert("revert admin or credit limit manager only");
});

it("fails if set new credit limit by guardian", async () => {
it("fails if called by guardian", async () => {
const cToken = await makeCToken({supportMarket: true});
await send(cToken.comptroller, '_setPauseGuardian', [accounts[0]]);

Expand Down Expand Up @@ -285,6 +285,45 @@ describe('Comptroller', () => {
// Credit limit manager clears the limit.
const result3 = await send(cToken.comptroller, '_setCreditLimit', [accounts[0], cToken._address, 0], {from: accounts[0]});
expect(result3).toHaveLog('CreditLimitChanged', {protocol: accounts[0], market: cToken._address, creditLimit: 0});

const isCreditAccount = await call(cToken.comptroller, 'isCreditAccount', [accounts[0], cToken._address]);
expect(isCreditAccount).toBeFalsy();
});
});

describe('_pauseCreditLimit', () => {
const creditLimit = etherMantissa(500);

it("fails if not called by guardian", async () => {
const cToken = await makeCToken({supportMarket: true});
await send(cToken.comptroller, '_setPauseGuardian', [accounts[0]]);

await expect(send(cToken.comptroller, '_pauseCreditLimit', [accounts[0], cToken._address], {from: accounts[1]})).rejects.toRevert("revert pause guardian only");
});

it("fails for invalid market", async () => {
const cToken = await makeCToken();
await send(cToken.comptroller, '_setPauseGuardian', [accounts[0]]);

await expect(send(cToken.comptroller, '_pauseCreditLimit', [accounts[0], cToken._address], {from: accounts[0]})).rejects.toRevert("revert market not listed");
});

it("succeeds and pauses credit limit", async () => {
const cToken = await makeCToken({supportMarket: true});
await send(cToken.comptroller, '_setPauseGuardian', [accounts[0]]);

const result1 = await send(cToken.comptroller, '_setCreditLimit', [accounts[0], cToken._address, creditLimit]);
expect(result1).toHaveLog('CreditLimitChanged', {protocol: accounts[0], market: cToken._address, creditLimit: creditLimit.toString()});

const _creditLimit = await call(cToken.comptroller, 'creditLimits', [accounts[0], cToken._address]);
expect(_creditLimit).toEqual(creditLimit.toString());

// Pause guardian pauses the limit.
const result3 = await send(cToken.comptroller, '_pauseCreditLimit', [accounts[0], cToken._address], {from: accounts[0]});
expect(result3).toHaveLog('CreditLimitChanged', {protocol: accounts[0], market: cToken._address, creditLimit: 1}); // 1 Wei

const isCreditAccount = await call(cToken.comptroller, 'isCreditAccount', [accounts[0], cToken._address]);
expect(isCreditAccount).toBeTruthy(); // still a credit account
});
});

Expand Down

0 comments on commit 66ca604

Please sign in to comment.