sanctum-solana-serde

Crates.iosanctum-solana-serde
lib.rssanctum-solana-serde
version1.0.1
sourcesrc
created_at2023-12-11 04:13:05.169174
updated_at2024-01-17 06:55:33.867669
descriptionCollection of newtypes for common solana types to implement serde on for easy use in web applications
homepage
repositoryhttps://github.com/igneous-labs/sanctum-solana-serde.git
max_upload_size
id1064729
size23,316
Han Yang (billythedummy)

documentation

README

sanctum-solana-serde

Collection of newtypes for common solana types to implement serde on for easy use in web applications.

Examples

Using B64VersionedTx with Jupiter Swap API

Let's say you're building an application that calls Jupiter Swap API's /swap endpoint. You can use B64VersionedTx to handle deserialization of the API response for you:

use sanctum_solana_serde::b64_versioned_tx::B64VersionedTx;
use serde::Deserialize;

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct JupSwapResp {
    pub swap_transaction: B64VersionedTx,
    pub last_valid_block_height: u64, // note: jup API serializes this as a json number, not a string
}
use reqwest::Client;
use solana_sdk::transaction::VersionedTransaction;

async fn get_swap_tx_from_jup(
    client: &Client,
    post_params: serde_json::Value
) -> VersionedTransaction {
    let JupSwapResp {
        swap_transaction,
        ..
    } = client.post("https://quote-api.jup.ag/v6/swap")
        .json(post_params)
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    swap_transaction.0
}

Features

All individual newtypes are behind their own individual feature flag. All of them are enabled by default.

utoipa

The non-default utoipa feature implements utoipa::ToSchema for all the newtypes, allowing you to document them in openAPI/Swagger docs generated by utoipa.

Commit count: 24

cargo fmt