heavy-duty / znap

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

Support variables inside href fo links #46

Closed danmt closed 2 months ago

danmt commented 2 months ago

Given the current example:

#[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 = "Alice's website",
    description = "Website to make a donation to Alice",
    label = "Send",
    link = {
        label = "Send 1 SOL",
        href = "/api/send_donation?amount=1",
    },
    link = {
        label = "Send 5 SOL",
        href = "/api/send_donation?amount=5",
    },
    link = {
        label = "Send SOL",
        href = "/api/send_donation?amount={amount}",
        parameter = { label = "Amount in SOL", name = "amount" }
    },
)]
#[query(amount: u64)]
#[params(receiver_address: String)]
pub struct SendDonationAction;

The href for the links wouldn't work in practice, since they don't have the respective receiver_address. One way would be to support variables inside the href. Essentially an API that looks like:

#[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 = "Donate to {{receiver_address}}",
    description = "Website to make a donation to {{receiver_address}}",
    label = "Send",
    link = {
        label = "Send 1 SOL",
        href = "/api/send_donation/{{receiver_address}}?amount=1",
    },
    link = {
        label = "Send 5 SOL",
        href = "/api/send_donation/{{receiver_address}}?amount=5",
    },
    link = {
        label = "Send SOL",
        href = "/api/send_donation/{{receiver_address}}?amount={amount}",
        parameter = { label = "Amount in SOL", name = "amount" }
    },
)]
#[query(amount: u64)]
#[params(receiver_address: String)]
pub struct SendDonationAction;

The default behavior for ToMetadata would return a proper link after replacing the receiver_address with the address provided. For a call to /api/send_donation/8ZjWXGYsY5N4ZsP6ymNoC7GqPa5s7nFUF97iR391Ch5k would have receiver_address replaced by 8ZjWXGYsY5N4ZsP6ymNoC7GqPa5s7nFUF97iR391Ch5k.

danmt commented 2 months ago

This is marked as a bug because the links functionality is not compatible with an action with params. The tests for send donation are giving a false positive at the moment.