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

Fix OKX integration approve transaction #104

Merged
merged 12 commits into from
Jan 27, 2025
29 changes: 28 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ tower = "0.4"
tower-http = { version = "0.4", features = ["trace"] }
tracing = "0.1"
web3 = "0.19"
lru = "0.12.5"

contracts = { git = "https://github.com/cowprotocol/services.git", tag = "v2.294.0", package = "contracts" }
ethrpc = { git = "https://github.com/cowprotocol/services.git", tag = "v2.294.0", package = "ethrpc" }
Expand Down
2 changes: 1 addition & 1 deletion config/example.okx.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ node-url = "http://localhost:8545"
chain-id = "1"

# Optionally specify a custom OKX Swap API endpoint
# endpoint = "https://www.okx.com/api/v5/dex/aggregator/swap"
# endpoint = "https://www.okx.com/api/v5/dex/aggregator/"

# OKX Project ID. Instruction on how to create a project:
# https://www.okx.com/en-au/web3/build/docs/waas/introduction-to-developer-portal-interface#create-project
Expand Down
2 changes: 1 addition & 1 deletion src/infra/config/dex/okx/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Into<okx::OkxCredentialsConfig> for OkxCredentialsConfig {
}

fn default_endpoint() -> reqwest::Url {
"https://www.okx.com/api/v5/dex/aggregator/swap"
"https://www.okx.com/api/v5/dex/aggregator/"
.parse()
.unwrap()
}
Expand Down
87 changes: 65 additions & 22 deletions src/infra/dex/okx/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,14 @@ impl SwapRequest {
}
}

/// A OKX API swap response - generic wrapper for success and failure cases.
/// A OKX API swap response.
///
/// See [API](https://www.okx.com/en-au/web3/build/docs/waas/dex-swap)
/// documentation for more detailed information on each parameter.
#[serde_as]
#[derive(Deserialize)]
#[derive(Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SwapResponse {
/// Error code, 0 for success, otherwise one of:
/// [error codes](https://www.okx.com/en-au/web3/build/docs/waas/dex-error-code)
#[serde_as(as = "serde_with::DisplayFromStr")]
pub code: i64,

/// Response data.
pub data: Vec<SwapResponseInner>,

/// Error code text message.
pub msg: String,
}

/// A OKX API swap response.
#[serde_as]
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SwapResponseInner {
/// Quote execution path.
pub router_result: SwapResponseRouterResult,

Expand All @@ -101,7 +84,7 @@ pub struct SwapResponseInner {
/// For all possible fields look into the documentation:
/// [API](https://www.okx.com/en-au/web3/build/docs/waas/dex-swap)
#[serde_as]
#[derive(Deserialize)]
#[derive(Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SwapResponseRouterResult {
/// The information of a token to be sold.
Expand All @@ -124,7 +107,7 @@ pub struct SwapResponseRouterResult {
/// For all possible fields look into the documentation:
/// [API](https://www.okx.com/en-au/web3/build/docs/waas/dex-swap)
#[serde_as]
#[derive(Deserialize)]
#[derive(Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SwapResponseFromToToken {
/// Address of the token smart contract.
Expand All @@ -136,7 +119,7 @@ pub struct SwapResponseFromToToken {
/// For all possible fields look into the documentation:
/// [API](https://www.okx.com/en-au/web3/build/docs/waas/dex-swap)
#[serde_as]
#[derive(Deserialize)]
#[derive(Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SwapResponseTx {
/// Estimated amount of the gas limit.
Expand All @@ -151,6 +134,66 @@ pub struct SwapResponseTx {
pub data: Vec<u8>,
}

/// A OKX API approve transaction request.
///
/// See [API](https://www.okx.com/en-au/web3/build/docs/waas/dex-approve-transaction)
/// documentation for more detailed information on each parameter.
#[serde_as]
#[derive(Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ApproveTransactionRequest {
/// Chain ID
#[serde_as(as = "serde_with::DisplayFromStr")]
pub chain_id: u64,

/// Contract address of a token to be permitted.
pub token_contract_address: H160,

/// The amount of token that needs to be permitted (in minimal divisible
/// units).
#[serde_as(as = "serialize::U256")]
pub approve_amount: U256,
}

impl ApproveTransactionRequest {
pub fn with_domain(chain_id: u64, order: &dex::Order) -> Self {
Self {
chain_id,
token_contract_address: order.sell.0,
approve_amount: order.amount.get(),
}
}
}

/// A OKX API approve transaction response.
/// Deserializing fields which are only used by the implementation.
/// See [API](https://www.okx.com/en-au/web3/build/docs/waas/dex-approve-transaction)
/// documentation for more detailed information on each parameter.
#[serde_as]
#[derive(Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApproveTransactionResponse {
/// The contract address of OKX DEX approve.
pub dex_contract_address: H160,
}

/// A OKX API response - generic wrapper for success and failure cases.
#[serde_as]
#[derive(Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Response<T> {
/// Error code, 0 for success, otherwise one of:
/// [error codes](https://www.okx.com/en-au/web3/build/docs/waas/dex-error-code)
#[serde_as(as = "serde_with::DisplayFromStr")]
pub code: i64,

/// Response data.
pub data: Vec<T>,

/// Error code text message.
pub msg: String,
}

#[derive(Deserialize)]
pub struct Error {
pub code: i64,
Expand Down
Loading
Loading