123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- use anchor_client::solana_sdk::commitment_config::CommitmentConfig;
- use anchor_client::solana_sdk::signature::read_keypair_file;
- use anchor_client::solana_sdk::signature::{Keypair, Signer};
- use anchor_client::solana_sdk::system_instruction;
- use anchor_client::solana_sdk::sysvar;
- use anchor_client::Client;
- use anyhow::Result;
- // The `accounts` and `instructions` modules are generated by the framework.
- use basic_2::accounts::CreateAuthor;
- use basic_2::instruction::Basic2Instruction;
- use basic_2::Author;
- // The `accounts` and `instructions` modules are generated by the framework.
- use composite::accounts::{Bar, CompositeUpdate, Foo, Initialize};
- use composite::instruction::CompositeInstruction;
- use composite::{DummyA, DummyB};
- use rand::rngs::OsRng;
- fn main() -> Result<()> {
- // Wallet and cluster params.
- let payer = read_keypair_file(&shellexpand::tilde("~/.config/solana/id.json"))
- .expect("Example requires a keypair file");
- let url = "http://localhost:8899";
- let opts = CommitmentConfig::recent();
- // Client.
- let client = Client::new_with_options(url, payer, opts);
- // Run tests.
- composite(&client)?;
- basic_2(&client)?;
- // Success.
- Ok(())
- }
- // Runs a client for examples/tutorial/composite.
- //
- // Make sure to run a localnet with the program deploy to run this example.
- fn composite(client: &Client) -> Result<()> {
- // Deployed program to execute.
- let pid = "75TykCe6b1oBa8JWVvfkXsFbZydgqi3QfRjgBEJJwy2g"
- .parse()
- .unwrap();
- // Program client.
- let program = client.program(pid);
- // `Initialize` parameters.
- let dummy_a = Keypair::generate(&mut OsRng);
- let dummy_b = Keypair::generate(&mut OsRng);
- // Build and send a transaction.
- program
- .request()
- .instruction(system_instruction::create_account(
- &program.payer(),
- &dummy_a.pubkey(),
- program.rpc().get_minimum_balance_for_rent_exemption(500)?,
- 500,
- &program.id(),
- ))
- .instruction(system_instruction::create_account(
- &program.payer(),
- &dummy_b.pubkey(),
- program.rpc().get_minimum_balance_for_rent_exemption(500)?,
- 500,
- &program.id(),
- ))
- .signer(&dummy_a)
- .signer(&dummy_b)
- .accounts(Initialize {
- dummy_a: dummy_a.pubkey(),
- dummy_b: dummy_b.pubkey(),
- rent: sysvar::rent::ID,
- })
- .args(CompositeInstruction::Initialize)
- .send()?;
- // Assert the transaction worked.
- let dummy_a_account: DummyA = program.account(dummy_a.pubkey())?;
- let dummy_b_account: DummyB = program.account(dummy_b.pubkey())?;
- assert_eq!(dummy_a_account.data, 0);
- assert_eq!(dummy_b_account.data, 0);
- // Build and send another transaction, using composite account parameters.
- program
- .request()
- .accounts(CompositeUpdate {
- foo: Foo {
- dummy_a: dummy_a.pubkey(),
- },
- bar: Bar {
- dummy_b: dummy_b.pubkey(),
- },
- })
- .args(CompositeInstruction::CompositeUpdate {
- dummy_a: 1234,
- dummy_b: 4321,
- })
- .send()?;
- // Assert the transaction worked.
- let dummy_a_account: DummyA = program.account(dummy_a.pubkey())?;
- let dummy_b_account: DummyB = program.account(dummy_b.pubkey())?;
- assert_eq!(dummy_a_account.data, 1234);
- assert_eq!(dummy_b_account.data, 4321);
- println!("Success!");
- Ok(())
- }
- // Runs a client for examples/tutorial/basic-2.
- //
- // Make sure to run a localnet with the program deploy to run this example.
- fn basic_2(client: &Client) -> Result<()> {
- // Deployed program to execute.
- let program_id = "FU3yvTEGTFUdMa6qAjVyKfNcDU6hb4yXbPhz8f5iFyvE"
- .parse()
- .unwrap();
- let program = client.program(program_id);
- // `CreateAuthor` parameters.
- let author = Keypair::generate(&mut OsRng);
- let authority = program.payer();
- // Build and send a transaction.
- program
- .request()
- .instruction(system_instruction::create_account(
- &authority,
- &author.pubkey(),
- program.rpc().get_minimum_balance_for_rent_exemption(500)?,
- 500,
- &program_id,
- ))
- .signer(&author)
- .accounts(CreateAuthor {
- author: author.pubkey(),
- rent: sysvar::rent::ID,
- })
- .args(Basic2Instruction::CreateAuthor {
- authority,
- name: "My Book Name".to_string(),
- })
- .send()?;
- let author_account: Author = program.account(author.pubkey())?;
- assert_eq!(author_account.authority, authority);
- assert_eq!(author_account.name, "My Book Name".to_string());
- println!("Success!");
- Ok(())
- }
|