|
8 | 8 | "\n", |
9 | 9 | "### Pool address: 0x8b6e6E7B5b3801FEd2CaFD4b22b8A16c2F2Db21a -- 80% WETH - 20% DAI \n", |
10 | 10 | "\n", |
11 | | - "The Balancer Pool cadCAD model provides a core infrastructure for simulating Balancer Pools in the **Token Engineering design and verification process**.\n", |
| 11 | + "The Balancer Pool cadCAD model provides a core infrastructure for simulating Balancer Pools in the **Token Engineering Design and Verification process**.\n", |
12 | 12 | "\n", |
13 | | - "It allows to simulate all types of Balancer Pool transactions and verify new designs and application cases. \n", |
14 | | - "In this notebook we plug **historical on-chain data** to the cadCAD model, and analyse **Pool Power** and **Pool Characteristics**.\n", |
| 13 | + "It allows to simulate Balancer Pool transactions and verify new designs and application cases. \n", |
| 14 | + "In this notebook we plug **historical on-chain data** to a cadCAD model, and analyse **Pool Power** and **Pool Characteristics**.\n", |
15 | 15 | "\n", |
16 | 16 | "For more information check out the [Balancer Simulations documentation](https://token-engineering-balancer.gitbook.io/balancer-simulations/)." |
17 | 17 | ] |
|
27 | 27 | "cell_type": "markdown", |
28 | 28 | "metadata": {}, |
29 | 29 | "source": [ |
30 | | - "### System Specification\n", |
| 30 | + "### A1.1 System Specification\n", |
31 | 31 | "System specification details are available in the Balancer Simulations documentation:\n", |
32 | | - "- [Differential Specification](https://app.gitbook.com/@token-engineering-balancer/s/balancer-simulation-portal/v/master/balancer-simulations/nboverview)\n", |
33 | | - "- [Mathematical Specification](https://app.gitbook.com/@token-engineering-balancer/s/balancer-simulation-portal/v/master/additional-code-and-instructions/balancer-the-python-edition/)\n", |
34 | | - "- [Model Architecture](https://app.gitbook.com/@token-engineering-balancer/s/balancer-simulation-portal/v/master/balancer-simulations/nboverview)\n", |
35 | | - "- [Naming Convention](https://app.gitbook.com/@token-engineering-balancer/s/balancer-simulation-portal/v/master/additional-code-and-instructions/naming-convention)" |
| 32 | + "- [Differential Specification](https://token-engineering-balancer.gitbook.io/balancer-simulations/balancer-simulations/v10nboverview)\n", |
| 33 | + "- [Mathematical Specification](https://token-engineering-balancer.gitbook.io/balancer-simulations/additional-code-and-instructions/balancer-the-python-edition/balancer_math.py)\n", |
| 34 | + "- [Model Architecture](https://token-engineering-balancer.gitbook.io/balancer-simulations/balancer-simulations/v10nboverview)\n", |
| 35 | + "- [Naming Convention](https://token-engineering-balancer.gitbook.io/balancer-simulations/additional-code-and-instructions/naming-convention)" |
36 | 36 | ] |
37 | 37 | }, |
38 | 38 | { |
|
70 | 70 | "cell_type": "markdown", |
71 | 71 | "metadata": {}, |
72 | 72 | "source": [ |
73 | | - "This simulation is routing on-chain transaction data through the cadCAD model, adding USD price feeds. \n", |
74 | | - "For more information how to add transaction and price data please visit the [Balancer Simulations documentation](https://app.gitbook.com/@token-engineering-balancer/s/balancer-simulation-portal/v/master/additional-code-and-instructions/transactions-pricefeeds). \n", |
75 | | - "(XX - remove spot price reference)" |
| 73 | + "In this section you specify the Balancer Pool to be analyzed and run scripts to produce the'genesis_state', based on on-chain data. \n", |
| 74 | + "\n", |
| 75 | + "More information here: [Balancer Simulations documentation](https://token-engineering-balancer.gitbook.io/balancer-simulations/additional-code-and-instructions/onchaintransactions). " |
76 | 76 | ] |
77 | 77 | }, |
78 | 78 | { |
79 | 79 | "cell_type": "code", |
80 | | - "execution_count": null, |
| 80 | + "execution_count": 1, |
81 | 81 | "metadata": {}, |
82 | 82 | "outputs": [], |
83 | 83 | "source": [ |
|
86 | 86 | "\n", |
87 | 87 | "from decimal import Decimal\n", |
88 | 88 | "\n", |
89 | | - "# Spot prices are needed for relevant notebooks and it's needed in the CadCAD model. Ignore this param here\n", |
90 | | - "# Spot price reference must be a symbol of a token in the pool in ALL_CAPS\n", |
| 89 | + "# Spot price reference must be a symbol of a token in the pool in ALL_CAPS, you can ignore the spot price parameter for the simulations in this notebook.\n", |
91 | 90 | "parameters = {\n", |
92 | 91 | " 'spot_price_reference': ['DAI'],\n", |
93 | 92 | "}\n" |
|
99 | 98 | "source": [ |
100 | 99 | "#### Import Pool Transactions and Initialize Pool state\n", |
101 | 100 | "\n", |
102 | | - "Pool Transactions, initial pool state and USD token prices are pulled from on-chain data collected in a .json file that has to be referenced here. \n", |
| 101 | + "Genesis state, pool transactions, and (external) USD token prices are pulled from on-chain/API data collected in a .json file. \n", |
| 102 | + "**Choose *your* Balancer Pool, and produce this .json file first, then add the file path below.**\n", |
103 | 103 | "\n", |
104 | | - "Learn more how to create this file using our parsing scripts in the [documentation](https://app.gitbook.com/@token-engineering-balancer/s/balancer-simulation-portal/v/master/additional-code-and-instructions/transactions-pricefeeds).\n" |
| 104 | + "Learn more how to create this file using parsing scripts in the [documentation](https://token-engineering-balancer.gitbook.io/balancer-simulations/v/master/additional-code-and-instructions/onchaintransactions).\n" |
105 | 105 | ] |
106 | 106 | }, |
107 | 107 | { |
|
162 | 162 | "source": [ |
163 | 163 | "### B1.3 State Update Functions & Policies\n", |
164 | 164 | "\n", |
165 | | - "cadCAD state update functions replicate the following Balancer Pool Transactions (see system policies.py) \n", |
166 | | - "\n", |
167 | | - "\n", |
168 | | - "\n", |
169 | | - "**a) Add Liquidity**\n", |
170 | | - "= join policy \n", |
171 | | - "- `p_join_pool` (all-asset deposit)\n", |
172 | | - "- `p_join_swap_extern_amount_in` (single-asset deposit)\n", |
173 | | - "\n", |
174 | | - "\n", |
175 | | - "**b) Withdraw Liquidity**\n", |
176 | | - "= exit policy \n", |
177 | | - "- `p_exit_pool` (all-asset withdrawal) \n", |
178 | | - "- `p_exit_swap_extern_amount_out` (single-asset withdrawal) # NOTE, next version will use p_exit_swap_pool_amount_in\n", |
179 | | - "\n", |
180 | | - "**C) Swap**\n", |
181 | | - "= swap policy \n", |
182 | | - "- `p_swap_exact_amount_in` (out-given-in)\n", |
183 | | - "\n", |
184 | | - "\n", |
185 | | - "Balancer Pools offer additional transaction types:\n", |
186 | | - "- `p_swap_exact_amount_out` (in-given-out)\n", |
187 | | - "- `p_join_swap_pool_amount_out` (pool_out_given_single_in)\n", |
188 | | - "- `p_exit_swap_pool_amount_in`(single_out_given_pool_in)\n", |
189 | | - "Which is not applied in Balancer Simulations V1.0 since we are using only data from LOG_JOIN, LOG_EXIT and LOG_SWAP events from BPool contract. This does not provide information of the method selected by pool user.\n", |
190 | | - "\n", |
191 | | - "#### Fees\n", |
192 | | - "\n", |
193 | | - "Each `swap`, `join_swap` and `exit_swap` will have the pool collect a fee in the token goin into it. This fee is proportional to the `swap_fee` pool variable. We extract the token fee value from BMath methods and store it in `generated_fee_token_XX` state variables\n", |
194 | | - "\n", |
195 | | - " \n", |
196 | | - "\n", |
197 | | - "For a detailed description of the transactions, please visit the [Balancer Simulations documentation/Balancer Pool Functions](https://app.gitbook.com/@token-engineering-balancer/s/balancer-simulation-portal/v/master/additional-code-and-instructions/balancer-the-python-edition/balancer-pool-functions)." |
| 165 | + "Balancer Simulations replicate Balancer Pool Transactions in state update functions.\n", |
| 166 | + "For a detailed description on how we implement transactions, please visit the [Balancer Simulations documentation/Balancer Pool Functions](https://token-engineering-balancer.gitbook.io/balancer-simulations/v/master/additional-code-and-instructions/balancer-the-python-edition/balancer-pool-functions)." |
198 | 167 | ] |
199 | 168 | }, |
200 | 169 | { |
|
203 | 172 | "source": [ |
204 | 173 | "### B1.4 Partial State Update Blocks\n", |
205 | 174 | "\n", |
206 | | - "Partial State Update Blocks combine the following steps:\n", |
207 | | - "0. Skip initial action (pool creation)\n", |
208 | | - "1. Parse actions each timestemp and update the pool balances, weights, spot prices\n", |
209 | | - "2. Update external USD prices\n", |
210 | | - "3. Store action datetime\n", |
| 175 | + "Partial State Update Blocks combine the following steps: \n", |
| 176 | + "1. **Initialize pool**, generate genesis state\n", |
| 177 | + "2. Compute subsequent **actions in discrete timesteps**, store datetime and update the pool state variables\n", |
| 178 | + "3. Update external **USD prices in discrete timesteps**, and store datetime \n", |
211 | 179 | "\n", |
212 | | - "For more information please visit the [Balancer Simulations documentation/V1.0 Model Overview](https://app.gitbook.com/@token-engineering-balancer/s/balancer-simulation-portal/v/master/balancer-simulations/nboverview)." |
| 180 | + "For more information please visit the [Balancer Simulations documentation/V1.0 Model Overview](https://token-engineering-balancer.gitbook.io/balancer-simulations/v/master/balancer-simulations/v10nboverview)." |
213 | 181 | ] |
214 | 182 | }, |
215 | 183 | { |
|
231 | 199 | "source": [ |
232 | 200 | "### B1.5 Configuration\n", |
233 | 201 | "\n", |
234 | | - "A pool's state is updated by\n", |
235 | | - "- **actions** (such as swaps and external price updates) and\n", |
236 | | - "- **price signals** (USD values of tokens)\n", |
237 | | - "\n", |
238 | | - "These updates are captured in unique **timesteps**. 1 Timestep = 1 action (price signal or pool transaction). Each action has a datetime for time related analysis\n", |
239 | | - "\n", |
240 | | - "**steps_number** is automatically calculated from action json\n", |
241 | | - "\n", |
242 | | - "In most cases you might want to run the simulation across all timesteps included in your .json file - however you can specify any simulation range below.\n", |
243 | | - "\n", |
244 | | - "### Note: You need to start from the pool creation state, as seen in 0x123..._initial_pool_states.json " |
| 202 | + "[cadCAD simulations](https://github.com/cadCAD-org/cadCAD/blob/master/documentation/README.md) support Monte Carlo runs, and parameter sweeps which are not applied in this notebook and can be ignored here. \n", |
| 203 | + "Timesteps 'T' are defined by your pool's .json file, no need to make edits here." |
245 | 204 | ] |
246 | 205 | }, |
247 | 206 | { |
248 | 207 | "cell_type": "code", |
249 | | - "execution_count": null, |
250 | | - "metadata": {}, |
251 | | - "outputs": [], |
| 208 | + "execution_count": 2, |
| 209 | + "metadata": {}, |
| 210 | + "outputs": [ |
| 211 | + { |
| 212 | + "ename": "NameError", |
| 213 | + "evalue": "name 'result' is not defined", |
| 214 | + "output_type": "error", |
| 215 | + "traceback": [ |
| 216 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 217 | + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", |
| 218 | + "\u001b[0;32m<ipython-input-2-825212656cea>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msteps_number\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'steps_number'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'# Steps '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msteps_number\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m sim_config = config_sim(\n\u001b[1;32m 4\u001b[0m {\n\u001b[1;32m 5\u001b[0m \u001b[0;34m'N'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;31m# number of monte carlo runs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 219 | + "\u001b[0;31mNameError\u001b[0m: name 'result' is not defined" |
| 220 | + ] |
| 221 | + } |
| 222 | + ], |
252 | 223 | "source": [ |
253 | 224 | "steps_number = result['steps_number']\n", |
254 | 225 | "print('# Steps ', steps_number)\n", |
|
285 | 256 | "source": [ |
286 | 257 | "### B1.7 Simulation Output Preparation\n", |
287 | 258 | "\n", |
288 | | - "Post-processing (utils.py) separate complex variables (like pool and token prices) into independent variables, and adds metrics to the data frame, such as \n", |
289 | | - "- `token_k_values` \n", |
290 | | - "based on\n", |
| 259 | + "Post-processing (utils.py) splits up state variable dictionaries, and adds metrics to the data frame, such as \n", |
| 260 | + "- `token_total_value` (TVL)\n", |
| 261 | + "- `generated_fees_(tokensymbol)` (fee collected per transaction)\n", |
| 262 | + "- `token_k_values` (token value in USD) \n", |
| 263 | + "based on \n", |
291 | 264 | "- `token_k_balances`\n", |
292 | 265 | "- `token_k_price` \n", |
293 | | - "and calculates accumulated values, such as\n", |
294 | | - "- `token_total_value` \n" |
| 266 | + "\n" |
295 | 267 | ] |
296 | 268 | }, |
297 | 269 | { |
|
318 | 290 | "Below we show a range of plots exploring pool states in the simulation:\n", |
319 | 291 | "\n", |
320 | 292 | "**a) Pool Power:** \n", |
321 | | - "- D1.1 TVL (Total Value Locked, over time) compared to \n", |
322 | | - "- D1.1 Pool Size Growth (Number of tokens in the pool, over time) \n", |
323 | | - "- D1.2 Token Balances (individual balances, over time)\n", |
| 293 | + "- C1.1 TVL (Total Value Locked, over time) compared to \n", |
| 294 | + "- C1.1 Pool Size Growth (Number of tokens in the pool, over time) \n", |
| 295 | + "- C1.2 Token Balances (individual balances, over time)\n", |
324 | 296 | "\n", |
325 | 297 | "**b) Pool Characteristics:** \n", |
326 | | - "- D1.3 Source of Pool Growth (Total Growth vs. Fees collected)\n", |
327 | | - "- D1.4 Token Ratio\n", |
328 | | - "- D1.5 Action Types" |
| 298 | + "- C1.3 Source of Pool Growth (Total Growth vs. Fees collected)\n", |
| 299 | + "- C1.4 Token Ratio\n", |
| 300 | + "- C1.5 Action Types" |
329 | 301 | ] |
330 | 302 | }, |
| 303 | + { |
| 304 | + "cell_type": "code", |
| 305 | + "execution_count": null, |
| 306 | + "metadata": {}, |
| 307 | + "outputs": [], |
| 308 | + "source": [] |
| 309 | + }, |
331 | 310 | { |
332 | 311 | "cell_type": "code", |
333 | 312 | "execution_count": null, |
|
622 | 601 | "name": "python", |
623 | 602 | "nbconvert_exporter": "python", |
624 | 603 | "pygments_lexer": "ipython3", |
625 | | - "version": "3.8.5" |
| 604 | + "version": "3.9.1" |
626 | 605 | } |
627 | 606 | }, |
628 | 607 | "nbformat": 4, |
|
0 commit comments