{"version":3,"file":"Token.mjs","sources":["../../../../../src/plugins/tokenModule/models/Token.ts"],"sourcesContent":["import type { PublicKey } from '@solana/web3.js';\nimport { AccountState } from '@solana/spl-token';\n\nimport { amount, Pda, SplTokenAmount, token } from '../../../types';\nimport { assert } from '../../../utils/assert';\nimport { Option } from '../../../utils/types';\nimport { TokenAccount } from '../accounts';\nimport { associatedTokenProgram } from '../program';\nimport { Mint } from './Mint';\n\n/**\n * This model represents a token account.\n *\n * @group Models\n */\nexport type Token = {\n  /** A model identifier to distinguish models in the SDK. */\n  readonly model: 'token';\n\n  /** The address of the token account. */\n  readonly address: PublicKey | Pda;\n\n  /** Whether or not this is an associated token account. */\n  readonly isAssociatedToken: boolean;\n\n  /** The address of the mint account. */\n  readonly mintAddress: PublicKey;\n\n  /** The address of the owner of this token account. */\n  readonly ownerAddress: PublicKey;\n\n  /** The amount of tokens held in this account. */\n  readonly amount: SplTokenAmount;\n\n  /**\n   * The address of the authority that can close the account.\n   * This field is optional and may be `null`.\n   */\n  readonly closeAuthorityAddress: Option<PublicKey>;\n\n  /**\n   * The address of the authority that can act on behalf of the owner\n   * of the account. This field is optional and may be `null`.\n   */\n  readonly delegateAddress: Option<PublicKey>;\n\n  /**\n   * The amount of tokens that were delegated to the delegate authority.\n   * This means the delegate authority cannot transfer more tokens\n   * than this amount even if the token account has more tokens available.\n   *\n   * This field is only relevant if the account has a delegate authority.\n   */\n  readonly delegateAmount: SplTokenAmount;\n\n  /**\n   * The state of the token account.\n   * It is mostly used to determine whether or not the account is frozen.\n   *\n   * It can be one of the following:\n   * - `AccountState.Uninitialized`: The account has not been initialized.\n   *   This should never happen in this model since the SDK would fail to\n   *   parse this model if it were uninitialized.\n   * - `AccountState.Initialized`: The account has been initialized and is not frozen.\n   * - `AccountState.Frozen`: The account has been initialized and is frozen.\n   */\n  readonly state: AccountState;\n};\n\n/** @group Model Helpers */\nexport const isToken = (value: any): value is Token =>\n  typeof value === 'object' && value.model === 'token';\n\n/** @group Model Helpers */\nexport function assertToken(value: any): asserts value is Token {\n  assert(isToken(value), 'Expected Token model');\n}\n\n/** @group Model Helpers */\nexport const toToken = (account: TokenAccount): Token => {\n  const associatedTokenAddress = Pda.find(associatedTokenProgram.address, [\n    account.data.owner.toBuffer(),\n    account.owner.toBuffer(),\n    account.data.mint.toBuffer(),\n  ]);\n  const isAssociatedToken = associatedTokenAddress.equals(account.publicKey);\n\n  return {\n    model: 'token',\n    address: isAssociatedToken ? associatedTokenAddress : account.publicKey,\n    isAssociatedToken,\n    mintAddress: account.data.mint,\n    ownerAddress: account.data.owner,\n    amount: token(account.data.amount.toString()),\n    closeAuthorityAddress: account.data.closeAuthorityOption\n      ? account.data.closeAuthority\n      : null,\n    delegateAddress: account.data.delegateOption ? account.data.delegate : null,\n    delegateAmount: token(account.data.delegatedAmount.toString()),\n    state: account.data.state,\n  };\n};\n\n/** @group Models */\nexport type TokenWithMint = Omit<Token, 'model' | 'mintAddress'> &\n  Readonly<{\n    model: 'tokenWithMint';\n    mint: Mint;\n  }>;\n\n/** @group Model Helpers */\nexport const isTokenWithMint = (value: any): value is TokenWithMint =>\n  typeof value === 'object' && value.model === 'tokenWithMint';\n\n/** @group Model Helpers */\nexport function assertTokenWithMint(\n  value: any\n): asserts value is TokenWithMint {\n  assert(isTokenWithMint(value), 'Expected TokenWithMint model');\n}\n\n/** @group Model Helpers */\nexport const toTokenWithMint = (\n  tokenAccount: TokenAccount,\n  mintModel: Mint\n): TokenWithMint => {\n  const token = toToken(tokenAccount);\n  return {\n    ...token,\n    model: 'tokenWithMint',\n    mint: mintModel,\n    amount: amount(token.amount.basisPoints, mintModel.currency),\n    delegateAmount: amount(\n      token.delegateAmount.basisPoints,\n      mintModel.currency\n    ),\n  };\n};\n"],"names":["isToken","value","_typeof","model","assertToken","assert","toToken","account","associatedTokenAddress","Pda","find","associatedTokenProgram","address","data","owner","toBuffer","mint","isAssociatedToken","equals","publicKey","mintAddress","ownerAddress","amount","token","toString","closeAuthorityAddress","closeAuthorityOption","closeAuthority","delegateAddress","delegateOption","delegate","delegateAmount","delegatedAmount","state","isTokenWithMint","assertTokenWithMint","toTokenWithMint","tokenAccount","mintModel","_objectSpread","basisPoints","currency"],"mappings":";;;;;;AAUA;AACA;AACA;AACA;AACA;;AAuDA;IACaA,OAAO,GAAG,SAAVA,OAAOA,CAAIC,KAAU,EAAA;EAAA,OAChCC,OAAA,CAAOD,KAAK,CAAK,KAAA,QAAQ,IAAIA,KAAK,CAACE,KAAK,KAAK,OAAO,CAAA;AAAA,EAAA;;AAEtD;AACO,SAASC,WAAWA,CAACH,KAAU,EAA0B;AAC9DI,EAAAA,MAAM,CAACL,OAAO,CAACC,KAAK,CAAC,EAAE,sBAAsB,CAAC,CAAA;AAChD,CAAA;;AAEA;IACaK,OAAO,GAAG,SAAVA,OAAOA,CAAIC,OAAqB,EAAY;AACvD,EAAA,IAAMC,sBAAsB,GAAGC,GAAG,CAACC,IAAI,CAACC,sBAAsB,CAACC,OAAO,EAAE,CACtEL,OAAO,CAACM,IAAI,CAACC,KAAK,CAACC,QAAQ,EAAE,EAC7BR,OAAO,CAACO,KAAK,CAACC,QAAQ,EAAE,EACxBR,OAAO,CAACM,IAAI,CAACG,IAAI,CAACD,QAAQ,EAAE,CAC7B,CAAC,CAAA;EACF,IAAME,iBAAiB,GAAGT,sBAAsB,CAACU,MAAM,CAACX,OAAO,CAACY,SAAS,CAAC,CAAA;EAE1E,OAAO;AACLhB,IAAAA,KAAK,EAAE,OAAO;AACdS,IAAAA,OAAO,EAAEK,iBAAiB,GAAGT,sBAAsB,GAAGD,OAAO,CAACY,SAAS;AACvEF,IAAAA,iBAAiB,EAAjBA,iBAAiB;AACjBG,IAAAA,WAAW,EAAEb,OAAO,CAACM,IAAI,CAACG,IAAI;AAC9BK,IAAAA,YAAY,EAAEd,OAAO,CAACM,IAAI,CAACC,KAAK;AAChCQ,IAAAA,MAAM,EAAEC,KAAK,CAAChB,OAAO,CAACM,IAAI,CAACS,MAAM,CAACE,QAA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