From c2c13367556d12f481d8fff4d28cc4a4bd64c86d Mon Sep 17 00:00:00 2001 From: tft Date: Tue, 15 Jan 2019 13:27:14 +0530 Subject: [PATCH 1/4] Invoked function _adjustTotalSupplyCheckpoints inside of createCheckpoint instead of mint/burn fucntions --- contracts/tokens/SecurityToken.sol | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/contracts/tokens/SecurityToken.sol b/contracts/tokens/SecurityToken.sol index 2979a6a5d..3168037b2 100644 --- a/contracts/tokens/SecurityToken.sol +++ b/contracts/tokens/SecurityToken.sol @@ -648,7 +648,6 @@ contract SecurityToken is ERC20, ERC20Detailed, ReentrancyGuard, RegistryUpdater returns(bool success) { require(_updateTransfer(address(0), _investor, _value, _data), "Transfer invalid"); - _adjustTotalSupplyCheckpoints(); _mint(_investor, _value); emit Minted(_investor, _value); return true; @@ -692,7 +691,6 @@ contract SecurityToken is ERC20, ERC20Detailed, ReentrancyGuard, RegistryUpdater function _checkAndBurn(address _from, uint256 _value, bytes memory _data) internal returns(bool) { bool verified = _updateTransfer(_from, address(0), _value, _data); - _adjustTotalSupplyCheckpoints(); _burn(_from, _value); emit Burnt(_from, _value); return verified; @@ -709,7 +707,6 @@ contract SecurityToken is ERC20, ERC20Detailed, ReentrancyGuard, RegistryUpdater function _checkAndBurnFrom(address _from, uint256 _value, bytes memory _data) internal returns(bool) { bool verified = _updateTransfer(_from, address(0), _value, _data); - _adjustTotalSupplyCheckpoints(); _burnFrom(_from, _value); emit Burnt(_from, _value); return verified; @@ -735,6 +732,7 @@ contract SecurityToken is ERC20, ERC20Detailed, ReentrancyGuard, RegistryUpdater /*solium-disable-next-line security/no-block-members*/ checkpointTimes.push(now); /*solium-disable-next-line security/no-block-members*/ + _adjustTotalSupplyCheckpoints(); emit CheckpointCreated(currentCheckpointId, now); return currentCheckpointId; } From ccf095682a66707d1ec180a9072db0f6f02b659d Mon Sep 17 00:00:00 2001 From: tft Date: Tue, 15 Jan 2019 16:41:30 +0530 Subject: [PATCH 2/4] Removed _adjustTotalSupplyCheckpoints & adjustCheckpoints as additional conditions were not needed and only one statement was happening in the function --- contracts/libraries/TokenLib.sol | 18 ------------------ contracts/tokens/SecurityToken.sol | 9 +-------- 2 files changed, 1 insertion(+), 26 deletions(-) diff --git a/contracts/libraries/TokenLib.sol b/contracts/libraries/TokenLib.sol index 8585a3dba..2980392b0 100644 --- a/contracts/libraries/TokenLib.sol +++ b/contracts/libraries/TokenLib.sol @@ -222,24 +222,6 @@ library TokenLib { return _checkpoints[max].value; } - /** - * @notice Stores the changes to the checkpoint objects - * @param _checkpoints is the affected checkpoint object array - * @param _newValue is the new value that needs to be stored - */ - function adjustCheckpoints(TokenLib.Checkpoint[] storage _checkpoints, uint256 _newValue, uint256 _currentCheckpointId) public { - //No checkpoints set yet - if (_currentCheckpointId == 0) { - return; - } - //No new checkpoints since last update - if ((_checkpoints.length > 0) && (_checkpoints[_checkpoints.length - 1].checkpointId == _currentCheckpointId)) { - return; - } - //New checkpoint, so record balance - _checkpoints.push(TokenLib.Checkpoint({checkpointId: _currentCheckpointId, value: _newValue})); - } - /** * @notice Keeps track of the number of non-zero token holders * @param _investorData Date releated to investor metrics diff --git a/contracts/tokens/SecurityToken.sol b/contracts/tokens/SecurityToken.sol index 3168037b2..6082854a5 100644 --- a/contracts/tokens/SecurityToken.sol +++ b/contracts/tokens/SecurityToken.sol @@ -458,13 +458,6 @@ contract SecurityToken is ERC20, ERC20Detailed, ReentrancyGuard, RegistryUpdater emit FreezeTransfers(false, now); } - /** - * @notice Internal - adjusts totalSupply at checkpoint after minting or burning tokens - */ - function _adjustTotalSupplyCheckpoints() internal { - TokenLib.adjustCheckpoints(checkpointTotalSupply, totalSupply(), currentCheckpointId); - } - /** * @notice Internal - adjusts token holder balance at checkpoint after a token transfer * @param _investor address of the token holder affected @@ -732,7 +725,7 @@ contract SecurityToken is ERC20, ERC20Detailed, ReentrancyGuard, RegistryUpdater /*solium-disable-next-line security/no-block-members*/ checkpointTimes.push(now); /*solium-disable-next-line security/no-block-members*/ - _adjustTotalSupplyCheckpoints(); + checkpointTotalSupply.push(TokenLib.Checkpoint({checkpointId: currentCheckpointId, value: totalSupply()})); emit CheckpointCreated(currentCheckpointId, now); return currentCheckpointId; } From 617a28871f9b9da3530f523df895bba7a3e711b1 Mon Sep 17 00:00:00 2001 From: tft Date: Tue, 15 Jan 2019 18:57:03 +0530 Subject: [PATCH 3/4] Reinstated the function adjustCheckpoints --- contracts/libraries/TokenLib.sol | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/contracts/libraries/TokenLib.sol b/contracts/libraries/TokenLib.sol index 2980392b0..8585a3dba 100644 --- a/contracts/libraries/TokenLib.sol +++ b/contracts/libraries/TokenLib.sol @@ -222,6 +222,24 @@ library TokenLib { return _checkpoints[max].value; } + /** + * @notice Stores the changes to the checkpoint objects + * @param _checkpoints is the affected checkpoint object array + * @param _newValue is the new value that needs to be stored + */ + function adjustCheckpoints(TokenLib.Checkpoint[] storage _checkpoints, uint256 _newValue, uint256 _currentCheckpointId) public { + //No checkpoints set yet + if (_currentCheckpointId == 0) { + return; + } + //No new checkpoints since last update + if ((_checkpoints.length > 0) && (_checkpoints[_checkpoints.length - 1].checkpointId == _currentCheckpointId)) { + return; + } + //New checkpoint, so record balance + _checkpoints.push(TokenLib.Checkpoint({checkpointId: _currentCheckpointId, value: _newValue})); + } + /** * @notice Keeps track of the number of non-zero token holders * @param _investorData Date releated to investor metrics From 1e05bbd94ae74fc12fd6856ac622789ef5454368 Mon Sep 17 00:00:00 2001 From: tft Date: Tue, 15 Jan 2019 20:20:08 +0530 Subject: [PATCH 4/4] Changed data-type of checkpointTotalSupply from struct to mapping as it will save gas and will do the work --- contracts/tokens/SecurityToken.sol | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contracts/tokens/SecurityToken.sol b/contracts/tokens/SecurityToken.sol index 6082854a5..505a403ee 100644 --- a/contracts/tokens/SecurityToken.sol +++ b/contracts/tokens/SecurityToken.sol @@ -76,8 +76,8 @@ contract SecurityToken is ERC20, ERC20Detailed, ReentrancyGuard, RegistryUpdater // Map each investor to a series of checkpoints mapping(address => TokenLib.Checkpoint[]) checkpointBalances; - // List of checkpoints that relate to total supply - TokenLib.Checkpoint[] checkpointTotalSupply; + // Mapping of checkpoints that relate to total supply + mapping (uint256 => uint256) checkpointTotalSupply; // Times at which each checkpoint was created uint256[] checkpointTimes; @@ -725,7 +725,7 @@ contract SecurityToken is ERC20, ERC20Detailed, ReentrancyGuard, RegistryUpdater /*solium-disable-next-line security/no-block-members*/ checkpointTimes.push(now); /*solium-disable-next-line security/no-block-members*/ - checkpointTotalSupply.push(TokenLib.Checkpoint({checkpointId: currentCheckpointId, value: totalSupply()})); + checkpointTotalSupply[currentCheckpointId] = totalSupply(); emit CheckpointCreated(currentCheckpointId, now); return currentCheckpointId; } @@ -745,7 +745,7 @@ contract SecurityToken is ERC20, ERC20Detailed, ReentrancyGuard, RegistryUpdater */ function totalSupplyAt(uint256 _checkpointId) external view returns(uint256) { require(_checkpointId <= currentCheckpointId); - return TokenLib.getValueAt(checkpointTotalSupply, _checkpointId, totalSupply()); + return checkpointTotalSupply[_checkpointId]; } /**