idl.ts 3.5 KB

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