Beispiel #1
0
def transfer_value_with_the_fee(password, fee, decimal_point, to, amount, file_path, url):
    """ Transfer the value to the specific address with the fee.

    :param password: Password including alphabet character, number, and special character.
    If the user doesn't give password with -p, then CLI will show the prompt and user need to type the password.
    :param fee: Transaction fee.
    :param decimal_point: A user can change the decimal point to express all numbers including fee and amount.
    :param to: Address of wallet to receive the asset.
    :param amount: Amount of money. *The decimal point number is valid up to tenth power of 18. *
    :param file_path: File path for the keystore file of the wallet.
    :param url: Api url. type(str)
    :return:
    """
    try:
        url = f'{url}v2'
        validate_key_store_file(file_path)
        private_key_bytes = __key_from_key_store(file_path, bytes(password, 'utf-8'))
        user_address = get_address_by_privkey(private_key_bytes)

        validate_address(user_address)
        validate_address(to)

        method = 'icx_sendTransaction'

        amount_wei = icx_str_to_wei(amount)
        fixed_amount = int(floor_point(amount_wei, decimal_point))

        fee_wei = get_fee_wei(fee)
        fixed_fee = int(floor_point(fee_wei, decimal_point))

        check_amount_and_fee_is_valid(fixed_amount, fixed_fee)

        params = __make_params(user_address, to, fixed_amount, fixed_fee, method, private_key_bytes)
        payload = create_jsonrpc_request_content(0, method, params)

        # Request the balance repeatedly until we get the response from ICON network.
        request_gen = request_generator(url)
        balance = __get_balance_after_trasfer(user_address, url, request_gen)
        check_balance_enough(balance, amount, fee)
        next(request_gen)
        response = request_gen.send(payload)
        return response

    except FileNotFoundError:
        print("File does not exists.")
        raise FilePathIsWrong
    except IsADirectoryError:
        print(f"{file_path} is a directory.")
        raise FilePathIsWrong
    except ValueError:
        raise PasswordIsWrong
Beispiel #2
0
def __get_balance(address, url):
    """ Get balance of the address indicated by address.

    :param address: icx account address starting with 'hx'
    :param url:
    :return: icx
    """
    method = 'icx_getBalance'
    params = {'address': address}
    payload = create_jsonrpc_request_content(0, method, params)
    response = post(url, payload)
    content = response.json()
    hex_balance = content['result']['response']
    dec_loop_balance = int(hex_balance, 16)
    return dec_loop_balance