Ejemplo n.º 1
0
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', '4000'), tester.k1, "create order"):
        askOrderID = createOrder.publicCreateOrder(ASK, fix(12), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), longTo32Bytes(42), False, sender= tester.k1)
    assert askOrderID
    assert cash.balanceOf(tester.a1) == fix('0')
    assert yesShareToken.balanceOf(tester.a1) == 0
    assert noShareToken.balanceOf(tester.a1) == 0

    # 2. fill ASK order for YES with cash
    with BuyWithCash(cash, fix('12', '6000'), tester.k2, "create order"):
        amountRemaining = fillOrder.publicFillOrder(askOrderID, fix(12), longTo32Bytes(42), False, "0x0000000000000000000000000000000000000000", sender = tester.k2)
    assert amountRemaining == 0
    assert cash.balanceOf(tester.a1) == fix('0')
    assert cash.balanceOf(tester.a2) == fix('0')
    assert yesShareToken.balanceOf(tester.a1) == 0
    assert yesShareToken.balanceOf(tester.a2) == fix(12)
    assert noShareToken.balanceOf(tester.a1) == fix(12)
    assert noShareToken.balanceOf(tester.a2) == 0
Ejemplo n.º 2
0
def test_two_asks_on_books_buy_full_and_partial(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, "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
    with BuyWithCash(cash, fix('7', '4000'), tester.k3, "buy complete set"):
        orderID2 = createOrder.publicCreateOrder(ASK, fix(7), 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) == 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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
def test_one_bid_on_books_buy_full_order(withSelf, contractsFixture, cash, market, universe):
    createOrder = contractsFixture.contracts['CreateOrder']
    trade = contractsFixture.contracts['Trade']
    orders = contractsFixture.contracts['Orders']
    tradeGroupID = longTo32Bytes(42)

    # create order
    sender = tester.k2 if withSelf else tester.k1
    with BuyWithCash(cash, fix('2', '6000'), sender, "create order"):
        orderID = createOrder.publicCreateOrder(BID, fix(2), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, False, nullAddress, sender = sender)

    # fill best order
    orderFilledLog = {
        "filler": bytesToHexString(tester.a2),
        "marketCreatorFees": 0,
        "reporterFees": 0,
        "tradeGroupId": stringToBytes(longTo32Bytes(42)),
    }
    with BuyWithCash(cash, fix('2', '4000'), tester.k2, "fill order"):
        with AssertLog(contractsFixture, "OrderFilled", orderFilledLog):
            assert trade.publicTrade(SHORT,market.address, YES, fix(2), 6000, "0", "0", tradeGroupID, 6, False, nullAddress, nullAddress, 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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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 = longTo32Bytes(42)
    fingerprint = longTo32Bytes(11)

    creatorCost = fix('2', '60')
    fillerCost = fix('2', market.getNumTicks()-60)

    # create order
    with BuyWithCash(cash, creatorCost, contractsFixture.accounts[1], "creating order"):
        orderID = createOrder.publicCreateOrder(BID, fix(2), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender=contractsFixture.accounts[1])

    # fill best order
    with BuyWithCash(cash, fillerCost, contractsFixture.accounts[2], "filling order"):
        fillOrderID = fillOrder.publicFillOrder(orderID, fix(2), tradeGroupID, fingerprint, 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 fillOrderID == 0
Ejemplo n.º 7
0
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
    with BuyWithCash(cash, creatorCost, tester.k1, "creating order"):
        orderID = createOrder.publicCreateOrder(ASK, fix(2), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = tester.k1)

    # fill best order
    with BuyWithCash(cash, fillerCost, tester.k2, "filling order"):
        fillOrderID = fillOrder.publicFillOrder(orderID, fix(2), tradeGroupID, sender = tester.k2)

    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
Ejemplo n.º 8
0
def test_two_bids_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 = longTo32Bytes(42)

    # create order 1
    with BuyWithCash(cash, fix('12', '60'), contractsFixture.accounts[1], "create order"):
        orderID1 = createOrder.publicCreateOrder(BID, fix(12), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, nullAddress, sender = contractsFixture.accounts[1])
    # create order 2
    with BuyWithCash(cash, fix('7', '60'), contractsFixture.accounts[3], "create order"):
        orderID2 = createOrder.publicCreateOrder(BID, fix(7), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, nullAddress, sender = contractsFixture.accounts[3])

    # fill best order
    with BuyWithCash(cash, fix('15', '40'), contractsFixture.accounts[2], "trade"):
        fillOrderID = trade.publicTrade(SHORT,market.address, YES, fix(15), 60, "0", "0", tradeGroupID, 6, longTo32Bytes(11), nullAddress, sender = contractsFixture.accounts[2])

    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) == 60
    assert orders.getOrderCreator(orderID2) == contractsFixture.accounts[3]
    assert orders.getOrderMoneyEscrowed(orderID2) == fix('4', '60')
    assert orders.getOrderSharesEscrowed(orderID2) == 0
    assert orders.getBetterOrderId(orderID2) == longTo32Bytes(0)
    assert orders.getWorseOrderId(orderID2) == longTo32Bytes(0)

    assert fillOrderID == longTo32Bytes(1)
Ejemplo n.º 9
0
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', '40'), contractsFixture.accounts[1], "buy complete set"):
        orderID1 = createOrder.publicCreateOrder(ASK, fix(12), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, nullAddress, sender = contractsFixture.accounts[1])
    # create order 2
    with BuyWithCash(cash, fix('3', '40'), contractsFixture.accounts[3], "buy complete set"):
        orderID2 = createOrder.publicCreateOrder(ASK, fix(3), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, nullAddress, sender = contractsFixture.accounts[3])

    # 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(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)
Ejemplo n.º 10
0
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, tester.k0, "buy complete set"):
        assert completeSets.publicBuyCompleteSets(market.address, 100)
    outcome = 0
    orderID = createOrder.publicCreateOrder(ASK, 100, 5000,
                                            market.address, outcome,
                                            longTo32Bytes(0), longTo32Bytes(0),
                                            longTo32Bytes(7), False)

    cost = 500000
    with BuyWithCash(cash, cost, tester.k1, "fill order"):
        startGas = localFixture.chain.head_state.gas_used
        fillOrder.publicFillOrder(orderID,
                                  fix(1),
                                  tradeGroupID,
                                  False,
                                  "0x0000000000000000000000000000000000000000",
                                  sender=tester.k1)
        maxGas = localFixture.chain.head_state.gas_used - startGas
        assert maxGas == FILL_ORDER_TAKE_SHARES[marketIndex]
Ejemplo n.º 11
0
def test_one_bid_on_books_buy_full_order(withSelf, contractsFixture, cash, market, universe):
    createOrder = contractsFixture.contracts['CreateOrder']
    trade = contractsFixture.contracts['Trade']
    orders = contractsFixture.contracts['Orders']
    tradeGroupID = longTo32Bytes(42)

    # create order
    sender = contractsFixture.accounts[2] if withSelf else contractsFixture.accounts[1]
    with BuyWithCash(cash, fix('2', '60'), sender, "create order"):
        orderID = createOrder.publicCreateOrder(BID, fix(2), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, nullAddress, sender = sender)

    # fill best order
    orderEventLog = {
	    "eventType": 2,
	    "addressData": [nullAddress, contractsFixture.accounts[2] if withSelf else contractsFixture.accounts[1] , contractsFixture.accounts[2]],
	    "uint256Data": [60, 0, YES, 0, 0, 0, fix(2),  contractsFixture.contracts['Time'].getTimestamp(), 0, 0],
    }
    with BuyWithCash(cash, fix('2', '40'), contractsFixture.accounts[2], "fill order"):
        with AssertLog(contractsFixture, "OrderEvent", orderEventLog):
            assert trade.publicTrade(SHORT,market.address, YES, fix(2), 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)
Ejemplo n.º 12
0
def test_fill_order_with_shares_escrowed_sell_with_shares(contractsFixture, cash, market, universe, tokensFail):
    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
    with BuyWithCash(cash, fix('1', '100'), contractsFixture.accounts[1], "buy complete set"):
        assert completeSets.publicBuyCompleteSets(market.address, fix(1), sender=contractsFixture.accounts[1])
    assert yesShareToken.balanceOf(contractsFixture.accounts[1]) == fix(1)
    with BuyWithCash(cash, fix('1', '100'), contractsFixture.accounts[0], "buy complete set"):
        assert completeSets.publicBuyCompleteSets(market.address, fix(1))
    assert noShareToken.balanceOf(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), False, nullAddress, sender=contractsFixture.accounts[1])
    assert orderID

    # fill order with shares
    tokensFail.setFail(True)
    assert fillOrder.publicFillOrder(orderID, fix(1), "43", False, "0x0000000000000000000000000000000000000000") == 0

    tokensFail.setFail(False)
    assert noShareToken.balanceOf(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)
