Skip to content

Commit

Permalink
begin significant refactor of sia-rust Client impls
Browse files Browse the repository at this point in the history
  • Loading branch information
Alrighttt committed Sep 12, 2024
1 parent d912cb3 commit 66e67d7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion mm2src/coins/sia-rust
24 changes: 16 additions & 8 deletions mm2src/coins/siacoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use mm2_number::{BigDecimal, BigInt, MmNumber};
use num_traits::ToPrimitive;
use rpc::v1::types::{Bytes as BytesJson, H256 as H256Json};
use serde_json::Value as Json;
use sia_rust::http::client::{SiaApiClient, SiaApiClientError, SiaHttpConf};
use sia_rust::http::endpoints::{AddressUtxosRequest, AddressUtxosResponse, AddressesEventsRequest,
use sia_rust::http::client::{ApiClientHelpers, ApiClient as SiaApiClient, ApiClientError as SiaApiClientError, ClientConf};
use sia_rust::http::endpoints::{GetAddressUtxosRequest, GetAddressUtxosResponse, AddressesEventsRequest,
TxpoolBroadcastRequest};
use sia_rust::spend_policy::SpendPolicy;
use sia_rust::transaction::{V1Transaction, V2Transaction};
Expand All @@ -41,6 +41,12 @@ use std::ops::Deref;
use std::str::FromStr;
use std::sync::{Arc, Mutex};

#[cfg(not(target_arch = "wasm32"))]
use sia_rust::http::client::native::NativeClient as SiaClientType;

#[cfg(target_arch = "wasm32")]
use sia_rust::http::client::WasmClient as SiaClientType;

pub mod sia_hd_wallet;
mod sia_withdraw;

Expand Down Expand Up @@ -72,7 +78,7 @@ pub struct SiaCoinActivationParams {
pub tx_history: bool,
pub required_confirmations: Option<u64>,
pub gap_limit: Option<u32>,
pub http_conf: SiaHttpConf,
pub http_conf: ClientConf,
}

pub struct SiaConfBuilder<'a> {
Expand All @@ -94,19 +100,21 @@ impl<'a> SiaConfBuilder<'a> {

// TODO see https://github.com/KomodoPlatform/komodo-defi-framework/pull/2086#discussion_r1521668313
// for additional fields needed
pub struct SiaCoinFields {
pub struct SiaCoinFieldsGeneric<T: SiaApiClient + ApiClientHelpers> {
/// SIA coin config
pub conf: SiaCoinConf,
pub priv_key_policy: PrivKeyPolicy<Keypair>,
/// HTTP(s) client
pub http_client: SiaApiClient,
pub http_client: T, // FIXME replace this with a generic that impls ApiClient trait
/// State of the transaction history loop (enabled, started, in progress, etc.)
pub history_sync_state: Mutex<HistorySyncState>,
/// This abortable system is used to spawn coin's related futures that should be aborted on coin deactivation
/// and on [`MmArc::stop`].
pub abortable_system: AbortableQueue,
}

pub type SiaCoinFields = SiaCoinFieldsGeneric<SiaClientType>;

pub async fn sia_coin_from_conf_and_params(
ctx: &MmArc,
ticker: &str,
Expand Down Expand Up @@ -943,14 +951,14 @@ pub enum SiaTransactionTypes {
}

impl SiaCoin {
async fn get_unspent_outputs(&self, address: Address) -> Result<AddressUtxosResponse, MmError<SiaApiClientError>> {
let request = AddressUtxosRequest { address };
async fn get_unspent_outputs(&self, address: Address) -> Result<GetAddressUtxosResponse, MmError<SiaApiClientError>> {
let request = GetAddressUtxosRequest { address , limit: None, offset: None};
let res = self.0.http_client.dispatcher(request).await?;
Ok(res)
}

async fn get_address_events(&self, address: Address) -> Result<Vec<Event>, MmError<SiaApiClientError>> {
let request = AddressesEventsRequest { address };
let request = AddressesEventsRequest { address , limit: None, offset: None };
let res = self.0.http_client.dispatcher(request).await?;
Ok(res)
}
Expand Down
6 changes: 3 additions & 3 deletions mm2src/coins/siacoin/sia_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{MarketCoinOps, PrivKeyPolicy, TransactionData, TransactionDetails, T
use common::now_sec;
use mm2_err_handle::mm_error::MmError;
use mm2_err_handle::prelude::*;
use sia_rust::http::endpoints::AddressUtxosResponse;
use sia_rust::http::endpoints::GetAddressUtxosResponse;
use sia_rust::spend_policy::SpendPolicy;
use sia_rust::transaction::{SiacoinOutput, V2TransactionBuilder};
use sia_rust::types::{Address, Currency};
Expand Down Expand Up @@ -46,9 +46,9 @@ impl<'a> SiaWithdrawBuilder<'a> {
#[allow(clippy::result_large_err)]
fn select_outputs(
&self,
mut unspent_outputs: AddressUtxosResponse,
mut unspent_outputs: GetAddressUtxosResponse,
total_amount: u128,
) -> Result<AddressUtxosResponse, MmError<WithdrawError>> {
) -> Result<GetAddressUtxosResponse, MmError<WithdrawError>> {
// Sort outputs from largest to smallest
unspent_outputs.sort_by(|a, b| b.siacoin_output.value.0.cmp(&a.siacoin_output.value.0));

Expand Down

0 comments on commit 66e67d7

Please sign in to comment.