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

Make referrer address configurable for 1Inch #459

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions crates/autopilot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ pub async fn main(args: arguments::Arguments) {
one_inch_api.as_ref().unwrap().clone(),
args.shared.disabled_one_inch_protocols.clone(),
rate_limiter(estimator.name()),
args.shared.one_inch_referrer_address
)),
PriceEstimatorType::Yearn => create_http_estimator(
"yearn-price-estimator".to_string(),
Expand Down
1 change: 1 addition & 0 deletions crates/orderbook/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ async fn main() {
one_inch_api.as_ref().unwrap().clone(),
args.shared.disabled_one_inch_protocols.clone(),
rate_limiter(estimator.name()),
args.shared.one_inch_referrer_address
)),
PriceEstimatorType::Yearn => create_http_estimator(
"yearn-price-estimator".to_string(),
Expand Down
7 changes: 7 additions & 0 deletions crates/shared/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ pub struct Arguments {
#[structopt(long, env, default_value = "https://api.1inch.exchange/")]
pub one_inch_url: Url,

/// Which address should receive the rewards for referring trades to 1Inch.
#[structopt(long, env)]
pub one_inch_referrer_address: Option<H160>,

/// The list of disabled 0x sources.
#[clap(long, env, use_value_delimiter = true)]
pub disabled_zeroex_sources: Vec<String>,
Expand Down Expand Up @@ -270,6 +274,9 @@ impl Display for Arguments {
self.disabled_one_inch_protocols
)?;
writeln!(f, "one_inch_url: {}", self.one_inch_url)?;
write!(f, "one_inch_referrer_address: ")?;
display_option(&self.one_inch_referrer_address.map(|a| format!("{a:?}")), f)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was needed because the Display implementation of H160 prints something like 0x123...abc which is not very helpful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun fact, you can also use {:#x}.

writeln!(f)?;
writeln!(
f,
"disabled_zeroex_sources: {:?}",
Expand Down
42 changes: 30 additions & 12 deletions crates/shared/src/oneinch_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pub struct SellOrderQuoteQuery {
pub virtual_parts: Option<Amount<1, 500>>,
/// Which tokens should be used for intermediate trading hops.
pub connector_tokens: Option<Vec<H160>>,
/// Adress referring this trade which will receive a portion of the swap
/// fees as a reward.
pub referrer_address: Option<H160>,
}

// The `Display` implementation for `H160` unfortunately does not print
Expand Down Expand Up @@ -131,6 +134,10 @@ impl SellOrderQuoteQuery {
url.query_pairs_mut()
.append_pair("gasPrice", &gas_price.to_string());
}
if let Some(referrer_address) = self.referrer_address {
url.query_pairs_mut()
.append_pair("referrerAddress", &addr2str(referrer_address));
}

url
}
Expand All @@ -140,6 +147,7 @@ impl SellOrderQuoteQuery {
buy_token: H160,
protocols: Option<Vec<String>>,
amount: U256,
referrer_address: Option<H160>,
) -> Self {
Self {
from_token_address: sell_token,
Expand All @@ -154,6 +162,7 @@ impl SellOrderQuoteQuery {
gas_price: None,
virtual_parts: None,
connector_tokens: None,
referrer_address,
}
}
}
Expand Down Expand Up @@ -189,8 +198,6 @@ pub struct SwapQuery {
pub disable_estimate: Option<bool>,
/// Receiver of destination currency. default: from_address
pub dest_receiver: Option<H160>,
/// Who is referring this swap to 1Inch.
pub referrer_address: Option<H160>,
/// Should Chi of from_token_address be burnt to compensate for gas.
/// default: false
pub burn_chi: Option<bool>,
Expand Down Expand Up @@ -243,7 +250,7 @@ impl SwapQuery {
url.query_pairs_mut()
.append_pair("destReceiver", &addr2str(dest_receiver));
}
if let Some(referrer_address) = self.referrer_address {
if let Some(referrer_address) = self.quote.referrer_address {
url.query_pairs_mut()
.append_pair("referrerAddress", &addr2str(referrer_address));
}
Expand Down Expand Up @@ -287,6 +294,7 @@ impl SwapQuery {
from_address: H160,
protocols: Option<Vec<String>>,
slippage: Slippage,
referrer_address: Option<H160>,
) -> Self {
Self {
from_address,
Expand All @@ -295,10 +303,13 @@ impl SwapQuery {
// does not hold balances to traded tokens.
disable_estimate: Some(true),
quote: SellOrderQuoteQuery::with_default_options(
sell_token, buy_token, protocols, in_amount,
sell_token,
buy_token,
protocols,
in_amount,
referrer_address,
),
dest_receiver: None,
referrer_address: None,
burn_chi: None,
allow_partial_fill: Some(false),
}
Expand Down Expand Up @@ -595,9 +606,9 @@ mod tests {
gas_price: None,
virtual_parts: None,
connector_tokens: None,
referrer_address: None,
},
dest_receiver: None,
referrer_address: None,
burn_chi: None,
allow_partial_fill: None,
}
Expand Down Expand Up @@ -637,11 +648,11 @@ mod tests {
addr!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"),
addr!("6810e776880c02933d47db1b9fc05908e5386b96"),
]),
referrer_address: Some(addr!("41111a111217dc0aa78b774fa6a738024120c302")),
},
burn_chi: Some(false),
allow_partial_fill: Some(false),
dest_receiver: Some(addr!("41111a111217dc0aa78b774fa6a738024120c302")),
referrer_address: Some(addr!("41111a111217dc0aa78b774fa6a738024120c302")),
}
.into_url(&base_url, 1);

Expand Down Expand Up @@ -818,11 +829,11 @@ mod tests {
addr!("111111111117dc0aa78b770fa6a738034120c302"),
None,
1_000_000_000_000_000_000u128.into(),
None,
),
burn_chi: None,
allow_partial_fill: None,
dest_receiver: None,
referrer_address: None,
})
.await
.unwrap();
Expand All @@ -848,15 +859,16 @@ mod tests {
main_route_parts: Some(Amount::new(3).unwrap()),
parts: Some(Amount::new(3).unwrap()),
fee: Some(1.5),
gas_price: Some(100_000.into()),
// setting `gas_price` will produce a response with different fields
gas_price: None,
connector_tokens: Some(vec![
addr!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"),
addr!("6810e776880c02933d47db1b9fc05908e5386b96"),
]),
virtual_parts: Some(Amount::new(10).unwrap()),
referrer_address: Some(addr!("9008D19f58AAbD9eD0D60971565AA8510560ab41")),
},
dest_receiver: Some(addr!("9008D19f58AAbD9eD0D60971565AA8510560ab41")),
referrer_address: Some(addr!("9008D19f58AAbD9eD0D60971565AA8510560ab41")),
burn_chi: Some(false),
allow_partial_fill: Some(false),
})
Expand Down Expand Up @@ -903,6 +915,7 @@ mod tests {
gas_price: None,
virtual_parts: None,
connector_tokens: None,
referrer_address: None,
}
.into_url(&base_url, 1);

