anchor/client/example/src/main.rs

158 lines
4.8 KiB
Rust
Raw Normal View History

2021-01-29 08:02:34 -08:00
use anchor_client::solana_sdk::commitment_config::CommitmentConfig;
use anchor_client::solana_sdk::pubkey::Pubkey;
2021-01-29 08:02:34 -08:00
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;
2021-01-29 08:02:34 -08:00
// The `accounts` and `instructions` modules are generated by the framework.
use clap::Clap;
2021-01-29 08:02:34 -08:00
use composite::accounts::{Bar, CompositeUpdate, Foo, Initialize};
use composite::instruction as composite_instruction;
2021-01-29 08:02:34 -08:00
use composite::{DummyA, DummyB};
use rand::rngs::OsRng;
#[derive(Clap)]
pub struct Opts {
#[clap(long)]
composite_pid: Pubkey,
#[clap(long)]
basic_2_pid: Pubkey,
}
// This example assumes a local validator is running with the programs
// deployed at the addresses given by the CLI args.
2021-01-29 08:02:34 -08:00
fn main() -> Result<()> {
let opts = Opts::parse();
2021-01-29 08:02:34 -08:00
// Wallet and cluster params.
2021-02-26 02:43:14 -08:00
let payer = read_keypair_file(&*shellexpand::tilde("~/.config/solana/id.json"))
2021-01-29 08:02:34 -08:00
.expect("Example requires a keypair file");
let url = "http://localhost:8899";
// Client.
2021-02-26 02:43:14 -08:00
let client = Client::new_with_options(url, payer, CommitmentConfig::processed());
2021-01-29 08:02:34 -08:00
// Run tests.
composite(&client, opts.composite_pid)?;
basic_2(&client, opts.basic_2_pid)?;
2021-01-29 08:02:34 -08:00
// 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, pid: Pubkey) -> Result<()> {
2021-01-29 08:02:34 -08:00
// 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)
2021-01-29 08:02:34 -08:00
.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 {
2021-01-29 08:02:34 -08:00
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, pid: Pubkey) -> Result<()> {
let program = client.program(pid);
2021-01-29 08:02:34 -08:00
// `Create` parameters.
let counter = Keypair::generate(&mut OsRng);
2021-01-29 08:02:34 -08:00
let authority = program.payer();
// Build and send a transaction.
program
.request()
.instruction(system_instruction::create_account(
&authority,
&counter.pubkey(),
2021-01-29 08:02:34 -08:00
program.rpc().get_minimum_balance_for_rent_exemption(500)?,
500,
&pid,
2021-01-29 08:02:34 -08:00
))
.signer(&counter)
.accounts(basic_2_accounts::Create {
counter: counter.pubkey(),
2021-01-29 08:02:34 -08:00
rent: sysvar::rent::ID,
})
.args(basic_2_instruction::Create { authority })
2021-01-29 08:02:34 -08:00
.send()?;
let counter_account: Counter = program.account(counter.pubkey())?;
2021-01-29 08:02:34 -08:00
assert_eq!(counter_account.authority, authority);
assert_eq!(counter_account.count, 0);
2021-01-29 08:02:34 -08:00
println!("Success!");
Ok(())
}