idl.ts 3.5 KB

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