Skip to content

Commit

Permalink
Migrate to 1Inch dev portal (#2029)
Browse files Browse the repository at this point in the history
# Description
1Inch requested all integrations to move to their new 1Inch dev portal
(similar to what 0x did not too long ago).

# Changes
This basically just needs 3 changes:
1. change base URL
2. add some prefix path in the url (in our case all APIs belong to the
`/swap` group)
3. add the `Authorization: Bearer API_KEY` header to all requests
(happens in cowprotocol/infrastructure#828)

## How to test
Did a manual test running all the ignored `oneinch_api` tests which do
actual requests to the API (note that this needed a slight modification
to the code adding the `Authorization` header.

<img width="686" alt="Screenshot 2023-10-31 at 19 52 47"
src="https://github.com/cowprotocol/services/assets/19190235/8804a5ea-d8ba-4248-8dbf-1ca57b46fbab">
  • Loading branch information
MartinquaXD authored Nov 3, 2023
1 parent 42d9618 commit cb8accd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/shared/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ pub struct Arguments {
pub disabled_one_inch_protocols: Vec<String>,

/// The 1Inch REST API URL to use.
#[structopt(long, env, default_value = "https://api.1inch.io/")]
#[structopt(long, env, default_value = "https://api.1inch.dev/")]
pub one_inch_url: Url,

/// Which address should receive the rewards for referring trades to 1Inch.
Expand Down
26 changes: 13 additions & 13 deletions crates/shared/src/oneinch_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn addr2str(addr: H160) -> String {

impl SellOrderQuoteQuery {
fn into_url(self, base_url: &Url, chain_id: u64) -> Url {
let endpoint = format!("v5.0/{chain_id}/quote");
let endpoint = format!("/swap/v5.0/{chain_id}/quote");
let mut url = crate::url::join(base_url, &endpoint);

url.query_pairs_mut()
Expand Down Expand Up @@ -246,7 +246,7 @@ impl Display for Slippage {
impl SwapQuery {
/// Encodes the swap query as
fn into_url(self, base_url: &Url, chain_id: u64) -> Url {
let endpoint = format!("v5.0/{chain_id}/swap");
let endpoint = format!("/swap/v5.0/{chain_id}/swap");
let mut url = crate::url::join(base_url, &endpoint);
url.query_pairs_mut()
.append_pair("fromTokenAddress", &addr2str(self.quote.from_token_address))
Expand Down Expand Up @@ -502,7 +502,7 @@ pub struct OneInchClientImpl {
}

impl OneInchClientImpl {
pub const DEFAULT_URL: &'static str = "https://api.1inch.io/";
pub const DEFAULT_URL: &'static str = "https://api.1inch.dev/";
// 1: mainnet, 100: gnosis chain
pub const SUPPORTED_CHAINS: &'static [u64] = &[1, 100];

Expand Down Expand Up @@ -570,13 +570,13 @@ impl OneInchClient for OneInchClientImpl {
}

async fn get_spender(&self) -> Result<Spender, OneInchError> {
let endpoint = format!("v5.0/{}/approve/spender", self.chain_id);
let endpoint = format!("/swap/v5.0/{}/approve/spender", self.chain_id);
let url = crate::url::join(&self.base_url, &endpoint);
logged_query(&self.client, url, None).await
}

async fn get_liquidity_sources(&self) -> Result<Protocols, OneInchError> {
let endpoint = format!("v5.0/{}/liquidity-sources", self.chain_id);
let endpoint = format!("/swap/v5.0/{}/liquidity-sources", self.chain_id);
let url = crate::url::join(&self.base_url, &endpoint);
logged_query(&self.client, url, None).await
}
Expand Down Expand Up @@ -750,7 +750,7 @@ mod tests {

#[test]
fn swap_query_serialization() {
let base_url = Url::parse("https://api.1inch.io/").unwrap();
let base_url = Url::parse("https://api.1inch.dev/").unwrap();
let url = SwapQuery {
from_address: addr!("00000000219ab540356cBB839Cbe05303d7705Fa"),
slippage: Slippage::percentage(0.5).unwrap(),
Expand All @@ -777,7 +777,7 @@ mod tests {

assert_eq!(
url.as_str(),
"https://api.1inch.io/v5.0/1/swap\
"https://api.1inch.dev/swap/v5.0/1/swap\
?fromTokenAddress=0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\
&toTokenAddress=0x111111111117dc0aa78b770fa6a738034120c302\
&amount=1000000000000000000\
Expand All @@ -788,7 +788,7 @@ mod tests {

#[test]
fn swap_query_serialization_options_parameters() {
let base_url = Url::parse("https://api.1inch.io/").unwrap();
let base_url = Url::parse("https://api.1inch.dev/").unwrap();
let url = SwapQuery {
from_address: addr!("00000000219ab540356cBB839Cbe05303d7705Fa"),
slippage: Slippage::percentage(0.5).unwrap(),
Expand Down Expand Up @@ -818,7 +818,7 @@ mod tests {

assert_eq!(
url.as_str(),
"https://api.1inch.io/v5.0/1/swap\
"https://api.1inch.dev/swap/v5.0/1/swap\
?fromTokenAddress=0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\
&toTokenAddress=0x111111111117dc0aa78b770fa6a738034120c302\
&amount=1000000000000000000\
Expand Down Expand Up @@ -1055,7 +1055,7 @@ mod tests {

#[test]
fn sell_order_quote_query_serialization() {
let base_url = Url::parse("https://api.1inch.io/").unwrap();
let base_url = Url::parse("https://api.1inch.dev/").unwrap();
let url = SellOrderQuoteQuery {
from_token_address: addr!("EeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"),
to_token_address: addr!("111111111117dc0aa78b770fa6a738034120c302"),
Expand All @@ -1074,7 +1074,7 @@ mod tests {

assert_eq!(
url.as_str(),
"https://api.1inch.io/v5.0/1/quote\
"https://api.1inch.dev/swap/v5.0/1/quote\
?fromTokenAddress=0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\
&toTokenAddress=0x111111111117dc0aa78b770fa6a738034120c302\
&amount=1000000000000000000"
Expand All @@ -1083,7 +1083,7 @@ mod tests {

#[test]
fn sell_order_quote_query_serialization_optional_parameters() {
let base_url = Url::parse("https://api.1inch.io/").unwrap();
let base_url = Url::parse("https://api.1inch.dev/").unwrap();
let url = SellOrderQuoteQuery {
from_token_address: addr!("EeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"),
to_token_address: addr!("111111111117dc0aa78b770fa6a738034120c302"),
Expand All @@ -1105,7 +1105,7 @@ mod tests {

assert_eq!(
url.as_str(),
"https://api.1inch.io/v5.0/1/quote\
"https://api.1inch.dev/swap/v5.0/1/quote\
?fromTokenAddress=0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\
&toTokenAddress=0x111111111117dc0aa78b770fa6a738034120c302\
&amount=1000000000000000000\
Expand Down

0 comments on commit cb8accd

Please sign in to comment.