delegate.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import * as beet from "@metaplex-foundation/beet";
  2. import * as web3 from "@solana/web3.js";
  3. import {
  4. DelegateAccounts,
  5. DELEGATION_PROGRAM_ID,
  6. } from "@magicblock-labs/delegation-program";
  7. export interface DelegateInstructionArgs {
  8. validUntil: beet.bignum;
  9. commitFrequencyMs: number;
  10. }
  11. export const delegateStruct = new beet.FixableBeetArgsStruct<
  12. DelegateInstructionArgs & {
  13. instructionDiscriminator: number[] /* size: 8 */;
  14. }
  15. >(
  16. [
  17. ["instructionDiscriminator", beet.uniformFixedSizeArray(beet.u8, 8)],
  18. ["validUntil", beet.i64],
  19. ["commitFrequencyMs", beet.u32],
  20. ],
  21. "DelegateInstructionArgs"
  22. );
  23. /**
  24. * Accounts required by the _delegate_ instruction
  25. *
  26. */
  27. export interface DelegateInstructionAccounts {
  28. payer: web3.PublicKey;
  29. entity: web3.PublicKey;
  30. account: web3.PublicKey;
  31. ownerProgram: web3.PublicKey;
  32. buffer?: web3.PublicKey;
  33. delegationRecord?: web3.PublicKey;
  34. delegationMetadata?: web3.PublicKey;
  35. delegationProgram?: web3.PublicKey;
  36. systemProgram?: web3.PublicKey;
  37. }
  38. export const delegateInstructionDiscriminator = [
  39. 90, 147, 75, 178, 85, 88, 4, 137,
  40. ];
  41. /**
  42. * Creates a Delegate instruction.
  43. */
  44. export function createDelegateInstruction(
  45. accounts: DelegateInstructionAccounts,
  46. validUntil: beet.bignum = 0,
  47. commitFrequencyMs: number = 30000,
  48. programId = accounts.ownerProgram
  49. ) {
  50. const [data] = delegateStruct.serialize({
  51. instructionDiscriminator: delegateInstructionDiscriminator,
  52. validUntil,
  53. commitFrequencyMs,
  54. });
  55. const { delegationPda, delegationMetadata, bufferPda } = DelegateAccounts(
  56. accounts.account,
  57. accounts.ownerProgram
  58. );
  59. const keys: web3.AccountMeta[] = [
  60. {
  61. pubkey: accounts.payer,
  62. isWritable: false,
  63. isSigner: true,
  64. },
  65. {
  66. pubkey: accounts.entity,
  67. isWritable: false,
  68. isSigner: false,
  69. },
  70. {
  71. pubkey: accounts.account,
  72. isWritable: true,
  73. isSigner: false,
  74. },
  75. {
  76. pubkey: accounts.ownerProgram,
  77. isWritable: false,
  78. isSigner: false,
  79. },
  80. {
  81. pubkey: accounts.buffer ?? bufferPda,
  82. isWritable: true,
  83. isSigner: false,
  84. },
  85. {
  86. pubkey: accounts.delegationRecord ?? delegationPda,
  87. isWritable: true,
  88. isSigner: false,
  89. },
  90. {
  91. pubkey: accounts.delegationMetadata ?? delegationMetadata,
  92. isWritable: true,
  93. isSigner: false,
  94. },
  95. {
  96. pubkey:
  97. accounts.delegationProgram ?? new web3.PublicKey(DELEGATION_PROGRAM_ID),
  98. isWritable: false,
  99. isSigner: false,
  100. },
  101. {
  102. pubkey: accounts.systemProgram ?? web3.SystemProgram.programId,
  103. isWritable: false,
  104. isSigner: false,
  105. },
  106. ];
  107. return new web3.TransactionInstruction({
  108. programId,
  109. keys,
  110. data,
  111. });
  112. }