def test_create_ultimate_oracle(self): forwarded_oracle = CentralizedOracleFactory() ultimate_oracle = UltimateOracleFactory() block = { 'number': ultimate_oracle.creation_block, 'timestamp': mktime(ultimate_oracle.creation_date_time.timetuple()) } oracle_event = { 'address': ultimate_oracle.factory[0:7] + 'another', 'params': [{ 'name': 'creator', 'value': ultimate_oracle.creator }, { 'name': 'ultimateOracle', 'value': ultimate_oracle.address[0:7] + 'another', }, { 'name': 'oracle', 'value': forwarded_oracle.address }, { 'name': 'collateralToken', 'value': ultimate_oracle.collateral_token }, { 'name': 'spreadMultiplier', 'value': ultimate_oracle.spread_multiplier }, { 'name': 'challengePeriod', 'value': ultimate_oracle.challenge_period }, { 'name': 'challengeAmount', 'value': ultimate_oracle.challenge_amount }, { 'name': 'frontRunnerPeriod', 'value': ultimate_oracle.front_runner_period }] } s = UltimateOracleSerializer(data=oracle_event, block=block) self.assertTrue(s.is_valid(), s.errors) instance = s.save() self.assertIsNotNone(instance) self.assertIsNotNone(instance.pk)
def test_ultimate_oracle_receiver(self): forwarded_oracle = CentralizedOracleFactory() ultimate_oracle = UltimateOracleFactory() block = { 'number': ultimate_oracle.creation_block, 'timestamp': mktime(ultimate_oracle.creation_date_time.timetuple()) } oracle_address = ultimate_oracle.address[0:7] + 'another' oracle_event = { 'address': ultimate_oracle.factory[0:7] + 'another', 'params': [{ 'name': 'creator', 'value': ultimate_oracle.creator }, { 'name': 'ultimateOracle', 'value': oracle_address, }, { 'name': 'oracle', 'value': forwarded_oracle.address }, { 'name': 'collateralToken', 'value': ultimate_oracle.collateral_token }, { 'name': 'spreadMultiplier', 'value': ultimate_oracle.spread_multiplier }, { 'name': 'challengePeriod', 'value': ultimate_oracle.challenge_period }, { 'name': 'challengeAmount', 'value': ultimate_oracle.challenge_amount }, { 'name': 'frontRunnerPeriod', 'value': ultimate_oracle.front_runner_period }] } UltimateOracleFactoryReceiver().save(oracle_event, block) created_oracle = UltimateOracle.objects.get(address=oracle_address) self.assertIsNotNone(created_oracle.pk)
def test_market_factory_rollback(self): oracle = CentralizedOracleFactory() event = CategoricalEventFactory(oracle=oracle) market = MarketFactory() block = { 'number': oracle.creation_block, 'timestamp': self.to_timestamp(oracle.creation_date_time) } market_creation_event = { 'name': 'StandardMarketCreation', 'address': market.factory, 'params': [{ 'name': 'creator', 'value': market.creator }, { 'name': 'centralizedOracle', 'value': oracle.address, }, { 'name': 'marketMaker', 'value': settings.LMSR_MARKET_MAKER }, { 'name': 'fee', 'value': market.fee }, { 'name': 'eventContract', 'value': event.address }, { 'name': 'market', 'value': market.address }] } market.delete() MarketFactoryReceiver().save(market_creation_event, block) market_without_rollback = Market.objects.get(event=event.address) self.assertIsNotNone(market_without_rollback.pk) # Rollback MarketFactoryReceiver().rollback(market_creation_event, block) with self.assertRaises(Market.DoesNotExist): Market.objects.get(event=event.address)
def test_create_centralized_oracle(self): oracle = CentralizedOracleFactory() block = { 'number': oracle.creation_block, 'timestamp': mktime(oracle.creation_date_time.timetuple()) } oracle_event = { 'address': oracle.factory, 'params': [{ 'name': 'creator', 'value': oracle.creator }, { 'name': 'centralizedOracle', 'value': oracle.address, }, { 'name': 'ipfsHash', 'value': oracle.event_description.ipfs_hash[1:-7] + '' }] } oracle.delete() s = CentralizedOracleSerializer(data=oracle_event, block=block) # ipfs_hash not saved to IPFS self.assertFalse(s.is_valid(), s.errors) # oracle.event_description event_description_json = { 'title': oracle.event_description.title, 'description': oracle.event_description.description, 'resolutionDate': oracle.event_description.resolution_date.isoformat(), 'outcomes': ['Yes', 'No'] } # save event_description to IPFS ipfs_hash = self.ipfs.post(event_description_json) oracle_event.get('params')[2]['value'] = ipfs_hash s = CentralizedOracleSerializer(data=oracle_event, block=block) self.assertTrue(s.is_valid(), s.errors) instance = s.save() self.assertIsNotNone(instance)
def test_create_scalar_event(self): event = ScalarEventFactory() event_description = ScalarEventDescriptionFactory() oracle = CentralizedOracleFactory(event_description=event_description) block = { 'number': event.creation_block, 'timestamp': mktime(event.creation_date_time.timetuple()) } scalar_event = { 'address': event.factory, 'params': [{ 'name': 'creator', 'value': event.creator }, { 'name': 'collateralToken', 'value': event.collateral_token }, { 'name': 'oracle', 'value': oracle.address }, { 'name': 'upperBound', 'value': 1 }, { 'name': 'lowerBound', 'value': 0 }] } event.delete() s = ScalarEventSerializer(data=scalar_event, block=block) self.assertFalse(s.is_valid(), s.errors) scalar_event.get('params').append({ 'name': 'scalarEvent', 'value': event.address }) s = ScalarEventSerializer(data=scalar_event, block=block) self.assertTrue(s.is_valid(), s.errors) instance = s.save() self.assertIsNotNone(instance)
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_event_description_different_outcomes(self): oracle = CentralizedOracleFactory() oracle.event_description.outcomes = ['Yes', 'No', 'Third'] oracle.event_description.save() # Categorical Event with different outcomes block = { 'number': oracle.creation_block, 'timestamp': mktime(oracle.creation_date_time.timetuple()) } categorical_event = { 'address': oracle.factory, 'params': [{ 'name': 'creator', 'value': oracle.creator }, { 'name': 'collateralToken', 'value': oracle.creator }, { 'name': 'oracle', 'value': oracle.address }, { 'name': 'outcomeCount', 'value': 2 }, { 'name': 'categoricalEvent', 'value': oracle.factory[1:-11] + 'CATEGORICAL', }] } s = CategoricalEventSerializer(data=categorical_event, block=block) self.assertFalse(s.is_valid()) categorical_event['params'][3]['value'] = 3 s2 = CategoricalEventSerializer(data=categorical_event, block=block) self.assertTrue(s2.is_valid(), s.errors) instance = s2.save() self.assertIsNotNone(instance)
def test_create_categorical_event(self): event = CategoricalEventFactory() oracle = CentralizedOracleFactory() block = { 'number': event.creation_block, 'timestamp': mktime(event.creation_date_time.timetuple()) } categorical_event = { 'address': event.factory, 'params': [{ 'name': 'creator', 'value': event.creator }, { 'name': 'collateralToken', 'value': event.collateral_token }, { 'name': 'oracle', 'value': oracle.address }, { 'name': 'outcomeCount', 'value': 2 }] } event.delete() s = CategoricalEventSerializer(data=categorical_event, block=block) self.assertFalse(s.is_valid(), s.errors) categorical_event.get('params').append({ 'name': 'categoricalEvent', 'value': event.address }) s = CategoricalEventSerializer(data=categorical_event, block=block) self.assertTrue(s.is_valid(), s.errors) instance = s.save() self.assertIsNotNone(instance)
def test_create_centralized_oracle_instance(self): oracle = CentralizedOracleFactory() # oracle.event_description event_description_json = { 'title': oracle.event_description.title, 'description': oracle.event_description.description, 'resolutionDate': oracle.event_description.resolution_date.isoformat(), 'outcomes': ['Yes', 'No'] } # save event_description to IPFS ipfs_hash = self.ipfs.post(event_description_json) block = { 'number': oracle.creation_block, 'timestamp': mktime(oracle.creation_date_time.timetuple()) } oracle_event = { 'address': oracle.factory[0:-7] + 'GIACOMO', 'params': [{ 'name': 'creator', 'value': oracle.address }, { 'name': 'centralizedOracle', 'value': oracle.address[1:-8] + 'INSTANCE', }, { 'name': 'ipfsHash', 'value': ipfs_hash }] } s = CentralizedOracleInstanceSerializer(data=oracle_event, block=block) self.assertTrue(s.is_valid(), s.errors) instance = s.save() self.assertIsNotNone(instance)
def test_shares_by_account(self): account1 = '{:040d}'.format(13) account2 = '{:040d}'.format(14) url = reverse('api:shares-by-account', kwargs={'account_address': account1}) empty_shares_response = self.client.get( url, content_type='application/json') self.assertEquals( len(json.loads(empty_shares_response.content).get('results')), 0) oracle = CentralizedOracleFactory() event = CategoricalEventFactory(oracle=oracle) market = MarketFactory(event=event, creator=account1) outcome_token = OutcomeTokenFactory(event=market.event) OutcomeTokenBalanceFactory(owner=market.creator, outcome_token=outcome_token) BuyOrderFactory(outcome_token=outcome_token, sender=account1, market=market) url = reverse('api:shares-by-account', kwargs={'account_address': account1}) shares_response = self.client.get(url, content_type='application/json') decoded_response = json.loads(shares_response.content) self.assertEquals(len(decoded_response.get('results')), 1) self.assertEquals( decoded_response.get('results')[0].get('eventDescription').get( 'title'), oracle.event_description.title) self.assertEquals( decoded_response.get('results')[0].get('marginalPrice'), 0.5) url = reverse('api:shares-by-account', kwargs={'account_address': account2}) no_shares_response = self.client.get(url, content_type='application/json') self.assertEquals( len(json.loads(no_shares_response.content).get('results')), 0)
def test_centralized_oracle(self): # test empty centralized-oracles response empty_centralized_response = self.client.get(reverse('api:centralized-oracles'), content_type='application/json') self.assertEquals(len(json.loads(empty_centralized_response.content).get('results')), 0) # create centralized oracles centralized_oracles = [CentralizedOracleFactory() for x in range(0, 10)] centralized_oraclesdb = CentralizedOracle.objects.all() self.assertEquals(len(centralized_oracles), centralized_oraclesdb.count()) centralized_response_data = self.client.get(reverse('api:centralized-oracles'), content_type='application/json') self.assertEquals(centralized_response_data.status_code, status.HTTP_200_OK) self.assertEquals(len(json.loads(centralized_response_data.content).get('results')), len(centralized_oracles)) centralized_search_response = self.client.get(reverse('api:centralized-oracles-by-address', kwargs={'oracle_address': centralized_oracles[0].address}), content_type='application/json') self.assertEquals(centralized_search_response.status_code, status.HTTP_200_OK) self.assertEquals(json.loads(centralized_search_response.content).get('contract').get('creator'), add_0x_prefix(centralized_oracles[0].creator)) # test empty response centralized_empty_search_response = self.client.get(reverse('api:centralized-oracles-by-address', kwargs={'oracle_address': "abcdef0"}), content_type='application/json') self.assertEquals(centralized_empty_search_response.status_code, status.HTTP_404_NOT_FOUND) centralized_empty_search_response = self.client.get(reverse('api:centralized-oracles-by-address', kwargs={'oracle_address': centralized_oracles[0].creator}), content_type='application/json') self.assertEquals(centralized_empty_search_response.status_code, status.HTTP_200_OK) self.assertEquals(json.loads(centralized_empty_search_response.content).get('contract').get('creator'), add_0x_prefix(centralized_oracles[0].address))
def test_centralized_oracle_receiver(self): oracle = CentralizedOracleFactory() # saving event_description to IPFS event_description_json = { 'title': 'Test title', 'description': 'test long description', 'resolutionDate': datetime.now().isoformat(), 'outcomes': ['YES', 'NO'] } ipfs_hash = self.ipfs_api.post(event_description_json) block = { 'number': oracle.creation_block, 'timestamp': self.to_timestamp(oracle.creation_date_time) } oracle_address = oracle.address[1:-7] + 'GIACOMO' oracle_event = { 'address': oracle.factory[1:-7] + 'GIACOMO', 'params': [{ 'name': 'creator', 'value': oracle.creator }, { 'name': 'centralizedOracle', 'value': oracle_address, }, { 'name': 'ipfsHash', 'value': ipfs_hash }] } CentralizedOracleFactoryReceiver().save(oracle_event, block) created_oracle = CentralizedOracle.objects.get(address=oracle_address) self.assertIsNotNone(created_oracle.pk)
def test_create_centralized_oracle_instance(self): oracle = CentralizedOracleFactory() oracle.delete() # oracle.event_description event_description_json = { 'title': oracle.event_description.title, 'description': oracle.event_description.description, 'resolutionDate': oracle.event_description.resolution_date.isoformat(), 'outcomes': oracle.event_description.outcomes } block = { 'number': oracle.creation_block, 'timestamp': mktime(oracle.creation_date_time.timetuple()) } oracle_event = { 'address': oracle.factory, 'params': [{ 'name': 'creator', 'value': oracle.creator }, { 'name': 'centralizedOracle', 'value': oracle.address, }, { 'name': 'ipfsHash', 'value': oracle.event_description.ipfs_hash }] } s = CentralizedOracleInstanceSerializer(data=oracle_event, block=block) self.assertTrue(s.is_valid(), s.errors) instance = s.save() self.assertIsNotNone(instance)
def test_oracle_outcome_assignment_rollback(self): # Create the oracle oracle_factory = CentralizedOracleFactory() block = { 'number': 1, 'timestamp': self.to_timestamp(datetime.now()) } outcome_assignment_event = { 'name': 'OutcomeAssignment', 'address': oracle_factory.address, 'params': [{ 'name': 'outcome', 'value': 1, }] } CentralizedOracleInstanceReceiver().save(outcome_assignment_event) centralized_oracle_with_outcome_assignment = CentralizedOracle.objects.get(address=oracle_factory.address) self.assertTrue(centralized_oracle_with_outcome_assignment.is_outcome_set) self.assertEqual(centralized_oracle_with_outcome_assignment.outcome, 1) CentralizedOracleInstanceReceiver().rollback(outcome_assignment_event, block) centralized_oracle_with_outcome_assignment_rollback = CentralizedOracle.objects.get(address=oracle_factory.address) self.assertFalse(centralized_oracle_with_outcome_assignment_rollback.is_outcome_set) self.assertIsNone(centralized_oracle_with_outcome_assignment_rollback.outcome)
def test_create_market(self): oracle = CentralizedOracleFactory() event_factory = CategoricalEventFactory(oracle=oracle) market_factory = MarketFactory() block = { 'number': oracle.creation_block, 'timestamp': mktime(oracle.creation_date_time.timetuple()) } market_dict = { 'address': oracle.factory[1:-7] + 'GIACOMO', 'params': [{ 'name': 'creator', 'value': oracle.creator }, { 'name': 'centralizedOracle', 'value': oracle.address[1:-7] + 'GIACOMO', }, { 'name': 'marketMaker', 'value': market_factory.market_maker }, { 'name': 'fee', 'value': market_factory.fee }, { 'name': 'market', 'value': market_factory.address[0:-7] + 'MARKET' }] } s = MarketSerializer(data=market_dict, block=block) self.assertFalse(s.is_valid(), s.errors) market_dict.get('params').append({ 'name': 'eventContract', 'value': event_factory.address[1:-9] + 'xGIACOMOx' }) market_dict.get('params').append({ 'name': 'fee', 'value': market_factory.fee }) s = MarketSerializer(data=market_dict, block=block) self.assertFalse(s.is_valid(), s.errors) market_dict.get('params')[-2]['value'] = event_factory.address s = MarketSerializer(data=market_dict, block=block) self.assertFalse(s.is_valid(), s.errors) marketMaker = [ x for x in market_dict.get('params') if x.get('name') == 'marketMaker' ][0] marketMaker.update({'value': settings.LMSR_MARKET_MAKER}) s = MarketSerializer(data=market_dict, block=block) self.assertTrue(s.is_valid(), s.errors) instance = s.save() self.assertIsNotNone(instance)