Expand description
Performance-first Rust Framework to build APIs compatible with the Solana Actions Spec.
§Features
- Creating Solana Actions Metadata interfaces
- Creating Solana Actions POST Requests Transactions with and without query params.
- Creating Solana Actions GET Requests Transactions with and without query params.
§How to import znap
cargo add znap
- In your lib.rs file import:
use znap::prelude::*
§Znap ecosystem
§Example
ⓘ
use solana_sdk::{message::Message, pubkey, pubkey::Pubkey, transaction::Transaction};
use spl_associated_token_account::get_associated_token_address;
use spl_token::{instruction::transfer, ID as TOKEN_PROGRAM_ID};
use std::str::FromStr;
use znap::prelude::*;
#[collection]
pub mod my_actions {
use super::*;
pub fn fixed_transfer(ctx: Context<FixedTransferAction>) -> Result<Transaction> {
let account_pubkey = match Pubkey::from_str(&ctx.payload.account) {
Ok(account_pubkey) => account_pubkey,
_ => return Err(Error::from(ActionError::InvalidAccountPublicKey)),
};
let mint_pubkey = pubkey!("FtaDaiPPAy52vKtzdrpMLS3bXvG9LVUYJt6TeG6XxMUi");
let receiver_pubkey = pubkey!("6GBLiSwAPhDMttmdjo3wvEsssEnCiW3yZwVyVZnhFm3G");
let source_pubkey = get_associated_token_address(&account_pubkey, &mint_pubkey);
let destination_pubkey = get_associated_token_address(&receiver_pubkey, &mint_pubkey);
let transfer_instruction = match transfer(
&TOKEN_PROGRAM_ID,
&source_pubkey,
&destination_pubkey,
&account_pubkey,
&[&account_pubkey],
1,
) {
Ok(transfer_instruction) => transfer_instruction,
_ => return Err(Error::from(ActionError::InvalidInstruction)),
};
let transaction_message = Message::new(&[transfer_instruction], None);
Ok(Transaction::new_unsigned(transaction_message))
}
}
#[derive(Action)]
#[action(
icon = "https://google.com",
title = "Fixed transfer",
description = "Send a fixed transfer to the treasury",
label = "Send"
)]
pub struct FixedTransferAction;
#[derive(ErrorCode)]
enum ActionError {
#[error(msg = "Invalid account public key")]
InvalidAccountPublicKey,
#[error(msg = "Invalid instruction")]
InvalidInstruction,
}
Re-exports§
pub extern crate base64;
pub extern crate bincode;
pub extern crate colored;
pub extern crate solana_client;
pub extern crate tower_http;
pub extern crate znap_macros;
Modules§
Structs§
- Represents the data structure returned by a GET request to an endpoint of the Solana Actions API.
- Represents the data structure returned by the POST handlers.
- Represents the data structure returned by a POST request to an endpoint of the Solana Actions API.
- Data structure required to make a POST request to an endpoint of the Solana Actions API.
- Error occurred during the processing of the request.
Traits§
- Trait used to transform a struct into an Action.
- Trait used to transform a struct into an error code.
- Allows a struct to capture its internal values and return them as an ActionMetadata interface.
Functions§
Type Aliases§
- Used to rename Resolve and limit errors to those that occur within the program.