Skip to content

Commit 371aeb4

Browse files
authored
Merge pull request #82 from laminar-protocol/update-subgraph
Update subgraph for latest version
2 parents 41202b6 + 0a3b137 commit 371aeb4

File tree

10 files changed

+175
-207
lines changed

10 files changed

+175
-207
lines changed

artifacts/kovan/deployment.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
22
"baseToken": "0xbF7A7169562078c96f0eC1A8aFD6aE50f12e5A99",
33
"cToken": "0x0A1e4D0B5c71B955c0a5993023fc48bA6E380496",
4-
"moneyMarket": "0x4BAB551e94185fFB0A2C2539A1B3829e5A63E9ed",
5-
"iToken": "0xf7D9c2F13db627cF292Dc01CFa4bD6A2D850cc62",
6-
"oracle": "0x89F2e7a3726e1dBe7d12f386b13435A596A5Bec3",
7-
"syntheticProtocol": "0x87078D357C8EedA62a8C142c81129010CA99FFbD",
8-
"fEUR": "0x462201831F3E843608C3cB2cC7CF2cCdA01D7271",
9-
"fJPY": "0xdE6C9Ee4F1592EE5164B9eeA3543754c4B51a91A",
10-
"fXAU": "0x1a5d99cd0643D83a403913c8471Ca2757bcfcb40",
11-
"fAAPL": "0xD827a54aCd970867Cab145ACF163Dce1c0Af5003",
12-
"marginProtocol": "0xB3b9702C4844EaF2A7934970Ec90f1e97f325C7e",
13-
"marginProtocolSafety": "0x557ab86BA06a67146E4C925DAaB52bD4A72550b2",
14-
"marginPoolRegistry": "0x55cc881358AeA2Bd6a1Fa9B511b36951262eBfad",
15-
"marginPool": "0xAAe87632fAE233Cba4e2175c15717924b88BEE8D",
16-
"marginPool2": "0x08Bf04b6c0BaefEEeD1569Fdb7719b1fc5e9E0B1",
17-
"syntheticPool": "0xb587B74502104cf1002d1F9cF4f7D7662E45A63D",
18-
"syntheticPool2": "0x31e2d63BDbB1822422821b90C29c8716b1707246"
4+
"moneyMarket": "0x35288f341bf85BA2E06bc46C4Ce3d458AD5C01ed",
5+
"iToken": "0xdc779681bf0378f3c18fbBB2b0cd9F20571D95b1",
6+
"oracle": "0xdf704B17aCc69B7E8422Ae3E4c99541a3A463e83",
7+
"syntheticProtocol": "0x404462596052F74c682556b091B68df3F28e6d02",
8+
"fEUR": "0xaD94B24bB61b03517EB166E64293301232113873",
9+
"fJPY": "0x02958a524B2ffF8320Bfa0bA0d5368FBdd4C9e78",
10+
"fXAU": "0xc58B8c214527143c681E4f52045c44C1bfbB866c",
11+
"fAAPL": "0xa5711d6aB4Dd814Fa88513ef2198bB622Ab66178",
12+
"marginProtocol": "0xedeB2c3332C88F582afd4c713306f46402eE293b",
13+
"marginProtocolSafety": "0x6ac71C8FCa1501a3B86D97c87416D829fB84124a",
14+
"marginPoolRegistry": "0x687476A8BA21317E9aD1058878D1b2AB6d01Fe42",
15+
"marginPool": "0x4f3C118536090a88D7C046c1e9bACf129C9eBdF0",
16+
"marginPool2": "0xa89925B31ED8259171c961633178cd0C0DfBa1fC",
17+
"syntheticPool": "0x96a47968338458bd31Bdf7c33c627262B1597313",
18+
"syntheticPool2": "0x99237c8054C8d3ffE99b59d701C01598C975f07C"
1919
}

