-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathengine_cloud.py
More file actions
231 lines (207 loc) · 8.33 KB
/
engine_cloud.py
File metadata and controls
231 lines (207 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# At the top of the file, add:
from typing import Annotated, Any, Literal, TypedDict
from thirdweb_ai.services.service import Service
from thirdweb_ai.tools.tool import tool
class FilterField(TypedDict):
field: Literal["id", "batchIndex", "from", "signerAddress", "smartAccountAddress", "chainId"]
class FilterValues(TypedDict):
values: list[int]
class FilterOperator(TypedDict):
operator: Literal["AND", "OR"]
FilterCondition = FilterField | FilterValues | FilterOperator
class EngineCloud(Service):
def __init__(
self,
secret_key: str,
vault_access_token: str,
):
super().__init__(base_url="https://engine.thirdweb.com/v1", secret_key=secret_key)
self.vault_access_token = vault_access_token
def _make_headers(self):
headers = super()._make_headers()
if self.vault_access_token:
headers["x-vault-access-token"] = self.vault_access_token
return headers
@tool(
description="Create a new engine server wallet. This is a helper route for creating a new EOA with your KMS provider, provided as a convenient alternative to creating an EOA directly with your KMS provider."
)
def create_server_wallet(
self,
label: Annotated[
str,
"A human-readable label to identify this wallet.",
],
) -> dict[str, Any]:
payload = {"label": label}
return self._post("accounts", payload)
@tool(
description="Call a write function on a contract. This endpoint allows you to execute state-changing functions on smart contracts, with support for various execution strategies."
)
def write_contract(
self,
from_address: Annotated[
str,
"The address of the account to send the transaction from. Can be the address of a smart account or an EOA.",
],
chain_id: Annotated[
int,
"The numeric blockchain network ID where the contract is deployed (e.g., '1' for Ethereum mainnet, '137' for Polygon).",
],
method: Annotated[str, "The name of the contract function to call on the contract."],
params: Annotated[list[Any], "The arguments to pass to the contract function."],
contract_address: Annotated[str, "The address of the smart contract to interact with."],
abi: Annotated[list[dict[str, Any]], "The ABI (Application Binary Interface) of the contract."],
value: Annotated[str, "The amount of native currency to send with the transaction (in wei)."] = "0",
) -> dict[str, Any]:
payload = {
"executionOptions": {
"from": from_address,
"chainId": chain_id,
},
"params": [
{
"method": method,
"params": params,
"contractAddress": contract_address,
"abi": abi,
"value": value,
}
],
}
return self._post("write/contract", payload)
@tool(
description="Send an encoded transaction or a batch of transactions. This endpoint allows you to execute low-level transactions with raw transaction data."
)
def send_transaction(
self,
from_address: Annotated[str, "The address of the account to send the transaction from."],
chain_id: Annotated[
int,
"The numeric blockchain network ID where the transaction will be sent (e.g., '1' for Ethereum mainnet, '137' for Polygon).",
],
to_address: Annotated[
str,
"The recipient address for the transaction.",
],
data: Annotated[
str,
"The encoded transaction data (hexadecimal).",
],
value: Annotated[
str,
"The amount of native currency to send with the transaction (in wei).",
] = "0",
) -> dict[str, Any]:
payload = {
"executionOptions": {
"from": from_address,
"chainId": chain_id,
},
"params": [
{
"to": to_address,
"data": data,
"value": value,
}
],
}
return self._post("write/transaction", payload)
@tool(
description="Call read-only contract functions or batch read using multicall. This is a gas-efficient way to query data from blockchain contracts without modifying state."
)
def read_contract(
self,
multicall_address: Annotated[
str | None,
"Optional multicall contract address for batching multiple calls. Defaults to the default multicall3 address for the chain",
],
chain_id: Annotated[
int,
"The numeric blockchain network ID where the contract is deployed (e.g., '1' for Ethereum mainnet, '137' for Polygon).",
],
from_address: Annotated[str, "EVM address in hex format"],
method: Annotated[str, "The name of the contract function to call."],
params: Annotated[list[Any], "The arguments to pass to the contract function."],
contract_address: Annotated[
str,
"The address of the smart contract to read from.",
],
abi: Annotated[list[dict[str, Any]], "The ABI (Application Binary Interface) for the contract."],
) -> dict[str, Any]:
payload = {
"readOptions": {
"multicallAddress": multicall_address,
"chainId": chain_id,
"from": from_address,
},
"params": [
{
"method": method,
"params": params,
"contractAddress": contract_address,
"abi": abi,
}
],
}
return self._post("read/contract", payload)
@tool(
description="Fetch the native cryptocurrency balance (e.g., ETH, MATIC) for a given address on a specific blockchain."
)
def get_native_balance(
self,
chain_id: Annotated[
int,
"The numeric blockchain network ID to query (e.g., '1' for Ethereum mainnet, '137' for Polygon).",
],
address: Annotated[str, "The wallet address to check the balance for."],
) -> dict[str, Any]:
payload = {
"chainId": chain_id,
"address": address,
}
return self._post("read/balance", payload)
@tool(
description="Search for transactions with flexible filtering options. Retrieve transaction history with customizable filters for addresses, chains, statuses, and more."
)
def search_transactions(
self,
filters: Annotated[FilterField, "List of filter conditions to apply"],
filters_operation: Annotated[
Literal["AND", "OR"],
"Logical operation to apply between filters. 'AND' means all conditions must match, 'OR' means any condition can match.",
] = "AND",
page: Annotated[
int | None,
"Page number for paginated results, starting from 1.",
] = 1,
limit: Annotated[
int | None,
"Maximum number of transactions to return per page (1-100).",
] = 20,
sort_by: Annotated[
Literal["createdAt", "confirmedAt"],
"Field to sort results by.",
] = "createdAt",
sort_direction: Annotated[
Literal["asc", "desc"],
"Sort direction ('asc' for ascending, 'desc' for descending).",
] = "desc",
) -> dict[str, Any]:
payload = {
"filters": filters,
"filtersOperation": filters_operation,
"page": page,
"limit": limit,
"sortBy": sort_by,
"sortDirection": sort_direction,
}
return self._post("transactions/search", payload)
@tool(
description="List all engine server wallets for the current project. Returns an array of EOA addresses with their corresponding predicted smart account addresses."
)
def get_accounts(self) -> dict[str, Any]:
"""Get all engine server wallets for the current project.
Returns:
dict containing list of account objects with address and smartAccountAddress
"""
return self._get("accounts")