Skip to content

Commit 92c12c0

Browse files
authored
Merge pull request #690 from PolymathNetwork/task_3.17
Add StatusCode library
2 parents 6322b72 + 125fc9d commit 92c12c0

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

contracts/libraries/StatusCodes.sol

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pragma solidity ^0.5.0;
2+
3+
library StatusCodes {
4+
5+
// ERC1400 status code inspired from ERC1066
6+
enum Status {
7+
TransferFailure,
8+
TransferSuccess,
9+
InsufficientBalance,
10+
InsufficientAllowance,
11+
TransfersHalted,
12+
FundsLocked,
13+
InvalidSender,
14+
InvalidReceiver,
15+
InvalidOperator
16+
}
17+
18+
function code(Status _status) internal pure returns (byte) {
19+
return byte(uint8(0x50) + (uint8(_status)));
20+
}
21+
}

contracts/libraries/TokenLib.sol

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pragma solidity ^0.5.0;
22

33
import "../interfaces/IPoly.sol";
4+
import "./StatusCodes.sol";
45
import "../modules/UpgradableModuleFactory.sol";
56
import "../interfaces/IDataStore.sol";
67
import "../tokens/SecurityTokenStorage.sol";
@@ -450,9 +451,9 @@ library TokenLib {
450451
// If no unarchived modules, return true by default
451452
// Use the local variables to avoid the stack too deep error
452453
isValid = transfersFrozen ? (isForceValid ? true : (isInvalid ? false : isValid)) : true;
453-
return (isValid, isValid ? bytes32(hex"51"): appCode);
454+
return (isValid, isValid ? bytes32(StatusCodes.code(StatusCodes.Status.TransferSuccess)): appCode);
454455
}
455-
return (false, bytes32(hex"54"));
456+
return (false, bytes32(StatusCodes.code(StatusCodes.Status.TransfersHalted)));
456457
}
457458

458459
function canTransfer(
@@ -467,19 +468,19 @@ library TokenLib {
467468
returns (bool, byte, bytes32)
468469
{
469470
if (!success)
470-
return (false, 0x50, appCode);
471+
return (false, StatusCodes.code(StatusCodes.Status.TransferFailure), appCode);
471472

472473
if (balanceOfFrom < value)
473-
return (false, 0x52, bytes32(0));
474+
return (false, StatusCodes.code(StatusCodes.Status.InsufficientBalance), bytes32(0));
474475

475476
if (to == address(0))
476-
return (false, 0x57, bytes32(0));
477+
return (false, StatusCodes.code(StatusCodes.Status.InvalidReceiver), bytes32(0));
477478

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

482-
return (true, 0x51, bytes32(0));
483+
return (true, StatusCodes.code(StatusCodes.Status.TransferSuccess), bytes32(0));
483484
}
484485

485486
function _getKey(bytes32 _key1, address _key2) internal pure returns(bytes32) {

contracts/tokens/SecurityToken.sol

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "../PolymathRegistry.sol";
55
import "../interfaces/IModule.sol";
66
import "./SecurityTokenStorage.sol";
77
import "../libraries/TokenLib.sol";
8+
import "../libraries/StatusCodes.sol";
89
import "../interfaces/IDataStore.sol";
910
import "../interfaces/IUpgradableTokenFactory.sol";
1011
import "../interfaces/IModuleFactory.sol";
@@ -934,15 +935,15 @@ contract SecurityToken is ERC20, ReentrancyGuard, SecurityTokenStorage, IERC1594
934935
function canTransferFrom(address _from, address _to, uint256 _value, bytes calldata _data) external view returns (bool success, byte reasonCode, bytes32 appCode) {
935936
(success, reasonCode, appCode) = _canTransfer(_from, _to, _value, _data);
936937
if (success && _value > allowance(_from, msg.sender)) {
937-
return (false, 0x53, bytes32(0));
938+
return (false, StatusCodes.code(StatusCodes.Status.InsufficientAllowance), bytes32(0));
938939
}
939940
}
940941

941942
function _canTransfer(address _from, address _to, uint256 _value, bytes memory _data) internal view returns (bool, byte, bytes32) {
942943
bytes32 appCode;
943944
bool success;
944945
if (_value % granularity != 0) {
945-
return (false, 0x50, bytes32(0));
946+
return (false, StatusCodes.code(StatusCodes.Status.TransferFailure), bytes32(0));
946947
}
947948
(success, appCode) = TokenLib.verifyTransfer(modules[TRANSFER_KEY], modulesToData, _from, _to, _value, _data, transfersFrozen);
948949
return TokenLib.canTransfer(success, appCode, _to, _value, balanceOf(_from));
@@ -981,7 +982,7 @@ contract SecurityToken is ERC20, ReentrancyGuard, SecurityTokenStorage, IERC1594
981982
}
982983
return (esc, appStatusCode, toPartition);
983984
}
984-
return (0x50, bytes32(0), bytes32(0));
985+
return (StatusCodes.code(StatusCodes.Status.TransferFailure), bytes32(0), bytes32(0));
985986
}
986987

987988
/**

0 commit comments

Comments
 (0)