misc.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. it("Can use u128 and i128", async () => {
  18. const data = new anchor.web3.Account();
  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 executable attribtue", async () => {
  43. await program.rpc.testExecutable({
  44. accounts: {
  45. program: program.programId,
  46. },
  47. });
  48. await assert.rejects(
  49. async () => {
  50. await program.rpc.testExecutable({
  51. accounts: {
  52. program: program.provider.wallet.publicKey,
  53. },
  54. });
  55. },
  56. (err) => {
  57. return true;
  58. }
  59. );
  60. });
  61. it("Can CPI to state instructions", async () => {
  62. const oldData = new anchor.BN(0);
  63. await misc2Program.state.rpc.new({
  64. accounts: {
  65. authority: program.provider.wallet.publicKey,
  66. },
  67. });
  68. let stateAccount = await misc2Program.state();
  69. assert.ok(stateAccount.data.eq(oldData));
  70. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  71. const newData = new anchor.BN(2134);
  72. await program.rpc.testStateCpi(newData, {
  73. accounts: {
  74. authority: program.provider.wallet.publicKey,
  75. cpiState: await misc2Program.state.address(),
  76. misc2Program: misc2Program.programId,
  77. },
  78. });
  79. stateAccount = await misc2Program.state();
  80. assert.ok(stateAccount.data.eq(newData));
  81. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  82. });
  83. });