Ejemplo n.º 13
0
def test_create_bid_with_cash_fill_with_shares(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 complete sets with account 2
    with BuyWithCash(cash, fix('12', market.getNumTicks()), tester.k2,
                     "buy complete set"):
        assert completeSets.publicBuyCompleteSets(market.address,
                                                  fix(12),
                                                  sender=tester.k2)
    assert cash.balanceOf(tester.a2) == fix('0')
    assert yesShareToken.balanceOf(tester.a2) == fix(12)
    assert noShareToken.balanceOf(tester.a2) == fix(12)

    # 2. create BID order for YES with cash escrowed
    with BuyWithCash(cash, fix('12', '60'), tester.k1, "create order"):
        orderID = createOrder.publicCreateOrder(BID,
                                                fix(12),
                                                60,
                                                market.address,
                                                YES,
                                                longTo32Bytes(0),
                                                longTo32Bytes(0),
                                                longTo32Bytes(42),
                                                False,
                                                nullAddress,
                                                sender=tester.k1)
    assert orderID
    assert cash.balanceOf(tester.a1) == fix('0')
    assert yesShareToken.balanceOf(tester.a1) == 0
    assert noShareToken.balanceOf(tester.a1) == 0

    # 3. fill BID order for YES with shares of YES
    initialMakerETH = contractsFixture.chain.head_state.get_balance(tester.a1)
    initialFillerETH = contractsFixture.chain.head_state.get_balance(tester.a2)
    assert yesShareToken.approve(fillOrder.address, fix(12), sender=tester.k2)
    leftoverInOrder = fillOrder.publicFillOrder(
        orderID,
        fix(12),
        longTo32Bytes(42),
        False,
        "0x0000000000000000000000000000000000000000",
        sender=tester.k2)
    assert leftoverInOrder == 0
    assert cash.balanceOf(tester.a1) == 0
    assert cash.balanceOf(tester.a2) == fix('12', '60')
    assert contractsFixture.chain.head_state.get_balance(
        tester.a1) == initialMakerETH
    assert contractsFixture.chain.head_state.get_balance(
        tester.a2) == initialFillerETH
    assert yesShareToken.balanceOf(tester.a1) == fix(12)
    assert yesShareToken.balanceOf(tester.a2) == 0
    assert noShareToken.balanceOf(tester.a1) == 0
    assert noShareToken.balanceOf(tester.a2) == fix(12)
Ejemplo n.º 14
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', '6000'), tester.k1, "create order"):
        orderID = createOrder.publicCreateOrder(BID, fix(12), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), longTo32Bytes(42), False, sender = tester.k1)
    assert orderID
    assert cash.balanceOf(tester.a1) == fix('0')
    assert yesShareToken.balanceOf(tester.a1) == 0
    assert noShareToken.balanceOf(tester.a1) == 0

    # 2. fill BID order for YES with cash
    with BuyWithCash(cash, fix('12', '4000'), tester.k2, "create order"):
        leftoverInOrder = fillOrder.publicFillOrder(orderID, fix(12), longTo32Bytes(42), False, "0x0000000000000000000000000000000000000000", sender = tester.k2)
    assert leftoverInOrder == 0
    assert cash.balanceOf(tester.a1) == fix('0')
    assert cash.balanceOf(tester.a2) == fix('0')
    assert yesShareToken.balanceOf(tester.a1) == fix(12)
    assert yesShareToken.balanceOf(tester.a2) == 0
    assert noShareToken.balanceOf(tester.a1) == 0
    assert noShareToken.balanceOf(tester.a2) == fix(12)
