Skip to content

Use hex values in send_transaction #15

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

Merged
merged 4 commits into from
Mar 25, 2025
Merged
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
14 changes: 7 additions & 7 deletions python/thirdweb-ai/src/thirdweb_ai/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def extract_digits(value: int | str) -> int:
digit_match = re.search(r"\d+", value_str)

if not digit_match:
raise ValueError(f"Chain ID '{value}' does not contain any digits")
raise ValueError(f"Input '{value}' does not contain any digits")

extracted_digits = digit_match.group()

Expand All @@ -18,14 +18,14 @@ def extract_digits(value: int | str) -> int:


def normalize_chain_id(
chain_id: int | str | list[int | str] | None,
in_value: int | str | list[int | str] | None,
) -> int | list[int] | None:
"""Normalize chain IDs to integers."""
"""Normalize str values integers."""

if chain_id is None:
if in_value is None:
return None

if isinstance(chain_id, list):
return [extract_digits(c) for c in chain_id]
if isinstance(in_value, list):
return [extract_digits(c) for c in in_value]

return extract_digits(chain_id)
return extract_digits(in_value)
19 changes: 14 additions & 5 deletions python/thirdweb-ai/src/thirdweb_ai/services/engine.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Annotated, Any

from thirdweb_ai.common.utils import normalize_chain_id
from thirdweb_ai.common.utils import extract_digits, normalize_chain_id
from thirdweb_ai.services.service import Service
from thirdweb_ai.tools.tool import tool

Expand Down Expand Up @@ -68,7 +68,9 @@ def get_all_backend_wallet(
] = 20,
) -> dict[str, Any]:
"""Get all backend wallets."""
return self._get("backend-wallet/get-all", params={"page": page, "limit": limit})
return self._get(
"backend-wallet/get-all", params={"page": page, "limit": limit}
)

@tool(
description="Check the current balance of a backend wallet on a specific blockchain. Returns the balance in wei (smallest unit) for both native currency (ETH, MATIC, etc.) and ERC20 tokens. Essential for verifying if a wallet has sufficient funds before sending transactions."
Expand All @@ -87,7 +89,9 @@ def get_wallet_balance(
"""Get wallet balance for native or ERC20 tokens."""
normalized_chain = normalize_chain_id(chain_id) or self.chain_id
backend_wallet_address = backend_wallet_address or self.backend_wallet_address
return self._get(f"backend-wallet/{normalized_chain}/{backend_wallet_address}/get-balance")
return self._get(
f"backend-wallet/{normalized_chain}/{backend_wallet_address}/get-balance"
)

@tool(
description="Send an on-chain transaction. This powerful function can transfer native currency (ETH, MATIC), ERC20 tokens, or execute any arbitrary contract interaction. The transaction is signed and broadcast to the blockchain automatically."
Expand Down Expand Up @@ -117,9 +121,12 @@ def send_transaction(
) -> dict[str, Any]:
"""Send a transaction from a backend wallet."""

normalized_value = extract_digits(value)
hex_value = hex(normalized_value)

payload = {
"toAddress": to_address,
"value": value,
"value": hex_value,
"data": data or "0x",
}

Expand Down Expand Up @@ -172,7 +179,9 @@ def read_contract(
"args": function_args or [],
}
normalized_chain = normalize_chain_id(chain_id) or self.chain_id
return self._get(f"contract/{normalized_chain}/{contract_address}/read", payload)
return self._get(
f"contract/{normalized_chain}/{contract_address}/read", payload
)

@tool(
description="Execute a state-changing function on a smart contract by sending a transaction. This allows you to modify on-chain data, such as transferring tokens, minting NFTs, or updating contract configuration. The transaction is automatically signed by your backend wallet and submitted to the blockchain."
Expand Down