def test_redeem(localFixture, universe, cash, market): # Initial report proceedToNextRound(localFixture, market) # Initial losing proceedToNextRound(localFixture, market) # Initial Winning proceedToNextRound(localFixture, market, doGenerateFees=True) initialReporter = localFixture.applySignature( 'InitialReporter', market.getReportingParticipant(0)) winningDisputeCrowdsourcer1 = localFixture.applySignature( 'DisputeCrowdsourcer', market.getReportingParticipant(2)) feeWindow = localFixture.applySignature('FeeWindow', market.getFeeWindow()) assert feeWindow.buy(100) localFixture.contracts["Time"].setTimestamp(feeWindow.getEndTime() + 1) assert market.finalize() with PrintGasUsed(localFixture, "InitialReporter:redeem", INITIAL_REPORT_REDEMPTION): initialReporter.redeem(tester.a0) with PrintGasUsed(localFixture, "DisputeCrowdsourcer:redeem", CROWDSOURCER_REDEMPTION): winningDisputeCrowdsourcer1.redeem(tester.a0) with PrintGasUsed(localFixture, "FeeWindow:redeem", PARTICIPATION_TOKEN_REDEMPTION): feeWindow.redeem(tester.a0)
def test_warp_sync(contractsFixture, augur, universe, reputationToken, warpSync, cash): account = contractsFixture.accounts[0] time = contractsFixture.contracts["Time"] # See that warp sync market does not exist initially assert warpSync.markets(universe.address) == nullAddress # We can initialize the warp sync market for a universe and be rewarded with REP based on how long since the universe was created expectedCreationReward = warpSync.getCreationReward(universe.address) with PrintGasUsed(contractsFixture, "WS Market Finalization Cost", 0): with TokenDelta(reputationToken, expectedCreationReward, account, "REP reward not minted for initializing universe"): warpSync.initializeUniverse(universe.address) # The market now exists market = contractsFixture.applySignature("Market", warpSync.markets(universe.address)) # Initially there is no warp sync data for this universe assert warpSync.data(universe.address) == [0, 0] # Finalize the warp sync market with some value proceedToDesignatedReporting(contractsFixture, market) numTicks = market.getNumTicks() assert warpSync.doInitialReport(universe.address, [0,0,numTicks], "") disputeWindow = contractsFixture.applySignature("DisputeWindow", market.getDisputeWindow()) time.setTimestamp(disputeWindow.getEndTime()) # Finalizing the warp sync market will award the finalizer REP based on time since it became finalizable expectedFinalizationReward = warpSync.getFinalizationReward(market.address) WarpSyncDataUpdatedLog = { "universe": universe.address, "warpSyncHash": numTicks, "marketEndTime": market.getEndTime() } with AssertLog(contractsFixture, "WarpSyncDataUpdated", WarpSyncDataUpdatedLog): with PrintGasUsed(contractsFixture, "WS Market Finalization Cost", 0): with TokenDelta(reputationToken, expectedFinalizationReward, account, "REP reward not minted for finalizer"): assert market.finalize() # Check Warp Sync contract for universe and see existing value assert warpSync.data(universe.address) == [numTicks, market.getEndTime()] # See new warp sync market newWarpSyncMarket = contractsFixture.applySignature("Market", warpSync.markets(universe.address)) assert newWarpSyncMarket.address != market.address # Finalize it proceedToInitialReporting(contractsFixture, newWarpSyncMarket) numTicks = newWarpSyncMarket.getNumTicks() assert newWarpSyncMarket.doInitialReport([0,1,numTicks-1], "", 0) disputeWindow = contractsFixture.applySignature("DisputeWindow", newWarpSyncMarket.getDisputeWindow()) time.setTimestamp(disputeWindow.getEndTime()) assert newWarpSyncMarket.finalize() # See new warp sync value assert warpSync.data(universe.address) == [numTicks-1, newWarpSyncMarket.getEndTime()] # See another new market assert newWarpSyncMarket.address != warpSync.markets(universe.address)
def test_report(localFixture, universe, cash, market): proceedToFirstReporting(localFixture, universe, market, False, 1, [0, 10**18], [10**18, 0]) stakeTokenYes = localFixture.getStakeToken(market, [0, 10**18]) with PrintGasUsed(localFixture, "FIRST StakeToken:buy", FIRST_REPORT): stakeTokenYes.buy(0, sender=tester.k2) with PrintGasUsed(localFixture, "SECOND StakeToken:buy", SECOND_REPORT): stakeTokenYes.buy(1, sender=tester.k2)
def test_orderCreation(hints, localFixture, categoricalMarket): createOrder = localFixture.contracts['CreateOrder'] for i in range(39, 43): localFixture.contracts["Cash"].faucet(fix(1, i)) createOrder.publicCreateOrder(BID, fix(1), i, categoricalMarket.address, 1, longTo32Bytes(0), longTo32Bytes(0), "7", False, nullAddress) localFixture.contracts["Cash"].faucet(fix(1, 44)) worseOrderId = createOrder.publicCreateOrder(BID, fix(1), 44, categoricalMarket.address, 1, longTo32Bytes(0), longTo32Bytes(0), "7", False, nullAddress) localFixture.contracts["Cash"].faucet(fix(1, 46)) betterOrderId = createOrder.publicCreateOrder(BID, fix(1), 46, categoricalMarket.address, 1, longTo32Bytes(0), longTo32Bytes(0), "7", False, nullAddress) for i in range(47, 80): localFixture.contracts["Cash"].faucet(fix(1, i)) createOrder.publicCreateOrder(BID, fix(1), i, categoricalMarket.address, 1, longTo32Bytes(0), longTo32Bytes(0), "7", False, nullAddress) if not hints: with PrintGasUsed(localFixture, "CreateOrder:publicCreateOrder NO Hints", CREATE_ORDER): localFixture.contracts["Cash"].faucet(fix(1, 45)) orderID = createOrder.publicCreateOrder(BID, fix(1), 45, categoricalMarket.address, 1, longTo32Bytes(0), longTo32Bytes(0), "7", False, nullAddress) else: with PrintGasUsed(localFixture, "CreateOrder:publicCreateOrder HINTS", CREATE_ORDER): localFixture.contracts["Cash"].faucet(fix(1, 45)) orderID = createOrder.publicCreateOrder(BID, fix(1), 45, categoricalMarket.address, 1, betterOrderId, worseOrderId, "7", False, nullAddress)
def test_redeemParticipation(localFixture, universe, cash, market, categoricalMarket, scalarMarket): proceedToDesignatedReporting(localFixture, universe, market, [0, 10**18]) assert localFixture.designatedReport(market, [0, 10**18], tester.k0) assert localFixture.designatedReport( categoricalMarket, [0, 0, categoricalMarket.getNumTicks()], tester.k0) assert localFixture.designatedReport(scalarMarket, [0, scalarMarket.getNumTicks()], tester.k0) reportingWindow = localFixture.applySignature('ReportingWindow', market.getReportingWindow()) localFixture.chain.head_state.timestamp = reportingWindow.getEndTime() + 1 assert market.tryFinalize() assert categoricalMarket.tryFinalize() assert scalarMarket.tryFinalize() # We cannot purchase participation tokens yet since the window isn't active participationToken = localFixture.applySignature( "ParticipationToken", reportingWindow.getParticipationToken()) # We'll progress to the start of the window and purchase some participation tokens localFixture.chain.head_state.timestamp = reportingWindow.getStartTime( ) + 1 assert participationToken.buy(1) # Fast forward time until the window is over and we can redeem our winning stake and participation tokens and receive fees localFixture.chain.head_state.timestamp = reportingWindow.getEndTime() + 1 with PrintGasUsed(localFixture, "DisputeBond:withdraw", PARTICIPATION_TOKEN_REDEMPTION): assert participationToken.redeem(False)
def test_redeemDispute(localFixture, universe, cash, market, categoricalMarket, scalarMarket): proceedToDesignatedReporting(localFixture, universe, market, [0, 10**18]) assert localFixture.designatedReport(market, [0, 10**18], tester.k0) assert localFixture.designatedReport( categoricalMarket, [0, 0, categoricalMarket.getNumTicks()], tester.k0) assert localFixture.designatedReport(scalarMarket, [0, scalarMarket.getNumTicks()], tester.k0) assert market.disputeDesignatedReport([10**18, 0], 1, False) reportingWindow = localFixture.applySignature('ReportingWindow', market.getReportingWindow()) localFixture.chain.head_state.timestamp = reportingWindow.getEndTime() + 1 assert market.tryFinalize() assert categoricalMarket.tryFinalize() assert scalarMarket.tryFinalize() disputeBond = localFixture.applySignature( "DisputeBond", market.getDesignatedReporterDisputeBond()) with PrintGasUsed(localFixture, "DisputeBond:withdraw", DISPUTE_BOND_REDEMPTION): assert disputeBond.withdraw()
def test_orderFilling(localFixture, market): createOrder = localFixture.contracts['CreateOrder'] fillOrder = localFixture.contracts['FillOrder'] tradeGroupID = 42 creatorCost = fix('2', '0.6') fillerCost = fix('2', '0.4') orderID = createOrder.publicCreateOrder(BID, 2, fix('0.6'), market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender=tester.k1, value=creatorCost) with PrintGasUsed(localFixture, "FillOrder:publicFillOrder", FILL_ORDER): fillOrderID = fillOrder.publicFillOrder(orderID, 2, tradeGroupID, sender=tester.k2, value=fillerCost)
def test_one_bid_on_books_buy_partial_order_fill_loop_limit(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] orders = contractsFixture.contracts['Orders'] tradeGroupID = longTo32Bytes(42) # create order with BuyWithCash(cash, fix('2', '60'), contractsFixture.accounts[1], "trade 1"): orderID = createOrder.publicCreateOrder(BID, fix(2), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, nullAddress, sender = contractsFixture.accounts[1]) # fill best order orderEventLog = { "eventType": 2, "addressData": [nullAddress, contractsFixture.accounts[1], contractsFixture.accounts[2]], "uint256Data": [60, fix(1), YES, 0, 0, 0, fix(1), contractsFixture.contracts['Time'].getTimestamp(), 0, fix(1, 60)], } with BuyWithCash(cash, fix('1', '40'), contractsFixture.accounts[2], "trade 2"): with AssertLog(contractsFixture, "OrderEvent", orderEventLog): with PrintGasUsed(contractsFixture, "publicTrade", 0): fillOrderID = trade.publicTrade(1, market.address, YES, fix(1), 60, "0", "0", tradeGroupID, 6, longTo32Bytes(11), nullAddress, sender=contractsFixture.accounts[2]) assert orders.getAmount(orderID) == fix(1) assert orders.getPrice(orderID) == 60 assert orders.getOrderCreator(orderID) == contractsFixture.accounts[1] assert orders.getOrderMoneyEscrowed(orderID) == fix('1', '60') assert orders.getOrderSharesEscrowed(orderID) == 0 assert orders.getBetterOrderId(orderID) == longTo32Bytes(0) assert orders.getWorseOrderId(orderID) == longTo32Bytes(0) assert fillOrderID == longTo32Bytes(1)
def test_orderFilling(localFixture, market): createOrder = localFixture.contracts['CreateOrder'] fillOrder = localFixture.contracts['FillOrder'] tradeGroupID = longTo32Bytes(42) creatorCost = fix('2', '40') fillerCost = fix('2', '60') # create order localFixture.contracts["Cash"].faucet(creatorCost, sender=localFixture.accounts[1]) orderID = createOrder.publicCreateOrder(ASK, fix(2), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, False, nullAddress, sender=localFixture.accounts[1]) with PrintGasUsed(localFixture, "FillOrder:publicFillOrder", FILL_ORDER): localFixture.contracts["Cash"].faucet(fillerCost, sender=localFixture.accounts[2]) fillOrderID = fillOrder.publicFillOrder( orderID, fix(2), tradeGroupID, False, "0x0000000000000000000000000000000000000000", sender=localFixture.accounts[2])
def test_order_filling_double_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', '50') localFixture.contracts["Cash"].faucet(1000000) assert completeSets.publicBuyCompleteSets(market.address, 100) outcome = 0 shareToken = localFixture.applySignature('ShareToken', market.getShareToken(outcome)) shareToken.transfer(localFixture.accounts[1], 100) localFixture.contracts["Cash"].faucet(cost) orderID = createOrder.publicCreateOrder(BID, fix(1), 50, market.address, outcome, longTo32Bytes(0), longTo32Bytes(0), "7", nullAddress) localFixture.contracts["Cash"].faucet(cost, sender=localFixture.accounts[1]) with PrintGasUsed(localFixture, "FILL ORDER BOTH REVERSE: %i" % numOutcomes, FILL_ORDER_DOUBLE_REVERSE_POSITION[numOutcomes - 2]): fillOrder.publicFillOrder(orderID, fix(1), tradeGroupID, "0x0000000000000000000000000000000000000000", sender=localFixture.accounts[1])
def test_order_filling_both_eth(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', '50') outcome = 0 localFixture.contracts["Cash"].faucet(fix(1, 50)) orderID = createOrder.publicCreateOrder(BID, fix(1), 50, market.address, outcome, longTo32Bytes(0), longTo32Bytes(0), "7", nullAddress) localFixture.contracts["Cash"].faucet(cost, sender=localFixture.accounts[1]) with PrintGasUsed(localFixture, "FILL ORDER BOTH ETH: %i" % numOutcomes, FILL_ORDER_BOTH_ETH[numOutcomes - 2]): fillOrder.publicFillOrder(orderID, fix(1), tradeGroupID, "0x0000000000000000000000000000000000000000", sender=localFixture.accounts[1])
def test_order_filling_take_shares(numOutcomes, localFixture, markets): createOrder = localFixture.contracts['CreateOrder'] completeSets = localFixture.contracts['CompleteSets'] fillOrder = localFixture.contracts['FillOrder'] cash = localFixture.contracts['Cash'] tradeGroupID = longTo32Bytes(42) marketIndex = numOutcomes - 2 market = markets[marketIndex] cost = 100 * market.getNumTicks() with BuyWithCash(cash, cost, localFixture.accounts[0], "buy complete set"): assert completeSets.publicBuyCompleteSets(market.address, 100) outcome = 0 orderID = createOrder.publicCreateOrder(ASK, 100, 50, market.address, outcome, longTo32Bytes(0), longTo32Bytes(0), longTo32Bytes(7), nullAddress) cost = 50000 localFixture.contracts["Cash"].faucet(cost, sender=localFixture.accounts[1]) with PrintGasUsed(localFixture, "FILL ORDER TAKE SHARES: %i" % numOutcomes, FILL_ORDER_TAKE_SHARES[numOutcomes - 2]): fillOrder.publicFillOrder(orderID, fix(1), tradeGroupID, "0x0000000000000000000000000000000000000000", sender=localFixture.accounts[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: 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_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 with PrintGasUsed(contractsFixture, "fill multiple asks", 0): 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_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 with PrintGasUsed(contractsFixture, "Fill two", 0): 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_order_filling_both_eth(numOutcomes, localFixture, markets): ZeroXTrade = localFixture.contracts['ZeroXTrade'] cash = localFixture.contracts['Cash'] shareToken = localFixture.contracts["ShareToken"] expirationTime = localFixture.contracts['Time'].getTimestamp() + 10000 salt = 5 tradeGroupID = longTo32Bytes(42) marketIndex = numOutcomes - 2 market = markets[marketIndex] amount = fix(10) cost = amount * 50 outcome = 0 localFixture.contracts["Cash"].faucet(cost) rawZeroXOrderData, orderHash = ZeroXTrade.createZeroXOrder( ASK, amount, 50, market.address, outcome, expirationTime, salt) signature = signOrder(orderHash, localFixture.privateKeys[0]) fingerprint = longTo32Bytes(11) orders = [rawZeroXOrderData] signatures = [signature] localFixture.contracts["Cash"].faucet(cost, sender=localFixture.accounts[1]) with PrintGasUsed(localFixture, "FILL ORDER BOTH ETH: %i" % numOutcomes, FILL_ORDER_BOTH_ETH[numOutcomes - 2]): ZeroXTrade.trade(amount, fingerprint, tradeGroupID, 0, 10, orders, signatures, sender=localFixture.accounts[1])
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_bid_on_books_buy_partial_order_fill_loop_limit(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] orders = contractsFixture.contracts['Orders'] tradeGroupID = longTo32Bytes(42) # create order with BuyWithCash(cash, fix('2', '6000'), tester.k1, "trade 1"): orderID = createOrder.publicCreateOrder(BID, fix(2), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, False, nullAddress, sender = tester.k1) # fill best order orderFilledLog = { "amountFilled": fix(1), } with BuyWithCash(cash, fix('1', '4000'), tester.k2, "trade 2"): with AssertLog(contractsFixture, "OrderFilled", orderFilledLog): with PrintGasUsed(contractsFixture, "publicTrade", 0): fillOrderID = trade.publicTrade(1, market.address, YES, fix(1), 6000, "0", "0", tradeGroupID, 6, False, nullAddress, nullAddress, sender=tester.k2) 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_marketCreation(localFixture, universe, cash): marketCreationFee = universe.getMarketCreationCost() endTime = long(localFixture.chain.head_state.timestamp + timedelta(days=1).total_seconds()) feePerEthInWei = 10**16 denominationToken = cash designatedReporterAddress = tester.a0 numTicks = 10**18 numOutcomes = 2 reportingWindow = localFixture.applySignature( 'ReportingWindow', universe.getReportingWindowByMarketEndTime(endTime)) with PrintGasUsed(localFixture, "ReportingWindow:createMarket", MARKET_CREATION): marketAddress = reportingWindow.createMarket(endTime, numOutcomes, numTicks, feePerEthInWei, denominationToken.address, designatedReporterAddress, "", value=marketCreationFee, startgas=long(6.7 * 10**6))
def test_take_best_order_multiple_orders(contractsFixture, cash, market): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] orders = contractsFixture.contracts['Orders'] # create orders with cash orderIDs = [] numOrders = 5 for i in range(numOrders): with BuyWithCash(cash, fix('1', 4000 - i), tester.k1, "create order"): orderID = createOrder.publicCreateOrder(ASK, fix(1), 6000 + i, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), longTo32Bytes(42), False, nullAddress, sender=tester.k1) assert orderID orderIDs.append(orderID) # fill orders with cash using on-chain matcher price = 6000 + numOrders with PrintGasUsed(contractsFixture, "fill multiple asks", 0): # Fills across orders of differing prices, give it some eth to play with assert cash.faucet(fix(numOrders, price), sender=tester.k1) assert trade.publicFillBestOrder(BID, market.address, YES, fix(numOrders), price, "43", 6, False, nullAddress, nullAddress, sender=tester.k1) == 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_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_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_two_bids_on_books_buy_one_with_limit(contractsFixture, cash, market, universe): createOrder = contractsFixture.contracts['CreateOrder'] trade = contractsFixture.contracts['Trade'] fillOrder = contractsFixture.contracts['FillOrder'] orders = contractsFixture.contracts['Orders'] tradeGroupID = longTo32Bytes(42) with BuyWithCash(cash, fix('4', '6000'), tester.k1, "create order 1"): orderID1 = createOrder.publicCreateOrder(BID, fix(4), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, False, nullAddress, sender = tester.k1) with BuyWithCash(cash, fix('1', '6000'), tester.k3, "create order 2"): 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('4', '4000'), tester.k2, "buy complete set"): fillOrderID = trade.publicTrade(SHORT,market.address, YES, fix(5), 6000, "0", "0", tradeGroupID, 1, 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(1) # We dont create an order since an existing match is on the books assert fillOrderID == longTo32Bytes(1)
def test_feeWindowCreation(localFixture, universe, cash): endTime = long(localFixture.chain.head_state.timestamp + timedelta(days=365).total_seconds()) with PrintGasUsed(localFixture, "REPORTING_WINDOW_CREATE", REPORTING_WINDOW_CREATE): universe.getOrCreateFeeWindowByTimestamp(endTime)
def test_order_filling_take_shares(numOutcomes, localFixture, markets): ZeroXTrade = localFixture.contracts['ZeroXTrade'] cash = localFixture.contracts['Cash'] shareToken = localFixture.contracts["ShareToken"] expirationTime = localFixture.contracts['Time'].getTimestamp() + 10000 salt = 5 tradeGroupID = longTo32Bytes(42) marketIndex = numOutcomes - 2 market = markets[marketIndex] amount = fix(10) cost = amount * market.getNumTicks() with BuyWithCash(cash, cost, localFixture.accounts[0], "buy complete set"): assert shareToken.publicBuyCompleteSets(market.address, amount) outcome = 0 rawZeroXOrderData, orderHash = ZeroXTrade.createZeroXOrder( ASK, amount, 50, market.address, outcome, expirationTime, salt) signature = signOrder(orderHash, localFixture.privateKeys[0]) fingerprint = longTo32Bytes(11) orders = [rawZeroXOrderData] signatures = [signature] cost = amount * 50 localFixture.contracts["Cash"].faucet(cost, sender=localFixture.accounts[1]) with PrintGasUsed(localFixture, "FILL ORDER TAKE SHARES: %i" % numOutcomes, FILL_ORDER_TAKE_SHARES[numOutcomes - 2]): ZeroXTrade.trade(amount, fingerprint, tradeGroupID, 0, 10, orders, signatures, sender=localFixture.accounts[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_fill_order_with_shares(localFixture, zeroX, market, cash, augur): expirationTimestampInSec = augur.getTimestamp() + 1 orderAddresses = [localFixture.accounts[0], market.address] orderValues = [YES, ASK, 10, 10, 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 = localFixture.accounts[1]) yesShareAddress = market.getShareToken(YES) noShareAddress = market.getShareToken(NO) invalidShareAddress = market.getShareToken(0) yesShareToken = localFixture.applySignature('ShareToken', yesShareAddress) noShareToken = localFixture.applySignature('ShareToken', noShareAddress) invalidShareToken = localFixture.applySignature('ShareToken', invalidShareAddress) completeSets = localFixture.contracts['CompleteSets'] cash.faucet(fix('20', market.getNumTicks())) assert completeSets.publicBuyCompleteSets(market.address, fix(20)) assert noShareToken.transfer(localFixture.accounts[1], 10) assert invalidShareToken.transfer(localFixture.accounts[1], 10) assert yesShareToken.approve(zeroX.address, 10) assert zeroX.deposit(yesShareAddress, 10) assert noShareToken.approve(zeroX.address, 10, sender=localFixture.accounts[1]) assert zeroX.deposit(noShareAddress, 10, sender=localFixture.accounts[1]) assert invalidShareToken.approve(zeroX.address, 10, sender=localFixture.accounts[1]) assert zeroX.deposit(invalidShareAddress, 10, sender=localFixture.accounts[1]) with PrintGasUsed(localFixture, "FILL_0X"): assert zeroX.fillOrder( orderAddresses, orderValues, fillAmount, v, r, s, sender = localFixture.accounts[1]) yesShareAddress = market.getShareToken(YES) noShareAddress = market.getShareToken(NO) assert zeroX.getTokenBalance(cash.address, localFixture.accounts[0]) == 49 assert zeroX.getTokenBalance(yesShareAddress, localFixture.accounts[0]) == 5 assert zeroX.getTokenBalance(cash.address, localFixture.accounts[1]) == 441 assert zeroX.getTokenBalance(noShareAddress, localFixture.accounts[1]) == 5 assert zeroX.getUnavailableAmount(orderHash) == fillAmount
def proceedToNextRound(fixture, market, contributor=tester.k0, doGenerateFees=False, moveTimeForward=True, randomPayoutNumerators=False, description=""): if fixture.contracts["Controller"].getTimestamp() < market.getEndTime(): fixture.contracts["Time"].setTimestamp( market.getDesignatedReportingEndTime() + 1) feeWindow = market.getFeeWindow() payoutNumerators = [0] * market.getNumberOfOutcomes() payoutNumerators[0] = market.getNumTicks() if (feeWindow == longToHexString(0)): market.doInitialReport(payoutNumerators, False, "") assert market.getFeeWindow() else: feeWindow = fixture.applySignature('FeeWindow', market.getFeeWindow()) if market.getDisputePacingOn(): fixture.contracts["Time"].setTimestamp(feeWindow.getStartTime() + 1) # This will also use the InitialReporter which is not a DisputeCrowdsourcer, but has the called function from abstract inheritance winningReport = fixture.applySignature( 'DisputeCrowdsourcer', market.getWinningReportingParticipant()) winningPayoutHash = winningReport.getPayoutDistributionHash() if (randomPayoutNumerators): chosenPayoutNumerators = [0] * market.getNumberOfOutcomes() chosenPayoutNumerators[0] = randint(0, market.getNumTicks()) chosenPayoutNumerators[1] = market.getNumTicks( ) - chosenPayoutNumerators[0] else: firstReportWinning = market.derivePayoutDistributionHash( payoutNumerators, False) == winningPayoutHash chosenPayoutNumerators = payoutNumerators if not firstReportWinning else payoutNumerators[:: -1] chosenPayoutHash = market.derivePayoutDistributionHash( chosenPayoutNumerators, False) amount = 2 * market.getParticipantStake( ) - 3 * market.getStakeInOutcome(chosenPayoutHash) with PrintGasUsed(fixture, "Contribute:", 0): market.contribute(chosenPayoutNumerators, False, amount, description, sender=contributor) assert market.getForkingMarket() or market.getFeeWindow() != feeWindow if (doGenerateFees): universe = fixture.applySignature("Universe", market.getUniverse()) generateFees(fixture, universe, market) if (moveTimeForward): feeWindow = fixture.applySignature('FeeWindow', market.getFeeWindow()) fixture.contracts["Time"].setTimestamp(feeWindow.getStartTime() + 1)
def test_redeem_reporting_participants(kitchenSinkFixture, market, categoricalMarket, scalarMarket, universe, cash): redeemStake = kitchenSinkFixture.contracts["RedeemStake"] reputationToken = kitchenSinkFixture.applySignature( "ReputationToken", universe.getReputationToken()) constants = kitchenSinkFixture.contracts["Constants"] # Initial Report proceedToNextRound(kitchenSinkFixture, market, doGenerateFees=True) # Initial Report Losing proceedToNextRound(kitchenSinkFixture, market, doGenerateFees=True) # Initial Report Winning proceedToNextRound(kitchenSinkFixture, market, doGenerateFees=True) # Initial Report Losing proceedToNextRound(kitchenSinkFixture, market, doGenerateFees=True) # Initial Report Winning proceedToNextRound(kitchenSinkFixture, market, doGenerateFees=True) # Get the winning reporting participants initialReporter = kitchenSinkFixture.applySignature( 'InitialReporter', market.getReportingParticipant(0)) winningDisputeCrowdsourcer1 = kitchenSinkFixture.applySignature( 'DisputeCrowdsourcer', market.getReportingParticipant(2)) winningDisputeCrowdsourcer2 = kitchenSinkFixture.applySignature( 'DisputeCrowdsourcer', market.getReportingParticipant(4)) # Fast forward time until the new dispute window is over and we can redeem disputeWindow = kitchenSinkFixture.applySignature( "DisputeWindow", market.getDisputeWindow()) # Purchase PTs and inject fees into the window ptAmount = 10**18 additionalfees = 10**18 assert cash.faucet(additionalfees) assert cash.transfer(disputeWindow.address, additionalfees) assert disputeWindow.buy(ptAmount) kitchenSinkFixture.contracts["Time"].setTimestamp( disputeWindow.getEndTime() + 1) assert market.finalize() expectedRep = winningDisputeCrowdsourcer2.getStake( ) + winningDisputeCrowdsourcer1.getStake() + initialReporter.getStake() expectedRep = expectedRep + expectedRep * 2 / 5 expectedRep += ptAmount expectedRep -= 1 # Rounding error fees = cash.balanceOf(disputeWindow.address) with TokenDelta(reputationToken, expectedRep, kitchenSinkFixture.accounts[0], "Redeeming didn't refund REP"): with TokenDelta(cash, fees, kitchenSinkFixture.accounts[0], "Redeeming didn't pay out fees"): with PrintGasUsed(kitchenSinkFixture, "Universe Redeem:", 0): assert redeemStake.redeemStake([ initialReporter.address, winningDisputeCrowdsourcer1.address, winningDisputeCrowdsourcer2.address ], [disputeWindow.address])
def test_marketFinalization(localFixture, universe, market): proceedToNextRound(localFixture, market) feeWindow = localFixture.applySignature('FeeWindow', market.getFeeWindow()) localFixture.contracts["Time"].setTimestamp(feeWindow.getEndTime() + 1) with PrintGasUsed(localFixture, "Market:finalize", MARKET_FINALIZATION): assert market.finalize()