contracts/impls/margin/MarginFlowProtocol.sol

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,17 @@ contract MarginFlowProtocol is FlowProtocolBase {
5959
* @param baseToken The base token
6060
* @param quoteToken The quote token
6161
* @param leverage The leverage, e.g., 20x
62-
* @param amount The quoteToken amount to open position
62+
* @param leveragedDebitsInUsd The base token amount to open position
6363
* @param price The max/min price for opening, 0 means accept all.
6464
*/
6565
event PositionOpened(
66+
uint256 positionId,
6667
address indexed sender,
6768
address indexed liquidityPool,
6869
address indexed baseToken,
6970
address quoteToken,
7071
int256 leverage,
71-
uint256 amount,
72+
int256 leveragedDebitsInUsd,
7273
uint256 price
7374
);
7475

@@ -78,15 +79,17 @@ contract MarginFlowProtocol is FlowProtocolBase {
7879
* @param liquidityPool The MarginLiquidityPool
7980
* @param baseToken The base token
8081
* @param quoteToken The quote token
82+
* @param realizedPl The realized profit or loss after closing
8183
* @param positionId The position id
8284
* @param price The max/min price for closing, 0 means accept all.
8385
*/
8486
event PositionClosed(
87+
uint256 positionId,
8588
address indexed sender,
8689
address indexed liquidityPool,
8790
address indexed baseToken,
8891
address quoteToken,
89-
uint256 positionId,
92+
int256 realizedPl,
9093
uint256 price
9194
);
9295

@@ -314,16 +317,6 @@ contract MarginFlowProtocol is FlowProtocolBase {
314317
_leveragedHeld,
315318
debitsPrice
316319
);
317-
318-
emit PositionOpened(
319-
msg.sender,
320-
address(_pool),
321-
_base,
322-
_quote,
323-
_leverage,
324-
_leveragedHeld,
325-
debitsPrice.value
326-
);
327320
}
328321

329322
/**
@@ -368,11 +361,12 @@ contract MarginFlowProtocol is FlowProtocolBase {
368361
_removePositionFromLists(position);
369362

370363
emit PositionClosed(
364+
_positionId,
371365
msg.sender,
372366
address(position.pool),
373367
address(position.pair.base),
374368
address(position.pair.quote),
375-
_positionId,
369+
balanceDelta,
376370
marketPrice.value
377371
);
378372
}
@@ -612,6 +606,17 @@ contract MarginFlowProtocol is FlowProtocolBase {
612606
positionsById[positionId] = position;
613607
positionsByPoolAndTrader[_pool][msg.sender].push(position);
614608
positionsByPool[_pool].push(position);
609+
610+
emit PositionOpened(
611+
positionId,
612+
msg.sender,
613+
address(_pool),
614+
_pair.base,
615+
_pair.quote,
616+
_leverage,
617+
int256(leveragedHeldInUsd).mul(debitSignum),
618+
_debitsPrice.value
619+
);
615620
}
616621

617622
function _removePositionFromLists(Position memory _position) internal {

contracts/impls/margin/MarginFlowProtocolSafety.sol

Lines changed: 56 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ contract MarginFlowProtocolSafety is Initializable, UpgradeOwnable, UpgradeReent
180180
(
181181
Percentage.Percent memory enp,
182182
Percentage.Percent memory ell
183-
) = _getEnpAndEll(_pool);
183+
) = getEnpAndEll(_pool);
184184
bool isSafe = enp.value > liquidityPoolENPMarginThreshold &&
185185
ell.value > liquidityPoolELLMarginThreshold;
186186

@@ -197,7 +197,7 @@ contract MarginFlowProtocolSafety is Initializable, UpgradeOwnable, UpgradeReent
197197
address _trader
198198
) external nonReentrant poolIsVerified(_pool) {
199199
require(!marginProtocol.traderIsMarginCalled(_pool, _trader), "TM1");
200-
require(!_isTraderSafe(_pool, _trader), "TM2");
200+
require(!isTraderSafe(_pool, _trader), "TM2");
201201

202202
marginProtocol.setTraderIsMarginCalled(_pool, _trader, true);
203203
marginProtocol.moneyMarket().baseToken().safeTransfer(
@@ -219,7 +219,7 @@ contract MarginFlowProtocolSafety is Initializable, UpgradeOwnable, UpgradeReent
219219
poolIsVerified(_pool)
220220
{
221221
require(marginProtocol.traderIsMarginCalled(_pool, _trader), "TS1");
222-
require(_isTraderSafe(_pool, _trader), "TS2");
222+
require(isTraderSafe(_pool, _trader), "TS2");
223223

224224
marginProtocol.setTraderIsMarginCalled(_pool, _trader, false);
225225
marginProtocol.moneyMarket().baseToken().safeTransferFrom(
@@ -285,7 +285,7 @@ contract MarginFlowProtocolSafety is Initializable, UpgradeOwnable, UpgradeReent
285285
MarginLiquidityPoolInterface _pool,
286286
address _trader
287287
) external nonReentrant poolIsVerified(_pool) {
288-
Percentage.SignedPercent memory marginLevel = _getMarginLevel(
288+
Percentage.SignedPercent memory marginLevel = getMarginLevel(
289289
_pool,
290290
_trader
291291
);
@@ -330,7 +330,7 @@ contract MarginFlowProtocolSafety is Initializable, UpgradeOwnable, UpgradeReent
330330
(
331331
Percentage.Percent memory enp,
332332
Percentage.Percent memory ell
333-
) = _getEnpAndEll(_pool);
333+
) = getEnpAndEll(_pool);
334334
require(
335335
enp.value <= liquidityPoolENPLiquidateThreshold ||
336336
ell.value <= liquidityPoolELLLiquidateThreshold,
@@ -379,11 +379,8 @@ contract MarginFlowProtocolSafety is Initializable, UpgradeOwnable, UpgradeReent
379379
// Ensure a trader is safe, based on equity delta, opened positions or plus a new one to open.
380380
//
381381
// Return true if ensured safe or false if not.
382-
function _isTraderSafe(MarginLiquidityPoolInterface _pool, address _trader)
383-
internal
384-
returns (bool)
385-
{
386-
Percentage.SignedPercent memory marginLevel = _getMarginLevel(
382+
function isTraderSafe(MarginLiquidityPoolInterface _pool, address _trader) public returns (bool) {
383+
Percentage.SignedPercent memory marginLevel = getMarginLevel(
387384
_pool,
388385
_trader
389386
);
@@ -394,12 +391,9 @@ contract MarginFlowProtocolSafety is Initializable, UpgradeOwnable, UpgradeReent
394391
}
395392

396393
// Margin level of a given user.
397-
function _getMarginLevel(
398-
MarginLiquidityPoolInterface _pool,
399-
address _trader
400-
) internal returns (Percentage.SignedPercent memory) {
394+
function getMarginLevel(MarginLiquidityPoolInterface _pool, address _trader) public returns (Percentage.SignedPercent memory) {
401395
int256 equity = marginProtocol.getEquityOfTrader(_pool, _trader);
402-
uint256 leveragedDebitsInUsd = _getLeveragedDebitsOfTrader(
396+
uint256 leveragedDebitsInUsd = getLeveragedDebitsOfTrader(
403397
_pool,
404398
_trader
405399
);
@@ -419,11 +413,8 @@ contract MarginFlowProtocolSafety is Initializable, UpgradeOwnable, UpgradeReent
419413
//
420414
// ENP - Equity to Net Position ratio of a liquidity pool.
421415
// ELL - Equity to Longest Leg ratio of a liquidity pool.
422-
function _getEnpAndEll(MarginLiquidityPoolInterface _pool)
423-
internal
424-
returns (Percentage.Percent memory, Percentage.Percent memory)
425-
{
426-
int256 equity = _getEquityOfPool(_pool);
416+
function getEnpAndEll(MarginLiquidityPoolInterface _pool) public returns (Percentage.Percent memory, Percentage.Percent memory) {
417+
int256 equity = getEquityOfPool(_pool);
427418

428419
if (equity < 0) {
429420
return (Percentage.Percent(0), Percentage.Percent(0));
@@ -465,53 +456,7 @@ contract MarginFlowProtocolSafety is Initializable, UpgradeOwnable, UpgradeReent
465456
return (enp, ell);
466457
}
467458

468-
// Force closure position to liquidate liquidity pool based on opened positions.
469-
function _liquidityPoolClosePosition(
470-
MarginLiquidityPoolInterface _pool,
471-
uint256 _id,
472-
MarginFlowProtocol.TradingPair memory _pair,
473-
int256 _leverage,
474-
int256 _leveragedHeld
475-
) internal returns (uint256) {
476-
Percentage.Percent memory price = marginProtocol.getPrice(
477-
_pair.base,
478-
_pair.quote
479-
);
480-
uint256 spread = _leverage > 0
481-
? marginProtocol.getBidSpread(
482-
_pool,
483-
address(_pair.base),
484-
address(_pair.quote)
485-
)
486-
: marginProtocol.getAskSpread(
487-
_pool,
488-
address(_pair.base),
489-
address(_pair.quote)
490-
);
491-
492-
uint256 leveragedHeldAbs = _leveragedHeld >= 0
493-
? uint256(_leveragedHeld)
494-
: uint256(-_leveragedHeld);
495-
uint256 spreadProfit = leveragedHeldAbs.mul(spread.mulPercent(price));
496-
uint256 spreadProfitInUsd = uint256(
497-
marginProtocol.getUsdValue(_pair.base, int256(spreadProfit))
498-
);
499-
500-
uint256 penalty = spreadProfitInUsd;
501-
uint256 subAmount = spreadProfitInUsd.add(penalty);
502-
503-
marginProtocol.closePosition(_id, 0);
504-
505-
uint256 realized = Math.min(_pool.getLiquidity(), subAmount);
506-
_pool.withdrawLiquidity(realized);
507-
508-
return realized;
509-
}
510-
511-
function _getLeveragedDebitsOfTrader(
512-
MarginLiquidityPoolInterface _pool,
513-
address _trader
514-
) internal view returns (uint256) {
459+
function getLeveragedDebitsOfTrader(MarginLiquidityPoolInterface _pool, address _trader) public view returns (uint256) {
515460
uint256 accumulatedLeveragedDebits = uint256(0);
516461
uint256 positionsLength = marginProtocol
517462
.getPositionsByPoolAndTraderLength(_pool, _trader);
@@ -531,10 +476,7 @@ contract MarginFlowProtocolSafety is Initializable, UpgradeOwnable, UpgradeReent
531476
}
532477

533478
// equityOfPool = liquidity - (allUnrealizedPl + allAccumulatedSwapRate)
534-
function _getEquityOfPool(MarginLiquidityPoolInterface _pool)
535-
internal
536-
returns (int256)
537-
{
479+
function getEquityOfPool(MarginLiquidityPoolInterface _pool) public returns (int256) {
538480
uint256 liquidity = marginProtocol.moneyMarket().convertAmountToBase(
539481
marginProtocol.moneyMarket().iToken().balanceOf(address(_pool))
540482
);
@@ -563,4 +505,47 @@ contract MarginFlowProtocolSafety is Initializable, UpgradeOwnable, UpgradeReent
563505

564506
return int256(liquidity).sub(unrealizedPlAndRate);
565507
}
508+
509+
// Force closure position to liquidate liquidity pool based on opened positions.
510+
function _liquidityPoolClosePosition(
511+
MarginLiquidityPoolInterface _pool,
512+
uint256 _id,
513+
MarginFlowProtocol.TradingPair memory _pair,
514+
int256 _leverage,
515+
int256 _leveragedHeld
516+
) internal returns (uint256) {
517+
Percentage.Percent memory price = marginProtocol.getPrice(
518+
_pair.base,
519+
_pair.quote
520+
);
521+
uint256 spread = _leverage > 0
522+
? marginProtocol.getBidSpread(
523+
_pool,
524+
address(_pair.base),
525+
address(_pair.quote)
526+
)
527+
: marginProtocol.getAskSpread(
528+
_pool,
529+
address(_pair.base),
530+
address(_pair.quote)
531+
);
532+
533+
uint256 leveragedHeldAbs = _leveragedHeld >= 0
534+
? uint256(_leveragedHeld)
535+
: uint256(-_leveragedHeld);
536+
uint256 spreadProfit = leveragedHeldAbs.mul(spread.mulPercent(price));
537+
uint256 spreadProfitInUsd = uint256(
538+
marginProtocol.getUsdValue(_pair.base, int256(spreadProfit))
539+
);
540+
541+
uint256 penalty = spreadProfitInUsd;
542+
uint256 subAmount = spreadProfitInUsd.add(penalty);
543+
544+
marginProtocol.closePosition(_id, 0);
545+
546+
uint256 realized = Math.min(_pool.getLiquidity(), subAmount);
547+
_pool.withdrawLiquidity(realized);
548+
549+
return realized;
550+
}
566551
}

contracts/mocks/margin/TestMarginFlowProtocolSafety.sol

Lines changed: 0 additions & 23 deletions
This file was deleted.

migrations/deploy_contracts.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ module.exports = (artifacts: Truffle.Artifacts, web3: Web3) => {
204204
await oracle.addPriceFeeder(priceFeeder);
205205
await oracle.addPriceFeeder('0x481c00e62cC701925a676BC713E0E71C692aC46d'); // kovan oracle server
206206
await oracle.setExpireIn(172800); // 2 days for now
207+
await oracle.feedPrice(baseToken.address, web3.utils.toWei('1'), {
208+
from: priceFeeder,
209+
});
207210
await oracle.feedPrice(fEUR.address, web3.utils.toWei('1.2'), {
208211
from: priceFeeder,
209212
});

0 commit comments

Comments
 (0)