-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodein.rs
180 lines (155 loc) · 5.24 KB
/
codein.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
use anchor_lang::prelude::*;
declare_id!("yourprogramid");
#[program]
pub mod code_in {
use super::*;
pub fn user_initialize(ctx: Context<UserInitialize>) -> Result<()> {
let (expected_pda, expected_bump) = Pubkey::find_program_address(
&[b"seedhere", ctx.accounts.user.key.as_ref()],
ctx.program_id,
);
let (_expected_db_pda, expected_db_bump) = Pubkey::find_program_address(
&[b"dbseedhere", ctx.accounts.user.key.as_ref()],
ctx.program_id,
);
require_keys_eq!(
ctx.accounts.code_account.key(),
expected_pda,
ErrorCode::InvalidAccount
);
let code_account = &mut ctx.accounts.code_account;
code_account.bump = expected_bump;
code_account.code = String::new();
code_account.method = 0;
code_account.decode_break = 0;
let db_account = &mut ctx.accounts.db_account;
db_account.bump = expected_db_bump;
db_account.handle = String::new();
db_account.tail_tx = String::new();
db_account.type_field = String::new();
db_account.offset = String::new();
Ok(())
}
pub fn send_code(
ctx: Context<SendCode>,
code: String,
before_tx: String,
method: u8,
decode_break: u8,
) -> Result<()> {
let (expected_pda, _expected_bump) = Pubkey::find_program_address(
&[b"seedhere", ctx.accounts.user.key.as_ref()],
ctx.program_id,
);
if ctx.accounts.code_account.key() != expected_pda {
return Err(ErrorCode::InvalidAccount.into());
}
ctx.accounts.code_account.before_tx = before_tx;
ctx.accounts.code_account.code = code;
ctx.accounts.code_account.method = method;
ctx.accounts.code_account.decode_break = decode_break;
Ok(())
}
pub fn db_code_in(
ctx: Context<DbCodeIn>,
handle: String,
tail_tx: String,
type_field: String,
offset: String,
) -> Result<()> {
let _required_lamports = 3_000_000;
let (expected_pda, _expected_bump) = Pubkey::find_program_address(
&[b"dbseedhere", ctx.accounts.user.key.as_ref()],
ctx.program_id,
);
if ctx.accounts.db_account.key() != expected_pda {
return Err(ErrorCode::InvalidAccount.into());
}
let expected_receiver = "your wallet"
.parse::<Pubkey>()
.map_err(|_| ErrorCode::InvalidWallet)?;
let _receiver_account = ctx
.remaining_accounts
.iter()
.find(|account| account.key == &expected_receiver);
let db_pda_account_info = &ctx.accounts.db_account.to_account_info();
let pda_balance = db_pda_account_info.lamports();
if pda_balance < _required_lamports {
return Err(ErrorCode::InvalidTransfer.into());
}
if **db_pda_account_info.try_borrow_lamports()? < _required_lamports {
return Err(ErrorCode::InsufficientFunds.into());
}
**db_pda_account_info.try_borrow_mut_lamports()? -= _required_lamports;
**_receiver_account
.expect("receiver_account not found")
.try_borrow_mut_lamports()? += _required_lamports;
ctx.accounts.db_account.handle = handle;
ctx.accounts.db_account.tail_tx = tail_tx;
ctx.accounts.db_account.type_field = type_field;
ctx.accounts.db_account.offset = offset;
Ok(())
}
}
#[account]
pub struct DBaccount {
pub bump: u8,
pub handle: String,
pub tail_tx: String,
pub type_field: String,
pub offset: String,
}
#[account]
pub struct CodeAccount {
pub bump: u8,
pub decode_break: u8,
pub method: u8,
pub code: String,
pub before_tx: String,
}
#[derive(Accounts)]
pub struct UserInitialize<'info> {
#[account(mut)]
pub user: Signer<'info>,
#[account(init, payer = user,seeds = [b"seedhere", user.key().as_ref()],bump,space = 1+1+1+900+100)]
pub code_account: Account<'info, CodeAccount>,
#[account(init, payer = user,seeds = [b"dbseedhere", user.key().as_ref()],bump,space =1+20+100+10+50)]
pub db_account: Account<'info, DBaccount>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct SendCode<'info> {
#[account(mut)]
pub user: Signer<'info>,
#[account(mut)]
pub code_account: Account<'info, CodeAccount>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct DbCodeIn<'info> {
#[account(mut)]
pub user: Signer<'info>,
#[account(mut)]
pub db_account: Account<'info, DBaccount>,
pub system_program: Program<'info, System>,
}
// 에러 코드 정의
#[error_code]
pub enum ErrorCode {
#[msg("Insufficient funds to send code.")]
InsufficientFunds,
#[msg("Invalid wallet address.")]
InvalidWallet,
#[msg("Invalid receiver address.")]
InvalidReceiver,
#[msg("Funds were not received by the expected wallet.")]
FundsNotReceived,
#[msg("Provided code account is invalid.")]
InvalidAccount,
#[msg("InvalidCodeFormat")]
InvalidCodeFormat,
#[msg("InvalidInstructionData")]
InvalidInstructionData,
#[msg("InvalidTransfer")]
InvalidTransfer,
}