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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use {
crate::state::{
Api, ApiAccount, Config, HttpMethod, Request, RequestAccount, SEED_REQUEST,
},
anchor_lang::{
prelude::*,
solana_program::system_program,
system_program::{transfer, Transfer},
},
clockwork_network_program::state::Pool,
std::{collections::HashMap, mem::size_of},
};
#[derive(Accounts)]
#[instruction(
id: String,
method: HttpMethod,
route: String
)]
pub struct RequestNew<'info> {
#[account(address = api.pubkey())]
pub api: Account<'info, Api>,
#[account()]
pub caller: Signer<'info>,
#[account(address = Config::pubkey())]
pub config: Account<'info, Config>,
#[account(mut)]
pub payer: Signer<'info>,
#[account()]
pub pool: Account<'info, Pool>,
#[account(
init,
seeds = [
SEED_REQUEST,
api.key().as_ref(),
caller.key().as_ref(),
id.as_bytes(),
],
bump,
space = 8 + size_of::<Request>(),
payer = payer
)]
pub request: Account<'info, Request>,
#[account(address = system_program::ID)]
pub system_program: Program<'info, System>,
}
pub fn handler<'info>(
ctx: Context<RequestNew>,
id: String,
method: HttpMethod,
route: String,
) -> Result<()> {
let api = &ctx.accounts.api;
let caller = &ctx.accounts.caller;
let config = &ctx.accounts.config;
let payer = &mut ctx.accounts.payer;
let pool = &ctx.accounts.pool;
let request = &mut ctx.accounts.request;
let system_program = &ctx.accounts.system_program;
let current_slot = Clock::get().unwrap().slot;
let fee_amount = config.request_fee;
let headers = HashMap::new(); let workers = pool
.clone()
.into_inner()
.workers
.iter()
.map(|k| *k)
.collect::<Vec<Pubkey>>();
request.init(
api,
caller.key(),
current_slot,
fee_amount,
headers,
id,
method,
route,
workers,
)?;
transfer(
CpiContext::new(
system_program.to_account_info(),
Transfer {
from: payer.to_account_info(),
to: request.to_account_info(),
},
),
fee_amount,
)?;
Ok(())
}