system-accounts.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const anchor = require("@coral-xyz/anchor");
  2. const splToken = require("@solana/spl-token");
  3. const { assert } = require("chai");
  4. describe("system_accounts", () => {
  5. anchor.setProvider(anchor.AnchorProvider.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.strictEqual(err.error.errorMessage, errMsg);
  49. assert.strictEqual(err.error.errorCode.number, 3011);
  50. }
  51. });
  52. });