Example #1
0
def do_transfer(ctx: Context, identifier: str, address: Address, amount: int,
                tx_fee: int) -> Optional[str]:
    """
    Perform wealth transfer to another account.

    :param ctx: click context
    :param identifier: str, ledger id to perform transfer operation
    :param address: address of the recepient
    :param amount: int, amount of wealth to transfer
    :param tx_fee: int, fee for transaction

    :return: str, transaction digest or None if failed.
    """
    click.echo("Starting transfer ...")
    wallet = get_wallet_from_context(ctx)
    source_address = wallet.addresses[identifier]

    _override_ledger_configurations(ctx.agent_config)
    balance = int(try_get_balance(ctx.agent_config, wallet, identifier))
    total_payable = amount + tx_fee
    if total_payable > balance:
        raise click.ClickException(
            f"Balance is not enough! Available={balance}, required={total_payable}!"
        )

    tx_nonce = LedgerApis.generate_tx_nonce(identifier, source_address,
                                            address)
    transaction = LedgerApis.get_transfer_transaction(identifier,
                                                      source_address, address,
                                                      amount, tx_fee, tx_nonce)
    tx_signed = wallet.sign_transaction(identifier, transaction)
    return LedgerApis.send_signed_transaction(identifier, tx_signed)
Example #2
0
def _try_get_address(ctx: Context, type_: str):
    """
    Try to get address.

    :param click_context: click context object.
    :param type_: type.

    :return: address.
    """
    wallet = get_wallet_from_context(ctx)
    try:
        address = wallet.addresses[type_]
        return address
    except ValueError as e:  # pragma: no cover
        raise click.ClickException(str(e))
Example #3
0
def _try_generate_wealth(ctx: Context, type_: str, url: Optional[str],
                         sync: bool) -> None:
    """
    Try generate wealth for the provided network identifier.

    :param ctx: the click context
    :param type_: the network type
    :param url: the url
    :param sync: whether to sync or not
    :return: None
    """
    wallet = get_wallet_from_context(ctx)
    try:
        address = wallet.addresses[type_]
        faucet_api_cls = make_faucet_api_cls(type_)
        testnet = faucet_api_cls.network_name
        click.echo(
            "Requesting funds for address {} on test network '{}'".format(
                address, testnet))
        try_generate_testnet_wealth(type_, address, url, sync)

    except ValueError as e:  # pragma: no cover
        raise click.ClickException(str(e))
Example #4
0
 def test_get_wallet_from_ctx(self):
     """Test get_wallet_from_context."""
     ctx = mock.Mock()
     with cd(self._get_cwd()):
         assert isinstance(get_wallet_from_context(ctx), Wallet)
Example #5
0
def _try_get_wealth(ctx: Context, type_: str) -> int:
    wallet = get_wallet_from_context(ctx)
    _override_ledger_configurations(ctx.agent_config)
    return try_get_balance(ctx.agent_config, wallet, type_)