@solana/spl-token#Account TypeScript Examples

The following examples show how to use @solana/spl-token#Account. 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
parseTokenAccount = (info: AccountInfo<Buffer>, address: PublicKey): Account => {
  if (!info) throw new TokenAccountNotFoundError()
  if (!info.owner.equals(TOKEN_PROGRAM_ID)) throw new TokenInvalidAccountOwnerError()
  if (info.data.length != ACCOUNT_SIZE) throw new TokenInvalidAccountSizeError()

  const rawAccount = AccountLayout.decode(info.data)

  return {
    address,
    mint: rawAccount.mint,
    owner: rawAccount.owner,
    amount: rawAccount.amount,
    delegate: rawAccount.delegateOption ? rawAccount.delegate : null,
    delegatedAmount: rawAccount.delegatedAmount,
    isInitialized: rawAccount.state !== AccountState.Uninitialized,
    isFrozen: rawAccount.state === AccountState.Frozen,
    isNative: !!rawAccount.isNativeOption,
    rentExemptReserve: rawAccount.isNativeOption ? rawAccount.isNative : null,
    closeAuthority: rawAccount.closeAuthorityOption ? rawAccount.closeAuthority : null
  }
}
Example #2
Source File: associatedToken.ts    From jet-engine with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
   * Creates an instance of AssociatedToken.
   * @param {AccountInfo<Buffer>} account
   * @param {JetTokenAccount} info
   * @memberof AssociatedToken
   */
  constructor(public account: AccountInfo<Buffer | ParsedAccountData>, public info: Account) {
    this.address = info.address
  }
Example #3
Source File: tokenAmount.ts    From jet-engine with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
   * @static
   * @param {TokenAccount} tokenAccount
   * @param {number} decimals
   * @returns {TokenAmount}
   * @memberof TokenAmount
   */
  public static tokenAccount(tokenAccount: Account, decimals: number): TokenAmount {
    return new TokenAmount(bigIntToBn(tokenAccount.amount), decimals, tokenAccount.mint)
  }
Example #4
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 #5
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 #6
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 #7
Source File: types.ts    From amman with Apache License 2.0 5 votes vote down vote up
export function isAccount(value: any): value is Account {
  const account = value as Account
  return (
    account.mint != null &&
    account.owner != null &&
    typeof account.amount === 'bigint'
  )
}