Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧑‍💻 Improved program_client generated code #100

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions crates/client/src/program_client_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,22 @@ pub fn generate_source_code(idl: Idl, use_modules: &[syn::ItemUse]) -> String {
.iter()
.map(|(name, ty)| {
let name = format_ident!("a_{name}");
let ty: syn::Type = parse_str(ty).unwrap();
// do not use fully qualified type for Pubkey
let ty = parse_str(ty).unwrap();
let ty: syn::Type = match &ty {
syn::Type::Path(tp) => {
let last_type =
&tp.path.segments.last().unwrap().ident.to_string();
if last_type == "Pubkey" {
let t: syn::Type = parse_str(last_type).unwrap();
t
} else {
// we expect only Pubkey, but if it is something different, than return fully qualified type
ty
}
}
_ => ty,
};
let account: syn::FnArg = parse_quote!(#name: #ty);
account
})
Expand Down Expand Up @@ -83,7 +98,7 @@ pub fn generate_source_code(idl: Idl, use_modules: &[syn::ItemUse]) -> String {
#(#accounts,)*
signers: impl IntoIterator<Item = Keypair> + Send + 'static,
) -> Result<EncodedConfirmedTransactionWithStatusMeta, ClientError> {
Ok(client.send_instruction(
client.send_instruction(
PROGRAM_ID,
#module_name::instruction::#instruction_struct_name {
#(#field_parameters,)*
Expand All @@ -92,7 +107,7 @@ pub fn generate_source_code(idl: Idl, use_modules: &[syn::ItemUse]) -> String {
#(#field_accounts,)*
},
signers,
).await?)
).await
}
};

Expand Down
92 changes: 46 additions & 46 deletions crates/client/tests/test_data/expected_client_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ pub mod escrow_instruction {
client: &Client,
i_initializer_amount: u64,
i_taker_amount: u64,
a_initializer: anchor_lang::solana_program::pubkey::Pubkey,
a_initializer_deposit_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_initializer_receive_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_escrow_account: anchor_lang::solana_program::pubkey::Pubkey,
a_system_program: anchor_lang::solana_program::pubkey::Pubkey,
a_token_program: anchor_lang::solana_program::pubkey::Pubkey,
a_initializer: Pubkey,
a_initializer_deposit_token_account: Pubkey,
a_initializer_receive_token_account: Pubkey,
a_escrow_account: Pubkey,
a_system_program: Pubkey,
a_token_program: Pubkey,
signers: impl IntoIterator<Item = Keypair> + Send + 'static,
) -> Result<EncodedConfirmedTransactionWithStatusMeta, ClientError> {
Ok(client
client
.send_instruction(
PROGRAM_ID,
escrow::instruction::InitializeEscrow {
Expand All @@ -35,17 +35,17 @@ pub mod escrow_instruction {
},
signers,
)
.await?)
.await
}
pub fn initialize_escrow_ix(
i_initializer_amount: u64,
i_taker_amount: u64,
a_initializer: anchor_lang::solana_program::pubkey::Pubkey,
a_initializer_deposit_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_initializer_receive_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_escrow_account: anchor_lang::solana_program::pubkey::Pubkey,
a_system_program: anchor_lang::solana_program::pubkey::Pubkey,
a_token_program: anchor_lang::solana_program::pubkey::Pubkey,
a_initializer: Pubkey,
a_initializer_deposit_token_account: Pubkey,
a_initializer_receive_token_account: Pubkey,
a_escrow_account: Pubkey,
a_system_program: Pubkey,
a_token_program: Pubkey,
) -> Instruction {
Instruction {
program_id: PROGRAM_ID,
Expand All @@ -67,14 +67,14 @@ pub mod escrow_instruction {
}
pub async fn cancel_escrow(
client: &Client,
a_initializer: anchor_lang::solana_program::pubkey::Pubkey,
a_pda_deposit_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_pda_account: anchor_lang::solana_program::pubkey::Pubkey,
a_escrow_account: anchor_lang::solana_program::pubkey::Pubkey,
a_token_program: anchor_lang::solana_program::pubkey::Pubkey,
a_initializer: Pubkey,
a_pda_deposit_token_account: Pubkey,
a_pda_account: Pubkey,
a_escrow_account: Pubkey,
a_token_program: Pubkey,
signers: impl IntoIterator<Item = Keypair> + Send + 'static,
) -> Result<EncodedConfirmedTransactionWithStatusMeta, ClientError> {
Ok(client
client
.send_instruction(
PROGRAM_ID,
escrow::instruction::CancelEscrow {},
Expand All @@ -87,14 +87,14 @@ pub mod escrow_instruction {
},
signers,
)
.await?)
.await
}
pub fn cancel_escrow_ix(
a_initializer: anchor_lang::solana_program::pubkey::Pubkey,
a_pda_deposit_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_pda_account: anchor_lang::solana_program::pubkey::Pubkey,
a_escrow_account: anchor_lang::solana_program::pubkey::Pubkey,
a_token_program: anchor_lang::solana_program::pubkey::Pubkey,
a_initializer: Pubkey,
a_pda_deposit_token_account: Pubkey,
a_pda_account: Pubkey,
a_escrow_account: Pubkey,
a_token_program: Pubkey,
) -> Instruction {
Instruction {
program_id: PROGRAM_ID,
Expand All @@ -111,18 +111,18 @@ pub mod escrow_instruction {
}
pub async fn exchange(
client: &Client,
a_taker: anchor_lang::solana_program::pubkey::Pubkey,
a_taker_deposit_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_taker_receive_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_pda_deposit_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_initializer_receive_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_initializer_main_account: anchor_lang::solana_program::pubkey::Pubkey,
a_escrow_account: anchor_lang::solana_program::pubkey::Pubkey,
a_pda_account: anchor_lang::solana_program::pubkey::Pubkey,
a_token_program: anchor_lang::solana_program::pubkey::Pubkey,
a_taker: Pubkey,
a_taker_deposit_token_account: Pubkey,
a_taker_receive_token_account: Pubkey,
a_pda_deposit_token_account: Pubkey,
a_initializer_receive_token_account: Pubkey,
a_initializer_main_account: Pubkey,
a_escrow_account: Pubkey,
a_pda_account: Pubkey,
a_token_program: Pubkey,
signers: impl IntoIterator<Item = Keypair> + Send + 'static,
) -> Result<EncodedConfirmedTransactionWithStatusMeta, ClientError> {
Ok(client
client
.send_instruction(
PROGRAM_ID,
escrow::instruction::Exchange {},
Expand All @@ -139,18 +139,18 @@ pub mod escrow_instruction {
},
signers,
)
.await?)
.await
}
pub fn exchange_ix(
a_taker: anchor_lang::solana_program::pubkey::Pubkey,
a_taker_deposit_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_taker_receive_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_pda_deposit_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_initializer_receive_token_account: anchor_lang::solana_program::pubkey::Pubkey,
a_initializer_main_account: anchor_lang::solana_program::pubkey::Pubkey,
a_escrow_account: anchor_lang::solana_program::pubkey::Pubkey,
a_pda_account: anchor_lang::solana_program::pubkey::Pubkey,
a_token_program: anchor_lang::solana_program::pubkey::Pubkey,
a_taker: Pubkey,
a_taker_deposit_token_account: Pubkey,
a_taker_receive_token_account: Pubkey,
a_pda_deposit_token_account: Pubkey,
a_initializer_receive_token_account: Pubkey,
a_initializer_main_account: Pubkey,
a_escrow_account: Pubkey,
a_pda_account: Pubkey,
a_token_program: Pubkey,
) -> Instruction {
Instruction {
program_id: PROGRAM_ID,
Expand Down
Loading