anchor/client/example/src/main.rs

155 lines
4.7 KiB
Rust

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 as basic_2_accounts;
use basic_2::instruction as basic_2_instruction;
use basic_2::Counter;
// The `accounts` and `instructions` modules are generated by the framework.
use composite::accounts::{Bar, CompositeUpdate, Foo, Initialize};
use composite::instruction as composite_instruction;
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 = "CD4y4hpiqB9N3vo2bAmZofsZuFmCnScqDPXejZSTeCV9"
.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(composite_instruction::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(composite_instruction::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 = "DXfgYBD7A3DvFDJoCTcS81EnyxfwXyeYadH5VdKMhVEx"
.parse()
.unwrap();
let program = client.program(program_id);
// `Create` parameters.
let counter = Keypair::generate(&mut OsRng);
let authority = program.payer();
// Build and send a transaction.
program
.request()
.instruction(system_instruction::create_account(
&authority,
&counter.pubkey(),
program.rpc().get_minimum_balance_for_rent_exemption(500)?,
500,
&program_id,
))
.signer(&counter)
.accounts(basic_2_accounts::Create {
counter: counter.pubkey(),
rent: sysvar::rent::ID,
})
.args(basic_2_instruction::Create { authority })
.send()?;
let counter_account: Counter = program.account(counter.pubkey())?;
assert_eq!(counter_account.authority, authority);
assert_eq!(counter_account.count, 0);
println!("Success!");
Ok(())
}