create_contract.spec.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: Apache-2.0
  2. import { Connection, Keypair, PublicKey, sendAndConfirmTransaction, SystemProgram, Transaction } from '@solana/web3.js';
  3. import expect from 'expect';
  4. import { Program, Provider, BN } from '@coral-xyz/anchor';
  5. import { create_account, loadContractAndCallConstructor } from './setup';
  6. import fs from 'fs';
  7. describe('ChildContract', function () {
  8. this.timeout(150000);
  9. let program: Program;
  10. let storage: Keypair;
  11. let program_key: PublicKey;
  12. let payer: Keypair;
  13. let provider: Provider;
  14. before(async function () {
  15. ({ program, storage, payer, program_key, provider } = await loadContractAndCallConstructor('creator'));
  16. });
  17. it('Create Contract', async function () {
  18. let child_program = new PublicKey("Chi1d5XD6nTAp2EyaNGqMxZzUjh6NvhXRxbGHP3D1RaT");
  19. let child = Keypair.generate();
  20. const signature = await program.methods.createChild()
  21. .accounts({
  22. dataAccount: storage.publicKey,
  23. Child_programId: child_program,
  24. payer: payer.publicKey,
  25. Child_dataAccount: child.publicKey,
  26. })
  27. .signers([payer, child])
  28. .rpc({ commitment: 'confirmed' });
  29. const tx = await provider.connection.getTransaction(signature, { commitment: 'confirmed' });
  30. expect(tx?.meta?.logMessages!.toString()).toContain('In child constructor');
  31. expect(tx?.meta?.logMessages!.toString()).toContain('Hello there');
  32. const info = await provider.connection.getAccountInfo(child.publicKey);
  33. expect(info?.data.length).toEqual(518);
  34. });
  35. it('Creates Contract with seed1', async function () {
  36. let seed_program = new PublicKey("SeedHw4CsFsDEGu2AVwFM1toGXsbAJSKnb7kS8TrLxu");
  37. let seed = Buffer.from("chai");
  38. let [address, bump] = PublicKey.findProgramAddressSync([seed], seed_program);
  39. const signature = await program.methods.createSeed1(
  40. seed, Buffer.from([bump]), new BN(711))
  41. .accounts({
  42. Seed1_programId: seed_program,
  43. payer: payer.publicKey,
  44. Seed1_dataAccount: address,
  45. })
  46. .signers([payer])
  47. .rpc({ commitment: 'confirmed' });
  48. const tx = await provider.connection.getTransaction(signature, { commitment: 'confirmed' });
  49. const logs = tx?.meta?.logMessages!;
  50. expect(logs.toString()).toContain('In Seed1 constructor');
  51. expect(logs.toString()).toContain('Hello from Seed1');
  52. const info = await provider.connection.getAccountInfo(address);
  53. expect(info?.data.length).toEqual(711);
  54. const idl = JSON.parse(fs.readFileSync('Seed1.json', 'utf8'));
  55. const seed1 = new Program(idl, seed_program, provider);
  56. const res = await seed1.methods.sign()
  57. .accounts({ dataAccount: address, creator_program_id: program_key })
  58. .simulate();
  59. expect(res.raw.toString()).toContain('Signer found');
  60. });
  61. it('Creates Contract with seed2', async function () {
  62. let seed_program = new PublicKey("Seed23VDZ9HFCfKvFwmemB6dpi25n5XjZdP52B2RUmh");
  63. let bare_seed = Buffer.from("poppy");
  64. let [address, bump] = PublicKey.findProgramAddressSync([Buffer.from("sunflower"), bare_seed], seed_program);
  65. let seed = Buffer.concat([bare_seed, Buffer.from([bump])]);
  66. const signature = await program.methods.createSeed2(
  67. seed, new BN(9889))
  68. .accounts({
  69. Seed2_programId: seed_program,
  70. payer: payer.publicKey,
  71. Seed2_dataAccount: address,
  72. })
  73. .signers([payer])
  74. .rpc({ commitment: 'confirmed' });
  75. const tx = await provider.connection.getTransaction(signature, { commitment: 'confirmed' });
  76. const logs = tx?.meta?.logMessages!;
  77. expect(logs.toString()).toContain('In Seed2 constructor');
  78. const info = await provider.connection.getAccountInfo(address);
  79. expect(info?.data.length).toEqual(9889);
  80. const idl = JSON.parse(fs.readFileSync('Seed2.json', 'utf8'));
  81. const seed2 = new Program(idl, seed_program, provider);
  82. let res = await seed2.methods.check()
  83. .accounts({ dataAccount: address })
  84. .simulate();
  85. expect(res.raw.toString()).toContain('I am PDA.');
  86. res = await seed2.methods.sign()
  87. .accounts({ dataAccount: address, creator_program_id: program_key })
  88. .simulate();
  89. expect(res.raw.toString()).toContain('Signer found');
  90. const [pdaSigner, bump2] = PublicKey.findProgramAddressSync([Buffer.from("mint_authority")], seed_program);
  91. res = await seed2.methods.pdaSign(new BN(bump2))
  92. .accounts({pdaSigner: pdaSigner, creatorId: program_key})
  93. .simulate();
  94. expect(res.raw.toString()).toContain('Signer found');
  95. });
  96. it('Create Contract with account metas vector', async function () {
  97. let child = Keypair.generate();
  98. let child_program = new PublicKey("Chi1d5XD6nTAp2EyaNGqMxZzUjh6NvhXRxbGHP3D1RaT");
  99. const signature = await program.methods.createChildWithMetas()
  100. .accounts({
  101. child: child.publicKey,
  102. payer: payer.publicKey,
  103. Child_programId: child_program,
  104. })
  105. .signers([payer, child])
  106. .rpc({ commitment: 'confirmed' });
  107. const tx = await provider.connection.getTransaction(signature, { commitment: 'confirmed' });
  108. expect(tx?.meta?.logMessages!.toString()).toContain('In child constructor');
  109. expect(tx?.meta?.logMessages!.toString()).toContain('I am using metas');
  110. const info = await provider.connection.getAccountInfo(child.publicKey);
  111. expect(info?.data.length).toEqual(518);
  112. });
  113. it('Create Contract without annotations', async function () {
  114. const child = Keypair.generate();
  115. const child_program = new PublicKey("8gTkAidfM82u3DGbKcZpHwL5p47KQA16MDb4WmrHdmF6");
  116. await create_account(child, child_program, 8192);
  117. const signature = await program.methods.createWithoutAnnotation()
  118. .accounts({
  119. MyCreature_dataAccount: child.publicKey,
  120. MyCreature_programId: child_program,
  121. })
  122. .rpc({ commitment: 'confirmed' });
  123. const tx = await provider.connection.getTransaction(signature, {
  124. commitment: 'confirmed',
  125. maxSupportedTransactionVersion: undefined,
  126. });
  127. expect(tx?.meta?.logMessages!.toString()).toContain('In child constructor');
  128. expect(tx?.meta?.logMessages!.toString()).toContain('say_my_name');
  129. });
  130. });