Ejemplo n.º 15
0
def test_complete_set_auto_sale(contractsFixture, cash, market, universe):
    createOrder = contractsFixture.contracts['CreateOrder']
    fillOrder = contractsFixture.contracts['FillOrder']
    orders = contractsFixture.contracts['Orders']
    tradeGroupID = longTo32Bytes(42)
    shareToken = contractsFixture.contracts['ShareToken']
    fingerprint = longTo32Bytes(11)

    # create non matching orders
    with BuyWithCash(cash, fix('2', '60'), contractsFixture.accounts[1], "create order 1"):
        orderID1 = createOrder.publicCreateOrder(BID, fix(2), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = contractsFixture.accounts[1])
    with BuyWithCash(cash, fix('2', '30'), contractsFixture.accounts[1], "create order 2"):
        orderID2 = createOrder.publicCreateOrder(ASK, fix(2), 70, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = contractsFixture.accounts[1])

    # Have other users fill them
    with BuyWithCash(cash, fix('2', '40'), contractsFixture.accounts[2], "fill order 1"):
        assert fillOrder.publicFillOrder(orderID1, fix(2), tradeGroupID, fingerprint, sender = contractsFixture.accounts[2]) == 0
    with BuyWithCash(cash, fix('2', '70'), contractsFixture.accounts[3], "fill order 1"):
        assert fillOrder.publicFillOrder(orderID2, fix(2), tradeGroupID, fingerprint, sender = contractsFixture.accounts[3]) == 0

    # The first user would have ended up with 2 complete sets at the end of the second fill and we expect those to be automatically sold
    assert shareToken.balanceOfMarketOutcome(market.address, 0, contractsFixture.accounts[1]) == 0
    assert shareToken.balanceOfMarketOutcome(market.address, 1, contractsFixture.accounts[1]) == 0
    assert shareToken.balanceOfMarketOutcome(market.address, 2, contractsFixture.accounts[1]) == 0

    totalPaid = fix('2', '60') + fix('2', '30')
    totalPayout = fix(2) * market.getNumTicks()
    fees = totalPayout / universe.getOrCacheReportingFeeDivisor()
    fees += market.deriveMarketCreatorFeeAmount(totalPayout)
    totalPayout -= fees

    assert cash.balanceOf(contractsFixture.accounts[1]) == totalPayout
