Skip to content

Commit

Permalink
[TRIVIAL] Base: Add support for Balancer (#90)
Browse files Browse the repository at this point in the history
Add support for Balancer chain ID.
  • Loading branch information
m-lord-renkse authored Dec 2, 2024
1 parent c7592d0 commit 8ed1470
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
17 changes: 9 additions & 8 deletions src/infra/dex/balancer/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Query<'_> {
order: &dex::Order,
tokens: &auction::Tokens,
slippage: &dex::Slippage,
chain_id: eth::ChainId,
chain: Chain,
contract_address: eth::ContractAddress,
query_batch_swap: bool,
swap_deadline: Option<u64>,
Expand All @@ -72,7 +72,7 @@ impl Query<'_> {
sender: contract_address.0,
slippage_percentage: slippage.as_factor().clone(),
},
chain: Chain::from_domain(chain_id)?,
chain,
query_batch_swap,
swap_amount: HumanReadableAmount::from_u256(&order.amount.get(), token_decimals),
swap_type: SwapType::from_domain(order.side),
Expand Down Expand Up @@ -166,9 +166,9 @@ struct CallDataInput {
}

/// Balancer SOR API supported chains.
#[derive(Serialize)]
#[derive(Serialize, Clone, Copy)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
enum Chain {
pub(super) enum Chain {
Arbitrum,
Avalanche,
Base,
Expand All @@ -184,12 +184,13 @@ enum Chain {
}

impl Chain {
fn from_domain(chain_id: eth::ChainId) -> Result<Self, Error> {
pub(crate) fn from_domain(chain_id: eth::ChainId) -> Result<Self, Error> {
match chain_id {
eth::ChainId::Mainnet => Ok(Self::Mainnet),
eth::ChainId::Gnosis => Ok(Self::Gnosis),
eth::ChainId::ArbitrumOne => Ok(Self::Arbitrum),
unsupported => Err(Error::UnsupportedChainId(unsupported)),
eth::ChainId::Base => Ok(Self::Base),
eth::ChainId::Goerli => Err(Error::UnsupportedChainId(chain_id)),
}
}
}
Expand Down Expand Up @@ -385,15 +386,15 @@ mod tests {
owner: H160::from_str("0x9008d19f58aabd9ed0d60971565aa8510560ab41").unwrap(),
};
let slippage = dex::Slippage::one_percent();
let chain_id = eth::ChainId::Mainnet;
let chain = Chain::Mainnet;
let contract_address = eth::ContractAddress(
H160::from_str("0x9008d19f58aabd9ed0d60971565aa8510560ab41").unwrap(),
);
let query = Query::from_domain(
&order,
&tokens,
&slippage,
chain_id,
chain,
contract_address,
false,
Some(12345_u64),
Expand Down
11 changes: 6 additions & 5 deletions src/infra/dex/balancer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use {
eth::{self, TokenAddress},
order,
},
infra::dex::balancer::dto::Chain,
util,
},
contracts::ethcontract::I256,
Expand All @@ -29,7 +30,7 @@ pub struct Sor {
endpoint: reqwest::Url,
vault: vault::Vault,
settlement: eth::ContractAddress,
chain_id: eth::ChainId,
chain_id: Chain,
query_batch_swap: bool,
}

Expand Down Expand Up @@ -61,15 +62,15 @@ impl Sor {
/// lost to time... See <https://github.com/cowprotocol/services/pull/171>.
const GAS_PER_SWAP: u64 = 88_892;

pub fn new(config: Config) -> Self {
Self {
pub fn new(config: Config) -> Result<Self, Error> {
Ok(Self {
client: super::Client::new(Default::default(), config.block_stream),
endpoint: config.endpoint,
vault: vault::Vault::new(config.vault),
settlement: config.settlement,
chain_id: config.chain_id,
chain_id: Chain::from_domain(config.chain_id)?,
query_batch_swap: config.query_batch_swap,
}
})
}

pub async fn swap(
Expand Down
4 changes: 3 additions & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ async fn run_with(args: cli::Args, bind: Option<oneshot::Sender<SocketAddr>>) {
cli::Command::Balancer { config } => {
let config = config::dex::balancer::file::load(&config).await;
Solver::Dex(solver::Dex::new(
dex::Dex::Balancer(dex::balancer::Sor::new(config.sor)),
dex::Dex::Balancer(
dex::balancer::Sor::new(config.sor).expect("invalid Balancer configuration"),
),
config.base.clone(),
))
}
Expand Down

0 comments on commit 8ed1470

Please sign in to comment.