/ test / finance / OptionsExchange / TestExchangeDeposit.t.sol
TestExchangeDeposit.t.sol
  1  pragma solidity >=0.6.0;
  2  
  3  import "truffle/Assert.sol";
  4  import "../../../contracts/utils/MoreMath.sol";
  5  import "../../common/utils/MoreAssert.t.sol";
  6  import "./Base.t.sol";
  7  
  8  contract TestExchangeDeposit is Base {
  9  
 10      function testBalances() public {
 11  
 12          uint vBase = 1e24;
 13          
 14          Assert.equal(bob.calcSurplus(), 0, "bob initial surplus");
 15          Assert.equal(alice.calcSurplus(), 0, "alice initial surplus");
 16          
 17          depositTokens(address(bob), 10 * vBase);
 18          Assert.equal(erc20.balanceOf(address(bob)), 0, "bob balance");
 19          Assert.equal(erc20.balanceOf(address(creditProvider)), 10 * vBase, "creditProvider balance");
 20          
 21          depositTokens(address(alice), 50 * vBase);
 22          Assert.equal(erc20.balanceOf(address(alice)), 0, "alice balance");
 23          Assert.equal(erc20.balanceOf(address(creditProvider)), 60 * vBase, "creditProvider balance");
 24          
 25          Assert.equal(bob.calcSurplus(), 10 * vBase, "bob final surplus");
 26          Assert.equal(alice.calcSurplus(), 50 * vBase, "alice final surplus");
 27      }
 28      
 29      function testSurplus() public {
 30  
 31          uint vBase = 1e24;
 32  
 33          int step = 40e18;
 34          depositTokens(address(bob), 1500 * vBase);
 35  
 36  
 37          exchange.createSymbol(address(feed), CALL, uint(ethInitialPrice-step), time.getNow() + 10 days);
 38          exchange.createSymbol(address(feed), CALL, uint(ethInitialPrice+step), time.getNow() + 10 days);
 39  
 40          addSymbol(uint(ethInitialPrice - step), 10 days);
 41          addSymbol(uint(ethInitialPrice + step), 10 days);
 42  
 43          (bool success1,) = address(this).call(
 44              abi.encodePacked(
 45                  bob.writeOption.selector,
 46                  abi.encode(CALL, ethInitialPrice - step, 10 days, pool)
 47              )
 48          );
 49          (bool success2,) = address(bob).call(
 50              abi.encodePacked(
 51                  bob.writeOption.selector,
 52                  abi.encode(CALL, ethInitialPrice + step, 10 days, pool)
 53              )
 54          );
 55  
 56          uint ct1 = MoreMath.sqrtAndMultiply(10, upperVol) + uint(step);
 57          uint ct2 = MoreMath.sqrtAndMultiply(10, upperVol);
 58  
 59          uint sp = 1500 * vBase - ct1 - ct2 ;
 60          //MoreAssert.equal(bob.calcSurplus(), sp, cBase, "check surplus");
 61          Assert.equal(bob.calcSurplus(), sp, "check surplus");
 62  
 63          bob.withdrawTokens();
 64  
 65          Assert.equal(bob.calcSurplus(), 0, "check surplus after withdraw");
 66          MoreAssert.equal(erc20.balanceOf(address(bob)), sp, cBase, "check tokens after withdraw");
 67      }
 68  
 69      function testAllowance() public {
 70  
 71          uint vBase = 1e24;
 72          
 73          depositTokens(address(bob), 10 * vBase);
 74  
 75          Assert.equal(
 76              exchange.allowance(address(bob), address(this)), 0, "allowance before aprrove"
 77          );
 78  
 79          bob.approve(address(this), 1 * vBase);
 80  
 81          Assert.equal(
 82              exchange.allowance(address(bob), address(this)), 1 * vBase, "allowance after aprrove"
 83          );
 84  
 85          (bool r1,) = address(exchange).call(
 86              abi.encodePacked(
 87                  bytes4(keccak256("transferBalance(address,address,uint256)")),
 88                  abi.encode(address(bob), address(this), 1 * vBase)
 89              )
 90          );
 91  
 92          Assert.isTrue(r1, "transferBalance should succeed");
 93  
 94          Assert.equal(
 95              exchange.allowance(address(bob), address(this)), 0, "allowance after transfer"
 96          );
 97  
 98          Assert.equal(exchange.balanceOf(address(bob)), 9 * vBase, "bob balance");
 99          Assert.equal(exchange.balanceOf(address(this)), 1 * vBase, "test balance");
100  
101          (bool r2,) = address(exchange).call(
102              abi.encodePacked(
103                  bytes4(keccak256("transferBalance(address,address,uint256)")),
104                  abi.encode(address(bob), address(this), 1 * vBase)
105              )
106          );
107  
108          Assert.isFalse(r2, "transferBalance should fail");
109      }
110  }