-
Notifications
You must be signed in to change notification settings - Fork 2
/
FinancialSwapManager.sol
244 lines (221 loc) · 7.53 KB
/
FinancialSwapManager.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.7;
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
import '@openzeppelin/contracts/access/Ownable.sol';
import '@openzeppelin/contracts/utils/Address.sol';
import '../abstract/MetalSwapAbstract.sol';
contract FinancialSwapManager is Ownable {
string public description;
address public _governance;
uint256 public iV;
uint256 public settlementFeePerc;
uint256 public PRICE_DECIMALS = 1e8;
uint256 public constant mult6 = 1e6;
event SetMainParameters(
uint256 _iV,
uint256 _settlementFeePerc,
address __governance,
string _description,
uint256 _PRICE_DECIMALS
);
event TransferGovernance(address newGovernace);
constructor(
uint256 _iV,
uint256 _settlementFeePerc,
address __governance,
string memory _description,
uint256 _PRICE_DECIMALS
) {
require(
__governance != address(0),
'Error on constructor: input cannot be zero address'
);
settlementFeePerc = _settlementFeePerc;
iV = _iV;
_governance = __governance;
description = _description;
PRICE_DECIMALS = _PRICE_DECIMALS;
}
function calcFees(uint256 period, uint256 targetSize)
public
view
returns (uint256 settlementFee, uint256 periodFee)
{
settlementFee = getSettlementFee(targetSize);
periodFee = getPeriodFee(targetSize, period);
}
function getSettlementFee(uint256 targetSize)
public
view
returns (uint256 fee)
{
return (targetSize * settlementFeePerc) / 100 / 1e6;
}
function getPeriodFee(uint256 targetSize, uint256 period)
internal
view
returns (uint256 fee)
{
return (targetSize * (sqrt(period))) / iV;
}
//profit and loss must be expressed accordingly (currency or asset)
function calcPenalties(
MetalSwapAbstract.SwapType swapType,
uint256 targetSize,
uint256 profit,
uint256 loss,
uint256 cover,
uint256 executionTime,
uint256 currentPrice
) public view returns (uint256 penalties) {
if (executionTime <= block.timestamp) {
return 0;
}
uint256 deltaTime = executionTime - block.timestamp;
uint256 profitConvert;
if (swapType == MetalSwapAbstract.SwapType(0)) {
profitConvert = (profit * PRICE_DECIMALS) / currentPrice;
} else {
profitConvert = (profit * currentPrice) / PRICE_DECIMALS;
}
if (loss == 0 && profitConvert == 0) {
return (targetSize * (sqrt(deltaTime))) / iV;
}
if (loss != 0) {
uint256 lossPercent = (loss * 1e6) / cover;
if (lossPercent > 1e6) {
lossPercent = 1e6;
}
return
(targetSize * (1e6 - lossPercent) * (sqrt(deltaTime))) /
(iV * 1e6);
} else if (profitConvert != 0) {
return
(targetSize *
(profitConvert + cover) *
1e6 *
(sqrt(deltaTime))) / (iV * 1e6 * cover);
}
}
function calcProfitLoss(
MetalSwapAbstract.SwapType swapType,
uint256 currentPrice,
uint256 initPrice,
uint256 targetSize
) public view returns (uint256 profit, uint256 loss) {
profit = 0;
int256 priceDifference = int256(currentPrice) - int256(initPrice);
if (swapType == MetalSwapAbstract.SwapType(0)) {
if (priceDifference > 0) {
loss = (targetSize * uint256(priceDifference)) / currentPrice;
return (0, loss);
}
profit = (uint256(-priceDifference) * targetSize) / PRICE_DECIMALS;
return (profit, 0);
}
if (swapType == MetalSwapAbstract.SwapType(1)) {
if (priceDifference < 0) {
loss = (targetSize * uint256(-priceDifference)) / initPrice;
return (0, loss);
}
profit =
(uint256(priceDifference) * targetSize * PRICE_DECIMALS) /
(currentPrice * initPrice);
return (profit, 0);
}
}
function getReward(
uint256 periodFee,
MetalSwapAbstract.SwapType swapType,
uint256 swapAvgAsset,
uint256 swapAvgCurrency,
uint256 rateReward,
uint256 currentPrice
) public view returns (uint256) {
uint256 BONUS_PERC; //bonus multiplier with 6 decimal places, 0-100% = [0-100*10^6]
if (swapAvgAsset != 0 || swapAvgCurrency != 0) {
if (swapType == MetalSwapAbstract.SwapType(0)) {
BONUS_PERC =
(swapAvgCurrency * 100 * 2 * mult6) /
(swapAvgAsset + swapAvgCurrency);
if (BONUS_PERC < 1e8) {
BONUS_PERC = 1e8;
}
return
(periodFee * rateReward * currentPrice * BONUS_PERC) /
(PRICE_DECIMALS * 1e8 * 1e6);
}
BONUS_PERC =
(swapAvgAsset * 100 * 2 * mult6) /
(swapAvgAsset + swapAvgCurrency);
if (BONUS_PERC < 1e8) {
BONUS_PERC = 1e8;
}
return (periodFee * rateReward * BONUS_PERC) / (1e8 * 1e6);
} else {
if (swapType == MetalSwapAbstract.SwapType(0)) {
return
(periodFee * rateReward * currentPrice) /
(PRICE_DECIMALS * 1e6);
}
return (periodFee * rateReward) / 1e6;
}
}
function setMainParameters(
uint256 _iV,
uint256 _settlementFeePerc,
address __governance,
string memory _description,
uint256 _PRICE_DECIMALS
) external onlyGovernance {
require(
_settlementFeePerc < 100 * 10**6,
'Error on setSettlementFeePerc: settlement fee percentual is not in the allowed range [0-100M] (equivalent to 0%-100% with a 6 decimal place precision)'
);
settlementFeePerc = _settlementFeePerc;
iV = _iV;
_governance = __governance;
description = _description;
PRICE_DECIMALS = _PRICE_DECIMALS;
emit SetMainParameters(
_iV,
_settlementFeePerc,
__governance,
_description,
_PRICE_DECIMALS
);
}
function sqrt(uint256 x) private pure returns (uint256 y) {
uint256 z = (x + 1) / 2;
y = x;
while (z < y) {
y = z;
z = (x / z + z) / 2;
}
}
function _transferGovernance(address newGovernance) private {
require(
newGovernance != address(0),
'Error on _transferGovernance: new owner cannot be zero address'
);
_governance = newGovernance;
}
function transferGovernance(address newGovernace) external onlyOwner {
require(
newGovernace != address(0),
'Error on transferGovernance: new owner cannot be the null address'
);
_transferGovernance(newGovernace);
emit TransferGovernance(newGovernace);
}
function governance() public view virtual returns (address) {
return _governance;
}
modifier onlyGovernance() {
require(
owner() == _msgSender() || governance() == _msgSender(),
'Error on onlyGovernance: caller is not the owner/governance'
);
_;
}
}