Skip to content

Commit

Permalink
Improve Kado models
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed May 23, 2024
1 parent bd95cfd commit 03ce2fb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
35 changes: 29 additions & 6 deletions crates/fiat/src/providers/kado/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,30 +131,30 @@ impl KadoClient {
fiat_currency: request.clone().fiat_currency,
crypto_amount: quote.clone().receive_unit_count_after_fees.amount,
redirect_url: self.redirect_url(
request.fiat_currency,
request.fiat_currency.as_str(),
request.fiat_amount,
request_map.clone(),
request.wallet_address,
request.wallet_address.as_str(),
),
}
}

pub fn redirect_url(
&self,
fiat_currency: String,
fiat_currency: &str,
fiat_amount: f64,
fiat_mapping: FiatMapping,
address: String,
address: &str,
) -> String {
let mut components = Url::parse(REDIRECT_URL).unwrap();
components
.query_pairs_mut()
.append_pair("apiKey", self.api_key.as_str())
.append_pair("product", "BUY")
.append_pair("onPayAmount", &fiat_amount.to_string())
.append_pair("onPayCurrency", &fiat_currency)
.append_pair("onPayCurrency", fiat_currency)
.append_pair("onRevCurrency", &fiat_mapping.symbol)
.append_pair("onToAddress", &address)
.append_pair("onToAddress", address)
.append_pair(
"network",
&fiat_mapping.network.unwrap_or_default().to_uppercase(),
Expand All @@ -164,3 +164,26 @@ impl KadoClient {
return components.as_str().to_string();
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_redirect_url() {
let client = KadoClient {
client: Client::new(),
api_key: "API_KEY".to_string(),
};
let fiat_mapping = FiatMapping {
symbol: "ETH".to_string(),
network: Some("ethereum".to_string()),
};

let expected_url = "https://app.kado.money/?apiKey=API_KEY&product=BUY&onPayAmount=100&onPayCurrency=USD&onRevCurrency=ETH&onToAddress=0x1234567890abcdef&network=ETHEREUM&mode=minimal";

let actual_url = client.redirect_url("USD", 100.0, fiat_mapping, "0x1234567890abcdef");

assert_eq!(actual_url, expected_url);
}
}
1 change: 0 additions & 1 deletion crates/fiat/src/providers/kado/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ impl FiatProvider for KadoClient {
Ok(assets)
}

// full transaction: https://github.com/mercuryoio/api-migration-docs/blob/master/Widget_API_Mercuryo_v1.6.md#22-callbacks-response-body
async fn webhook(
&self,
data: serde_json::Value,
Expand Down
25 changes: 15 additions & 10 deletions crates/fiat/src/providers/mercuryo/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use reqwest::Client;
use sha2::{Digest, Sha512};
use url::Url;

use super::model::{Asset, Currencies, MercyryoQuote, Response};
use super::model::{Asset, Currencies, Quote, QuoteQuery, Response};

const MERCURYO_API_BASE_URL: &str = "https://api.mercuryo.io";
const MERCURYO_REDIRECT_URL: &str = "https://exchange.mercuryo.io";
Expand Down Expand Up @@ -33,17 +33,22 @@ impl MercuryoClient {
symbol: String,
fiat_amount: f64,
network: String,
) -> Result<MercyryoQuote, Box<dyn std::error::Error + Send + Sync>> {
let url = format!(
"{}/v1.6/widget/buy/rate?from={}&to={}&amount={}&network={}&widget_id={}",
MERCURYO_API_BASE_URL, fiat_currency, symbol, fiat_amount, network, self.widget_id
);
) -> Result<Quote, Box<dyn std::error::Error + Send + Sync>> {
let query = QuoteQuery {
from: fiat_currency.clone(),
to: symbol.clone(),
amount: fiat_amount,
network: network.clone(),
widget_id: self.widget_id.clone(),
};
let url = format!("{}/v1.6/widget/buy/rate", MERCURYO_API_BASE_URL);
let quote = self
.client
.get(&url)
.get(url.as_str())
.query(&query)
.send()
.await?
.json::<Response<MercyryoQuote>>()
.json::<Response<Quote>>()
.await?;
Ok(quote.data)
}
Expand Down Expand Up @@ -107,7 +112,7 @@ impl MercuryoClient {
&self,
request: FiatBuyRequest,
request_map: FiatMapping,
quote: MercyryoQuote,
quote: Quote,
) -> FiatQuote {
FiatQuote {
provider: Self::NAME.as_fiat_provider(),
Expand All @@ -122,7 +127,7 @@ impl MercuryoClient {
}
}

pub fn redirect_url(&self, quote: MercyryoQuote, network: String, address: String) -> String {
pub fn redirect_url(&self, quote: Quote, network: String, address: String) -> String {
let mut components = Url::parse(MERCURYO_REDIRECT_URL).unwrap();
let signature_content = format!("{}{}", address, self.secret_key);
let signature = hex::encode(Sha512::digest(signature_content));
Expand Down
13 changes: 11 additions & 2 deletions crates/fiat/src/providers/mercuryo/model.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize)]
pub struct Response<T> {
pub data: T,
}

#[derive(Debug, Deserialize, Clone)]
pub struct MercyryoQuote {
pub struct Quote {
pub amount: String,
pub currency: String,
pub fiat_amount: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct QuoteQuery {
pub from: String,
pub to: String,
pub amount: f64,
pub network: String,
pub widget_id: String,
}

#[derive(Debug, Deserialize, Clone)]
pub struct Currencies {
pub config: Config,
Expand Down

0 comments on commit 03ce2fb

Please sign in to comment.