heavy-duty / znap

Performance-first Rust Framework to build APIs compatible with the Solana Actions Spec.
Apache License 2.0
63 stars 1 forks source link

Support query and params attributes to be of type Pubkey #60

Open danmt opened 3 months ago

danmt commented 3 months ago

Using the type Pubkey in the query and params attributes from a derive Action should be validated and parsed before reaching the context.

Assuming there's a case like this:

#[derive(Action)]
#[params(receiver: Pubkey)]
pub struct SendDonationAction;

Then the handler of this action gets access to the receiver already parsed and the validation happens behind the scenes. A handler could look like this:

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

    pub fn send_donation(ctx: Context<SendDonationAction>) -> Result<Transaction> {
        let account_pubkey = &ctx.payload.account; // payload.account should be parsed too
        let receiver_pubkey = &ctx.params.receiver; // receiver is already a pubkey
       // ...
    }
}

Note that the account and receiver come from the HTTP request as strings. Look into extractors with custom types.