def test_one_ask_on_books_buy_excess_order(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] tradeGroupID = "42" # create order orderID = createOrder.publicCreateOrder(ASK, fix(12), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k1, value=fix('12', '4000')) # fill best order fillOrderID = trade.publicBuy(market.address, YES, fix(15), 6000, "0", "0", tradeGroupID, sender = tester.k2, value=fix('15', '6000')) assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0) assert orders.getAmount(fillOrderID) == fix(3) assert orders.getPrice(fillOrderID) == 6000 assert orders.getOrderCreator(fillOrderID) == bytesToHexString(tester.a2) assert orders.getOrderMoneyEscrowed(fillOrderID) == fix('3', '6000') assert orders.getOrderSharesEscrowed(fillOrderID) == 0 assert orders.getBetterOrderId(fillOrderID) == longTo32Bytes(0) assert orders.getWorseOrderId(fillOrderID) == longTo32Bytes(0)
def test_one_bid_on_books_buy_full_order(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] tradeGroupID = "42" # create order orderID = createOrder.publicCreateOrder(BID, fix(2), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k1, value=fix('2', '6000')) # fill best order orderFilledLog = { "filler": bytesToHexString(tester.a2), "numCreatorShares": 0, "numCreatorTokens": fix('2', '6000'), "numFillerShares": 0, "numFillerTokens": fix('2', '4000'), "marketCreatorFees": 0, "reporterFees": 0, "shareToken": market.getShareToken(YES), "tradeGroupId": stringToBytes("42"), } with AssertLog(contractsFixture, "OrderFilled", orderFilledLog): assert trade.publicSell(market.address, YES, fix(2), 6000, "0", "0", tradeGroupID, sender = tester.k2, value=fix('2', '4000')) assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0)
def test_two_asks_on_books_buy_full_and_partial(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] tradeGroupID = "42" # create order 1 orderID1 = createOrder.publicCreateOrder(ASK, fix(12), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k1, value=fix('12', '4000')) # create order 2 orderID2 = createOrder.publicCreateOrder(ASK, fix(7), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k3, value=fix('7', '4000')) # fill best order fillOrderID = trade.publicBuy(market.address, YES, fix(15), 6000, "0", "0", tradeGroupID, sender = tester.k2, value=fix('15', '6000')) assert orders.getAmount(orderID1) == 0 assert orders.getPrice(orderID1) == 0 assert orders.getOrderCreator(orderID1) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID1) == 0 assert orders.getOrderSharesEscrowed(orderID1) == 0 assert orders.getBetterOrderId(orderID1) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID1) == longTo32Bytes(0) assert orders.getAmount(orderID2) == fix(4) assert orders.getPrice(orderID2) == 6000 assert orders.getOrderCreator(orderID2) == bytesToHexString(tester.a3) assert orders.getOrderMoneyEscrowed(orderID2) == fix('4', '4000') assert orders.getOrderSharesEscrowed(orderID2) == 0 assert orders.getBetterOrderId(orderID2) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID2) == longTo32Bytes(0) assert fillOrderID == longTo32Bytes(1)
def test_two_bids_on_books_buy_both(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] tradeGroupID = "42" # create order 1 orderID1 = createOrder.publicCreateOrder(BID, fix(4), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k1, value=fix('4', '6000')) # create order 2 orderID2 = createOrder.publicCreateOrder(BID, fix(1), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k3, value=fix('1', '6000')) # fill best order fillOrderID = trade.publicSell(market.address, YES, fix(5), 6000, "0", "0", tradeGroupID, sender = tester.k2, value=fix('5', '4000')) assert orders.getAmount(orderID1) == 0 assert orders.getPrice(orderID1) == 0 assert orders.getOrderCreator(orderID1) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID1) == 0 assert orders.getOrderSharesEscrowed(orderID1) == 0 assert orders.getBetterOrderId(orderID1) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID1) == longTo32Bytes(0) assert orders.getAmount(orderID2) == 0 assert orders.getPrice(orderID2) == 0 assert orders.getOrderCreator(orderID2) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID2) == 0 assert orders.getOrderSharesEscrowed(orderID2) == 0 assert orders.getBetterOrderId(orderID2) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID2) == longTo32Bytes(0) assert fillOrderID == longTo32Bytes(1)
def test_one_bid_on_books_buy_partial_order(useTrade, contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] tradeGroupID = "42" # create order orderID = createOrder.publicCreateOrder(BID, fix(2), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k1, value=fix('2', '6000')) # fill best order fillOrderID = None if useTrade: fillOrderID = trade.publicTrade(1, market.address, YES, fix(1), 6000, "0", "0", tradeGroupID, sender = tester.k2, value=fix('1', '4000')) else: fillOrderID = trade.publicSell(market.address, YES, fix(1), 6000, "0", "0", tradeGroupID, sender = tester.k2, value=fix('1', '4000')) assert orders.getAmount(orderID) == fix(1) assert orders.getPrice(orderID) == 6000 assert orders.getOrderCreator(orderID) == bytesToHexString(tester.a1) assert orders.getOrderMoneyEscrowed(orderID) == fix('1', '6000') assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0) assert fillOrderID == longTo32Bytes(1)
def test_publicFillOrder_ask(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] tradeGroupID = "42" initialMakerETH = contractsFixture.chain.head_state.get_balance(tester.a1) initialFillerETH = contractsFixture.chain.head_state.get_balance(tester.a2) creatorCost = fix('2', '4000') fillerCost = fix('2', '6000') # create order orderID = createOrder.publicCreateOrder(ASK, fix(2), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k1, value=creatorCost) # fill best order fillOrderID = fillOrder.publicFillOrder(orderID, fix(2), tradeGroupID, sender = tester.k2, value=fillerCost) assert contractsFixture.chain.head_state.get_balance(tester.a1) == initialMakerETH - creatorCost assert contractsFixture.chain.head_state.get_balance(tester.a2) == initialFillerETH - fillerCost assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0) assert fillOrderID == 0
def test_publicCreateOrder_bid2(contractsFixture, cash, market, universe): orders = contractsFixture.contracts['Orders'] createOrder = contractsFixture.contracts['CreateOrder'] orderType = BID amount = fix(1) fxpPrice = 4000 outcome = 0 tradeGroupID = "42" marketInitialCash = cash.balanceOf(market.address) creatorInitialETH = contractsFixture.chain.head_state.get_balance(tester.a1) orderID = None shareToken = contractsFixture.getShareToken(market, 0) orderCreatedLog = { 'creator': bytesToHexString(tester.a1), 'shareToken': shareToken.address, 'tradeGroupId': stringToBytes("42"), } with AssertLog(contractsFixture, "OrderCreated", orderCreatedLog): orderID = createOrder.publicCreateOrder(orderType, amount, fxpPrice, market.address, outcome, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender=tester.k1, value = fix('1', '4000')) assert orderID != bytearray(32), "Order ID should be non-zero" assert orders.getAmount(orderID) == amount assert orders.getPrice(orderID) == fxpPrice assert orders.getOrderCreator(orderID) == bytesToHexString(tester.a1) assert orders.getOrderMoneyEscrowed(orderID) == fix(1, 4000) assert orders.getOrderSharesEscrowed(orderID) == 0 assert cash.balanceOf(tester.a1) == 0 assert contractsFixture.chain.head_state.get_balance(tester.a1) == creatorInitialETH - long(4000 * 10**18) assert cash.balanceOf(market.address) - marketInitialCash == 4000 * 10**18
def test_publicFillOrder_bid_scalar(contractsFixture, cash, scalarMarket, universe): createOrder = contractsFixture.contracts['CreateOrder'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] # We're testing the scalar market because it has a different numTicks than 10**18 as the other do. In particular it's numTicks is 40*18**18 market = scalarMarket tradeGroupID = "42" initialMakerETH = contractsFixture.chain.head_state.get_balance(tester.a1) initialFillerETH = contractsFixture.chain.head_state.get_balance(tester.a2) creatorCost = fix('2', '6000') fillerCost = fix('2', '394000') # create order orderID = createOrder.publicCreateOrder(BID, fix(2), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k1, value=creatorCost) # fill best order fillOrderID = fillOrder.publicFillOrder(orderID, fix(2), tradeGroupID, sender = tester.k2, value=fillerCost) assert contractsFixture.chain.head_state.get_balance(tester.a1) == initialMakerETH - creatorCost assert contractsFixture.chain.head_state.get_balance(tester.a2) == initialFillerETH - fillerCost assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0) assert fillOrderID == 0
def test_take_best_order_with_shares_escrowed_buy_with_cash(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] orders = contractsFixture.contracts['Orders'] completeSets = contractsFixture.contracts['CompleteSets'] yesShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(YES)) # buy complete sets assert completeSets.publicBuyCompleteSets(market.address, fix(1), sender=tester.k1, value=fix('1', '10000')) assert yesShareToken.balanceOf(tester.a1) == fix(1) # create order with shares orderID = createOrder.publicCreateOrder(ASK, fix(1), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), "42", sender=tester.k1) assert orderID # fill order with cash using on-chain matcher assert trade.publicFillBestOrder(BID, market.address, YES, fix(1), 6000, "43", sender=tester.k2, value=fix('1', '6000')) == 0 assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0)
def test_ask_withPartialShares(contractsFixture, universe, cash, market): orders = contractsFixture.contracts['Orders'] createOrder = contractsFixture.contracts['CreateOrder'] fillOrder = contractsFixture.contracts['FillOrder'] completeSets = contractsFixture.contracts['CompleteSets'] yesShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(YES)) noShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(NO)) # buy fix(2) complete sets assert completeSets.publicBuyCompleteSets(market.address, fix(2), sender = tester.k1, value= fix(2, market.getNumTicks())) assert cash.balanceOf(tester.a1) == fix('0') assert yesShareToken.balanceOf(tester.a1) == fix(2) assert noShareToken.balanceOf(tester.a1) == fix(2) orderID = None orderCreatedLog = { 'creator': bytesToHexString(tester.a1), 'shareToken': yesShareToken.address, 'tradeGroupId': stringToBytes("42"), } with AssertLog(contractsFixture, "OrderCreated", orderCreatedLog): orderID = createOrder.publicCreateOrder(ASK, fix(3), 4000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), "42", sender=tester.k1, value=fix('6000')) assert cash.balanceOf(tester.a1) == fix('0') assert yesShareToken.balanceOf(tester.a1) == 0 assert noShareToken.balanceOf(tester.a1) == fix(2) # validate the order contains expected results assert orderID != bytearray(32), "Order ID should be non-zero" assert orders.getAmount(orderID) == fix(3) assert orders.getPrice(orderID) == 4000 assert orders.getOrderCreator(orderID) == bytesToHexString(tester.a1) assert orders.getOrderMoneyEscrowed(orderID) == fix('6000') assert orders.getOrderSharesEscrowed(orderID) == fix(2)
def test_take_best_order_multiple_orders(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] orders = contractsFixture.contracts['Orders'] initialTester1ETH = contractsFixture.chain.head_state.get_balance(tester.a1) initialTester2ETH = contractsFixture.chain.head_state.get_balance(tester.a2) # create orders with cash orderIDs = [] numOrders = 5 for i in range(numOrders): orderID = createOrder.publicCreateOrder(ASK, fix(1), 6000 + i, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), "42", sender=tester.k1, value=fix('1', 4000 - i)) assert orderID orderIDs.append(orderID) # fill orders with cash using on-chain matcher price = 6000 + numOrders assert trade.publicFillBestOrder(BID, market.address, YES, fix(numOrders), price, "43", sender=tester.k2, value=fix(numOrders, price)) == 0 for i in range(numOrders): orderID = orderIDs[i] assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0)
def test_duplicate_creation_transaction(contractsFixture, cash, market): orders = contractsFixture.contracts['Orders'] createOrder = contractsFixture.contracts['CreateOrder'] with EtherDelta(-fix(1, 4000), tester.a0, contractsFixture.chain): orderID = createOrder.publicCreateOrder(BID, fix(1), 4000, market.address, 1, longTo32Bytes(0), longTo32Bytes(0), "7", value = fix(1, 4000)) assert orderID with raises(TransactionFailed): createOrder.publicCreateOrder(BID, fix(1), 4000, market.address, 1, longTo32Bytes(0), longTo32Bytes(0), "7", value = fix(1, 4000))
def test_fill_order_with_shares(localFixture, zeroX, market, cash, controller): expirationTimestampInSec = controller.getTimestamp() + 1 orderAddresses = [tester.a0, market.address] orderValues = [YES, ASK, 10, 1000, expirationTimestampInSec, 42] orderHash = zeroX.getOrderHash(orderAddresses, orderValues) v, r, s = createOrder(orderHash) fillAmount = 5 # Fail with no shares deposited with raises(TransactionFailed): zeroX.fillOrder( orderAddresses, orderValues, fillAmount, v, r, s, sender = tester.k1) yesShareAddress = market.getShareToken(YES) noShareAddress = market.getShareToken(NO) yesShareToken = localFixture.applySignature('ShareToken', yesShareAddress) noShareToken = localFixture.applySignature('ShareToken', noShareAddress) completeSets = localFixture.contracts['CompleteSets'] assert completeSets.publicBuyCompleteSets(market.address, fix(20), value=fix('20', market.getNumTicks())) assert noShareToken.transfer(tester.a1, 10) assert yesShareToken.approve(zeroX.address, 10) assert zeroX.deposit(yesShareAddress, 10) assert noShareToken.approve(zeroX.address, 10, sender=tester.k1) assert zeroX.deposit(noShareAddress, 10, sender=tester.k1) with PrintGasUsed(localFixture, "FILL_0X"): assert zeroX.fillOrder( orderAddresses, orderValues, fillAmount, v, r, s, sender = tester.k1) yesShareAddress = market.getShareToken(YES) noShareAddress = market.getShareToken(NO) assert zeroX.getTokenBalance(cash.address, tester.a0) == 4900 assert zeroX.getTokenBalance(yesShareAddress, tester.a0) == 5 assert zeroX.getTokenBalance(cash.address, tester.a1) == 44100 assert zeroX.getTokenBalance(noShareAddress, tester.a1) == 5 assert zeroX.getUnavailableAmount(orderHash) == fillAmount
def check_randoms(market, price, numTicks): fxpPrice = fix(price) fxpMaxDisplayPrice = numTicks fxpTradingFee = fix('0.0101') if fxpPrice <= 0: return 0 if fxpPrice >= fxpMaxDisplayPrice: return 0 if fxpTradingFee >= fxpPrice: return 0 if fxpTradingFee >= fxpMaxDisplayPrice - fxpPrice: return 0 return 1
def test_orderFilling(localFixture, market): createOrder = localFixture.contracts['CreateOrder'] fillOrder = localFixture.contracts['FillOrder'] tradeGroupID = "42" creatorCost = fix('2', '4000') fillerCost = fix('2', '6000') # create order orderID = createOrder.publicCreateOrder(ASK, fix(2), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k1, value=creatorCost) with PrintGasUsed(localFixture, "FillOrder:publicFillOrder", FILL_ORDER): fillOrderID = fillOrder.publicFillOrder(orderID, fix(2), tradeGroupID, sender = tester.k2, value=fillerCost)
def test_publicCreateOrder_ask(contractsFixture, cash, market): orders = contractsFixture.contracts['Orders'] createOrder = contractsFixture.contracts['CreateOrder'] orderID = createOrder.publicCreateOrder(ASK, fix(1), 4000, market.address, 0, longTo32Bytes(0), longTo32Bytes(0), "7", value = fix(1, 6000)) assert orders.getAmount(orderID) == fix(1) assert orders.getPrice(orderID) == 4000 assert orders.getOrderCreator(orderID) == bytesToHexString(tester.a0) assert orders.getOrderMoneyEscrowed(orderID) == fix(1, 6000) assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == bytearray(32) assert orders.getWorseOrderId(orderID) == bytearray(32) assert cash.balanceOf(market.address) == fix(1, 6000)
def test_cancelBid(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] cancelOrder = contractsFixture.contracts['CancelOrder'] orders = contractsFixture.contracts['Orders'] orderType = BID fxpAmount = fix(1) fxpPrice = 6000 outcomeID = YES tradeGroupID = "42" yesShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(YES)) noShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(NO)) creatorInitialETH = contractsFixture.chain.head_state.get_balance(tester.a1) creatorInitialShares = yesShareToken.balanceOf(tester.a1) marketInitialCash = cash.balanceOf(market.address) marketInitialYesShares = yesShareToken.totalSupply() marketInitialNoShares = noShareToken.totalSupply() orderID = createOrder.publicCreateOrder(orderType, fxpAmount, fxpPrice, market.address, outcomeID, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender=tester.k1, value = fix('10000')) assert orderID, "Order ID should be non-zero" assert orders.getOrderCreator(orderID), "Order should have an owner" assert contractsFixture.chain.head_state.get_balance(tester.a1) == creatorInitialETH - fix('1', '6000'), "ETH should be deducted from the creator balance" orderCanceledLog = { 'orderId': orderID, 'shareToken': yesShareToken.address, 'sender': bytesToHexString(tester.a1), 'orderType': orderType, 'sharesRefund': 0, 'tokenRefund': fix('1', '6000'), } with AssertLog(contractsFixture, 'OrderCanceled', orderCanceledLog): assert(cancelOrder.cancelOrder(orderID, sender=tester.k1) == 1), "cancelOrder should succeed" assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0) assert(creatorInitialETH == contractsFixture.chain.head_state.get_balance(tester.a1)), "Maker's ETH should be the same as before the order was placed" assert(marketInitialCash == cash.balanceOf(market.address)), "Market's cash balance should be the same as before the order was placed" assert(creatorInitialShares == yesShareToken.balanceOf(tester.a1)), "Maker's shares should be unchanged" assert(marketInitialYesShares == yesShareToken.totalSupply()), "Market's yes shares should be unchanged" assert marketInitialNoShares == noShareToken.totalSupply(), "Market's no shares should be unchanged"
def icon_style(self,Style,kml): href = unicode(Style.IconStyle.Icon.href) href = utils.fix(href) icon = None if href.startswith('http'): path = self.handle_href_icon(Style,href) if path: icon = mapnik.PointSymbolizer(mapnik.PathExpression(str(path))) icon.allow_overlap = True else: # local file (maybe bundled with kmz) if not os.path.exists(href): href = os.path.join(os.path.dirname(kml),href) if os.path.exists(href): path = self.handle_icon(Style,href) if path: icon = mapnik.PointSymbolizer(mapnik.PathExpression(str(path))) icon.allow_overlap = True else: sys.stderr.write('could not find %s\n' % href) if not icon: icon = mapnik.PointSymbolizer() icon.allow_overlap = True #else: # im = Image.open(href) # icon = mapnik.PointSymbolizer(mapnik.PathExpression(href)) # #icon = mapnik.PointSymbolizer(href, im.format.lower(), im.size[0], im.size[1]) # icon.allow_overlap = True # rule.symbols.append(icon) return icon
def test_order_creation_best_case(numOutcomes, localFixture, markets): createOrder = localFixture.contracts['CreateOrder'] completeSets = localFixture.contracts['CompleteSets'] marketIndex = numOutcomes - 2 market = markets[marketIndex] maxGas = 0 cost = fix('1', '5000') outcome = 0 startGas = localFixture.chain.head_state.gas_used orderID = createOrder.publicCreateOrder(BID, fix(1), 5000, market.address, outcome, longTo32Bytes(0), longTo32Bytes(0), "7", value = fix(1, 5000)) maxGas = localFixture.chain.head_state.gas_used - startGas assert maxGas == CREATE_ORDER_BEST_CASE[marketIndex]
def test_take_best_order_with_shares_escrowed_buy_with_shares_categorical(contractsFixture, cash, categoricalMarket, universe): market = categoricalMarket createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] orders = contractsFixture.contracts['Orders'] completeSets = contractsFixture.contracts['CompleteSets'] firstShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(0)) secondShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(1)) thirdShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(2)) # buy complete sets for both users numTicks = market.getNumTicks() assert completeSets.publicBuyCompleteSets(market.address, fix(1), sender=tester.k1, value=fix('1', numTicks)) assert completeSets.publicBuyCompleteSets(market.address, fix(1), sender=tester.k2, value=fix('1', numTicks)) assert firstShareToken.balanceOf(tester.a1) == firstShareToken.balanceOf(tester.a2) == fix(1) assert secondShareToken.balanceOf(tester.a1) == secondShareToken.balanceOf(tester.a2) == fix(1) assert thirdShareToken.balanceOf(tester.a1) == thirdShareToken.balanceOf(tester.a2) == fix(1) # create order with shares orderID = createOrder.publicCreateOrder(ASK, fix(1), 6000, market.address, 0, longTo32Bytes(0), longTo32Bytes(0), "42", sender=tester.k1) assert orderID # fill order with shares using on-chain matcher totalProceeds = fix(1, numTicks) totalProceeds -= fix(1, numTicks) / market.getMarketCreatorSettlementFeeDivisor() totalProceeds -= fix(1, numTicks) / universe.getOrCacheReportingFeeDivisor() expectedTester1Payout = totalProceeds * 6000 / numTicks expectedTester2Payout = totalProceeds * (numTicks - 6000) / numTicks with EtherDelta(expectedTester1Payout, tester.a1, contractsFixture.chain, "Tester 1 ETH delta wrong"): with EtherDelta(expectedTester2Payout, tester.a2, contractsFixture.chain, "Tester 2 ETH delta wrong"): assert trade.publicFillBestOrder(BID, market.address, 0, fix(1), 6000, "43", sender=tester.k2) == 0 assert firstShareToken.balanceOf(tester.a1) == 0 assert secondShareToken.balanceOf(tester.a1) == fix(1) assert thirdShareToken.balanceOf(tester.a1) == fix(1) assert firstShareToken.balanceOf(tester.a2) == fix(1) assert secondShareToken.balanceOf(tester.a2) == 0 assert thirdShareToken.balanceOf(tester.a2) == 0 assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0)
def test_saveOrder(contractsFixture, market): orders = contractsFixture.contracts['Orders'] orderId1 = orders.saveOrder(BID, market.address, fix(10), 5000, tester.a1, NO, 0, fix(10), longTo32Bytes(0), longTo32Bytes(0), "1") assert(orderId1 != bytearray(32)), "saveOrder wasn't executed successfully" orderId2 = orders.saveOrder(ASK, market.address, fix(10), 5000, tester.a2, NO, fix('10', '5000'), 0, longTo32Bytes(0), longTo32Bytes(0), "1") assert(orderId2 != bytearray(32)), "saveOrder wasn't executed successfully" assert(orders.getAmount(orderId1) == fix(10)), "amount for order1 should be set to 10" assert(orders.getAmount(orderId2) == fix(10)), "amount for order2 should be set to 10" assert(orders.getPrice(orderId1) == 5000), "price for order1 should be set to 5000 wei" assert(orders.getPrice(orderId2) == 5000), "price for order2 should be set to 5000 wei" assert(orders.getOrderCreator(orderId1) == bytesToHexString(tester.a1)), "orderOwner for order1 should be tester.a1" assert(orders.getOrderCreator(orderId2) == bytesToHexString(tester.a2)), "orderOwner for order2 should be tester.a2" assert orders.getOrderMoneyEscrowed(orderId1) == 0, "money escrowed should be 0" assert orders.getOrderMoneyEscrowed(orderId2) == fix('10', '5000'), "money escrowed should be 50000 ETH" assert orders.getOrderSharesEscrowed(orderId1) == fix(10), "shares escrowed should be fix(10)" assert orders.getOrderSharesEscrowed(orderId2) == 0, "shares escrowed should be 0" assert orders.getBetterOrderId(orderId1) == longTo32Bytes(0), "better order id should be 0" assert orders.getBetterOrderId(orderId2) == longTo32Bytes(0), "better order id should be 0" assert orders.getWorseOrderId(orderId1) == longTo32Bytes(0), "worse order id should be 0" assert orders.getWorseOrderId(orderId2) == longTo32Bytes(0), "worse order id should be 0" assert(orders.removeOrder(orderId1) == 1), "Remove order 1" assert(orders.removeOrder(orderId2) == 1), "Remove order 2"
def test_order_filling_both_eth(numOutcomes, localFixture, markets): createOrder = localFixture.contracts['CreateOrder'] completeSets = localFixture.contracts['CompleteSets'] fillOrder = localFixture.contracts['FillOrder'] tradeGroupID = "42" marketIndex = numOutcomes - 2 market = markets[marketIndex] cost = fix('1', '5000') outcome = 0 orderID = createOrder.publicCreateOrder(BID, fix(1), 5000, market.address, outcome, longTo32Bytes(0), longTo32Bytes(0), "7", value = fix(1, 5000)) startGas = localFixture.chain.head_state.gas_used fillOrder.publicFillOrder(orderID, fix(1), tradeGroupID, sender = tester.k1, value=cost) maxGas = localFixture.chain.head_state.gas_used - startGas assert maxGas == FILL_ORDER_BOTH_ETH[marketIndex]
def test_cancelWithSharesInEscrow(contractsFixture, cash, market, universe): completeSets = contractsFixture.contracts['CompleteSets'] createOrder = contractsFixture.contracts['CreateOrder'] cancelOrder = contractsFixture.contracts['CancelOrder'] orders = contractsFixture.contracts['Orders'] yesShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(YES)) noShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(NO)) totalProceeds = fix('12', market.getNumTicks()) marketCreatorFee = totalProceeds / market.getMarketCreatorSettlementFeeDivisor() reporterFee = totalProceeds / universe.getOrCacheReportingFeeDivisor() completeSetFees = marketCreatorFee + reporterFee # buy complete sets assert completeSets.publicBuyCompleteSets(market.address, fix(12), sender = tester.k1, value=fix('12', market.getNumTicks())) assert cash.balanceOf(tester.a1) == fix('0') assert yesShareToken.balanceOf(tester.a1) == fix(12) assert noShareToken.balanceOf(tester.a1) == fix(12) creatorInitialETH = contractsFixture.chain.head_state.get_balance(tester.a1) creatorInitialShares = yesShareToken.balanceOf(tester.a1) marketInitialCash = cash.balanceOf(market.address) marketInitialYesShares = yesShareToken.totalSupply() marketInitialNoShares = noShareToken.totalSupply() # create BID order for YES with NO shares escrowed assert noShareToken.approve(createOrder.address, fix(12), sender = tester.k1) orderID = createOrder.publicCreateOrder(BID, fix(12), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), "42", sender = tester.k1) assert orderID assert cash.balanceOf(tester.a1) == fix('0') assert yesShareToken.balanceOf(tester.a1) == fix(12) assert noShareToken.balanceOf(tester.a1) == 0 # now cancel the order assert(cancelOrder.cancelOrder(orderID, sender=tester.k1) == 1), "cancelOrder should succeed" assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0) assert(creatorInitialETH == contractsFixture.chain.head_state.get_balance(tester.a1)), "Maker's ETH should be the same as before the order was placed" assert(marketInitialCash == cash.balanceOf(market.address)), "Market's cash balance should be the same as before the order was placed" assert(creatorInitialShares == yesShareToken.balanceOf(tester.a1)), "Maker's shares should be unchanged" assert(marketInitialYesShares == yesShareToken.totalSupply()), "Market's yes shares should be unchanged" assert marketInitialNoShares == noShareToken.totalSupply(), "Market's no shares should be unchanged"
def test_minimum_gas_failure(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] tradeGroupID = "42" # create order orderID = createOrder.publicCreateOrder(BID, fix(4), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k1, value=fix('4', '6000')) # We need to provide a minimum gas amount or we'll get back a failure. In testing we use a much higher minimum since in some cases we alter the contracts in a way that massively increases gas cost. In production this value is 500000 minGas = 5000000 fillOrderID = trade.publicSell(market.address, YES, fix(5), 6000, "0", "0", tradeGroupID, sender = tester.k2, value=fix('5', '4000'), startgas=minGas-1) # We get back a sentinal byte value since not enough gas was provided assert fillOrderID == longTo32Bytes(1) # If we provide enough gas we get a legitimate value fillOrderID = trade.publicSell(market.address, YES, fix(5), 6000, "0", "0", tradeGroupID, sender = tester.k2, value=fix('5', '4000')) assert fillOrderID != longTo32Bytes(1)
def test_orderCreationMax(numOutcomes, localFixture, markets): createOrder = localFixture.contracts['CreateOrder'] completeSets = localFixture.contracts['CompleteSets'] marketIndex = numOutcomes - 2 market = markets[marketIndex] maxGas = 0 cost = fix('1', '5000') assert completeSets.publicBuyCompleteSets(market.address, 100, value=1000000) outcome = 0 shareToken = localFixture.applySignature('ShareToken', market.getShareToken(outcome)) shareToken.transfer(tester.a7, 100) startGas = localFixture.chain.head_state.gas_used orderID = createOrder.publicCreateOrder(BID, fix(1), 5000, market.address, outcome, longTo32Bytes(0), longTo32Bytes(0), "7", value = fix(1, 5000)) maxGas = localFixture.chain.head_state.gas_used - startGas assert maxGas == CREATE_ORDER_MAXES[marketIndex]
def test_case_1(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] orders = contractsFixture.contracts['Orders'] ordersFinder = contractsFixture.contracts['OrdersFinder'] tradeGroupID = "42" nullAddress = longTo32Bytes(0) # Orphaned order due to early exit logic from bad data caused by Orders worst order setting bug assert orders.getBestOrderId(BID, market.address, 1) == nullAddress orderID1 = createOrder.publicCreateOrder(BID, fix(1), 3000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('1', '3000')) orderID2 = createOrder.publicCreateOrder(BID, fix(2), 3000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('2', '3000')) orderID3 = createOrder.publicCreateOrder(BID, fix(3), 2999, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, value=fix('3', '2999')) assert orders.getWorseOrderId(orderID1) == orderID3 assert orders.getWorseOrderId(orderID3) == longTo32Bytes(0) # order 2 orphaned orderIds = ordersFinder.getExistingOrders5(BID, market.address, 1) assert orderID1 in orderIds assert orderID3 in orderIds assert orderID2 not in orderIds
def test_take_best_order(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] orders = contractsFixture.contracts['Orders'] initialTester1ETH = contractsFixture.chain.head_state.get_balance(tester.a1) initialTester2ETH = contractsFixture.chain.head_state.get_balance(tester.a2) # create order with cash orderID = createOrder.publicCreateOrder(ASK, fix(1), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), "42", sender=tester.k1, value=fix('1', '4000')) assert orderID # fill order with cash using on-chain matcher assert trade.publicFillBestOrder(BID, market.address, YES, fix(1), 6000, "43", sender=tester.k2, value=fix('1', '6000')) == 0 assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0)
def test_order_filling_double_reverse(numOutcomes, localFixture, markets): createOrder = localFixture.contracts['CreateOrder'] completeSets = localFixture.contracts['CompleteSets'] fillOrder = localFixture.contracts['FillOrder'] tradeGroupID = "42" marketIndex = numOutcomes - 2 market = markets[marketIndex] cost = fix('1', '5000') assert completeSets.publicBuyCompleteSets(market.address, 100, value=1000000) outcome = 0 shareToken = localFixture.applySignature('ShareToken', market.getShareToken(outcome)) shareToken.transfer(tester.a1, 100) orderID = createOrder.publicCreateOrder(BID, fix(1), 5000, market.address, outcome, longTo32Bytes(0), longTo32Bytes(0), "7", value = fix(1, 5000)) startGas = localFixture.chain.head_state.gas_used fillOrder.publicFillOrder(orderID, fix(1), tradeGroupID, sender = tester.k1, value=cost) maxGas = localFixture.chain.head_state.gas_used - startGas assert maxGas == FILL_ORDER_DOUBLE_REVERSE_POSITION[marketIndex]
def test_take_best_order_with_shares_escrowed_buy_with_cash(withSelf, contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] orders = contractsFixture.contracts['Orders'] shareToken = contractsFixture.contracts['ShareToken'] shareToken = contractsFixture.contracts["ShareToken"] # buy complete sets sender = contractsFixture.accounts[2] if withSelf else contractsFixture.accounts[1] account = contractsFixture.accounts[2] if withSelf else contractsFixture.accounts[1] with BuyWithCash(cash, fix('1', '100'), sender, "buy complete set"): assert shareToken.publicBuyCompleteSets(market.address, fix(1), sender=sender) assert shareToken.balanceOfMarketOutcome(market.address, 0, account) == fix(1) # create order with shares orderID = createOrder.publicCreateOrder(ASK, fix(1), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), longTo32Bytes(42), nullAddress, sender=sender) assert orderID # fill order with cash using on-chain matcher with PrintGasUsed(contractsFixture, "buy shares escrowed order", 0): with BuyWithCash(cash, fix('1', '60'), contractsFixture.accounts[2], "fill best order"): assert trade.publicFillBestOrder(BID, market.address, YES, fix(1), 60, "43", 6, longTo32Bytes(11), nullAddress, sender=contractsFixture.accounts[2]) == 0 assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0)
def test_one_ask_on_books_buy_excess_order(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] tradeGroupID = longTo32Bytes(42) # create order with BuyWithCash(cash, fix('12', '40'), contractsFixture.accounts[1], "buy complete set"): orderID = createOrder.publicCreateOrder(ASK, fix(12), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, nullAddress, sender = contractsFixture.accounts[1]) # fill best order with BuyWithCash(cash, fix('15', '60'), contractsFixture.accounts[2], "buy complete set"): fillOrderID = trade.publicTrade(LONG,market.address, YES, fix(15), 60, "0", "0", tradeGroupID, 6, longTo32Bytes(11), nullAddress, sender = contractsFixture.accounts[2]) assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0) assert orders.getAmount(fillOrderID) == fix(3) assert orders.getPrice(fillOrderID) == 60 assert orders.getOrderCreator(fillOrderID) == contractsFixture.accounts[2] assert orders.getOrderMoneyEscrowed(fillOrderID) == fix('3', '60') assert orders.getOrderSharesEscrowed(fillOrderID) == 0 assert orders.getBetterOrderId(fillOrderID) == longTo32Bytes(0) assert orders.getWorseOrderId(fillOrderID) == longTo32Bytes(0)
def test_one_bid_on_books_buy_partial_order(useTrade, contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] tradeGroupID = "42" # create order orderID = createOrder.publicCreateOrder(BID, fix(2), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k1, value=fix('2', '6000')) # fill best order fillOrderID = None if useTrade: orderFilledLog = { "amountFilled": fix(1), } with AssertLog(contractsFixture, "OrderFilled", orderFilledLog): with PrintGasUsed(contractsFixture, "publicTrade", 0): fillOrderID = trade.publicTrade(1, market.address, YES, fix(1), 6000, "0", "0", tradeGroupID, sender = tester.k2, value=fix('1', '4000')) else: fillOrderID = trade.publicSell(market.address, YES, fix(1), 6000, "0", "0", tradeGroupID, sender = tester.k2, value=fix('1', '4000')) assert orders.getAmount(orderID) == fix(1) assert orders.getPrice(orderID) == 6000 assert orders.getOrderCreator(orderID) == bytesToHexString(tester.a1) assert orders.getOrderMoneyEscrowed(orderID) == fix('1', '6000') assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0) assert fillOrderID == longTo32Bytes(1)
def test_one_ask_on_books_buy_full_order(withTotalCost, contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] tradeGroupID = "42" # create order with BuyWithCash(cash, fix('12', '4000'), tester.k1, "buy complete set"): orderID = createOrder.publicCreateOrder(ASK, fix(12), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k1) # fill best order if withTotalCost: with BuyWithCash(cash, fix('12', '6000'), tester.k2, "buy complete set"): fillOrderID = trade.publicTradeWithTotalCost(LONG, market.address, YES, fix(12, 6000), 6000, "0", "0", tradeGroupID, 6, sender = tester.k2) else: with BuyWithCash(cash, fix('12', '6000'), tester.k2, "buy complete set"): fillOrderID = trade.publicTrade(LONG, market.address, YES, fix(12), 6000, "0", "0", tradeGroupID, 6, sender = tester.k2) assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0) assert fillOrderID == longTo32Bytes(1)
def test_create_ask_with_shares_fill_with_cash(contractsFixture, cash, market): completeSets = contractsFixture.contracts['CompleteSets'] createOrder = contractsFixture.contracts['CreateOrder'] fillOrder = contractsFixture.contracts['FillOrder'] yesShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(YES)) noShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(NO)) # 1. buy a complete set with account 1 assert completeSets.publicBuyCompleteSets(market.address, 12, sender=tester.k1, value=fix('12')) assert cash.balanceOf(tester.a1) == fix('0') assert yesShareToken.balanceOf( tester.a1) == 12, "Account 1 should have 12 shares of outcome 1" assert noShareToken.balanceOf( tester.a1) == 12, "Account 1 should have 12 shares of outcome 2" # 2. create ASK order for YES with YES shares for escrow assert yesShareToken.approve(createOrder.address, 12, sender=tester.k1) askOrderID = createOrder.publicCreateOrder(ASK, 12, fix('0.6'), market.address, YES, longTo32Bytes(0), longTo32Bytes(0), 42, sender=tester.k1) assert askOrderID, "Order ID should be non-zero" assert cash.balanceOf(tester.a1) == fix('0') assert yesShareToken.balanceOf(tester.a1) == 0 assert noShareToken.balanceOf(tester.a1) == 12 # 3. fill ASK order for YES with cash initialMakerETH = contractsFixture.chain.head_state.get_balance(tester.a1) initialFillerETH = contractsFixture.chain.head_state.get_balance(tester.a2) fxpAmountRemaining = fillOrder.publicFillOrder(askOrderID, 12, sender=tester.k2, value=fix('12', '0.6')) assert fxpAmountRemaining == 0 assert cash.balanceOf(tester.a1) == 0 assert cash.balanceOf(tester.a2) == 0 assert contractsFixture.chain.head_state.get_balance( tester.a1) == initialMakerETH + fix('12', '0.6') assert contractsFixture.chain.head_state.get_balance( tester.a2) == initialFillerETH - fix('12', '0.6') assert yesShareToken.balanceOf(tester.a1) == 0 assert yesShareToken.balanceOf(tester.a2) == 12 assert noShareToken.balanceOf(tester.a1) == 12 assert noShareToken.balanceOf(tester.a2) == 0
def test_escapeHatch(contractsFixture, cash, market): controller = contractsFixture.contracts['Controller'] createOrder = contractsFixture.contracts['CreateOrder'] fillOrder = contractsFixture.contracts['FillOrder'] trade = contractsFixture.contracts['Trade'] tradingEscapeHatch = contractsFixture.contracts['TradingEscapeHatch'] yesShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(YES)) noShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(NO)) initialTester1ETH = contractsFixture.chain.head_state.get_balance(tester.a1) initialTester2ETH = contractsFixture.chain.head_state.get_balance(tester.a2) # create order with cash orderID = createOrder.publicCreateOrder(contractsFixture.contracts['Constants'].ASK(), fix(1), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), "42", sender=tester.k1, value=fix('1', '4000')) assert orderID # fill order with cash using on-chain matcher assert trade.publicFillBestOrder(LONG, market.address, YES, fix(1), 6000, sender=tester.k2, value=fix('1', '6000')) == 0 # assert starting values assert cash.balanceOf(market.address) == fix('10000') assert noShareToken.balanceOf(tester.a1) == fix(1) assert yesShareToken.balanceOf(tester.a2) == fix(1) with raises(TransactionFailed): tradingEscapeHatch.claimSharesInUpdate(market.address) # emergency stop and then have everyone liquidate their position controller.emergencyStop() curTester1Balance = contractsFixture.chain.head_state.get_balance(tester.a1) assert tradingEscapeHatch.getFrozenShareValueInMarket(market.address, sender = tester.k1) == initialTester1ETH - curTester1Balance assert tradingEscapeHatch.claimSharesInUpdate(market.address, sender = tester.k1) assert tradingEscapeHatch.claimSharesInUpdate(market.address, sender = tester.k2) # assert final values (should be a zero sum game) assert contractsFixture.chain.head_state.get_balance(tester.a1) == initialTester1ETH assert contractsFixture.chain.head_state.get_balance(tester.a2) == initialTester2ETH assert cash.balanceOf(market.address) == 0 assert noShareToken.balanceOf(tester.a1) == 0 assert yesShareToken.balanceOf(tester.a2) == 0
def test_multiple_trades(contractsFixture, cash, market, universe): ZeroXTrade = contractsFixture.contracts['ZeroXTrade'] zeroXExchange = contractsFixture.contracts["ZeroXExchange"] simulateTrade = contractsFixture.contracts["SimulateTrade"] cash = contractsFixture.contracts["Cash"] expirationTime = contractsFixture.contracts['Time'].getTimestamp() + 10000 direction = LONG outcome = YES amount = fix(1) salt = 5 tradeGroupID = longTo32Bytes(42) fillOnly = False makerAccount = contractsFixture.accounts[1] makerPrivKey = contractsFixture.privateKeys[1] fillerAccount = contractsFixture.accounts[0] numOrders = 10 orders = [] signatures = [] for i in range(numOrders): price = i + 1 cash.faucet(amount * price, sender=makerAccount) rawZeroXOrderData, orderHash = ZeroXTrade.createZeroXOrder( direction, amount, price, market.address, outcome, expirationTime, salt, sender=makerAccount) signature = signOrder(orderHash, makerPrivKey) orders.append(rawZeroXOrderData) signatures.append(signature) simulate_then_trade(contractsFixture, market, outcome, direction, orders, signatures, (amount * numOrders) - fix(0.5), fillOnly, fillerAccount)
def test_use_shares_multiple(contractsFixture, cash, market, universe): outcome = YES amount = fix(1) price = 40 kycToken = nullAddress fillOnly = False account1 = contractsFixture.accounts[1] account2 = contractsFixture.accounts[2] simulate_then_trade(contractsFixture, LONG, market, outcome, amount, price, kycToken, False, sender=account1) simulate_then_trade(contractsFixture, SHORT, market, outcome, amount + fix(1), price, kycToken, True) simulate_then_trade(contractsFixture, LONG, market, outcome, amount, price, kycToken, False, sender=account2) simulate_then_trade(contractsFixture, SHORT, market, outcome, amount * 2, price, kycToken, False, sender=account1)
def test_create_ask_with_cash_fill_with_cash(contractsFixture, cash, market): completeSets = contractsFixture.contracts['CompleteSets'] createOrder = contractsFixture.contracts['CreateOrder'] fillOrder = contractsFixture.contracts['FillOrder'] yesShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(YES)) noShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(NO)) # 1. create ASK order for YES with cash escrowed with BuyWithCash(cash, fix('12', '40'), contractsFixture.accounts[1], "create order"): askOrderID = createOrder.publicCreateOrder( ASK, fix(12), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), longTo32Bytes(42), nullAddress, sender=contractsFixture.accounts[1]) assert askOrderID assert cash.balanceOf(contractsFixture.accounts[1]) == fix('0') assert yesShareToken.balanceOf(contractsFixture.accounts[1]) == 0 assert noShareToken.balanceOf(contractsFixture.accounts[1]) == 0 # 2. fill ASK order for YES with cash with BuyWithCash(cash, fix('12', '60'), contractsFixture.accounts[2], "create order"): amountRemaining = fillOrder.publicFillOrder( askOrderID, fix(12), longTo32Bytes(42), "0x0000000000000000000000000000000000000000", sender=contractsFixture.accounts[2]) assert amountRemaining == 0 assert cash.balanceOf(contractsFixture.accounts[1]) == fix('0') assert cash.balanceOf(contractsFixture.accounts[2]) == fix('0') assert yesShareToken.balanceOf(contractsFixture.accounts[1]) == 0 assert yesShareToken.balanceOf(contractsFixture.accounts[2]) == fix(12) assert noShareToken.balanceOf(contractsFixture.accounts[1]) == fix(12) assert noShareToken.balanceOf(contractsFixture.accounts[2]) == 0
def test_fill_buy_order_with_buy_categorical(contractsFixture, cash, categoricalMarket, universe): market = categoricalMarket createOrder = contractsFixture.contracts['CreateOrder'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] firstShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(0)) secondShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(1)) thirdShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(2)) # create order with cash price = 6000 numTicks = market.getNumTicks() with BuyWithCash(cash, fix(1, price), tester.k1, "create order"): orderID = createOrder.publicCreateOrder(BID, fix(1), price, market.address, 0, longTo32Bytes(0), longTo32Bytes(0), longTo32Bytes(42), False, sender=tester.k1) assert orderID # fill order with cash with BuyWithCash(cash, fix(1, numTicks - price), tester.k2, "fill order"): assert fillOrder.publicFillOrder( orderID, fix(1), "43", False, "0x0000000000000000000000000000000000000000", sender=tester.k2) == 0 # A complete set was purchased with the provided cash and the shares were provided to each user assert firstShareToken.balanceOf(tester.a1) == fix(1) assert secondShareToken.balanceOf(tester.a1) == 0 assert thirdShareToken.balanceOf(tester.a1) == 0 assert firstShareToken.balanceOf(tester.a2) == 0 assert secondShareToken.balanceOf(tester.a2) == fix(1) assert thirdShareToken.balanceOf(tester.a2) == fix(1) assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0)
def test_take_best_order_with_shares_escrowed_buy_with_cash(withSelf, contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] orders = contractsFixture.contracts['Orders'] completeSets = contractsFixture.contracts['CompleteSets'] yesShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(YES)) # buy complete sets sender = tester.k2 if withSelf else tester.k1 account = tester.a2 if withSelf else tester.a1 with BuyWithCash(cash, fix('1', '10000'), sender, "buy complete set"): assert completeSets.publicBuyCompleteSets(market.address, fix(1), sender=sender) assert yesShareToken.balanceOf(account) == fix(1) # create order with shares orderID = createOrder.publicCreateOrder(ASK, fix(1), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), longTo32Bytes(42), False, nullAddress, sender=sender) assert orderID # fill order with cash using on-chain matcher with PrintGasUsed(contractsFixture, "buy shares escrowed order", 0): with BuyWithCash(cash, fix('1', '6000'), tester.k2, "fill best order"): assert trade.publicFillBestOrder(BID, market.address, YES, fix(1), 6000, "43", 6, False, nullAddress, nullAddress, sender=tester.k2) == 0 assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0)
def test_fill_order_with_shares_escrowed_sell_with_shares( contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] shareToken = contractsFixture.contracts['ShareToken'] fingerprint = longTo32Bytes(11) # buy complete sets for both users with BuyWithCash(cash, fix('1', '100'), contractsFixture.accounts[1], "buy complete set"): assert shareToken.buyCompleteSets(market.address, contractsFixture.accounts[1], fix(1), sender=contractsFixture.accounts[1]) assert shareToken.balanceOfMarketOutcome( market.address, YES, contractsFixture.accounts[1]) == fix(1) with BuyWithCash(cash, fix('1', '100'), contractsFixture.accounts[0], "buy complete set"): assert shareToken.buyCompleteSets(market.address, contractsFixture.accounts[0], fix(1)) assert shareToken.balanceOfMarketOutcome( market.address, NO, contractsFixture.accounts[0]) == fix(1) # create order with shares orderID = createOrder.publicCreateOrder( ASK, fix(1), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), longTo32Bytes(42), nullAddress, sender=contractsFixture.accounts[1]) assert orderID # fill order with shares assert fillOrder.publicFillOrder(orderID, fix(1), "43", fingerprint) == 0 assert shareToken.balanceOfMarketOutcome(market.address, NO, contractsFixture.accounts[0]) == 0 assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0)
def test_publicFillOrder_bid_price_zero(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] tradeGroupID = longTo32Bytes(42) creatorCost = 0 fillerCost = fix(2, 10000) # create order orderID = createOrder.publicCreateOrder(BID, fix(2), 0, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, False, nullAddress, sender=tester.k1) with BuyWithCash(cash, fillerCost, tester.k2, "filling order"): fillOrderID = fillOrder.publicFillOrder( orderID, fix(2), tradeGroupID, False, "0x0000000000000000000000000000000000000000", sender=tester.k2) assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0) assert fillOrderID == 0
def test_two_asks_on_books_buy_one_full_then_create(contractsFixture, cash, market): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] orders = contractsFixture.contracts['Orders'] tradeGroupID = longTo32Bytes(42) # create order 1 with BuyWithCash(cash, fix('12', '4000'), tester.k1, "create order"): orderID1 = createOrder.publicCreateOrder(ASK, fix(12), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, False, nullAddress, sender = tester.k1) # create order 2 with BuyWithCash(cash, fix('7', '3000'), tester.k3, "create order"): orderID2 = createOrder.publicCreateOrder(ASK, fix(7), 7000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, False, nullAddress, sender = tester.k3) # fill/create with BuyWithCash(cash, fix('15', '6000'), tester.k2, "fill and create order"): fillOrderID = trade.publicTrade(LONG,market.address, YES, fix(15), 6000, "0", "0", tradeGroupID, 6, False, nullAddress, nullAddress, sender = tester.k2) assert orders.getAmount(orderID1) == 0 assert orders.getPrice(orderID1) == 0 assert orders.getOrderCreator(orderID1) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID1) == 0 assert orders.getOrderSharesEscrowed(orderID1) == 0 assert orders.getBetterOrderId(orderID1) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID1) == longTo32Bytes(0) assert orders.getAmount(orderID2) == fix(7) assert orders.getPrice(orderID2) == 7000 assert orders.getOrderCreator(orderID2) == bytesToHexString(tester.a3) assert orders.getOrderMoneyEscrowed(orderID2) == fix('7', '3000') assert orders.getOrderSharesEscrowed(orderID2) == 0 assert orders.getBetterOrderId(orderID2) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID2) == longTo32Bytes(0) assert orders.getAmount(fillOrderID) == fix(3) assert orders.getPrice(fillOrderID) == 6000 assert orders.getOrderCreator(fillOrderID) == bytesToHexString(tester.a2) assert orders.getOrderMoneyEscrowed(fillOrderID) == fix('3', '6000') assert orders.getOrderSharesEscrowed(fillOrderID) == 0 assert orders.getBetterOrderId(fillOrderID) == longTo32Bytes(0) assert orders.getWorseOrderId(fillOrderID) == longTo32Bytes(0)
def test_removeOrder(contractsFixture, market): orders = contractsFixture.contracts['Orders'] orderId1 = orders.saveOrder(BID, market.address, fix('10'), 5000, tester.a1, NO, 0, fix('10'), longTo32Bytes(0), longTo32Bytes(0), "1") assert(orderId1 != bytearray(32)), "saveOrder wasn't executed successfully" orderId2 = orders.saveOrder(BID, market.address, fix('10'), 5000, tester.a2, NO, fix('10', '5000'), 0, longTo32Bytes(0), longTo32Bytes(0), "1") assert(orderId2 != bytearray(32)), "saveOrder wasn't executed successfully" orderId3 = orders.saveOrder(BID, market.address, fix('10'), 5000, tester.a1, YES, 0, fix('10'), longTo32Bytes(0), longTo32Bytes(0), "1") assert(orderId3 != bytearray(32)), "saveOrder wasn't executed successfully" assert orders.getAmount(orderId3) == fix('10') assert orders.getPrice(orderId3) == 5000 assert orders.getOrderCreator(orderId3) == bytesToHexString(tester.a1) assert orders.getOrderMoneyEscrowed(orderId3) == 0 assert orders.getOrderSharesEscrowed(orderId3) == fix('10') assert orders.getBetterOrderId(orderId3) == longTo32Bytes(0) assert orders.getWorseOrderId(orderId3) == longTo32Bytes(0) assert(orders.removeOrder(orderId3) == 1), "removeOrder wasn't executed successfully" assert orders.getAmount(orderId3) == 0 assert orders.getPrice(orderId3) == 0 assert orders.getOrderCreator(orderId3) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderId3) == 0 assert orders.getOrderSharesEscrowed(orderId3) == 0 assert orders.getBetterOrderId(orderId3) == longTo32Bytes(0) assert orders.getWorseOrderId(orderId3) == longTo32Bytes(0) assert(orders.removeOrder(orderId1) == 1), "Remove order 1" assert(orders.removeOrder(orderId2) == 1), "Remove order 2"
def test_two_asks_on_books_buy_both(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] tradeGroupID = longTo32Bytes(42) # create order 1 with BuyWithCash(cash, fix('12', '4000'), tester.k1, "buy complete set"): orderID1 = createOrder.publicCreateOrder(ASK, fix(12), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, False, nullAddress, sender = tester.k1) # create order 2 with BuyWithCash(cash, fix('3', '4000'), tester.k3, "buy complete set"): orderID2 = createOrder.publicCreateOrder(ASK, fix(3), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, False, nullAddress, sender = tester.k3) # fill best order with BuyWithCash(cash, fix('15', '6000'), tester.k2, "buy complete set"): fillOrderID = trade.publicTrade(LONG,market.address, YES, fix(15), 6000, "0", "0", tradeGroupID, 6, False, nullAddress, nullAddress, sender = tester.k2) assert orders.getAmount(orderID1) == 0 assert orders.getPrice(orderID1) == 0 assert orders.getOrderCreator(orderID1) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID1) == 0 assert orders.getOrderSharesEscrowed(orderID1) == 0 assert orders.getBetterOrderId(orderID1) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID1) == longTo32Bytes(0) assert orders.getAmount(orderID2) == 0 assert orders.getPrice(orderID2) == 0 assert orders.getOrderCreator(orderID2) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID2) == 0 assert orders.getOrderSharesEscrowed(orderID2) == 0 assert orders.getBetterOrderId(orderID2) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID2) == longTo32Bytes(0) assert fillOrderID == longTo32Bytes(1)
def test_fill_buy_order_with_buy_categorical(contractsFixture, cash, categoricalMarket, universe): market = categoricalMarket createOrder = contractsFixture.contracts['CreateOrder'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] firstShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(0)) secondShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(1)) thirdShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(2)) # create order with cash price = 6000 numTicks = market.getNumTicks() orderID = createOrder.publicCreateOrder(BID, fix(1), price, market.address, 0, longTo32Bytes(0), longTo32Bytes(0), "42", value=fix(1, price), sender=tester.k1) assert orderID # fill order with cash assert fillOrder.publicFillOrder(orderID, fix(1), "43", sender=tester.k2, value=fix(1, numTicks - price)) == 0 # A complete set was purchased with the provided cash and the shares were provided to each user assert firstShareToken.balanceOf(tester.a1) == fix(1) assert secondShareToken.balanceOf(tester.a1) == 0 assert thirdShareToken.balanceOf(tester.a1) == 0 assert firstShareToken.balanceOf(tester.a2) == 0 assert secondShareToken.balanceOf(tester.a2) == fix(1) assert thirdShareToken.balanceOf(tester.a2) == fix(1) assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0)
def test_two_bids_on_books_buy_both(contractsFixture, cash, market): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] orders = contractsFixture.contracts['Orders'] tradeGroupID = longTo32Bytes(42) # create order 1 with BuyWithCash(cash, fix('4', '6000'), tester.k1, "create order"): orderID1 = createOrder.publicCreateOrder(BID, fix(4), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, False, nullAddress, sender = tester.k1) # create order 2 with BuyWithCash(cash, fix('1', '6000'), tester.k3, "create order"): orderID2 = createOrder.publicCreateOrder(BID, fix(1), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, False, nullAddress, sender = tester.k3) # fill best order with PrintGasUsed(contractsFixture, "Fill two", 0): with BuyWithCash(cash, fix('5', '4000'), tester.k2, "fill best orders"): fillOrderID = trade.publicTrade(SHORT,market.address, YES, fix(5), 6000, "0", "0", tradeGroupID, 6, False, nullAddress, nullAddress, sender = tester.k2) assert orders.getAmount(orderID1) == 0 assert orders.getPrice(orderID1) == 0 assert orders.getOrderCreator(orderID1) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID1) == 0 assert orders.getOrderSharesEscrowed(orderID1) == 0 assert orders.getBetterOrderId(orderID1) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID1) == longTo32Bytes(0) assert orders.getAmount(orderID2) == 0 assert orders.getPrice(orderID2) == 0 assert orders.getOrderCreator(orderID2) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID2) == 0 assert orders.getOrderSharesEscrowed(orderID2) == 0 assert orders.getBetterOrderId(orderID2) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID2) == longTo32Bytes(0) assert fillOrderID == longTo32Bytes(1)
def test_trading(kitchenSinkFixture, augur, cash, market): hotLoading = kitchenSinkFixture.contracts[ "HotLoadingUniversal"] if kitchenSinkFixture.paraAugur else kitchenSinkFixture.contracts[ "HotLoading"] if kitchenSinkFixture.paraAugur: augur = kitchenSinkFixture.contracts["ParaAugur"] fillOrder = kitchenSinkFixture.contracts["FillOrder"] trade = kitchenSinkFixture.contracts["Trade"] orders = kitchenSinkFixture.contracts["Orders"] account = kitchenSinkFixture.accounts[0] # We'll do some trades and confirm volume, OI, and last trade prices change createOrder = kitchenSinkFixture.contracts['CreateOrder'] tradeGroupID = longTo32Bytes(42) creatorCost = fix('2', '600') fillerCost = fix('2', '400') # create order with BuyWithCash(cash, creatorCost, kitchenSinkFixture.accounts[1], "complete set buy"): orderID = createOrder.publicCreateOrder( BID, fix(2), 600, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, nullAddress, sender=kitchenSinkFixture.accounts[1]) # take order with BuyWithCash(cash, fillerCost, kitchenSinkFixture.accounts[2], "fill order"): assert trade.publicTrade(SHORT, market.address, YES, fix(2), 600, "0", "0", tradeGroupID, 6, longTo32Bytes(11), nullAddress, sender=kitchenSinkFixture.accounts[2]) marketData = getMarketData(hotLoading, augur, market, fillOrder, orders) assert marketData.volume == fix(2, 1000) assert marketData.openInterest == fix(2, 1000) assert marketData.lastTradedPrices == [0, 0, 600] assert marketData.outcomeVolumes == [0, 0, fix(2, 1000)]
def test_fill_order_with_shares_escrowed_sell_with_shares(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] completeSets = contractsFixture.contracts['CompleteSets'] yesShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(YES)) noShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(NO)) # buy complete sets for both users assert completeSets.publicBuyCompleteSets(market.address, fix(1), sender=tester.k1, value=fix('1', '10000')) assert yesShareToken.balanceOf(tester.a1) == fix(1) assert completeSets.publicBuyCompleteSets(market.address, fix(1), sender=tester.k2, value=fix('1', '10000')) assert noShareToken.balanceOf(tester.a2) == fix(1) # create order with shares orderID = createOrder.publicCreateOrder(ASK, fix(1), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), "42", sender=tester.k1) assert orderID # fill order with shares assert fillOrder.publicFillOrder(orderID, fix(1), "43", sender=tester.k2) == 0 assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0)
def test_take_best_order(withTotalCost, contractsFixture, cash, market): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] orders = contractsFixture.contracts['Orders'] initialTester1ETH = contractsFixture.chain.head_state.get_balance(tester.a1) initialTester2ETH = contractsFixture.chain.head_state.get_balance(tester.a2) # create order with cash with BuyWithCash(cash, fix('1', '4000'), tester.k1, "create order"): orderID = createOrder.publicCreateOrder(ASK, fix(1), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), longTo32Bytes(42), False, nullAddress, sender=tester.k1) assert orderID # fill order with cash using on-chain matcher if withTotalCost: with BuyWithCash(cash, fix('1', '6000'), tester.k2, "fill best order"): assert trade.publicFillBestOrderWithTotalCost(BID, market.address, YES, fix(1, 6000), 6000, "43", 6, False, nullAddress, nullAddress, sender=tester.k2) == 0 else: with BuyWithCash(cash, fix('1', '6000'), tester.k2, "fill best order"): assert trade.publicFillBestOrder(BID, market.address, YES, fix(1), 6000, "43", 6, False, nullAddress, nullAddress, sender=tester.k2) == 0 assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0)
def test_saveOrder(contractsFixture, market): orders = contractsFixture.contracts['Orders'] orderId1 = orders.saveOrder(BID, market.address, 10, fix('0.5'), tester.a1, NO, 0, 10, longTo32Bytes(0), longTo32Bytes(0), 1) assert(orderId1 != bytearray(32)), "saveOrder wasn't executed successfully" orderId2 = orders.saveOrder(ASK, market.address, 10, fix('0.5'), tester.a2, NO, fix('5'), 0, longTo32Bytes(0), longTo32Bytes(0), 1) assert(orderId2 != bytearray(32)), "saveOrder wasn't executed successfully" assert(orders.getAmount(orderId1) == 10), "amount for order1 should be set to 10" assert(orders.getAmount(orderId2) == 10), "amount for order2 should be set to 10" assert(orders.getPrice(orderId1) == fix('0.5')), "price for order1 should be set to 0.5 ETH" assert(orders.getPrice(orderId2) == fix('0.5')), "price for order2 should be set to 0.5 ETH" assert(orders.getOrderCreator(orderId1) == bytesToHexString(tester.a1)), "orderOwner for order1 should be tester.a1" assert(orders.getOrderCreator(orderId2) == bytesToHexString(tester.a2)), "orderOwner for order2 should be tester.a2" assert orders.getOrderMoneyEscrowed(orderId1) == 0, "money escrowed should be 0" assert orders.getOrderMoneyEscrowed(orderId2) == fix('5'), "money escrowed should be 5 ETH" assert orders.getOrderSharesEscrowed(orderId1) == 10, "shares escrowed should be 10" assert orders.getOrderSharesEscrowed(orderId2) == 0, "shares escrowed should be 0" assert orders.getBetterOrderId(orderId1) == longTo32Bytes(0), "better order id should be 0" assert orders.getBetterOrderId(orderId2) == longTo32Bytes(0), "better order id should be 0" assert orders.getWorseOrderId(orderId1) == longTo32Bytes(0), "worse order id should be 0" assert orders.getWorseOrderId(orderId2) == longTo32Bytes(0), "worse order id should be 0" assert(orders.removeOrder(orderId1) == 1), "Remove order 1" assert(orders.removeOrder(orderId2) == 1), "Remove order 2"
def test_order_filling_taker_reverse(numOutcomes, localFixture, markets): createOrder = localFixture.contracts['CreateOrder'] completeSets = localFixture.contracts['CompleteSets'] fillOrder = localFixture.contracts['FillOrder'] tradeGroupID = longTo32Bytes(42) marketIndex = numOutcomes - 2 market = markets[marketIndex] cost = fix('1', '5000') assert completeSets.publicBuyCompleteSets(market.address, 100, value=1000000, sender=tester.k2) outcome = 0 shareToken = localFixture.applySignature('ShareToken', market.getShareToken(outcome)) shareToken.transfer(tester.a1, 100, sender=tester.k2) orderID = createOrder.publicCreateOrder(BID, fix(1), 5000, market.address, outcome, longTo32Bytes(0), longTo32Bytes(0), "7", value=fix(1, 5000)) startGas = localFixture.chain.head_state.gas_used fillOrder.publicFillOrder(orderID, fix(1), tradeGroupID, False, "0x0000000000000000000000000000000000000000", sender=tester.k1, value=cost) maxGas = localFixture.chain.head_state.gas_used - startGas assert maxGas == FILL_ORDER_TAKER_REVERSE_POSITION[marketIndex]
def test_cancelOrders(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] cancelOrder = contractsFixture.contracts['CancelOrder'] orders = contractsFixture.contracts['Orders'] orderType = BID amount = fix(1) fxpPrice = 60 outcomeID = YES tradeGroupID = longTo32Bytes(42) orderIDs = [] for i in range(10): with BuyWithCash(cash, fix(fxpPrice + i), contractsFixture.accounts[0], "create order"): orderIDs.append(createOrder.publicCreateOrder(orderType, amount, fxpPrice + i, market.address, outcomeID, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, nullAddress)) for i in range(10): assert orders.getAmount(orderIDs[i]) == amount assert cancelOrder.cancelOrders(orderIDs) for i in range(10): assert orders.getAmount(orderIDs[i]) == 0
def test_create_bid_with_cash_fill_with_cash(contractsFixture, cash, market): createOrder = contractsFixture.contracts['CreateOrder'] fillOrder = contractsFixture.contracts['FillOrder'] yesShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(YES)) noShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(NO)) # 1. create BID order for YES with cash escrowed with BuyWithCash(cash, fix('12', '60'), contractsFixture.accounts[1], "create order"): orderID = createOrder.publicCreateOrder( BID, fix(12), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), longTo32Bytes(42), nullAddress, sender=contractsFixture.accounts[1]) assert orderID assert cash.balanceOf(contractsFixture.accounts[1]) == fix('0') assert yesShareToken.balanceOf(contractsFixture.accounts[1]) == 0 assert noShareToken.balanceOf(contractsFixture.accounts[1]) == 0 # 2. fill BID order for YES with cash with BuyWithCash(cash, fix('12', '40'), contractsFixture.accounts[2], "create order"): leftoverInOrder = fillOrder.publicFillOrder( orderID, fix(12), longTo32Bytes(42), "0x0000000000000000000000000000000000000000", sender=contractsFixture.accounts[2]) assert leftoverInOrder == 0 assert cash.balanceOf(contractsFixture.accounts[1]) == fix('0') assert cash.balanceOf(contractsFixture.accounts[2]) == fix('0') assert yesShareToken.balanceOf(contractsFixture.accounts[1]) == fix(12) assert yesShareToken.balanceOf(contractsFixture.accounts[2]) == 0 assert noShareToken.balanceOf(contractsFixture.accounts[1]) == 0 assert noShareToken.balanceOf(contractsFixture.accounts[2]) == fix(12)
def test_exceptions(contractsFixture, cash, market): createOrder = contractsFixture.contracts['CreateOrder'] cancelOrder = contractsFixture.contracts['CancelOrder'] orderType = BID amount = fix(1) fxpPrice = 60 outcomeID = YES tradeGroupID = longTo32Bytes(42) with BuyWithCash(cash, fix(fxpPrice), contractsFixture.accounts[1], "create order"): orderID = createOrder.publicCreateOrder( orderType, amount, fxpPrice, market.address, outcomeID, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, False, nullAddress, sender=contractsFixture.accounts[1]) assert (orderID != bytearray(32)), "Order ID should be non-zero" # cancelOrder exceptions with raises(TransactionFailed): cancelOrder.cancelOrder(longTo32Bytes(0), sender=contractsFixture.accounts[1]) with raises(TransactionFailed): cancelOrder.cancelOrder(longTo32Bytes(1), sender=contractsFixture.accounts[1]) with raises(TransactionFailed): cancelOrder.cancelOrder(orderID, sender=contractsFixture.accounts[2]) assert (cancelOrder.cancelOrder(orderID, sender=contractsFixture.accounts[1]) == 1 ), "cancelOrder should succeed" with raises(TransactionFailed): cancelOrder.cancelOrder(orderID, sender=contractsFixture.accounts[1])
def test_publicCreateOrder_ask(contractsFixture, cash, market): orders = contractsFixture.contracts['Orders'] createOrder = contractsFixture.contracts['CreateOrder'] orderID = createOrder.publicCreateOrder(ASK, fix(1), 4000, market.address, 0, longTo32Bytes(0), longTo32Bytes(0), "7", value=fix(1, 6000)) assert orders.getAmount(orderID) == fix(1) assert orders.getPrice(orderID) == 4000 assert orders.getOrderCreator(orderID) == bytesToHexString(tester.a0) assert orders.getOrderMoneyEscrowed(orderID) == fix(1, 6000) assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == bytearray(32) assert orders.getWorseOrderId(orderID) == bytearray(32) assert cash.balanceOf(market.address) == fix(1, 6000)
def test_fillOrder(contractsFixture, market): orders = contractsFixture.contracts['Orders'] orderId1 = orders.saveOrder(BID, market.address, 10, fix('0.5'), tester.a1, NO, 0, 10, longTo32Bytes(0), longTo32Bytes(0), 1) assert(orderId1 != bytearray(32)), "saveOrder wasn't executed successfully" orderId2 = orders.saveOrder(BID, market.address, 10, fix('0.5'), tester.a2, NO, fix('5'), 0, longTo32Bytes(0), longTo32Bytes(0), 1) assert(orderId2 != bytearray(32)), "saveOrder wasn't executed successfully" # orderID, fill, money, shares with raises(TransactionFailed): orders.fillOrder(orderId1, 11, 0) with raises(TransactionFailed): orders.fillOrder(orderId1, 0, fix('1')) with raises(TransactionFailed): orders.fillOrder(orderId1, 10, fix('1')) # fully fill assert(orders.fillOrder(orderId1, 10, 0) == 1), "fillOrder wasn't executed successfully" # prove all assert orders.getAmount(orderId1) == 0 assert orders.getPrice(orderId1) == 0 assert orders.getOrderCreator(orderId1) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderId1) == 0 assert orders.getOrderSharesEscrowed(orderId1) == 0 assert orders.getBetterOrderId(orderId1) == longTo32Bytes(0) assert orders.getWorseOrderId(orderId1) == longTo32Bytes(0) # test partial fill assert(orders.fillOrder(orderId2, 0, fix('3')) == 1), "fillOrder wasn't executed successfully" # confirm partial fill assert orders.getAmount(orderId2) == 4 assert orders.getPrice(orderId2) == fix('0.5') assert orders.getOrderCreator(orderId2) == bytesToHexString(tester.a2) assert orders.getOrderMoneyEscrowed(orderId2) == fix('2') assert orders.getOrderSharesEscrowed(orderId2) == 0 assert orders.getBetterOrderId(orderId2) == longTo32Bytes(0) assert orders.getWorseOrderId(orderId2) == longTo32Bytes(0) # fill rest of order2 assert(orders.fillOrder(orderId2, 0, fix('2')) == 1), "fillOrder wasn't executed successfully" assert orders.getAmount(orderId2) == 0 assert orders.getPrice(orderId2) == 0 assert orders.getOrderCreator(orderId2) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderId2) == 0 assert orders.getOrderSharesEscrowed(orderId2) == 0 assert orders.getBetterOrderId(orderId2) == longTo32Bytes(0) assert orders.getWorseOrderId(orderId2) == longTo32Bytes(0)
def test_take_best_order(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] orders = contractsFixture.contracts['Orders'] initialTester1ETH = contractsFixture.chain.head_state.get_balance( tester.a1) initialTester2ETH = contractsFixture.chain.head_state.get_balance( tester.a2) # create order with cash orderID = createOrder.publicCreateOrder(ASK, fix(1), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), "42", sender=tester.k1, value=fix('1', '4000')) assert orderID # fill order with cash using on-chain matcher assert trade.publicFillBestOrder(BID, market.address, YES, fix(1), 6000, "43", sender=tester.k2, value=fix('1', '6000')) == 0 assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0)
def test_one_ask_on_books_buy_full_order(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] tradeGroupID = "42" # create order orderID = createOrder.publicCreateOrder(ASK, fix(12), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender=tester.k1, value=fix('12', '4000')) # fill best order fillOrderID = trade.publicBuy(market.address, YES, fix(12), 6000, "0", "0", tradeGroupID, sender=tester.k2, value=fix('12', '6000')) assert orders.getAmount(orderID) == 0 assert orders.getPrice(orderID) == 0 assert orders.getOrderCreator(orderID) == longToHexString(0) assert orders.getOrderMoneyEscrowed(orderID) == 0 assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0) assert fillOrderID == longTo32Bytes(1)
def test_multiple_trades(contractsFixture, cash, market, universe): outcome = YES amount = fix(1) kycToken = nullAddress fillOnly = False account1 = contractsFixture.accounts[1] numOrders = 10 for i in range(numOrders): simulate_then_trade(contractsFixture, SHORT, market, outcome, amount, i + 1, kycToken, fillOnly, sender=account1) simulate_then_trade(contractsFixture, LONG, market, outcome, fix(numOrders - 0.5), numOrders + 10, kycToken, fillOnly)
def test_orderCancelationMax(numOutcomes, localFixture, markets): createOrder = localFixture.contracts['CreateOrder'] cancelOrder = localFixture.contracts['CancelOrder'] completeSets = localFixture.contracts['CompleteSets'] marketIndex = numOutcomes - 2 market = markets[marketIndex] localFixture.contracts["Cash"].faucet(1000000) assert completeSets.publicBuyCompleteSets(market.address, 100) outcome = 0 shareToken = localFixture.applySignature('ShareToken', market.getShareToken(outcome)) shareToken.transfer(localFixture.accounts[7], 100) localFixture.contracts["Cash"].faucet(fix(1, 50)) orderID = createOrder.publicCreateOrder(BID, fix(1), 50, market.address, outcome, longTo32Bytes(0), longTo32Bytes(0), "7", nullAddress) with PrintGasUsed(localFixture, "CANCEL ORDER: %i" % numOutcomes, CANCEL_ORDER_MAXES[numOutcomes - 2]): cancelOrder.cancelOrder(orderID)