Skip to content

Commit 3c43c92

Browse files
committed
NB modifications and VERBOSE mode to show warnings
1 parent 611278a commit 3c43c92

2 files changed

Lines changed: 34 additions & 22 deletions

File tree

NB1-BalancerAMM-V1.0.ipynb

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"### System Specification\n",
3131
"System specification details are available in the Balancer Simulations documentation:\n",
3232
"- [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/balancer_math.py)\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",
3434
"- [Model Architecture](https://app.gitbook.com/@token-engineering-balancer/s/balancer-simulation-portal/v/master/balancer-simulations/nboverview)\n",
3535
"- [Naming Convention](https://app.gitbook.com/@token-engineering-balancer/s/balancer-simulation-portal/v/master/additional-code-and-instructions/naming-convention)"
3636
]
@@ -85,10 +85,12 @@
8585
"pp = pprint.PrettyPrinter(indent=4)\n",
8686
"\n",
8787
"from decimal import Decimal\n",
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",
8891
"parameters = {\n",
8992
" 'spot_price_reference': ['DAI'],\n",
90-
"}\n",
91-
"pp.pprint(parameters)"
93+
"}\n"
9294
]
9395
},
9496
{
@@ -99,8 +101,7 @@
99101
"\n",
100102
"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",
101103
"\n",
102-
"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",
103-
"(XX - remove spot price reference)"
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"
104105
]
105106
},
106107
{
@@ -163,6 +164,8 @@
163164
"\n",
164165
"cadCAD state update functions replicate the following Balancer Pool Transactions (see system policies.py) \n",
165166
"\n",
167+
"\n",
168+
"\n",
166169
"**a) Add Liquidity**\n",
167170
"= join policy \n",
168171
"- `p_join_pool` (all-asset deposit)\n",
@@ -172,19 +175,25 @@
172175
"**b) Withdraw Liquidity**\n",
173176
"= exit policy \n",
174177
"- `p_exit_pool` (all-asset withdrawal) \n",
175-
"- `p_exit_swap_extern_amount_out` (single-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",
176179
"\n",
177180
"**C) Swap**\n",
178181
"= swap policy \n",
179182
"- `p_swap_exact_amount_in` (out-given-in)\n",
180183
"\n",
181184
"\n",
182-
"Balancer Pools offer an additional transaction type:\n",
185+
"Balancer Pools offer additional transaction types:\n",
183186
"- `p_swap_exact_amount_out` (in-given-out)\n",
184-
"Which is not applied in Balancer Simulations V1.0\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",
185194
"\n",
186-
"(XX - fees??)\n",
187195
" \n",
196+
"\n",
188197
"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)."
189198
]
190199
},
@@ -195,9 +204,10 @@
195204
"### B1.4 Partial State Update Blocks\n",
196205
"\n",
197206
"Partial State Update Blocks combine the following steps:\n",
198-
"1. Parse actions and update the pool\n",
207+
"0. Skip initial action (pool creation)\n",
208+
"1. Parse actions each timestemp and update the pool balances, weights, spot prices\n",
199209
"2. Update external USD prices\n",
200-
"3. Calculate metrics\n",
210+
"3. Store action datetime\n",
201211
"\n",
202212
"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)."
203213
]
@@ -222,12 +232,16 @@
222232
"### B1.5 Configuration\n",
223233
"\n",
224234
"A pool's state is updated by\n",
225-
"- **actions** (such as swaps) and\n",
235+
"- **actions** (such as swaps and external price updates) and\n",
226236
"- **price signals** (USD values of tokens)\n",
227237
"\n",
228-
"These updates are captured in unique **timesteps**.\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",
229241
"\n",
230-
"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."
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 "
231245
]
232246
},
233247
{
@@ -271,7 +285,7 @@
271285
"source": [
272286
"### B1.7 Simulation Output Preparation\n",
273287
"\n",
274-
"Post-processing (utils.py) adds metrics to the data frame, such as \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",
275289
"- `token_k_values` \n",
276290
"based on\n",
277291
"- `token_k_balances`\n",
@@ -480,9 +494,6 @@
480494
"metadata": {},
481495
"outputs": [],
482496
"source": [
483-
"# TODO: use generated_fees instead of price\n",
484-
"# TODO: loop over tokens in a list\n",
485-
"\n",
486497
"#balance growth (total observation period)\n",
487498
"daib_df = (p_df.iloc[-1][['token_dai_balance']])-(p_df.iloc[0][['token_dai_balance']]).copy()\n",
488499
"daib_df = daib_df.reset_index()\n",
@@ -611,7 +622,7 @@
611622
"name": "python",
612623
"nbconvert_exporter": "python",
613624
"pygments_lexer": "ipython3",
614-
"version": "3.9.1"
625+
"version": "3.8.5"
615626
}
616627
},
617628
"nbformat": 4,

model/parts/system_policies.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pandas as pd
1010

11+
VERBOSE=False
1112

1213
def update_fee(token_symbol: str, fee: Decimal, pool: dict):
1314
generated_fees = pool['generated_fees']
@@ -118,7 +119,7 @@ def p_join_pool(params, step, history, current_state, action):
118119
amount_expected = token['amount']
119120
symbol = token['symbol']
120121
amount = ratio * pool['tokens'][symbol].balance
121-
if amount != amount_expected:
122+
if amount != amount_expected and VERBOSE:
122123
print("WARNING: calculated that user should get {} {} but input specified that he should get {} {} instead".format(amount, symbol,
123124
amount_expected,
124125
symbol))
@@ -152,7 +153,7 @@ def p_join_swap_extern_amount_in(params, step, history, current_state, action):
152153
update_fee(tokens_in_symbol, join_swap.fee, pool)
153154

154155
pool_amount_out = join_swap.result
155-
if pool_amount_out != pool_amount_out_expected:
156+
if pool_amount_out != pool_amount_out_expected and VERBOSE:
156157
print(
157158
"WARNING: calculated that user should get {} pool shares but input specified that he should get {} pool shares instead.".format(
158159
pool_amount_out, pool_amount_out_expected))
@@ -194,7 +195,7 @@ def p_exit_swap_extern_amount_out(params, step, history, current_state, action):
194195

195196
if pool_amount_in == 0:
196197
raise Exception("ERR_MATH_APPROX")
197-
if pool_amount_in != action["pool_amount_in"]:
198+
if pool_amount_in != action["pool_amount_in"] and VERBOSE:
198199
print(
199200
"WARNING: calculated that pool should get {} pool shares but input specified that pool should get {} pool shares instead".format(
200201
pool_amount_in, action["pool_amount_in"]))

0 commit comments

Comments
 (0)