balancer_sdk 0.1.16-alpha

A Rust SDK which provides commonly used utilties for interacting with Balancer Protocol V2
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! Defines the vault struct and its methods.
//!
//! # What is the Vault?
//! It is a single contract that holds and manages all the assets added by all Balancer pools. This allows Balancer to take full advantage of its multi-pool trading routing in order to offer the best trade routing options.
//! ## How does the Vault work?
//! Balancer V2 separates the Automated Market Maker (AMM) logic from the token management and accounting. Token management/accounting is done by the Vault while the AMM logic is individual to each pool.
//! Because pools are contracts external to the Vault, they can implement any arbitrary, customized AMM logic.
//!
//! # Basic Usage
//!
//! ## Create instance
//! ```rust
//! use balancer_sdk::vault::Vault;
//!
//! const RPC_URL: &str = balancer_sdk::constants::rpc_endpoints::KOVAN_TESTNET;
//! let transport = ethcontract::web3::transports::Http::new(RPC_URL).unwrap();
//! let web3 = ethcontract::Web3::new(transport);
//!
//! let vault_instance = Vault::new(web3);
//! ```
//!
//! ## Domain specific structs, enums, macros
//! Some of the examples below use "helper" structs, enums, macros, etc. from this crate taken from the Balancer domain.
//! Here are a few for easy reference:
//!
//! - [`addr!` macro](crate::addr)
//! - [`pool_id!` macro](crate::pool_id)
//! - [`BatchSwapStep`](crate::BatchSwapStep)
//! - [`PoolId`](crate::PoolId)
//! - [`JoinPoolRequest`](crate::JoinPoolRequest)
//! - [`UserData`](crate::UserData)
//!
//!
//! # Vault Methods
//! [See Balancer's Vault API documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#getauthorizer)
//!
//! ## Authorization
//! #### get_authorizer()
//! [See interface](struct.Vault.html#method.get_authorizer)
//!
//! Returns the Vault's Authorizer (Balancer governance contract).
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#getauthorizer)
//!
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! Vault::new(web3).get_authorizer().call().await.unwrap()
//! # });
//! ```
//! #### set_authorizer()
//! [See interface](struct.Vault.html#method.set_authorizer)
//!
//! Sets a new Authorizer for the Vault. The caller must be allowed by the current Authorizer to do this.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#setauthorizer)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let authorizer_address = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! Vault::new(web3).set_authorizer(authorizer_address).call().await.unwrap()
//! # });
//! ```
//! #### has_approved_relayer()
//! [See interface](struct.Vault.html#method.has_approved_relayer)
//!
//! Returns true if `user` has allowed relayer to act as a `relayer` for them.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#hasapprovedrelayer)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let some_user_address = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let some_relayer_address = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! Vault::new(web3).has_approved_relayer(some_user_address, some_relayer_address).call().await.unwrap()
//! # });
//! ```
//! #### set_relayer_approval()
//! [See interface](struct.Vault.html#method.set_relayer_approval)
//!
//! Grants or revokes approval for the given `relayer` to call Authorizer-approved functions on behalf of `user`.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#hasapprovedrelayer)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let some_sender_address = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let some_relayer_address = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let is_approved = true;
//! Vault::new(web3).set_relayer_approval(some_sender_address, some_relayer_address, is_approved).call().await.unwrap()
//! # });
//! ```
//!
//! ## Internal Balances
//! #### get_internal_balances()
//! [See interface](struct.Vault.html#method.get_internal_balances)
//!
//! Get a user's internal balances. This is called UserBalance in external interfaces, and "internal balance" in the internal functions.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#getinternalbalances)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let some_user_address = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let token_address_1 = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let token_address_2 = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let tokens = vec![token_address_1, token_address_2];
//! Vault::new(web3).get_internal_balance(some_user_address, tokens).call().await.unwrap()
//! # });
//! ```
//! #### manage_user_balance()
//! [See interface](struct.Vault.html#method.manage_user_balance)
//!
//! There are four possible operations in manageUserBalance: each designates a sender/receiver, asset, and amount. The asset is either a token address or the zero address (meaning ETH). The Vault does not store ETH, but you can use ETH when interacting with internal balances; the Vault will do any necessary wrapping/unwrapping.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#manageuserbalance)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let some_user_address = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let token_address_1 = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let token_address_2 = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let tokens = vec![token_address_1, token_address_2];
//! Vault::new(web3).get_internal_balance(some_user_address, tokens).call().await.unwrap()
//! # });
//! ```
//!
//! ## Pools
//! #### register_pool()
//! [See interface](struct.Vault.html#method.register_pool)
//!
//! Called from the pool contract to generate a  Pool ID, and enter it in the Vault's pool data structures.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#registerpool)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let pool_id = PoolId::from_str("0xBA12222222228d8Ba445958a75a0704d566BF2C8").unwrap();
//! let specialization = PoolSpecialization::General;
//! Vault::new(web3).register_pool(specialization as u8).call().await.unwrap()
//! # });
//! ```
//!
//! #### get_pool()
//! [See interface](struct.Vault.html#method.get_pool)
//!
//! Returns a Pool's contract address and specialization setting.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#getpool)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! # use balancer_sdk::helpers::*;
//! use balancer_sdk::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let pool_id: PoolId = "0x0371c272fdd28ac13c434f1ef6b8b52ea3e6d844".parse().unwrap();
//!
//! Vault::new(web3).get_pool(pool_id.into()).call().await.unwrap()
//! # });
//! ```
//!
//! #### register_tokens()
//! [See interface](struct.Vault.html#method.register_tokens)
//!
//! Called from the pool contract to tell the Vault which tokens are valid for this pool (i.e., which can be used to swap, join, or exit). An asset manager can also be assigned to each token at this step, which is thereafter immutable (unless you deregister and register again).
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#registertokens)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! # use balancer_sdk::helpers::*;
//! use balancer_sdk::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let pool_id: PoolId = "0x0371c272fdd28ac13c434f1ef6b8b52ea3e6d844".parse().unwrap();
//! let token_address_1 = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let token_address_2 = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let tokens = vec![token_address_1, token_address_2];
//! let manager_address_1 = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let manager_address_2 = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let asset_managers = vec![manager_address_1, manager_address_2];
//!
//! Vault::new(web3).register_tokens(pool_id.into(), tokens, asset_managers).call().await.unwrap()
//! # });
//! ```
//!
//! #### deregister_tokens()
//! [See interface](struct.Vault.html#method.deregister_tokens)
//!
//! Remove tokens from the pool (must have zero balance).
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#deregistertokens)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! # use balancer_sdk::helpers::*;
//! use balancer_sdk::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let pool_id: PoolId = "0x0371c272fdd28ac13c434f1ef6b8b52ea3e6d844".parse().unwrap();
//! let token_address_1 = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let token_address_2 = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let tokens = vec![token_address_1, token_address_2];
//!
//! Vault::new(web3).deregister_tokens(pool_id.into(), tokens).call().await.unwrap()
//! # });
//! ```
//!
//! #### get_pool_token_info()
//! [See interface](struct.Vault.html#method.get_pool_token_info)
//!
//! Return details of a particular token. While getPoolTokens gives the total balance, getPoolTokenInfo returns each component of the balance, as well as the time (block) it was last modified, and the asset manager.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#getpooltokeninfo)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! # use balancer_sdk::helpers::*;
//! use balancer_sdk::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let pool_id: PoolId = "0x0371c272fdd28ac13c434f1ef6b8b52ea3e6d844".parse().unwrap();
//! let token_address = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//!
//! Vault::new(web3).get_pool_token_info(pool_id.into(), token_address).call().await.unwrap()
//! # });
//! ```
//!
//! #### get_pool_tokens()
//! [See interface](struct.Vault.html#method.get_pool_tokens)
//!
//! Returns a Pool's registered tokens, the total balance for each, and the most recent block in which any of the tokens were updated. Implemented by PoolAssets.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#getpooltokens)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! # use balancer_sdk::helpers::*;
//! use balancer_sdk::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let pool_id: PoolId = "0x0371c272fdd28ac13c434f1ef6b8b52ea3e6d844".parse().unwrap();
//!
//! Vault::new(web3).get_pool_tokens(pool_id.into()).call().await.unwrap()
//! # });
//! ```
//!
//! ## Joins and Exits
//!
//! #### join_pool()
//! [See interface](struct.Vault.html#method.join_pool)
//!
//! [See Balancer Pool Joins documentation](https://dev.balancer.fi/resources/joins-and-exits/pool-joins)
//!
//! [See Balancer method documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#joinpool)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let pool_id = PoolId::from_str("0xBA12222222228d8Ba445958a75a0704d566BF2C8").unwrap();
//! let sender_address = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let recipient_address = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let join_pool_request = JoinPoolRequest {
//!     assets: vec![addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8"), addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8")],
//!     max_amounts_in: vec![u256!("100"), u256!("200")],
//!     user_data: UserData("0x"),
//!     from_internal_balance: false
//! };
//!
//! Vault::new(web3).join_pool(pool_id.into(), sender_address, recipient_address, join_pool_request.into()).call().await.unwrap()
//! # });
//! ```
//!
//! #### exit_pool()
//! [See interface](struct.Vault.html#method.exit_pool)
//!
//! [See Balancer method documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#exitpool)
//!
//! [See Balancer Pool Exits documentation](https://dev.balancer.fi/resources/joins-and-exits/pool-exits)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let pool_id = PoolId::from_str("0xBA12222222228d8Ba445958a75a0704d566BF2C8").unwrap();
//! let sender_address = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let recipient_address = addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8");
//! let exit_pool_request = ExitPoolRequest {
//!     assets: vec![addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8"), addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8")],
//!     max_amounts_out: vec![u256!("100"), u256!("200")],
//!     user_data: UserData("0x"),
//!     to_internal_balance: false
//! };
//!
//! Vault::new(web3).join_pool(pool_id.into(), sender_address, recipient_address, exit_pool_request.into()).call().await.unwrap()
//! # });
//! ```
//!
//! ## Single Swaps
//! [See Balancer Single Swaps documentation](https://dev.balancer.fi/guides/swaps/single-swaps)
//!
//! #### swap()
//! [See interface](struct.Vault.html#method.swap)
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#swap)
//!
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//!
//! let swap_step = SingleSwap {
//!     pool_id: PoolId::from_str("0xBA12222222228d8Ba445958a75a0704d566BF2C8").unwrap(),
//!     kind: SwapKind::GivenIn,
//!     asset_in: addr!("0x1f9840a85d5af5bf1d1762f925bdaddc4201f984"),
//!     asset_out: addr!("0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9"),
//!     amount: u256!("10"),
//!     user_data: UserData("0x").into(),
//! };
//!
//! let funds = FundManagement {
//!     sender: addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8"),
//!     from_internal_balance: false,
//!     recipient: addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8"),
//!     to_internal_balance: false,
//! };
//!
//! let limit = u256!("9125892514880");
//! let deadline = u256!("999999999999999999");
//! // Fake Private Key
//! let private_key = PrivateKey::from_str("00e0000a00aaaa0e0a000e0e0000e00e000a000000000000000aaa00a0aaaaaa").unwrap();
//!
//! let result = Vault::new(web3)
//!     .swap(swap_step.clone().into(), funds.into(), limit, deadline)
//!     .from(Account::Offline(private_key, Some(42)))
//!     .gas(u256!("4712388"))
//!     .gas_price(u256!("100000000000").into())
//!     .send()
//!     .await
//!     .unwrap();
//! # });
//! ```
//! ## Batch Swaps
//! [See Balancer Batch Swaps documentation](https://dev.balancer.fi/guides/swaps/batch-swaps)
//!
//! #### batch_swap()
//! [See interface](struct.Vault.html#method.batch_swap)
//!
//! Batch swap "steps" specify the assets involved, "many-to-many" sources and destinations, and min/max token limits to guard against slippage. There is also an optional deadline, after which the swap will timeout and revert. These return the token "deltas" - the net result of executing each swap sequentially.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#batch-swaps)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let assets = vec![
//!     addr!("0x1f9840a85d5af5bf1d1762f925bdaddc4201f984"),
//!     addr!("0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9"),
//! ];
//!
//! let limits = vec![i256!("1000000000000000000"), i256!("1000000000000000000")];
//!
//! let swap_step = BatchSwapStep {
//!     pool_id: pool_id!("0x0371c272fdd28ac13c434f1ef6b8b52ea3e6d844"),
//!     asset_in_index: 0,
//!     asset_out_index: 1,
//!     amount: u256!("10"),
//!     user_data: UserData("0x"),
//! };
//!
//! let funds = FundManagement {
//!     sender: addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8"),
//!     from_internal_balance: false,
//!     recipient: addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8"),
//!     to_internal_balance: false,
//! };
//!
//! let private_key = PrivateKey::from_str("00e0000a00aaaa0e0a000e0e0000e00e000a000000000000000aaa00a0aaaaaa").unwrap();
//!
//! let result = Vault::new(web3)
//!     .batch_swap(
//!         SwapKind::GivenIn as u8,
//!         vec![swap_step.into()],
//!         assets,
//!         funds.into(),
//!         limits,
//!         // Infinity
//!         u256!("999999999999999999"),
//!     )
//!     .from(Account::Offline(private_key, None))
//!     .gas(u256!("4712388"))
//!     .gas_price(u256!("100000000000").into())
//!     .send()
//!     .await
//!     .unwrap();
//! # });
//! ```
//!
//! #### query_batch_swap()
//! [See interface](struct.Vault.html#method.query_batch_swap)
//!
//! The queryBatchSwap method executes the exact same code as batchSwap - but reverts at the end. This is for GUIs or scripts to calculate a "dry run" of a sequence of swaps. Implemented in Swaps.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#batch-swaps)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let assets = vec![
//!     addr!("0x1f9840a85d5af5bf1d1762f925bdaddc4201f984"),
//!     addr!("0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9"),
//! ];
//!
//! let swap_step = BatchSwapStep {
//!     pool_id: pool_id!("0x0371c272fdd28ac13c434f1ef6b8b52ea3e6d844"),
//!     asset_in_index: 0,
//!     asset_out_index: 1,
//!     amount: u256!("10"),
//!     user_data: UserData("0x"),
//! };
//!
//! let funds = FundManagement {
//!     sender: addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8"),
//!     from_internal_balance: false,
//!     recipient: addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8"),
//!     to_internal_balance: false,
//! };
//!
//! let result = Vault::new(web3)
//!     .query_batch_swap(
//!         SwapKind::GivenIn as u8,
//!         vec![swap_step.into()],
//!         assets,
//!         funds.into(),
//!     )
//!     .send()
//!     .await
//!     .unwrap();
//! # });
//! ```
//!
//! ## Flash Loans
//!
//! #### flash_loan()
//! [See interface](struct.Vault.html#method.flash_loan)
//!
//! Execute a flash loan. This sends the given token amounts to the flash loan receiver contract; all borrowed funds - plus the protocol flash loan fee - must be returned to the vault in the same transaction, or it will revert. Implemented by a FlashLoans subclass. Implemented in FlashLoans.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#flashloan)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let tokens = vec![
//!     addr!("0x1f9840a85d5af5bf1d1762f925bdaddc4201f984"),
//!     addr!("0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9"),
//! ];
//!
//! let private_key = PrivateKey::from_str("00e0000a00aaaa0e0a000e0e0000e00e000a000000000000000aaa00a0aaaaaa").unwrap();
//!
//! let result = Vault::new(web3)
//!     .flash_loan(
//!         // Recipient address
//!         addr!("0xBA12222222228d8Ba445958a75a0704d566BF2C8"),
//!         tokens,
//!         vec![u256!("1000000000"), u256!("100000000")],
//!         UserData("0x").into(),
//!     )
//!     .from(Account::Offline(private_key, None))
//!     .gas(u256!("4712388"))
//!     .gas_price(u256!("100000000000").into())
//!     .send()
//!     .await
//!     .unwrap();
//! # });
//! ```
//!
//! ## Asset Management
//! This can only be called by the asset manager of a token in a pool.
//!
//! #### manage_pool_balance()
//! [See interface](struct.Vault.html#method.manage_pool_balance)
//!
//! Deposit or withdraw funds from the pool (i.e., move funds between cash and managed balances), or update the total balance (i.e., reporting a gain or loss from management activities). Implemented in AssetManagers. Each PoolBalanceOp describes the type of operation (deposit/withdraw/update), the pool ID, the token, and the amount.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#managepoolbalance)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//! let pool_balance_op = PoolBalanceOp {
//!     kind: PoolBalanceOpKind::Deposit,
//!     pool_id: pool_id!("0x0371c272fdd28ac13c434f1ef6b8b52ea3e6d844"),
//!     token: addr!("0x1f9840a85d5af5bf1d1762f925bdaddc4201f984"),
//!     amount: u256!("1000000000")
//! };
//!
//! let private_key = PrivateKey::from_str("00e0000a00aaaa0e0a000e0e0000e00e000a000000000000000aaa00a0aaaaaa").unwrap();
//!
//! let result = Vault::new(web3)
//!     .manage_pool_balance(vec![pool_balance_op.into()])
//!     .from(Account::Offline(private_key, None))
//!     .send()
//!     .await
//!     .unwrap();
//! # });
//! ```
//! ## Miscellaneous
//!
//! #### get_protocol_fees_collector()
//! [See interface](struct.Vault.html#method.get_protocol_fees_collector)
//!
//! The external contract authorized to collect protocol fees. Implemented by Fees.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#getProtocolFeesCollector)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//!
//! let result = Vault::new(web3)
//!     .get_protocol_fees_collector()
//!     .call()
//!     .await
//!     .unwrap();
//! # });
//! ```
//! #### set_paused()
//! [See interface](struct.Vault.html#method.set_paused)
//!
//! Safety mechanism to halt most Vault operations in the event of an emergency. The only functions allowed involve withdrawing funds (e.g., from internal balances, or proportional pool exits). Implemented by Vault.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#setPaused)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//!
//! let private_key = PrivateKey::from_str("00e0000a00aaaa0e0a000e0e0000e00e000a000000000000000aaa00a0aaaaaa").unwrap();
//!
//! let result = Vault::new(web3)
//!     .set_paused(true)
//!     .from(Account::Offline(private_key, None))
//!     .send()
//!     .await
//!     .unwrap();
//! # });
//! ```
//! #### weth()
//! [See interface](struct.Vault.html#method.weth)
//!
//! The Vault's address for WETH. Implemented by Vault.
//!
//! [See Balancer documentation](https://dev.balancer.fi/references/contracts/apis/the-vault#weth)
//! ```no_run
//! use balancer_sdk::vault::Vault;
//! use balancer_sdk::*;
//! # use balancer_sdk::helpers::*;
//!
//! # tokio_test::block_on(async {
//! # let web3 = build_web3(&get_env_var("RPC_URL"));
//!
//! let result = Vault::new(web3)
//!     .weth()
//!     .call()
//!     .await
//!     .unwrap();
//! # });
//! ```

pub use super::generated_contracts::vault::Vault;
use crate::Address;
use std::str::FromStr;

/// This should always be the same address across chains
pub const VAULT_CONTRACT_ADDRESS: &str = "0xBA12222222228d8Ba445958a75a0704d566BF2C8";

impl Vault {
    pub fn new(web3: ethcontract::Web3<ethcontract::web3::transports::Http>) -> Self {
        let vault_address = super::addr!(VAULT_CONTRACT_ADDRESS);
        Vault::at(&web3, vault_address)
    }
}