system-accounts.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const anchor = require('@project-serum/anchor');
  2. const splToken = require('@solana/spl-token');
  3. const assert = require('assert');
  4. describe('system_accounts', () => {
  5. anchor.setProvider(anchor.Provider.local());
  6. const program = anchor.workspace.SystemAccounts;
  7. const authority = program.provider.wallet.payer;
  8. const wallet = anchor.web3.Keypair.generate();
  9. it('Is initialized!', async () => {
  10. const tx = await program.rpc.initialize({
  11. accounts: {
  12. authority: authority.publicKey,
  13. wallet: wallet.publicKey
  14. },
  15. signers: [authority]
  16. });
  17. console.log("Your transaction signature", tx);
  18. });
  19. it('Emits an AccountNotSystemOwned error', async () => {
  20. const mint = await splToken.Token.createMint(
  21. program.provider.connection,
  22. authority,
  23. authority.publicKey,
  24. null,
  25. 9,
  26. splToken.TOKEN_PROGRAM_ID,
  27. );
  28. const tokenAccount = await mint.createAssociatedTokenAccount(
  29. wallet.publicKey
  30. );
  31. await mint.mintTo(
  32. tokenAccount,
  33. authority.publicKey,
  34. [],
  35. 1 * anchor.web3.LAMPORTS_PER_SOL,
  36. );
  37. try {
  38. await program.rpc.initialize({
  39. accounts: {
  40. authority: authority.publicKey,
  41. wallet: tokenAccount
  42. },
  43. signers: [authority]
  44. })
  45. assert.ok(false);
  46. } catch (err) {
  47. const errMsg = 'The given account is not owned by the system program';
  48. assert.equal(err.toString(), errMsg);
  49. assert.equal(err.msg, errMsg);
  50. assert.equal(err.code, 171);
  51. }
  52. });
  53. });