zera-sdk 0.1.0

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

use crate::contract::shared::validate_key_pair;
use crate::error::{Result, ZeraError};
use crate::grpc::{UnaryTransport, ValidatorApiClient};
use crate::sign::sign_with_key;
use crate::smart_contracts::{
    build_smart_contract_execute_txn_with_client, create_smart_contract_execute_txn,
    CreateSmartContractExecuteOptions, ExecuteParameter, ParameterType,
};

use super::types::StakingOptions;

pub const STAKING_CONTRACT_NAME: &str = "staking_proxy";
pub const STAKING_INSTANCE: u32 = 1;
pub const DEFAULT_STAKING_FEE_ID: &str = "$ZRA+0000";

pub async fn create_staking_transaction(
    action_name: &str,
    parameter_value: &str,
    public_key_base58_identifier: &str,
    private_key_base58: &str,
    fee_id: &str,
    options: StakingOptions,
) -> Result<SmartContractExecuteTxn> {
    let parameters = vec![
        ExecuteParameter {
            parameter_type: ParameterType::String.as_str().to_string(),
            value: action_name.as_bytes().to_vec(),
        },
        ExecuteParameter {
            parameter_type: ParameterType::String.as_str().to_string(),
            value: parameter_value.as_bytes().to_vec(),
        },
    ];

    create_smart_contract_execute_txn(
        STAKING_CONTRACT_NAME,
        STAKING_INSTANCE,
        "execute",
        &parameters,
        public_key_base58_identifier,
        private_key_base58,
        CreateSmartContractExecuteOptions {
            memo: options.memo,
            grpc_config: options.grpc_config,
            gas_fee_in_usd: options.gas_fee_in_usd,
            overestimate_percent: options.overestimate_percent,
            nonce: options.nonce,
            fee_id: Some(fee_id.to_string()),
            fee_amount_parts: options.fee_amount_usd,
        },
    )
    .await
}

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

    let parameters = vec![
        ExecuteParameter {
            parameter_type: ParameterType::String.as_str().to_string(),
            value: action_name.as_bytes().to_vec(),
        },
        ExecuteParameter {
            parameter_type: ParameterType::String.as_str().to_string(),
            value: parameter_value.as_bytes().to_vec(),
        },
    ];

    let mut txn = build_smart_contract_execute_txn_with_client(
        STAKING_CONTRACT_NAME,
        STAKING_INSTANCE,
        "execute",
        &parameters,
        public_key_base58_identifier,
        CreateSmartContractExecuteOptions {
            memo: options.memo,
            grpc_config: options.grpc_config,
            gas_fee_in_usd: options.gas_fee_in_usd,
            overestimate_percent: options.overestimate_percent,
            nonce: options.nonce,
            fee_id: Some(fee_id.to_string()),
            fee_amount_parts: options.fee_amount_usd,
        },
        client,
    )
    .await?;

    sign_with_key(&mut txn, private_key_base58, public_key_base58_identifier)?;
    Ok(txn)
}