-
Notifications
You must be signed in to change notification settings - Fork 3
/
LendMath.sol
77 lines (70 loc) · 2.23 KB
/
LendMath.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
// SPDX-License-Identifier: MIT
pragma solidity =0.8.4;
import {IPair} from '../interfaces/IPair.sol';
import {FullMath} from './FullMath.sol';
import {ConstantProduct} from './ConstantProduct.sol';
import {SafeCast} from './SafeCast.sol';
library LendMath {
using FullMath for uint256;
using ConstantProduct for IPair.State;
using SafeCast for uint256;
function check(
IPair.State memory state,
uint112 xIncrease,
uint112 yDecrease,
uint112 zDecrease,
uint16 fee
) internal pure {
uint128 feeBase = 0x10000 + fee;
uint112 xReserve = state.x + xIncrease;
uint128 yAdjusted = adjust(state.y, yDecrease, feeBase);
uint128 zAdjusted = adjust(state.z, zDecrease, feeBase);
state.checkConstantProduct(xReserve, yAdjusted, zAdjusted);
uint256 minimum = xIncrease;
minimum *= state.y;
minimum <<= 12;
uint256 denominator = xReserve;
denominator *= feeBase;
minimum /= denominator;
require(yDecrease >= minimum, 'E302');
}
function adjust(
uint112 reserve,
uint112 decrease,
uint128 feeBase
) private pure returns (uint128 adjusted) {
adjusted = reserve;
adjusted <<= 16;
adjusted -= feeBase * decrease;
}
function getBond(
uint256 maturity,
uint112 xIncrease,
uint112 yDecrease
) internal view returns (uint128 bondOut) {
uint256 _bondOut = maturity;
_bondOut -= block.timestamp;
_bondOut *= yDecrease;
_bondOut >>= 32;
_bondOut += xIncrease;
bondOut = _bondOut.toUint128();
}
function getInsurance(
uint256 maturity,
IPair.State memory state,
uint112 xIncrease,
uint112 zDecrease
) internal view returns (uint128 insuranceOut) {
uint256 _insuranceOut = maturity;
_insuranceOut -= block.timestamp;
_insuranceOut *= zDecrease;
_insuranceOut >>= 25;
uint256 minimum = state.z;
minimum *= xIncrease;
uint256 denominator = state.x;
denominator += xIncrease;
minimum /= denominator;
_insuranceOut += minimum;
insuranceOut = _insuranceOut.toUint128();
}
}