zera-sdk 0.1.0

Rust SDK for ZERA transactions, validator APIs, and bridge workflows
Documentation
use zera_proto::zera_txn::SmartContractExecuteTxn;

use crate::error::{Result, ZeraError};
use crate::grpc::{UnaryTransport, ValidatorApiClient};
use crate::smart_contracts::send_smart_contract_execute_txn;

use super::super::types::StakingOptions;
use super::super::utils::{
    create_staking_transaction, create_staking_transaction_with_client, DEFAULT_STAKING_FEE_ID,
};

pub async fn release_instant(
    public_key_base58_identifier: &str,
    private_key_base58: &str,
    options: StakingOptions,
) -> Result<SmartContractExecuteTxn> {
    if public_key_base58_identifier.is_empty() {
        return Err(ZeraError::Validation(
            "publicKeyBase58Identifier is required".to_string(),
        ));
    }
    if private_key_base58.is_empty() {
        return Err(ZeraError::Validation(
            "privateKeyBase58 is required".to_string(),
        ));
    }

    let fee_id = options
        .fee_id
        .as_deref()
        .unwrap_or(DEFAULT_STAKING_FEE_ID)
        .to_string();

    create_staking_transaction(
        "release_instant",
        "",
        public_key_base58_identifier,
        private_key_base58,
        &fee_id,
        options,
    )
    .await
}

pub async fn release_instant_with_client<T>(
    public_key_base58_identifier: &str,
    private_key_base58: &str,
    options: StakingOptions,
    client: &ValidatorApiClient<T>,
) -> Result<SmartContractExecuteTxn>
where
    T: UnaryTransport,
{
    if public_key_base58_identifier.is_empty() {
        return Err(ZeraError::Validation(
            "publicKeyBase58Identifier is required".to_string(),
        ));
    }
    if private_key_base58.is_empty() {
        return Err(ZeraError::Validation(
            "privateKeyBase58 is required".to_string(),
        ));
    }

    let fee_id = options
        .fee_id
        .as_deref()
        .unwrap_or(DEFAULT_STAKING_FEE_ID)
        .to_string();

    create_staking_transaction_with_client(
        "release_instant",
        "",
        public_key_base58_identifier,
        private_key_base58,
        &fee_id,
        options,
        client,
    )
    .await
}

pub async fn release_instant_and_send(
    public_key_base58_identifier: &str,
    private_key_base58: &str,
    options: StakingOptions,
) -> Result<String> {
    let grpc_config = options
        .grpc_config
        .clone()
        .unwrap_or_default();
    let txn = release_instant(public_key_base58_identifier, private_key_base58, options).await?;
    send_smart_contract_execute_txn(&txn, Some(grpc_config)).await
}