PoolTrader.t.sol
1 pragma solidity >=0.6.0; 2 pragma experimental ABIEncoderV2; 3 4 import "../../../contracts/finance/OptionsExchange.sol"; 5 import "../../../contracts/interfaces/IERC20.sol"; 6 import "../../../contracts/interfaces/IGovernableLiquidityPool.sol"; 7 import "../../../contracts/interfaces/IOptionsExchange.sol"; 8 9 contract PoolTrader { 10 11 IERC20 private erc20; 12 OptionsExchange private exchange; 13 IGovernableLiquidityPool private pool; 14 15 address private addr; 16 address private feed; 17 uint private volumeBase = 1e18; 18 string symbol; 19 20 constructor(address _erc20, address _exchange, address _pool, address _feed, string memory _symbol) public { 21 22 erc20 = IERC20(_erc20); 23 exchange = OptionsExchange(_exchange); 24 pool = IGovernableLiquidityPool(_pool); 25 addr = address(this); 26 feed = _feed; 27 symbol= _symbol; 28 } 29 30 function balance() external view returns (uint) { 31 32 return erc20.balanceOf(addr) + exchange.balanceOf(addr); 33 } 34 35 function approve(address spender, uint value) external { 36 37 erc20.approve(spender, value); 38 } 39 40 function depositInExchange(uint value) external { 41 42 erc20.approve(address(exchange), value); 43 exchange.depositTokens(address(this), address(erc20), value); 44 } 45 46 function writeOptions( 47 uint volume, 48 IOptionsExchange.OptionType optType, 49 uint strike, 50 uint maturity 51 ) 52 public 53 returns (address _tk) 54 { 55 IOptionsExchange.OpenExposureInputs memory oEi; 56 57 oEi.symbols = new string[](1); 58 oEi.volume = new uint[](1); 59 oEi.isShort = new bool[](1); 60 oEi.isCovered = new bool[](1); 61 oEi.poolAddrs = new address[](1); 62 oEi.paymentTokens = new address[](1); 63 64 65 oEi.symbols[0] = symbol; 66 oEi.volume[0] = volume * volumeBase; 67 oEi.isShort[0] = true; 68 oEi.poolAddrs[0] = address(pool);//poolAddr; 69 //oEi.isCovered[0] = false; //expoliting default to save gas 70 //oEi.paymentTokens[0] = address(0); //exploiting default to save gas 71 72 73 exchange.openExposure( 74 oEi, 75 address(this) 76 ); 77 } 78 79 function withdrawFromPool() external { 80 81 pool.withdraw(IERC20(address(pool)).balanceOf(address(this))); 82 } 83 84 function buyFromPool(string calldata symbol, uint price, uint volume) 85 external 86 returns (address) 87 { 88 erc20.approve(address(pool), price * volume / volumeBase); 89 return pool.buy(symbol, price, volume, address(erc20)); 90 } 91 92 function sellToPool(string calldata symbol, uint price, uint volume) external { 93 94 IERC20(exchange.resolveToken(symbol)).approve(address(pool), price * volume / volumeBase); 95 pool.sell(symbol, price, volume); 96 } 97 98 function withdrawTokens(uint amount) public { 99 address[] memory tks = new address[](1); 100 tks[0] = address(erc20); 101 102 uint[] memory tksAmt = new uint[](1); 103 tksAmt[0] = amount; 104 exchange.withdrawTokens(tks, tksAmt); 105 } 106 }