Created
April 9, 2023 17:42
-
-
Save charles-cooper/9784dd4b16079358bd48a0c342fcc254 to your computer and use it in GitHub Desktop.
defillama chatgpt plugin generated by chatgpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from openai_plugin import Plugin | |
class DefiLlama(Plugin): | |
def __init__(self): | |
self.base_url = "https://api.llama.fi" | |
def fetch_data(self, endpoint): | |
try: | |
response = requests.get(f"{self.base_url}/{endpoint}") | |
response.raise_for_status() | |
return response.json() | |
except requests.exceptions.RequestException as e: | |
print(f"Error fetching data from DefiLlama: {e}") | |
return None | |
def get_tvl(self): | |
data = self.fetch_data("tvl") | |
return data if data else "Error fetching TVL data from DefiLlama." | |
def get_protocols(self): | |
data = self.fetch_data("protocols") | |
return data if data else "Error fetching protocols data from DefiLlama." | |
def get_chains(self): | |
data = self.fetch_data("chains") | |
return data if data else "Error fetching chains data from DefiLlama." | |
def on_message(message): | |
text = message.get("text", "").strip() | |
defillama = DefiLlama() | |
if text == "/tvl": | |
tvl_data = defillama.get_tvl() | |
return {"text": f"Current TVL: ${tvl_data['tvlUSD']:,.0f}"} | |
elif text == "/protocols": | |
protocols_data = defillama.get_protocols() | |
protocols = "\n".join([f"{protocol['name']} (TVL: ${protocol['tvl']['total']['USD']:,.0f})" for protocol in protocols_data]) | |
return {"text": f"Protocols:\n{protocols}"} | |
elif text == "/chains": | |
chains_data = defillama.get_chains() | |
chains = "\n".join([f"{chain['name']} (TVL: ${chain['tvl']:,.0f})" for chain in chains_data]) | |
return {"text": f"Chains:\n{chains}"} | |
else: | |
return {"text": "Please use one of the following commands:\n/tvl - Get Total Value Locked (TVL)\n/protocols - List protocols\n/chains - List chains"} | |
openai_plugin.run(on_message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment