Need to put in a loop that works
api_key = "5JMy60ThHmJWvS8kXnxJfNjrThMooHfW2IWsOSQA9ZfLh94g3Yryj8GR0NenQQ1f"
params = {
"address": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
"chain": "eth"
}
try:
result = evm_api.token.get_token_price(
api_key=api_key,
params=params,
)
# Assuming that the API response is in JSON format with a "usdPrice" field
usd_price = result.get("usdPrice")
if usd_price:
print(f"Bitcoin (USD): ${usd_price}")
else:
print("USD Price not found in the API response.")
except Exception as e:
print(f"An error occurred: {e}")```
i want to add a loop bt i dont really kno how
We have here an example of using loops for the price, you can check it and make your code similar to it
https://docs.moralis.io/web3-data-api/evm/how-to-get-historical-erc20-token-price?programming-language=python
that makes a loop every 5 mins?
You can also check our full video tutorial here - https://www.youtube.com/watch?v=B5tsTsIHOeo, which shows how to make the loops
Iโm using Python he uses js
The logic will be same as in the tutorial you just have to add the similar loop logic in your python code.
Have a look at the solution on the below forum post on how to add timed loops in python.
If I do that the rest of my files doesnโt load
How does it affect your files?
When that loop runs in that file it looks like it freezes the rest of the startup
If you remove the loop does it work?
Try adding any prints to check if the expected code is running
If I remove the loop it works and it does print the price with the loop
Maybe you are calling the loop in an async function? If possible try making multiple parallel requests at once.
Can I send my code and you can edit it
api_key = "API key"
params = {
"address": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
"chain": "eth"
}
try:
result = evm_api.token.get_token_price(
api_key=api_key,
params=params,
)
# Assuming that the API response is in JSON format with a "usdPrice" field
usd_price = result.get("usdPrice")
if usd_price:
print(f"Bitcoin (USD): ${usd_price}")
else:
print("USD Price not found in the API response.")
except Exception as e:
print(f"An error occurred: {e}")```
Can u add a loop here and Iโll see if it works
Here is an example code with loops. I used threading to run the loop in the background so it should not affect another part of your app.
from moralis import evm_api
import threading
import time
api_key = "API Key"
params = {"address": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599", "chain": "eth"}
# Separate the logic into a function to fetch the token price.
def get_token_price():
try:
result = evm_api.token.get_token_price(
api_key=api_key,
params=params,
)
usd_price = result.get("usdPrice")
if usd_price:
print(f"Bitcoin (USD): ${usd_price}")
else:
print("USD Price not found in the API response.")
except Exception as e:
print(f"An error occurred: {e}")
# Loop that runs in a separate thread.
def price_loop(stop_event):
while not stop_event.is_set():
get_token_price()
time.sleep(5) # Pause for 5 seconds before the next API call
# Function to start the lop
def start_loop():
stop_event.clear()
threading.Thread(target=price_loop, args=(stop_event,)).start()
# Function to stop the loop
def stop_loop():
stop_event.set()
stop_event = threading.Event()
K Tks Iโll let u kno if it works I just need the change the api key right
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.