• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IndexCoop / index-protocol / 3386a5b4-1090-4a99-af92-0eee2a8178c4

05 Feb 2025 06:12AM UTC coverage: 90.86% (-5.1%) from 95.955%
3386a5b4-1090-4a99-af92-0eee2a8178c4

push

circleci

ckoopmann
feat: Aero Slipstream Exchange Adapter

2178 of 2520 branches covered (86.43%)

Branch coverage included in aggregate %.

0 of 11 new or added lines in 1 file covered. (0.0%)

127 existing lines in 10 files now uncovered.

3429 of 3651 relevant lines covered (93.92%)

204.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.01
/contracts/lib/AddressArrayUtils.sol
1
/*
2
    Copyright 2020 Set Labs Inc.
3

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

8
    http://www.apache.org/licenses/LICENSE-2.0
9

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

16
    SPDX-License-Identifier: Apache License, Version 2.0
17

18
*/
19

20
pragma solidity 0.6.10;
21

22
/**
23
 * @title AddressArrayUtils
24
 * @author Set Protocol
25
 *
26
 * Utility functions to handle Address Arrays
27
 *
28
 * CHANGELOG:
29
 * - 4/21/21: Added validatePairsWithArray methods
30
 */
31
library AddressArrayUtils {
32

33
    /**
34
     * Finds the index of the first occurrence of the given element.
35
     * @param A The input array to search
36
     * @param a The value to find
37
     * @return Returns (index and isIn) for the first occurrence starting from index 0
38
     */
39
    function indexOf(address[] memory A, address a) internal pure returns (uint256, bool) {
40
        uint256 length = A.length;
5,138✔
41
        for (uint256 i = 0; i < length; i++) {
5,138✔
42
            if (A[i] == a) {
7,026✔
43
                return (i, true);
2,631✔
44
            }
45
        }
46
        return (uint256(-1), false);
2,507✔
47
    }
48

49
    /**
50
    * Returns true if the value is present in the list. Uses indexOf internally.
51
    * @param A The input array to search
52
    * @param a The value to find
53
    * @return Returns isIn for the first occurrence starting from index 0
54
    */
55
    function contains(address[] memory A, address a) internal pure returns (bool) {
56
        (, bool isIn) = indexOf(A, a);
4,291✔
57
        return isIn;
4,291✔
58
    }
59

60
    /**
61
    * Returns true if there are 2 elements that are the same in an array
62
    * @param A The input array to search
63
    * @return Returns boolean for the first occurrence of a duplicate
64
    */
65
    function hasDuplicate(address[] memory A) internal pure returns(bool) {
66
        require(A.length > 0, "A is empty");
1,585!
67

68
        for (uint256 i = 0; i < A.length - 1; i++) {
1,585✔
69
            address current = A[i];
747✔
70
            for (uint256 j = i + 1; j < A.length; j++) {
747✔
71
                if (current == A[j]) {
1,096✔
72
                    return true;
13✔
73
                }
74
            }
75
        }
76
        return false;
1,572✔
77
    }
78

79
    /**
80
     * @param A The input array to search
81
     * @param a The address to remove
82
     * @return Returns the array with the object removed.
83
     */
84
    function remove(address[] memory A, address a)
85
        internal
86
        pure
87
        returns (address[] memory)
88
    {
89
        (uint256 index, bool isIn) = indexOf(A, a);
80✔
90
        if (!isIn) {
80!
UNCOV
91
            revert("Address not in array.");
×
92
        } else {
93
            (address[] memory _A,) = pop(A, index);
80✔
94
            return _A;
80✔
95
        }
96
    }
97

98
    /**
99
     * @param A The input array to search
100
     * @param a The address to remove
101
     */
102
    function removeStorage(address[] storage A, address a)
103
        internal
104
    {
105
        (uint256 index, bool isIn) = indexOf(A, a);
752✔
106
        if (!isIn) {
752!
UNCOV
107
            revert("Address not in array.");
×
108
        } else {
109
            uint256 lastIndex = A.length - 1; // If the array would be empty, the previous line would throw, so no underflow here
752✔
110
            if (index != lastIndex) { A[index] = A[lastIndex]; }
752✔
111
            A.pop();
752✔
112
        }
113
    }
114

115
    /**
116
    * Removes specified index from array
117
    * @param A The input array to search
118
    * @param index The index to remove
119
    * @return Returns the new array and the removed entry
120
    */
121
    function pop(address[] memory A, uint256 index)
122
        internal
123
        pure
124
        returns (address[] memory, address)
125
    {
126
        uint256 length = A.length;
80✔
127
        require(index < A.length, "Index must be < A length");
80!
128
        address[] memory newAddresses = new address[](length - 1);
80✔
129
        for (uint256 i = 0; i < index; i++) {
80✔
130
            newAddresses[i] = A[i];
131
        }
132
        for (uint256 j = index + 1; j < length; j++) {
80✔
133
            newAddresses[j - 1] = A[j];
134
        }
135
        return (newAddresses, A[index]);
80✔
136
    }
137

138
    /**
139
     * Returns the combination of the two arrays
140
     * @param A The first array
141
     * @param B The second array
142
     * @return Returns A extended by B
143
     */
144
    function extend(address[] memory A, address[] memory B) internal pure returns (address[] memory) {
145
        uint256 aLength = A.length;
180✔
146
        uint256 bLength = B.length;
180✔
147
        address[] memory newAddresses = new address[](aLength + bLength);
180✔
148
        for (uint256 i = 0; i < aLength; i++) {
180✔
149
            newAddresses[i] = A[i];
150
        }
151
        for (uint256 j = 0; j < bLength; j++) {
180✔
152
            newAddresses[aLength + j] = B[j];
153
        }
154
        return newAddresses;
180✔
155
    }
156

157
    /**
158
     * Validate that address and uint array lengths match. Validate address array is not empty
159
     * and contains no duplicate elements.
160
     *
161
     * @param A         Array of addresses
162
     * @param B         Array of uint
163
     */
164
    function validatePairsWithArray(address[] memory A, uint[] memory B) internal pure {
165
        require(A.length == B.length, "Array length mismatch");
19✔
166
        _validateLengthAndUniqueness(A);
18✔
167
    }
168

169
    /**
170
     * Validate that address and bool array lengths match. Validate address array is not empty
171
     * and contains no duplicate elements.
172
     *
173
     * @param A         Array of addresses
174
     * @param B         Array of bool
175
     */
176
    function validatePairsWithArray(address[] memory A, bool[] memory B) internal pure {
177
        require(A.length == B.length, "Array length mismatch");
40✔
178
        _validateLengthAndUniqueness(A);
38✔
179
    }
180

181
    /**
182
     * Validate that address and string array lengths match. Validate address array is not empty
183
     * and contains no duplicate elements.
184
     *
185
     * @param A         Array of addresses
186
     * @param B         Array of strings
187
     */
188
    function validatePairsWithArray(address[] memory A, string[] memory B) internal pure {
189
        require(A.length == B.length, "Array length mismatch");
22✔
190
        _validateLengthAndUniqueness(A);
21✔
191
    }
192

193
    /**
194
     * Validate that address array lengths match, and calling address array are not empty
195
     * and contain no duplicate elements.
196
     *
197
     * @param A         Array of addresses
198
     * @param B         Array of addresses
199
     */
200
    function validatePairsWithArray(address[] memory A, address[] memory B) internal pure {
UNCOV
201
        require(A.length == B.length, "Array length mismatch");
×
UNCOV
202
        _validateLengthAndUniqueness(A);
×
203
    }
204

205
    /**
206
     * Validate that address and bytes array lengths match. Validate address array is not empty
207
     * and contains no duplicate elements.
208
     *
209
     * @param A         Array of addresses
210
     * @param B         Array of bytes
211
     */
212
    function validatePairsWithArray(address[] memory A, bytes[] memory B) internal pure {
213
        require(A.length == B.length, "Array length mismatch");
12✔
214
        _validateLengthAndUniqueness(A);
11✔
215
    }
216

217
    /**
218
     * Validate address array is not empty and contains no duplicate elements.
219
     *
220
     * @param A          Array of addresses
221
     */
222
    function _validateLengthAndUniqueness(address[] memory A) internal pure {
223
        require(A.length > 0, "Array length must be > 0");
88✔
224
        require(!hasDuplicate(A), "Cannot duplicate addresses");
83✔
225
    }
226
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc