Skip to content

Commit

Permalink
adding submit wallet connect
Browse files Browse the repository at this point in the history
  • Loading branch information
dougEfresh committed Apr 2, 2024
1 parent 795e356 commit a89314c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
30 changes: 28 additions & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
},
FireblocksFactory, Result,
};
use crate::types::connect::{WalletApprove, WalletConnectRequest, WalletConnectResponse};

pub const FIREBLOCKS_API: &str = "https://api.fireblocks.io/v1";
pub const FIREBLOCKS_SANDBOX_API: &str = "https://sandbox-api.fireblocks.io/v1";
Expand Down Expand Up @@ -74,6 +75,15 @@ impl FireblocksHttpClient {
self.send_no_body(url, Method::POST).await
}

async fn put<R: DeserializeOwned + Default, S: Serialize + Debug>(&self, url: Url, body: S) -> Result<R> {
let mut path = String::from(url.path());
if let Some(q) = url.query() {
path = format!("{path}?{q}");
}
let req = self.client.put(url).json(&body);
self.send(&path, req, body).await
}

async fn post_body<S, R>(&self, url: Url, body: S) -> Result<R>
where
S: Serialize + Debug,
Expand Down Expand Up @@ -114,15 +124,21 @@ impl FireblocksHttpClient {
let request_id =
response.headers().get("x-request-id").and_then(|value| value.to_str().ok()).unwrap_or_default().to_string();
debug!("got response with x-request-id={}", request_id);

let json_response =
response.headers().get("content-type").and_then(|value| value.to_str().ok()).unwrap_or_default().to_string().contains("json");
debug!("got response with x-request-id={}", request_id);

let text = response.text().await?;

// debug!("body response {}", text.clone());
let r: Result<R> = match status {
StatusCode::OK | StatusCode::ACCEPTED | StatusCode::CREATED => {
if text.is_empty() {
Ok((R::default(), request_id))
} else if !json_response{
Ok((R::default(), request_id))
} else {
//debug!("body: {text}");
match serde_json::from_str::<R>(&text) {
Ok(deserialized) => Ok((deserialized, request_id)),
Err(err) => Err(FireblocksError::SerdeJson { request_id, err, text }),
Expand Down Expand Up @@ -302,12 +318,22 @@ impl FireblocksFactory for FireblocksHttpClient {
}

async fn wallet_connections(&self) -> Result<PagedWalletConnectResponse> {
let (u, _) = self.build_uri("connections", None)?;
let u= self.build_uri("connections", None)?.0;
self.get(u).await
}

async fn wallet_connect(&self, request: &WalletConnectRequest) -> Result<WalletConnectResponse> {
let u = self.build_uri("connections/wc", None)?.0;
self.post_body(u, request).await
}

async fn wallet_connection_delete(&self, id: &str) -> Result<()> {
let u = self.build_uri(&format!("connections/wc/{id}"), None)?.0;
self.delete(u).await
}

async fn wallet_connection_approve(&self, id: &str, approve: bool) -> Result<()> {
let u = self.build_uri(&format!("connections/wc/{id}"), None)?.0;
self.put(u, WalletApprove{approve}).await
}
}
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::types::{
wallet::WalletCreateAssetResponse,
PaginatedAssetWallet, PagingVaultRequest,
};
use crate::types::connect::{WalletConnectRequest, WalletConnectResponse};

pub mod api;
pub mod error;
Expand Down Expand Up @@ -63,7 +64,9 @@ pub trait FireblocksClient {
async fn get_transaction(&self, id: &str) -> Result<Transaction>;

async fn wallet_connections(&self) -> Result<PagedWalletConnectResponse>;
async fn wallet_connect(&self, request: &WalletConnectRequest) -> Result<WalletConnectResponse>;
async fn wallet_connection_delete(&self, id: &str) -> Result<()>;
async fn wallet_connection_approve(&self, id: &str, approve: bool) -> Result<()>;
}

#[cfg(test)]
Expand Down Expand Up @@ -246,7 +249,7 @@ mod tests {

let (addr_response, _) = config
.client()
.external_wallet_asset(&contract_response.id, "ETH_TEST3", "0x9bb4d44e6963260a1850926e8f6beb8d5803836f")
.external_wallet_asset(&contract_response.id, "ETH_TEST5", "0x9bb4d44e6963260a1850926e8f6beb8d5803836f")
.await?;
assert!(!addr_response.id.is_empty());

Expand Down
34 changes: 34 additions & 0 deletions src/types/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,40 @@ pub struct PagedWalletConnectResponse {
pub page: Option<NextPage>,
}


#[derive(Debug, Deserialize, Serialize, Default)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[allow(dead_code)]
pub enum FeeLevel {
#[default]
Medium
}

#[derive(Debug, Deserialize, Serialize, Default)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct WalletConnectRequest {
pub fee_level: FeeLevel,
pub vault_account_id: i32,
pub uri: String,
pub chain_ids: Vec<String>
}

#[derive(Debug, Deserialize, Serialize, Default)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct WalletConnectResponse {
pub id: String,
}


#[derive(Debug, Deserialize, Serialize, Default)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct WalletApprove {
pub approve: bool,
}

#[cfg(test)]
mod test {
use crate::types::connect::WalletConnection;
Expand Down

0 comments on commit a89314c

Please sign in to comment.