undelegate.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import * as beet from "@metaplex-foundation/beet";
  2. import * as web3 from "@solana/web3.js";
  3. import {
  4. MAGIC_CONTEXT_ID,
  5. MAGIC_PROGRAM_ID,
  6. } from "@magicblock-labs/ephemeral-rollups-sdk";
  7. export const undelegateStruct = new beet.BeetArgsStruct<{
  8. instructionDiscriminator: number[] /* size: 8 */;
  9. }>(
  10. [["instructionDiscriminator", beet.uniformFixedSizeArray(beet.u8, 8)]],
  11. "undelegateInstructionArgs"
  12. );
  13. export interface UndelegateInstructionAccounts {
  14. payer: web3.PublicKey;
  15. delegatedAccount: web3.PublicKey;
  16. componentPda: web3.PublicKey;
  17. }
  18. export const undelegateInstructionDiscriminator = [
  19. 131, 148, 180, 198, 91, 104, 42, 238,
  20. ];
  21. /**
  22. * Creates an Undelegate instruction.
  23. */
  24. export function createUndelegateInstruction(
  25. accounts: UndelegateInstructionAccounts
  26. ) {
  27. const [data] = undelegateStruct.serialize({
  28. instructionDiscriminator: undelegateInstructionDiscriminator,
  29. });
  30. const keys: web3.AccountMeta[] = [
  31. {
  32. pubkey: accounts.payer,
  33. isWritable: true,
  34. isSigner: true,
  35. },
  36. {
  37. pubkey: accounts.delegatedAccount,
  38. isWritable: true,
  39. isSigner: false,
  40. },
  41. {
  42. pubkey: new web3.PublicKey(MAGIC_CONTEXT_ID),
  43. isWritable: true,
  44. isSigner: false,
  45. },
  46. {
  47. pubkey: new web3.PublicKey(MAGIC_PROGRAM_ID),
  48. isWritable: false,
  49. isSigner: false,
  50. },
  51. ];
  52. return new web3.TransactionInstruction({
  53. programId: accounts.componentPda,
  54. keys,
  55. data,
  56. });
  57. }