forked from Synthetixio/synthetix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollateralManagerState.sol
146 lines (115 loc) · 4.37 KB
/
CollateralManagerState.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
pragma solidity ^0.5.16;
pragma experimental ABIEncoderV2;
// Inheritance
import "./Owned.sol";
import "./State.sol";
// Libraries
import "./SafeDecimalMath.sol";
contract CollateralManagerState is Owned, State {
using SafeMath for uint;
using SafeDecimalMath for uint;
struct Balance {
uint long;
uint short;
}
uint public totalLoans;
uint[] public borrowRates;
uint public borrowRatesLastUpdated;
mapping(bytes32 => uint[]) public shortRates;
mapping(bytes32 => uint) public shortRatesLastUpdated;
// The total amount of long and short for a synth,
mapping(bytes32 => Balance) public totalIssuedSynths;
constructor(address _owner, address _associatedContract) public Owned(_owner) State(_associatedContract) {
borrowRates.push(0);
borrowRatesLastUpdated = block.timestamp;
}
function incrementTotalLoans() external onlyAssociatedContract returns (uint) {
totalLoans = totalLoans.add(1);
return totalLoans;
}
function long(bytes32 synth) external view onlyAssociatedContract returns (uint) {
return totalIssuedSynths[synth].long;
}
function short(bytes32 synth) external view onlyAssociatedContract returns (uint) {
return totalIssuedSynths[synth].short;
}
function incrementLongs(bytes32 synth, uint256 amount) external onlyAssociatedContract {
totalIssuedSynths[synth].long = totalIssuedSynths[synth].long.add(amount);
}
function decrementLongs(bytes32 synth, uint256 amount) external onlyAssociatedContract {
totalIssuedSynths[synth].long = totalIssuedSynths[synth].long.sub(amount);
}
function incrementShorts(bytes32 synth, uint256 amount) external onlyAssociatedContract {
totalIssuedSynths[synth].short = totalIssuedSynths[synth].short.add(amount);
}
function decrementShorts(bytes32 synth, uint256 amount) external onlyAssociatedContract {
totalIssuedSynths[synth].short = totalIssuedSynths[synth].short.sub(amount);
}
// Borrow rates, one array here for all currencies.
function getRateAt(uint index) public view returns (uint) {
return borrowRates[index];
}
function getRatesLength() public view returns (uint) {
return borrowRates.length;
}
function updateBorrowRates(uint rate) external onlyAssociatedContract {
borrowRates.push(rate);
borrowRatesLastUpdated = block.timestamp;
}
function ratesLastUpdated() public view returns (uint) {
return borrowRatesLastUpdated;
}
function getRatesAndTime(uint index)
external
view
returns (
uint entryRate,
uint lastRate,
uint lastUpdated,
uint newIndex
)
{
newIndex = getRatesLength();
entryRate = getRateAt(index);
lastRate = getRateAt(newIndex - 1);
lastUpdated = ratesLastUpdated();
}
// Short rates, one array per currency.
function addShortCurrency(bytes32 currency) external onlyAssociatedContract {
if (shortRates[currency].length > 0) {} else {
shortRates[currency].push(0);
shortRatesLastUpdated[currency] = block.timestamp;
}
}
function removeShortCurrency(bytes32 currency) external onlyAssociatedContract {
delete shortRates[currency];
}
function getShortRateAt(bytes32 currency, uint index) internal view returns (uint) {
return shortRates[currency][index];
}
function getShortRatesLength(bytes32 currency) public view returns (uint) {
return shortRates[currency].length;
}
function updateShortRates(bytes32 currency, uint rate) external onlyAssociatedContract {
shortRates[currency].push(rate);
shortRatesLastUpdated[currency] = block.timestamp;
}
function shortRateLastUpdated(bytes32 currency) internal view returns (uint) {
return shortRatesLastUpdated[currency];
}
function getShortRatesAndTime(bytes32 currency, uint index)
external
view
returns (
uint entryRate,
uint lastRate,
uint lastUpdated,
uint newIndex
)
{
newIndex = getShortRatesLength(currency);
entryRate = getShortRateAt(currency, index);
lastRate = getShortRateAt(currency, newIndex - 1);
lastUpdated = shortRateLastUpdated(currency);
}
}