Skip to content

Instantly share code, notes, and snippets.

@phantomk
Created March 3, 2019 13:42
Show Gist options
  • Save phantomk/b77c97afd5bcfa68925575e05cb28d14 to your computer and use it in GitHub Desktop.
Save phantomk/b77c97afd5bcfa68925575e05cb28d14 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## EOS_RAM"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"import random\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"max_ram_size = 64 * 1024 * 1024 * 1024 # 64G RAM\n",
"total_ram_bytes_reserved = 0 # 已占用的RAM\n",
"total_ram_size_free = max_ram_size-total_ram_bytes_reserved # RAM余额\n",
"system_token_supply = 1000000000 # EOS发行量"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"supply = {'amount':100000000000000, 'symbol':'RAMCORE'} \n",
"base = {'amount':total_ram_size_free, 'symbol':'RAM', 'weight':.5}\n",
"quote = {'amount':system_token_supply / 1000, 'symbol':'EOS', 'weight':.5}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def convert_to_exchange(c, in_token):\n",
" global supply\n",
" R = supply['amount'] \n",
" C = c['amount'] + in_token['amount']\n",
" F = c['weight'] / 1000.0\n",
" T = in_token['amount']\n",
" ONE = 1.0\n",
" E = -R * (ONE - math.pow( ONE + T / C, F))\n",
" #P = C/(F*R)\n",
" supply['amount'] += E;\n",
" c['amount'] += in_token['amount'];\n",
" return c, {'amount': E,'symbol': supply['symbol']}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def convert_from_exchange(c, in_token):\n",
" global supply\n",
" R = supply['amount'] - in_token['amount']\n",
" C = c['amount']\n",
" F = 1000.0 / c['weight']\n",
" E = in_token['amount']\n",
" ONE = 1.0\n",
" T = C * (math.pow(ONE + E/R, F) - ONE)\n",
" supply['amount'] -= in_token['amount']\n",
" c['amount'] -= T;\n",
" return c, {'amount': T,'symbol': c['symbol']}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def convert(from_token, to_symbol):\n",
" global supply\n",
" global base\n",
" global quote\n",
" sell_symbol = from_token['symbol']\n",
" ex_symbol = supply['symbol']\n",
" base_symbol = base['symbol']\n",
" quote_symbol = quote['symbol']\n",
" if sell_symbol != ex_symbol:\n",
" if sell_symbol == base_symbol:\n",
" base, from_token = convert_to_exchange(base, from_token)\n",
" elif sell_symbol == quote_symbol:\n",
" quote, from_token = convert_to_exchange(quote, from_token)\n",
" else:\n",
" print(\"invalid sell\")\n",
" else:\n",
" if to_symbol == base_symbol:\n",
" base, from_token = convert_from_exchange(base, from_token )\n",
" elif to_symbol == quote_symbol:\n",
" quote, from_token = convert_from_exchange(quote, from_token )\n",
" else:\n",
" print(\"invalid conversion\")\n",
" \n",
" if to_symbol != from_token['symbol']:\n",
" return convert(from_token, to_symbol)\n",
" return from_token"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def buy_ram(amount):\n",
" fee = amount / 200\n",
" amount_after_fee = amount - fee\n",
" ram = convert({'symbol':'EOS','amount':amount_after_fee},'RAM')\n",
" price = amount / (ram['amount'] / 1024.0)\n",
" percent = 1 - base['amount'] / max_ram_size\n",
" return price, ram['amount'], percent"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def buy_ram_bytes(amount):\n",
" eos = convert({'symbol':'RAM','amount':amount},'EOS')\n",
" price, ram, percent = buy_ram(eos['amount'])\n",
" return price, ram, percent, eos['amount']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def sell_ram(amount):\n",
" eos = convert({'symbol':'RAM','amount':amount},'EOS')\n",
" fee = eos['amount'] / 200\n",
" eos_after_fee = eos['amount'] - fee\n",
" price = eos['amount'] / ( amount / 1024.0)\n",
" percent = 1 - base['amount'] / max_ram_size\n",
" return price, eos_after_fee, percent"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def restart():\n",
" global supply\n",
" global base\n",
" global quote\n",
" supply = {'amount':100000000000000, 'symbol':'RAMCORE'} #\n",
" base = {'amount':total_ram_size_free, 'symbol':'RAM', 'weight':.5}\n",
" quote = {'amount':system_token_supply / 1000, 'symbol':'EOS', 'weight':.5}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def add_ram(amount):\n",
" # bytes\n",
" global base\n",
" base['amount'] += amount"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"wanner_percent = 0.8\n",
"x = []\n",
"y = []\n",
"#z = []\n",
"step = 1000\n",
"while ((1 - base['amount'] / max_ram_size) <= wanner_percent):\n",
" price, ram, percent = buy_ram(step)\n",
" x.append(percent)\n",
" y.append(price)\n",
" #z.append(ram)\n",
"plt.figure(figsize=(8,4))\n",
"plt.plot(np.array(x),np.array(y),label=\"$price$\",color=\"red\",linewidth=2)\n",
"plt.xlabel(\"Ram Sold Percentage\")\n",
"plt.ylabel(\"Price\")\n",
"plt.title(\"EOS RAM\")\n",
"plt.ylim(0.0,max(y)*1.1)\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"price, ram, percent = buy_ram(1)\n",
"print(price, ram, percent)\n",
"print(quote)"
]
}
],
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment