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

Upload Adhoc notebooks #1237

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
173 changes: 173 additions & 0 deletions notebooks/adhoc/debank/api_test.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"Jupyter Notebook: Testing the DeBank API Endpoints\n",
"\n",
"This notebook demonstrates how to query a few of DeBank's key endpoints \n",
"using Python's requests library. You'll need a valid AccessKey (API Token)\n",
"from DeBank to run these successfully.\n",
"\n",
"Note:\n",
"1. In production, consider adding robust error handling, \n",
" rate-limit checks, etc.\n",
"2. Example addresses and chain IDs are for demonstration purposes only.\n",
"3. We only show a handful of endpoints here. Feel free to copy/paste \n",
" and adapt code blocks to query others.\n",
"\"\"\"\n",
"\n",
"import requests\n",
"import json\n",
"\n",
"# === Parameters ===\n",
"API_TOKEN = \"\" # Insert your DeBank AccessKey here.\n",
"TEST_ADDRESS = \"\" # Example from docs\n",
"TEST_CHAIN = \"eth\" # Example: Ethereum\n",
"TEST_PROTOCOL_ID = \"bsc_bdollar\" # Example: bDollar on BSC\n",
"\"\"\"\n",
"Jupyter Notebook: Testing the DeBank API Endpoints (Sampled Output)\n",
"\n",
"In this notebook, we’ll make a handful of DeBank API calls and display \n",
"only small samples of the returned data, so we can quickly see what’s \n",
"going on without flooding ourselves with massive JSON dumps.\n",
"\n",
"Note:\n",
"1. In production, consider adding robust error handling, \n",
" rate-limit checks, caching, etc.\n",
"2. Example addresses and chain IDs are for demonstration purposes only.\n",
"3. We'll show just a small subset of the data from each response \n",
" to keep things clear.\n",
"\"\"\"\n",
"\n",
"# === Helper Function: GET Requests ===\n",
"def debank_get(endpoint, params=None):\n",
" \"\"\"\n",
" Basic helper to GET data from DeBank's API.\n",
" endpoint: str, e.g. '/v1/user/complex_protocol_list'\n",
" params: dict, query params for the request\n",
" \n",
" Returns: dict or list, the parsed JSON response\n",
" \"\"\"\n",
" base_url = \"https://pro-openapi.debank.com\"\n",
" url = base_url + endpoint\n",
" headers = {\n",
" \"accept\": \"application/json\",\n",
" \"AccessKey\": API_TOKEN,\n",
" }\n",
" try:\n",
" response = requests.get(url, headers=headers, params=params, timeout=10)\n",
" response.raise_for_status()\n",
" return response.json()\n",
" except requests.exceptions.RequestException as e:\n",
" print(f\"Request error: {e}\")\n",
" return None\n",
"\n",
"def print_sample_data(data, max_items=2):\n",
" \"\"\"\n",
" Print just the first `max_items` items (or key-value pairs) from the data\n",
" in a nicely formatted way.\n",
" \"\"\"\n",
" # If data is a dict, we can show a few key-value pairs\n",
" if isinstance(data, dict):\n",
" print(\"Data is a dictionary. Showing sample key-value pairs:\\n\")\n",
" for i, (k, v) in enumerate(data.items()):\n",
" print(f\"{k} -> {v}\")\n",
" if i >= max_items - 1:\n",
" break\n",
" # If data is a list, let's show the first few elements\n",
" elif isinstance(data, list):\n",
" print(\"Data is a list. Showing a few items:\\n\")\n",
" for i, item in enumerate(data[:max_items]):\n",
" # Let's pretty-print each item\n",
" print(f\"Item #{i+1}:\")\n",
" print(json.dumps(item, indent=2))\n",
" print(\"-\" * 40)\n",
" else:\n",
" print(\"Data is neither a dict nor a list. Showing raw data:\\n\")\n",
" print(data)\n",
"\n",
"# --------------------------------------------------------------------------------\n",
"# 1) Test: Get user used chain list\n",
"# Endpoint: /v1/user/used_chain_list\n",
"# This returns the list of chains that the user has used.\n",
"# --------------------------------------------------------------------------------\n",
"print(\"=== 1) User Used Chain List (Sample) ===\")\n",
"params_used_chain_list = {\"id\": TEST_ADDRESS}\n",
"used_chain_list = debank_get(\"/v1/user/used_chain_list\", params_used_chain_list)\n",
"print_sample_data(used_chain_list, max_items=2)\n",
"\n",
"# --------------------------------------------------------------------------------\n",
"# 2) Test: Get user chain balance\n",
"# Endpoint: /v1/user/chain_balance\n",
"# Returns the user's total balance in USD on a specific chain.\n",
"# --------------------------------------------------------------------------------\n",
"print(\"\\n=== 2) User Chain Balance (Sample) ===\")\n",
"params_chain_balance = {\"id\": TEST_ADDRESS, \"chain_id\": TEST_CHAIN}\n",
"chain_balance = debank_get(\"/v1/user/chain_balance\", params_chain_balance)\n",
"print_sample_data(chain_balance, max_items=2)\n",
"\n",
"# --------------------------------------------------------------------------------\n",
"# 3) Test: Get user complex protocol list (aka “protocols tab”)\n",
"# Endpoint: /v1/user/complex_protocol_list\n",
"# Returns info about the protocols in which the user has holdings on a single chain.\n",
"# --------------------------------------------------------------------------------\n",
"print(\"\\n=== 3) User Complex Protocol List (Sample) ===\")\n",
"params_complex_protocol = {\"id\": TEST_ADDRESS, \"chain_id\": TEST_CHAIN}\n",
"complex_protocol_list = debank_get(\"/v1/user/complex_protocol_list\", params_complex_protocol)\n",
"print_sample_data(complex_protocol_list, max_items=2)\n",
"\n",
"# --------------------------------------------------------------------------------\n",
"# 4) Test: Get user all complex protocol list (all supported chains)\n",
"# Endpoint: /v1/user/all_complex_protocol_list\n",
"# Returns \"protocols tab\" data for all chains the user has been active on.\n",
"# --------------------------------------------------------------------------------\n",
"print(\"\\n=== 4) User All Complex Protocol List (Sample) ===\")\n",
"params_all_complex = {\"id\": TEST_ADDRESS} # Optionally add chain_ids\n",
"all_complex_protocol_list = debank_get(\"/v1/user/all_complex_protocol_list\", params_all_complex)\n",
"print_sample_data(all_complex_protocol_list, max_items=2)\n",
"\n",
"# --------------------------------------------------------------------------------\n",
"# 5) Test: Get user protocol\n",
"# Endpoint: /v1/user/protocol\n",
"# Returns detailed info about positions in a specific protocol (by ID).\n",
"# --------------------------------------------------------------------------------\n",
"print(\"\\n=== 5) User Protocol (Specific Protocol) (Sample) ===\")\n",
"params_user_protocol = {\"id\": TEST_ADDRESS, \"protocol_id\": TEST_PROTOCOL_ID}\n",
"user_protocol = debank_get(\"/v1/user/protocol\", params_user_protocol)\n",
"print_sample_data(user_protocol, max_items=2)\n",
"\n",
"# --------------------------------------------------------------------------------\n",
"# Conclusion:\n",
"# We only print a small slice of the data (max_items=2 for dict, max_items=1 or 2 for list).\n",
"# This way, we can quickly see the structure without overwhelming ourselves with the entire JSON.\n",
"# --------------------------------------------------------------------------------"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading
Loading