Ejemplo n.º 16
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']
    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)
Ejemplo n.º 17
0
def test_publicFillOrder_ask(contractsFixture, cash, market, universe):
    createOrder = contractsFixture.contracts['CreateOrder']
    fillOrder = contractsFixture.contracts['FillOrder']
    orders = contractsFixture.contracts['Orders']
    tradeGroupID = longTo32Bytes(42)
    fingerprint = longTo32Bytes(11)

    creatorCost = fix('2', '40')
    fillerCost = fix('2', '60')

    # create order
    with BuyWithCash(cash, creatorCost, contractsFixture.accounts[1], "creating order"):
        orderID = createOrder.publicCreateOrder(ASK, fix(2), 60, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = contractsFixture.accounts[1])

    # fill best order
    with BuyWithCash(cash, fillerCost, contractsFixture.accounts[2], "filling order"):
        fillOrderID = fillOrder.publicFillOrder(orderID, fix(2), tradeGroupID, fingerprint, 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 fillOrderID == 0
Ejemplo n.º 18
0
def test_trade_with_self_take_order_make_order(contractsFixture, cash, market):
    createOrder = contractsFixture.contracts['CreateOrder']
    trade = contractsFixture.contracts['Trade']
    orders = contractsFixture.contracts['Orders']
    tradeGroupID = longTo32Bytes(42)

    # create order
    createCost = fix('0.003', '60')
    with BuyWithCash(cash, createCost, contractsFixture.accounts[1], "create order"):
        orderID = createOrder.publicCreateOrder(ASK, fix('0.003'), 40, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, nullAddress, sender = contractsFixture.accounts[1])

    # fill best order
    takeCost = fix('1', '50')
    with BuyWithCash(cash, takeCost, contractsFixture.accounts[1], "publicTrade"):
        fillOrderID = trade.publicTrade(BID, market.address, YES, fix(1), 50, "0", "0", tradeGroupID, 6, longTo32Bytes(11), nullAddress, sender = contractsFixture.accounts[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)

    orderAmount = fix(1) - fix('0.003')
    assert orders.getAmount(fillOrderID) == orderAmount
    assert orders.getPrice(fillOrderID) == 50
    assert orders.getOrderCreator(fillOrderID) == contractsFixture.accounts[1]
    assert orders.getOrderMoneyEscrowed(fillOrderID) == fix('0.997', 50)
    # Note that we never ended up with the original orders shares. The ETH escrowed for those was simply returned to us for this case.
    assert orders.getOrderSharesEscrowed(fillOrderID) == 0
    assert orders.getBetterOrderId(fillOrderID) == longTo32Bytes(0)
    assert orders.getWorseOrderId(fillOrderID) == longTo32Bytes(0)
Ejemplo n.º 19
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']
    completeSets = contractsFixture.contracts['CompleteSets']
    yesShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(YES))
    noShareToken = contractsFixture.applySignature('ShareToken', market.getShareToken(NO))

    # buy complete sets for both users
    with BuyWithCash(cash, fix('1', '10000'), tester.k1, "buy complete set"):
        assert completeSets.publicBuyCompleteSets(market.address, fix(1), sender=tester.k1)
    assert yesShareToken.balanceOf(tester.a1) == fix(1)
    with BuyWithCash(cash, fix('1', '10000'), tester.k2, "buy complete set"):
        assert completeSets.publicBuyCompleteSets(market.address, fix(1), sender=tester.k2)
    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)
