test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {
  2. Connection,
  3. Keypair,
  4. LAMPORTS_PER_SOL,
  5. PublicKey,
  6. sendAndConfirmTransaction,
  7. SystemProgram,
  8. Transaction,
  9. TransactionInstruction,
  10. } from '@solana/web3.js';
  11. function createKeypairFromFile(path: string): Keypair {
  12. return Keypair.fromSecretKey(
  13. Buffer.from(JSON.parse(require('fs').readFileSync(path, "utf-8")))
  14. )
  15. };
  16. describe("Change an account's owner", async () => {
  17. const connection = new Connection(`http://localhost:8899`, 'confirmed');
  18. const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
  19. const PROGRAM_ID: PublicKey = new PublicKey(
  20. "Au21huMZuDQrbzu2Ec5ohpW5CKRqhcGV6qLawfydStGs"
  21. );
  22. const newKeypair = Keypair.generate();
  23. it("Create the account", async () => {
  24. let ix = SystemProgram.createAccount({
  25. fromPubkey: payer.publicKey,
  26. newAccountPubkey: newKeypair.publicKey,
  27. lamports: 1 * LAMPORTS_PER_SOL,
  28. space: 0,
  29. programId: SystemProgram.programId,
  30. });
  31. await sendAndConfirmTransaction(
  32. connection,
  33. new Transaction().add(ix),
  34. [payer, newKeypair]
  35. );
  36. });
  37. it("Change ownership for the account", async () => {
  38. const newKeypair = Keypair.generate();
  39. let ix = SystemProgram.assign({
  40. accountPubkey: newKeypair.publicKey,
  41. programId: PROGRAM_ID
  42. })
  43. await sendAndConfirmTransaction(
  44. connection,
  45. new Transaction().add(ix),
  46. [payer, newKeypair]
  47. );
  48. });
  49. it("Change it again using the System Program", async () => {
  50. const newKeypair = Keypair.generate();
  51. let ix = SystemProgram.assign({
  52. accountPubkey: newKeypair.publicKey,
  53. programId: SystemProgram.programId,
  54. })
  55. await sendAndConfirmTransaction(
  56. connection,
  57. new Transaction().add(ix),
  58. [payer, newKeypair]
  59. );
  60. });
  61. });