aboutsummaryrefslogtreecommitdiffstats
path: root/contracts/extensions/contracts/src/BalanceThresholdFilter/MixinBalanceThresholdFilterCore.sol
blob: 68dabafebad06eb5e9260950e9dd0e08b48594be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*

  Copyright 2018 ZeroEx Intl.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

*/

pragma solidity ^0.4.24;

import "@0x/contracts-exchange-libs/contracts/src/LibExchangeSelectors.sol";
import "@0x/contracts-exchange-libs/contracts/src/LibOrder.sol";
import "./mixins/MBalanceThresholdFilterCore.sol";
import "./MixinExchangeCalldata.sol";


contract MixinBalanceThresholdFilterCore is
    MBalanceThresholdFilterCore,
    MixinExchangeCalldata,
    LibOrder,
    LibExchangeSelectors
{

    /// @dev Executes an Exchange transaction iff the maker and taker meet 
    ///      the hold at least `BALANCE_THRESHOLD` of the asset `THRESHOLD_ASSET` OR 
    ///      the exchange function is a cancellation.
    ///      Supported Exchange functions:
    ///          batchFillOrders
    ///          batchFillOrdersNoThrow
    ///          batchFillOrKillOrders
    ///          fillOrder
    ///          fillOrderNoThrow
    ///          fillOrKillOrder
    ///          marketBuyOrders
    ///          marketBuyOrdersNoThrow
    ///          marketSellOrders
    ///          marketSellOrdersNoThrow
    ///          matchOrders
    ///          cancelOrder
    ///          batchCancelOrders
    ///          cancelOrdersUpTo
    ///     Trying to call any other exchange function will throw.
    /// @param salt Arbitrary number to ensure uniqueness of transaction hash.
    /// @param signerAddress Address of transaction signer.
    /// @param signedExchangeTransaction AbiV2 encoded calldata.
    /// @param signature Proof of signer transaction by signer.
    function executeTransaction(
        uint256 salt,
        address signerAddress,
        bytes signedExchangeTransaction,
        bytes signature
    ) 
        external
    {
        // Get accounts whose balances must be validated
        address[] memory addressesToValidate = getAddressesToValidate(signerAddress);

        // Validate account balances
        uint256 balanceThreshold = BALANCE_THRESHOLD;
        IThresholdAsset thresholdAsset = THRESHOLD_ASSET;
        for (uint256 i = 0; i < addressesToValidate.length; ++i) {
            uint256 addressBalance = thresholdAsset.balanceOf(addressesToValidate[i]);
            require(
                addressBalance >= balanceThreshold,
                "AT_LEAST_ONE_ADDRESS_DOES_NOT_MEET_BALANCE_THRESHOLD"
            );
        }
        emit ValidatedAddresses(addressesToValidate);
        
        // All addresses are valid. Execute exchange function.
        EXCHANGE.executeTransaction(
            salt,
            signerAddress,
            signedExchangeTransaction,
            signature
        );
    }

    /// @dev Constructs an array of addresses to be validated.
    ///      Addresses depend on which Exchange function is to be called
    ///      (defined by `signedExchangeTransaction` above).
    /// @param signerAddress Address of transaction signer.
    /// @return addressesToValidate Array of addresses to validate.
    function getAddressesToValidate(address signerAddress)
        internal pure
        returns (address[] memory addressesToValidate)
    {
        bytes4 exchangeFunctionSelector = bytes4(exchangeCalldataload(0));
        // solhint-disable expression-indent
        if (
            exchangeFunctionSelector == BATCH_FILL_ORDERS_SELECTOR              ||
            exchangeFunctionSelector == BATCH_FILL_ORDERS_NO_THROW_SELECTOR     ||
            exchangeFunctionSelector == BATCH_FILL_OR_KILL_ORDERS_SELECTOR      ||
            exchangeFunctionSelector == MARKET_BUY_ORDERS_SELECTOR              ||
            exchangeFunctionSelector == MARKET_BUY_ORDERS_NO_THROW_SELECTOR     ||
            exchangeFunctionSelector == MARKET_SELL_ORDERS_SELECTOR             ||
            exchangeFunctionSelector == MARKET_SELL_ORDERS_NO_THROW_SELECTOR
        ) {
            addressesToValidate = loadMakerAddressesFromOrderArray(0);
            addressesToValidate = addressesToValidate.append(signerAddress);
        } else if (
            exchangeFunctionSelector == FILL_ORDER_SELECTOR             ||
            exchangeFunctionSelector == FILL_ORDER_NO_THROW_SELECTOR    ||
            exchangeFunctionSelector == FILL_OR_KILL_ORDER_SELECTOR
        ) {
            address makerAddress = loadMakerAddressFromOrder(0);
            addressesToValidate = addressesToValidate.append(makerAddress);
            addressesToValidate = addressesToValidate.append(signerAddress);
        } else if (exchangeFunctionSelector == MATCH_ORDERS_SELECTOR) {
            address leftMakerAddress = loadMakerAddressFromOrder(0);
            addressesToValidate = addressesToValidate.append(leftMakerAddress);
            address rightMakerAddress = loadMakerAddressFromOrder(1);
            addressesToValidate = addressesToValidate.append(rightMakerAddress);
            addressesToValidate = addressesToValidate.append(signerAddress);
        } else if (
            exchangeFunctionSelector != CANCEL_ORDER_SELECTOR           &&
            exchangeFunctionSelector != BATCH_CANCEL_ORDERS_SELECTOR    &&
            exchangeFunctionSelector != CANCEL_ORDERS_UP_TO_SELECTOR
        ) {
            revert("INVALID_OR_BLOCKED_EXCHANGE_SELECTOR");
        }
        // solhint-enable expression-indent
        return addressesToValidate;
    }
}