misc.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const anchor = require("@project-serum/anchor");
  2. const serumCmn = require("@project-serum/common");
  3. const assert = require("assert");
  4. describe("misc", () => {
  5. // Configure the client to use the local cluster.
  6. anchor.setProvider(anchor.Provider.env());
  7. const program = anchor.workspace.Misc;
  8. const misc2Program = anchor.workspace.Misc2;
  9. it("Can allocate extra space for a state constructor", async () => {
  10. const tx = await program.state.rpc.new();
  11. const addr = await program.state.address();
  12. const state = await program.state();
  13. const accountInfo = await program.provider.connection.getAccountInfo(addr);
  14. assert.ok(state.v.equals(Buffer.from([])));
  15. assert.ok(accountInfo.data.length === 99);
  16. });
  17. const data = new anchor.web3.Account();
  18. it("Can use u128 and i128", async () => {
  19. const tx = await program.rpc.initialize(
  20. new anchor.BN(1234),
  21. new anchor.BN(22),
  22. {
  23. accounts: {
  24. data: data.publicKey,
  25. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  26. },
  27. signers: [data],
  28. instructions: [await program.account.data.createInstruction(data)],
  29. }
  30. );
  31. const dataAccount = await program.account.data(data.publicKey);
  32. assert.ok(dataAccount.udata.eq(new anchor.BN(1234)));
  33. assert.ok(dataAccount.idata.eq(new anchor.BN(22)));
  34. });
  35. it("Can embed programs into genesis from the Anchor.toml", async () => {
  36. const pid = new anchor.web3.PublicKey(
  37. "FtMNMKp9DZHKWUyVAsj3Q5QV8ow4P3fUPP7ZrWEQJzKr"
  38. );
  39. let accInfo = await anchor.getProvider().connection.getAccountInfo(pid);
  40. assert.ok(accInfo.executable);
  41. });
  42. it("Can use the owner constraint", async () => {
  43. await program.rpc.testOwner({
  44. accounts: {
  45. data: data.publicKey,
  46. misc: program.programId,
  47. },
  48. });
  49. await assert.rejects(
  50. async () => {
  51. await program.rpc.testOwner({
  52. accounts: {
  53. data: program.provider.wallet.publicKey,
  54. misc: program.programId,
  55. },
  56. });
  57. },
  58. (err) => {
  59. return true;
  60. }
  61. );
  62. });
  63. it("Can use the executable attribtue", async () => {
  64. await program.rpc.testExecutable({
  65. accounts: {
  66. program: program.programId,
  67. },
  68. });
  69. await assert.rejects(
  70. async () => {
  71. await program.rpc.testExecutable({
  72. accounts: {
  73. program: program.provider.wallet.publicKey,
  74. },
  75. });
  76. },
  77. (err) => {
  78. return true;
  79. }
  80. );
  81. });
  82. it("Can CPI to state instructions", async () => {
  83. const oldData = new anchor.BN(0);
  84. await misc2Program.state.rpc.new({
  85. accounts: {
  86. authority: program.provider.wallet.publicKey,
  87. },
  88. });
  89. let stateAccount = await misc2Program.state();
  90. assert.ok(stateAccount.data.eq(oldData));
  91. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  92. const newData = new anchor.BN(2134);
  93. await program.rpc.testStateCpi(newData, {
  94. accounts: {
  95. authority: program.provider.wallet.publicKey,
  96. cpiState: await misc2Program.state.address(),
  97. misc2Program: misc2Program.programId,
  98. },
  99. });
  100. stateAccount = await misc2Program.state();
  101. assert.ok(stateAccount.data.eq(newData));
  102. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  103. });
  104. });