idl.ts 2.8 KB

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