renzoide commited on
Commit
23af1f7
·
verified ·
1 Parent(s): ae7a494

added moralis sdk to get wallet activity

Browse files
Files changed (1) hide show
  1. app.py +98 -18
app.py CHANGED
@@ -1,22 +1,98 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
 
3
  import requests
4
  import pytz
 
5
  import yaml
 
6
  from tools.final_answer import FinalAnswerTool
7
-
8
  from Gradio_UI import GradioUI
9
 
 
 
 
 
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
 
 
 
 
 
 
 
 
 
 
 
 
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
 
 
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -37,33 +113,37 @@ def get_current_time_in_timezone(timezone: str) -> str:
37
  final_answer = FinalAnswerTool()
38
 
39
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
 
 
 
 
41
 
42
  model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
45
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
- custom_role_conversions=None,
47
  )
48
 
49
 
50
  # Import tool from Hub
51
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
52
 
53
- with open("prompts.yaml", 'r') as stream:
54
  prompt_templates = yaml.safe_load(stream)
55
-
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
62
  planning_interval=None,
63
  name=None,
64
  description=None,
65
- prompt_templates=prompt_templates
66
  )
67
 
68
 
69
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
+ from typing import Any
4
  import requests
5
  import pytz
6
+ import os
7
  import yaml
8
+ from web3 import Web3, AsyncWeb3
9
  from tools.final_answer import FinalAnswerTool
10
+ from moralis import evm_api
11
  from Gradio_UI import GradioUI
12
 
13
+
14
+ VALID_NETWORKS = ["eth", "base", "bsc", "gnosis"]
15
+ MAX_TOKENS_CONTEXT = 2096
16
+
17
+
18
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
19
+ def process_wallet_activity(activity_data):
20
+ """Process and summarize wallet activity data to reduce token size
21
+ Args:
22
+ activity_data: Raw activity data from Moralis API
23
+ Returns:
24
+ dict: Summarized activity data
25
+ """
26
+ summary = {
27
+ 'total_transactions': len(activity_data['result']),
28
+ 'tokens': {},
29
+ 'date_range': {
30
+ 'from': None,
31
+ 'to': None
32
+ }
33
+ }
34
+
35
+ for tx in activity_data['result']:
36
+ token_symbol = tx['token_symbol']
37
+ if token_symbol not in summary['tokens']:
38
+ summary['tokens'][token_symbol] = {
39
+ 'count': 0,
40
+ 'total_value': 0,
41
+ 'token_name': tx['token_name'],
42
+ 'token_decimals': tx['token_decimals'],
43
+ 'verified': tx['verified_contract'],
44
+ 'security_score': tx['security_score']
45
+ }
46
+
47
+ summary['tokens'][token_symbol]['count'] += 1
48
+ summary['tokens'][token_symbol]['total_value'] += float(tx['value_decimal'] or 0)
49
+
50
+ tx_date = tx['block_timestamp']
51
+ if not summary['date_range']['from'] or tx_date < summary['date_range']['from']:
52
+ summary['date_range']['from'] = tx_date
53
+ if not summary['date_range']['to'] or tx_date > summary['date_range']['to']:
54
+ summary['date_range']['to'] = tx_date
55
+
56
+ return summary
57
+
58
  @tool
59
+ def get_wallet_activity(
60
+ address: str, network: str = "eth"
61
+ ) -> dict[str, Any]:
62
+ """A tool that gets the tokens transactions of ERC20 of a wallet in a specific blockchain network and returns detailed information.
63
+ The returned data includes:
64
+ - total_transactions: Total number of transactions
65
+ - tokens: Dictionary with token details including:
66
+ - count: Number of transactions for this token
67
+ - total_value: Total value transferred
68
+ - token_name: Full name of the token
69
+ - token_decimals: Number of decimals for the token
70
+ - verified: Whether the token contract is verified
71
+ - security_score: Security score of the token if available
72
+ - date_range: Time range of the transactions
73
+
74
  Args:
75
+ address: the address of the wallet
76
+ network: the blockchain network to get the activity from (default: eth) valid options: "eth", "base", "bsc", "gnosis"
77
+ Returns:
78
+ dict: Detailed wallet activity information that can be analyzed to understand the wallet's behavior
79
  """
80
+ params = {
81
+ "address": address,
82
+ "chain": network,
83
+ "order": "DESC",
84
+ "limit": 50 # free models have a low context window
85
+ }
86
+
87
+ api_key = os.getenv("MORALIS_API_KEY")
88
+ result = evm_api.token.get_wallet_token_transfers(
89
+ api_key=api_key,
90
+ params=params,
91
+ )
92
+
93
+ # Process and return the detailed data for the LLM to analyze
94
+ return process_wallet_activity(result)
95
+
96
 
97
  @tool
98
  def get_current_time_in_timezone(timezone: str) -> str:
 
113
  final_answer = FinalAnswerTool()
114
 
115
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
116
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
117
+
118
+ # Get environment variables - works both locally and in Hugging Face Spaces
119
+ os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN")
120
+
121
 
122
  model = HfApiModel(
123
+ max_tokens=MAX_TOKENS_CONTEXT,
124
+ temperature=0.5,
125
+ model_id="Qwen/Qwen2.5-Coder-32B-Instruct", # it is possible that this model may be overloaded
126
+ custom_role_conversions=None,
127
  )
128
 
129
 
130
  # Import tool from Hub
131
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
132
 
133
+ with open("prompts.yaml", "r") as stream:
134
  prompt_templates = yaml.safe_load(stream)
135
+
136
  agent = CodeAgent(
137
  model=model,
138
+ tools=[final_answer, get_wallet_activity, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
139
  max_steps=6,
140
  verbosity_level=1,
141
  grammar=None,
142
  planning_interval=None,
143
  name=None,
144
  description=None,
145
+ prompt_templates=prompt_templates,
146
  )
147
 
148
 
149
+ GradioUI(agent).launch()