-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.rs
82 lines (69 loc) · 2.36 KB
/
lib.rs
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
use anchor_lang::prelude::*;
use anchor_spl::token::{self, MintTo};
#[program]
pub mod faucet {
use super::*;
pub fn initialize(
ctx: Context<InitializeFaucet>,
nonce: u8,
drip_volume: u64,
) -> ProgramResult {
let faucet_config = &mut ctx.accounts.faucet_config;
faucet_config.token_program = *ctx.accounts.token_program.key;
faucet_config.token_mint = *ctx.accounts.token_mint.key;
faucet_config.token_authority = *ctx.accounts.token_authority.key;
faucet_config.nonce = nonce;
faucet_config.drip_volume = drip_volume;
Ok(())
}
pub fn drip(ctx: Context<Drip>) -> ProgramResult {
let faucet_config = ctx.accounts.faucet_config.clone();
let seeds = &[
faucet_config.to_account_info().key.as_ref(),
&[faucet_config.nonce],
];
let signer_seeds = &[&seeds[..]];
let cpi_accounts = MintTo {
mint: ctx.accounts.token_mint.to_account_info(),
to: ctx.accounts.receiver.to_account_info(),
authority: ctx.accounts.token_authority.to_account_info(),
};
let cpi_program = ctx.accounts.token_program.clone();
let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer_seeds);
token::mint_to(cpi_ctx, faucet_config.drip_volume)?;
Ok(())
}
}
#[derive(Accounts)]
pub struct InitializeFaucet<'info> {
#[account(init)]
faucet_config: ProgramAccount<'info, FaucetConfig>,
#[account("token_program.key == &token::ID")]
token_program: AccountInfo<'info>,
#[account(mut)]
token_mint: AccountInfo<'info>,
#[account()]
token_authority: AccountInfo<'info>,
rent: Sysvar<'info, Rent>,
}
#[derive(Accounts)]
pub struct Drip<'info> {
#[account()]
faucet_config: ProgramAccount<'info, FaucetConfig>,
// #[account("token_program.key == &token::ID")]
token_program: AccountInfo<'info>,
// #[account(mut, "&faucet_config.token_mint == token_mint.key")]
token_mint: AccountInfo<'info>,
// #[account("&faucet_config.token_authority == token_authority.key")]
token_authority: AccountInfo<'info>,
#[account(mut)]
receiver: AccountInfo<'info>,
}
#[account]
pub struct FaucetConfig {
token_program: Pubkey,
token_mint: Pubkey,
token_authority: Pubkey,
nonce: u8,
drip_volume: u64,
}