{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Intro to Flipside API/SDK: Getting Started\n", "\n", "install Flipside with pip
\n", "`pip install flipside`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import the package" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from flipside import Flipside" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Run your first query
\n", "Remember to copy/paste your API Key from https://flipsidecrypto.xyz/api-keys below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "YOUR_API_KEY = os.environ.get(\"FLIPSIDE_API_KEY\")\n", "\n", "# Invoke the ShroomDK class to create an instance of the SDK\n", "sdk = Flipside(YOUR_API_KEY)\n", "\n", "# Run a query\n", "xMETRIC_contract_address = '0x15848C9672e99be386807b9101f83A16EB017bb5'\n", "\n", "query_result_set = sdk.query(f\"\"\"\n", " SELECT count(distinct to_address) as recipient_count\n", " FROM polygon.core.fact_token_transfers\n", " WHERE block_timestamp > '2022-07-10T00:00:00'\n", " AND contract_address = lower('{xMETRIC_contract_address}')\n", " AND to_address != lower('0x4b8923746a1D9943bbd408F477572762801efE4d')\n", "\"\"\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Query Result Set\n", "\n", "```python\n", "class QueryResultSet(BaseModel):\n", " query_id: Union[str, None] = Field(None, description=\"The server id of the query\")\n", " status: str = Field(False, description=\"The status of the query (`PENDING`, `FINISHED`, `ERROR`)\")\n", " columns: Union[List[str], None] = Field(None, description=\"The names of the columns in the result set\")\n", " column_types: Union[List[str], None] = Field(None, description=\"The type of the columns in the result set\")\n", " rows: Union[List[Any], None] = Field(None, description=\"The results of the query\")\n", " run_stats: Union[QueryRunStats, None] = Field(\n", " None,\n", " description=\"Summary stats on the query run (i.e. the number of rows returned, the elapsed time, etc)\",\n", " )\n", " records: Union[List[Any], None] = Field(None, description=\"The results of the query transformed as an array of objects\")\n", " error: Any\n", "\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Explore the result set object\n", "\n", "records = query_result_set.records\n", "\n", "print(records[0])\n", "\n", "print(f\"There are {records[0]['recipient_count']} unique recipients of xMETRIC tokens.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### xMETRIC Leaderboard\n", "Retrieve the balance of every xMETRIC holder" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "sql" } }, "outputs": [], "source": [ "WITH sent_tokens AS (\n", " SELECT \n", " to_address as Participant,\n", " sum(raw_amount/pow(10,18)) as xMETRIC\n", " FROM polygon.core.fact_token_transfers\n", " WHERE\n", " block_timestamp::date > '2022-07-10'::date \n", " AND contract_address = lower('0x15848C9672e99be386807b9101f83A16EB017bb5')\n", " AND to_address != lower('0x4b8923746a1D9943bbd408F477572762801efE4d')\n", " GROUP BY 1\n", "),\n", "burnt_tokens AS (\n", " SELECT\n", " to_address as Participant,\n", " sum(raw_amount/pow(10,18)) as xMETRIC\n", " FROM polygon.core.fact_token_transfers\n", " WHERE\n", " block_timestamp::date > '2022-07-10'::date \n", " AND contract_address = lower('0x15848C9672e99be386807b9101f83A16EB017bb5')\n", " AND to_address = lower('0x0000000000000000000000000000000000000000')\n", " GROUP BY 1\n", ")\n", "SELECT\n", " sent_tokens.Participant as \"participant_addr\",\n", " coalesce(sent_tokens.xmetric,0) - coalesce(burnt_tokens.xMETRIC,0) as \"balance\"\n", "FROM sent_tokens \n", "LEFT JOIN burnt_tokens ON sent_tokens.Participant = burnt_tokens.Participant\n", "ORDER BY 2 DESC" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load the sql query from a file\n", "leaderboard_sql_query = open(\"./sql/xmetric_leaderboard.sql\", 'r').read()\n", "\n", "# Run the query with pagination\n", "\n", "page_number = 1\n", "page_size = 10\n", "\n", "leaderboard_result_set = sdk.query(\n", " leaderboard_sql_query, \n", " page_size=page_size,\n", " page_number=page_number)\n", "\n", "for record in leaderboard_result_set.records:\n", " print(record)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plot the xMETRIC LeaderBoard Results" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "full_leaderboard_result_set = sdk.query(leaderboard_sql_query)\n", "\n", "import pandas as pd\n", "import plotly.express as px\n", "\n", "df = pd.DataFrame(full_leaderboard_result_set.records)\n", "\n", "fig = px.histogram(df, x=\"balance\", marginal=\"box\", hover_data=df.columns, nbins=200)\n", "\n", "fig.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Cross Chain xMETRIC User Exploration" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "sql" } }, "outputs": [], "source": [ "WITH xmetric_holders AS (\n", " SELECT to_address as holder_addr\n", " FROM polygon.core.fact_token_transfers\n", " WHERE block_timestamp > '2022-07-10T00:00:00'\n", " AND contract_address = lower('0x15848C9672e99be386807b9101f83A16EB017bb5')\n", " AND to_address != lower('0x4b8923746a1D9943bbd408F477572762801efE4d')\n", ")\n", "SELECT\n", " token_name,\n", " symbol,\n", " count(distinct user_address) as num_holders,\n", " median(usd_value_now) as median_usd_holdings\n", "FROM ethereum.core.ez_current_balances\n", "INNER JOIN xmetric_holders \n", " ON ethereum.core.ez_current_balances.user_address = xmetric_holders.holder_addr\n", "WHERE ethereum.core.ez_current_balances.usd_value_now > 0\n", "GROUP BY 1, 2\n", "ORDER BY 3 DESC\n", "LIMIT 25" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load the sql query from a file\n", "xmetric_eth_holdings_sql_query = open(\"./sql/xmetric_eth_holdings.sql\", 'r').read()\n", "\n", "# Run the query\n", "xmetric_eth_holdings_results = sdk.query(xmetric_eth_holdings_sql_query)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Plot the results\n", "df = pd.DataFrame(xmetric_eth_holdings_results.records)\n", "\n", "fig = px.bar(df, x=\"token_name\", y=\"num_holders\", hover_data=df.columns)\n", "\n", "fig.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.1 64-bit", "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.10.1" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" } } }, "nbformat": 4, "nbformat_minor": 2 }