Skip to content

Commit d9ccbed

Browse files
authored
Add trade routing to FlexibleLeverageStrategyExtension (#59)
* add multiple exchange support for FLI rebalancing * add tests for adding/removing/updating exchange settings * add getTotalRebalanceNotional * fix integration tests * removed unneeded test * improve javadocs * use multiple exchanges in FLI integration tests * use list of exchanges in constructor * use StringArrayUtils for validations * improve timestamp validations * emit events for adding/updating/removing exchanges * improve comments * make removeEnabledExchange more efficient * fix integration tests * add tests using multiple exchanges * add tests for StringArrayUtils * improve coverage * clean up * update getTotalRebalanceNotional * fix comment * update changelog * improve tests * improve comments * fix timestamps accounting * fix variable name * allow exchange updates during rebalances * update getChunkRebalanceNotional * clean up * refactor getChunkRebalanceNotional * change return values of getChunkRebalanceNotional * change name of adapter to extension * update changelog * review changes * fix IFLIStrategyExtension interface
1 parent 43b7d6a commit d9ccbed

File tree

13 files changed

+1990
-571
lines changed

13 files changed

+1990
-571
lines changed

contracts/adapters/FlexibleLeverageStrategyAdapter.sol renamed to contracts/adapters/FlexibleLeverageStrategyExtension.sol

Lines changed: 360 additions & 105 deletions
Large diffs are not rendered by default.

contracts/interfaces/IFLIStrategyAdapter.sol renamed to contracts/interfaces/IFLIStrategyExtension.sol

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,29 @@
1717
pragma solidity 0.6.10;
1818
pragma experimental "ABIEncoderV2";
1919

20-
import { FlexibleLeverageStrategyAdapter } from "../adapters/FlexibleLeverageStrategyAdapter.sol";
20+
import { FlexibleLeverageStrategyExtension } from "../adapters/FlexibleLeverageStrategyExtension.sol";
2121

22-
interface IFLIStrategyAdapter {
23-
function getStrategy() external view returns (FlexibleLeverageStrategyAdapter.ContractSettings memory);
24-
function getMethodology() external view returns (FlexibleLeverageStrategyAdapter.MethodologySettings memory);
25-
function getIncentive() external view returns (FlexibleLeverageStrategyAdapter.IncentiveSettings memory);
26-
function getExecution() external view returns (FlexibleLeverageStrategyAdapter.ExecutionSettings memory);
22+
interface IFLIStrategyExtension {
23+
function getStrategy() external view returns (FlexibleLeverageStrategyExtension.ContractSettings memory);
24+
function getMethodology() external view returns (FlexibleLeverageStrategyExtension.MethodologySettings memory);
25+
function getIncentive() external view returns (FlexibleLeverageStrategyExtension.IncentiveSettings memory);
26+
function getExecution() external view returns (FlexibleLeverageStrategyExtension.ExecutionSettings memory);
2727

2828
function getCurrentLeverageRatio() external view returns (uint256);
2929

30-
function shouldRebalance() external view returns (FlexibleLeverageStrategyAdapter.ShouldRebalance);
30+
function getChunkRebalanceNotional(
31+
string[] calldata _exchangeNames
32+
)
33+
external
34+
view
35+
returns(uint256[] memory sizes, address sellAsset, address buyAsset);
36+
37+
function shouldRebalance() external view returns(string[] memory, FlexibleLeverageStrategyExtension.ShouldRebalance[] memory);
3138
function shouldRebalanceWithBounds(
3239
uint256 _customMinLeverageRatio,
3340
uint256 _customMaxLeverageRatio
3441
)
3542
external
3643
view
37-
returns(FlexibleLeverageStrategyAdapter.ShouldRebalance);
44+
returns(string[] memory, FlexibleLeverageStrategyExtension.ShouldRebalance[] memory);
3845
}

contracts/lib/StringArrayUtils.sol

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2021 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+
pragma solidity 0.6.10;
20+
21+
/**
22+
* @title StringArrayUtils
23+
* @author Set Protocol
24+
*
25+
* Utility functions to handle String Arrays
26+
*/
27+
library StringArrayUtils {
28+
29+
/**
30+
* Finds the index of the first occurrence of the given element.
31+
* @param A The input string to search
32+
* @param a The value to find
33+
* @return Returns (index and isIn) for the first occurrence starting from index 0
34+
*/
35+
function indexOf(string[] memory A, string memory a) internal pure returns (uint256, bool) {
36+
uint256 length = A.length;
37+
for (uint256 i = 0; i < length; i++) {
38+
if (keccak256(bytes(A[i])) == keccak256(bytes(a))) {
39+
return (i, true);
40+
}
41+
}
42+
return (uint256(-1), false);
43+
}
44+
45+
/**
46+
* @param A The input array to search
47+
* @param a The string to remove
48+
*/
49+
function removeStorage(string[] storage A, string memory a)
50+
internal
51+
{
52+
(uint256 index, bool isIn) = indexOf(A, a);
53+
if (!isIn) {
54+
revert("String not in array.");
55+
} else {
56+
uint256 lastIndex = A.length - 1; // If the array would be empty, the previous line would throw, so no underflow here
57+
if (index != lastIndex) { A[index] = A[lastIndex]; }
58+
A.pop();
59+
}
60+
}
61+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2021 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+
pragma solidity 0.6.10;
20+
pragma experimental "ABIEncoderV2";
21+
22+
import { StringArrayUtils } from "../lib/StringArrayUtils.sol";
23+
24+
25+
contract StringArrayUtilsMock {
26+
using StringArrayUtils for string[];
27+
28+
string[] public storageArray;
29+
30+
function testIndexOf(string[] memory A, string memory a) external pure returns (uint256, bool) {
31+
return A.indexOf(a);
32+
}
33+
34+
function testRemoveStorage(string memory a) external {
35+
storageArray.removeStorage(a);
36+
}
37+
38+
function setStorageArray(string[] memory A) external {
39+
storageArray = A;
40+
}
41+
42+
function getStorageArray() external view returns(string[] memory) {
43+
return storageArray;
44+
}
45+
}

0 commit comments

Comments
 (0)