fix(tauri, gui): Allow Tauri command to be called with empty arguments

- Allow Tauri command to be called with empty arguments
- Add struct for GetSwapInfosAllArgs
This commit is contained in:
binarybaron 2024-08-27 18:53:58 +02:00 committed by binarybaron
parent de1f77bf80
commit ca25e0454f
5 changed files with 185 additions and 1239 deletions

View File

@ -26,47 +26,6 @@ export interface BuyXmrArgs {
monero_receive_address: string;
}
export interface ResumeArgs {
swap_id: string;
}
export interface CancelAndRefundArgs {
swap_id: string;
}
export interface MoneroRecoveryArgs {
swap_id: string;
}
export interface WithdrawBtcArgs {
amount?: number;
address: string;
}
export interface BalanceArgs {
force_refresh: boolean;
}
export interface ListSellersArgs {
rendezvous_point: string;
}
export interface StartDaemonArgs {
server_address: string;
}
export interface GetSwapInfoArgs {
swap_id: string;
}
export interface ResumeSwapResponse {
result: string;
}
export interface BalanceResponse {
balance: number;
}
/** Represents a quote for buying XMR. */
export interface BidQuote {
/** The price at which the maker is willing to buy at. */
@ -85,13 +44,42 @@ export interface BuyXmrResponse {
quote: BidQuote;
}
export interface GetHistoryEntry {
export interface ResumeSwapArgs {
swap_id: string;
state: string;
}
export interface GetHistoryResponse {
swaps: GetHistoryEntry[];
export interface ResumeSwapResponse {
result: string;
}
export interface CancelAndRefundArgs {
swap_id: string;
}
export interface MoneroRecoveryArgs {
swap_id: string;
}
export interface WithdrawBtcArgs {
amount?: number;
address: string;
}
export interface WithdrawBtcResponse {
amount: number;
txid: string;
}
export interface ListSellersArgs {
rendezvous_point: string;
}
export interface StartDaemonArgs {
server_address: string;
}
export interface GetSwapInfoArgs {
swap_id: string;
}
export interface Seller {
@ -126,11 +114,122 @@ export interface GetSwapInfoResponse {
timelock?: ExpiredTimelocks;
}
export interface BalanceArgs {
force_refresh: boolean;
}
export interface BalanceResponse {
balance: number;
}
export interface GetHistoryArgs {
}
export interface GetHistoryEntry {
swap_id: string;
state: string;
}
export interface GetHistoryResponse {
swaps: GetHistoryEntry[];
}
export interface SuspendCurrentSwapResponse {
swap_id: string;
}
export interface BuyXmrArgs {
seller: string;
bitcoin_change_address: string;
monero_receive_address: string;
}
export interface BuyXmrResponse {
swap_id: string;
quote: BidQuote;
}
export interface ResumeSwapArgs {
swap_id: string;
}
export interface ResumeSwapResponse {
result: string;
}
export interface CancelAndRefundArgs {
swap_id: string;
}
export interface MoneroRecoveryArgs {
swap_id: string;
}
export interface WithdrawBtcArgs {
amount?: number;
address: string;
}
export interface WithdrawBtcResponse {
amount: number;
txid: string;
}
export interface ListSellersArgs {
rendezvous_point: string;
}
export interface StartDaemonArgs {
server_address: string;
}
export interface GetSwapInfoArgs {
swap_id: string;
}
export interface GetSwapInfoResponse {
swap_id: string;
seller: Seller;
completed: boolean;
start_date: string;
state_name: string;
xmr_amount: number;
btc_amount: number;
tx_lock_id: string;
tx_cancel_fee: number;
tx_refund_fee: number;
tx_lock_fee: number;
btc_refund_address: string;
cancel_timelock: CancelTimelock;
punish_timelock: PunishTimelock;
timelock?: ExpiredTimelocks;
}
export interface BalanceArgs {
force_refresh: boolean;
}
export interface BalanceResponse {
balance: number;
}
export interface GetHistoryArgs {
}
export interface GetHistoryEntry {
swap_id: string;
state: string;
}
export interface GetHistoryResponse {
swaps: GetHistoryEntry[];
}
export interface Seller {
peer_id: string;
addresses: string[];
}
export interface SuspendCurrentSwapResponse {
swap_id: string;
}

View File

@ -34,7 +34,7 @@ async function invoke<ARGS, RESPONSE>(
}
async function invokeNoArgs<RESPONSE>(command: string): Promise<RESPONSE> {
return invokeUnsafe(command, {}) as Promise<RESPONSE>;
return invokeUnsafe(command) as Promise<RESPONSE>;
}
export async function checkBitcoinBalance() {

View File

@ -3,8 +3,8 @@ use std::sync::Arc;
use swap::cli::{
api::{
request::{
BalanceArgs, BuyXmrArgs, GetHistoryArgs, ResumeSwapArgs, SuspendCurrentSwapArgs,
WithdrawBtcArgs,
BalanceArgs, BuyXmrArgs, GetHistoryArgs, GetSwapInfosAllArgs, ResumeSwapArgs,
SuspendCurrentSwapArgs, WithdrawBtcArgs,
},
Context, ContextBuilder,
},
@ -36,6 +36,17 @@ impl<T, E: ToString> ToStringResult<T> for Result<T, E> {
/// async fn get_balance(context: tauri::State<'...>, args: BalanceArgs) -> Result<BalanceArgs::Response, String> {
/// args.handle(context.inner().clone()).await.to_string_result()
/// }
///
/// # Example 2
/// ```ignored
/// tauri_command!(get_balance, BalanceArgs, no_args);
/// ```
/// will resolve to
/// ```ignored
/// #[tauri::command]
/// async fn get_balance(context: tauri::State<'...>) -> Result<BalanceArgs::Response, String> {
/// BalanceArgs {}.handle(context.inner().clone()).await.to_string_result()
/// }
/// ```
macro_rules! tauri_command {
($fn_name:ident, $request_name:ident) => {
@ -52,14 +63,28 @@ macro_rules! tauri_command {
.to_string_result()
}
};
($fn_name:ident, $request_name:ident, no_args) => {
#[tauri::command]
async fn $fn_name(
context: tauri::State<'_, Arc<Context>>,
) -> Result<<$request_name as swap::cli::api::request::Request>::Response, String> {
<$request_name as swap::cli::api::request::Request>::request(
$request_name {},
context.inner().clone(),
)
.await
.to_string_result()
}
};
}
tauri_command!(get_balance, BalanceArgs);
tauri_command!(get_swap_infos_all, BalanceArgs);
tauri_command!(buy_xmr, BuyXmrArgs);
tauri_command!(get_history, GetHistoryArgs);
tauri_command!(resume_swap, ResumeSwapArgs);
tauri_command!(withdraw_btc, WithdrawBtcArgs);
tauri_command!(suspend_current_swap, SuspendCurrentSwapArgs);
tauri_command!(suspend_current_swap, SuspendCurrentSwapArgs, no_args);
tauri_command!(get_swap_infos_all, GetSwapInfosAllArgs, no_args);
tauri_command!(get_history, GetHistoryArgs, no_args);
fn setup<'a>(app: &'a mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
tauri::async_runtime::block_on(async {

File diff suppressed because it is too large Load Diff

View File

@ -344,6 +344,16 @@ impl Request for GetConfigArgs {
}
}
pub struct GetSwapInfosAllArgs;
impl Request for GetSwapInfosAllArgs {
type Response = Vec<GetSwapInfoResponse>;
async fn request(self, ctx: Arc<Context>) -> Result<Self::Response> {
get_swap_infos_all(ctx).await
}
}
#[tracing::instrument(fields(method = "suspend_current_swap"), skip(context))]
pub async fn suspend_current_swap(context: Arc<Context>) -> Result<SuspendCurrentSwapResponse> {
let swap_id = context.swap_lock.get_current_swap_id().await;