nado-sdk 0.3.4

Official Rust SDK for the Nado Protocol API
Documentation
use ethers::types::H160;
use eyre::Result;

use crate::bindings::endpoint;
use crate::eip712_structs;

use crate::core::execute::NadoExecute;
use crate::utils::client_error::none_error;
use crate::{fields_to_vars, nado_builder};

nado_builder!(
    WithdrawCollateralBuilder,
    NadoExecute,
    amount: u128,
    product_id: u32,
    nonce: u64,
    linked_sender: [u8; 32],
    spot_leverage: bool;

    // we do not use macro here because of extra required argument
    pub async fn execute(&self) -> Result<()> {
        self.nado
            .withdraw_collateral(self.build().await?, self.spot_leverage)
            .await
    }

    pub async fn build_endpoint_tx(&self) -> Result<endpoint::WithdrawCollateral> {
        let tx = self.build().await?;
        Ok(
            endpoint::WithdrawCollateral {
                sender: tx.sender,
                amount: tx.amount,
                nonce: tx.nonce,
                product_id: tx.productId,
            }
        )
    }

    pub async fn build(&self) -> Result<eip712_structs::WithdrawCollateral> {
        let default_sender = self.nado.subaccount()?;
        let sender = self.linked_sender.unwrap_or(default_sender);
        let address = H160::from_slice(&sender[0..20]).0;
        let nonce = self
            .nonce
            .unwrap_or(self.nado.next_tx_nonce(address).await?);
        fields_to_vars!(self, amount, product_id);

        Ok(eip712_structs::WithdrawCollateral {
            sender,
            amount,
            nonce,
            productId: product_id,
        })
    }
);