Skip to content

Commit 22f9278

Browse files
authored
Merge pull request #85 from laminar-protocol/margin-swap-rates-per-pair
MarginProtocol: Swap rates per token pair
2 parents 3d7cba1 + a917adb commit 22f9278

File tree

5 files changed

+218
-69
lines changed

5 files changed

+218
-69
lines changed

contracts/impls/margin/MarginFlowProtocol.sol

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,14 @@ contract MarginFlowProtocol is FlowProtocolBase {
125125
mapping (MarginLiquidityPoolInterface => mapping(address => bool)) public traderHasPaidFees;
126126
mapping (MarginLiquidityPoolInterface => mapping(address => bool)) public traderIsMarginCalled;
127127
mapping(address => mapping (address => bool)) public tradingPairWhitelist;
128+
mapping (address => mapping(address => mapping (bool => Percentage.Percent))) public currentSwapRates;
128129

129-
Percentage.Percent public currentSwapRate;
130130
uint256 public minLeverage;
131131
uint256 public maxLeverage;
132132
uint256 public minLeverageAmount;
133-
uint256 public rateUnit;
133+
uint256 public swapRateUnit;
134+
bool constant public LONG = true;
135+
bool constant public SHORT = false;
134136
uint256 constant public TRADER_MARGIN_CALL_FEE = 20 ether; // TODO
135137
uint256 constant public TRADER_LIQUIDATION_FEE = 60 ether; // TODO
136138

@@ -150,51 +152,62 @@ contract MarginFlowProtocol is FlowProtocolBase {
150152
* @dev Initialize the MarginFlowProtocol.
151153
* @param _oracle The price oracle
152154
* @param _moneyMarket The money market.
155+
* @param _safetyProtocol The _safetyProtocol.
153156
* @param _liquidityPoolRegistry The liquidity pool registry.
154-
* @param _initialSwapRate The initial swap rate as percentage.
157+
* @param _initialMinLeverage The _initialMinLeverage.
158+
* @param _initialMaxLeverage The _initialMaxLeverage.
159+
* @param _initialMinLeverageAmount The _initialMinLeverageAmount.
160+
* @param _swapRateUnit The _swapRateUnit.
155161
*/
156162
function initialize(
157163
PriceOracleInterface _oracle,
158164
MoneyMarketInterface _moneyMarket,
159165
MarginFlowProtocolSafety _safetyProtocol,
160166
MarginLiquidityPoolRegistry _liquidityPoolRegistry,
161-
uint256 _initialSwapRate,
162167
uint256 _initialMinLeverage,
163168
uint256 _initialMaxLeverage,
164169
uint256 _initialMinLeverageAmount,
165-
uint256 _rateUnit
170+
uint256 _swapRateUnit
166171
) external initializer {
167172
FlowProtocolBase.initialize(_oracle, _moneyMarket);
168173
safetyProtocol = _safetyProtocol;
169174
liquidityPoolRegistry = _liquidityPoolRegistry;
170-
currentSwapRate = Percentage.Percent(_initialSwapRate);
171175
minLeverage = _initialMinLeverage;
172176
maxLeverage = _initialMaxLeverage;
173177
minLeverageAmount = _initialMinLeverageAmount;
174-
rateUnit = _rateUnit;
178+
swapRateUnit = _swapRateUnit;
175179
}
176180

177181
/**
178182
* @dev Add new trading pair, only for the owner.
179183
* @param _base The base token.
180184
* @param _quote The quote token.
185+
* @param _swapRateLong The swap rate as percentage for longs.
186+
* @param _swapRateShort The swap rate as percentage for shorts.
181187
*/
182-
function addTradingPair(address _base, address _quote) external onlyOwner {
188+
function addTradingPair(address _base, address _quote, uint256 _swapRateLong, uint256 _swapRateShort) external onlyOwner {
183189
require(_base != address(0) && _quote != address(0), "0");
184190
require(_base != _quote, "TP3");
185191
require(!tradingPairWhitelist[_base][_quote], "TP2");
192+
193+
currentSwapRates[_base][_quote][LONG] = Percentage.Percent(_swapRateLong);
194+
currentSwapRates[_base][_quote][SHORT] = Percentage.Percent(_swapRateShort);
186195
tradingPairWhitelist[_base][_quote] = true;
187196

188197
emit NewTradingPair(_base, _quote);
189198
}
190199

191200
/**
192-
* @dev Set new swap rate, only for the owner.
193-
* @param _newSwapRate The new swap rate as percentage.
201+
* @dev Set new swap rate for token pair, only for the owner.
202+
* @param _base The base token.
203+
* @param _quote The quote token.
204+
* @param _newSwapRateLong The new swap rate as percentage for longs.
205+
* @param _newSwapRateShort The new swap rate as percentage for shorts.
194206
*/
195-
function setCurrentSwapRate(uint256 _newSwapRate) external onlyOwner {
196-
require(_newSwapRate > 0, "0");
197-
currentSwapRate = Percentage.Percent(_newSwapRate);
207+
function setCurrentSwapRateForPair(address _base, address _quote, uint256 _newSwapRateLong, uint256 _newSwapRateShort) external onlyOwner {
208+
require(_newSwapRateLong > 0 && _newSwapRateShort > 0, "0");
209+
currentSwapRates[_base][_quote][LONG] = Percentage.Percent(_newSwapRateLong);
210+
currentSwapRates[_base][_quote][SHORT] = Percentage.Percent(_newSwapRateShort);
198211
}
199212

200213
/**
@@ -427,7 +440,7 @@ contract MarginFlowProtocol is FlowProtocolBase {
427440
Position memory position = positionsById[_positionId];
428441

429442
uint256 timeDeltaInSeconds = now.sub(position.timeWhenOpened);
430-
uint256 daysSinceOpen = timeDeltaInSeconds.div(rateUnit);
443+
uint256 daysSinceOpen = timeDeltaInSeconds.div(swapRateUnit);
431444
uint256 leveragedDebitsAbs = position.leveragedDebitsInUsd >= 0
432445
? uint256(position.leveragedDebitsInUsd)
433446
: uint256(-position.leveragedDebitsInUsd);
@@ -578,7 +591,7 @@ contract MarginFlowProtocol is FlowProtocolBase {
578591
int256(leveragedDebits).mul(debitSignum),
579592
int256(leveragedHeldInUsd).mul(debitSignum),
580593
marginHeld,
581-
currentSwapRate,
594+
currentSwapRates[_pair.base][_pair.quote][_leverage > 0 ? LONG : SHORT],
582595
now
583596
);
584597

cucumber/step-definitions/margin.steps.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ const parseAmount = (amount: string): BN => {
9696
};
9797

9898
const parseSwapRate = (amount: string): BN => {
99-
const parsed = amount.replace('%', '');
99+
const parsed = amount.replace(/%|-/g, '');
100100
const onePercentSpread = new BN(web3.utils.toWei('1')).div(new BN(100));
101+
101102
return onePercentSpread.mul(new BN(parsed));
102103
};
103104

@@ -257,7 +258,7 @@ Given('oracle price', async (table: TableDefinition) => {
257258

258259
Given('margin spread', async (table: TableDefinition) => {
259260
for (const [pair, value] of table.rows()) {
260-
const spreadValue = parseAmount(value); // TODO? .div(new BN(10000));
261+
const spreadValue = parseAmount(value);
261262
const { baseAddress, quoteAddress } = parseTradingPair(pair);
262263

263264
await sendTx({
@@ -297,13 +298,16 @@ Given(
297298

298299
Given('margin set swap rate', async (table: TableDefinition) => {
299300
for (const [pair, long, short] of table.rows()) {
300-
const { baseAddress, quoteAddress } = parseTradingPair(pair); // TODO
301+
const { baseAddress, quoteAddress } = parseTradingPair(pair);
301302
const longSwapRate = parseSwapRate(long);
302-
const shortSpread = parseSwapRate(short); // TODO
303+
const shortSpread = parseSwapRate(short);
303304

304305
await sendTx({
305-
contractMethod: flowMarginProtocolContract.methods.setCurrentSwapRate(
306+
contractMethod: flowMarginProtocolContract.methods.setCurrentSwapRateForPair(
307+
baseAddress,
308+
quoteAddress,
306309
longSwapRate,
310+
shortSpread,
307311
),
308312
to: flowMarginProtocolAddress,
309313
});
@@ -322,6 +326,8 @@ Given(/margin enable trading pair (\D*)/, async (tradingPair: string) => {
322326
contractMethod: flowMarginProtocolContract.methods.addTradingPair(
323327
baseAddress,
324328
quoteAddress,
329+
1,
330+
1,
325331
),
326332
to: flowMarginProtocolAddress,
327333
});

migrations/deploy_contracts.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ module.exports = (artifacts: Truffle.Artifacts, web3: Web3) => {
270270
moneyMarket.address,
271271
marginProtocolSafety.address,
272272
marginLiquidityPoolRegistry.address,
273-
initialSwapRate,
274273
1,
275274
50,
276275
2,
@@ -298,14 +297,54 @@ module.exports = (artifacts: Truffle.Artifacts, web3: Web3) => {
298297

299298
const usd = await moneyMarket.baseToken();
300299

301-
await marginProtocol.addTradingPair(fEUR.address, usd);
302-
await marginProtocol.addTradingPair(usd, fEUR.address);
303-
await marginProtocol.addTradingPair(fJPY.address, usd);
304-
await marginProtocol.addTradingPair(usd, fJPY.address);
305-
await marginProtocol.addTradingPair(fXAU.address, usd);
306-
await marginProtocol.addTradingPair(usd, fXAU.address);
307-
await marginProtocol.addTradingPair(fAAPL.address, usd);
308-
await marginProtocol.addTradingPair(usd, fAAPL.address);
300+
await marginProtocol.addTradingPair(
301+
fEUR.address,
302+
usd,
303+
initialSwapRate,
304+
initialSwapRate,
305+
);
306+
await marginProtocol.addTradingPair(
307+
usd,
308+
fEUR.address,
309+
initialSwapRate,
310+
initialSwapRate,
311+
);
312+
await marginProtocol.addTradingPair(
313+
fJPY.address,
314+
usd,
315+
initialSwapRate,
316+
initialSwapRate,
317+
);
318+
await marginProtocol.addTradingPair(
319+
usd,
320+
fJPY.address,
321+
initialSwapRate,
322+
initialSwapRate,
323+
);
324+
await marginProtocol.addTradingPair(
325+
fXAU.address,
326+
usd,
327+
initialSwapRate,
328+
initialSwapRate,
329+
);
330+
await marginProtocol.addTradingPair(
331+
usd,
332+
fXAU.address,
333+
initialSwapRate,
334+
initialSwapRate,
335+
);
336+
await marginProtocol.addTradingPair(
337+
fAAPL.address,
338+
usd,
339+
initialSwapRate,
340+
initialSwapRate,
341+
);
342+
await marginProtocol.addTradingPair(
343+
usd,
344+
fAAPL.address,
345+
initialSwapRate,
346+
initialSwapRate,
347+
);
309348

310349
// approve default account
311350

0 commit comments

Comments
 (0)