Skip to content

Commit 881e0c2

Browse files
committed
Update NB1-BalancerAMM-V1.0.ipynb
- text/instructions - all links checked
1 parent 3c43c92 commit 881e0c2

1 file changed

Lines changed: 60 additions & 81 deletions

File tree

NB1-BalancerAMM-V1.0.ipynb

Lines changed: 60 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"\n",
99
"### Pool address: 0x8b6e6E7B5b3801FEd2CaFD4b22b8A16c2F2Db21a -- 80% WETH - 20% DAI \n",
1010
"\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",
1212
"\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",
1515
"\n",
1616
"For more information check out the [Balancer Simulations documentation](https://token-engineering-balancer.gitbook.io/balancer-simulations/)."
1717
]
@@ -27,12 +27,12 @@
2727
"cell_type": "markdown",
2828
"metadata": {},
2929
"source": [
30-
"### System Specification\n",
30+
"### A1.1 System Specification\n",
3131
"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)"
3636
]
3737
},
3838
{
@@ -70,14 +70,14 @@
7070
"cell_type": "markdown",
7171
"metadata": {},
7272
"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). "
7676
]
7777
},
7878
{
7979
"cell_type": "code",
80-
"execution_count": null,
80+
"execution_count": 1,
8181
"metadata": {},
8282
"outputs": [],
8383
"source": [
@@ -86,8 +86,7 @@
8686
"\n",
8787
"from decimal import Decimal\n",
8888
"\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",
9190
"parameters = {\n",
9291
" 'spot_price_reference': ['DAI'],\n",
9392
"}\n"
@@ -99,9 +98,10 @@
9998
"source": [
10099
"#### Import Pool Transactions and Initialize Pool state\n",
101100
"\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",
103103
"\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"
105105
]
106106
},
107107
{
@@ -162,39 +162,8 @@
162162
"source": [
163163
"### B1.3 State Update Functions & Policies\n",
164164
"\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)."
198167
]
199168
},
200169
{
@@ -203,13 +172,12 @@
203172
"source": [
204173
"### B1.4 Partial State Update Blocks\n",
205174
"\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",
211179
"\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)."
213181
]
214182
},
215183
{
@@ -231,24 +199,27 @@
231199
"source": [
232200
"### B1.5 Configuration\n",
233201
"\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."
245204
]
246205
},
247206
{
248207
"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+
],
252223
"source": [
253224
"steps_number = result['steps_number']\n",
254225
"print('# Steps ', steps_number)\n",
@@ -285,13 +256,14 @@
285256
"source": [
286257
"### B1.7 Simulation Output Preparation\n",
287258
"\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",
291264
"- `token_k_balances`\n",
292265
"- `token_k_price` \n",
293-
"and calculates accumulated values, such as\n",
294-
"- `token_total_value` \n"
266+
"\n"
295267
]
296268
},
297269
{
@@ -318,16 +290,23 @@
318290
"Below we show a range of plots exploring pool states in the simulation:\n",
319291
"\n",
320292
"**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",
324296
"\n",
325297
"**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"
329301
]
330302
},
303+
{
304+
"cell_type": "code",
305+
"execution_count": null,
306+
"metadata": {},
307+
"outputs": [],
308+
"source": []
309+
},
331310
{
332311
"cell_type": "code",
333312
"execution_count": null,
@@ -622,7 +601,7 @@
622601
"name": "python",
623602
"nbconvert_exporter": "python",
624603
"pygments_lexer": "ipython3",
625-
"version": "3.8.5"
604+
"version": "3.9.1"
626605
}
627606
},
628607
"nbformat": 4,

0 commit comments

Comments
 (0)