UNPKG

@raydium-io/raydium-sdk-v2

Version:

An SDK for building applications on top of Raydium.

1 lines 11.7 kB
{"version":3,"sources":["../../src/common/transfer.ts"],"sourcesContent":["import { EpochInfo } from \"@solana/web3.js\";\nimport BN from \"bn.js\";\nimport { TransferFee, TransferFeeConfig } from \"@solana/spl-token\";\n\nimport { TransferFeeDataBaseType } from \"../api/type\";\nimport { GetTransferAmountFee } from \"../raydium/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\nexport function ceilDivBN(amountA: BN, amountB: BN): BN {\n if (amountA.isZero()) return new BN(0);\n\n const quotient = amountA.div(amountB);\n\n if (quotient.isZero()) return new BN(1);\n\n const remainder = amountA.mod(amountB);\n if (remainder.gt(new BN(0))) {\n return quotient.add(new BN(1));\n }\n return quotient;\n}\n\nexport function getTransferAmountFeeFromPre(\n amount: BN,\n feeConfig: TransferFeeConfig | undefined,\n slot: number,\n): GetTransferAmountFee {\n if (feeConfig === undefined) {\n return {\n amount,\n fee: undefined,\n expirationTime: undefined,\n };\n }\n const epoch = Math.floor(slot / 432000);\n const nowFeeConfig: TransferFee =\n epoch < feeConfig.newerTransferFee.epoch ? feeConfig.olderTransferFee : feeConfig.newerTransferFee;\n const maxFee = new BN(nowFeeConfig.maximumFee.toString());\n const expirationTime: number | undefined =\n epoch < feeConfig.newerTransferFee.epoch\n ? ((Number(feeConfig.newerTransferFee.epoch) * 432000 - slot) * 400) / 1000\n : undefined;\n const _fee = BNDivCeil(amount.mul(new BN(nowFeeConfig.transferFeeBasisPoints)), new BN(POINT));\n const fee = _fee.gt(maxFee) ? maxFee : _fee;\n return {\n amount,\n fee,\n expirationTime,\n };\n}\nexport function getTransferAmountFeeFromPost(\n amount: BN,\n feeConfig: TransferFeeConfig | undefined,\n slot: number,\n): GetTransferAmountFee {\n if (feeConfig === undefined) {\n return {\n amount,\n fee: undefined,\n expirationTime: undefined,\n };\n }\n const epoch = Math.floor(slot / 432000);\n const nowFeeConfig: TransferFee =\n epoch < feeConfig.newerTransferFee.epoch ? feeConfig.olderTransferFee : feeConfig.newerTransferFee;\n const maxFee = new BN(nowFeeConfig.maximumFee.toString());\n const expirationTime: number | undefined =\n epoch < feeConfig.newerTransferFee.epoch\n ? ((Number(feeConfig.newerTransferFee.epoch) * 432000 - slot) * 400) / 1000\n : undefined;\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 const nowMaxFee = new BN(nowFeeConfig.maximumFee.toString());\n const TAmount = _TAmount.sub(amount).gt(nowMaxFee) ? amount.add(nowMaxFee) : _TAmount;\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}\n"],"mappings":"6aACA,qBAMA,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,aA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