Skip to content

Commit

Permalink
back to checks on every loan operation
Browse files Browse the repository at this point in the history
  • Loading branch information
computerphysicslab committed May 17, 2021
1 parent bc78a4c commit 227cfbf
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 24 deletions.
12 changes: 12 additions & 0 deletions contracts/connectors/loantoken/LoanTokenLogicStandard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
nonReentrant
returns (bytes memory)
{
/// @dev bZx function level pause
_checkPause();
require(borrowAmount != 0, "38");
_settleInterest();
Expand Down Expand Up @@ -134,6 +137,9 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
uint256 // returns new principal and new collateral added to loan
)
{
/// @dev bZx function level pause
_checkPause();

require(withdrawAmount != 0, "6");

//temporary: limit transaction size
Expand Down Expand Up @@ -204,6 +210,9 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
uint256 // returns new principal and new collateral added to trade
)
{
/// @dev bZx function level pause
_checkPause();

if (collateralTokenAddress == address(0)) {
collateralTokenAddress = wrbtcTokenAddress;
}
Expand Down Expand Up @@ -659,6 +668,9 @@ contract LoanTokenLogicStandard is LoanTokenSettingsLowerAdmin {
uint256[5] memory sentAmounts,
bytes memory loanDataBytes
) internal returns (uint256, uint256) {
/// @dev bZx function level pause
_checkPause();

require(
sentAmounts[1] <= _underlyingBalance() && // newPrincipal (borrowed amount + fees)
sentAddresses[1] != address(0), // borrower
Expand Down
27 changes: 18 additions & 9 deletions contracts/connectors/loantoken/Pausable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ contract Pausable is Ownable {
address public pauser;

/// @notice Pause state flag for module granurality.
mapping (address => bool) public moduleIsPaused;
mapping(address => bool) public moduleIsPaused;

/// @notice Pause global state flag. It pauses the whole protocol.
bool public protocolPaused;

Expand Down Expand Up @@ -123,17 +123,26 @@ contract Pausable is Ownable {
}
}

function toggleProtocolPause(
bool isPaused
) public {
function _debug_toggleFunctionPause(
string memory funcId
) public view returns (bytes32) {
/// keccak256("iToken_FunctionPause")
bytes32 slot =
keccak256(
abi.encodePacked(
bytes4(keccak256(abi.encodePacked(funcId))),
uint256(0xd46a704bc285dbd6ff5ad3863506260b1df02812f4f857c8cc852317a6ac64f2)
)
);
return slot;
}

function toggleProtocolPause(bool isPaused) public {
// require(msg.sender == pauser, "onlyPauser");
protocolPaused = isPaused;
}

function toggleModulePause(
address module,
bool isPaused
) public {
function toggleModulePause(address module, bool isPaused) public {
// require(msg.sender == pauser, "onlyPauser");
moduleIsPaused[module] = isPaused;
}
Expand Down
62 changes: 51 additions & 11 deletions tests-js/FeeSharingProxyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ contract("FeeSharingProxy:", (accounts) => {

//Staking
let stakingLogic = await StakingLogic.new(SOVToken.address);
staking = await StakingProxy.new(SOVToken.address);
await staking.setImplementation(stakingLogic.address);
staking = await StakingLogic.at(staking.address);
stakingProxy = await StakingProxy.new(SOVToken.address);
await stakingProxy.setImplementation(stakingLogic.address);
staking = await StakingLogic.at(stakingProxy.address);

//Protocol
protocol = await Protocol.new();
Expand All @@ -81,9 +81,9 @@ contract("FeeSharingProxy:", (accounts) => {
//Loan token
loanTokenSettings = await LoanTokenSettings.new();
loanTokenLogic = await LoanTokenLogic.new();
loanToken = await LoanToken.new(root, loanTokenLogic.address, protocol.address, wrbtc.address);
await loanToken.initialize(susd.address, "iSUSD", "iSUSD");
loanToken = await LoanTokenLogic.at(loanToken.address);
loanTokenProxy = await LoanToken.new(root, loanTokenLogic.address, protocol.address, wrbtc.address);
await loanTokenProxy.initialize(susd.address, "iSUSD", "iSUSD");
loanToken = await LoanTokenLogic.at(loanTokenProxy.address);
await loanToken.setAdmin(root);
await protocol.setLoanPool([loanToken.address], [susd.address]);
//FeeSharingProxy
Expand Down Expand Up @@ -266,17 +266,29 @@ contract("FeeSharingProxy:", (accounts) => {

it("Shouldn't be able to transfer tokens when protocol is paused", async () => {
// Set global protocol pause to on
loanToken.toggleProtocolPause(true);

await stakingProxy.toggleProtocolPause(true);

// Verify it is paused
assert.ok(await stakingProxy.protocolPaused() === true);
// let kk = await loanToken.profitOf(SOVToken.address);

// stake - getPriorTotalVotingPower
let totalStake = 1000;
await stake(totalStake, root);

let amount = 1000;
await SOVToken.approve(feeSharingProxy.address, amount * 7);

let tx = await feeSharingProxy.transferTokens(SOVToken.address, amount);

// Set global protocol pause to on for loanToken
await loanTokenProxy.toggleProtocolPause(true);

// Verify it is paused
assert.ok(await loanTokenProxy.protocolPaused() === true);

await expectRevert(
feeSharingProxy.transferTokens(SOVToken.address, amount),
"Proxy::(): Protocol is paused."
);
});
it("Should be able to transfer tokens", async () => {
// stake - getPriorTotalVotingPower
Expand Down Expand Up @@ -524,6 +536,34 @@ contract("FeeSharingProxy:", (accounts) => {
expect(processedCheckpoints.toNumber()).to.be.equal(10);
});

it("Shouldn't be able to process 10 checkpoints when protocol is paused", async () => {
//stake - getPriorTotalVotingPower
await stake(900, root);
let userStake = 100;
if (MOCK_PRIOR_WEIGHTED_STAKE) {
await staking.MOCK_priorWeightedStake(userStake * 10);
}
await SOVToken.transfer(account1, userStake);
await stake(userStake, account1);

//mock data
await createCheckpoints(10);

// Set global protocol pause to on for loanToken
loanToken.toggleProtocolPause(true);

await expectRevert(feeSharingProxy.withdraw(loanToken.address, 1000, ZERO_ADDRESS, { from: account1 }), "Proxy::(): Protocol is paused.");

// Set global protocol pause to off for loanToken
loanToken.toggleProtocolPause(false);

let tx = await feeSharingProxy.withdraw(loanToken.address, 1000, ZERO_ADDRESS, { from: account1 });
console.log("\nwithdraw(checkpoints = 10).gasUsed: " + tx.receipt.gasUsed);
//processedCheckpoints
let processedCheckpoints = await feeSharingProxy.processedCheckpoints.call(account1, loanToken.address);
expect(processedCheckpoints.toNumber()).to.be.equal(10);
});

it("Should be able to process 10 checkpoints and 3 withdrawal", async () => {
//stake - getPriorTotalVotingPower
await stake(900, root);
Expand Down Expand Up @@ -652,7 +692,7 @@ contract("FeeSharingProxy:", (accounts) => {
console.log("\nwithdraw(checkpoints = 1).gasUsed: " + tx.receipt.gasUsed);
});

it("should compute the weighted stake and show gas usage", async () => {
it("Should compute the weighted stake and show gas usage", async () => {
await stake(100, root);
let kickoffTS = await staking.kickoffTS.call();
let stakingDate = kickoffTS.add(new BN(MAX_DURATION));
Expand Down
47 changes: 43 additions & 4 deletions tests-js/bZx_Pausable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,25 @@ contract("Whether Pausable Margin Trading is reverting properly on Pause state",
await doc.transfer(loanTokenV2.address, wei("500", "ether"));
await doc.approve(loanToken.address, web3.utils.toWei("20", "ether"));
});
it("Margin trading with 3X leverage with DOC token and topUp position by 12rwBTC", async () => {
// setting up interest rates
//Giving some testRbtc to sovrynAddress (by minting some testRbtc),so that it can open position in wRBTC.
it("Pause/Resume a function and check the pause function status", async () => {
let functionSignature = "marginTrade(bytes32,uint256,uint256,uint256,address,address,bytes)";
await loanTokenV2.setPauser(accounts[0], { from: owner });

// Pause a function
await loanTokenV2.toggleFunctionPause(functionSignature, true, { from: accounts[0] });

// Verify it is paused
assert.ok(await loanTokenV2.checkPause(functionSignature) === true);

// Unpause a function
await loanTokenV2.toggleFunctionPause(functionSignature, false, { from: accounts[0] });

// Verify it is not paused
assert.ok(await loanTokenV2.checkPause(functionSignature) === false);
});
it("Pausing margin trading", async () => {
// Setting up interest rates
// Giving some testRbtc to sovrynAddress (by minting some testRbtc), so that it can open position in wRBTC.
await testWrbtc.mint(sovryn.address, wei("500", "ether"));

// assert.equal(await sovryn.protocolAddress(), sovryn.address);
Expand All @@ -136,6 +152,10 @@ contract("Whether Pausable Margin Trading is reverting properly on Pause state",
await loanTokenV2.setPauser(accounts[0], { from: owner });
await loanTokenV2.toggleFunctionPause(functionSignature, true, { from: accounts[0] });

// Verify it is paused
assert.ok(await loanTokenV2.checkPause(functionSignature) === true);

// Call margin trading
await expectRevert(
loanTokenV2.marginTrade(
constants.ZERO_BYTES32, // loanId (0 for new loans)
Expand All @@ -150,6 +170,25 @@ contract("Whether Pausable Margin Trading is reverting properly on Pause state",
),
"Function paused. It cannot be executed."
);
// expect(await sovryn.getUserNotFirstTradeFlag(owner), "sovryn.getUserNotFirstTradeFlag(trader) should be true").to.be.true;

// Unpause the given function
await loanTokenV2.toggleFunctionPause(functionSignature, false, { from: accounts[0] });

// Verify it is not paused
assert.ok(await loanTokenV2.checkPause(functionSignature) === false);

// This one should work
// Call margin trading
await loanTokenV2.marginTrade(
constants.ZERO_BYTES32, // loanId (0 for new loans)
leverageAmount, // leverageAmount
loanTokenSent, // loanTokenSent
0, // no collateral token sent
testWrbtc.address, // collateralTokenAddress
owner, //trader, // trader,
//referrer, // affiliates referrer
"0x", // loanDataBytes (only required with ether)
{ from: owner }
);
});
});

0 comments on commit 227cfbf

Please sign in to comment.