Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add coingecko #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 224 additions & 0 deletions integrations/coingecko.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import requests

"""
List all supported NFT, ordered by 24h trading volume of native token
Response in the format:
[{'id': 'drawshop-kingdom-reverse',
'contract_address': '0x253ebdb767f18002a22cbb26176356efeb0bf641',
'name': 'Drawshop Kingdom Reverse',
'asset_platform_id': 'klay-token',
'symbol': 'DKR'},
{'id': 'puuvillasociety',
'contract_address': '0xd643bb39f81ff9079436f726d2ed27abc547cb38',
'name': 'Puuvilla Society',
'asset_platform_id': 'klay-token',
'symbol': 'Puuvilla'}]
"""
def get_top_nft_by_24h_native_token():
url = f'https://api.coingecko.com/api/v3/nfts/list?order=h24_volume_native_desc'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe good to extract the base api url out into a constant?

response = requests.get(url)
response.raise_for_status()
result = response.json()
return ','.join(["{name} on address {contract_address}".format(name=item['name'],contract_address=item['contract_address']) for item in result])


"""
List all supported NFT, ordered by floor price of native token
Response in the format:
[{'id': 'drawshop-kingdom-reverse',
'contract_address': '0x253ebdb767f18002a22cbb26176356efeb0bf641',
'name': 'Drawshop Kingdom Reverse',
'asset_platform_id': 'klay-token',
'symbol': 'DKR'},
{'id': 'puuvillasociety',
'contract_address': '0xd643bb39f81ff9079436f726d2ed27abc547cb38',
'name': 'Puuvilla Society',
'asset_platform_id': 'klay-token',
'symbol': 'Puuvilla'}]
"""
def get_top_nft_by_floor_price_native_token():
url = f'https://api.coingecko.com/api/v3/nfts/list?order=floor_price_native_desc'
response = requests.get(url)
response.raise_for_status()
result = response.json()
return ','.join(["{name} on address {contract_address}".format(name=item['name'],contract_address=item['contract_address']) for item in result])


"""
List all supported NFT, ordered by market cap of native token
Response in the format:
[{'id': 'drawshop-kingdom-reverse',
'contract_address': '0x253ebdb767f18002a22cbb26176356efeb0bf641',
'name': 'Drawshop Kingdom Reverse',
'asset_platform_id': 'klay-token',
'symbol': 'DKR'},
{'id': 'puuvillasociety',
'contract_address': '0xd643bb39f81ff9079436f726d2ed27abc547cb38',
'name': 'Puuvilla Society',
'asset_platform_id': 'klay-token',
'symbol': 'Puuvilla'}]
"""
def get_top_nft_by_market_cap_native_token():
url = f'https://api.coingecko.com/api/v3/nfts/list?order=market_cap_native_desc'
response = requests.get(url)
response.raise_for_status()
result = response.json()
return ','.join(["{name} on address {contract_address}".format(name=item['name'],contract_address=item['contract_address']) for item in result])


"""
List all supported NFT, ordered by market cap of USD
Response in the format:
[{'id': 'drawshop-kingdom-reverse',
'contract_address': '0x253ebdb767f18002a22cbb26176356efeb0bf641',
'name': 'Drawshop Kingdom Reverse',
'asset_platform_id': 'klay-token',
'symbol': 'DKR'},
{'id': 'puuvillasociety',
'contract_address': '0xd643bb39f81ff9079436f726d2ed27abc547cb38',
'name': 'Puuvilla Society',
'asset_platform_id': 'klay-token',
'symbol': 'Puuvilla'}]
"""
def get_top_nft_by_marketcap_usd():
url = f'https://api.coingecko.com/api/v3/nfts/list?order=market_cap_usd_desc'
response = requests.get(url)
response.raise_for_status()
result = response.json()
return ','.join(["{name} on address {contract_address}".format(name=item['name'],contract_address=item['contract_address']) for item in result])



