1+ """
2+ MORALIS API v2.2
3+ """
14import requests
25
3- from typing import Any
46from moralis import evm_api
57from langchain .tools import tool
68
79from src .util .configuration import Config
10+ from src .util .exceptions import NotSupportedError
811from src .agentflow .utils .shared_tools import handle_exceptions
912
1013_config = Config .get_config ()
1114_moralis_config = Config .get_service_config (_config , "MORALIS" )
1215
13- _MORALIS_URL = "https://deep-index.moralis.io/api/v2"
16+ _MORALIS_URL = "https://deep-index.moralis.io/api/v2.2 "
1417
15- @tool
16- @handle_exceptions
17- def get_wallet_active_chains (wallet_address : str , output_include : list [str ]) -> list [dict [str , Any ]]:
18- """
19- Get active chains for a wallet address across all chains
20-
21- Args:
22- wallet_address (str): Ethereum wallet address
23- output_include (list[str]):
24- A list of field names to include in in the output.
25-
26-
27- Returns:
28- List[dict[str, Any]]:
29- A list of dictionaries where each dictionary only contains the keys
30- listed in `output_include` (if they exist in the source data).
31- Possible fields include:
3218
33- - chain, chain_id, first_transaction, last_transaction
19+ def check_chain_supported ( chain : str ) -> bool :
3420 """
35- params = {
36- "address" : wallet_address
37- }
38-
39- result = evm_api .wallets .get_wallet_active_chains (
40- api_key = _moralis_config ["api_key" ],
41- params = params ,
42- )
43-
44- results = result ["active_chains" ]
45- final_results = []
46- for result in results :
47- final_results .append ({item : result [item ]
48- for item in result .keys () if item in output_include })
49- return final_results
21+ Moralis supported chains in v2.2
22+ """
23+ supported_chains = ["eth" , "0x1" , "polygon" , "0x89" , "bsc" , "0x38" , "avalanche" , "0xa86a" , "fantom" , "0xfa" , "palm" , "0x2a15c308d" , "cronos" , "0x19" , "arbitrum" , "0xa4b1" , "chiliz" , "0x15b38" ,
24+ "gnosis" , "0x64" , "base" , "0x2105" , "optimism" , "0xa" , "linea" , "0xe708" , "moonbeam" , "0x504" , "moonriver" , "0x505" , "flow" , "0x2eb" , "ronin" , "0x7e4" , "lisk" , "0x46f" , "pulse" , "0x171" ]
25+ if chain in supported_chains :
26+ return True
27+ raise NotSupportedError (
28+ f"chain { chain } is not supported. supported chains are : { supported_chains } " )
5029
5130
5231@tool
5332@handle_exceptions
54- def get_wallet_token_balances (wallet_address : str , output_include : list [str ], cursor : str = "" ) -> dict :
33+ def get_wallet_token_balances (wallet_address : str , chain : str , output_include : list [str ], cursor : str = "" ) -> dict :
5534 """
5635 Get token balances for a specific wallet address and their token prices in USD. (paginated)
5736 apply decimal conversion for balance
5837
5938 Args:
6039 wallet_address (str): Ethereum wallet address
40+ chain (str): The chain ID can be ["eth", "0x1", "polygon", "0x89", "bsc", "0x38", "avalanche", "0xa86a", "fantom", "0xfa", "palm", "0x2a15c308d", "cronos", "0x19", "arbitrum", "0xa4b1", "chiliz", "0x15b38","gnosis", "0x64", "base", "0x2105", "optimism", "0xa", "linea", "0xe708", "moonbeam", "0x504", "moonriver", "0x505", "flow", "0x2eb", "ronin", "0x7e4", "lisk", "0x46f", "pulse", "0x171"]
6141 output_include (list[str]): A list of field names to include in the output.
6242 cursor (str): The cursor returned in the previous response (used for getting the next page). end of page cursor is None
6343
@@ -72,8 +52,10 @@ def get_wallet_token_balances(wallet_address: str, output_include: list[str], cu
7252 usd_price, usd_price_24hr_percent_change, usd_price_24hr_usd_change, usd_value,
7353 usd_value_24hr_usd_change, native_token, portfolio_percentage
7454 """
55+ check_chain_supported (chain )
56+
7557 params = {
76- "chain" : "eth" ,
58+ "chain" : chain ,
7759 "address" : wallet_address
7860 }
7961
@@ -97,12 +79,14 @@ def get_wallet_token_balances(wallet_address: str, output_include: list[str], cu
9779
9880@tool
9981@handle_exceptions
100- def get_wallet_stats (wallet_address : str , output_include : list [str ]) -> dict :
82+ def get_wallet_stats (wallet_address : str , chain : str , output_include : list [str ]) -> dict :
10183 """
10284 Get the stats for a wallet address.
10385
10486 Args:
10587 wallet_address (str): Ethereum wallet address
88+ chain (str): The chain ID can be ["eth", "0x1", "polygon", "0x89", "bsc", "0x38", "avalanche", "0xa86a", "fantom", "0xfa", "palm", "0x2a15c308d", "cronos", "0x19", "arbitrum", "0xa4b1", "chiliz", "0x15b38","gnosis", "0x64", "base", "0x2105", "optimism", "0xa", "linea", "0xe708", "moonbeam", "0x504", "moonriver", "0x505", "flow", "0x2eb", "ronin", "0x7e4", "lisk", "0x46f", "pulse", "0x171"]
89+ output_include (list[str]): A list of field names to include in the output.
10690
10791 Returns:
10892 List[dict[str, Any]]:
@@ -112,8 +96,10 @@ def get_wallet_stats(wallet_address: str, output_include: list[str]) -> dict:
11296
11397 - nfts, collections, transactions, nft_transfers, token_transfers
11498 """
99+ check_chain_supported (chain )
100+
115101 params = {
116- "chain" : "eth" ,
102+ "chain" : chain ,
117103 "address" : wallet_address
118104 }
119105
@@ -128,13 +114,14 @@ def get_wallet_stats(wallet_address: str, output_include: list[str]) -> dict:
128114
129115@tool
130116@handle_exceptions
131- def get_wallet_history (wallet_address : str , output_include : list [str ], cursor : str = "" ) -> dict :
117+ def get_wallet_history (wallet_address : str , chain : str , output_include : list [str ], cursor : str = "" ) -> dict :
132118 """
133119 Retrieve the full transaction history of a specified wallet address, including sends, receives, token and NFT transfers
134120 and contract interactions. (paginated & in descending order)
135121
136122 Args:
137123 wallet_address (str): Ethereum wallet address
124+ chain (str): The chain ID can be ["eth", "0x1", "polygon", "0x89", "bsc", "0x38", "avalanche", "0xa86a", "fantom", "0xfa", "palm", "0x2a15c308d", "cronos", "0x19", "arbitrum", "0xa4b1", "chiliz", "0x15b38","gnosis", "0x64", "base", "0x2105", "optimism", "0xa", "linea", "0xe708", "moonbeam", "0x504", "moonriver", "0x505", "flow", "0x2eb", "ronin", "0x7e4", "lisk", "0x46f", "pulse", "0x171"]
138125 output_include (list[str]): A list of field names to include in the output.
139126 cursor (str): The cursor returned in the previous response (used for getting the next page). end of page cursor is None
140127
@@ -149,8 +136,10 @@ def get_wallet_history(wallet_address: str, output_include: list[str], cursor: s
149136 value, receipt_contract_address, block_timestamp, block_number, block_hash, internal_transactions,
150137 nft_transfers, erc20_transfer, native_transfers
151138 """
139+ check_chain_supported (chain )
140+
152141 params = {
153- "chain" : "eth" ,
142+ "chain" : chain ,
154143 "order" : "DESC" ,
155144 "address" : wallet_address
156145 }
@@ -174,12 +163,13 @@ def get_wallet_history(wallet_address: str, output_include: list[str], cursor: s
174163
175164@tool
176165@handle_exceptions
177- def get_transaction_detail (transaction_hash : str , output_include : list [str ]) -> dict :
166+ def get_transaction_detail (transaction_hash : str , chain : str , output_include : list [str ]) -> dict :
178167 """
179168 Get the contents of a transaction by the given transaction hash.
180169
181170 Args:
182171 transaction_hash (str): transaction hash to be decoded
172+ chain (str): The chain ID can be ["eth", "0x1", "polygon", "0x89", "bsc", "0x38", "avalanche", "0xa86a", "fantom", "0xfa", "palm", "0x2a15c308d", "cronos", "0x19", "arbitrum", "0xa4b1", "chiliz", "0x15b38","gnosis", "0x64", "base", "0x2105", "optimism", "0xa", "linea", "0xe708", "moonbeam", "0x504", "moonriver", "0x505", "flow", "0x2eb", "ronin", "0x7e4", "lisk", "0x46f", "pulse", "0x171"]
183173 output_include (list[str]): A list of field names to include in the output.
184174
185175 Returns:
@@ -195,8 +185,10 @@ def get_transaction_detail(transaction_hash: str, output_include: list[str]) ->
195185
196186
197187 """
188+ check_chain_supported (chain )
189+
198190 params = {
199- "chain" : "eth" ,
191+ "chain" : chain ,
200192 "transaction_hash" : transaction_hash
201193 }
202194
@@ -211,12 +203,13 @@ def get_transaction_detail(transaction_hash: str, output_include: list[str]) ->
211203
212204@tool
213205@handle_exceptions
214- def get_token_approvals (wallet_address : str , output_include : list [str ], cursor : str = "" ) -> dict :
206+ def get_token_approvals (wallet_address : str , chain : str , output_include : list [str ], cursor : str = "" ) -> dict :
215207 """
216208 Get ERC20 approvals for one or many wallet addresses and/or contract addresses, ordered by block number in descending order.
217209
218210 Args:
219211 wallet_address (str): Ethereum wallet address
212+ chain (str): The chain ID can be ["eth", "0x1", "polygon", "0x89", "bsc", "0x38", "avalanche", "0xa86a", "fantom", "0xfa", "palm", "0x2a15c308d", "cronos", "0x19", "arbitrum", "0xa4b1", "chiliz", "0x15b38","gnosis", "0x64", "base", "0x2105", "optimism", "0xa", "linea", "0xe708", "moonbeam", "0x504", "moonriver", "0x505", "flow", "0x2eb", "ronin", "0x7e4", "lisk", "0x46f", "pulse", "0x171"]
220213 output_include (list[str]): A list of field names to include in the output.
221214 cursor (str): The cursor returned in the previous response (used for getting the next page). end of page cursor is None
222215
@@ -228,10 +221,12 @@ def get_token_approvals(wallet_address: str, output_include: list[str], cursor:
228221
229222 - block_number, block_timestamp, transaction_hash, value, value_formatted, token, spender
230223 """
224+ check_chain_supported (chain )
225+
231226 base_url = _MORALIS_URL
232227 api_url = f"{ base_url } /wallets/{ wallet_address } /approvals"
233228
234- params = {'chain' : 'eth' }
229+ params = {'chain' : chain }
235230
236231 if cursor :
237232 params ["cursor" ] = cursor
0 commit comments