Skip to main content

sablier_webhook_program/instructions/
webhook_respond.rs

1use crate::constants::{SEED_WEBHOOK, TIMEOUT_THRESHOLD};
2
3use {
4    crate::state::Webhook,
5    anchor_lang::{prelude::*, system_program},
6};
7
8#[derive(Accounts)]
9#[instruction()]
10pub struct WebhookRespond<'info> {
11    #[account(mut)]
12    pub ack_authority: Signer<'info>,
13
14    #[account(
15        mut,
16        seeds = [
17            SEED_WEBHOOK,
18            webhook.authority.as_ref(),
19        ],
20        bump,
21        // close = caller,
22        // has_one = caller
23    )]
24    pub webhook: Account<'info, Webhook>,
25
26    #[account(address = system_program::ID)]
27    pub system_program: Program<'info, System>,
28
29    #[account()]
30    pub worker: SystemAccount<'info>,
31}
32
33pub fn handler(ctx: Context<WebhookRespond>) -> Result<()> {
34    // Get accounts
35    // let fee = &mut ctx.accounts.fee;
36    let webhook = &mut ctx.accounts.webhook;
37    let worker = &mut ctx.accounts.worker;
38
39    // Payout webhook fee
40    let current_slot = Clock::get().unwrap().slot;
41    let _is_authorized_worker = webhook.workers.contains(&worker.key());
42    let _is_within_execution_window = current_slot < webhook.created_at + TIMEOUT_THRESHOLD;
43    // if is_authorized_worker && is_within_execution_window {
44    //     // Pay worker for executing webhook
45    //     // fee.pay_to_worker(webhook)?;
46    // } else {
47    //     // Either someone is spamming or this webhook has timed out. Do not pay worker.
48    //     // TODO Perhaps rather than being paid to the admin, this could be put in an escrow account where all workers could claim equal rewards.
49    //     // TODO If not claimed within X slots, the admin can claim their rewards and close the account.
50    //     // fee.pay_to_admin(webhook)?;
51    // }
52
53    Ok(())
54}