create_contract.spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 payer: Keypair;
  12. let provider: Provider;
  13. before(async function () {
  14. ({ program, storage, payer, provider } = await loadContractAndCallConstructor('creator'));
  15. });
  16. it('Create Contract', async function () {
  17. let child_program = new PublicKey("Chi1d5XD6nTAp2EyaNGqMxZzUjh6NvhXRxbGHP3D1RaT");
  18. let child = Keypair.generate();
  19. const signature = await program.methods.createChild()
  20. .accounts({
  21. dataAccount: storage.publicKey,
  22. Child_programId: child_program,
  23. payer: payer.publicKey,
  24. Child_dataAccount: child.publicKey,
  25. })
  26. .signers([payer, child])
  27. .rpc({ commitment: 'confirmed' });
  28. const tx = await provider.connection.getTransaction(signature, { commitment: 'confirmed' });
  29. expect(tx?.meta?.logMessages!.toString()).toContain('In child constructor');
  30. expect(tx?.meta?.logMessages!.toString()).toContain('Hello there');
  31. const info = await provider.connection.getAccountInfo(child.publicKey);
  32. expect(info?.data.length).toEqual(518);
  33. });
  34. it('Creates Contract with seed1', async function () {
  35. let seed_program = new PublicKey("SeedHw4CsFsDEGu2AVwFM1toGXsbAJSKnb7kS8TrLxu");
  36. let seed = Buffer.from("chai");
  37. let [address, bump] = await PublicKey.findProgramAddress([seed], seed_program);
  38. const signature = await program.methods.createSeed1(
  39. seed, Buffer.from([bump]), new BN(711))
  40. .accounts({
  41. Seed1_programId: seed_program,
  42. payer: payer.publicKey,
  43. Seed1_dataAccount: address,
  44. })
  45. .signers([payer])
  46. .rpc({ commitment: 'confirmed' });
  47. const tx = await provider.connection.getTransaction(signature, { commitment: 'confirmed' });
  48. const logs = tx?.meta?.logMessages!;
  49. expect(logs.toString()).toContain('In Seed1 constructor');
  50. expect(logs.toString()).toContain('Hello from Seed1');
  51. const info = await provider.connection.getAccountInfo(address);
  52. expect(info?.data.length).toEqual(711);
  53. });
  54. it('Creates Contract with seed2', async function () {
  55. let seed_program = new PublicKey("Seed23VDZ9HFCfKvFwmemB6dpi25n5XjZdP52B2RUmh");
  56. let bare_seed = Buffer.from("poppy");
  57. let [address, bump] = await PublicKey.findProgramAddress([Buffer.from("sunflower"), bare_seed], seed_program);
  58. let seed = Buffer.concat([bare_seed, Buffer.from([bump])]);
  59. const signature = await program.methods.createSeed2(
  60. seed, new BN(9889))
  61. .accounts({
  62. Seed2_programId: seed_program,
  63. payer: payer.publicKey,
  64. Seed2_dataAccount: address,
  65. })
  66. .signers([payer])
  67. .rpc({ commitment: 'confirmed' });
  68. const tx = await provider.connection.getTransaction(signature, { commitment: 'confirmed' });
  69. const logs = tx?.meta?.logMessages!;
  70. expect(logs.toString()).toContain('In Seed2 constructor');
  71. const info = await provider.connection.getAccountInfo(address);
  72. expect(info?.data.length).toEqual(9889);
  73. const idl = JSON.parse(fs.readFileSync('Seed2.json', 'utf8'));
  74. const seed2 = new Program(idl, seed_program, provider);
  75. let res = await seed2.methods.check()
  76. .accounts({ dataAccount: address })
  77. .simulate();
  78. expect(res.raw.toString()).toContain('I am PDA.');
  79. });
  80. it('Create Contract with account metas vector', async function () {
  81. let child = Keypair.generate();
  82. let child_program = new PublicKey("Chi1d5XD6nTAp2EyaNGqMxZzUjh6NvhXRxbGHP3D1RaT");
  83. const signature = await program.methods.createChildWithMetas(child.publicKey, payer.publicKey)
  84. .accounts({
  85. dataAccount: storage.publicKey,
  86. Child_programId: child_program,
  87. })
  88. .remainingAccounts([
  89. { pubkey: child.publicKey, isSigner: true, isWritable: true },
  90. { pubkey: payer.publicKey, isSigner: true, isWritable: true },
  91. ])
  92. .signers([payer, child])
  93. .rpc({ commitment: 'confirmed' });
  94. const tx = await provider.connection.getTransaction(signature, { commitment: 'confirmed' });
  95. expect(tx?.meta?.logMessages!.toString()).toContain('In child constructor');
  96. expect(tx?.meta?.logMessages!.toString()).toContain('I am using metas');
  97. const info = await provider.connection.getAccountInfo(child.publicKey);
  98. expect(info?.data.length).toEqual(518);
  99. });
  100. it('Create Contract without annotations', async function () {
  101. const child = Keypair.generate();
  102. const child_program = new PublicKey("8gTkAidfM82u3DGbKcZpHwL5p47KQA16MDb4WmrHdmF6");
  103. await create_account(child, child_program, 8192);
  104. const signature = await program.methods.createWithoutAnnotation()
  105. .accounts({
  106. MyCreature_dataAccount: child.publicKey,
  107. MyCreature_programId: child_program,
  108. })
  109. .rpc({ commitment: 'confirmed' });
  110. const tx = await provider.connection.getTransaction(signature, {
  111. commitment: 'confirmed',
  112. maxSupportedTransactionVersion: undefined,
  113. });
  114. expect(tx?.meta?.logMessages!.toString()).toContain('In child constructor');
  115. expect(tx?.meta?.logMessages!.toString()).toContain('say_my_name');
  116. });
  117. });