lang: Use try_accounts instead of manual deser (#666)
This commit is contained in:
parent
8a9ea76095
commit
d1edf2653f
|
@ -25,7 +25,7 @@ use syn::parse_macro_input;
|
|||
/// For more, see the [`account`](./attr.account.html) attribute.
|
||||
#[deprecated(
|
||||
since = "0.14.0",
|
||||
note = "#[state] will be removed in a future version. Use a PDA with static seeds instead",
|
||||
note = "#[state] will be removed in a future version. Use a PDA with static seeds instead"
|
||||
)]
|
||||
#[proc_macro_attribute]
|
||||
pub fn state(
|
||||
|
|
|
@ -366,9 +366,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
|
|||
if remaining_accounts.is_empty() {
|
||||
return Err(anchor_lang::__private::ErrorCode::AccountNotEnoughKeys.into());
|
||||
}
|
||||
let state_account = &remaining_accounts[0];
|
||||
let loader: anchor_lang::Loader<#mod_name::#name> = anchor_lang::Loader::try_from(program_id, &state_account)?;
|
||||
remaining_accounts = &remaining_accounts[1..];
|
||||
let loader: anchor_lang::Loader<#mod_name::#name> = anchor_lang::Loader::try_accounts(program_id, &mut remaining_accounts, &[])?;
|
||||
|
||||
// Deserialize accounts.
|
||||
let mut accounts = #anchor_ident::try_accounts(
|
||||
|
@ -411,13 +409,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
|
|||
if remaining_accounts.is_empty() {
|
||||
return Err(anchor_lang::__private::ErrorCode::AccountNotEnoughKeys.into());
|
||||
}
|
||||
let state_account = &remaining_accounts[0];
|
||||
let mut state: #state_ty = {
|
||||
let data = state_account.try_borrow_data()?;
|
||||
let mut sliced: &[u8] = &data;
|
||||
anchor_lang::AccountDeserialize::try_deserialize(&mut sliced)?
|
||||
};
|
||||
remaining_accounts = &remaining_accounts[1..];
|
||||
let mut state: anchor_lang::ProgramState<#state_ty> = anchor_lang::ProgramState::try_accounts(program_id, &mut remaining_accounts, &[])?;
|
||||
|
||||
// Deserialize accounts.
|
||||
let mut accounts = #anchor_ident::try_accounts(
|
||||
|
@ -435,7 +427,8 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
|
|||
|
||||
// Serialize the state and save it to storage.
|
||||
accounts.exit(program_id)?;
|
||||
let mut data = state_account.try_borrow_mut_data()?;
|
||||
let acc_info = state.to_account_info();
|
||||
let mut data = acc_info.try_borrow_mut_data()?;
|
||||
let dst: &mut [u8] = &mut data;
|
||||
let mut cursor = std::io::Cursor::new(dst);
|
||||
state.try_serialize(&mut cursor)?;
|
||||
|
@ -526,13 +519,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
|
|||
if remaining_accounts.is_empty() {
|
||||
return Err(anchor_lang::__private::ErrorCode::AccountNotEnoughKeys.into());
|
||||
}
|
||||
let state_account = &remaining_accounts[0];
|
||||
let mut state: #state_ty = {
|
||||
let data = state_account.try_borrow_data()?;
|
||||
let mut sliced: &[u8] = &data;
|
||||
anchor_lang::AccountDeserialize::try_deserialize(&mut sliced)?
|
||||
};
|
||||
remaining_accounts = &remaining_accounts[1..];
|
||||
let mut state: anchor_lang::ProgramState<#state_ty> = anchor_lang::ProgramState::try_accounts(program_id, &mut remaining_accounts, &[])?;
|
||||
|
||||
// Deserialize accounts.
|
||||
let mut accounts = #anchor_ident::try_accounts(
|
||||
|
@ -550,7 +537,8 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
|
|||
|
||||
// Exit procedures.
|
||||
accounts.exit(program_id)?;
|
||||
let mut data = state_account.try_borrow_mut_data()?;
|
||||
let acc_info = state.to_account_info();
|
||||
let mut data = acc_info.try_borrow_mut_data()?;
|
||||
let dst: &mut [u8] = &mut data;
|
||||
let mut cursor = std::io::Cursor::new(dst);
|
||||
state.try_serialize(&mut cursor)?;
|
||||
|
|
Loading…
Reference in New Issue