Exemplo n.º 1
0
def _try_generate_wealth(click_context: click.core.Context, type_: str,
                         sync: bool) -> None:
    """
    Try generate wealth for the provided network identifier.

    :param click_context: the click context
    :param type_: the network type
    :param sync: whether to sync or not
    :return: None
    """
    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)
    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)
        if sync:
            _wait_funds_release(ctx.agent_config, wallet, type_)

    except (AssertionError, ValueError) as e:  # pragma: no cover
        raise click.ClickException(str(e))
Exemplo n.º 2
0
 def tests_generate_wealth_ethereum_fail_no_url(self, caplog):
     """Test generate wealth for ethereum."""
     address = "my_address"
     with caplog.at_level(logging.DEBUG,
                          logger="aea.crypto.ethereum._default_logger"):
         try_generate_testnet_wealth(identifier=EthereumCrypto.identifier,
                                     address=address)
         assert (
             "Url is none, no default url provided. Please provide a faucet url."
             in caplog.text)
Exemplo n.º 3
0
 def tests_generate_wealth_ethereum_fail_valid_url(self, caplog):
     """Test generate wealth for ethereum."""
     address = "my_address"
     result = ResponseMock(status_code=200)
     with patch.object(requests, "get", return_value=result):
         with caplog.at_level(logging.DEBUG,
                              logger="aea.crypto.ethereum._default_logger"):
             try_generate_testnet_wealth(
                 identifier=EthereumCrypto.identifier,
                 address=address,
                 url="correct_url",
             )
Exemplo n.º 4
0
    def tests_generate_wealth_ethereum(self, mock_logging):
        """Test generate wealth for ethereum."""
        address = "my_address"
        result = ResponseMock(status_code=500)
        with patch.object(requests, "get", return_value=result):
            try_generate_testnet_wealth(identifier=EthereumCrypto.identifier,
                                        address=address)
            assert mock_logging.error.called

        result.status_code = 200
        with patch.object(requests, "get", return_value=result):
            try_generate_testnet_wealth(identifier=EthereumCrypto.identifier,
                                        address=address)
def run():
    # Create a private keys
    create_private_key(FetchAICrypto.identifier,
                       private_key_file=FETCHAI_PRIVATE_KEY_FILE_1)
    create_private_key(FetchAICrypto.identifier,
                       private_key_file=FETCHAI_PRIVATE_KEY_FILE_2)

    # Set up the wallets
    wallet_1 = Wallet({FetchAICrypto.identifier: FETCHAI_PRIVATE_KEY_FILE_1})
    wallet_2 = Wallet({FetchAICrypto.identifier: FETCHAI_PRIVATE_KEY_FILE_2})

    # Set up the LedgerApis
    ledger_apis = LedgerApis(
        {FetchAICrypto.identifier: {
            "network": "testnet"
        }}, FetchAICrypto.identifier)

    # Generate some wealth
    try_generate_testnet_wealth(FetchAICrypto.identifier,
                                wallet_1.addresses[FetchAICrypto.identifier])

    logger.info("Sending amount to {}".format(
        wallet_2.addresses.get(FetchAICrypto.identifier)))

    # Create the transaction and send it to the ledger.
    tx_nonce = ledger_apis.generate_tx_nonce(
        FetchAICrypto.identifier,
        wallet_2.addresses.get(FetchAICrypto.identifier),
        wallet_1.addresses.get(FetchAICrypto.identifier),
    )
    transaction = ledger_apis.get_transfer_transaction(
        identifier=FetchAICrypto.identifier,
        sender_address=wallet_1.addresses.get(FetchAICrypto.identifier),
        destination_address=wallet_2.addresses.get(FetchAICrypto.identifier),
        amount=1,
        tx_fee=1,
        tx_nonce=tx_nonce,
    )
    signed_transaction = wallet_1.sign_transaction(FetchAICrypto.identifier,
                                                   transaction)
    transaction_digest = ledger_apis.send_signed_transaction(
        FetchAICrypto.identifier, signed_transaction)

    logger.info("Transaction complete.")
    logger.info("The transaction digest is {}".format(transaction_digest))
Exemplo n.º 6
0
def _try_generate_wealth(ctx, type_, sync):
    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)
    try:
        address = wallet.addresses[type_]
        testnet = TESTNETS[type_]
        click.echo(
            "Requesting funds for address {} on test network '{}'".format(
                address, testnet))
        try_generate_testnet_wealth(type_, address)
        if sync:
            _wait_funds_release(ctx.agent_config, wallet, type_)

    except (AssertionError, ValueError) as e:  # pragma: no cover
        raise click.ClickException(str(e))
