Browse Source

Improve Kinobi IDL

Loris Leiva 1 year ago
parent
commit
d7df738f64

+ 1 - 1
clients/js/src/generated/accounts/index.ts

@@ -6,6 +6,6 @@
  * @see https://github.com/metaplex-foundation/kinobi
  */
 
-export * from './account';
 export * from './mint';
 export * from './multisig';
+export * from './token';

+ 18 - 0
clients/js/src/generated/accounts/mint.ts

@@ -42,18 +42,36 @@ import {
 } from '@solana/web3.js';
 
 export type Mint = {
+  /**
+   * Optional authority used to mint new tokens. The mint authority may only
+   * be provided during mint creation. If no mint authority is present
+   * then the mint has a fixed supply and no further tokens may be minted.
+   */
   mintAuthority: Option<Address>;
+  /** Total supply of tokens. */
   supply: bigint;
+  /** Number of base 10 digits to the right of the decimal place. */
   decimals: number;
+  /** Is `true` if this structure has been initialized. */
   isInitialized: boolean;
+  /** Optional authority to freeze token accounts. */
   freezeAuthority: Option<Address>;
 };
 
 export type MintArgs = {
+  /**
+   * Optional authority used to mint new tokens. The mint authority may only
+   * be provided during mint creation. If no mint authority is present
+   * then the mint has a fixed supply and no further tokens may be minted.
+   */
   mintAuthority: OptionOrNullable<Address>;
+  /** Total supply of tokens. */
   supply: number | bigint;
+  /** Number of base 10 digits to the right of the decimal place. */
   decimals: number;
+  /** Is `true` if this structure has been initialized. */
   isInitialized: boolean;
+  /** Optional authority to freeze token accounts. */
   freezeAuthority: OptionOrNullable<Address>;
 };
 

+ 4 - 0
clients/js/src/generated/accounts/multisig.ts

@@ -36,9 +36,13 @@ import {
 } from '@solana/web3.js';
 
 export type Multisig = {
+  /** Number of signers required. */
   m: number;
+  /** Number of valid signers. */
   n: number;
+  /** Is `true` if this structure has been initialized. */
   isInitialized: boolean;
+  /** Signer public keys. */
   signers: Array<Address>;
 };
 

+ 66 - 34
clients/js/src/generated/accounts/account.ts → clients/js/src/generated/accounts/token.ts

@@ -37,35 +37,67 @@ import {
   getU64Encoder,
 } from '@solana/web3.js';
 import {
-  TokenState,
-  TokenStateArgs,
-  getTokenStateDecoder,
-  getTokenStateEncoder,
+  AccountState,
+  AccountStateArgs,
+  getAccountStateDecoder,
+  getAccountStateEncoder,
 } from '../types';
 