"""
Top-7 trending coins on CoinGecko as searched by users in the last 24 hours (Ordered by most popular first)
Response in the format:
{
"coins": [
{
"item": {
"id": "camelot-token",
"coin_id": 28416,
"name": "Camelot Token",
"symbol": "GRAIL",
"market_cap_rank": 500,
"thumb": "https://assets.coingecko.com/coins/images/28416/thumb/vj5DIMhP_400x400.jpeg?1670457013",
"small": "https://assets.coingecko.com/coins/images/28416/small/vj5DIMhP_400x400.jpeg?1670457013",
"large": "https://assets.coingecko.com/coins/images/28416/large/vj5DIMhP_400x400.jpeg?1670457013",
"slug": "camelot-token",
"price_btc": 0.16421050648435834,
"score": 0
}
}]
"""
def get_top_searched_token_last_24h():
url = f'https://api.coingecko.com/api/v3/search/trending'
response = requests.get(url)
response.raise_for_status()
result = response.json()
return ','.join(["{name} with symbol {symbol}".format(name=item['item']['name'],symbol=item['item']['symbol']) for item in result])


"""
Get Top 100 Cryptocurrency Global Decentralized Finance(defi) data
Response in the format:
{
"data": {
"defi_market_cap": "49345991438.7624632293259565585",
"eth_market_cap": "215715925887.586366238685068393",
"defi_to_eth_ratio": "22.8754512378829132927013639333919477262638870348465613291519787",
"trading_volume_24h": "3451809782.9077426466020906851",
"defi_dominance": "4.0986673335718624991995052507291142723503369772850766789102689",
"top_coin_name": "Lido Staked Ether",
"top_coin_defi_dominance": 21.084575288583714
}
}
"""
def getGlobalDefiData():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good to use the pythonic style for functions here, just for consistency

url = f'https://api.coingecko.com/api/v3/global/decentralized_finance_defi'
response = requests.get(url)
response.raise_for_status()
result = response.json()['data']
return result

"""
Get Defi Market Cap
"""
def get_defi_market_cap():
data=getGlobalDefiData()
return data['defi_market_cap']

"""
Get Ether Market Cap
"""
def get_eth_market_cap():
data=getGlobalDefiData()
return data['eth_market_cap']

"""
Get Trading Volumn in the last 24h
"""
def get_trading_volume_last_24h():
data=getGlobalDefiData()
return data['trading_volume_24h']

"""
Get Top Coin Volume
"""
def get_top_coin_name():
data=getGlobalDefiData()
return data['top_coin_name']


"""
Get public companies bitcoin or ethereum holdings (Ordered by total holdings descending)
Response in the format:
{
"total_holdings": 174374.4658,
"total_value_usd": 4764147923.454023,
"market_cap_dominance": 0.9,
"companies": [
{
"name": "MicroStrategy Inc.",
"symbol": "NASDAQ:MSTR",
"country": "US",
"total_holdings": 129699,
"total_entry_value_usd": 3975000000,
"total_current_value_usd": 3543553344,
"percentage_of_total_supply": 0.618
}]}
"""
def get_top_public_companies_holding_bitcoin():
url = f'https://api.coingecko.com/api/v3/companies/public_treasury/bitcoin'
response = requests.get(url)
response.raise_for_status()
result = response.json()['companies']
return ','.join(["{name} with total holding of {total_holdings}".format(name=item['name'],total_holdings=item['total_holdings']) for item in result])



"""
Get public companies ethereum holdings (Ordered by total holdings descending)
Response in the format:
{
"total_holdings": 174374.4658,
"total_value_usd": 4764147923.454023,
"market_cap_dominance": 0.9,
"companies": [
{
"name": "MicroStrategy Inc.",
"symbol": "NASDAQ:MSTR",
"country": "US",
"total_holdings": 129699,
"total_entry_value_usd": 3975000000,
"total_current_value_usd": 3543553344,
"percentage_of_total_supply": 0.618
}]}
"""
def get_top_public_companies_holding_eth():
url = f'https://api.coingecko.com/api/v3/companies/public_treasury/ethereum'
response = requests.get(url)
response.raise_for_status()
result = response.json()['companies']
return ','.join(["{name} with total holding of {total_holdings}".format(name=item['name'],total_holdings=item['total_holdings']) for item in result])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these return comma-delimited text lists? or should we try to use table/list containers?

it would be helpful to have the return describe something like "An NFT collection with name {name} and address {address}", instead of just "{name} with address {address}". If you see the center.py integration, there could be some useful examples to follow for containers.



44 changes: 44 additions & 0 deletions knowledge_base/widgets.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,50 @@ Required parameters:
Return value description:
-JSON object with trait names and values of the NFT asset
---
Widget magic command: <|fetch-nft-list-order-by-trading-volume-native-token()|>
Description of widget: List all supported NFT, ordered by 24h trading volume of native token
Connected wallet required: no
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry i experimented with removing this in another diff to simplify wallet stuff, so we can drop this. but we should add a "Return value description" to any fetch-* commands, so the AI knows what to expect.

