@@ -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
0 commit comments