-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCurveMath18.sol
56 lines (51 loc) · 1.94 KB
/
CurveMath18.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
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;
import "../number/types/UFixed18.sol";
import "../number/types/Fixed18.sol";
/**
* @title CurveMath18
* @notice Library for managing math operations for utilization curves.
*/
library CurveMath18 {
error CurveMath18OutOfBoundsError();
/**
* @notice Computes a linear interpolation between two points
* @param startX First point's x-coordinate
* @param startY First point's y-coordinate
* @param endX Second point's x-coordinate
* @param endY Second point's y-coordinate
* @param targetX x-coordinate to interpolate
* @return y-coordinate for `targetX` along the line from (`startX`, `startY`) -> (`endX`, `endY`)
*/
function linearInterpolation(
UFixed18 startX,
Fixed18 startY,
UFixed18 endX,
Fixed18 endY,
UFixed18 targetX
) internal pure returns (Fixed18) {
if (targetX.lt(startX) || targetX.gt(endX)) revert CurveMath18OutOfBoundsError();
UFixed18 xRange = endX.sub(startX);
Fixed18 yRange = endY.sub(startY);
UFixed18 xRatio = targetX.sub(startX).div(xRange);
return yRange.mul(Fixed18Lib.from(xRatio)).add(startY);
}
/**
* @notice Computes a linear interpolation between two points
* @param startX First point's x-coordinate
* @param startY First point's y-coordinate
* @param endX Second point's x-coordinate
* @param endY Second point's y-coordinate
* @param targetX x-coordinate to interpolate
* @return y-coordinate for `targetX` along the line from (`startX`, `startY`) -> (`endX`, `endY`)
*/
function linearInterpolation(
UFixed18 startX,
UFixed18 startY,
UFixed18 endX,
UFixed18 endY,
UFixed18 targetX
) internal pure returns (UFixed18) {
return UFixed18Lib.from(linearInterpolation(startX, Fixed18Lib.from(startY), endX, Fixed18Lib.from(endY), targetX));
}
}