/
Sun.sol
76 lines (62 loc) · 2.07 KB
/
Sun.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
/**
* SPDX-License-Identifier: MIT
**/
pragma solidity ^0.7.6;
pragma experimental ABIEncoderV2;
import "./Weather.sol";
/**
* @author Publius
* @title Sun
**/
contract Sun is Weather {
using SafeMath for uint256;
using Decimal for Decimal.D256;
event SupplyIncrease(
uint256 indexed season,
uint256 price,
uint256 newHarvestable,
uint256 newSilo,
int256 newSoil
);
event SupplyDecrease(uint256 indexed season, uint256 price, int256 newSoil);
event SupplyNeutral(uint256 indexed season, int256 newSoil);
/**
* Internal
**/
// Sun
function stepSun(Decimal.D256 memory beanPrice, Decimal.D256 memory usdcPrice)
internal
returns
(uint256)
{
(uint256 eth_reserve, uint256 bean_reserve) = reserves();
uint256 currentBeans = sqrt(
bean_reserve.mul(eth_reserve).mul(1e6).div(beanPrice.mul(1e18).asUint256())
);
uint256 targetBeans = sqrt(
bean_reserve.mul(eth_reserve).mul(1e6).div(usdcPrice.mul(1e18).asUint256())
);
uint256 price = beanPrice.mul(1e18).div(usdcPrice).asUint256();
uint256 newSilo;
if (currentBeans < targetBeans) {
newSilo = growSupply(targetBeans.sub(currentBeans), price);
} else if (currentBeans > targetBeans) {
shrinkSupply(currentBeans.sub(targetBeans), price);
} else {
int256 newSoil = ensureSoilBounds();
emit SupplyNeutral(season(), newSoil);
}
s.w.startSoil = s.f.soil;
return newSilo;
}
function shrinkSupply(uint256 beans, uint256 price) private {
int256 newSoil = increaseSoil(beans);
emit SupplyDecrease(season(), price, newSoil);
}
function growSupply(uint256 beans, uint256 price) private returns (uint256) {
(uint256 newHarvestable, uint256 newSilo) = increaseSupply(beans);
int256 newSoil = decreaseSoil(beans, newHarvestable);
emit SupplyIncrease(season(), price, newHarvestable, newSilo, newSoil);
return newSilo;
}
}