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

OKX integration #97

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ path = "src/main.rs"
anyhow = "1"
async-trait = "0.1.80"
axum = "0.6"
base64 = "0.22.1"
bigdecimal = { version = "0.3", features = ["serde"] }
chrono = { version = "0.4.38", features = ["serde"], default-features = false }
clap = { version = "4", features = ["derive", "env"] }
ethereum-types = "0.14"
futures = "0.3.30"
hex = "0.4"
hmac = "0.12.1"
humantime = "2.1.0"
humantime-serde = "1.1.1"
hyper = "0.14"
Expand All @@ -34,6 +36,7 @@ reqwest = "0.11"
serde = "1"
serde_json = "1"
serde_with = "3"
sha2 = "0.10.8"
thiserror = "1"
tokio = { version = "1", features = ["macros", "rt-multi-thread", "signal", "time"] }
toml = "0.7"
Expand Down
5 changes: 5 additions & 0 deletions src/infra/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ pub enum Command {
#[clap(long, env)]
config: PathBuf,
},
/// solve individual orders using OKX API
Okx {
#[clap(long, env)]
config: PathBuf,
},
}
1 change: 1 addition & 0 deletions src/infra/config/dex/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod balancer;
mod file;
pub mod okx;
pub mod oneinch;
pub mod paraswap;
pub mod zeroex;
Expand Down
63 changes: 63 additions & 0 deletions src/infra/config/dex/okx/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use {
crate::{
domain::eth,
infra::{config::dex::file, contracts, dex::okx},
util::serialize,
},
serde::Deserialize,
serde_with::serde_as,
std::path::Path,
};

#[serde_as]
#[derive(Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
struct Config {
/// The versioned URL endpoint for the 0x swap API.
#[serde(default = "default_endpoint")]
#[serde_as(as = "serde_with::DisplayFromStr")]
endpoint: reqwest::Url,

/// Chain ID used to automatically determine contract addresses.
#[serde_as(as = "serialize::ChainId")]
chain_id: eth::ChainId,

pub api_project_id: String,

pub api_key: String,

pub api_secret_key: String,

pub api_passphrase: String,
}

fn default_endpoint() -> reqwest::Url {
"https://www.okx.com/api/v5/dex/aggregator/"
.parse()
.unwrap()
}

/// Load the 0x solver configuration from a TOML file.
///
/// # Panics
///
/// This method panics if the config is invalid or on I/O errors.
pub async fn load(path: &Path) -> super::Config {
let (base, config) = file::load::<Config>(path).await;

let settlement = contracts::Contracts::for_chain(config.chain_id).settlement;

super::Config {
okx: okx::Config {
chain_id: config.chain_id,
project_id: config.api_project_id,
api_key: config.api_key,
api_secret_key: config.api_secret_key,
api_passphrase: config.api_passphrase,
endpoint: config.endpoint,
settlement,
block_stream: base.block_stream.clone(),
},
base,
}
}
6 changes: 6 additions & 0 deletions src/infra/config/dex/okx/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod file;

pub struct Config {
pub okx: crate::infra::dex::okx::Config,
pub base: super::Config,
}
14 changes: 14 additions & 0 deletions src/infra/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
};

pub mod balancer;
pub mod okx;
pub mod oneinch;
pub mod paraswap;
pub mod simulator;
Expand All @@ -18,6 +19,7 @@ pub enum Dex {
OneInch(oneinch::OneInch),
ZeroEx(zeroex::ZeroEx),
ParaSwap(paraswap::ParaSwap),
Okx(okx::Okx),
}

impl Dex {
Expand All @@ -36,6 +38,7 @@ impl Dex {
Dex::OneInch(oneinch) => oneinch.swap(order, slippage).await?,
Dex::ZeroEx(zeroex) => zeroex.swap(order, slippage).await?,
Dex::ParaSwap(paraswap) => paraswap.swap(order, slippage, tokens).await?,
Dex::Okx(okx) => okx.swap(order, slippage).await?,
};
Ok(swap)
}
Expand Down Expand Up @@ -141,3 +144,14 @@ impl From<paraswap::Error> for Error {
}
}
}

impl From<okx::Error> for Error {
fn from(err: okx::Error) -> Self {
match err {
okx::Error::NotFound => Self::NotFound,
okx::Error::RateLimited => Self::RateLimited,
okx::Error::UnavailableForLegalReasons => Self::UnavailableForLegalReasons,
_ => Self::Other(Box::new(err)),
}
}
}
Loading
Loading