Skip to content

Commit 42c5539

Browse files
authored
Use hex values in send_transaction (#15)
* Ensure that chain_id is always an int * style: generalize function name * fix: convert value to hex in send_transaction * remove class method
1 parent 29192d5 commit 42c5539

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

python/thirdweb-ai/src/thirdweb_ai/common/utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def extract_digits(value: int | str) -> int:
66
digit_match = re.search(r"\d+", value_str)
77

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

1111
extracted_digits = digit_match.group()
1212

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

1919

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

25-
if chain_id is None:
25+
if in_value is None:
2626
return None
2727

28-
if isinstance(chain_id, list):
29-
return [extract_digits(c) for c in chain_id]
28+
if isinstance(in_value, list):
29+
return [extract_digits(c) for c in in_value]
3030

31-
return extract_digits(chain_id)
31+
return extract_digits(in_value)

python/thirdweb-ai/src/thirdweb_ai/services/engine.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Annotated, Any
22

3-
from thirdweb_ai.common.utils import normalize_chain_id
3+
from thirdweb_ai.common.utils import extract_digits, normalize_chain_id
44
from thirdweb_ai.services.service import Service
55
from thirdweb_ai.tools.tool import tool
66

@@ -68,7 +68,9 @@ def get_all_backend_wallet(
6868
] = 20,
6969
) -> dict[str, Any]:
7070
"""Get all backend wallets."""
71-
return self._get("backend-wallet/get-all", params={"page": page, "limit": limit})
71+
return self._get(
72+
"backend-wallet/get-all", params={"page": page, "limit": limit}
73+
)
7274

7375
@tool(
7476
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."
@@ -87,7 +89,9 @@ def get_wallet_balance(
8789
"""Get wallet balance for native or ERC20 tokens."""
8890
normalized_chain = normalize_chain_id(chain_id) or self.chain_id
8991
backend_wallet_address = backend_wallet_address or self.backend_wallet_address
90-
return self._get(f"backend-wallet/{normalized_chain}/{backend_wallet_address}/get-balance")
92+
return self._get(
93+
f"backend-wallet/{normalized_chain}/{backend_wallet_address}/get-balance"
94+
)
9195

9296
@tool(
9397
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."
@@ -117,9 +121,12 @@ def send_transaction(
117121
) -> dict[str, Any]:
118122
"""Send a transaction from a backend wallet."""
119123

124+
normalized_value = extract_digits(value)
125+
hex_value = hex(normalized_value)
126+
120127
payload = {
121128
"toAddress": to_address,
122-
"value": value,
129+
"value": hex_value,
123130
"data": data or "0x",
124131
}
125132

@@ -172,7 +179,9 @@ def read_contract(
172179
"args": function_args or [],
173180
}
174181
normalized_chain = normalize_chain_id(chain_id) or self.chain_id
175-
return self._get(f"contract/{normalized_chain}/{contract_address}/read", payload)
182+
return self._get(
183+
f"contract/{normalized_chain}/{contract_address}/read", payload
184+
)
176185

177186
@tool(
178187
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."

0 commit comments

Comments
 (0)