Ejemplo n.º 20
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', '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)
Ejemplo n.º 21
0
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
    with BuyWithCash(cash, creatorCost, tester.k1, "creating order"):
        orderID = createOrder.publicCreateOrder(BID, fix(2), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender=tester.k1)

    # fill best order
    with BuyWithCash(cash, fillerCost, tester.k2, "filling order"):
        fillOrderID = fillOrder.publicFillOrder(orderID, fix(2), tradeGroupID, sender=tester.k2)

    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
Ejemplo n.º 22
0
def test_publicFillOrder_kyc(contractsFixture, cash, market, universe, reputationToken):
    createOrder = contractsFixture.contracts['CreateOrder']
    fillOrder = contractsFixture.contracts['FillOrder']
    orders = contractsFixture.contracts['Orders']
    tradeGroupID = longTo32Bytes(42)
    fingerprint = longTo32Bytes(11)

    creatorCost = fix('2', '400')
    fillerCost = fix('2', '600')

    # Using the reputation token as "KYC"
    reputationToken.transfer(contractsFixture.accounts[1], 1)

    # create order
    with BuyWithCash(cash, creatorCost, contractsFixture.accounts[1], "creating order"):
        orderID = createOrder.publicCreateOrder(ASK, fix(2), 600, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, reputationToken.address, sender = contractsFixture.accounts[1])

    with raises(TransactionFailed):
        fillOrder.publicFillOrder(orderID, fix(2), tradeGroupID, fingerprint, sender = contractsFixture.accounts[2])

    reputationToken.transfer(contractsFixture.accounts[2], 1)

    # fill best order
    with BuyWithCash(cash, fillerCost, contractsFixture.accounts[2], "filling order"):
        fillOrderID = fillOrder.publicFillOrder(orderID, fix(2), tradeGroupID, fingerprint, 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 fillOrderID == 0
Ejemplo n.º 23
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)
Ejemplo n.º 24
0
def test_publicFillOrder_bid(contractsFixture, cash, market, universe):
    createOrder = contractsFixture.contracts['CreateOrder']
    fillOrder = contractsFixture.contracts['FillOrder']
    orders = contractsFixture.contracts['Orders']
    tradeGroupID = longTo32Bytes(42)
    fingerprint = longTo32Bytes(11)

    creatorCost = fix('2', '600')
    fillerCost = fix('2', '400')

    # create order
    with BuyWithCash(cash, creatorCost, contractsFixture.accounts[1], "complete set buy"):
        orderID = createOrder.publicCreateOrder(BID, fix(2), 600, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, sender = contractsFixture.accounts[1])

    # fill best order
    orderEventLog = {
        "universe": universe.address,
        "market": market.address,
        "eventType": 2,
        "addressData": [contractsFixture.accounts[1], contractsFixture.accounts[2]],
        "uint256Data": [600, 0, YES, 0, 0, 0, fix(2),  contractsFixture.contracts['Time'].getTimestamp(), 0, 0],
    }

    marketVolumeChangedLog = {
        "universe": universe.address,
        "market": market.address,
        "volume": creatorCost + fillerCost,
        "outcomeVolumes": [0, 0, creatorCost + fillerCost],
        "totalTrades": 1,
    }

    profitLossChangedLog = {
        "market": market.address,
        "account": contractsFixture.accounts[2],
        "outcome": YES,
        "netPosition": -fix(2),
        "avgPrice": fix(600),
        "realizedProfit": 0,
        "frozenFunds": fix(fillerCost),
    }

    if contractsFixture.paraAugur:
        orderEventLog['para'] = cash.address
        marketVolumeChangedLog['para'] = cash.address
        profitLossChangedLog['para'] = cash.address

    with BuyWithCash(cash, fillerCost, contractsFixture.accounts[2], "filling order"):
        with AssertLog(contractsFixture, "ProfitLossChanged", profitLossChangedLog):
            with AssertLog(contractsFixture, "OrderEvent", orderEventLog):
                with AssertLog(contractsFixture, "MarketVolumeChanged", marketVolumeChangedLog):
                    fillOrderID = fillOrder.publicFillOrder(orderID, fix(2), tradeGroupID, fingerprint, sender = contractsFixture.accounts[2])
                    assert fillOrderID == 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)
