Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory used for balancer v2 liquidity #2099

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/driver/src/boundary/liquidity/balancer/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn to_interaction(
// change this assumption, we would need to change it there as well.
GPv2Settlement::at(&web3, receiver.0),
BalancerV2Vault::at(&web3, pool.vault.into()),
Arc::new(Allowances::empty(receiver.0)),
Allowances::empty(receiver.0),
);

let interaction = handler.swap(
Expand Down
67 changes: 41 additions & 26 deletions crates/solver/src/liquidity/balancer_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,19 @@ impl BalancerV2Liquidity {
let pools = self.pool_fetcher.fetch(pairs, block).await?;

let tokens = pools.relevant_tokens();
let allowances = Arc::new(
self.allowance_manager
.get_allowances(tokens, self.vault.address())
.await?,
);

let weighted_product_orders = pools
let allowances = self
.allowance_manager
.get_allowances(tokens, self.vault.address())
.await?;

let inner = Arc::new(Inner {
allowances,
settlement: self.settlement.clone(),
vault: self.vault.clone(),
});

let weighted_product_orders: Vec<_> = pools
.weighted_pools
.into_iter()
.map(|pool| WeightedProductOrder {
Expand All @@ -77,13 +83,11 @@ impl BalancerV2Liquidity {
version: pool.version,
settlement_handling: Arc::new(SettlementHandler {
pool_id: pool.common.id,
settlement: self.settlement.clone(),
vault: self.vault.clone(),
allowances: allowances.clone(),
inner: inner.clone(),
}),
})
.collect();
let stable_pool_orders = pools
let stable_pool_orders: Vec<_> = pools
.stable_pools
.into_iter()
.map(|pool| StablePoolOrder {
Expand All @@ -93,9 +97,7 @@ impl BalancerV2Liquidity {
amplification_parameter: pool.amplification_parameter,
settlement_handling: Arc::new(SettlementHandler {
pool_id: pool.common.id,
settlement: self.settlement.clone(),
vault: self.vault.clone(),
allowances: allowances.clone(),
inner: inner.clone(),
}),
})
.collect();
Expand Down Expand Up @@ -125,28 +127,34 @@ impl LiquidityCollecting for BalancerV2Liquidity {

pub struct SettlementHandler {
pool_id: H256,
inner: Arc<Inner>,
}

struct Inner {
settlement: GPv2Settlement,
vault: BalancerV2Vault,
allowances: Arc<Allowances>,
allowances: Allowances,
}

impl SettlementHandler {
pub fn new(
pool_id: H256,
settlement: GPv2Settlement,
vault: BalancerV2Vault,
allowances: Arc<Allowances>,
allowances: Allowances,
) -> Self {
SettlementHandler {
pool_id,
settlement,
vault,
allowances,
inner: Arc::new(Inner {
settlement,
vault,
allowances,
}),
}
}

pub fn vault(&self) -> &BalancerV2Vault {
&self.vault
&self.inner.vault
}

pub fn pool_id(&self) -> H256 {
Expand All @@ -159,8 +167,8 @@ impl SettlementHandler {
output: TokenAmount,
) -> BalancerSwapGivenOutInteraction {
BalancerSwapGivenOutInteraction {
settlement: self.settlement.clone(),
vault: self.vault.clone(),
settlement: self.inner.settlement.clone(),
vault: self.inner.vault.clone(),
pool_id: self.pool_id,
asset_in_max: input_max,
asset_out: output,
Expand Down Expand Up @@ -198,7 +206,11 @@ impl SettlementHandler {
execution: AmmOrderExecution,
encoder: &mut SettlementEncoder,
) -> Result<()> {
if let Some(approval) = self.allowances.approve_token(execution.input_max.clone())? {
if let Some(approval) = self
.inner
.allowances
.approve_token(execution.input_max.clone())?
{
encoder.append_to_execution_plan_internalizable(
Arc::new(approval),
execution.internalizable,
Expand Down Expand Up @@ -436,17 +448,20 @@ mod tests {
#[test]
fn encodes_swaps_in_settlement() {
let (settlement, vault) = dummy_contracts();
let handler = SettlementHandler {
pool_id: H256([0x90; 32]),
let inner = Arc::new(Inner {
settlement: settlement.clone(),
vault: vault.clone(),
allowances: Arc::new(Allowances::new(
allowances: Allowances::new(
vault.address(),
hashmap! {
H160([0x70; 20]) => 0.into(),
H160([0x71; 20]) => 100.into(),
},
)),
),
});
let handler = SettlementHandler {
pool_id: H256([0x90; 32]),
inner,
};

let mut encoder = SettlementEncoder::new(Default::default());
Expand Down
4 changes: 2 additions & 2 deletions crates/solver/src/settlement_simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,10 @@ mod tests {
.unwrap(),
contract.clone(),
balancer_vault,
Arc::new(Allowances::new(
Allowances::new(
contract.address(),
hashmap! {"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48".parse().unwrap()=> U256::from_dec_str("18000000000000000000000000").unwrap()},
)),
),
)),
};

Expand Down
Loading