-export type Account = {
+export type Token = {
+  /** The mint associated with this account. */
   mint: Address;
+  /** The owner of this account. */
   owner: Address;
+  /** The amount of tokens this account holds. */
   amount: bigint;
+  /**
+   * If `delegate` is `Some` then `delegated_amount` represents
+   * the amount authorized by the delegate.
+   */
   delegate: Option<Address>;
-  state: TokenState;
+  /** The account's state. */
+  state: AccountState;
+  /**
+   * If is_native.is_some, this is a native token, and the value logs the
+   * rent-exempt reserve. An Account is required to be rent-exempt, so
+   * the value is used by the Processor to ensure that wrapped SOL
+   * accounts do not drop below this threshold.
+   */
   isNative: Option<bigint>;
+  /** The amount delegated. */
   delegatedAmount: bigint;
+  /** Optional authority to close the account. */
   closeAuthority: Option<Address>;
 };
 
-export type AccountArgs = {
+export type TokenArgs = {
+  /** The mint associated with this account. */
   mint: Address;
+  /** The owner of this account. */
   owner: Address;
+  /** The amount of tokens this account holds. */
   amount: number | bigint;
+  /**
+   * If `delegate` is `Some` then `delegated_amount` represents
+   * the amount authorized by the delegate.
+   */
   delegate: OptionOrNullable<Address>;
-  state: TokenStateArgs;
+  /** The account's state. */
+  state: AccountStateArgs;
+  /**
+   * If is_native.is_some, this is a native token, and the value logs the
+   * rent-exempt reserve. An Account is required to be rent-exempt, so
+   * the value is used by the Processor to ensure that wrapped SOL
+   * accounts do not drop below this threshold.
+   */
   isNative: OptionOrNullable<number | bigint>;
+  /** The amount delegated. */
   delegatedAmount: number | bigint;
+  /** Optional authority to close the account. */
   closeAuthority: OptionOrNullable<Address>;
 };
 
-export function getAccountEncoder(): Encoder<AccountArgs> {
+export function getTokenEncoder(): Encoder<TokenArgs> {
   return getStructEncoder([
     ['mint', getAddressEncoder()],
     ['owner', getAddressEncoder()],
@@ -77,7 +109,7 @@ export function getAccountEncoder(): Encoder<AccountArgs> {
         fixed: true,
       }),
     ],
-    ['state', getTokenStateEncoder()],
+    ['state', getAccountStateEncoder()],
     [
       'isNative',
       getOptionEncoder(getU64Encoder(), {
@@ -96,7 +128,7 @@ export function getAccountEncoder(): Encoder<AccountArgs> {
   ]);
 }
 
-export function getAccountDecoder(): Decoder<Account> {
+export function getTokenDecoder(): Decoder<Token> {
   return getStructDecoder([
     ['mint', getAddressDecoder()],
     ['owner', getAddressDecoder()],
@@ -108,7 +140,7 @@ export function getAccountDecoder(): Decoder<Account> {
         fixed: true,
       }),
     ],
-    ['state', getTokenStateDecoder()],
+    ['state', getAccountStateDecoder()],
     [
       'isNative',
       getOptionDecoder(getU64Decoder(), {
@@ -127,63 +159,63 @@ export function getAccountDecoder(): Decoder<Account> {
   ]);
 }
 
-export function getAccountCodec(): Codec<AccountArgs, Account> {
-  return combineCodec(getAccountEncoder(), getAccountDecoder());
+export function getTokenCodec(): Codec<TokenArgs, Token> {
+  return combineCodec(getTokenEncoder(), getTokenDecoder());
 }
 
-export function decodeAccount<TAddress extends string = string>(
+export function decodeToken<TAddress extends string = string>(
   encodedAccount: EncodedAccount<TAddress>
-): Account<Account, TAddress>;
-export function decodeAccount<TAddress extends string = string>(
+): Account<Token, TAddress>;
+export function decodeToken<TAddress extends string = string>(
   encodedAccount: MaybeEncodedAccount<TAddress>
-): MaybeAccount<Account, TAddress>;
-export function decodeAccount<TAddress extends string = string>(
+): MaybeAccount<Token, TAddress>;
+export function decodeToken<TAddress extends string = string>(
   encodedAccount: EncodedAccount<TAddress> | MaybeEncodedAccount<TAddress>
-): Account<Account, TAddress> | MaybeAccount<Account, TAddress> {
+): Account<Token, TAddress> | MaybeAccount<Token, TAddress> {
   return decodeAccount(
     encodedAccount as MaybeEncodedAccount<TAddress>,
-    getAccountDecoder()
+    getTokenDecoder()
   );
 }
 
-export async function fetchAccount<TAddress extends string = string>(
+export async function fetchToken<TAddress extends string = string>(
   rpc: Parameters<typeof fetchEncodedAccount>[0],
   address: Address<TAddress>,
   config?: FetchAccountConfig
-): Promise<Account<Account, TAddress>> {
-  const maybeAccount = await fetchMaybeAccount(rpc, address, config);
+): Promise<Account<Token, TAddress>> {
+  const maybeAccount = await fetchMaybeToken(rpc, address, config);
   assertAccountExists(maybeAccount);
   return maybeAccount;
 }
 
-export async function fetchMaybeAccount<TAddress extends string = string>(
+export async function fetchMaybeToken<TAddress extends string = string>(
   rpc: Parameters<typeof fetchEncodedAccount>[0],
   address: Address<TAddress>,
   config?: FetchAccountConfig
-): Promise<MaybeAccount<Account, TAddress>> {
+): Promise<MaybeAccount<Token, TAddress>> {
   const maybeAccount = await fetchEncodedAccount(rpc, address, config);
-  return decodeAccount(maybeAccount);
+  return decodeToken(maybeAccount);
 }
 
-export async function fetchAllAccount(
+export async function fetchAllToken(
   rpc: Parameters<typeof fetchEncodedAccounts>[0],
   addresses: Array<Address>,
   config?: FetchAccountsConfig
-): Promise<Account<Account>[]> {
-  const maybeAccounts = await fetchAllMaybeAccount(rpc, addresses, config);
+): Promise<Account<Token>[]> {
+  const maybeAccounts = await fetchAllMaybeToken(rpc, addresses, config);
   assertAccountsExist(maybeAccounts);
   return maybeAccounts;
 }
 
-export async function fetchAllMaybeAccount(
+export async function fetchAllMaybeToken(
   rpc: Parameters<typeof fetchEncodedAccounts>[0],
   addresses: Array<Address>,
   config?: FetchAccountsConfig
-): Promise<MaybeAccount<Account>[]> {
+): Promise<MaybeAccount<Token>[]> {
   const maybeAccounts = await fetchEncodedAccounts(rpc, addresses, config);
-  return maybeAccounts.map((maybeAccount) => decodeAccount(maybeAccount));
+  return maybeAccounts.map((maybeAccount) => decodeToken(maybeAccount));
 }
 
-export function getAccountSize(): number {
+export function getTokenSize(): number {
   return 165;
 }

+ 19 - 1
clients/js/src/generated/programs/token.ts

@@ -40,10 +40,28 @@ export const TOKEN_PROGRAM_ADDRESS =
 
 export enum TokenAccount {
   Mint,
-  Account,
+  Token,
   Multisig,
 }
 
+export function identifyTokenAccount(
+  account: { data: Uint8Array } | Uint8Array
+): TokenAccount {
+  const data = account instanceof Uint8Array ? account : account.data;
+  if (data.length === 82) {
+    return TokenAccount.Mint;
+  }
+  if (data.length === 165) {
+    return TokenAccount.Token;
+  }
+  if (data.length === 355) {
+    return TokenAccount.Multisig;
+  }
+  throw new Error(
+    'The provided account could not be identified as a token account.'
+  );
+}
+
 export enum TokenInstruction {
   InitializeMint,
   InitializeAccount,

+ 36 - 0
clients/js/src/generated/types/accountState.ts

@@ -0,0 +1,36 @@
+/**
+ * This code was AUTOGENERATED using the kinobi library.
+ * Please DO NOT EDIT THIS FILE, instead use visitors
+ * to add features, then rerun kinobi to update it.
+ *
+ * @see https://github.com/metaplex-foundation/kinobi
+ */
+
+import {
+  Codec,
+  Decoder,
+  Encoder,
+  combineCodec,
+  getEnumDecoder,
+  getEnumEncoder,
+} from '@solana/web3.js';
+
+export enum AccountState {
+  Uninitialized,
+  Initialized,
+  Frozen,
+}
+
+export type AccountStateArgs = AccountState;
+
+export function getAccountStateEncoder(): Encoder<AccountStateArgs> {
+  return getEnumEncoder(AccountState);
+}
+
+export function getAccountStateDecoder(): Decoder<AccountState> {
+  return getEnumDecoder(AccountState);
+}
+
+export function getAccountStateCodec(): Codec<AccountStateArgs, AccountState> {
+  return combineCodec(getAccountStateEncoder(), getAccountStateDecoder());
+}

+ 1 - 1
clients/js/src/generated/types/index.ts

@@ -6,5 +6,5 @@
  * @see https://github.com/metaplex-foundation/kinobi
  */
 
+export * from './accountState';
 export * from './authorityType';
-export * from './tokenState';

+ 0 - 36
clients/js/src/generated/types/tokenState.ts

@@ -1,36 +0,0 @@
-/**
- * This code was AUTOGENERATED using the kinobi library.
- * Please DO NOT EDIT THIS FILE, instead use visitors
- * to add features, then rerun kinobi to update it.
- *
- * @see https://github.com/metaplex-foundation/kinobi
- */
-
-import {
-  Codec,
-  Decoder,
-  Encoder,
-  combineCodec,
-  getEnumDecoder,
-  getEnumEncoder,
-} from '@solana/web3.js';
-
-export enum TokenState {
-  Uninitialized,
-  Initialized,
-  Frozen,
-}
-
-export type TokenStateArgs = TokenState;
-
-export function getTokenStateEncoder(): Encoder<TokenStateArgs> {
-  return getEnumEncoder(TokenState);
-}
-
-export function getTokenStateDecoder(): Decoder<TokenState> {
-  return getEnumDecoder(TokenState);
-}
-
-export function getTokenStateCodec(): Codec<TokenStateArgs, TokenState> {
-  return combineCodec(getTokenStateEncoder(), getTokenStateDecoder());
-}

+ 52 - 20
program/idl.json

@@ -22,7 +22,11 @@
                 },
                 "fixed": true
               },
-              "docs": []
+              "docs": [
+                "Optional authority used to mint new tokens. The mint authority may only",
+                "be provided during mint creation. If no mint authority is present",
+                "then the mint has a fixed supply and no further tokens may be minted."
+              ]
             },
             {
               "kind": "structFieldTypeNode",
@@ -32,7 +36,7 @@
                 "format": "u64",
                 "endian": "le"
               },
-              "docs": []
+              "docs": ["Total supply of tokens."]
             },
             {
               "kind": "structFieldTypeNode",
@@ -42,7 +46,9 @@
                 "format": "u8",
                 "endian": "le"
               },
-              "docs": []
+              "docs": [
+                "Number of base 10 digits to the right of the decimal place."
+              ]
             },
             {
               "kind": "structFieldTypeNode",
@@ -55,7 +61,7 @@
                   "endian": "le"
                 }
               },
-              "docs": []
+              "docs": ["Is `true` if this structure has been initialized."]
             },
             {
               "kind": "structFieldTypeNode",
@@ -70,10 +76,16 @@
                 },
                 "fixed": true
               },
-              "docs": []
+              "docs": ["Optional authority to freeze token accounts."]
             }
           ]
         },
+        "discriminators": [
+          {
+            "kind": "sizeDiscriminatorNode",
+            "size": 82
+          }
+        ],
         "name": "mint",
         "docs": [],
         "size": 82
@@ -87,13 +99,13 @@
               "kind": "structFieldTypeNode",
               "name": "mint",
               "type": { "kind": "publicKeyTypeNode" },
-              "docs": []
+              "docs": ["The mint associated with this account."]
             },
             {
               "kind": "structFieldTypeNode",
               "name": "owner",
               "type": { "kind": "publicKeyTypeNode" },
-              "docs": []
+              "docs": ["The owner of this account."]
             },
             {
               "kind": "structFieldTypeNode",
@@ -103,7 +115,7 @@
                 "format": "u64",
                 "endian": "le"
               },
-              "docs": []
+              "docs": ["The amount of tokens this account holds."]
             },
             {
               "kind": "structFieldTypeNode",
@@ -118,13 +130,16 @@
                 },
                 "fixed": true
               },
-              "docs": []
+              "docs": [
+                "If `delegate` is `Some` then `delegated_amount` represents",
+                "the amount authorized by the delegate."
+              ]
             },
             {
               "kind": "structFieldTypeNode",
               "name": "state",
-              "type": { "kind": "definedTypeLinkNode", "name": "tokenState" },
-              "docs": []
+              "type": { "kind": "definedTypeLinkNode", "name": "accountState" },
+              "docs": ["The account's state."]
             },
             {
               "kind": "structFieldTypeNode",
@@ -143,7 +158,12 @@
                 },
                 "fixed": true
               },
