def do_load(args, config): url = config.get('DEFAULT', 'url') key_file = config.get('DEFAULT', 'key_file') client = BondClient(base_url=url, keyfile=key_file) for filename in args.filename: try: with open(filename) as fd: data = yaml.load(fd) except IOError, ioe: raise ClientException("IOError: {}".format(str(ioe))) print "Loading file: {}".format(filename) with UpdateBatch(client): for i in xrange(0, len(data['Transactions'])): for j in xrange(0, len(data['Transactions'][i]["Updates"])): update = data['Transactions'][i]["Updates"][j] if "ObjectId" not in update: update["ObjectId"] = \ hashlib.sha256(dict2cbor(update)).hexdigest() print "Sending transaction {}, {}...".format(i, j) print "Update ", update client.send_bond_update(update) if args.wait: client.wait_for_commit()
def do_order_create(args, config): url = config.get('DEFAULT', 'url') key_file = config.get('DEFAULT', 'key_file') username = config.get("DEFAULT", 'username') client = BondClient(base_url=url, keyfile=key_file) args_dict = vars(args) store = client.get_all_store_objects() limit_price = None if args_dict.get('order-type') == 'Limit': if args.limit_price is not None: limit_price = args.limit_price if args.firm_id is None: firm_id = args.firm_id try: firm_id = try_get_then_lookup(store, 'participant', ['participant:username'], username)["firm-id"] print firm_id except KeyError: pass else: firm_id = args.firm_id client.create_order(args.action, args_dict.get('order-type'), firm_id, args.quantity, args.isin, args.cusip, limit_price, object_id=args.object_id) if args.wait: client.wait_for_commit()
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 do_user_register(args, config): url = config.get('DEFAULT', 'url') key_file = config.get('DEFAULT', 'key_file') username = config.get('DEFAULT', 'username') client = BondClient(base_url=url, keyfile=key_file) client.create_participant(username, args.firm_id) if args.wait: client.wait_for_commit()
def do_settlement_create(args, config): url = config.get('DEFAULT', 'url') key_file = config.get('DEFAULT', 'key_file') client = BondClient(base_url=url, keyfile=key_file) args_dict = vars(args) client.create_settlement(args_dict.get('order-id'), args.object_id) if args.wait: client.wait_for_commit()
def do_user_update(args, config): url = config.get('DEFAULT', 'url') key_file = config.get('DEFAULT', 'key_file') key_dir = config.get("DEFAULT", 'key_dir') username = config.get("DEFAULT", 'username') args_dict = vars(args) print args_dict client = BondClient(base_url=url, keyfile=key_file) client.update_participant(args_dict.get("object-id"), username=args.username, firm_id=args.firm_id) # need to change the username in bond.cfg and the .wif filename if args.username is not None: change_wif_and_addr_filename(key_dir, username, args.username) change_config('username', args.username) if args.wait: client.wait_for_commit()
def do_holding_create(args, config): url = config.get('DEFAULT', 'url') key_file = config.get('DEFAULT', 'key_file') client = BondClient(base_url=url, keyfile=key_file) state = client.get_all_store_objects() args_dict = vars(args) org = try_get_then_lookup( state, "organization", ["organization:ticker", "organization:pricing_source"], args.owner) if args_dict.get("asset-type") == "Currency": asset_id = args_dict.get("asset-id") else: asset_id = try_get_then_lookup(state, "bond", ["bond:cusip", "bond:isin"], args_dict.get("asset-id"))["object-id"] client.create_holding(org["object-id"], args_dict.get("asset-type"), asset_id, args.amount, args.object_id) if args.wait: client.wait_for_commit()
def do_quote_create(args, config): url = config.get('DEFAULT', 'url') key_file = config.get('DEFAULT', 'key_file') client = BondClient(base_url=url, keyfile=key_file) args_dict = vars(args) try: fp = float(args_dict.get('bid-price')) bid_price = float_to_bondprice(fp) except ValueError: bid_price = args_dict.get('bid-price') try: fp = float(args_dict.get('ask-price')) ask_price = float_to_bondprice(fp) except ValueError: ask_price = args_dict.get('ask-price') client.create_quote(args.firm, args_dict.get('ask-qty'), ask_price, args_dict.get('bid-qty'), bid_price, args.isin, args.cusip, args.object_id) if args.wait: client.wait_for_commit()
def on_will_start(self): # Note that the simulator will have already let us know about # the initial list of validators. So, we can simply use the # first client to submit our initial transactions. try: # Before the simulator starts, we will do any setup of # participants, etc., if the configuration tells us to do so setup_file = self.config.get('BondWorkload', 'setup_file') # Now read in the setup file, which is to be in YAML format, # and process the transactions. with open(setup_file) as fd: data = yaml.load(fd) # Unless we know about at least one validator, we cannot load # the setup date if len(self._clients) > 0: transactions = [] organizations = [] with self._create_temporary_key_file() as key_file: client = \ BondClient( base_url=self._clients[0].bond_client.base_url, keyfile=key_file.name) LOGGER.info( 'Submit initial transactions from %s to %s', setup_file, client.base_url) for transaction in data['Transactions']: try: transactions.append( client.send_bond_txn(transaction)) # Add any organizations with a pricing source so # later we can add market maker role authorizations organizations.extend( [u for u in transaction['Updates'] if u['UpdateType'] == 'CreateOrganization' and u.get('PricingSource') is not None and u.get('ObjectId') is not None]) except (InvalidTransactionError, UniqueConstraintError) as err: LOGGER.error('Transaction failed: %s', err) LOGGER.info( 'Wait for transactions to commit on %s', client.base_url) client.wait_for_commit() # It is not enough to have waited for the client to which # bond setup transactions were issued to commit. We really # want to make sure that all of the validators have the # transactions before we begin as once things get started we # will issue new transactions to random validators. LOGGER.info( 'Wait for transactions to propagate to all validators') self._wait_for_all_to_commit(transactions) # Because we may have loaded organizations that can create # quotes (i.e., have a pricing source), we want to be sure # to catch them here as well since they may not have # already been in the system. So, run through the # transactions and look for any updates that create an # organization with a pricing source and add market maker # role authorizations by the participants. for client in self._clients: # Refresh the client state to ensure that we get the # local store in sync so that local validation of # transactions doesn't fail. for organization in organizations: try: LOGGER.info( 'Add marketmaker role authorization to ' '%s (%s) by participant {%s}', organization.get('Name'), organization.get('PricingSource'), client.id) client.bond_client.add_authorization_to_org( object_id=organization.get('ObjectId'), role='marketmaker', participant_id=client.id) # Because we succeeded, we can add the # organization to the client so it can use it # when it starts issuing orders/quotes. We will # also keep the transaction ID so we can wi client.organizations.append({ 'object-id': organization.get('ObjectId'), 'name': organization.get('Name'), 'pricing-source': organization.get('PricingSource') }) except (InvalidTransactionError, UniqueConstraintError) as err: LOGGER.error('Transaction failed: %s', err) else: LOGGER.warning( 'Cannot pre-load bond transactions as we do not have any ' 'known validators') except ConfigParser.NoSectionError: LOGGER.warning( 'Configuration does not contain BondWorkload section. ' 'Will not pre-load bond transactions before starting.') except ConfigParser.NoOptionError: LOGGER.warning( 'Configuration BondWorkload section does not contain a ' 'setup_file option. Will not pre-load bond transactions ' 'before starting.') except IOError as err: LOGGER.warning('Failed to pre-load bond transactions: %s', err) # Ensure that all clients have fresh state of the new transactions # that we submitted and that all transactions submitted to the client # are committed for client in self._clients: LOGGER.info( 'Wait for all transactions on %s to commit', client.bond_client.base_url) client.bond_client.wait_for_commit() # Fetch the state from the first client and find the list of bonds so # we can use them to issue orders state = self._clients[0].bond_client.get_all_store_objects() bond_list = [b for b in state.iteritems() if b[1]["object-type"] == 'bond'] self._bond_ids = \ [BondIdentifier(isin=b.get('isin'), cusip=b.get('cusip')) for _, b in bond_list] if len(self._bond_ids) == 0: raise \ Exception('There are no bonds to issue orders/quotes against')