Skip to content

Commit

Permalink
Pass serialized data to helper fns
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyera Eulberg committed Jun 29, 2020
1 parent 057b0a5 commit 07d8a07
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 69 deletions.
89 changes: 39 additions & 50 deletions sdk/program-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,18 @@ impl AccountDetails {
if self.is_optional {
quote! {
if let Some(#ident) = #ident {
account_metas.push(#account_meta_path(#ident, #is_signer));
accounts.push(#account_meta_path(#ident, #is_signer));
}
}
} else if self.allows_multiple {
quote! {
for pubkey in #ident.into_iter() {
account_metas.push(#account_meta_path(pubkey, #is_signer));
accounts.push(#account_meta_path(pubkey, #is_signer));
}
}
} else {
quote! {
account_metas.push(#account_meta_path(#ident, #is_signer));
accounts.push(#account_meta_path(#ident, #is_signer));
}
}
}
Expand Down Expand Up @@ -269,7 +269,6 @@ fn build_helper_fns(
program_id: ProgramId,
) -> proc_macro2::TokenStream {
let mut stream = proc_macro2::TokenStream::new();
let ident = &program_details.instruction_enum.ident;
for (variant, variant_details) in program_details
.instruction_enum
.variants
Expand All @@ -281,13 +280,11 @@ fn build_helper_fns(
&variant.ident.to_string().to_snake_case(),
Span::call_site(),
);
let variant_ident = &variant.ident;
let mut fields: Punctuated<Ident, Comma> = Punctuated::new();
let mut args: Punctuated<FnArg, Comma> = Punctuated::new();

let mut accounts_stream = proc_macro2::TokenStream::new();
accounts_stream.extend(quote! {
let mut account_metas: Vec<::solana_sdk::instruction::AccountMeta> = vec![];
let mut accounts: Vec<::solana_sdk::instruction::AccountMeta> = vec![];
});
for account in &variant_details.account_details {
let account_meta = account.format_account_meta();
Expand All @@ -299,27 +296,19 @@ fn build_helper_fns(
args.push(account.format_arg());
}

for field in variant.fields.iter() {
let ident = field.ident.as_ref().unwrap();
fields.push(ident.clone());

if let Type::Path(path) = &field.ty {
let arg: FnArg = parse_quote!(#ident: #path);
args.push(arg);
}
}

stream.extend(
quote!( pub fn #fn_ident( #args ) -> ::solana_sdk::instruction::Instruction {
#accounts_stream
::solana_sdk::instruction::Instruction::new(
#program_id,
&#ident::#variant_ident{ #fields },
account_metas,
)
stream.extend(quote!(
pub fn #fn_ident(
#args,
data: Vec<u8>,
) -> ::solana_sdk::instruction::Instruction {
#accounts_stream
::solana_sdk::instruction::Instruction {
program_id: #program_id,
data,
accounts,
}
),
);
}
));
}
}
stream
Expand Down Expand Up @@ -488,31 +477,31 @@ mod tests {
let account_meta = account_details.format_account_meta();
let account_meta: Stmt = parse_quote!(#account_meta);
let expected_account_meta: Stmt = parse_quote!(
account_metas.push(::solana_sdk::instruction::AccountMeta::new_readonly(test, false));
accounts.push(::solana_sdk::instruction::AccountMeta::new_readonly(test, false));
);
assert_eq!(account_meta, expected_account_meta);

account_details.is_signer = true;
let account_meta = account_details.format_account_meta();
let account_meta: Stmt = parse_quote!(#account_meta);
let expected_account_meta: Stmt = parse_quote!(
account_metas.push(::solana_sdk::instruction::AccountMeta::new_readonly(test, true));
accounts.push(::solana_sdk::instruction::AccountMeta::new_readonly(test, true));
);
assert_eq!(account_meta, expected_account_meta);

account_details.is_writable = true;
let account_meta = account_details.format_account_meta();
let account_meta: Stmt = parse_quote!(#account_meta);
let expected_account_meta: Stmt = parse_quote!(
account_metas.push(::solana_sdk::instruction::AccountMeta::new(test, true));
accounts.push(::solana_sdk::instruction::AccountMeta::new(test, true));
);
assert_eq!(account_meta, expected_account_meta);

account_details.is_optional = true;
let account_meta = account_details.format_account_meta();
let account_meta: Stmt = parse_quote!(#account_meta);
let expected_account_meta: Stmt = parse_quote!(if let Some(test) = test {
account_metas.push(::solana_sdk::instruction::AccountMeta::new(test, true));
accounts.push(::solana_sdk::instruction::AccountMeta::new(test, true));
});
assert_eq!(account_meta, expected_account_meta);

Expand All @@ -521,7 +510,7 @@ mod tests {
let account_meta = account_details.format_account_meta();
let account_meta: Stmt = parse_quote!(#account_meta);
let expected_account_meta: Stmt = parse_quote!(for pubkey in test.into_iter() {
account_metas.push(::solana_sdk::instruction::AccountMeta::new(pubkey, true));
accounts.push(::solana_sdk::instruction::AccountMeta::new(pubkey, true));
});
assert_eq!(account_meta, expected_account_meta);
}
Expand Down Expand Up @@ -663,39 +652,39 @@ mod tests {
pub fn test(
test_account: ::solana_sdk::pubkey::Pubkey,
another: ::solana_sdk::pubkey::Pubkey,
lamports: u64,
data: Vec<u8>,
) -> ::solana_sdk::instruction::Instruction {
let mut account_metas: Vec<::solana_sdk::instruction::AccountMeta> = vec![];
account_metas.push(
let mut accounts: Vec<::solana_sdk::instruction::AccountMeta> = vec![];
accounts.push(
::solana_sdk::instruction::AccountMeta::new(test_account, true)
);
if let Some(another) = another {
account_metas.push(
accounts.push(
::solana_sdk::instruction::AccountMeta::new_readonly(another, false)
);
}
::solana_sdk::instruction::Instruction::new(
program::id(),
&TestInstruction::Transfer{lamports,},
account_metas,
)
::solana_sdk::instruction::Instruction {
program_id: program::id(),
data,
accounts,
}
}

pub fn multiple(
signers: Vec<::solana_sdk::pubkey::Pubkey>
signers: Vec<::solana_sdk::pubkey::Pubkey>,
data: Vec<u8>,
) -> ::solana_sdk::instruction::Instruction {
let mut account_metas: Vec<::solana_sdk::instruction::AccountMeta> = vec![];
let mut accounts: Vec<::solana_sdk::instruction::AccountMeta> = vec![];
for pubkey in signers.into_iter() {
account_metas.push(
accounts.push(
::solana_sdk::instruction::AccountMeta::new_readonly(pubkey, true)
);
}

::solana_sdk::instruction::Instruction::new(
program::id(),
&TestInstruction::Multiple{},
account_metas,
)
::solana_sdk::instruction::Instruction {
program_id: program::id(),
data,
accounts,
}
}
};
assert_eq!(helper_fns[1], expected_fns[1]);
Expand Down
3 changes: 1 addition & 2 deletions sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,12 @@ extern crate solana_sdk_macro_frozen_abi;
/// pub fn transfer(
/// from_account: Pubkey,
/// to_account: Pubkey,
/// lamports: u64
/// data: Vec<u8>,
/// ) -> Instruction {
/// let mut accounts: Vec<AccountMeta> = vec![];
/// accounts.push(AccountMeta::new(from_account, true));
/// accounts.push(AccountMeta::new(to_account, false));
///
/// let data = serialize(&TestInstruction::Transfer{lamports}).unwrap();
/// Instruction {
/// program_id: test_program::id(),
/// data,
Expand Down
44 changes: 27 additions & 17 deletions sdk/tests/program_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ fn test_helper_fns() {
let pubkey1 = Pubkey::new_rand();
let pubkey2 = Pubkey::new_rand();

let transfer_data = serialize(&TestInstruction::Transfer { lamports: 42 }).unwrap();
assert_eq!(
transfer(pubkey0, pubkey1, 42),
transfer(pubkey0, pubkey1, transfer_data.clone()),
Instruction {
program_id: test_program::id(),
accounts: vec![
Expand All @@ -66,12 +67,17 @@ fn test_helper_fns() {
is_writable: true,
}
],
data: serialize(&TestInstruction::Transfer { lamports: 42 }).unwrap(),
data: transfer_data,
}
);

let multiple_accounts_data = serialize(&TestInstruction::MultipleAccounts).unwrap();
assert_eq!(
multiple_accounts(pubkey0, vec![pubkey1, pubkey2]),
multiple_accounts(
pubkey0,
vec![pubkey1, pubkey2],
multiple_accounts_data.clone()
),
Instruction {
program_id: test_program::id(),
accounts: vec![
Expand All @@ -91,27 +97,31 @@ fn test_helper_fns() {
is_writable: false,
}
],
data: serialize(&TestInstruction::MultipleAccounts).unwrap(),
data: multiple_accounts_data.clone(),
}
);

assert_eq!(
multiple_accounts(pubkey0, vec![]),
multiple_accounts(pubkey0, vec![], multiple_accounts_data.clone()),
Instruction {
program_id: test_program::id(),
accounts: vec![
AccountMeta {
pubkey: pubkey0,
is_signer: false,
is_writable: true,
}
],
data: serialize(&TestInstruction::MultipleAccounts).unwrap(),
accounts: vec![AccountMeta {
pubkey: pubkey0,
is_signer: false,
is_writable: true,
}],
data: multiple_accounts_data,
}
);

let optional_account_data = serialize(&TestInstruction::OptionalAccount).unwrap();
assert_eq!(
optional_account(pubkey0, pubkey1, Some(pubkey2)),
optional_account(
pubkey0,
pubkey1,
Some(pubkey2),
optional_account_data.clone()
),
Instruction {
program_id: test_program::id(),
accounts: vec![
Expand All @@ -131,12 +141,12 @@ fn test_helper_fns() {
is_writable: false,
}
],
data: serialize(&TestInstruction::OptionalAccount).unwrap(),
data: optional_account_data.clone(),
}
);

assert_eq!(
optional_account(pubkey0, pubkey1, None),
optional_account(pubkey0, pubkey1, None, optional_account_data.clone()),
Instruction {
program_id: test_program::id(),
accounts: vec![
Expand All @@ -151,7 +161,7 @@ fn test_helper_fns() {
is_writable: false,
}
],
data: serialize(&TestInstruction::OptionalAccount).unwrap(),
data: optional_account_data,
}
);
}
Expand Down

0 comments on commit 07d8a07

Please sign in to comment.