@0x:contracts-exchange Eliminated code duplication within LibExchangeRichErrors and IExchangeRichErrors

This commit is contained in:
James Towle
2019-07-11 16:47:17 -05:00
committed by Amir Bandeali
parent dabef47ce7
commit 53db047a4e
3 changed files with 168 additions and 90 deletions

View File

@@ -25,6 +25,8 @@ import "./interfaces/IExchangeRichErrors.sol";
library LibExchangeRichErrors {
/*** Selector Getters ***/
// bytes4(keccak256("SignatureError(uint8,bytes32,address,bytes)"))
bytes4 internal constant SIGNATURE_ERROR_SELECTOR =
0x7e5a2318;
@@ -97,6 +99,153 @@ library LibExchangeRichErrors {
bytes4 internal constant INCOMPLETE_FILL_ERROR_SELECTOR =
0x152aa60e;
// solhint-disable func-name-mixedcase
function SignatureErrorSelector()
internal
pure
returns (bytes4)
{
return SIGNATURE_ERROR_SELECTOR;
}
function SignatureValidatorNotApprovedErrorSelector()
internal
pure
returns (bytes4)
{
return SIGNATURE_VALIDATOR_NOT_APPROVED_ERROR_SELECTOR;
}
function SignatureValidatorErrorSelector()
internal
pure
returns (bytes4)
{
return SIGNATURE_VALIDATOR_ERROR_SELECTOR;
}
function SignatureWalletErrorSelector()
internal
pure
returns (bytes4)
{
return SIGNATURE_WALLET_ERROR_SELECTOR;
}
function OrderStatusErrorSelector()
internal
pure
returns (bytes4)
{
return ORDER_STATUS_ERROR_SELECTOR;
}
function InvalidSenderErrorSelector()
internal
pure
returns (bytes4)
{
return INVALID_SENDER_ERROR_SELECTOR;
}
function InvalidMakerErrorSelector()
internal
pure
returns (bytes4)
{
return INVALID_MAKER_ERROR_SELECTOR;
}
function FillErrorSelector()
internal
pure
returns (bytes4)
{
return FILL_ERROR_SELECTOR;
}
function InvalidTakerErrorSelector()
internal
pure
returns (bytes4)
{
return INVALID_TAKER_ERROR_SELECTOR;
}
function OrderEpochErrorSelector()
internal
pure
returns (bytes4)
{
return ORDER_EPOCH_ERROR_SELECTOR;
}
function AssetProxyExistsErrorSelector()
internal
pure
returns (bytes4)
{
return ASSET_PROXY_EXISTS_ERROR_SELECTOR;
}
function AssetProxyDispatchErrorSelector()
internal
pure
returns (bytes4)
{
return ASSET_PROXY_DISPATCH_ERROR_SELECTOR;
}
function AssetProxyTransferErrorSelector()
internal
pure
returns (bytes4)
{
return ASSET_PROXY_TRANSFER_ERROR_SELECTOR;
}
function NegativeSpreadErrorSelector()
internal
pure
returns (bytes4)
{
return NEGATIVE_SPREAD_ERROR_SELECTOR;
}
function TransactionErrorSelector()
internal
pure
returns (bytes4)
{
return TRANSACTION_ERROR_SELECTOR;
}
function TransactionSignatureErrorSelector()
internal
pure
returns (bytes4)
{
return TRANSACTION_SIGNATURE_ERROR_SELECTOR;
}
function TransactionExecutionErrorSelector()
internal
pure
returns (bytes4)
{
return TRANSACTION_EXECUTION_ERROR_SELECTOR;
}
function IncompleteFillErrorSelector()
internal
pure
returns (bytes4)
{
return INCOMPLETE_FILL_ERROR_SELECTOR;
}
/*** Rich Error Functions ***/
// solhint-disable func-name-mixedcase
function SignatureError(
IExchangeRichErrors.SignatureErrorCodes errorCode,

View File

@@ -28,76 +28,4 @@ contract IExchangeRichErrors {
ALREADY_EXECUTED,
EXPIRED
}
// bytes4(keccak256("SignatureError(uint8,bytes32,address,bytes)"))
bytes4 internal constant SIGNATURE_ERROR_SELECTOR =
0x7e5a2318;
// bytes4(keccak256("SignatureValidatorNotApprovedError(address,address)"))
bytes4 internal constant SIGNATURE_VALIDATOR_NOT_APPROVED_ERROR_SELECTOR =
0xa15c0d06;
// bytes4(keccak256("SignatureValidatorError(bytes32,address,address,bytes,bytes)"))
bytes4 internal constant SIGNATURE_VALIDATOR_ERROR_SELECTOR =
0xa23838b8;
// bytes4(keccak256("SignatureWalletError(bytes32,address,bytes,bytes)"))
bytes4 internal constant SIGNATURE_WALLET_ERROR_SELECTOR =
0x1b8388f7;
// bytes4(keccak256("OrderStatusError(bytes32,uint8)"))
bytes4 internal constant ORDER_STATUS_ERROR_SELECTOR =
0xfdb6ca8d;
// bytes4(keccak256("InvalidSenderError(bytes32,address)"))
bytes4 internal constant INVALID_SENDER_ERROR_SELECTOR =
0x95b59997;
// bytes4(keccak256("InvalidMakerError(bytes32,address)"))
bytes4 internal constant INVALID_MAKER_ERROR_SELECTOR =
0x26bf55d9;
// bytes4(keccak256("FillError(uint8,bytes32)"))
bytes4 internal constant FILL_ERROR_SELECTOR =
0xe94a7ed0;
// bytes4(keccak256("InvalidTakerError(bytes32,address)"))
bytes4 internal constant INVALID_TAKER_ERROR_SELECTOR =
0xfdb328be;
// bytes4(keccak256("OrderEpochError(address,address,uint256)"))
bytes4 internal constant ORDER_EPOCH_ERROR_SELECTOR =
0x4ad31275;
// bytes4(keccak256("AssetProxyExistsError(address)"))
bytes4 internal constant ASSET_PROXY_EXISTS_ERROR_SELECTOR =
0xcc8b3b53;
// bytes4(keccak256("AssetProxyDispatchError(uint8,bytes32,bytes)"))
bytes4 internal constant ASSET_PROXY_DISPATCH_ERROR_SELECTOR =
0x488219a6;
// bytes4(keccak256("AssetProxyTransferError(bytes32,bytes,bytes)"))
bytes4 internal constant ASSET_PROXY_TRANSFER_ERROR_SELECTOR =
0x4678472b;
// bytes4(keccak256("NegativeSpreadError(bytes32,bytes32)"))
bytes4 internal constant NEGATIVE_SPREAD_ERROR_SELECTOR =
0xb6555d6f;
// bytes4(keccak256("TransactionError(uint8,bytes32)"))
bytes4 internal constant TRANSACTION_ERROR_SELECTOR =
0xf5985184;
// bytes4(keccak256("TransactionSignatureError(bytes32,address,bytes)"))
bytes4 internal constant TRANSACTION_SIGNATURE_ERROR_SELECTOR =
0xbfd56ef6;
// bytes4(keccak256("TransactionExecutionError(bytes32,bytes)"))
bytes4 internal constant TRANSACTION_EXECUTION_ERROR_SELECTOR =
0x20d11f61;
// bytes4(keccak256("IncompleteFillError(bytes32)"))
bytes4 internal constant INCOMPLETE_FILL_ERROR_SELECTOR =
0x152aa60e;
}

View File

@@ -21,6 +21,7 @@ pragma solidity ^0.5.9;
import "@0x/contracts-exchange-libs/contracts/src/LibOrder.sol";
import "@0x/contracts-utils/contracts/src/LibBytes.sol";
import "../interfaces/IExchangeRichErrors.sol";
import "../LibExchangeRichErrors.sol";
contract LibExchangeRichErrorDecoder is
@@ -41,7 +42,7 @@ contract LibExchangeRichErrorDecoder is
bytes memory signature
)
{
_assertSelectorBytes(encoded, SIGNATURE_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.SignatureErrorSelector());
errorCode = SignatureErrorCodes(_readErrorParameterAsUint256(encoded, 0));
hash = _readErrorParameterAsBytes32(encoded, 1);
signerAddress = _readErrorParameterAsAddress(encoded, 2);
@@ -64,7 +65,7 @@ contract LibExchangeRichErrorDecoder is
bytes memory errorData
)
{
_assertSelectorBytes(encoded, SIGNATURE_VALIDATOR_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.SignatureValidatorErrorSelector());
hash = _readErrorParameterAsBytes32(encoded, 0);
signerAddress = _readErrorParameterAsAddress(encoded, 1);
validatorAddress = _readErrorParameterAsAddress(encoded, 2);
@@ -84,7 +85,7 @@ contract LibExchangeRichErrorDecoder is
address validatorAddress
)
{
_assertSelectorBytes(encoded, SIGNATURE_VALIDATOR_NOT_APPROVED_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.SignatureValidatorNotApprovedErrorSelector());
signerAddress = _readErrorParameterAsAddress(encoded, 0);
validatorAddress = _readErrorParameterAsAddress(encoded, 1);
}
@@ -105,7 +106,7 @@ contract LibExchangeRichErrorDecoder is
bytes memory errorData
)
{
_assertSelectorBytes(encoded, SIGNATURE_WALLET_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.SignatureWalletErrorSelector());
hash = _readErrorParameterAsBytes32(encoded, 0);
signerAddress = _readErrorParameterAsAddress(encoded, 1);
signature = _readErrorParameterAsBytes(encoded, 2);
@@ -124,7 +125,7 @@ contract LibExchangeRichErrorDecoder is
LibOrder.OrderStatus orderStatus
)
{
_assertSelectorBytes(encoded, ORDER_STATUS_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.OrderStatusErrorSelector());
orderHash = _readErrorParameterAsBytes32(encoded, 0);
orderStatus = LibOrder.OrderStatus(_readErrorParameterAsUint256(encoded, 1));
}
@@ -141,7 +142,7 @@ contract LibExchangeRichErrorDecoder is
address senderAddress
)
{
_assertSelectorBytes(encoded, INVALID_SENDER_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.InvalidSenderErrorSelector());
orderHash = _readErrorParameterAsBytes32(encoded, 0);
senderAddress = _readErrorParameterAsAddress(encoded, 1);
}
@@ -158,7 +159,7 @@ contract LibExchangeRichErrorDecoder is
address makerAddress
)
{
_assertSelectorBytes(encoded, INVALID_MAKER_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.InvalidMakerErrorSelector());
orderHash = _readErrorParameterAsBytes32(encoded, 0);
makerAddress = _readErrorParameterAsAddress(encoded, 1);
}
@@ -175,7 +176,7 @@ contract LibExchangeRichErrorDecoder is
address takerAddress
)
{
_assertSelectorBytes(encoded, INVALID_TAKER_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.InvalidTakerErrorSelector());
orderHash = _readErrorParameterAsBytes32(encoded, 0);
takerAddress = _readErrorParameterAsAddress(encoded, 1);
}
@@ -192,7 +193,7 @@ contract LibExchangeRichErrorDecoder is
bytes32 orderHash
)
{
_assertSelectorBytes(encoded, FILL_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.FillErrorSelector());
errorCode = FillErrorCodes(_readErrorParameterAsUint256(encoded, 0));
orderHash = _readErrorParameterAsBytes32(encoded, 1);
}
@@ -211,7 +212,7 @@ contract LibExchangeRichErrorDecoder is
uint256 currentEpoch
)
{
_assertSelectorBytes(encoded, ORDER_EPOCH_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.OrderEpochErrorSelector());
makerAddress = _readErrorParameterAsAddress(encoded, 0);
orderSenderAddress = _readErrorParameterAsAddress(encoded, 1);
currentEpoch = _readErrorParameterAsUint256(encoded, 2);
@@ -225,7 +226,7 @@ contract LibExchangeRichErrorDecoder is
pure
returns (address assetProxyAddress)
{
_assertSelectorBytes(encoded, ASSET_PROXY_EXISTS_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.AssetProxyExistsErrorSelector());
assetProxyAddress = _readErrorParameterAsAddress(encoded, 0);
}
@@ -243,7 +244,7 @@ contract LibExchangeRichErrorDecoder is
bytes memory assetData
)
{
_assertSelectorBytes(encoded, ASSET_PROXY_DISPATCH_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.AssetProxyDispatchErrorSelector());
errorCode = AssetProxyDispatchErrorCodes(_readErrorParameterAsUint256(encoded, 0));
orderHash = _readErrorParameterAsBytes32(encoded, 1);
assetData = _readErrorParameterAsBytes(encoded, 2);
@@ -263,7 +264,7 @@ contract LibExchangeRichErrorDecoder is
bytes memory errorData
)
{
_assertSelectorBytes(encoded, ASSET_PROXY_TRANSFER_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.AssetProxyTransferErrorSelector());
orderHash = _readErrorParameterAsBytes32(encoded, 0);
assetData = _readErrorParameterAsBytes(encoded, 1);
errorData = _readErrorParameterAsBytes(encoded, 2);
@@ -281,7 +282,7 @@ contract LibExchangeRichErrorDecoder is
bytes32 rightOrderHash
)
{
_assertSelectorBytes(encoded, NEGATIVE_SPREAD_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.NegativeSpreadErrorSelector());
leftOrderHash = _readErrorParameterAsBytes32(encoded, 0);
rightOrderHash = _readErrorParameterAsBytes32(encoded, 1);
}
@@ -298,7 +299,7 @@ contract LibExchangeRichErrorDecoder is
bytes32 transactionHash
)
{
_assertSelectorBytes(encoded, TRANSACTION_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.TransactionErrorSelector());
errorCode = TransactionErrorCodes(_readErrorParameterAsUint256(encoded, 0));
transactionHash = _readErrorParameterAsBytes32(encoded, 1);
}
@@ -317,7 +318,7 @@ contract LibExchangeRichErrorDecoder is
bytes memory signature
)
{
_assertSelectorBytes(encoded, TRANSACTION_SIGNATURE_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.TransactionSignatureErrorSelector());
transactionHash = _readErrorParameterAsBytes32(encoded, 0);
signerAddress = _readErrorParameterAsAddress(encoded, 1);
signature = _readErrorParameterAsBytes(encoded, 2);
@@ -335,7 +336,7 @@ contract LibExchangeRichErrorDecoder is
bytes memory errorData
)
{
_assertSelectorBytes(encoded, TRANSACTION_EXECUTION_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.TransactionExecutionErrorSelector());
transactionHash = _readErrorParameterAsBytes32(encoded, 0);
errorData = _readErrorParameterAsBytes(encoded, 1);
}
@@ -350,7 +351,7 @@ contract LibExchangeRichErrorDecoder is
bytes32 orderHash
)
{
_assertSelectorBytes(encoded, INCOMPLETE_FILL_ERROR_SELECTOR);
_assertSelectorBytes(encoded, LibExchangeRichErrors.IncompleteFillErrorSelector());
orderHash = _readErrorParameterAsBytes32(encoded, 0);
}