Expand Down Expand Up @@ -934,6 +947,7 @@ mod tests {
gas_limit: Some(Amount::new(750_000).unwrap()),
main_route_parts: Some(Amount::new(3).unwrap()),
parts: Some(Amount::new(3).unwrap()),
referrer_address: Some(addr!("9008D19f58AAbD9eD0D60971565AA8510560ab41")),
}
.into_url(&base_url, 1);

Expand All @@ -951,7 +965,8 @@ mod tests {
&mainRouteParts=3\
&virtualParts=42\
&parts=3\
&gasPrice=200000"
&gasPrice=200000\
&referrerAddress=0x9008d19f58aabd9ed0d60971565aa8510560ab41"
);
}

Expand Down Expand Up @@ -1078,6 +1093,7 @@ mod tests {
addr!("111111111117dc0aa78b770fa6a738034120c302"),
None,
1_000_000_000_000_000_000u128.into(),
None,
))
.await
.unwrap();
Expand All @@ -1100,11 +1116,13 @@ mod tests {
addr!("6810e776880c02933d47db1b9fc05908e5386b96"),
]),
virtual_parts: Some(Amount::new(42).unwrap()),
gas_price: Some(200_000.into()),
// setting `gas_price` will produce a response with different fields
gas_price: None,
complexity_level: Some(Amount::new(3).unwrap()),
gas_limit: Some(Amount::new(750_000).unwrap()),
main_route_parts: Some(Amount::new(2).unwrap()),
parts: Some(Amount::new(2).unwrap()),
referrer_address: Some(addr!("6C642caFCbd9d8383250bb25F67aE409147f78b2")),
})
.await
.unwrap();
Expand Down
6 changes: 6 additions & 0 deletions crates/shared/src/price_estimation/oneinch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
};
use futures::{future::BoxFuture, FutureExt, StreamExt};
use model::order::OrderKind;
use primitive_types::H160;
use std::sync::Arc;

