2个版本

0.1.1 2024年6月16日
0.1.0 2024年6月16日

8#znap


用于 znap-lang

Apache-2.0

44KB
1K SLoC

Watch the demo

Znap

观看演示

以性能优先的Rust框架,用于构建与Solana Actions Spec兼容的API。

Tutorials

Znap是一个基于Rust的创新框架,旨在简化在Solana区块链上创建Solana Actions。

  • 用于编写Solana动作的Rust eDSL
  • 宏集合
  • CLI和工作区管理,用于开发完整的Solana动作

如果你熟悉使用Anchor框架进行开发,那么体验将会很熟悉。

入门指南

  1. cargo安装 znap-cli
  2. znap init<my-project-name>
  3. cd<my-project-name>
  4. znap new<collection-name>

描述 版本 文档
znap Znap框架的核心库,用于创建Solana动作 Crates.io Docs.rs
znap-syn 为Rust中的宏解析和生成代码 Crates.io Docs.rs
znap-macros 用于创建Solana动作的宏集合 Crates.io Docs.rs
znap-cli 用于与znap工作区交互的znap CLI Crates.io Docs.rs

示例

use solana_sdk::{
    message::Message, native_token::LAMPORTS_PER_SOL, pubkey::Pubkey, system_instruction::transfer,
    transaction::Transaction,
};
use std::str::FromStr;
use znap::prelude::*;

#[collection]
pub mod my_actions {
    use super::*;

    pub fn send_donation(ctx: Context<SendDonationAction>) -> Result<ActionTransaction> {
        let account_pubkey = Pubkey::from_str(&ctx.payload.account)
            .or_else(|_| Err(Error::from(ActionError::InvalidAccountPublicKey)))?;
        let receiver_pubkey = Pubkey::from_str(&ctx.params.receiver_address)
            .or_else(|_| Err(Error::from(ActionError::InvalidReceiverPublicKey)))?;
        let transfer_instruction = transfer(
            &account_pubkey,
            &receiver_pubkey,
            ctx.query.amount * LAMPORTS_PER_SOL,
        );
        let transaction_message = Message::new(&[transfer_instruction], None);
        let transaction = Transaction::new_unsigned(transaction_message);

        Ok(ActionTransaction {
            transaction,
            message: Some("send donation to alice".to_string()),
        })
    }
}

#[derive(Action)]
#[action(
    icon = "https://media.discordapp.net/attachments/1205590693041541181/1212566609202520065/icon.png?ex=667eb568&is=667d63e8&hm=0f247078545828c0a5cf8300a5601c56bbc9b59d3d87a0c74b082df0f3a6d6bd&=&format=webp&quality=lossless&width=660&height=660",
    title = "Send a Donation to {{params.receiver_address}}",
    description = "Send a donation to {{params.receiver_address}} using the Solana blockchain via a Blink.",
    label = "Send",
    link = {
        label = "Send 1 SOL",
        href = "/api/send_donation/{{params.receiver_address}}?amount=1",
    },
    link = {
        label = "Send 5 SOL",
        href = "/api/send_donation/{{params.receiver_address}}?amount=5",
    },
    link = {
        label = "Send SOL",
        href = "/api/send_donation/{{params.receiver_address}}?amount={amount}",
        parameter = { label = "Amount in SOL", name = "amount" }
    },
)]
#[query(amount: u64)]
#[params(receiver_address: String)]
pub struct SendDonationAction;

#[derive(ErrorCode)]
enum ActionError {
    #[error(msg = "Invalid account public key")]
    InvalidAccountPublicKey,
    #[error(msg = "Invalid receiver public key")]
    InvalidReceiverPublicKey,
}

依赖项

~3–13MB
~109K SLoC