This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
BoundedStepwiseLogarithmicPriceAdapter.sol
197 lines (181 loc) · 7.29 KB
/
BoundedStepwiseLogarithmicPriceAdapter.sol
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.17;
import { FixedPointMathLib } from "solady/src/utils/FixedPointMathLib.sol";
/**
* @title BoundedStepwiseLogarithmicPriceAdapter
* @author Index Coop
* @notice Price adapter contract for the AuctionRebalanceModuleV1. It returns a price that
* increases or decreases logarithmically in steps over time, within a bounded range.
* The rate of change is decreasing.
* Price formula: price = initialPrice +/- scalingFactor * ln(timeCoefficient * timeBucket)
*/
contract BoundedStepwiseLogarithmicPriceAdapter {
using FixedPointMathLib for int256;
int256 private constant WAD = 1e18; // Equivalent to PreciseUnitMath.preciseUnit()
int256 private constant MAX_LOG_ARG = 2 ** 254 - 1; // To protect against overflow when adding WAD
/**
* @dev Calculates and returns the logarithmic price.
*
* @param _timeElapsed Time elapsed since the start of the auction.
* @param _priceAdapterConfigData Encoded bytes representing the logarithmic function parameters.
*
* @return price The price calculated using the logarithmic function.
*/
function getPrice(
address /* _setToken */,
address /* _component */,
uint256 /* _componentQuantity */,
uint256 _timeElapsed,
uint256 /* _duration */,
bytes memory _priceAdapterConfigData
)
external
pure
returns (uint256 price)
{
(
uint256 initialPrice,
uint256 scalingFactor,
uint256 timeCoefficient,
uint256 bucketSize,
bool isDecreasing,
uint256 maxPrice,
uint256 minPrice
) = getDecodedData(_priceAdapterConfigData);
require(
areParamsValid(initialPrice, scalingFactor, timeCoefficient, bucketSize, maxPrice, minPrice),
"BoundedStepwiseLogarithmicPriceAdapter: Invalid params"
);
uint256 timeBucket = _timeElapsed / bucketSize;
// Protect against logarithmic argument overflow
if (timeBucket > type(uint256).max / timeCoefficient) {
return _getBoundaryPrice(isDecreasing, maxPrice, minPrice);
}
int256 lnArgument = int256(timeBucket * timeCoefficient);
// Protect against logarithmic overflow and increasing relative error
if (lnArgument > MAX_LOG_ARG) {
return _getBoundaryPrice(isDecreasing, maxPrice, minPrice);
}
uint256 lnExpression = uint256(FixedPointMathLib.lnWad(lnArgument + WAD));
// Protect against priceChange overflow
if (lnExpression > type(uint256).max / scalingFactor) {
return _getBoundaryPrice(isDecreasing, maxPrice, minPrice);
}
uint256 priceChange = scalingFactor * lnExpression;
if (isDecreasing) {
// Protect against price underflow
if (priceChange > initialPrice) {
return minPrice;
}
return FixedPointMathLib.max(initialPrice - priceChange , minPrice);
} else {
// Protect against price overflow
if (priceChange > type(uint256).max - initialPrice) {
return maxPrice;
}
return FixedPointMathLib.min(initialPrice + priceChange, maxPrice);
}
}
/**
* @dev Returns true if the price adapter is valid for the given parameters.
*
* @param _priceAdapterConfigData Encoded data for configuring the price adapter.
*
* @return isValid Boolean indicating if the adapter config data is valid.
*/
function isPriceAdapterConfigDataValid(
bytes memory _priceAdapterConfigData
)
external
pure
returns (bool isValid)
{
(
uint256 initialPrice,
uint256 scalingFactor,
uint256 timeCoefficient,
uint256 bucketSize,
,
uint256 maxPrice,
uint256 minPrice
) = getDecodedData(_priceAdapterConfigData);
return areParamsValid(initialPrice, scalingFactor, timeCoefficient, bucketSize, maxPrice, minPrice);
}
/**
* @dev Returns true if the price adapter parameters are valid.
*
* @param _initialPrice Initial price of the auction
* @param _scalingFactor Scaling factor for logarithmic expression
* @param _timeCoefficient Scaling factor for logarithmic argument
* @param _bucketSize Time elapsed between each bucket
* @param _maxPrice Maximum price of the auction
* @param _minPrice Minimum price of the auction
*/
function areParamsValid(
uint256 _initialPrice,
uint256 _scalingFactor,
uint256 _timeCoefficient,
uint256 _bucketSize,
uint256 _maxPrice,
uint256 _minPrice
)
public
pure
returns (bool)
{
return _initialPrice > 0
&& _scalingFactor > 0
&& _timeCoefficient > 0
&& _bucketSize > 0
&& _initialPrice <= _maxPrice
&& _initialPrice >= _minPrice;
}
/**
* @dev Returns the encoded data for the price curve parameters
*
* @param _initialPrice Initial price of the auction
* @param _scalingFactor Scaling factor for logarithmic expression
* @param _timeCoefficient Scaling factor for logarithmic argument
* @param _bucketSize Time elapsed between each bucket
* @param _isDecreasing Flag for whether the price is decreasing or increasing
* @param _maxPrice Maximum price of the auction
* @param _minPrice Minimum price of the auction
*/
function getEncodedData(
uint256 _initialPrice,
uint256 _scalingFactor,
uint256 _timeCoefficient,
uint256 _bucketSize,
bool _isDecreasing,
uint256 _maxPrice,
uint256 _minPrice
)
external
pure
returns (bytes memory data)
{
return abi.encode(_initialPrice, _scalingFactor, _timeCoefficient, _bucketSize, _isDecreasing, _maxPrice, _minPrice);
}
/**
* @dev Decodes the parameters from the provided bytes.
*
* @param _data Bytes encoded auction parameters
* @return initialPrice Initial price of the auction
* @return scalingFactor Scaling factor for logarithmic expression
* @return timeCoefficient Scaling factor for logarithmic argument
* @return bucketSize Time elapsed between each bucket
* @return isDecreasing Flag for whether the price is decreasing or increasing
* @return maxPrice Maximum price of the auction
* @return minPrice Minimum price of the auction
*/
function getDecodedData(bytes memory _data)
public
pure
returns (uint256 initialPrice, uint256 scalingFactor, uint256 timeCoefficient, uint256 bucketSize, bool isDecreasing, uint256 maxPrice, uint256 minPrice)
{
return abi.decode(_data, (uint256, uint256, uint256, uint256, bool, uint256, uint256));
}
function _getBoundaryPrice(bool isDecreasing, uint256 maxPrice, uint256 minPrice) private pure returns (uint256) {
return isDecreasing ? minPrice : maxPrice;
}
}