idl.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { PublicKey } from "@solana/web3.js";
  2. import * as borsh from "@project-serum/borsh";
  3. export type Idl = {
  4. version: string;
  5. name: string;
  6. instructions: IdlInstruction[];
  7. state?: IdlState;
  8. accounts?: IdlTypeDef[];
  9. types?: IdlTypeDef[];
  10. errors?: IdlErrorCode[];
  11. };
  12. export type IdlInstruction = {
  13. name: string;
  14. accounts: IdlAccountItem[];
  15. args: IdlField[];
  16. };
  17. // IdlStateMethods are similar to instructions, except they only allow
  18. // for a single account, the state account.
  19. export type IdlState = {
  20. struct: IdlTypeDef;
  21. methods: IdlStateMethod[];
  22. };
  23. export type IdlStateMethod = IdlInstruction;
  24. export type IdlAccountItem = IdlAccount | IdlAccounts;
  25. export type IdlAccount = {
  26. name: string;
  27. isMut: boolean;
  28. isSigner: boolean;
  29. };
  30. // A nested/recursive version of IdlAccount.
  31. export type IdlAccounts = {
  32. name: string;
  33. accounts: IdlAccountItem[];
  34. };
  35. export type IdlField = {
  36. name: string;
  37. type: IdlType;
  38. };
  39. export type IdlTypeDef = {
  40. name: string;
  41. type: IdlTypeDefTy;
  42. };
  43. type IdlTypeDefTy = {
  44. kind: "struct" | "enum";
  45. fields?: IdlTypeDefStruct;
  46. variants?: IdlEnumVariant[];
  47. };
  48. type IdlTypeDefStruct = Array<IdlField>;
  49. export type IdlType =
  50. | "bool"
  51. | "u8"
  52. | "i8"
  53. | "u16"
  54. | "i16"
  55. | "u32"
  56. | "i32"
  57. | "u64"
  58. | "i64"
  59. | "bytes"
  60. | "string"
  61. | "publicKey"
  62. | IdlTypeVec
  63. | IdlTypeOption
  64. | IdlTypeDefined;
  65. export type IdlTypeVec = {
  66. vec: IdlType;
  67. };
  68. export type IdlTypeOption = {
  69. option: IdlType;
  70. };
  71. // User defined type.
  72. export type IdlTypeDefined = {
  73. defined: string;
  74. };
  75. export type IdlEnumVariant = {
  76. name: string;
  77. fields?: IdlEnumFields;
  78. };
  79. type IdlEnumFields = IdlEnumFieldsNamed | IdlEnumFieldsTuple;
  80. type IdlEnumFieldsNamed = IdlField[];
  81. type IdlEnumFieldsTuple = IdlType[];
  82. type IdlErrorCode = {
  83. code: number;
  84. name: string;
  85. msg?: string;
  86. };
  87. // Deterministic IDL address as a function of the program id.
  88. export async function idlAddress(programId: PublicKey): Promise<PublicKey> {
  89. const base = (await PublicKey.findProgramAddress([], programId))[0];
  90. return await PublicKey.createWithSeed(base, seed(), programId);
  91. }
  92. // Seed for generating the idlAddress.
  93. export function seed(): string {
  94. return "anchor:idl";
  95. }
  96. // The on-chain account of the IDL.
  97. export interface IdlProgramAccount {
  98. authority: PublicKey;
  99. data: Buffer;
  100. }
  101. const IDL_ACCOUNT_LAYOUT: borsh.Layout<IdlProgramAccount> = borsh.struct([
  102. borsh.publicKey("authority"),
  103. borsh.vecU8("data"),
  104. ]);
  105. export function decodeIdlAccount(data: Buffer): IdlProgramAccount {
  106. return IDL_ACCOUNT_LAYOUT.decode(data);
  107. }
  108. export function encodeIdlAccount(acc: IdlProgramAccount): Buffer {
  109. const buffer = Buffer.alloc(1000); // TODO: use a tighter buffer.
  110. const len = IDL_ACCOUNT_LAYOUT.encode(acc, buffer);
  111. return buffer.slice(0, len);
  112. }