@solana/spl-token#Mint TypeScript Examples

The following examples show how to use @solana/spl-token#Mint. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: accountParser.ts    From jet-engine with GNU Affero General Public License v3.0 6 votes vote down vote up
parseMintAccount = (info: AccountInfo<Buffer>, address: PublicKey): Mint => {
  if (!info) throw new TokenAccountNotFoundError()
  if (!info.owner.equals(TOKEN_PROGRAM_ID)) throw new TokenInvalidAccountOwnerError()
  if (info.data.length != MINT_SIZE) throw new TokenInvalidAccountSizeError()

  const rawMint = MintLayout.decode(info.data)

  return {
    address,
    mintAuthority: rawMint.mintAuthorityOption ? rawMint.mintAuthority : null,
    supply: rawMint.supply,
    decimals: rawMint.decimals,
    isInitialized: rawMint.isInitialized,
    freezeAuthority: rawMint.freezeAuthorityOption ? rawMint.freezeAuthority : null
  }
}
Example #2
Source File: associatedToken.ts    From jet-engine with GNU Affero General Public License v3.0 6 votes vote down vote up
/** TODO:
   * Get mint info
   * @static
   * @param {Provider} connection
   * @param {Address} mint
   * @returns {(Promise<Mint | undefined>)}
   * @memberof AssociatedToken
   */
  static async loadMint(connection: Connection, mint: Address): Promise<Mint | undefined> {
    const mintAddress = translateAddress(mint)
    const mintInfo = await connection.getAccountInfo(mintAddress)
    if (!mintInfo) {
      return undefined
    }
    return parseMintAccount(mintInfo, mintAddress)
  }
Example #3
Source File: associatedToken.ts    From jet-engine with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
   * Use a specified token mint.
   * @static
   * @param {Provider} [connection]
   * @param {PublicKey} [address]
   * @returns {(Mint | undefined)}
   * @memberof AssociatedToken
   */
  static useMint(connection: Connection | undefined, address: PublicKey | undefined): Mint | undefined {
    return Hooks.usePromise(
      async () => connection && address && AssociatedToken.loadMint(connection, address),
      [connection, address?.toBase58()]
    )
  }
Example #4
Source File: tokenAmount.ts    From jet-engine with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
   * @static
   * @param {MintInfo} mint
   * @param {PublicKey} mintAddress
   * @returns {TokenAmount}
   * @memberof TokenAmount
   */
  public static mint(mint: Mint, mintAddress: PublicKey): TokenAmount {
    return new TokenAmount(bigIntToBn(mint.supply), mint.decimals, mintAddress)
  }
Example #5
Source File: marginPool.ts    From jet-engine with GNU Affero General Public License v3.0 6 votes vote down vote up
constructor(
    public program: Program<JetMarginPoolIdl>,
    public addresses: MarginPoolAddresses,
    public marginPool: MarginPoolData,
    public vault: Account,
    public depositNoteMint: Mint,
    public loanNoteMint: Mint,
    public poolTokenMint: Mint
  ) {}
Example #6
Source File: stakePool.ts    From jet-engine with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
   * Creates an instance of StakePool.
   * @param {Program<StakeIdl>} program
   * @param {StakePoolAccounts} addresses
   * @param {StakePoolInfo} stakePool
   * @param {MintInfo} voteMint
   * @param {MintInfo} collateralMint
   * @param {JetTokenAccount} vault
   * @param {MintInfo} tokenMint
   * @memberof StakePool
   */
  constructor(
    public program: Program<StakeIdl>,
    public addresses: StakePoolAccounts,
    public stakePool: StakePoolInfo,
    public collateralMint: Mint,
    public vault: Account,
    public tokenMint: Mint,
    public maxVoterWeightRecord: MaxVoterWeightRecord
  ) {}
Example #7
Source File: providers.ts    From amman with Apache License 2.0 5 votes vote down vote up
// -----------------
  // Helpers
  // -----------------
  private async _toAmmanAccount(
    account: Mint | Account
  ): Promise<AmmanAccount> {
    const acc: Record<string, any> = {}
    const amountDivisor = isAccount(account)
      ? (await this._getMintDecimals(account.mint)).divisor
      : isMint(account)
      ? Math.pow(10, account.decimals)
      : 1
    for (let [key, value] of Object.entries(account)) {
      if (value == null) {
        acc[key] = value
      } else if (isKeyLike(value)) {
        const publicKeyStr = publicKeyString(value)
        const label = await this._tryResolveAddressRemote(publicKeyStr)
        acc[key] = label == null ? publicKeyStr : `${label} (${publicKeyStr})`
      } else if (typeof value === 'bigint') {
        const formatted = numeral(value).format('0,0')
        // Mint specific adjustments
        if (key === 'amount' || key === 'supply') {
          const balance = value / BigInt(amountDivisor)
          acc[key] = formatted + ` (balance: ${balance.toString()})`
        } else {
          acc[key] = formatted
        }
      } else if (typeof value === 'number') {
        acc[key] = numeral(value).format('0,0')
      } else if (
        BN.isBN(value) ||
        (typeof value === 'object' &&
          'negative' in value &&
          'words' in value &&
          'red' in value)
      ) {
        acc[key] = new BN(value).toNumber()
      } else if (typeof value.pretty === 'function') {
        acc[key] = value.pretty()
      } else if (typeof value === 'object') {
        acc[key] = JSON.stringify(value)
      } else {
        acc[key] = value
      }
    }
    return {
      pretty() {
        return acc
      },
    }
  }
Example #8
Source File: types.ts    From amman with Apache License 2.0 5 votes vote down vote up
export function isMint(value: any): value is Mint {
  const mint = value as Mint
  return typeof mint.supply === 'bigint' && typeof mint.decimals === 'number'
}