def validate (db, source, destination, asset, quantity): try: util.get_asset_id(db, asset, util.CURRENT_BLOCK_INDEX) except AssetError: raise ValidateError('asset invalid') try: script.validate(source) except AddressError: raise ValidateError('source address invalid') if destination: raise ValidateError('destination exists') if asset == config.SCH: raise ValidateError('cannot destroy {}'.format(config.SCH)) if type(quantity) != int: raise ValidateError('quantity not integer') if quantity > config.MAX_INT: raise ValidateError('quantity too large') if quantity < 0: raise ValidateError('quantity negative') if util.get_balance(db, source, asset) < quantity: raise BalanceError('balance insufficient') if not config.TESTNET: raise ValidateError('disabled on mainnet')
def validate(db, source, destination, asset, quantity, block_index): try: util.get_asset_id(db, asset, block_index) except AssetError: raise ValidateError('asset invalid') try: script.validate(source) except AddressError: raise ValidateError('source address invalid') try: script.validate(destination) except AddressError: raise ValidateError('destination address invalid') if asset == config.SCH: raise ValidateError('cannot send {}'.format(config.SCH)) if type(quantity) != int: raise ValidateError('quantity not integer') if quantity > config.MAX_INT: raise ValidateError('quantity too large') if quantity <= 0: raise ValidateError('quantity nonāpositive') if util.get_balance(db, source, asset) < quantity: raise BalanceError('balance insufficient')
def compose(db, source, give_asset, give_quantity, get_asset, get_quantity, expiration, fee_required): cursor = db.cursor() # Check balance. if give_asset != config.SCH: balances = list( cursor.execute("""SELECT * FROM balances WHERE (address = ? AND asset = ?)""", (source, give_asset)) ) if not balances or balances[0]["quantity"] < give_quantity: raise exceptions.ComposeError("insufficient funds") problems = validate( db, source, give_asset, give_quantity, get_asset, get_quantity, expiration, fee_required, util.CURRENT_BLOCK_INDEX, ) if problems: raise exceptions.ComposeError(problems) give_id = util.get_asset_id(db, give_asset, util.CURRENT_BLOCK_INDEX) get_id = util.get_asset_id(db, get_asset, util.CURRENT_BLOCK_INDEX) data = struct.pack(config.TXTYPE_FORMAT, ID) data += struct.pack(FORMAT, give_id, give_quantity, get_id, get_quantity, expiration, fee_required) cursor.close() return (source, [], data)
def pack(db, asset, quantity, tag): data = struct.pack(config.TXTYPE_FORMAT, ID) data += struct.pack(FORMAT, util.get_asset_id(db, asset, util.CURRENT_BLOCK_INDEX), quantity, tag) return data