Exemplo n.º 7
0
def run():
    # Create a private keys
    create_private_key(
        CosmosCrypto.identifier, private_key_file=COSMOS_PRIVATE_KEY_FILE_1
    )
    create_private_key(
        CosmosCrypto.identifier, private_key_file=COSMOS_PRIVATE_KEY_FILE_2
    )

    # Set up the wallets
    wallet_1 = Wallet({CosmosCrypto.identifier: COSMOS_PRIVATE_KEY_FILE_1})
    wallet_2 = Wallet({CosmosCrypto.identifier: COSMOS_PRIVATE_KEY_FILE_2})

    # Generate some wealth
    try_generate_testnet_wealth(
        CosmosCrypto.identifier, wallet_1.addresses[CosmosCrypto.identifier]
    )

    logger.info(
        "Sending amount to {}".format(wallet_2.addresses.get(CosmosCrypto.identifier))
    )

    # Create the transaction and send it to the ledger.
    tx_nonce = LedgerApis.generate_tx_nonce(
        CosmosCrypto.identifier,
        wallet_2.addresses.get(CosmosCrypto.identifier),
        wallet_1.addresses.get(CosmosCrypto.identifier),
    )
    transaction = LedgerApis.get_transfer_transaction(
        identifier=CosmosCrypto.identifier,
        sender_address=wallet_1.addresses.get(CosmosCrypto.identifier),
        destination_address=wallet_2.addresses.get(CosmosCrypto.identifier),
        amount=1,
        tx_fee=1,
        tx_nonce=tx_nonce,
    )
    signed_transaction = wallet_1.sign_transaction(CosmosCrypto.identifier, transaction)
    transaction_digest = LedgerApis.send_signed_transaction(
        CosmosCrypto.identifier, signed_transaction
    )

    logger.info("Transaction complete.")
    logger.info("The transaction digest is {}".format(transaction_digest))
Exemplo n.º 8
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))
Exemplo n.º 9
0
 def test_try_generate_testnet_wealth_error_resp_ethereum(self, *mocks):
     """Test try_generate_testnet_wealth error_resp."""
     try_generate_testnet_wealth(EthereumCrypto.identifier, "address")
Exemplo n.º 10
0
 def test_try_generate_testnet_wealth_error_resp_fetchai(self, *mocks):
     """Test try_generate_testnet_wealth error_resp."""
     try_generate_testnet_wealth(FetchAICrypto.identifier, "address")
Exemplo n.º 11
0
def run():
    # Create a private key
    create_private_key(FetchAICrypto.identifier,
                       private_key_file=FETCHAI_PRIVATE_KEY_FILE_1)

    # Instantiate the builder and build the AEA
    # By default, the default protocol, error skill and stub connection are added
    builder = AEABuilder()

    builder.set_name("my_aea")

    builder.add_private_key(FetchAICrypto.identifier,
                            FETCHAI_PRIVATE_KEY_FILE_1)

    builder.add_ledger_api_config(FetchAICrypto.identifier,
                                  {"network": "testnet"})

    # Create our AEA
    my_aea = builder.build()

    # Generate some wealth for the default address
    try_generate_testnet_wealth(FetchAICrypto.identifier,
                                my_aea.identity.address)

    # add a simple skill with handler
    skill_context = SkillContext(my_aea.context)
    skill_config = SkillConfig(name="simple_skill",
                               author="fetchai",
                               version="0.1.0")
    tx_handler = TransactionHandler(skill_context=skill_context,
                                    name="transaction_handler")
    simple_skill = Skill(skill_config,
                         skill_context,
                         handlers={tx_handler.name: tx_handler})
    my_aea.resources.add_skill(simple_skill)

    # create a second identity
    create_private_key(FetchAICrypto.identifier,
                       private_key_file=FETCHAI_PRIVATE_KEY_FILE_2)

    counterparty_wallet = Wallet(
        {FetchAICrypto.identifier: FETCHAI_PRIVATE_KEY_FILE_2})

    counterparty_identity = Identity(
        name="counterparty_aea",
        addresses=counterparty_wallet.addresses,
        default_address_key=FetchAICrypto.identifier,
    )

    # create tx message for decision maker to process
    fetchai_ledger_api = my_aea.context.ledger_apis.apis[
        FetchAICrypto.identifier]
    tx_nonce = fetchai_ledger_api.generate_tx_nonce(
        my_aea.identity.address, counterparty_identity.address)

    tx_msg = TransactionMessage(
        performative=TransactionMessage.Performative.PROPOSE_FOR_SETTLEMENT,
        skill_callback_ids=[skill_config.public_id],
        tx_id="transaction0",
        tx_sender_addr=my_aea.identity.address,
        tx_counterparty_addr=counterparty_identity.address,
        tx_amount_by_currency_id={"FET": -1},
        tx_sender_fee=1,
        tx_counterparty_fee=0,
        tx_quantities_by_good_id={},
        ledger_id=FetchAICrypto.identifier,
        info={"some_info_key": "some_info_value"},
        tx_nonce=tx_nonce,
    )
    my_aea.context.decision_maker_message_queue.put_nowait(tx_msg)

    # Set the AEA running in a different thread
    try:
        logger.info("STARTING AEA NOW!")
        t = Thread(target=my_aea.start)
        t.start()

        # Let it run long enough to interact with the weather station
        time.sleep(20)
    finally:
        # Shut down the AEA
        logger.info("STOPPING AEA NOW!")
        my_aea.stop()
        t.join()