/ test / pools / LinearLiquidityPool / TestQueryPool.t.sol
TestQueryPool.t.sol
  1  pragma solidity >=0.6.0;
  2  
  3  import "truffle/Assert.sol";
  4  import "./Base.t.sol";
  5  
  6  contract TestQueryPool is Base {
  7  
  8      function testQueryWithoutFunds() public {
  9  
 10          addSymbol();
 11  
 12          queryBuyAndAssert(applyBuySpread(y[3]), 0, "buy ATM");
 13          querySellAndAssert(applySellSpread(y[3]), 0, "sell ATM");
 14  
 15          feed.setPrice(525e18);
 16  
 17          queryBuyAndAssert(applyBuySpread(y[3]), 0, "buy OTM");
 18          querySellAndAssert(applySellSpread(y[3]), 0, "sell OTM");
 19  
 20          feed.setPrice(575e18);
 21  
 22          queryBuyAndAssert(applyBuySpread((y[3] + y[4]) / 2), 0, "buy ITM");
 23          querySellAndAssert(applySellSpread((y[3] + y[4]) / 2), 0, "sell ITM");
 24      }
 25  
 26      function testQueryWithFunds() public {
 27  
 28          createTraders();
 29  
 30          uint balance = 50 * calcCollateralUnit();
 31          uint freeBalance = 80 * balance / 100;
 32          uint r = fractionBase - reserveRatio;
 33  
 34          depositInPool(address(bob), balance);
 35  
 36          addSymbol();
 37  
 38          time.setFixedTime(1 days);
 39  
 40          uint p0 = applyBuySpread(y[10]);
 41          queryBuyAndAssert(
 42              p0,
 43              freeBalance * volumeBase / (calcCollateralUnit() - (p0 * r / fractionBase)),
 44              "buy ATM"
 45          );
 46          querySellAndAssert(applySellSpread(y[10]), 200 * volumeBase, "sell ATM");
 47  
 48          feed.setPrice(525e18);
 49  
 50          uint p1 = applyBuySpread(y[10]);
 51          queryBuyAndAssert(
 52              p1,
 53              freeBalance * volumeBase / (calcCollateralUnit() - (p1 * r / fractionBase)),
 54              "buy OTM"
 55          );
 56          querySellAndAssert(applySellSpread(y[10]), 200 * volumeBase, "sell OTM");
 57  
 58          feed.setPrice(575e18);
 59  
 60          uint p2 = applyBuySpread((y[10] + y[11]) / 2);
 61          queryBuyAndAssert(
 62              p2,
 63              freeBalance * volumeBase / (calcCollateralUnit() - (p2 * r / fractionBase)),
 64              "buy ITM"
 65          );
 66          querySellAndAssert(applySellSpread((y[10] + y[11]) / 2), 200 * volumeBase, "sell ITM");
 67      }
 68  
 69      function testQueryHalfway() public {
 70  
 71          createTraders();
 72  
 73          uint balance = 1000 * calcCollateralUnit();
 74          depositInPool(address(bob), balance);
 75  
 76          addSymbol();
 77  
 78          time.setFixedTime(15 hours);
 79  
 80          feed.setPrice(575e18);
 81  
 82          uint p0 = (y[3] + y[4]) / 2;
 83          uint p1 = (y[10] + y[11]) / 2;
 84          uint p = p0 - (15 * (p0 - p1) / 24);
 85  
 86          queryBuyAndAssert(applyBuySpread(p), 100 * volumeBase, "buy halfway");
 87          querySellAndAssert(applySellSpread(p), 200 * volumeBase, "sell halfway");
 88      }
 89  
 90      function queryBuyAndAssert(
 91          uint expectPrice,
 92          uint expectedVolume,
 93          string memory message
 94      )
 95          private
 96      {    
 97          (uint ps, uint vs) = IGovernableLiquidityPool(pool).queryBuy(symbol, true);
 98          Assert.equal(ps, expectPrice, message);
 99          Assert.equal(vs, expectedVolume, message);
100      }
101  
102      function querySellAndAssert(
103          uint expectPrice,
104          uint expectedVolume,
105          string memory message
106      )
107          private
108      {    
109          (uint ps, uint vs) = IGovernableLiquidityPool(pool).queryBuy(symbol, false);
110          Assert.equal(ps, expectPrice, message);
111          Assert.equal(vs, expectedVolume, message);
112      }
113  }