system_instruction.spec.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // SPDX-License-Identifier: Apache-2.0
  2. import { loadContractAndCallConstructor } from "./setup";
  3. import { Keypair, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";
  4. import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
  5. import { BN } from '@coral-xyz/anchor';
  6. describe('Test system instructions', function () {
  7. this.timeout(500000);
  8. const system_account = new PublicKey('11111111111111111111111111111111');
  9. const recent_block_hashes = new PublicKey('SysvarRecentB1ockHashes11111111111111111111');
  10. const rentAddress = new PublicKey('SysvarRent111111111111111111111111111111111');
  11. const seed = 'my_seed_is_tea';
  12. it('create account', async function create_account() {
  13. const { program, storage, payer } = await loadContractAndCallConstructor('TestingInstruction');
  14. const to_key_pair = Keypair.generate();
  15. await program.methods.createAccount(
  16. new BN(100000000),
  17. new BN(5),
  18. TOKEN_PROGRAM_ID
  19. ).accounts(
  20. {
  21. from: payer.publicKey,
  22. to: to_key_pair.publicKey
  23. }
  24. )
  25. .remainingAccounts([
  26. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
  27. ])
  28. .signers([payer, to_key_pair]).rpc();
  29. });
  30. it('create account with seed', async function create_account_with_seed() {
  31. const { payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  32. const base_keypair = Keypair.generate();
  33. const to_key_pair = await PublicKey.createWithSeed(base_keypair.publicKey, seed, TOKEN_PROGRAM_ID);
  34. await program.methods.createAccountWithSeed(
  35. seed,
  36. new BN(100000000),
  37. new BN(5),
  38. TOKEN_PROGRAM_ID)
  39. .accounts({
  40. from: payer.publicKey,
  41. to: to_key_pair,
  42. base: base_keypair.publicKey
  43. })
  44. .remainingAccounts([
  45. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
  46. ])
  47. .signers([payer, base_keypair]).rpc();
  48. });
  49. it('assign', async function assign() {
  50. const { program } = await loadContractAndCallConstructor('TestingInstruction');
  51. const to_key_pair = Keypair.generate();
  52. const assign_account = new PublicKey('AddressLookupTab1e1111111111111111111111111');
  53. await program.methods.assign(
  54. assign_account)
  55. .accounts(
  56. {
  57. assignAccount: to_key_pair.publicKey,
  58. }
  59. )
  60. .signers([to_key_pair]).rpc();
  61. });
  62. it('assign with seed', async function assign_with_with_seed() {
  63. const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  64. const assign_account = new PublicKey('AddressLookupTab1e1111111111111111111111111');
  65. const to_key_pair = await PublicKey.createWithSeed(payer.publicKey, seed, assign_account);
  66. await program.methods.assignWithSeed(
  67. seed,
  68. assign_account)
  69. .accounts({
  70. assignAccount: to_key_pair,
  71. owner: payer.publicKey,
  72. })
  73. .signers([payer]).rpc();
  74. });
  75. it('transfer', async function transfer() {
  76. const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  77. const dest = new Keypair();
  78. await program.methods.transfer(
  79. new BN(100000000))
  80. .accounts({
  81. from: payer.publicKey,
  82. to: dest.publicKey,
  83. })
  84. .signers([payer]).rpc();
  85. });
  86. it('transfer with seed', async function transfer_with_seed() {
  87. const { payer, provider, program } = await loadContractAndCallConstructor('TestingInstruction');
  88. const dest = new Keypair();
  89. const assign_account = new PublicKey('AddressLookupTab1e1111111111111111111111111');
  90. const derived_payer = await PublicKey.createWithSeed(payer.publicKey, seed, assign_account);
  91. let signature = await provider.connection.requestAirdrop(derived_payer, LAMPORTS_PER_SOL);
  92. await provider.connection.confirmTransaction(signature, 'confirmed');
  93. await program.methods.transferWithSeed(
  94. seed, // seed
  95. assign_account, // from_owner
  96. new BN(100000000))
  97. .accounts(
  98. {
  99. fromKey: derived_payer,
  100. fromBase: payer.publicKey,
  101. toKey: dest.publicKey,
  102. }
  103. )
  104. .signers([payer]).rpc({commitment: "confirmed"});
  105. });
  106. it('allocate', async function allocate() {
  107. const { program } = await loadContractAndCallConstructor('TestingInstruction');
  108. const account = Keypair.generate();
  109. await program.methods.allocate(
  110. new BN(2))
  111. .accounts({accKey: account.publicKey})
  112. .signers([account]).rpc();
  113. });
  114. it('allocate with seed', async function allocate_with_seed() {
  115. const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  116. const account = Keypair.generate();
  117. const owner = new PublicKey('Stake11111111111111111111111111111111111111');
  118. const derived_key = await PublicKey.createWithSeed(account.publicKey, seed, owner);
  119. await program.methods.allocateWithSeed(
  120. seed,
  121. new BN(200),
  122. owner)
  123. .accounts({
  124. accKey: derived_key,
  125. base: account.publicKey,
  126. })
  127. .signers([account]).rpc();
  128. });
  129. it('create nonce account with seed', async function create_nonce_account_with_seed() {
  130. const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  131. const base_address = Keypair.generate();
  132. const derived_account = await PublicKey.createWithSeed(base_address.publicKey, seed, system_account);
  133. const authority = Keypair.generate();
  134. await program.methods.createNonceAccountWithSeed(
  135. seed,
  136. authority.publicKey,
  137. new BN(100000000))
  138. .accounts(
  139. {from: payer.publicKey,
  140. nonce: derived_account,
  141. base: base_address.publicKey}
  142. )
  143. .remainingAccounts([
  144. { pubkey: recent_block_hashes, isSigner: false, isWritable: false },
  145. { pubkey: rentAddress, isSigner: false, isWritable: false },
  146. ])
  147. .signers([payer, base_address]).rpc({commitment: "confirmed"});
  148. });
  149. it('nonce accounts', async function nonce_accounts() {
  150. const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  151. const nonce = Keypair.generate();
  152. const authority = Keypair.generate();
  153. await program.methods.createNonceAccount(
  154. authority.publicKey,
  155. new BN(100000000))
  156. .accounts(
  157. {
  158. from: payer.publicKey,
  159. nonce: nonce.publicKey
  160. }
  161. )
  162. .remainingAccounts([
  163. { pubkey: recent_block_hashes, isSigner: false, isWritable: false },
  164. { pubkey: rentAddress, isSigner: false, isWritable: false },
  165. ])
  166. .signers([payer, nonce]).rpc();
  167. await program.methods.advanceNonceAccount(authority.publicKey)
  168. .accounts({nonce: nonce.publicKey})
  169. .remainingAccounts([
  170. { pubkey: recent_block_hashes, isSigner: false, isWritable: false },
  171. { pubkey: authority.publicKey, isSigner: true, isWritable: false },
  172. ])
  173. .signers([authority]).rpc();
  174. await program.methods.withdrawNonceAccount(
  175. new BN(1000))
  176. .accounts({
  177. nonce: nonce.publicKey,
  178. to: payer.publicKey,
  179. authority: authority.publicKey
  180. })
  181. .remainingAccounts([
  182. { pubkey: recent_block_hashes, isSigner: false, isWritable: false },
  183. { pubkey: rentAddress, isSigner: false, isWritable: false },
  184. ])
  185. .signers([authority]).rpc();
  186. const new_authority = Keypair.generate();
  187. await program.methods.authorizeNonceAccount(
  188. new_authority.publicKey)
  189. .accounts({
  190. nonce: nonce.publicKey,
  191. authority: authority.publicKey
  192. })
  193. .signers([authority]).rpc();
  194. });
  195. });