sablier_webhook_program/state/
webhook.rs

1use std::{
2    collections::HashMap,
3    fmt::{Display, Formatter},
4    str::FromStr,
5};
6
7use anchor_lang::{prelude::*, AnchorDeserialize};
8use serde::{Deserialize, Serialize};
9
10use crate::{constants::SEED_WEBHOOK, errors::SablierError};
11
12#[account]
13#[derive(Debug, Deserialize, Serialize)]
14pub struct Webhook {
15    pub authority: Pubkey,
16    pub body: Vec<u8>,
17    pub created_at: u64,
18    pub headers: HashMap<String, String>,
19    pub id: Vec<u8>,
20    pub method: HttpMethod,
21    pub relayer: Relayer,
22    pub url: String,
23    pub workers: Vec<Pubkey>,
24}
25
26impl Webhook {
27    pub fn pubkey(authority: Pubkey, id: Vec<u8>) -> Pubkey {
28        Pubkey::find_program_address(
29            &[SEED_WEBHOOK, authority.as_ref(), id.as_slice()],
30            &crate::ID,
31        )
32        .0
33    }
34}
35
36/// WebhookAccount ...
37pub trait WebhookAccount {
38    fn pubkey(&self) -> Pubkey;
39}
40
41impl WebhookAccount for Account<'_, Webhook> {
42    fn pubkey(&self) -> Pubkey {
43        Webhook::pubkey(self.authority, self.id.clone())
44    }
45}
46
47/// HttpMethod
48#[derive(
49    AnchorDeserialize, AnchorSerialize, Deserialize, Serialize, Clone, Debug, PartialEq, InitSpace,
50)]
51pub enum HttpMethod {
52    Get,
53    Post,
54}
55
56impl Display for HttpMethod {
57    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
58        match *self {
59            HttpMethod::Get => write!(f, "GET"),
60            HttpMethod::Post => write!(f, "POST"),
61        }
62    }
63}
64
65impl FromStr for HttpMethod {
66    type Err = Error;
67
68    fn from_str(input: &str) -> std::result::Result<HttpMethod, Self::Err> {
69        match input.to_uppercase().as_str() {
70            "GET" => Ok(HttpMethod::Get),
71            "POST" => Ok(HttpMethod::Post),
72            _ => Err(SablierError::InvalidHttpMethod.into()),
73        }
74    }
75}
76
77#[derive(AnchorDeserialize, AnchorSerialize, Deserialize, Serialize, Debug, Clone, PartialEq)]
78pub enum Relayer {
79    Sablier,
80    Custom(String),
81}