|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use alloy::{ |
| 4 | + dyn_abi::{DynSolType, DynSolValue}, |
| 5 | + network::Network, |
| 6 | + providers::Provider, |
| 7 | + sol, |
| 8 | + transports::Transport, |
| 9 | +}; |
| 10 | + |
| 11 | +use crate::{ |
| 12 | + amm::{AutomatedMarketMaker, AMM}, |
| 13 | + errors::AMMError, |
| 14 | +}; |
| 15 | + |
| 16 | +use super::BalancerV2Pool; |
| 17 | + |
| 18 | +sol! { |
| 19 | + #[allow(missing_docs)] |
| 20 | + #[sol(rpc)] |
| 21 | + IGetBalancerV2PoolDataBatchRequest, |
| 22 | + "src/amm/balancer_v2/batch_request/GetBalancerV2PoolDataBatchRequest.json" |
| 23 | +} |
| 24 | + |
| 25 | +#[inline] |
| 26 | +fn populate_pool_data_from_tokens(pool: &mut BalancerV2Pool, tokens: &[DynSolValue]) { |
| 27 | + // TODO: Add error handling |
| 28 | + pool.tokens = tokens[0] |
| 29 | + .as_array() |
| 30 | + .expect("Expected array") |
| 31 | + .iter() |
| 32 | + .map(|t| t.as_address().expect("Expected address")) |
| 33 | + .collect(); |
| 34 | + pool.decimals = tokens[1] |
| 35 | + .as_array() |
| 36 | + .expect("Expected array") |
| 37 | + .iter() |
| 38 | + .map(|t| t.as_uint().expect("Expected uint").0.to::<u8>()) |
| 39 | + .collect(); |
| 40 | + pool.liquidity = tokens[2] |
| 41 | + .as_array() |
| 42 | + .expect("Expected array") |
| 43 | + .iter() |
| 44 | + .map(|t| t.as_uint().expect("Expected uint").0) |
| 45 | + .collect(); |
| 46 | + pool.weights = tokens[3] |
| 47 | + .as_array() |
| 48 | + .expect("Expected array") |
| 49 | + .iter() |
| 50 | + .map(|t| t.as_uint().expect("Expected uint").0) |
| 51 | + .collect(); |
| 52 | + pool.fee = tokens[4].as_uint().expect("Expected uint").0.to::<u32>(); |
| 53 | +} |
| 54 | + |
| 55 | +pub async fn get_balancer_v2_pool_data_batch_request<T, N, P>( |
| 56 | + pool: &mut BalancerV2Pool, |
| 57 | + block_number: Option<u64>, |
| 58 | + provider: Arc<P>, |
| 59 | +) -> Result<(), AMMError> |
| 60 | +where |
| 61 | + T: Transport + Clone, |
| 62 | + N: Network, |
| 63 | + P: Provider<T, N>, |
| 64 | +{ |
| 65 | + let deployer = IGetBalancerV2PoolDataBatchRequest::deploy_builder(provider, vec![pool.address]); |
| 66 | + let res = if let Some(block_number) = block_number { |
| 67 | + deployer.block(block_number.into()).call_raw().await? |
| 68 | + } else { |
| 69 | + deployer.call_raw().await? |
| 70 | + }; |
| 71 | + |
| 72 | + let constructor_return = DynSolType::Array(Box::new(DynSolType::Tuple(vec![ |
| 73 | + DynSolType::Array(Box::new(DynSolType::Address)), |
| 74 | + DynSolType::Array(Box::new(DynSolType::Uint(8))), |
| 75 | + DynSolType::Array(Box::new(DynSolType::Uint(256))), |
| 76 | + DynSolType::Array(Box::new(DynSolType::Uint(256))), |
| 77 | + DynSolType::Uint(32), |
| 78 | + ]))); |
| 79 | + |
| 80 | + let return_data_tokens = constructor_return.abi_decode_sequence(&res)?; |
| 81 | + |
| 82 | + if let Some(tokens_arr) = return_data_tokens.as_array() { |
| 83 | + for token in tokens_arr { |
| 84 | + let pool_data = token |
| 85 | + .as_tuple() |
| 86 | + .ok_or(AMMError::BatchRequestError(pool.address))?; |
| 87 | + |
| 88 | + populate_pool_data_from_tokens(pool, pool_data); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + Ok(()) |
| 93 | +} |
| 94 | + |
| 95 | +pub async fn get_amm_data_batch_request<T, N, P>( |
| 96 | + amms: &mut [AMM], |
| 97 | + provider: Arc<P>, |
| 98 | +) -> Result<(), AMMError> |
| 99 | +where |
| 100 | + T: Transport + Clone, |
| 101 | + N: Network, |
| 102 | + P: Provider<T, N>, |
| 103 | +{ |
| 104 | + let deployer = IGetBalancerV2PoolDataBatchRequest::deploy_builder( |
| 105 | + provider, |
| 106 | + amms.iter().map(|amm| amm.address()).collect(), |
| 107 | + ); |
| 108 | + let res = deployer.call_raw().await?; |
| 109 | + |
| 110 | + let constructor_return = DynSolType::Array(Box::new(DynSolType::Tuple(vec![ |
| 111 | + DynSolType::Array(Box::new(DynSolType::Address)), |
| 112 | + DynSolType::Array(Box::new(DynSolType::Uint(8))), |
| 113 | + DynSolType::Array(Box::new(DynSolType::Uint(256))), |
| 114 | + DynSolType::Array(Box::new(DynSolType::Uint(256))), |
| 115 | + DynSolType::Uint(32), |
| 116 | + ]))); |
| 117 | + |
| 118 | + let return_data_tokens = constructor_return.abi_decode_sequence(&res)?; |
| 119 | + |
| 120 | + if let Some(tokens_arr) = return_data_tokens.as_array() { |
| 121 | + for (i, token) in tokens_arr.iter().enumerate() { |
| 122 | + let pool_data = token |
| 123 | + .as_tuple() |
| 124 | + .ok_or(AMMError::BatchRequestError(amms[i].address()))?; |
| 125 | + if let AMM::BalancerV2Pool(pool) = &mut amms[i] { |
| 126 | + populate_pool_data_from_tokens(pool, pool_data); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + Ok(()) |
| 131 | +} |
0 commit comments