{"version":3,"sources":["../../src/common/transfer.ts"],"sourcesContent":["import { EpochInfo } from \"@solana/web3.js\";\nimport { TransferFeeConfig, TransferFee } from \"@solana/spl-token\";\nimport BN from \"bn.js\";\n\nimport { GetTransferAmountFee } from \"../raydium/type\";\nimport { TransferFeeDataBaseType } from \"../api/type\";\n\nconst POINT = 10_000;\nexport function getTransferAmountFee(\n  amount: BN,\n  feeConfig: TransferFeeConfig | undefined,\n  epochInfo: EpochInfo,\n  addFee: boolean,\n): GetTransferAmountFee {\n  if (feeConfig === undefined) {\n    return {\n      amount,\n      fee: undefined,\n      expirationTime: undefined,\n    };\n  }\n\n  const nowFeeConfig: TransferFee =\n    epochInfo.epoch < feeConfig.newerTransferFee.epoch ? feeConfig.olderTransferFee : feeConfig.newerTransferFee;\n  const maxFee = new BN(nowFeeConfig.maximumFee.toString());\n  const expirationTime: number | undefined =\n    epochInfo.epoch < feeConfig.newerTransferFee.epoch\n      ? ((Number(feeConfig.newerTransferFee.epoch) * epochInfo.slotsInEpoch - epochInfo.absoluteSlot) * 400) / 1000\n      : undefined;\n\n  if (addFee) {\n    if (nowFeeConfig.transferFeeBasisPoints === POINT) {\n      const nowMaxFee = new BN(nowFeeConfig.maximumFee.toString());\n      return {\n        amount: amount.add(nowMaxFee),\n        fee: nowMaxFee,\n        expirationTime,\n      };\n    } else {\n      const _TAmount = BNDivCeil(amount.mul(new BN(POINT)), new BN(POINT - nowFeeConfig.transferFeeBasisPoints));\n\n      const nowMaxFee = new BN(nowFeeConfig.maximumFee.toString());\n      const TAmount = _TAmount.sub(amount).gt(nowMaxFee) ? amount.add(nowMaxFee) : _TAmount;\n\n      const _fee = BNDivCeil(TAmount.mul(new BN(nowFeeConfig.transferFeeBasisPoints)), new BN(POINT));\n      const fee = _fee.gt(maxFee) ? maxFee : _fee;\n      return {\n        amount: TAmount,\n        fee,\n        expirationTime,\n      };\n    }\n  } else {\n    const _fee = BNDivCeil(amount.mul(new BN(nowFeeConfig.transferFeeBasisPoints)), new BN(POINT));\n    const fee = _fee.gt(maxFee) ? maxFee : _fee;\n\n    return {\n      amount,\n      fee,\n      expirationTime,\n    };\n  }\n}\n\nexport function getTransferAmountFeeV2(\n  amount: BN,\n  _feeConfig: TransferFeeDataBaseType | undefined,\n  epochInfo: EpochInfo,\n  addFee: boolean,\n): GetTransferAmountFee {\n  if (_feeConfig === undefined) {\n    return {\n      amount,\n      fee: undefined,\n      expirationTime: undefined,\n    };\n  }\n  const feeConfig = {\n    ..._feeConfig,\n    olderTransferFee: {\n      epoch: BigInt(_feeConfig.olderTransferFee.epoch),\n      maximumFee: BigInt(_feeConfig.olderTransferFee.maximumFee),\n      transferFeeBasisPoints: _feeConfig.olderTransferFee.transferFeeBasisPoints,\n    },\n    newerTransferFee: {\n      epoch: BigInt(_feeConfig.newerTransferFee.epoch),\n      maximumFee: BigInt(_feeConfig.newerTransferFee.maximumFee),\n      transferFeeBasisPoints: _feeConfig.newerTransferFee.transferFeeBasisPoints,\n    },\n  };\n\n  const nowFeeConfig: TransferFee =\n    epochInfo.epoch < feeConfig.newerTransferFee.epoch ? feeConfig.olderTransferFee : feeConfig.newerTransferFee;\n  const maxFee = new BN(nowFeeConfig.maximumFee.toString());\n  const expirationTime: number | undefined =\n    epochInfo.epoch < feeConfig.newerTransferFee.epoch\n      ? ((Number(feeConfig.newerTransferFee.epoch) * epochInfo.slotsInEpoch - epochInfo.absoluteSlot) * 400) / 1000\n      : undefined;\n\n  if (addFee) {\n    if (nowFeeConfig.transferFeeBasisPoints === POINT) {\n      const nowMaxFee = new BN(nowFeeConfig.maximumFee.toString());\n      return {\n        amount: amount.add(nowMaxFee),\n        fee: nowMaxFee,\n        expirationTime,\n      };\n    } else {\n      const _TAmount = BNDivCeil(amount.mul(new BN(POINT)), new BN(POINT - nowFeeConfig.transferFeeBasisPoints));\n\n      const nowMaxFee = new BN(nowFeeConfig.maximumFee.toString());\n      const TAmount = _TAmount.sub(amount).gt(nowMaxFee) ? amount.add(nowMaxFee) : _TAmount;\n\n      const _fee = BNDivCeil(TAmount.mul(new BN(nowFeeConfig.transferFeeBasisPoints)), new BN(POINT));\n      const fee = _fee.gt(maxFee) ? maxFee : _fee;\n      return {\n        amount: TAmount,\n        fee,\n        expirationTime,\n      };\n    }\n  } else {\n    const _fee = BNDivCeil(amount.mul(new BN(nowFeeConfig.transferFeeBasisPoints)), new BN(POINT));\n    const fee = _fee.gt(maxFee) ? maxFee : _fee;\n\n    return {\n      amount,\n      fee,\n      expirationTime,\n    };\n  }\n}\n\nexport function minExpirationTime(\n  expirationTime1: number | undefined,\n  expirationTime2: number | undefined,\n): number | undefined {\n  if (expirationTime1 === undefined) return expirationTime2;\n  if (expirationTime2 === undefined) return expirationTime1;\n\n  return Math.min(expirationTime1, expirationTime2);\n}\n\nexport function BNDivCeil(bn1: BN, bn2: BN): BN {\n  const { div, mod } = bn1.divmod(bn2);\n\n  if (mod.gt(new BN(0))) {\n    return div.add(new BN(1));\n  } else {\n    return div;\n  }\n}\n"],"mappings":"6aAEA,qBAKA,GAAM,GAAQ,IACP,WACL,EACA,EACA,EACA,EACsB,CACtB,GAAI,IAAc,OAChB,MAAO,CACL,SACA,IAAK,OACL,eAAgB,MAClB,EAGF,GAAM,GACJ,EAAU,MAAQ,EAAU,iBAAiB,MAAQ,EAAU,iBAAmB,EAAU,iBACxF,EAAS,GAAI,GAAG,EAAa,WAAW,SAAS,CAAC,EAClD,EACJ,EAAU,MAAQ,EAAU,iBAAiB,MACvC,QAAO,EAAU,iBAAiB,KAAK,EAAI,EAAU,aAAe,EAAU,cAAgB,IAAO,IACvG,OAEN,GAAI,EACF,GAAI,EAAa,yBAA2B,EAAO,CACjD,GAAM,GAAY,GAAI,GAAG,EAAa,WAAW,SAAS,CAAC,EAC3D,MAAO,CACL,OAAQ,EAAO,IAAI,CAAS,EAC5B,IAAK,EACL,gBACF,CACF,KAAO,CACL,GAAM,GAAW,EAAU,EAAO,IAAI,GAAI,GAAG,CAAK,CAAC,EAAG,GAAI,GAAG,E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