mango-explorer/ShowAllAccounts.ipynb

173 lines
5.8 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"id": "forty-jurisdiction",
"metadata": {},
"source": [
"# ⚠ Warning\n",
"\n",
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n",
"\n",
"[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gl/OpinionatedGeek%2Fmango-explorer/HEAD?filepath=ShowAllAccounts.ipynb) _🏃 To run this notebook press the ⏩ icon in the toolbar above._\n",
"\n",
"[🥭 Mango Markets](https://mango.markets/) support is available at: [Docs](https://docs.mango.markets/) | [Discord](https://discord.gg/67jySBhxrg) | [Twitter](https://twitter.com/mangomarkets) | [Github](https://github.com/blockworks-foundation) | [Email](mailto:hello@blockworks.foundation)"
]
},
{
"cell_type": "markdown",
"id": "modern-crash",
"metadata": {},
"source": [
"# 🥭 Show All Accounts\n",
"\n",
"This notebook tries to display information about all Mango margin accounts.\n",
"\n",
"It fetches the data from Solana, parses it, and then prints it.\n"
]
},
{
"cell_type": "markdown",
"id": "cheap-brazil",
"metadata": {},
"source": [
"## How To Use This Page\n",
"\n",
"Theo code should be runnable as-is. Just click the >> button in the toolbar above, and you should see output appear below the code."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "tutorial-listing",
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [],
"source": [
"import logging\n",
"logging.getLogger().setLevel(logging.ERROR)\n",
"\n",
"from pyserum.market import Market\n",
"\n",
"from BaseModel import Group, MarginAccount, OpenOrders, TokenAccount\n",
"from Context import default_context\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "impaired-northern",
"metadata": {},
"outputs": [],
"source": [
"def show_all_accounts():\n",
" print(\"Context:\", default_context);\n",
"\n",
" group = Group.load(default_context)\n",
" print(\"Group:\", group)\n",
"\n",
" markets = list(map(lambda market: Market.load(default_context.client, market.spot), group.markets))\n",
" print(\"Markets:\", markets)\n",
"\n",
" vaults = default_context.load_multiple_accounts([token.vault for token in group.tokens])\n",
" print(\"Vaults:\", vaults)\n",
"\n",
" for index, vault in enumerate(vaults):\n",
" token = TokenAccount.parse(vault.data)\n",
" decimals = group.tokens[index].decimals\n",
" amount = token.amount / (10 ** decimals)\n",
" print(f\"Vault token amount[{index}]: {amount:,.8f}\")\n",
"\n",
" import time\n",
" start_time = time.time()\n",
" print(\"Loading margin accounts...\")\n",
" margin_accounts = MarginAccount.load_all_for_group(default_context, default_context.program_id, group)\n",
" print(f\"Done. Time taken: {time.time() - start_time}\")\n",
"\n",
" print(\"Loading open orders accounts...\")\n",
" # Calling margin_account.load_open_orders() in a loop for each margin account would take a long time.\n",
" # We can do better. Load all the relevant OpenOrders accounts, and then just call each margin account\n",
" # to map its own OpenOrders from the loaded OpenOrders dictionary.\n",
" open_orders = OpenOrders.load_raw_open_orders_accounts(default_context, group)\n",
" open_orders_by_address = {key: value for key, value in [(str(address), open_orders_account) for address, open_orders_account in open_orders]}\n",
" for margin_account in margin_accounts:\n",
" margin_account.install_open_orders_accounts(group, open_orders_by_address)\n",
" print(f\"Done. Time taken: {time.time() - start_time}\")\n",
"\n",
" print(margin_accounts)\n",
"\n",
"show_all_accounts()\n",
"# import cProfile\n",
"# import pstats\n",
"# cProfile.run(\"show_all_accounts()\", sort=pstats.SortKey.TIME)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.6"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}