Ejemplo n.º 25
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', '4000'), tester.k1, "buy complete set"):
        orderID = createOrder.publicCreateOrder(ASK, fix(12), 6000, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), tradeGroupID, False, nullAddress, sender = tester.k1)

    # 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(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)
Ejemplo n.º 26
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)
Ejemplo n.º 27
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)
Ejemplo n.º 28
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']
    shareToken = contractsFixture.contracts['ShareToken']
    fingerprint = longTo32Bytes(11)

    # create order with cash
    price = 60
    numTicks = market.getNumTicks()
    with BuyWithCash(cash, fix(1, price), contractsFixture.accounts[1], "create order"):
        orderID = createOrder.publicCreateOrder(BID, fix(1), price, market.address, 0, longTo32Bytes(0), longTo32Bytes(0), longTo32Bytes(42), nullAddress, sender=contractsFixture.accounts[1])
    assert orderID

    # fill order with cash
    with BuyWithCash(cash, fix(1, numTicks - price), contractsFixture.accounts[2], "fill order"):
        assert fillOrder.publicFillOrder(orderID, fix(1), "43", fingerprint, sender=contractsFixture.accounts[2]) == 0

    # A complete set was purchased with the provided cash and the shares were provided to each user
    assert shareToken.balanceOfMarketOutcome(market.address, 0, contractsFixture.accounts[1]) == fix(1)
    assert shareToken.balanceOfMarketOutcome(market.address, 1, contractsFixture.accounts[1]) == 0
    assert shareToken.balanceOfMarketOutcome(market.address, 2, contractsFixture.accounts[1]) == 0

    assert shareToken.balanceOfMarketOutcome(market.address, 0, contractsFixture.accounts[2]) == 0
    assert shareToken.balanceOfMarketOutcome(market.address, 1, contractsFixture.accounts[2]) == fix(1)
    assert shareToken.balanceOfMarketOutcome(market.address, 2, contractsFixture.accounts[2]) == 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)
Ejemplo n.º 29
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)
Ejemplo n.º 30
0
def test_ask_withPartialShares(contractsFixture, universe, cash, market):
    orders = contractsFixture.contracts['Orders']
    createOrder = contractsFixture.contracts['CreateOrder']
    shareToken = contractsFixture.contracts['ShareToken']
    shareToken = contractsFixture.contracts["ShareToken"]

    # buy fix(2) complete sets
    with BuyWithCash(cash, fix(2, market.getNumTicks()), contractsFixture.accounts[1], "buy complete set"):
        assert shareToken.publicBuyCompleteSets(market.address, fix(2), sender = contractsFixture.accounts[1])
    assert cash.balanceOf(contractsFixture.accounts[1]) == fix('0')

    assert shareToken.balanceOfMarketOutcome(market.address, YES, contractsFixture.accounts[1]) == fix(2)
    assert shareToken.balanceOfMarketOutcome(market.address, NO, contractsFixture.accounts[1]) == fix(2)

    orderID = None

    orderCreatedEventLog = {
	    "eventType": 0,
	    "addressData": [contractsFixture.accounts[1] , nullAddress],
	    "uint256Data": [40, fix(3), YES, 0, 0, 0, 0,  contractsFixture.contracts['Time'].getTimestamp(), fix(2), fix(60)],
    }
    with BuyWithCash(cash, fix('60'), contractsFixture.accounts[1], "buy complete set"):
        with AssertLog(contractsFixture, "OrderEvent", orderCreatedEventLog):
            orderID = createOrder.publicCreateOrder(ASK, fix(3), 40, market.address, YES, longTo32Bytes(0), longTo32Bytes(0), longTo32Bytes(42), nullAddress, sender=contractsFixture.accounts[1])
    assert cash.balanceOf(contractsFixture.accounts[1]) == fix('0')
    assert shareToken.balanceOfMarketOutcome(market.address, YES, contractsFixture.accounts[1]) == 0
    assert shareToken.balanceOfMarketOutcome(market.address, NO, contractsFixture.accounts[1]) == 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) == 40
    assert orders.getOrderCreator(orderID) == contractsFixture.accounts[1]
    assert orders.getOrderMoneyEscrowed(orderID) == fix('60')
    assert orders.getOrderSharesEscrowed(orderID) == fix(2)