system_instruction.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. payer.publicKey,
  17. to_key_pair.publicKey,
  18. new BN(100000000),
  19. new BN(5),
  20. TOKEN_PROGRAM_ID)
  21. .remainingAccounts([
  22. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
  23. { pubkey: payer.publicKey, isSigner: true, isWritable: false },
  24. { pubkey: to_key_pair.publicKey, isSigner: true, isWritable: true },
  25. ])
  26. .accounts({ dataAccount: storage.publicKey })
  27. .signers([payer, to_key_pair]).rpc();
  28. });
  29. it('create account with seed', async function create_account_with_seed() {
  30. const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  31. const base_keypair = Keypair.generate();
  32. const to_key_pair = await PublicKey.createWithSeed(base_keypair.publicKey, seed, TOKEN_PROGRAM_ID);
  33. await program.methods.createAccountWithSeed(
  34. payer.publicKey,
  35. to_key_pair,
  36. base_keypair.publicKey,
  37. seed,
  38. new BN(100000000),
  39. new BN(5),
  40. TOKEN_PROGRAM_ID)
  41. .remainingAccounts([
  42. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
  43. { pubkey: payer.publicKey, isSigner: true, isWritable: false },
  44. { pubkey: to_key_pair, isSigner: false, isWritable: true },
  45. { pubkey: base_keypair.publicKey, isSigner: true, isWritable: false },
  46. ])
  47. .accounts({ dataAccount: storage.publicKey })
  48. .signers([payer, base_keypair]).rpc();
  49. });
  50. it('assign', async function assign() {
  51. const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  52. const to_key_pair = Keypair.generate();
  53. const assign_account = new PublicKey('AddressLookupTab1e1111111111111111111111111');
  54. await program.methods.assign(
  55. to_key_pair.publicKey,
  56. assign_account)
  57. .remainingAccounts([
  58. { pubkey: payer.publicKey, isSigner: false, isWritable: false },
  59. { pubkey: to_key_pair.publicKey, isSigner: true, isWritable: true },
  60. ])
  61. .accounts({ dataAccount: storage.publicKey })
  62. .signers([payer, to_key_pair]).rpc();
  63. });
  64. it('assign with seed', async function assign_with_with_seed() {
  65. const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  66. const assign_account = new PublicKey('AddressLookupTab1e1111111111111111111111111');
  67. const to_key_pair = await PublicKey.createWithSeed(payer.publicKey, seed, assign_account);
  68. await program.methods.assignWithSeed(
  69. to_key_pair,
  70. payer.publicKey,
  71. seed,
  72. assign_account)
  73. .remainingAccounts([
  74. { pubkey: assign_account, isSigner: false, isWritable: false },
  75. { pubkey: payer.publicKey, isSigner: false, isWritable: false },
  76. { pubkey: to_key_pair, isSigner: false, isWritable: true },
  77. ])
  78. .accounts({ dataAccount: storage.publicKey })
  79. .signers([payer]).rpc();
  80. });
  81. it('transfer', async function transfer() {
  82. const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  83. const dest = new Keypair();
  84. await program.methods.transfer(
  85. payer.publicKey,
  86. dest.publicKey,
  87. new BN(100000000))
  88. .remainingAccounts([
  89. { pubkey: payer.publicKey, isSigner: false, isWritable: true },
  90. { pubkey: dest.publicKey, isSigner: false, isWritable: true },
  91. ])
  92. .accounts({ dataAccount: storage.publicKey })
  93. .signers([payer]).rpc();
  94. });
  95. it('transfer with seed', async function transfer_with_seed() {
  96. const { storage, payer, provider, program } = await loadContractAndCallConstructor('TestingInstruction');
  97. const dest = new Keypair();
  98. const assign_account = new PublicKey('AddressLookupTab1e1111111111111111111111111');
  99. const derived_payer = await PublicKey.createWithSeed(payer.publicKey, seed, assign_account);
  100. let signature = await provider.connection.requestAirdrop(derived_payer, LAMPORTS_PER_SOL);
  101. await provider.connection.confirmTransaction(signature, 'confirmed');
  102. await program.methods.transferWithSeed(
  103. derived_payer, // from_pubkey
  104. payer.publicKey, // from_base
  105. seed, // seed
  106. assign_account, // from_owner
  107. dest.publicKey, // to_pubkey
  108. new BN(100000000))
  109. .remainingAccounts([
  110. { pubkey: assign_account, isSigner: false, isWritable: false },
  111. { pubkey: derived_payer, isSigner: false, isWritable: true },
  112. { pubkey: dest.publicKey, isSigner: false, isWritable: true },
  113. { pubkey: payer.publicKey, isSigner: true, isWritable: false },
  114. ])
  115. .accounts({ dataAccount: storage.publicKey })
  116. .signers([payer]).rpc();
  117. });
  118. it('allocate', async function allocate() {
  119. const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  120. const account = Keypair.generate();
  121. await program.methods.allocate(
  122. account.publicKey,
  123. new BN(2))
  124. .remainingAccounts([
  125. { pubkey: account.publicKey, isSigner: true, isWritable: true },
  126. ])
  127. .accounts({ dataAccount: storage.publicKey })
  128. .signers([payer, account]).rpc();
  129. });
  130. it('allocate with seed', async function allocate_with_seed() {
  131. const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  132. const account = Keypair.generate();
  133. const owner = new PublicKey('Stake11111111111111111111111111111111111111');
  134. const derived_key = await PublicKey.createWithSeed(account.publicKey, seed, owner);
  135. await program.methods.allocateWithSeed(
  136. derived_key,
  137. account.publicKey,
  138. seed,
  139. new BN(200),
  140. owner)
  141. .remainingAccounts([
  142. { pubkey: owner, isSigner: false, isWritable: false },
  143. { pubkey: account.publicKey, isSigner: true, isWritable: false },
  144. { pubkey: derived_key, isSigner: false, isWritable: true },
  145. ])
  146. .accounts({ dataAccount: storage.publicKey })
  147. .signers([payer, account]).rpc();
  148. });
  149. it('create nonce account with seed', async function create_nonce_account_with_seed() {
  150. const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  151. const base_address = Keypair.generate();
  152. const derived_account = await PublicKey.createWithSeed(base_address.publicKey, seed, system_account);
  153. const authority = Keypair.generate();
  154. await program.methods.createNonceAccountWithSeed(
  155. payer.publicKey,
  156. derived_account,
  157. base_address.publicKey,
  158. seed,
  159. authority.publicKey,
  160. new BN(100000000))
  161. .remainingAccounts([
  162. { pubkey: recent_block_hashes, isSigner: false, isWritable: false },
  163. { pubkey: rentAddress, isSigner: false, isWritable: false },
  164. { pubkey: payer.publicKey, isSigner: false, isWritable: true },
  165. { pubkey: derived_account, isSigner: false, isWritable: true },
  166. { pubkey: base_address.publicKey, isSigner: true, isWritable: true },
  167. ])
  168. .accounts({ dataAccount: storage.publicKey })
  169. .signers([payer, base_address]).rpc();
  170. });
  171. it('nonce accounts', async function nonce_accounts() {
  172. const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
  173. const nonce = Keypair.generate();
  174. const authority = Keypair.generate();
  175. await program.methods.createNonceAccount(
  176. payer.publicKey,
  177. nonce.publicKey,
  178. authority.publicKey,
  179. new BN(100000000))
  180. .remainingAccounts([
  181. { pubkey: recent_block_hashes, isSigner: false, isWritable: false },
  182. { pubkey: rentAddress, isSigner: false, isWritable: false },
  183. { pubkey: payer.publicKey, isSigner: false, isWritable: true },
  184. { pubkey: nonce.publicKey, isSigner: true, isWritable: true },
  185. ])
  186. .accounts({ dataAccount: storage.publicKey })
  187. .signers([payer, nonce]).rpc();
  188. await program.methods.advanceNonceAccount(
  189. nonce.publicKey,
  190. authority.publicKey)
  191. .remainingAccounts([
  192. { pubkey: recent_block_hashes, isSigner: false, isWritable: false },
  193. { pubkey: authority.publicKey, isSigner: true, isWritable: false },
  194. { pubkey: nonce.publicKey, isSigner: false, isWritable: true },
  195. ])
  196. .accounts({ dataAccount: storage.publicKey })
  197. .signers([authority]).rpc();
  198. await program.methods.withdrawNonceAccount(
  199. nonce.publicKey,
  200. authority.publicKey,
  201. payer.publicKey,
  202. new BN(1000))
  203. .remainingAccounts([
  204. { pubkey: recent_block_hashes, isSigner: false, isWritable: false },
  205. { pubkey: rentAddress, isSigner: false, isWritable: false },
  206. { pubkey: authority.publicKey, isSigner: true, isWritable: false },
  207. { pubkey: nonce.publicKey, isSigner: false, isWritable: true },
  208. { pubkey: payer.publicKey, isSigner: false, isWritable: true },
  209. ])
  210. .accounts({ dataAccount: storage.publicKey })
  211. .signers([authority]).rpc();
  212. const new_authority = Keypair.generate();
  213. await program.methods.authorizeNonceAccount(
  214. nonce.publicKey,
  215. authority.publicKey,
  216. new_authority.publicKey)
  217. .remainingAccounts([
  218. { pubkey: authority.publicKey, isSigner: true, isWritable: false },
  219. { pubkey: nonce.publicKey, isSigner: false, isWritable: true },
  220. ])
  221. .accounts({ dataAccount: storage.publicKey })
  222. .signers([authority]).rpc();
  223. });
  224. });