示例#1
0
def _do_transfer(args, state):
    LOGGER.debug("_do_transfer ...")
    updated = {k: v for k, v in state.items()}

    try:
        from_addr = args['Name']
        to_addr = args['to_addr']
        num_bgt = float(args['num_bgt'])
        group_id = args['group_id']
    except KeyError:
        msg = "_do_transfer not all args"
        LOGGER.debug(msg)
        return updated
    except ValueError as err:
        LOGGER.debug("args err=%s",err)
        return updated

    LOGGER.debug("SMART_BGT>processor>handler>_do_transfer"
                 "\nfrom_addr=%s\nto_addr=%s\nnum_bgt=%s\ngroup_id=%s",
                 from_addr, to_addr, num_bgt, group_id)

    if from_addr not in state:
        LOGGER.debug("Sending tokens - address %s not registered", from_addr)
        raise InvalidTransaction('Verb is "transfer" but name "{}" not in state'.format(from_addr))

    from_wallet_str = state[from_addr]
    LOGGER.debug("SMART_BGT>processor>handler>_do_transfer"
                 "\nfrom_wallet_str=%s",
                 from_wallet_str)
    from_wallet = BGXwallet()
    from_wallet.fromJSON(from_wallet_str)
    from_token = from_wallet.get_token(group_id)

    to_token = Token()
    to_wallet = BGXwallet()
    if to_addr in state:
        LOGGER.debug("SMART_BGT>processor>handler>_do_transfer"
                     "\nto_addr is in state")
        to_wallet_str = state[to_addr]
        to_wallet.fromJSON(to_wallet_str)
        to_token = to_wallet.get_token(group_id)

    to_token.copy(from_token)
    res = from_token.send(to_token, num_bgt)
    LOGGER.debug("Sending tokens - result = %s", str(res))

    if not res:
        LOGGER.debug("Sending tokens - not enough money")
        raise InvalidTransaction('Unhandled action (not enough money)')
    else:
        from_wallet.append(from_token)
        to_wallet.append(to_token)
        updated[from_addr] = str(from_wallet.toJSON())
        updated[to_addr] = str(to_wallet.toJSON())

    LOGGER.debug("Transfer - ready! updated=%s", updated)
    return updated
示例#2
0
def _do_transfer(args, state):
    LOGGER.debug("_do_transfer ...")
    try:
        from_addr = args['Name']
        to_addr = args['to_addr']
        num_bgt = float(args['num_bgt'])
        group_id = args['group_id']
    except KeyError:
        msg = "_do_transfer not all args"
        LOGGER.debug(msg)
        raise InternalError(msg)

    updated = {k: v for k, v in state.items()}

    if from_addr not in state:
        # raise InvalidTransaction('Verb is "transfer" but name "{}" not in state'.format(from_addr))
        LOGGER.debug("Sending tokens - address %s not registered", from_addr)
        return updated

    from_wallet_str = state[from_addr]
    from_wallet = BGXwallet()
    from_wallet.fromJSON(from_wallet_str)
    from_token = from_wallet.get_token(group_id)

    to_token = Token()
    to_wallet = BGXwallet()
    if to_addr in state:
        to_wallet_str = state[to_addr]
        to_wallet.fromJSON(to_wallet_str)
        to_token = to_wallet.get_token(group_id)

    to_token.copy(from_token)
    res = from_token.send(to_token, num_bgt)
    LOGGER.debug("Sending tokens - result = %s", str(res))

    if not res:
        LOGGER.debug("Sending tokens - not enough money")
    else:
        from_wallet.append(from_token)
        to_wallet.append(to_token)
        updated[from_addr] = str(from_wallet.toJSON())
        updated[to_addr] = str(to_wallet.toJSON())

    LOGGER.debug("Transfer - ready! updated=%s", updated)
    return updated