From e06ecbe4fb4c061308fa22d3bcdd9e9fdf82afc2 Mon Sep 17 00:00:00 2001 From: Stan Date: Wed, 18 Aug 2021 14:27:15 +0530 Subject: [PATCH 1/3] Adding event - new Loan Token Logic --- .../modules/LoanTokenSettingsLowerAdmin.sol | 7 +++++++ contracts/interfaces/ILoanTokenModules.sol | 2 ++ tests/loan-token/Administration.test.js | 18 +++++++++++++++--- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/contracts/connectors/loantoken/modules/LoanTokenSettingsLowerAdmin.sol b/contracts/connectors/loantoken/modules/LoanTokenSettingsLowerAdmin.sol index 89b6cda35..28861b6fe 100644 --- a/contracts/connectors/loantoken/modules/LoanTokenSettingsLowerAdmin.sol +++ b/contracts/connectors/loantoken/modules/LoanTokenSettingsLowerAdmin.sol @@ -22,6 +22,7 @@ contract LoanTokenSettingsLowerAdmin is LoanTokenLogicStorage { /* Events */ event SetTransactionLimits(address[] addresses, uint256[] limits); + event ToggleFunctionPaused(string functionId, bool prevFlag, bool newFlag); /* Functions */ @@ -184,6 +185,7 @@ contract LoanTokenSettingsLowerAdmin is LoanTokenLogicStorage { string memory funcId, /// example: "mint(uint256,uint256)" bool isPaused ) public { + bool paused; require(msg.sender == pauser, "onlyPauser"); /// keccak256("iToken_FunctionPause") bytes32 slot = @@ -193,9 +195,14 @@ contract LoanTokenSettingsLowerAdmin is LoanTokenLogicStorage { uint256(0xd46a704bc285dbd6ff5ad3863506260b1df02812f4f857c8cc852317a6ac64f2) ) ); + assembly { + paused := sload(slot) + } + require(paused != isPaused, "invalid"); assembly { sstore(slot, isPaused) } + emit ToggleFunctionPaused(funcId, !isPaused, isPaused); } /** diff --git a/contracts/interfaces/ILoanTokenModules.sol b/contracts/interfaces/ILoanTokenModules.sol index 13d2a09a2..d828eef7b 100644 --- a/contracts/interfaces/ILoanTokenModules.sol +++ b/contracts/interfaces/ILoanTokenModules.sol @@ -19,6 +19,8 @@ interface ILoanTokenModules { event SetTransactionLimits(address[] addresses, uint256[] limits); + event ToggleFunctionPaused(string functionId, bool prevFlag, bool newFlag); + /** INTERFACE */ /** START LOAN TOKEN SETTINGS LOWER ADMIN */ diff --git a/tests/loan-token/Administration.test.js b/tests/loan-token/Administration.test.js index 5649cfa7a..608bedad8 100644 --- a/tests/loan-token/Administration.test.js +++ b/tests/loan-token/Administration.test.js @@ -1,5 +1,5 @@ const { expect } = require("chai"); -const { expectRevert, BN } = require("@openzeppelin/test-helpers"); +const { expectRevert, BN, expectEvent } = require("@openzeppelin/test-helpers"); const LoanToken = artifacts.require("LoanToken"); const LoanTokenLogicBeacon = artifacts.require("LoanTokenLogicBeacon"); const LoanTokenLogicProxy = artifacts.require("LoanTokenLogicProxy"); @@ -131,7 +131,13 @@ contract("LoanTokenAdministration", (accounts) => { // pause the given function and make sure the function can't be called anymore let localLoanToken = loanToken; await localLoanToken.setPauser(accounts[0]); - await localLoanToken.toggleFunctionPause(functionSignature, true); + let tx = await localLoanToken.toggleFunctionPause(functionSignature, true); + expectEvent(tx, "ToggleFunctionPaused", { + functionId: functionSignature, + prevFlag: false, + newFlag: true, + }); + await expectRevert(localLoanToken.toggleFunctionPause(functionSignature, true), "invalid"); await expectRevert(open_margin_trade_position(loanToken, RBTC, WRBTC, SUSD, accounts[1]), "unauthorized"); @@ -139,7 +145,13 @@ contract("LoanTokenAdministration", (accounts) => { assert(localLoanToken.checkPause(functionSignature)); await localLoanToken.setPauser(accounts[0]); - await localLoanToken.toggleFunctionPause(functionSignature, false); + tx = await localLoanToken.toggleFunctionPause(functionSignature, false); + expectEvent(tx, "ToggleFunctionPaused", { + functionId: functionSignature, + prevFlag: true, + newFlag: false, + }); + await expectRevert(localLoanToken.toggleFunctionPause(functionSignature, false), "invalid"); await open_margin_trade_position(loanToken, RBTC, WRBTC, SUSD, accounts[1]); // check if checkPause returns false From 766c38a978812eaf3b934b0ec858eda2114247a0 Mon Sep 17 00:00:00 2001 From: Stan Marsh Date: Wed, 3 Nov 2021 18:21:32 +0530 Subject: [PATCH 2/3] Incorporating review comments --- .../modules/LoanTokenSettingsLowerAdmin.sol | 19 +++++++++---------- contracts/interfaces/ILoanTokenModules.sol | 2 +- tests/loan-token/Administration.test.js | 8 ++++---- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/contracts/connectors/loantoken/modules/LoanTokenSettingsLowerAdmin.sol b/contracts/connectors/loantoken/modules/LoanTokenSettingsLowerAdmin.sol index 28861b6fe..1b9c4f9ba 100644 --- a/contracts/connectors/loantoken/modules/LoanTokenSettingsLowerAdmin.sol +++ b/contracts/connectors/loantoken/modules/LoanTokenSettingsLowerAdmin.sol @@ -22,7 +22,7 @@ contract LoanTokenSettingsLowerAdmin is LoanTokenLogicStorage { /* Events */ event SetTransactionLimits(address[] addresses, uint256[] limits); - event ToggleFunctionPaused(string functionId, bool prevFlag, bool newFlag); + event ToggledFunctionPaused(string functionId, bool prevFlag, bool newFlag); /* Functions */ @@ -188,21 +188,20 @@ contract LoanTokenSettingsLowerAdmin is LoanTokenLogicStorage { bool paused; require(msg.sender == pauser, "onlyPauser"); /// keccak256("iToken_FunctionPause") - bytes32 slot = - keccak256( - abi.encodePacked( - bytes4(keccak256(abi.encodePacked(funcId))), - uint256(0xd46a704bc285dbd6ff5ad3863506260b1df02812f4f857c8cc852317a6ac64f2) - ) - ); + bytes32 slot = keccak256( + abi.encodePacked( + bytes4(keccak256(abi.encodePacked(funcId))), + uint256(0xd46a704bc285dbd6ff5ad3863506260b1df02812f4f857c8cc852317a6ac64f2) + ) + ); assembly { paused := sload(slot) } - require(paused != isPaused, "invalid"); + require(paused != isPaused, "isPaused is already set to that value"); assembly { sstore(slot, isPaused) } - emit ToggleFunctionPaused(funcId, !isPaused, isPaused); + emit ToggledFunctionPaused(funcId, !isPaused, isPaused); } /** diff --git a/contracts/interfaces/ILoanTokenModules.sol b/contracts/interfaces/ILoanTokenModules.sol index d828eef7b..38811c7b3 100644 --- a/contracts/interfaces/ILoanTokenModules.sol +++ b/contracts/interfaces/ILoanTokenModules.sol @@ -19,7 +19,7 @@ interface ILoanTokenModules { event SetTransactionLimits(address[] addresses, uint256[] limits); - event ToggleFunctionPaused(string functionId, bool prevFlag, bool newFlag); + event ToggledFunctionPaused(string functionId, bool prevFlag, bool newFlag); /** INTERFACE */ diff --git a/tests/loan-token/Administration.test.js b/tests/loan-token/Administration.test.js index 608bedad8..d0906d2cf 100644 --- a/tests/loan-token/Administration.test.js +++ b/tests/loan-token/Administration.test.js @@ -132,12 +132,12 @@ contract("LoanTokenAdministration", (accounts) => { let localLoanToken = loanToken; await localLoanToken.setPauser(accounts[0]); let tx = await localLoanToken.toggleFunctionPause(functionSignature, true); - expectEvent(tx, "ToggleFunctionPaused", { + expectEvent(tx, "ToggledFunctionPaused", { functionId: functionSignature, prevFlag: false, newFlag: true, }); - await expectRevert(localLoanToken.toggleFunctionPause(functionSignature, true), "invalid"); + await expectRevert(localLoanToken.toggleFunctionPause(functionSignature, true), "isPaused is already set to that value"); await expectRevert(open_margin_trade_position(loanToken, RBTC, WRBTC, SUSD, accounts[1]), "unauthorized"); @@ -146,12 +146,12 @@ contract("LoanTokenAdministration", (accounts) => { await localLoanToken.setPauser(accounts[0]); tx = await localLoanToken.toggleFunctionPause(functionSignature, false); - expectEvent(tx, "ToggleFunctionPaused", { + expectEvent(tx, "ToggledFunctionPaused", { functionId: functionSignature, prevFlag: true, newFlag: false, }); - await expectRevert(localLoanToken.toggleFunctionPause(functionSignature, false), "invalid"); + await expectRevert(localLoanToken.toggleFunctionPause(functionSignature, false), "isPaused is already set to that value"); await open_margin_trade_position(loanToken, RBTC, WRBTC, SUSD, accounts[1]); // check if checkPause returns false From 0f9e9ab1a04e91a84b8977b13a6d20038b87ffca Mon Sep 17 00:00:00 2001 From: Stan Marsh Date: Wed, 3 Nov 2021 18:22:56 +0530 Subject: [PATCH 3/3] Ran Prettier --- .../modules/LoanTokenSettingsLowerAdmin.sol | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/contracts/connectors/loantoken/modules/LoanTokenSettingsLowerAdmin.sol b/contracts/connectors/loantoken/modules/LoanTokenSettingsLowerAdmin.sol index 1b9c4f9ba..7198fffc4 100644 --- a/contracts/connectors/loantoken/modules/LoanTokenSettingsLowerAdmin.sol +++ b/contracts/connectors/loantoken/modules/LoanTokenSettingsLowerAdmin.sol @@ -188,12 +188,13 @@ contract LoanTokenSettingsLowerAdmin is LoanTokenLogicStorage { bool paused; require(msg.sender == pauser, "onlyPauser"); /// keccak256("iToken_FunctionPause") - bytes32 slot = keccak256( - abi.encodePacked( - bytes4(keccak256(abi.encodePacked(funcId))), - uint256(0xd46a704bc285dbd6ff5ad3863506260b1df02812f4f857c8cc852317a6ac64f2) - ) - ); + bytes32 slot = + keccak256( + abi.encodePacked( + bytes4(keccak256(abi.encodePacked(funcId))), + uint256(0xd46a704bc285dbd6ff5ad3863506260b1df02812f4f857c8cc852317a6ac64f2) + ) + ); assembly { paused := sload(slot) }