1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use {
anchor_lang::{prelude::*, AnchorDeserialize},
std::convert::TryFrom,
};
pub const SEED_API: &[u8] = b"api";
#[account]
#[derive(Debug)]
pub struct Api {
pub ack_authority: Pubkey,
pub authority: Pubkey,
pub base_url: String,
pub request_count: u64,
}
impl Api {
pub fn pubkey(authority: Pubkey, base_url: String) -> Pubkey {
Pubkey::find_program_address(
&[SEED_API, authority.as_ref(), base_url.as_bytes()],
&crate::ID,
)
.0
}
}
impl TryFrom<Vec<u8>> for Api {
type Error = Error;
fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
Api::try_deserialize(&mut data.as_slice())
}
}
pub trait ApiAccount {
fn pubkey(&self) -> Pubkey;
fn init(&mut self, ack_authority: Pubkey, authority: Pubkey, base_url: String) -> Result<()>;
}
impl ApiAccount for Account<'_, Api> {
fn pubkey(&self) -> Pubkey {
Api::pubkey(self.authority, self.base_url.clone())
}
fn init(&mut self, ack_authority: Pubkey, authority: Pubkey, base_url: String) -> Result<()> {
self.ack_authority = ack_authority;
self.authority = authority;
self.base_url = base_url;
self.request_count = 0;
Ok(())
}
}