def do_org_create(args, config): url = config.get('DEFAULT', 'url') key_file = config.get('DEFAULT', 'key_file') client = BondClient(base_url=url, keyfile=key_file) client.create_org(args.name, args.object_id, args.industry, args.ticker, args.pricing_src, args.auth) if args.wait: client.wait_for_commit()
def on_validator_discovered(self, url): # We need a key file for the client then create a new client and add # it to our cadre of clients to use. For each client, we are going to # get the list of organizations it knows about. For any organization # that has a pricing source (i.e., can be a market maker) that is not # one of our made up ones (i.e., starts with Z), we are going to add # an authorization to it for the newly-created participant and # role of market maker. If the client has no such organizations, we # will create one of our own. Later on we will be able to issue orders # and quotes against this(these) organization(s). It is important # that orders (buy/sell) and quotes be issued from the same client # that creates the participant. with self._create_temporary_key_file() as key_file: try: client = BondClient(base_url=url, keyfile=key_file.name) # Participant and organization names are limited to 16 # characters and a full ISO formatted date/time is too long # so generate something that can be used as a fairly random # string of 14 characters random_name = \ hashlib.md5(datetime.now().isoformat()).hexdigest() participant_name = 'P_{0:.14s}'.format(random_name) participant_id = 'intel_simparticipant_{0}'.format(random_name) LOGGER.info( 'Create participant %s with ID %s for %s', participant_name, participant_id, client.base_url) client.create_participant( username=participant_name, object_id=participant_id) # Get the list of organizations that have a pricing source # that doesn't start with Z (i.e., is one we made up). state = client.get_all_store_objects() organizations = [] org_list = [x[1] for x in state.iteritems() if x[1]["object-type"] == 'organization'] for org in org_list: pricing_source = org.get('pricing-source') if pricing_source is not None \ and not pricing_source.startswith('Z'): organizations.append(org) # If there were none, then we need to create one so that we # at least have one market maker for this client to issue # quotes/orders against. if len(organizations) == 0: marketmaker_name = 'M_{0:.14s}'.format(random_name) marketmaker_id = \ 'intel_simmarketmaker_{0}'.format(random_name) # Just in case there are market makers left lying around # from a previous run we will keep trying until we hit a # unique pricing source. Once successful, add this # organization to the list while True: try: pricing_source = \ next(self._pricing_source_generator) client.create_org( name=marketmaker_name, object_id=marketmaker_id, pricing_src=pricing_source) break except InvalidTransactionError: pass organizations.append({ 'object-id': marketmaker_id, 'name': marketmaker_name, 'pricing-source': pricing_source }) LOGGER.info( 'Create marketmaker %s with pricing source %s and ' 'ID %s for %s', marketmaker_name, pricing_source, marketmaker_id, client.base_url) # Now, we need to add a participant/market maker role # authorization to each organization for organization in organizations: LOGGER.info( 'Add marketmaker role authorization to %s (%s) by ' 'participant %s', organization.get('name'), organization.get('pricing-source'), participant_id) client.add_authorization_to_org( object_id=organization.get('object-id'), role='marketmaker', participant_id=participant_id) # Add the client to our list so that we can choose from it # when randomly issuing new transactions. with self._lock: self._clients.append( Client( organizations=organizations, bond_client=client, id=participant_id)) except (InvalidTransactionError, MessageException) as err: LOGGER.error( 'Failed to create participant and authorize ' 'organization(s): %s', err)