pub struct OneInchPriceEstimator {
Expand All @@ -22,6 +23,7 @@ pub struct OneInchPriceEstimator {
disabled_protocols: Vec<String>,
protocol_cache: ProtocolCache,
rate_limiter: Arc<RateLimiter>,
referrer_address: Option<H160>,
}

impl OneInchPriceEstimator {
Expand All @@ -41,6 +43,7 @@ impl OneInchPriceEstimator {
query.buy_token,
allowed_protocols,
query.in_amount,
self.referrer_address,
);
let quote_future = async move {
api.get_sell_order_quote(oneinch_query)
Expand Down Expand Up @@ -68,13 +71,15 @@ impl OneInchPriceEstimator {
api: Arc<dyn OneInchClient>,
disabled_protocols: Vec<String>,
rate_limiter: Arc<RateLimiter>,
referrer_address: Option<H160>,
) -> Self {
Self {
api,
disabled_protocols,
protocol_cache: ProtocolCache::default(),
sharing: Default::default(),
rate_limiter,
referrer_address,
}
}
}
Expand Down Expand Up @@ -118,6 +123,7 @@ mod tests {
Default::default(),
"test".into(),
)),
None,
)
}

Expand Down
1 change: 1 addition & 0 deletions crates/solver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ async fn main() {
args.shared.quasimodo_uses_internal_buffers,
args.shared.mip_uses_internal_buffers,
args.shared.one_inch_url,
args.shared.one_inch_referrer_address,
args.external_solvers.unwrap_or_default(),
args.oneinch_max_slippage_in_eth
.map(|float| U256::from_f64_lossy(float * 1e18)),
Expand Down
2 changes: 2 additions & 0 deletions crates/solver/src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ pub fn create(
quasimodo_uses_internal_buffers: bool,
mip_uses_internal_buffers: bool,
one_inch_url: Url,
one_inch_referrer_address: Option<H160>,
external_solvers: Vec<ExternalSolverArg>,
oneinch_max_slippage_in_wei: Option<U256>,
order_converter: Arc<OrderConverter>,
Expand Down Expand Up @@ -342,6 +343,7 @@ pub fn create(
one_inch_url.clone(),
oneinch_slippage_bps,
oneinch_max_slippage_in_wei,
one_inch_referrer_address,
)?,
)))),
SolverType::ZeroEx => {
Expand Down
8 changes: 7 additions & 1 deletion crates/solver/src/solver/oneinch_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use ethcontract::{Account, Bytes};
use maplit::hashmap;
use model::order::OrderKind;
use num::{BigRational, FromPrimitive, ToPrimitive};
use primitive_types::U256;
use primitive_types::{H160, U256};
use reqwest::Client;
use reqwest::Url;
use shared::conversions::U256Ext;
Expand All @@ -46,6 +46,7 @@ pub struct OneInchSolver {
oneinch_slippage_bps: u32,
/// how much slippage in wei we allow per trade
max_slippage_in_wei: Option<U256>,
referrer_address: Option<H160>,
}

impl From<RestError> for SettlementError {
Expand All @@ -70,6 +71,7 @@ impl OneInchSolver {
one_inch_url: Url,
oneinch_slippage_bps: u32,
max_slippage_in_wei: Option<U256>,
referrer_address: Option<H160>,
) -> Result<Self> {
let settlement_address = settlement_contract.address();
Ok(Self {
Expand All @@ -81,6 +83,7 @@ impl OneInchSolver {
protocol_cache: ProtocolCache::default(),
oneinch_slippage_bps,
max_slippage_in_wei,
referrer_address,
})
}
}
Expand Down Expand Up @@ -158,6 +161,7 @@ impl OneInchSolver {
self.settlement_contract.address(),
protocols,
slippage,
self.referrer_address,
);

tracing::debug!("querying 1Inch swap api with {:?}", query);
Expand Down Expand Up @@ -269,6 +273,7 @@ mod tests {
protocol_cache: ProtocolCache::default(),
oneinch_slippage_bps: 10u32,
max_slippage_in_wei: Some(U256::MAX),
referrer_address: None,
}
}

Expand Down Expand Up @@ -576,6 +581,7 @@ mod tests {
OneInchClientImpl::DEFAULT_URL.try_into().unwrap(),
10u32,
None,
None,
)
.unwrap();
let slippage = Slippage::percentage_from_basis_points(solver.oneinch_slippage_bps).unwrap();
Expand Down