create_contract.spec.ts 6.5 KB

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