Esempio n. 1
0
def _wait_funds_release(agent_config, wallet, type_):
    start_balance = try_get_balance(agent_config, wallet, type_)
    end_time = time.time() + FUNDS_RELEASE_TIMEOUT
    while time.time() < end_time:
        if start_balance != try_get_balance(agent_config, wallet, type_):
            break  # pragma: no cover
        time.sleep(1)
Esempio n. 2
0
    def test_try_get_balance_positive(self):
        """Test for try_get_balance method positive result."""
        agent_config = mock.Mock()
        agent_config.default_ledger_config = FETCHAI

        wallet_mock = mock.Mock()
        wallet_mock.addresses = {FETCHAI: "some-adress"}
        try_get_balance(agent_config, wallet_mock, FETCHAI)
Esempio n. 3
0
    def test_try_get_balance_positive(self):
        """Test for try_get_balance method positive result."""
        agent_config = mock.Mock()
        ledger_apis = {"type_": {"address": "some-adress"}}
        agent_config.ledger_apis_dict = ledger_apis

        wallet_mock = mock.Mock()
        wallet_mock.addresses = {"type_": "some-adress"}
        try_get_balance(agent_config, wallet_mock, "type_")
Esempio n. 4
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)
Esempio n. 5
0
def _try_get_wealth(click_context, type_):
    ctx = cast(Context, click_context.obj)
    verify_or_create_private_keys(ctx)
    private_key_paths = {
        config_pair[0]: config_pair[1]
        for config_pair in ctx.agent_config.private_key_paths.read_all()
    }
    wallet = Wallet(private_key_paths)
    return try_get_balance(ctx.agent_config, wallet, type_)
Esempio n. 6
0
def _try_get_wealth(click_context: click.core.Context, type_: str):
    ctx = cast(Context, click_context.obj)
    verify_or_create_private_keys_ctx(ctx=ctx)
    private_key_paths = {
        config_pair[0]: config_pair[1]
        for config_pair in ctx.agent_config.private_key_paths.read_all()
    }  # type: Dict[str, Optional[str]]
    wallet = Wallet(private_key_paths)
    return try_get_balance(ctx.agent_config, wallet, type_)
Esempio n. 7
0
 def get_balance(self) -> int:
     """Get balance for current agent."""
     with cd(self._get_cwd()):
         agent_config = AgentConfigManager.verify_or_create_private_keys(
             Path("."),
             substitude_env_vars=False,
             private_key_helper=private_key_verify_or_create,
         ).agent_config
         wallet = get_wallet_from_agent_config(agent_config)
         return int(try_get_balance(agent_config, wallet, self.LEDGER_ID))
Esempio n. 8
0
def _wait_funds_release(agent_config: AgentConfig, wallet: Wallet,
                        type_: str) -> None:
    """
    Wait for the funds to be released.

    :param agent_config: the agent config
    :param wallet: the wallet
    :param type_: the network type
    """
    start_balance = try_get_balance(agent_config, wallet, type_)
    end_time = time.time() + FUNDS_RELEASE_TIMEOUT
    has_hit_timeout = True
    while time.time() < end_time:
        current_balance = try_get_balance(agent_config, wallet, type_)
        if start_balance != current_balance:
            has_hit_timeout = False
            break  # pragma: no cover
        time.sleep(1)
    if has_hit_timeout:
        raise ValueError("Timeout hit. Syncing did not finish.")
Esempio n. 9
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_)
Esempio n. 10
0
 def get_balance(self) -> int:
     """Get balance for current agent."""
     with cd(self._get_cwd()):
         agent_config = verify_or_create_private_keys(Path("."), False)
         wallet = get_wallet_from_agent_config(agent_config)
         return int(try_get_balance(agent_config, wallet, self.LEDGER_ID))