idl.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { Buffer } from "buffer";
  2. import { PublicKey } from "@solana/web3.js";
  3. import * as borsh from "@project-serum/borsh";
  4. export type Idl = {
  5. version: string;
  6. name: string;
  7. docs?: string[];
  8. instructions: IdlInstruction[];
  9. state?: IdlState;
  10. accounts?: IdlAccountDef[];
  11. types?: IdlTypeDef[];
  12. events?: IdlEvent[];
  13. errors?: IdlErrorCode[];
  14. constants?: IdlConstant[];
  15. metadata?: IdlMetadata;
  16. };
  17. export type IdlMetadata = any;
  18. export type IdlConstant = {
  19. name: string;
  20. type: IdlType;
  21. value: string;
  22. };
  23. export type IdlEvent = {
  24. name: string;
  25. fields: IdlEventField[];
  26. };
  27. export type IdlEventField = {
  28. name: string;
  29. type: IdlType;
  30. index: boolean;
  31. };
  32. export type IdlInstruction = {
  33. name: string;
  34. docs?: string[];
  35. accounts: IdlAccountItem[];
  36. args: IdlField[];
  37. returns?: IdlType;
  38. };
  39. export type IdlState = {
  40. struct: IdlTypeDef;
  41. methods: IdlStateMethod[];
  42. };
  43. export type IdlStateMethod = IdlInstruction;
  44. export type IdlAccountItem = IdlAccount | IdlAccounts;
  45. export type IdlAccount = {
  46. name: string;
  47. isMut: boolean;
  48. isSigner: boolean;
  49. docs?: string[];
  50. pda?: IdlPda;
  51. };
  52. export type IdlPda = {
  53. seeds: IdlSeed[];
  54. programId?: IdlSeed;
  55. };
  56. export type IdlSeed = any; // TODO
  57. // A nested/recursive version of IdlAccount.
  58. export type IdlAccounts = {
  59. name: string;
  60. docs?: string[];
  61. accounts: IdlAccountItem[];
  62. };
  63. export type IdlField = {
  64. name: string;
  65. docs?: string[];
  66. type: IdlType;
  67. };
  68. export type IdlTypeDef = {
  69. name: string;
  70. docs?: string[];
  71. type: IdlTypeDefTy;
  72. };
  73. export type IdlAccountDef = {
  74. name: string;
  75. docs?: string[];
  76. type: IdlTypeDefTyStruct;
  77. };
  78. export type IdlTypeDefTyStruct = {
  79. kind: "struct";
  80. fields: IdlTypeDefStruct;
  81. };
  82. export type IdlTypeDefTyEnum = {
  83. kind: "enum";
  84. variants: IdlEnumVariant[];
  85. };
  86. export type IdlTypeDefTy = IdlTypeDefTyEnum | IdlTypeDefTyStruct;
  87. export type IdlTypeDefStruct = Array<IdlField>;
  88. export type IdlType =
  89. | "bool"
  90. | "u8"
  91. | "i8"
  92. | "u16"
  93. | "i16"
  94. | "u32"
  95. | "i32"
  96. | "f32"
  97. | "u64"
  98. | "i64"
  99. | "f64"
  100. | "u128"
  101. | "i128"
  102. | "bytes"
  103. | "string"
  104. | "publicKey"
  105. | IdlTypeDefined
  106. | IdlTypeOption
  107. | IdlTypeCOption
  108. | IdlTypeVec
  109. | IdlTypeArray;
  110. // User defined type.
  111. export type IdlTypeDefined = {
  112. defined: string;
  113. };
  114. export type IdlTypeOption = {
  115. option: IdlType;
  116. };
  117. export type IdlTypeCOption = {
  118. coption: IdlType;
  119. };
  120. export type IdlTypeVec = {
  121. vec: IdlType;
  122. };
  123. export type IdlTypeArray = {
  124. array: [idlType: IdlType, size: number];
  125. };
  126. export type IdlEnumVariant = {
  127. name: string;
  128. fields?: IdlEnumFields;
  129. };
  130. export type IdlEnumFields = IdlEnumFieldsNamed | IdlEnumFieldsTuple;
  131. export type IdlEnumFieldsNamed = IdlField[];
  132. export type IdlEnumFieldsTuple = IdlType[];
  133. export type IdlErrorCode = {
  134. code: number;
  135. name: string;
  136. msg?: string;
  137. };
  138. // Deterministic IDL address as a function of the program id.
  139. export async function idlAddress(programId: PublicKey): Promise<PublicKey> {
  140. const base = (await PublicKey.findProgramAddress([], programId))[0];
  141. return await PublicKey.createWithSeed(base, seed(), programId);
  142. }
  143. // Seed for generating the idlAddress.
  144. export function seed(): string {
  145. return "anchor:idl";
  146. }
  147. // The on-chain account of the IDL.
  148. export interface IdlProgramAccount {
  149. authority: PublicKey;
  150. data: Buffer;
  151. }
  152. const IDL_ACCOUNT_LAYOUT: borsh.Layout<IdlProgramAccount> = borsh.struct([
  153. borsh.publicKey("authority"),
  154. borsh.vecU8("data"),
  155. ]);
  156. export function decodeIdlAccount(data: Buffer): IdlProgramAccount {
  157. return IDL_ACCOUNT_LAYOUT.decode(data);
  158. }
  159. export function encodeIdlAccount(acc: IdlProgramAccount): Buffer {
  160. const buffer = Buffer.alloc(1000); // TODO: use a tighter buffer.
  161. const len = IDL_ACCOUNT_LAYOUT.encode(acc, buffer);
  162. return buffer.slice(0, len);
  163. }