def wallet(): wallet = { 'addresses': {}, 'assets': {} } def add_total(address, asset, quantity): if quantity: if address not in wallet['addresses']: wallet['addresses'][address] = {} if asset not in wallet['assets']: wallet['assets'][asset] = 0 if asset not in wallet['addresses'][address]: wallet['addresses'][address][asset] = 0 wallet['addresses'][address][asset] += quantity wallet['assets'][asset] += quantity for bunch in get_shell_balances(): address, shell_balance = bunch add_total(address, 'SCH', shell_balance) balances = api('get_balances', {'filters': [('address', '==', address),]}) for balance in balances: asset = balance['asset'] balance = D(value_out(balance['quantity'], asset)) add_total(address, asset, balance) return wallet
def balances(address): result = { 'SCH': get_shell_balance(address) } balances = api('get_balances', {'filters': [('address', '==', address),]}) for balance in balances: asset = balance['asset'] balance = D(value_out(balance['quantity'], asset)) result[asset] = balance return result
def asset(asset_name): supply = api('get_supply', {'asset': asset_name}) asset_id = api('get_assets', {'filters': [('asset_name', '==', asset_name),]})[0]['asset_id'] asset_info = { 'asset': asset_name, 'supply': D(value_out(supply, asset_name)), 'asset_id': asset_id } if asset_name in ['SHP', 'SCH']: asset_info.update({ 'owner': None, 'divisible': True, 'locked': False, 'description': '', 'issuer': None }) else: issuances = api('get_issuances', { 'filters': [('asset', '==', asset_name),], 'status': 'valid', 'order_by': 'tx_index', 'order_dir': 'DESC', }) if not issuances: raise WalletError('Asset not found') locked = False for issuance in issuances: if issuance['locked']: locked = True issuance = issuances[0] asset_info.update({ 'owner': issuance['issuer'], 'divisible': bool(issuance['divisible']), 'locked': locked, 'description': issuance['description'], 'issuer': issuance['issuer'] }) asset_info['balance'] = 0 asset_info['addresses'] = {} for bunch in get_shell_balances(): address, shell_balance = bunch if asset_name == 'SCH': balance = shell_balance else: balances = api('get_balances', {'filters': [('address', '==', address), ('asset', '==', asset_name)]}) if balances: balance = balances[0] balance = D(value_out(balance['quantity'], asset_name)) else: balance = 0 if balance: asset_info['balance'] += balance asset_info['addresses'][address] = balance addresses = list(asset_info['addresses'].keys()) if asset_name != 'SCH': all_sends = api('get_sends', {'filters': [('source', 'IN', addresses), ('destination', 'IN', addresses)], 'filterop': 'OR', 'status': 'valid'}) sends = [] for send in all_sends: if send['asset'] == asset_name: if send['source'] in addresses and send['destination'] in addresses: tx_type = 'in-wallet' elif send['source'] in addresses: tx_type = 'send' elif send['destination'] in addresses: tx_type = 'receive' send['type'] = tx_type send['quantity'] = D(value_out(send['quantity'], asset_name)) sends.append(send) asset_info['sends'] = sends return asset_info
def prepare_args(args, action): # Convert. args.fee_per_kb = int(args.fee_per_kb * config.UNIT) args.regular_dust_size = int(args.regular_dust_size * config.UNIT) args.multisig_dust_size = int(args.multisig_dust_size * config.UNIT) args.op_return_value = int(args.op_return_value * config.UNIT) # common if args.fee: args.fee = util.value_in(args.fee, config.SCH) # send if action == 'send': args.quantity = util.value_in(args.quantity, args.asset) # order if action == 'order': fee_required, fee_fraction_provided = D(args.fee_fraction_required), D(args.fee_fraction_provided) give_quantity, get_quantity = D(args.give_quantity), D(args.get_quantity) # Fee argument is either fee_required or fee_provided, as necessary. if args.give_asset == config.SCH: args.fee_required = 0 fee_fraction_provided = util.value_in(fee_fraction_provided, 'fraction') args.fee_provided = round(D(fee_fraction_provided) * D(give_quantity) * D(config.UNIT)) print('Fee provided: {} {}'.format(util.value_out(args.fee_provided, config.SCH), config.SCH)) elif args.get_asset == config.SCH: args.fee_provided = 0 fee_fraction_required = util.value_in(args.fee_fraction_required, 'fraction') args.fee_required = round(D(fee_fraction_required) * D(get_quantity) * D(config.UNIT)) print('Fee required: {} {}'.format(util.value_out(args.fee_required, config.SCH), config.SCH)) else: args.fee_required = 0 args.fee_provided = 0 args.give_quantity = util.value_in(give_quantity, args.give_asset) args.get_quantity = util.value_in(get_quantity, args.get_asset) # issuance if action == 'issuance': args.quantity = util.value_in(args.quantity, None, divisible=args.divisible) # broadcast if action == 'broadcast': args.value = util.value_in(args.value, 'value') args.fee_fraction = util.value_in(args.fee_fraction, 'fraction') args.timestamp = int(time.time()) # bet if action == 'bet': args.deadline = calendar.timegm(dateutil.parser.parse(args.deadline).utctimetuple()) args.wager = util.value_in(args.wager, config.SHP) args.counterwager = util.value_in(args.counterwager, config.SHP) args.target_value = util.value_in(args.target_value, 'value') args.leverage = util.value_in(args.leverage, 'leverage') args.bet_type = BET_TYPE_ID[args.bet_type] # dividend if action == 'dividend': args.quantity_per_unit = util.value_in(args.quantity_per_unit, config.SHP) # burn if action == 'burn': args.quantity = util.value_in(args.quantity, config.SCH) # execute if action == 'execute': args.value = util.value_in(args.value, 'SHP') args.startgas = util.value_in(args.startgas, 'SHP') # destroy if action == 'destroy': args.quantity = util.value_in(args.quantity, args.asset, 'input') # RPS if action == 'rps': def generate_move_random_hash(move): move = int(move).to_bytes(2, byteorder='big') random_bin = os.urandom(16) move_random_hash_bin = dhash(random_bin + move) return binascii.hexlify(random_bin).decode('utf8'), binascii.hexlify(move_random_hash_bin).decode('utf8') args.wager = util.value_in(args.wager, 'SHP') random, move_random_hash = generate_move_random_hash(args.move) setattr(args, 'move_random_hash', move_random_hash) print('random: {}'.format(random)) print('move_random_hash: {}'.format(move_random_hash)) return args