main.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. use anchor_client::solana_sdk::commitment_config::CommitmentConfig;
  2. use anchor_client::solana_sdk::signature::read_keypair_file;
  3. use anchor_client::solana_sdk::signature::{Keypair, Signer};
  4. use anchor_client::solana_sdk::system_instruction;
  5. use anchor_client::solana_sdk::sysvar;
  6. use anchor_client::Client;
  7. use anyhow::Result;
  8. // The `accounts` and `instructions` modules are generated by the framework.
  9. use basic_2::accounts::CreateAuthor;
  10. use basic_2::instruction::Basic2Instruction;
  11. use basic_2::Author;
  12. // The `accounts` and `instructions` modules are generated by the framework.
  13. use composite::accounts::{Bar, CompositeUpdate, Foo, Initialize};
  14. use composite::instruction::CompositeInstruction;
  15. use composite::{DummyA, DummyB};
  16. use rand::rngs::OsRng;
  17. fn main() -> Result<()> {
  18. // Wallet and cluster params.
  19. let payer = read_keypair_file(&shellexpand::tilde("~/.config/solana/id.json"))
  20. .expect("Example requires a keypair file");
  21. let url = "http://localhost:8899";
  22. let opts = CommitmentConfig::recent();
  23. // Client.
  24. let client = Client::new_with_options(url, payer, opts);
  25. // Run tests.
  26. composite(&client)?;
  27. basic_2(&client)?;
  28. // Success.
  29. Ok(())
  30. }
  31. // Runs a client for examples/tutorial/composite.
  32. //
  33. // Make sure to run a localnet with the program deploy to run this example.
  34. fn composite(client: &Client) -> Result<()> {
  35. // Deployed program to execute.
  36. let pid = "75TykCe6b1oBa8JWVvfkXsFbZydgqi3QfRjgBEJJwy2g"
  37. .parse()
  38. .unwrap();
  39. // Program client.
  40. let program = client.program(pid);
  41. // `Initialize` parameters.
  42. let dummy_a = Keypair::generate(&mut OsRng);
  43. let dummy_b = Keypair::generate(&mut OsRng);
  44. // Build and send a transaction.
  45. program
  46. .request()
  47. .instruction(system_instruction::create_account(
  48. &program.payer(),
  49. &dummy_a.pubkey(),
  50. program.rpc().get_minimum_balance_for_rent_exemption(500)?,
  51. 500,
  52. &program.id(),
  53. ))
  54. .instruction(system_instruction::create_account(
  55. &program.payer(),
  56. &dummy_b.pubkey(),
  57. program.rpc().get_minimum_balance_for_rent_exemption(500)?,
  58. 500,
  59. &program.id(),
  60. ))
  61. .signer(&dummy_a)
  62. .signer(&dummy_b)
  63. .accounts(Initialize {
  64. dummy_a: dummy_a.pubkey(),
  65. dummy_b: dummy_b.pubkey(),
  66. rent: sysvar::rent::ID,
  67. })
  68. .args(CompositeInstruction::Initialize)
  69. .send()?;
  70. // Assert the transaction worked.
  71. let dummy_a_account: DummyA = program.account(dummy_a.pubkey())?;
  72. let dummy_b_account: DummyB = program.account(dummy_b.pubkey())?;
  73. assert_eq!(dummy_a_account.data, 0);
  74. assert_eq!(dummy_b_account.data, 0);
  75. // Build and send another transaction, using composite account parameters.
  76. program
  77. .request()
  78. .accounts(CompositeUpdate {
  79. foo: Foo {
  80. dummy_a: dummy_a.pubkey(),
  81. },
  82. bar: Bar {
  83. dummy_b: dummy_b.pubkey(),
  84. },
  85. })
  86. .args(CompositeInstruction::CompositeUpdate {
  87. dummy_a: 1234,
  88. dummy_b: 4321,
  89. })
  90. .send()?;
  91. // Assert the transaction worked.
  92. let dummy_a_account: DummyA = program.account(dummy_a.pubkey())?;
  93. let dummy_b_account: DummyB = program.account(dummy_b.pubkey())?;
  94. assert_eq!(dummy_a_account.data, 1234);
  95. assert_eq!(dummy_b_account.data, 4321);
  96. println!("Success!");
  97. Ok(())
  98. }
  99. // Runs a client for examples/tutorial/basic-2.
  100. //
  101. // Make sure to run a localnet with the program deploy to run this example.
  102. fn basic_2(client: &Client) -> Result<()> {
  103. // Deployed program to execute.
  104. let program_id = "FU3yvTEGTFUdMa6qAjVyKfNcDU6hb4yXbPhz8f5iFyvE"
  105. .parse()
  106. .unwrap();
  107. let program = client.program(program_id);
  108. // `CreateAuthor` parameters.
  109. let author = Keypair::generate(&mut OsRng);
  110. let authority = program.payer();
  111. // Build and send a transaction.
  112. program
  113. .request()
  114. .instruction(system_instruction::create_account(
  115. &authority,
  116. &author.pubkey(),
  117. program.rpc().get_minimum_balance_for_rent_exemption(500)?,
  118. 500,
  119. &program_id,
  120. ))
  121. .signer(&author)
  122. .accounts(CreateAuthor {
  123. author: author.pubkey(),
  124. rent: sysvar::rent::ID,
  125. })
  126. .args(Basic2Instruction::CreateAuthor {
  127. authority,
  128. name: "My Book Name".to_string(),
  129. })
  130. .send()?;
  131. let author_account: Author = program.account(author.pubkey())?;
  132. assert_eq!(author_account.authority, authority);
  133. assert_eq!(author_account.name, "My Book Name".to_string());
  134. println!("Success!");
  135. Ok(())
  136. }