-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
290 lines (256 loc) · 8.37 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use anchor_lang::prelude::*;
use anchor_spl::token::{self, TokenAccount, Burn, Mint, MintTo, Token, Transfer};
pub mod utils;
declare_id!("D62yCVSQs6cerJJrfhnpSRVRUVk6HQU72YLmfXjWTzGC");
pub const CORE_STATE_SEED: &str = "core-state";
pub const VAULT_AUTH_SEED: &str = "vault-auth";
#[program]
pub mod coin_flip {
use super::*;
pub fn initialize(ctx: Context<Initialize>, args: InitializeArgs) -> ProgramResult {
ctx.accounts.core_state.admin = ctx.accounts.admin.key();
ctx.accounts.core_state.core_state_nonce = args.core_state_nonce;
ctx.accounts.core_state.flip_counter = 0;
Ok(())
}
pub fn deposit_sol(ctx: Context<DepositSol>, args: DepositSolArgs) -> ProgramResult {
let ix = anchor_lang::solana_program::system_instruction::transfer(
&ctx.accounts.admin.key(),
&ctx.accounts.vault_authority.key(),
args.amount,
);
let _result = anchor_lang::solana_program::program::invoke(
&ix,
&[
ctx.accounts.admin.to_account_info(),
ctx.accounts.vault_authority.to_account_info(),
ctx.accounts.system_program.to_account_info(),
],
);
Ok(())
}
pub fn withdraw_sol(ctx: Context<WithdrawSol>, args: WithdrawSolArgs) -> ProgramResult {
let ix = anchor_lang::solana_program::system_instruction::transfer(
&ctx.accounts.vault_authority.key(),
&ctx.accounts.admin.key(),
args.amount,
);
let _result = anchor_lang::solana_program::program::invoke(
&ix,
&[
ctx.accounts.vault_authority.to_account_info(),
ctx.accounts.admin.to_account_info(),
ctx.accounts.system_program.to_account_info(),
],
);
Ok(())
}
pub fn register_spl(ctx: Context<RegisterSpl>, args: RegisterSplArgs) -> ProgramResult {
Ok(())
}
pub fn deposit_spl(ctx: Context<DepositSpl>, args: DepositSplArgs) -> ProgramResult {
Ok(())
}
pub fn withdraw_spl(ctx: Context<WithdrawSpl>, args: WithdrawSplArgs) -> ProgramResult {
Ok(())
}
pub fn bet_sol(ctx: Context<BetSol>, args: BetSolArgs) -> ProgramResult {
Ok(())
}
pub fn bet_spl(ctx: Context<BetSpl>, args: BetSplArgs) -> ProgramResult {
Ok(())
}
}
// -------------------------------------------------------------------------------- //
// ----------------------------------- Contexts ----------------------------------- //
// -------------------------------------------------------------------------------- //
#[derive(Accounts)]
#[instruction(args: InitializeArgs)]
pub struct Initialize<'info> {
#[account(mut)]
pub admin: Signer<'info>,
#[account(
init,
seeds = [CORE_STATE_SEED.as_bytes().as_ref(), admin.key().as_ref()],
bump = args.core_state_nonce,
payer = admin,
)]
pub core_state: Account<'info, CoreState>,
#[account(
seeds = [VAULT_AUTH_SEED.as_bytes().as_ref(), admin.key().as_ref()],
bump = args.vault_auth_nonce,
)]
pub vault_authority: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
#[instruction(args: DepositSolArgs)]
pub struct DepositSol<'info> {
#[account(
mut,
constraint = admin.lamports() >= args.amount @ ErrorCode::InsufficientFunds,
)]
pub admin: Signer<'info>,
#[account(
mut,
seeds = [VAULT_AUTH_SEED.as_bytes().as_ref(), admin.key().as_ref()],
bump = args.vault_auth_nonce,
)]
pub vault_authority: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
#[instruction(args: WithdrawSolArgs)]
pub struct WithdrawSol<'info> {
#[account(mut)]
pub admin: Signer<'info>,
#[account(
mut,
seeds = [VAULT_AUTH_SEED.as_bytes().as_ref(), admin.key().as_ref()],
bump = args.vault_auth_nonce,
)]
pub vault_authority: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
#[instruction(args: RegisterSplArgs)]
pub struct RegisterSpl<'info> {
#[account(mut)]
pub admin: Signer<'info>,
#[account(
mut,
seeds = [VAULT_AUTH_SEED.as_bytes().as_ref(), admin.key().as_ref()],
bump = args.vault_auth_nonce,
)]
pub vault_authority: AccountInfo<'info>,
}
#[derive(Accounts)]
#[instruction(args: DepositSplArgs)]
pub struct DepositSpl<'info> {
#[account(mut)]
pub admin: Signer<'info>,
#[account(
mut,
seeds = [VAULT_AUTH_SEED.as_bytes().as_ref(), admin.key().as_ref()],
bump = args.vault_auth_nonce,
)]
pub vault_authority: AccountInfo<'info>,
}
#[derive(Accounts)]
#[instruction(args: WithdrawSplArgs)]
pub struct WithdrawSpl<'info> {
#[account(mut)]
pub admin: Signer<'info>,
#[account(
mut,
seeds = [VAULT_AUTH_SEED.as_bytes().as_ref(), admin.key().as_ref()],
bump = args.vault_auth_nonce,
)]
pub vault_authority: AccountInfo<'info>,
}
#[derive(Accounts)]
#[instruction(args: BetSolArgs)]
pub struct BetSol<'info> {
#[account(mut)]
pub admin: AccountInfo<'info>,
#[account(
seeds = [CORE_STATE_SEED.as_bytes().as_ref(), admin.key().as_ref()],
bump = args.core_state_nonce,
)]
pub core_state: Account<'info, CoreState>,
#[account(mut)]
pub user_authority: Signer<'info>,
}
#[derive(Accounts)]
#[instruction(args: BetSplArgs)]
pub struct BetSpl<'info> {
#[account(mut)]
pub user_authority: Signer<'info>,
pub admin: AccountInfo<'info>,
#[account(
seeds = [CORE_STATE_SEED.as_bytes().as_ref(), admin.key().as_ref()],
bump = args.core_state_nonce,
)]
pub core_state: Account<'info, CoreState>,
#[account(
constraint = token_mint.key() == args.token_mint @ ErrorCode::TokenMintMismatch,
)]
pub token_mint: Account<'info, Mint>,
#[account(
mut,
constraint = user_token_account.owner == user_authority.key() @ ErrorCode::TokenOnwerMismatch,
constraint = user_token_account.mint == token_mint.key() @ ErrorCode::TokenOnwerMismatch,
)]
pub user_token_account: Account<'info, TokenAccount>,
}
// -------------------------------------------------------------------------------- //
// ------------------------------------- Args ------------------------------------- //
// -------------------------------------------------------------------------------- //
#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct InitializeArgs {
pub core_state_nonce: u8,
pub vault_auth_nonce: u8,
}
#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct DepositSolArgs {
pub core_state_nonce: u8,
pub vault_auth_nonce: u8,
pub amount: u64,
}
#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct WithdrawSolArgs {
pub core_state_nonce: u8,
pub vault_auth_nonce: u8,
pub amount: u64,
}
#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct RegisterSplArgs {
pub core_state_nonce: u8,
pub vault_auth_nonce: u8,
}
#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct DepositSplArgs {
pub core_state_nonce: u8,
pub vault_auth_nonce: u8,
pub amount: u64,
}
#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct WithdrawSplArgs {
pub core_state_nonce: u8,
pub vault_auth_nonce: u8,
pub amount: u64,
}
#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct BetSolArgs {
pub core_state_nonce: u8,
pub amount: u64,
pub bet_side: bool, // true = Head, false = Tail
}
#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct BetSplArgs {
pub core_state_nonce: u8,
pub amount: u64,
pub bet_side: bool, // true = Head, false = Tail
pub token_mint: Pubkey,
}
// -------------------------------------------------------------------------------- //
// ------------------------------------ Others ------------------------------------ //
// -------------------------------------------------------------------------------- //
#[account]
#[derive(Default)]
pub struct CoreState {
pub core_state_nonce: u8,
pub admin: Pubkey, // admin public key
pub flip_counter: u8,
}
#[error]
pub enum ErrorCode {
#[msg("Wrong Admin Address")]
WrongAdmin,
#[msg("Token Onwer Mismatch")]
TokenOnwerMismatch,
#[msg("Token Mint Mismatch")]
TokenMintMismatch,
#[msg("Insufficient Funds")]
InsufficientFunds,
}