system_instruction.spec.ts 10 KB

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