Skip to content

Commit

Permalink
feat: added agent ledger and balance properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro-Morales committed Sep 29, 2023
1 parent 67d4634 commit 61220ce
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
10 changes: 4 additions & 6 deletions python/examples/13-agent-name-service/agent1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from cosmpy.aerial.wallet import LocalWallet

from uagents.network import get_faucet, get_name_service_contract
from uagents.network import get_faucet, get_name_service_contract, _testnet_ledger
from uagents.setup import fund_agent_if_low
from uagents import Agent, Context, Model

Expand All @@ -9,8 +9,6 @@

# NOTE: Run agent1.py before running agent2.py

# pylint: disable=protected-access


class Message(Model):
message: str
Expand All @@ -30,9 +28,9 @@ class Message(Model):
DOMAIN = "agent"

faucet = get_faucet()
AGENT_BALANCE = bob._ledger.query_bank_balance(my_wallet)
WALLET_BALANCE = _testnet_ledger.query_bank_balance(my_wallet)

if AGENT_BALANCE < REGISTRATION_FEE:
if WALLET_BALANCE < REGISTRATION_FEE:
print("Adding funds to wallet...")
faucet.get_wealth(my_wallet)
print("Adding funds to wallet...complete")
Expand All @@ -41,7 +39,7 @@ class Message(Model):
@bob.on_event("startup")
async def register_agent_name(ctx: Context):
await name_service_contract.register(
bob._ledger, my_wallet, ctx.address[-65:], ctx.name, DOMAIN
bob.ledger, my_wallet, ctx.address[-65:], ctx.name, DOMAIN
)


Expand Down
26 changes: 24 additions & 2 deletions python/src/uagents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from cosmpy.aerial.wallet import LocalWallet, PrivateKey
from cosmpy.crypto.address import Address
from cosmpy.aerial.client import LedgerClient

from uagents.asgi import ASGIServer
from uagents.context import (
Expand Down Expand Up @@ -316,6 +317,16 @@ def wallet(self) -> LocalWallet:
"""
return self._wallet

@property
def ledger(self) -> LedgerClient:
"""
Get the ledger of the agent.
Returns:
LedgerClient: The agent's ledger
"""
return self._ledger

@property
def storage(self) -> KeyValueStore:
"""
Expand Down Expand Up @@ -356,6 +367,17 @@ def mailbox_client(self) -> MailboxClient:
"""
return self._mailbox_client

@property
def balance(self) -> int:
"""
Get the balance of the agent.
Returns:
int: Bank balance.
"""

return self.ledger.query_bank_balance(Address(self.wallet.address()))

@mailbox.setter
def mailbox(self, config: Union[str, Dict[str, str]]):
"""
Expand Down Expand Up @@ -471,7 +493,7 @@ async def register(self):
or list(self.protocols.keys())
!= self._almanac_contract.get_protocols(self._identity.address)
):
agent_balance = self._ledger.query_bank_balance(
agent_balance = self.ledger.query_bank_balance(
Address(self.wallet.address())
)

Expand All @@ -484,7 +506,7 @@ async def register(self):
self._logger.info("Registering on almanac contract...")
signature = self.sign_registration()
await self._almanac_contract.register(
self._ledger,
self.ledger,
self.wallet,
self._identity.address,
list(self.protocols.keys()),
Expand Down
2 changes: 1 addition & 1 deletion python/src/uagents/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import sys

# from enum import Enum

from typing import Any, Dict, List, Optional, Union

from uvicorn.logging import DefaultFormatter
Expand Down

0 comments on commit 61220ce

Please sign in to comment.