def test_market_funding_rollback(self): market_factory = MarketFactory() block = { 'number': 1, 'timestamp': self.to_timestamp(datetime.now()) } market_funding_event = { 'name': 'MarketFunding', 'address': market_factory.address, 'params': [ { 'name': 'funding', 'value': 100 } ] } MarketInstanceReceiver().save(market_funding_event) market_without_rollback = Market.objects.get(address=market_factory.address) self.assertEquals(market_without_rollback.stage, 1) self.assertEquals(market_without_rollback.funding, 100) # Rollback MarketInstanceReceiver().rollback(market_funding_event, block) market_with_rollback = Market.objects.get(address=market_factory.address) self.assertEquals(market_with_rollback.stage, 0) self.assertIsNone(market_with_rollback.funding)
def test_market_outcome_token_purchase_rollback(self): oracle_factory = CentralizedOracleFactory() event_factory = CategoricalEventFactory(oracle=oracle_factory) outcome_token = OutcomeTokenFactory(event=event_factory, index=0) market_without_rollback = MarketFactory(event=event_factory) buyer_address = '{:040d}'.format(100) self.assertIsNotNone(market_without_rollback.pk) block = { 'number': oracle_factory.creation_block, 'timestamp': self.to_timestamp(oracle_factory.creation_date_time) } outcome_token_purchase_event = { 'name': 'OutcomeTokenPurchase', 'address': market_without_rollback.address, 'params': [ { 'name': 'outcomeTokenCost', 'value': 100 }, { 'name': 'marketFees', 'value': 10 }, { 'name': 'buyer', 'value': buyer_address }, { 'name': 'outcomeTokenIndex', 'value': 0 }, { 'name': 'outcomeTokenCount', 'value': 10 }, ] } # Send outcome token purchase event MarketInstanceReceiver().save(outcome_token_purchase_event, block) orders_before_rollback = BuyOrder.objects.filter( creation_block=block.get('number'), sender=buyer_address, market=market_without_rollback) self.assertEquals(len(orders_before_rollback), 1) # Outcome token purchase rollback MarketInstanceReceiver().rollback(outcome_token_purchase_event, block) market_with_rollback = Market.objects.get(event=event_factory.address) orders_after_rollback = BuyOrder.objects.filter( creation_block=block.get('number'), sender=buyer_address, market=market_with_rollback) self.assertEquals(len(orders_after_rollback), 0)
def test_collected_fees(self): categorical_event = CategoricalEventFactory() outcome_token = OutcomeTokenFactory(event=categorical_event, index=0) market = MarketFactory(event=categorical_event) sender_address = '{:040d}'.format(100) fees = 10 outcome_token_purchase_event = { 'name': 'OutcomeTokenPurchase', 'address': market.address, 'params': [ { 'name': 'outcomeTokenCost', 'value': 100 }, { 'name': 'marketFees', 'value': fees }, { 'name': 'buyer', 'value': sender_address }, { 'name': 'outcomeTokenIndex', 'value': 0 }, { 'name': 'outcomeTokenCount', 'value': 10 }, ] } block = {'number': 1, 'timestamp': self.to_timestamp(datetime.now())} # Save event MarketInstanceReceiver().save(outcome_token_purchase_event, block) # Check that collected fees was incremented market_check = Market.objects.get(address=market.address) self.assertEquals(market_check.collected_fees, market.collected_fees + fees) block.update({'number': 2}) MarketInstanceReceiver().save(outcome_token_purchase_event, block) market_check = Market.objects.get(address=market.address) self.assertEquals(market_check.collected_fees, market.collected_fees + fees + fees)
def test_market_outcome_token_sale_rollback(self): categorical_event = CategoricalEventFactory() outcome_token = OutcomeTokenFactory(event=categorical_event, index=0) market = MarketFactory(event=categorical_event) seller_address = '{:040d}'.format(100) block = {'number': 1, 'timestamp': self.to_timestamp(datetime.now())} outcome_token_sell_event = { 'name': 'OutcomeTokenSale', 'address': market.address, 'params': [ { 'name': 'outcomeTokenProfit', 'value': 100 }, { 'name': 'marketFees', 'value': 10 }, { 'name': 'seller', 'value': seller_address }, { 'name': 'outcomeTokenIndex', 'value': 0 }, { 'name': 'outcomeTokenCount', 'value': 10 }, ] } MarketInstanceReceiver().save(outcome_token_sell_event, block) orders_before_rollback = SellOrder.objects.filter( creation_block=block.get('number'), sender=seller_address) self.assertEquals(len(orders_before_rollback), 1) # Outcome token sell rollback MarketInstanceReceiver().rollback(outcome_token_sell_event, block) orders_before_rollback = SellOrder.objects.filter( creation_block=block.get('number'), sender=seller_address) self.assertEquals(len(orders_before_rollback), 0)
def test_market_closing_receiver(self): market_factory = MarketFactory() closing_event = { 'name': 'MarketClosing', 'address': market_factory.address, 'params': [] } MarketInstanceReceiver().save(closing_event) market = Market.objects.get(address=market_factory.address) self.assertEquals(market.stage, 2)
def test_market_fee_withdrawal_rollback(self): market_factory = MarketFactory() block = {'number': 1, 'timestamp': self.to_timestamp(datetime.now())} market_withdraw_event = { 'name': 'FeeWithdrawal', 'address': market_factory.address, 'params': [{ 'name': 'fees', 'value': 10 }] } MarketInstanceReceiver().save(market_withdraw_event) market_without_rollback = Market.objects.get( address=market_factory.address) self.assertEquals(market_without_rollback.withdrawn_fees, market_factory.withdrawn_fees + 10) # Rollback MarketInstanceReceiver().rollback(market_withdraw_event, block) market_with_rollback = Market.objects.get( address=market_factory.address) self.assertEquals(market_with_rollback.withdrawn_fees, market_factory.withdrawn_fees)
def test_market_funding_receiver(self): market_factory = MarketFactory() funding_event = { 'name': 'MarketFunding', 'address': market_factory.address, 'params': [{ 'name': 'funding', 'value': 100 }] } MarketInstanceReceiver().save(funding_event) market = Market.objects.get(address=market_factory.address) self.assertEquals(market.stage, 1) self.assertEquals(market.funding, 100)
def test_market_fee_withdrawal_receiver(self): market_factory = MarketFactory() withdraw_event = { 'name': 'FeeWithdrawal', 'address': market_factory.address, 'params': [{ 'name': 'fees', 'value': 10 }] } MarketInstanceReceiver().save(withdraw_event) market = Market.objects.get(address=market_factory.address) # self.assertEquals(market.stage, 3) self.assertEquals(market.withdrawn_fees, market_factory.withdrawn_fees + 10)
def test_outcome_token_purchase_marginal_price(self): categorical_event = CategoricalEventFactory() OutcomeTokenFactory(event=categorical_event, index=0) OutcomeTokenFactory(event=categorical_event, index=1) market = MarketFactory(event=categorical_event, funding=1e18, net_outcome_tokens_sold=[0, 0]) sender_address = '{:040d}'.format(100) outcome_token_purchase_event = { 'name': 'OutcomeTokenPurchase', 'address': market.address, 'params': [ { 'name': 'outcomeTokenCost', 'value': 1000000000000000000 }, { 'name': 'marketFees', 'value': 0 }, { 'name': 'buyer', 'value': sender_address }, { 'name': 'outcomeTokenIndex', 'value': 0 }, { 'name': 'outcomeTokenCount', 'value': 1584900000000000000 }, ] } block = {'number': 1, 'timestamp': self.to_timestamp(datetime.now())} self.assertEquals(BuyOrder.objects.all().count(), 0) MarketInstanceReceiver().save(outcome_token_purchase_event, block) buy_orders = BuyOrder.objects.all() self.assertEquals(buy_orders.count(), 1) self.assertListEqual( buy_orders[0].marginal_prices, [Decimal(0.7500), Decimal(0.2500)]) # outcomeTokenCost+fee
def test_outcome_token_sell(self): categorical_event = CategoricalEventFactory() outcome_token = OutcomeTokenFactory(event=categorical_event, index=0) market = MarketFactory(event=categorical_event) sender_address = '{:040d}'.format(100) outcome_token_sell_event = { 'name': 'OutcomeTokenSale', 'address': market.address, 'params': [ { 'name': 'outcomeTokenProfit', 'value': 100 }, { 'name': 'marketFees', 'value': 10 }, { 'name': 'seller', 'value': sender_address }, { 'name': 'outcomeTokenIndex', 'value': 0 }, { 'name': 'outcomeTokenCount', 'value': 10 }, ] } block = {'number': 1, 'timestamp': self.to_timestamp(datetime.now())} self.assertEquals(SellOrder.objects.all().count(), 0) MarketInstanceReceiver().save(outcome_token_sell_event, block) sell_orders = SellOrder.objects.all() self.assertEquals(sell_orders.count(), 1) self.assertEquals(sell_orders[0].profit, 90) # outcomeTokenProfit-fee
def test_outcome_token_purchase(self): categorical_event = CategoricalEventFactory() outcome_token = OutcomeTokenFactory(event=categorical_event, index=0) market = MarketFactory(event=categorical_event) sender_address = '{:040d}'.format(100) outcome_token_purchase_event = { 'name': 'OutcomeTokenPurchase', 'address': market.address, 'params': [ { 'name': 'outcomeTokenCost', 'value': 100 }, { 'name': 'marketFees', 'value': 10 }, { 'name': 'buyer', 'value': sender_address }, { 'name': 'outcomeTokenIndex', 'value': 0 }, { 'name': 'outcomeTokenCount', 'value': 10 }, ] } block = {'number': 1, 'timestamp': self.to_timestamp(datetime.now())} self.assertEquals(BuyOrder.objects.all().count(), 0) MarketInstanceReceiver().save(outcome_token_purchase_event, block) buy_orders = BuyOrder.objects.all() self.assertEquals(buy_orders.count(), 1) self.assertEquals(buy_orders[0].cost, 110) # outcomeTokenCost+fee