Skip to content

Add Extra Functions to SDK for C2A #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions virtuals_acp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,14 @@ def agent_address(self) -> str:
@property
def signer_address(self) -> str:
return self.signer_account.address


def get_balance(self) -> float:
return self.contract_manager.get_balance(self.agent_address)

def browse_agents(self, keyword: str, cluster: Optional[str] = None, sortBy: Optional[ACPAgentSort] = None, rerank: Optional[bool] = False, top_k: Optional[int] = None) -> List[IACPAgent]:
url = f"{self.acp_api_url}/agents?search={keyword}"

if len(sortBy) > 0:
if sortBy and len(sortBy) > 0:
url += f"&sort={','.join([s.value for s in sortBy])}"

if rerank is True:
Expand Down Expand Up @@ -205,6 +207,7 @@ def browse_agents(self, keyword: str, cluster: Optional[str] = None, sortBy: Opt
wallet_address=Web3.to_checksum_address(agent_data["walletAddress"]),
offerings=offerings,
twitter_handle=agent_data.get("twitterHandle"),
virtual_agent_id = agent_data.get("virtualAgentId"),
metrics=agent_data.get("metrics")
))
return agents
Expand Down Expand Up @@ -550,6 +553,48 @@ def get_memo_by_id(self, onchain_job_id: int, memo_id: int) -> 'ACPMemo':
except Exception as e:
raise ACPApiError(f"Failed to get memo by ID: {e}")

def get_agent_by_id(self, agent_id: int) -> IACPAgent:
url = f"{self.acp_api_url}/agents?filters[id][$eq]={agent_id}"

try:
response = requests.get(url)
response.raise_for_status()
data = response.json()

agents_data = data.get("data", [])
if not agents_data:
raise ACPApiError(f"No agent found with id {agent_id}")

agent_data = agents_data[0]

offerings = [
ACPJobOffering(
acp_client=self,
provider_address=agent_data["walletAddress"],
type=off["name"],
price=off["price"],
requirementSchema=off.get("requirementSchema", None)
)
for off in agent_data.get("offerings", [])
]

return IACPAgent(
id=agent_data["id"],
name=agent_data.get("name"),
description=agent_data.get("description"),
wallet_address=Web3.to_checksum_address(agent_data["walletAddress"]),
offerings=offerings,
twitter_handle=agent_data.get("twitterHandle"),
virtual_agent_id=agent_data.get("virtualAgentId"),
metrics=agent_data.get("metrics")
)
except requests.exceptions.RequestException as e:
raise ACPApiError(f"Failed to get agent by id: {e}")
except Exception as e:
raise ACPError(f"An unexpected error occurred while getting agent by id: {e}")



def get_agent(self, wallet_address: str) -> Optional[IACPAgent]:
url = f"{self.acp_api_url}/agents?filters[walletAddress]={wallet_address}"

Expand Down
3 changes: 3 additions & 0 deletions virtuals_acp/contract_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def approve_allowance(self, agent_wallet_address: str, price_in_wei: int) -> str
raise
time.sleep(2 * (3 - retries))

def get_balance(self, agent_wallet_address: str) -> float:
balance = self.token_contract.functions.balanceOf(Web3.to_checksum_address(agent_wallet_address)).call()
return balance / 10 ** 18

def create_memo(self, agent_wallet_address: str, job_id: int, content: str, memo_type: MemoType, is_secured: bool, next_phase: ACPJobPhase) -> str:
retries = 3
Expand Down