sablier_webhook_program/instructions/
webhook_create.rs

1use std::{collections::HashMap, mem::size_of};
2
3use anchor_lang::{
4    prelude::*,
5    system_program::{transfer, Transfer},
6};
7
8use crate::{
9    constants::{SEED_WEBHOOK, WEBHOOK_FEE},
10    state::{HttpMethod, Relayer, Webhook},
11};
12
13#[derive(Accounts)]
14#[instruction(
15    body: Vec<u8>,
16    headers: HashMap<String, String>,
17    id: Vec<u8>,
18    method: HttpMethod,
19    url: String
20)]
21pub struct WebhookCreate<'info> {
22    pub authority: Signer<'info>,
23
24    #[account(mut)]
25    pub payer: Signer<'info>,
26
27    #[account(
28        init,
29        seeds = [
30            SEED_WEBHOOK,
31            authority.key().as_ref(),
32            id.as_slice(),
33        ],
34        bump,
35        space = 8 + size_of::<Webhook>(),
36        payer = payer
37    )]
38    pub webhook: Account<'info, Webhook>,
39
40    pub system_program: Program<'info, System>,
41}
42
43pub fn handler(
44    ctx: Context<WebhookCreate>,
45    body: Vec<u8>,
46    headers: HashMap<String, String>,
47    id: Vec<u8>,
48    method: HttpMethod,
49    url: String,
50) -> Result<()> {
51    // Get accounts
52    let authority = &ctx.accounts.authority;
53    let payer = &mut ctx.accounts.payer;
54    let webhook = &mut ctx.accounts.webhook;
55    let system_program = &ctx.accounts.system_program;
56
57    // Initialize the webhook account
58    let current_slot = Clock::get()?.slot;
59    webhook.authority = authority.key();
60    webhook.body = body;
61    webhook.created_at = current_slot;
62    webhook.headers = headers;
63    webhook.id = id;
64    webhook.method = method;
65    webhook.relayer = Relayer::Sablier;
66    webhook.url = url;
67
68    // Transfer fees into webhook account to hold in escrow.
69    transfer(
70        CpiContext::new(
71            system_program.to_account_info(),
72            Transfer {
73                from: payer.to_account_info(),
74                to: webhook.to_account_info(),
75            },
76        ),
77        WEBHOOK_FEE,
78    )?;
79
80    Ok(())
81}