-              "docs": []
+              "docs": [
+                "If is_native.is_some, this is a native token, and the value logs the",
+                "rent-exempt reserve. An Account is required to be rent-exempt, so",
+                "the value is used by the Processor to ensure that wrapped SOL",
+                "accounts do not drop below this threshold."
+              ]
             },
             {
               "kind": "structFieldTypeNode",
@@ -153,7 +173,7 @@
                 "format": "u64",
                 "endian": "le"
               },
-              "docs": []
+              "docs": ["The amount delegated."]
             },
             {
               "kind": "structFieldTypeNode",
@@ -168,11 +188,17 @@
                 },
                 "fixed": true
               },
-              "docs": []
+              "docs": ["Optional authority to close the account."]
             }
           ]
         },
-        "name": "account",
+        "discriminators": [
+          {
+            "kind": "sizeDiscriminatorNode",
+            "size": 165
+          }
+        ],
+        "name": "token",
         "docs": [],
         "size": 165
       },
@@ -189,7 +215,7 @@
                 "format": "u8",
                 "endian": "le"
               },
-              "docs": []
+              "docs": ["Number of signers required."]
             },
             {
               "kind": "structFieldTypeNode",
@@ -199,7 +225,7 @@
                 "format": "u8",
                 "endian": "le"
               },
-              "docs": []
+              "docs": ["Number of valid signers."]
             },
             {
               "kind": "structFieldTypeNode",
@@ -212,7 +238,7 @@
                   "endian": "le"
                 }
               },
-              "docs": []
+              "docs": ["Is `true` if this structure has been initialized."]
             },
             {
               "kind": "structFieldTypeNode",
@@ -222,10 +248,16 @@
                 "item": { "kind": "publicKeyTypeNode" },
                 "count": { "kind": "fixedCountNode", "value": 11 }
               },
-              "docs": []
+              "docs": ["Signer public keys."]
             }
           ]
         },
+        "discriminators": [
+          {
+            "kind": "sizeDiscriminatorNode",
+            "size": 355
+          }
+        ],
         "name": "multisig",
         "docs": [],
         "size": 355
@@ -2025,7 +2057,7 @@
     "definedTypes": [
       {
         "kind": "definedTypeNode",
-        "name": "tokenState",
+        "name": "accountState",
         "type": {
           "kind": "enumTypeNode",
           "variants": [