Skip to content

Commit f4354b8

Browse files
Merge pull request #283 from propeller-heads/rf1/dc/ENG-4759-request-quote-in-execution
feat: Implement IndicativelyPriced for BebopState
2 parents 63a4c9f + 5f35c8d commit f4354b8

File tree

6 files changed

+85
-27
lines changed

6 files changed

+85
-27
lines changed

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ mini-moka = "0.10"
4848
lazy_static = "1.4.0"
4949

5050
# Tycho dependencies
51-
tycho-common = "0.79.0"
52-
tycho-client = "0.79.0"
51+
tycho-common = "0.80.0"
52+
tycho-client = "0.80.0"
5353

5454
# EVM dependencies
5555
foundry-config = { git = "https://github.com/foundry-rs/foundry", rev = "5a552bb0de7126fa35170fd84532bbd3d40cd348", optional = true }
@@ -97,7 +97,7 @@ unicode-width = "0.1.13"
9797
tracing-appender = "0.2.3"
9898

9999
# tycho execution for quickstart
100-
tycho-execution = "0.109.0"
100+
tycho-execution = "0.112.0"
101101

102102
[features]
103103
default = ["evm", "rfq"]

examples/quickstart/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ fn create_solution(
608608
sell_amount: BigUint,
609609
user_address: Bytes,
610610
expected_amount: BigUint,
611-
) -> Solution {
611+
) -> Solution<'static> {
612612
// Prepare data to encode. First we need to create a swap object
613613
let simple_swap = Swap::new(
614614
component,
@@ -618,6 +618,7 @@ fn create_solution(
618618
// the amount or the total remaining balance.
619619
0f64,
620620
None,
621+
None,
621622
);
622623

623624
// Compute a minimum amount out

src/rfq/protocols/bebop/client.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![allow(dead_code)] // TODO remove this
21
use std::{
32
collections::{HashMap, HashSet},
43
str::FromStr,
@@ -14,7 +13,9 @@ use reqwest::Client;
1413
use tokio_tungstenite::{connect_async_with_config, tungstenite::Message};
1514
use tracing::{error, info, warn};
1615
use tycho_common::{
17-
models::protocol::GetAmountOutParams, simulation::indicatively_priced::SignedQuote, Bytes,
16+
models::{protocol::GetAmountOutParams, Chain},
17+
simulation::indicatively_priced::SignedQuote,
18+
Bytes,
1819
};
1920

2021
use crate::{
@@ -26,11 +27,8 @@ use crate::{
2627
},
2728
tycho_client::feed::synchronizer::{ComponentWithState, Snapshot, StateSyncMessage},
2829
tycho_common::dto::{ProtocolComponent, ResponseProtocolState},
29-
tycho_core::dto::Chain,
3030
};
3131

32-
type BebopPriceMessage = HashMap<String, BebopPriceData>;
33-
3432
fn pair_to_bebop_format(pair: &(String, String)) -> Result<String, RFQError> {
3533
// Checksum addresses to match websocket output
3634
let token0 = Address::from_str(&pair.0)
@@ -59,7 +57,7 @@ fn chain_to_bebop_url(chain: Chain) -> Result<String, RFQError> {
5957
Ok(url)
6058
}
6159

62-
#[derive(Clone)]
60+
#[derive(Clone, Debug)]
6361
pub struct BebopClient {
6462
chain: Chain,
6563
price_ws: String,
@@ -115,7 +113,7 @@ impl BebopClient {
115113
id: component_id.clone(),
116114
protocol_system: "rfq:bebop".to_string(),
117115
protocol_type_name: "bebop_pool".to_string(),
118-
chain: self.chain,
116+
chain: self.chain.into(),
119117
tokens,
120118
contract_ids: vec![], // empty for RFQ
121119
static_attributes: Default::default(),
@@ -512,7 +510,10 @@ mod tests {
512510
.protocol_type_name,
513511
"bebop_pool"
514512
);
515-
assert_eq!(component_with_state.component.chain, Chain::Ethereum);
513+
assert_eq!(
514+
component_with_state.component.chain,
515+
Chain::Ethereum.into()
516+
);
516517

517518
let attributes = &component_with_state.state.attributes;
518519

src/rfq/protocols/bebop/state.rs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
use std::{any::Any, collections::HashMap};
22

3+
use async_trait::async_trait;
34
use num_bigint::BigUint;
45
use num_traits::{FromPrimitive, Pow, ToPrimitive};
56
use tycho_common::{
67
dto::ProtocolStateDelta,
7-
models::token::Token,
8+
models::{protocol::GetAmountOutParams, token::Token},
89
simulation::{
910
errors::{SimulationError, TransitionError},
11+
indicatively_priced::{IndicativelyPriced, SignedQuote},
1012
protocol_sim::{Balances, GetAmountOutResult, ProtocolSim},
1113
},
1214
Bytes,
1315
};
1416

15-
use crate::rfq::protocols::bebop::models::BebopPriceData;
17+
use crate::rfq::{
18+
client::RFQClient,
19+
protocols::bebop::{client::BebopClient, models::BebopPriceData},
20+
};
1621

1722
#[derive(Debug, Clone)]
1823
pub struct BebopState {
1924
pub base_token: Token,
2025
pub quote_token: Token,
2126
pub price_data: BebopPriceData,
27+
pub client: BebopClient,
2228
}
2329

2430
impl BebopState {
25-
pub fn new(base_token: Token, quote_token: Token, price_data: BebopPriceData) -> Self {
26-
BebopState { base_token, quote_token, price_data }
31+
pub fn new(
32+
base_token: Token,
33+
quote_token: Token,
34+
price_data: BebopPriceData,
35+
client: BebopClient,
36+
) -> Self {
37+
BebopState { base_token, quote_token, price_data, client }
2738
}
2839
}
2940

@@ -209,11 +220,28 @@ impl ProtocolSim for BebopState {
209220
false
210221
}
211222
}
223+
224+
fn as_indicatively_priced(&self) -> Option<&dyn IndicativelyPriced> {
225+
Some(self)
226+
}
227+
}
228+
229+
#[async_trait]
230+
impl IndicativelyPriced for BebopState {
231+
async fn request_signed_quote(
232+
&self,
233+
params: GetAmountOutParams,
234+
) -> Result<SignedQuote, SimulationError> {
235+
Ok(self
236+
.client
237+
.request_binding_quote(&params)
238+
.await?)
239+
}
212240
}
213241

214242
#[cfg(test)]
215243
mod tests {
216-
use std::str::FromStr;
244+
use std::{collections::HashSet, str::FromStr};
217245

218246
use tycho_common::models::Chain;
219247

@@ -259,6 +287,18 @@ mod tests {
259287
)
260288
}
261289

290+
fn empty_bebop_client() -> BebopClient {
291+
BebopClient::new(
292+
Chain::Ethereum,
293+
HashSet::new(),
294+
0.0,
295+
"".to_string(),
296+
"".to_string(),
297+
HashSet::new(),
298+
)
299+
.unwrap()
300+
}
301+
262302
fn create_test_bebop_state() -> BebopState {
263303
BebopState {
264304
base_token: wbtc(),
@@ -270,6 +310,7 @@ mod tests {
270310
bids: vec![65000.0f32, 1.5f32, 64950.0f32, 2.0f32, 64900.0f32, 0.5f32],
271311
asks: vec![65100.0f32, 1.0f32, 65150.0f32, 2.5f32, 65200.0f32, 1.5f32],
272312
},
313+
client: empty_bebop_client(),
273314
}
274315
}
275316

@@ -442,7 +483,7 @@ mod tests {
442483

443484
let weth = weth();
444485
let usdc = usdc();
445-
let state = BebopState::new(weth.clone(), usdc.clone(), price_data);
486+
let state = BebopState::new(weth.clone(), usdc.clone(), price_data, empty_bebop_client());
446487

447488
// swap 3 WETH -> USDC
448489
let amount_out_result = state

src/rfq/protocols/bebop/tycho_decoder.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use std::collections::HashMap;
1+
use std::collections::{HashMap, HashSet};
22

33
use tycho_client::feed::synchronizer::ComponentWithState;
44
use tycho_common::{models::token::Token, Bytes};
55

66
use super::{models::BebopPriceData, state::BebopState};
77
use crate::{
88
protocol::{errors::InvalidSnapshotError, models::TryFromWithBlock},
9-
rfq::models::TimestampHeader,
9+
rfq::{models::TimestampHeader, protocols::bebop::client::BebopClient},
1010
};
1111

1212
impl TryFromWithBlock<ComponentWithState, TimestampHeader> for BebopState {
@@ -75,7 +75,22 @@ impl TryFromWithBlock<ComponentWithState, TimestampHeader> for BebopState {
7575
.collect(),
7676
};
7777

78-
Ok(BebopState { base_token, quote_token, price_data })
78+
let ws_user = "".to_string();
79+
let ws_key = "".to_string();
80+
81+
let client = BebopClient::new(
82+
snapshot.component.chain.into(),
83+
HashSet::new(),
84+
0.0,
85+
ws_user,
86+
ws_key,
87+
HashSet::new(),
88+
)
89+
.map_err(|e| {
90+
InvalidSnapshotError::MissingAttribute(format!("Couldn't create BebopClient: {e}"))
91+
})?;
92+
93+
Ok(BebopState { base_token, quote_token, price_data, client })
7994
}
8095
}
8196

0 commit comments

Comments
 (0)