undelegate.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import * as beet from "@metaplex-foundation/beet";
  2. import * as web3 from "@solana/web3.js";
  3. import { DELEGATION_PROGRAM_ID, getDelegationAccounts } from "./accounts";
  4. import { PublicKey } from "@solana/web3.js";
  5. export const undelegateStruct = new beet.FixableBeetArgsStruct<{
  6. instructionDiscriminator: number[];
  7. }>(
  8. [["instructionDiscriminator", beet.uniformFixedSizeArray(beet.u8, 8)]],
  9. "UndelegateInstructionArgs"
  10. );
  11. /**
  12. * Accounts required by the _undelegate_ instruction
  13. */
  14. export interface UndelegateInstructionAccounts {
  15. payer: web3.PublicKey;
  16. delegatedAccount: web3.PublicKey;
  17. ownerProgram: web3.PublicKey;
  18. buffer?: web3.PublicKey;
  19. commitStatePda?: web3.PublicKey;
  20. commitStateRecordPda?: web3.PublicKey;
  21. delegationRecord?: web3.PublicKey;
  22. delegateAccountSeeds?: web3.PublicKey;
  23. reimbursement: web3.PublicKey;
  24. systemProgram?: web3.PublicKey;
  25. }
  26. export const undelegateInstructionDiscriminator = [3, 0, 0, 0, 0, 0, 0, 0];
  27. /**
  28. * Creates an _undelegate_ instruction.
  29. *
  30. */
  31. export function createUndelegateInstruction(
  32. accounts: UndelegateInstructionAccounts,
  33. programId = new PublicKey(DELEGATION_PROGRAM_ID)
  34. ) {
  35. const [data] = undelegateStruct.serialize({
  36. instructionDiscriminator: undelegateInstructionDiscriminator,
  37. });
  38. const {
  39. delegationPda,
  40. delegatedAccountSeedsPda,
  41. bufferPda,
  42. commitStateRecordPda,
  43. commitStatePda,
  44. } = getDelegationAccounts(
  45. accounts.delegatedAccount,
  46. accounts.ownerProgram,
  47. false
  48. );
  49. const keys: web3.AccountMeta[] = [
  50. {
  51. pubkey: accounts.payer,
  52. isWritable: false,
  53. isSigner: true,
  54. },
  55. {
  56. pubkey: accounts.delegatedAccount,
  57. isWritable: true,
  58. isSigner: false,
  59. },
  60. {
  61. pubkey: accounts.ownerProgram,
  62. isWritable: false,
  63. isSigner: false,
  64. },
  65. {
  66. pubkey: accounts.buffer ?? bufferPda,
  67. isWritable: true,
  68. isSigner: false,
  69. },
  70. {
  71. pubkey: accounts.commitStatePda ?? commitStatePda,
  72. isWritable: true,
  73. isSigner: false,
  74. },
  75. {
  76. pubkey: accounts.commitStateRecordPda ?? commitStateRecordPda,
  77. isWritable: true,
  78. isSigner: false,
  79. },
  80. {
  81. pubkey: accounts.delegationRecord ?? delegationPda,
  82. isWritable: true,
  83. isSigner: false,
  84. },
  85. {
  86. pubkey: accounts.delegateAccountSeeds ?? delegatedAccountSeedsPda,
  87. isWritable: true,
  88. isSigner: false,
  89. },
  90. {
  91. pubkey: accounts.reimbursement,
  92. isWritable: false,
  93. isSigner: false,
  94. },
  95. {
  96. pubkey: accounts.systemProgram ?? web3.SystemProgram.programId,
  97. isWritable: false,
  98. isSigner: false,
  99. },
  100. ];
  101. return new web3.TransactionInstruction({
  102. programId,
  103. keys,
  104. data,
  105. });
  106. }