def run(): # Create a private keys _create_fetchai_private_key(private_key_file=FETCHAI_PRIVATE_KEY_FILE_1) _create_fetchai_private_key(private_key_file=FETCHAI_PRIVATE_KEY_FILE_2) # Set up the wallets wallet_1 = Wallet({FETCHAI: FETCHAI_PRIVATE_KEY_FILE_1}) wallet_2 = Wallet({FETCHAI: FETCHAI_PRIVATE_KEY_FILE_2}) # Set up the LedgerApis ledger_apis = LedgerApis({FETCHAI: {"network": "testnet"}}, FETCHAI) # Generate some wealth _try_generate_testnet_wealth(FETCHAI, wallet_1.addresses[FETCHAI]) logger.info("Sending amount to {}".format(wallet_2.addresses.get(FETCHAI))) # Create the transaction and send it to the ledger. ledger_api = ledger_apis.apis[FETCHAI] tx_nonce = ledger_api.generate_tx_nonce( wallet_2.addresses.get(FETCHAI), wallet_1.addresses.get(FETCHAI) ) tx_digest = ledger_api.send_transaction( crypto=wallet_1.crypto_objects.get(FETCHAI), destination_address=wallet_2.addresses.get(FETCHAI), amount=1, tx_fee=1, tx_nonce=tx_nonce, ) logger.info("Transaction complete.") logger.info("The transaction digest is {}".format(tx_digest))
def test_act(): """Tests the act function of the AEA.""" with LocalNode() as node: agent_name = "MyAgent" private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem") wallet = Wallet({'default': private_key_pem_path}) ledger_apis = LedgerApis({}) public_key = wallet.public_keys['default'] connections = [OEFLocalConnection(public_key, node)] agent = AEA( agent_name, connections, wallet, ledger_apis, resources=Resources(str(Path(CUR_PATH, "data", "dummy_aea")))) t = Thread(target=agent.start) try: t.start() time.sleep(1.0) behaviour = agent.resources.behaviour_registry.fetch("dummy") assert behaviour[0].nb_act_called > 0, "Act() wasn't called" finally: agent.stop() t.join()
def setup_class(cls): """Set the test up.""" cls.node = LocalNode() cls.node.start() cls.agent_name = "MyAgent" cls.private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem") cls.wallet = Wallet({'default': cls.private_key_pem_path}) cls.ledger_apis = LedgerApis({}) cls.connection = OEFLocalConnection(cls.agent_name, cls.node) cls.connections = [cls.connection] cls.temp = tempfile.mkdtemp(prefix="test_aea_resources") cls.resources = Resources(cls.temp) cls.aea = AEA(cls.agent_name, cls.connections, cls.wallet, cls.ledger_apis, resources=cls.resources) cls.default_protocol_configuration = ProtocolConfig.from_json( yaml.safe_load(open(Path(AEA_DIR, "protocols", "default", "protocol.yaml")))) cls.default_protocol = Protocol("default", DefaultSerializer(), cls.default_protocol_configuration) cls.resources.protocol_registry.register(("default", None), cls.default_protocol) cls.error_skill = Skill.from_dir(Path(AEA_DIR, "skills", "error"), cls.aea.context) cls.dummy_skill = Skill.from_dir(Path(CUR_PATH, "data", "dummy_skill"), cls.aea.context) cls.resources.add_skill(cls.dummy_skill) cls.resources.add_skill(cls.error_skill) cls.expected_message = DefaultMessage(type=DefaultMessage.Type.BYTES, content=b"hello") cls.t = Thread(target=cls.aea.start) cls.t.start() time.sleep(0.5) cls.aea.outbox.put(Envelope(to=cls.agent_name, sender=cls.agent_name, protocol_id="default", message=DefaultSerializer().encode(cls.expected_message)))
def setup_class(cls): """Initialise the decision maker.""" cls._patch_logger() cls.multiplexer = Multiplexer([_make_dummy_connection()]) private_key_pem_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") eth_private_key_pem_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") cls.wallet = Wallet({ FETCHAI: private_key_pem_path, ETHEREUM: eth_private_key_pem_path }) cls.ledger_apis = LedgerApis({FETCHAI: DEFAULT_FETCHAI_CONFIG}, FETCHAI) cls.agent_name = "test" cls.identity = Identity(cls.agent_name, addresses=cls.wallet.addresses, default_address_key=FETCHAI) cls.ownership_state = OwnershipState() cls.preferences = Preferences() cls.decision_maker = DecisionMaker( identity=cls.identity, wallet=cls.wallet, ledger_apis=cls.ledger_apis, ) cls.multiplexer.connect() cls.tx_id = "transaction0" cls.tx_sender_addr = "agent_1" cls.tx_counterparty_addr = "pk" cls.info = {"some_info_key": "some_info_value"} cls.ledger_id = "fetchai" cls.decision_maker.start()
def setup_class(cls): """Initialise the decision maker.""" cls._patch_logger() private_key_pem_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") eth_private_key_pem_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") cls.wallet = Wallet({ FetchAICrypto.identifier: private_key_pem_path, EthereumCrypto.identifier: eth_private_key_pem_path, }) cls.agent_name = "test" cls.identity = Identity( cls.agent_name, addresses=cls.wallet.addresses, default_address_key=FetchAICrypto.identifier, ) cls.decision_maker_handler = DecisionMakerHandler( identity=cls.identity, wallet=cls.wallet) cls.decision_maker = DecisionMaker(cls.decision_maker_handler) cls.tx_sender_addr = "agent_1" cls.tx_counterparty_addr = "pk" cls.info = {"some_info_key": "some_info_value"} cls.ledger_id = "fetchai" cls.decision_maker.start()
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))
def test_error_handler_is_not_set(): """Test stop on no error handler presents.""" agent_name = "my_agent" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) wallet = Wallet({DEFAULT_LEDGER: private_key_path}) identity = Identity(agent_name, address=wallet.addresses[DEFAULT_LEDGER]) resources = Resources() context_namespace = {"key1": 1, "key2": 2} agent = AEA(identity, wallet, resources, **context_namespace) msg = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) msg.counterparty = agent.identity.address envelope = Envelope( to=agent.identity.address, sender=agent.identity.address, protocol_id=DefaultMessage.protocol_id, message=msg, ) with patch.object(agent, "stop") as mocked_stop: agent._handle(envelope) mocked_stop.assert_called()
def test_wallet_init_bad_paths(self): """Test Wallet init with bad paths to private keys""" private_key_paths = { FetchAICrypto.identifier: "this_path_does_not_exists" } with self.assertRaises(FileNotFoundError): Wallet(private_key_paths)
def test_act(): """Tests the act function of the AEA.""" with LocalNode() as node: agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") wallet = Wallet({FETCHAI: private_key_path}) identity = Identity(agent_name, address=wallet.addresses[FETCHAI]) ledger_apis = LedgerApis({}, FETCHAI) connections = [ OEFLocalConnection(identity.address, node, connection_id=LOCAL_CONNECTION_PUBLIC_ID) ] resources = Resources(str(Path(CUR_PATH, "data", "dummy_aea"))) agent = AEA(identity, connections, wallet, ledger_apis, resources, is_programmatic=False) t = Thread(target=agent.start) try: t.start() time.sleep(1.0) behaviour = agent.resources.behaviour_registry.fetch( (DUMMY_SKILL_PUBLIC_ID, "dummy")) assert behaviour.nb_act_called > 0, "Act() wasn't called" finally: agent.stop() t.join()
def test_initialise_aea(): """Tests the initialisation of the AEA.""" node = LocalNode() private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") wallet = Wallet({FETCHAI: private_key_path}) identity = Identity("my_name", address=wallet.addresses[FETCHAI]) connections1 = [ OEFLocalConnection(identity.address, node, connection_id=OEFLocalConnection.connection_id) ] ledger_apis = LedgerApis({}, FETCHAI) my_AEA = AEA( identity, connections1, wallet, ledger_apis, resources=Resources(str(Path(CUR_PATH, "aea"))), ) assert my_AEA.context == my_AEA._context, "Cannot access the Agent's Context" assert (not my_AEA.context.connection_status.is_connected ), "AEA should not be connected." my_AEA.setup() assert my_AEA.resources is not None, "Resources must not be None after setup" my_AEA.resources = Resources(str(Path(CUR_PATH, "aea"))) assert my_AEA.resources is not None, "Resources must not be None after set" assert (my_AEA.context.shared_state is not None), "Shared state must not be None after set" assert my_AEA.context.task_manager is not None assert my_AEA.context.identity is not None, "Identity must not be None after set." my_AEA.stop()
async def test_send_oef_message(network_node): """Test the send oef message.""" private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem") wallet = Wallet({'default': private_key_pem_path}) public_key = wallet.public_keys['default'] oef_connection = OEFConnection(public_key=public_key, oef_addr="127.0.0.1", oef_port=10000) oef_connection.loop = asyncio.get_event_loop() await oef_connection.connect() msg = OEFMessage(oef_type=OEFMessage.Type.OEF_ERROR, id=0, operation=OEFMessage.OEFErrorOperation.SEARCH_AGENTS) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope(to=DEFAULT_OEF, sender=public_key, protocol_id=OEFMessage.protocol_id, message=msg_bytes) with pytest.raises(ValueError): await oef_connection.send(envelope) data_model = DataModel("foobar", attributes=[]) query = Query(constraints=[], model=data_model) msg = OEFMessage(oef_type=OEFMessage.Type.SEARCH_AGENTS, id=0, query=query) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope(to="recipient", sender=public_key, protocol_id=OEFMessage.protocol_id, message=msg_bytes) await oef_connection.send(envelope) await oef_connection.disconnect()
def make_agent(agent_name="my_agent", runtime_mode="threaded") -> AEA: """Make AEA instance.""" wallet = Wallet({DEFAULT_LEDGER: None}) identity = Identity(agent_name, address=agent_name) resources = Resources() agent_context = MagicMock() agent_context.agent_name = agent_name agent_context.agent_address = agent_name resources.add_skill( Skill.from_dir( str( PACKAGES_DIR / FETCHAI / SKILLS / PublicId.from_str(DEFAULT_SKILL).name ), agent_context=agent_context, ) ) resources.add_protocol( Protocol.from_dir( str( PACKAGES_DIR / FETCHAI / PROTOCOLS / PublicId.from_str(DEFAULT_PROTOCOL).name ) ) ) return AEA(identity, wallet, resources, runtime_mode=runtime_mode)
def setup_class(cls): """Initialise the decision maker.""" cls._patch_logger() cls.multiplexer = Multiplexer( [DummyConnection(connection_id=DUMMY_CONNECTION_PUBLIC_ID)]) cls.outbox = OutBox(cls.multiplexer) private_key_pem_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") eth_private_key_pem_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") cls.wallet = Wallet({ FETCHAI: private_key_pem_path, ETHEREUM: eth_private_key_pem_path }) cls.ledger_apis = LedgerApis({FETCHAI: DEFAULT_FETCHAI_CONFIG}, FETCHAI) cls.agent_name = "test" cls.ownership_state = OwnershipState() cls.preferences = Preferences() cls.decision_maker = DecisionMaker( agent_name=cls.agent_name, max_reactions=MAX_REACTIONS, outbox=cls.outbox, wallet=cls.wallet, ledger_apis=cls.ledger_apis, ) cls.multiplexer.connect() cls.tx_id = "transaction0" cls.tx_sender_addr = "agent_1" cls.tx_counterparty_addr = "pk" cls.info = {"some_info_key": "some_info_value"} cls.ledger_id = "fetchai" cls.decision_maker.start()
def build(self, connection_ids: Optional[Collection[PublicId]] = None) -> AEA: """ Build the AEA. :param connection_ids: select only these connections to run the AEA. :return: the AEA object. """ wallet = Wallet(self.private_key_paths) identity = self._build_identity_from_wallet(wallet) self._load_and_add_protocols() self._load_and_add_contracts() connections = self._load_connections(identity.address, connection_ids) aea = AEA( identity, connections, wallet, LedgerApis(self.ledger_apis_config, self._default_ledger), self._resources, loop=None, timeout=0.0, is_debug=False, is_programmatic=True, max_reactions=20, ) self._load_and_add_skills(aea.context) return aea
def setup_class(cls): """Test the initialisation of the AEA.""" cls.node = LocalNode() private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") cls.wallet = Wallet({FETCHAI: private_key_path}) cls.ledger_apis = LedgerApis({}, FETCHAI) cls.agent_name = "Agent0" cls.connection = _make_dummy_connection() cls.connections = [cls.connection] cls.identity = Identity(cls.agent_name, address=cls.wallet.addresses[FETCHAI]) cls.address = cls.identity.address cls.my_aea = AEA( cls.identity, cls.connections, cls.wallet, cls.ledger_apis, timeout=2.0, resources=Resources(str(Path(CUR_PATH, "data/dummy_aea"))), is_programmatic=False, ) cls.skill_context = SkillContext(cls.my_aea._context) logger_name = "aea.{}.skills.{}.{}".format( cls.my_aea._context.agent_name, "fetchai", "error" ) cls.skill_context._logger = logging.getLogger(logger_name) cls.my_error_handler = ErrorHandler( name="error", skill_context=cls.skill_context ) cls.t = Thread(target=cls.my_aea.start) cls.t.start() time.sleep(0.5)
def test_run_agent(): """Test that we can set up and then run the agent.""" with LocalNode() as node: agent_name = "dummyagent" private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem") wallet = Wallet({'default': private_key_pem_path}) agent = DummyAgent(agent_name, [OEFLocalConnection("mypbk", node)], wallet) assert agent.name == agent_name assert isinstance(agent.wallet, Wallet) assert agent.agent_state == AgentState.INITIATED, "Agent state must be 'initiated'" agent.multiplexer.connect() assert agent.agent_state == AgentState.CONNECTED, "Agent state must be 'connected'" assert isinstance(agent.inbox, InBox) assert isinstance(agent.outbox, OutBox) agent_thread = Thread(target=agent.start) agent_thread.start() time.sleep(1.0) try: assert agent.agent_state == AgentState.RUNNING, "Agent state must be 'running'" finally: agent.stop() agent.multiplexer.disconnect() agent_thread.join()
def setup(cls): """Initialise the decision maker.""" cls._patch_logger() cls.wallet = Wallet( { COSMOS: COSMOS_PRIVATE_KEY_PATH, ETHEREUM: ETHEREUM_PRIVATE_KEY_PATH, FETCHAI: FETCHAI_PRIVATE_KEY_PATH, } ) cls.agent_name = "test" cls.identity = Identity( cls.agent_name, addresses=cls.wallet.addresses, default_address_key=COSMOS, ) cls.decision_maker_handler = DecisionMakerHandler( identity=cls.identity, wallet=cls.wallet ) cls.decision_maker = DecisionMaker(cls.decision_maker_handler) cls.tx_sender_addr = "agent_1" cls.tx_counterparty_addr = "pk" cls.info = {"some_info_key": "some_info_value"} cls.ledger_id = FETCHAI cls.decision_maker.start()
def setup_class(cls): """Set the tests up.""" # create temp agent folder cls.oldcwd = os.getcwd() cls.agent_name = "agent_test" + str(random.randint(0, 1000)) # nosec cls.t = tempfile.mkdtemp() cls.agent_folder = os.path.join(cls.t, cls.agent_name) shutil.copytree(os.path.join(CUR_PATH, "data", "dummy_aea"), cls.agent_folder) os.chdir(cls.agent_folder) connection = _make_dummy_connection() private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) wallet = Wallet({DEFAULT_LEDGER: private_key_path}) identity = Identity(cls.agent_name, address=wallet.addresses[DEFAULT_LEDGER]) resources = Resources() resources.add_component( Skill.from_dir( Path(CUR_PATH, "data", "dummy_skill"), agent_context=MagicMock(), ) ) resources.add_connection(connection) cls.aea = AEA(identity, wallet, resources=resources,) cls.aea.setup()
def setup_class(cls): """Set the tests up.""" cls._patch_logger() cls.cwd = os.getcwd() cls.t = tempfile.mkdtemp() cls.skill_directory = Path(cls.t, "dummy_skill") shutil.copytree(Path(CUR_PATH, "data", "dummy_skill"), cls.skill_directory) os.chdir(cls.t) private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") cls.wallet = Wallet({FETCHAI: private_key_path}) ledger_apis = LedgerApis({}, FETCHAI) cls.connections = [ DummyConnection(connection_id=DUMMY_CONNECTION_PUBLIC_ID) ] cls.identity = Identity("name", address=cls.wallet.addresses[FETCHAI]) cls.my_aea = AEA( cls.identity, cls.connections, cls.wallet, ledger_apis, resources=Resources(str(Path(CUR_PATH, "data", "dummy_aea"))), is_programmatic=False, ) cls.agent_context = cls.my_aea.context
def setup_class(cls): """Test the initialisation of the AEA.""" eth_private_key_path = os.path.join(CUR_PATH, "data", "eth_private_key.txt") fet_private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") cls.wallet = Wallet({ ETHEREUM: eth_private_key_path, FETCHAI: fet_private_key_path }) cls.ledger_apis = LedgerApis({FETCHAI: { "network": "testnet" }}, FETCHAI) cls.connections = [ DummyConnection(connection_id=DUMMY_CONNECTION_PUBLIC_ID) ] cls.identity = Identity("name", addresses=cls.wallet.addresses, default_address_key=FETCHAI) cls.my_aea = AEA( cls.identity, cls.connections, cls.wallet, cls.ledger_apis, resources=Resources(str(Path(CUR_PATH, "data", "dummy_aea"))), is_programmatic=False, ) cls.skill_context = SkillContext(cls.my_aea.context)
def setup_class(cls): """Set the tests up.""" # create temp agent folder cls.oldcwd = os.getcwd() cls.agent_name = "agent_test" + str(random.randint(0, 1000)) # nosec cls.t = tempfile.mkdtemp() cls.agent_folder = os.path.join(cls.t, cls.agent_name) shutil.copytree(os.path.join(CUR_PATH, "data", "dummy_aea"), cls.agent_folder) os.chdir(cls.agent_folder) connections = [_make_dummy_connection()] private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") wallet = Wallet({FETCHAI: private_key_path}) ledger_apis = LedgerApis({}, FETCHAI) identity = Identity(cls.agent_name, address=wallet.addresses[FETCHAI]) resources = Resources(cls.agent_folder) resources.add_component( Skill.from_dir(Path(CUR_PATH, "data", "dummy_skill"))) cls.aea = AEA( identity, connections, wallet, ledger_apis, resources=resources, is_programmatic=False, ) cls.aea.setup()
def setup_class(cls): """Set the tests up.""" cls._patch_logger() # create temp agent folder cls.oldcwd = os.getcwd() cls.agent_name = "agent_test" + str(random.randint(0, 1000)) cls.t = tempfile.mkdtemp() cls.agent_folder = os.path.join(cls.t, cls.agent_name) shutil.copytree(os.path.join(CUR_PATH, "data", "dummy_aea"), cls.agent_folder) os.chdir(cls.agent_folder) # make fake skill cls.fake_skill_id = "fake" agent_config_path = Path(cls.agent_folder, DEFAULT_AEA_CONFIG_FILE) agent_config = yaml.safe_load(agent_config_path.read_text()) agent_config.get("skills").append(cls.fake_skill_id) yaml.safe_dump(agent_config, open(agent_config_path, "w")) Path(cls.agent_folder, "skills", cls.fake_skill_id).mkdir() connections = [DummyConnection()] private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem") wallet = Wallet({'default': private_key_pem_path}) ledger_apis = LedgerApis({}) cls.resources = Resources(os.path.join(cls.agent_folder)) cls.aea = AEA(cls.agent_name, connections, wallet, ledger_apis, resources=cls.resources) cls.resources.load(cls.aea.context) cls.expected_skills = {"dummy", "error"}
def setup(self): """Test the initialisation of the AEA.""" private_key_path = os.path.join(CUR_PATH, "data", DEFAULT_PRIVATE_KEY_FILE) self.wallet = Wallet({DEFAULT_LEDGER: private_key_path}) self.agent_name = "Agent0" self.connection = _make_dummy_connection() self.identity = Identity(self.agent_name, address=self.wallet.addresses[DEFAULT_LEDGER]) self.address = self.identity.address self.my_aea = AEA( self.identity, self.wallet, timeout=0.1, resources=Resources(), default_connection=self.connection.public_id, ) self.my_aea.resources.add_connection(self.connection) self.my_aea._inbox = InboxWithHistory(self.my_aea.multiplexer) self.skill_context = SkillContext(self.my_aea._context) logger_name = "aea.{}.skills.{}.{}".format( self.my_aea._context.agent_name, "fetchai", "error") self.skill_context._logger = logging.getLogger(logger_name) self.my_error_handler = ErrorHandler(name="error", skill_context=self.skill_context) self.t = Thread(target=self.my_aea.start) self.t.start() wait_for_condition( lambda: self.my_aea._main_loop and self.my_aea._main_loop. is_running, 10)
def _try_get_wealth(ctx, type_): 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_)
def setup_class(cls): """Set the test up.""" agent_name = "MyAgent" private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") wallet = Wallet({FETCHAI: private_key_path}) ledger_apis = LedgerApis({}, FETCHAI) resources = Resources() resources.add_component( Skill.from_dir(Path(CUR_PATH, "data", "dummy_skill"))) identity = Identity(agent_name, address=wallet.addresses[FETCHAI]) cls.input_file = tempfile.mkstemp()[1] cls.output_file = tempfile.mkstemp()[1] cls.agent = AEA( identity, [_make_local_connection(identity.address, LocalNode())], wallet, ledger_apis, resources, ) for skill in resources.get_all_skills(): skill.skill_context.set_agent_context(cls.agent.context) cls.t = Thread(target=cls.agent.start) cls.t.start() time.sleep(1.0)
def setup_class(cls): """Set the test up.""" cls.node = LocalNode() cls.node.start() cls.agent_name = "MyAgent" cls.private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") cls.wallet = Wallet({FETCHAI: cls.private_key_path}) cls.ledger_apis = LedgerApis({}, FETCHAI) cls.identity = Identity(cls.agent_name, address=cls.wallet.addresses[FETCHAI]) cls.connection = _make_local_connection(cls.agent_name, cls.node) cls.connections = [cls.connection] cls.temp = tempfile.mkdtemp(prefix="test_aea_resources") cls.resources = Resources(cls.temp) cls.default_protocol = Protocol.from_dir( str(Path(AEA_DIR, "protocols", "default"))) cls.resources.add_protocol(cls.default_protocol) cls.error_skill = Skill.from_dir(str(Path(AEA_DIR, "skills", "error"))) cls.dummy_skill = Skill.from_dir( str(Path(CUR_PATH, "data", "dummy_skill"))) cls.resources.add_skill(cls.dummy_skill) cls.resources.add_skill(cls.error_skill) cls.aea = AEA( cls.identity, cls.connections, cls.wallet, cls.ledger_apis, resources=cls.resources, ) cls.error_skill.skill_context.set_agent_context(cls.aea.context) cls.dummy_skill.skill_context.set_agent_context(cls.aea.context) default_protocol_id = DefaultMessage.protocol_id cls.expected_message = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) cls.expected_message.counterparty = cls.agent_name cls.t = Thread(target=cls.aea.start) cls.t.start() time.sleep(0.5) cls.aea.outbox.put( Envelope( to=cls.agent_name, sender=cls.agent_name, protocol_id=default_protocol_id, message=DefaultSerializer().encode(cls.expected_message), ))
def test_wallet_init_positive(self): """Test Wallet init positive result.""" private_key_paths = { EthereumCrypto.identifier: ETHEREUM_PRIVATE_KEY_PATH, FetchAICrypto.identifier: FETCHAI_PRIVATE_KEY_PATH, CosmosCrypto.identifier: COSMOS_PRIVATE_KEY_PATH, } Wallet(private_key_paths)
def get_wallet_from_agent_config(agent_config: AgentConfig) -> Wallet: """Get wallet from agent_cofig provided.""" private_key_paths: Dict[str, Optional[str]] = { config_pair[0]: config_pair[1] for config_pair in agent_config.private_key_paths.read_all() } wallet = Wallet(private_key_paths) return wallet
def setup_class(cls): """Test the initialisation of the AEA.""" private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem") cls.wallet = Wallet({'default': private_key_pem_path}) cls.ledger_apis = LedgerApis({"fetchai": ("alpha.fetch-ai.com", 80)}) cls.connections = [DummyConnection()] cls.my_aea = AEA("Agent0", cls.connections, cls.wallet, cls.ledger_apis, resources=Resources(str(Path(CUR_PATH, "data", "dummy_aea")))) cls.skill_context = SkillContext(cls.my_aea.context)
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))