Also, this diff adds a lot more commands, I think the description of the widget needs to be very clear about what it is doing, so the tool knows when to pull up a given widget and use it. E.g. instead of market cap, it could state market capitalization. also trading volume of what tokens, etc.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, this might cause performance of other demo flows to drop, we will need to test it rigorously, or collapse into fewer, more specific use cases. not sure

---
Widget magic command: <|fetch-nft-list-order-by-floor-price()|>
Description of widget: List all supported NFT, ordered by floor price of native token
Connected wallet required: no
---
Widget magic command: <|fetch-nft-list-order-by-market-cap-native-token()|>
Description of widget: List all supported NFT, ordered by market cap of native token
Connected wallet required: no
---
Widget magic command: <|fetch-nft-list-order-by-market-cap-usd()|>
Description of widget: List all supported NFT, ordered by market cap of USD
Connected wallet required: no
---
Widget magic command: <|fetch-top-searched-token()|>
Description of widget: Top-7 trending coins on CoinGecko as searched by users in the last 24 hours (Ordered by most popular first)
Connected wallet required: no
---
Widget magic command: <|fetch-defi-market-cap()|>
Description of widget: Get Defi Market Cap
Connected wallet required: no
---
Widget magic command: <|fetch-ether-market-cap()|>
Description of widget: Get Ether Market Cap
Connected wallet required: no
---
Widget magic command: <|fetch-trading-volume-last-24h()|>
Description of widget: Get Trading Volume in the last 24h
Connected wallet required: no
---
Widget magic command: <|fetch-top-coin-volume()|>
Description of widget: Get Top Coin Volume
Connected wallet required: no
---
Widget magic command: <|fetch-top-public-companies-holding-bitcoin()|>
Description of widget: Get public companies bitcoin holdings (Ordered by total holdings descending)
Connected wallet required: no
---
Widget magic command: <|fetch-top-public-companies-holding-ether()|>
Description of widget: Get public companies ethereum holdings (Ordered by total holdings descending)
Connected wallet required: no
---
Widget magic command: <|fetch-eval({expression})|>
Description of widget: This widget is used when we need to calculate an arbitrary mathematical expression.
Connected wallet required: no
Expand Down
27 changes: 27 additions & 0 deletions tools/index_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
from integrations import (
etherscan, defillama, center,
)
from integrations.coingecko import get_top_nft_by_24h_native_token, \
get_top_nft_by_floor_price_native_token, get_top_nft_by_market_cap_native_token, \
get_top_nft_by_marketcap_usd, get_top_searched_token_last_24h, get_defi_market_cap, \
get_eth_market_cap, get_trading_volume_last_24h, get_top_coin_name, \
get_top_public_companies_holding_bitcoin, get_top_public_companies_holding_eth
from .index_lookup import IndexLookupTool


Expand Down Expand Up @@ -158,6 +163,28 @@ def replace_match(m: re.Match) -> str:
return str(fetch_nft_asset(*params))
elif command == 'fetch-nft-asset-traits':
return str(fetch_nft_asset_traits(*params))
elif command == 'fetch-nft-list-order-by-trading-volume-native-token':
return str(get_top_nft_by_24h_native_token())
elif command == 'fetch-nft-list-order-by-floor-price':
return str(get_top_nft_by_floor_price_native_token())
elif command == 'fetch-nft-list-order-by-market-cap-native-token':
return str(get_top_nft_by_market_cap_native_token())
elif command == 'fetch-nft-list-order-by-market-cap-usd':
return str(get_top_nft_by_marketcap_usd())
elif command == 'fetch-top-searched-token':
return str(get_top_searched_token_last_24h())
elif command == 'fetch-defi-market-cap':
return str(get_defi_market_cap())
elif command == 'fetch-ether-market-cap':
return str(get_eth_market_cap())
elif command == 'fetch-trading-volume-last-24h':
return str(get_trading_volume_last_24h())
elif command == 'fetch-top-coin-volume':
return str(get_top_coin_name())
elif command == 'fetch-top-public-companies-holding-bitcoin':
return str(get_top_public_companies_holding_bitcoin())
elif command == 'fetch-top-public-companies-holding-ether':
return str(get_top_public_companies_holding_eth())
elif command == 'fetch-eval':
return str(safe_eval(*params))
elif command == 'fetch-wallet':
Expand Down