Skip to main content

gmsol_sdk/js/simulation/
withdrawal.rs

1//! Withdrawal simulation.
2
3use serde::{Deserialize, Serialize};
4use tsify_next::Tsify;
5use wasm_bindgen::prelude::*;
6
7use crate::{
8    js::{
9        instructions::create_withdrawal::CreateWithdrawalParamsJs, simulation::encode_borsh_base64,
10    },
11    simulation::withdrawal::WithdrawalSimulationOutput,
12};
13
14/// Arguments for withdrawal simulation.
15#[derive(Debug, Serialize, Deserialize, Tsify)]
16#[tsify(into_wasm_abi, from_wasm_abi)]
17pub struct SimulateWithdrawalArgs {
18    pub(crate) params: CreateWithdrawalParamsJs,
19}
20
21/// Simulation output for withdrawal.
22#[wasm_bindgen(js_name = WithdrawalSimulationOutput)]
23pub struct JsWithdrawalSimulationOutput {
24    pub(crate) output: WithdrawalSimulationOutput,
25}
26
27#[wasm_bindgen(js_class = WithdrawalSimulationOutput)]
28impl JsWithdrawalSimulationOutput {
29    /// Returns the withdraw report.
30    pub fn report(&self) -> crate::Result<String> {
31        encode_borsh_base64(self.output.report())
32    }
33
34    /// Returns swap reports for the long token path.
35    pub fn long_swaps(&self) -> crate::Result<Vec<String>> {
36        self.output
37            .long_swaps()
38            .iter()
39            .map(encode_borsh_base64)
40            .collect()
41    }
42
43    /// Returns swap reports for the short token path.
44    pub fn short_swaps(&self) -> crate::Result<Vec<String>> {
45        self.output
46            .short_swaps()
47            .iter()
48            .map(encode_borsh_base64)
49            .collect()
50    }
51
52    /// Returns long token output amount.
53    pub fn long_output_amount(&self) -> u128 {
54        self.output.long_output_amount
55    }
56
57    /// Returns short token output amount.
58    pub fn short_output_amount(&self) -> u128 {
59        self.output.short_output_amount
60    }
61}