Skip to content

Commit

Permalink
Merge pull request #690 from PolymathNetwork/task_3.17
Browse files Browse the repository at this point in the history
Add StatusCode library
  • Loading branch information
maxsam4 authored May 31, 2019
2 parents 6322b72 + 125fc9d commit 92c12c0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
21 changes: 21 additions & 0 deletions contracts/libraries/StatusCodes.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pragma solidity ^0.5.0;

library StatusCodes {

// ERC1400 status code inspired from ERC1066
enum Status {
TransferFailure,
TransferSuccess,
InsufficientBalance,
InsufficientAllowance,
TransfersHalted,
FundsLocked,
InvalidSender,
InvalidReceiver,
InvalidOperator
}

function code(Status _status) internal pure returns (byte) {
return byte(uint8(0x50) + (uint8(_status)));
}
}
13 changes: 7 additions & 6 deletions contracts/libraries/TokenLib.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pragma solidity ^0.5.0;

import "../interfaces/IPoly.sol";
import "./StatusCodes.sol";
import "../modules/UpgradableModuleFactory.sol";
import "../interfaces/IDataStore.sol";
import "../tokens/SecurityTokenStorage.sol";
Expand Down Expand Up @@ -450,9 +451,9 @@ library TokenLib {
// If no unarchived modules, return true by default
// Use the local variables to avoid the stack too deep error
isValid = transfersFrozen ? (isForceValid ? true : (isInvalid ? false : isValid)) : true;
return (isValid, isValid ? bytes32(hex"51"): appCode);
return (isValid, isValid ? bytes32(StatusCodes.code(StatusCodes.Status.TransferSuccess)): appCode);
}
return (false, bytes32(hex"54"));
return (false, bytes32(StatusCodes.code(StatusCodes.Status.TransfersHalted)));
}

function canTransfer(
Expand All @@ -467,19 +468,19 @@ library TokenLib {
returns (bool, byte, bytes32)
{
if (!success)
return (false, 0x50, appCode);
return (false, StatusCodes.code(StatusCodes.Status.TransferFailure), appCode);

if (balanceOfFrom < value)
return (false, 0x52, bytes32(0));
return (false, StatusCodes.code(StatusCodes.Status.InsufficientBalance), bytes32(0));

if (to == address(0))
return (false, 0x57, bytes32(0));
return (false, StatusCodes.code(StatusCodes.Status.InvalidReceiver), bytes32(0));

// Balance overflow can never happen due to totalsupply being a uint256 as well
// else if (!KindMath.checkAdd(balanceOf(_to), _value))
// return (false, 0x50, bytes32(0));

return (true, 0x51, bytes32(0));
return (true, StatusCodes.code(StatusCodes.Status.TransferSuccess), bytes32(0));
}

function _getKey(bytes32 _key1, address _key2) internal pure returns(bytes32) {
Expand Down
7 changes: 4 additions & 3 deletions contracts/tokens/SecurityToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "../PolymathRegistry.sol";
import "../interfaces/IModule.sol";
import "./SecurityTokenStorage.sol";
import "../libraries/TokenLib.sol";
import "../libraries/StatusCodes.sol";
import "../interfaces/IDataStore.sol";
import "../interfaces/IUpgradableTokenFactory.sol";
import "../interfaces/IModuleFactory.sol";
Expand Down Expand Up @@ -934,15 +935,15 @@ contract SecurityToken is ERC20, ReentrancyGuard, SecurityTokenStorage, IERC1594
function canTransferFrom(address _from, address _to, uint256 _value, bytes calldata _data) external view returns (bool success, byte reasonCode, bytes32 appCode) {
(success, reasonCode, appCode) = _canTransfer(_from, _to, _value, _data);
if (success && _value > allowance(_from, msg.sender)) {
return (false, 0x53, bytes32(0));
return (false, StatusCodes.code(StatusCodes.Status.InsufficientAllowance), bytes32(0));
}
}

function _canTransfer(address _from, address _to, uint256 _value, bytes memory _data) internal view returns (bool, byte, bytes32) {
bytes32 appCode;
bool success;
if (_value % granularity != 0) {
return (false, 0x50, bytes32(0));
return (false, StatusCodes.code(StatusCodes.Status.TransferFailure), bytes32(0));
}
(success, appCode) = TokenLib.verifyTransfer(modules[TRANSFER_KEY], modulesToData, _from, _to, _value, _data, transfersFrozen);
return TokenLib.canTransfer(success, appCode, _to, _value, balanceOf(_from));
Expand Down Expand Up @@ -981,7 +982,7 @@ contract SecurityToken is ERC20, ReentrancyGuard, SecurityTokenStorage, IERC1594
}
return (esc, appStatusCode, toPartition);
}
return (0x50, bytes32(0), bytes32(0));
return (StatusCodes.code(StatusCodes.Status.TransferFailure), bytes32(0), bytes32(0));
}

/**
Expand Down

0 comments on commit 92c12c0

Please sign in to comment.