diff --git a/src/dex/api/teddyswap-api.ts b/src/dex/api/teddyswap-api.ts index 653c53e..74c6b5b 100644 --- a/src/dex/api/teddyswap-api.ts +++ b/src/dex/api/teddyswap-api.ts @@ -48,7 +48,8 @@ export class TeddyswapApi extends BaseApi { this.dex.orderAddress, this.dex.orderAddress, ); - + const [poolNftPolicyId, poolNftName] = poolResponse.id.split('.'); + liquidityPool.poolNft = new Asset(poolNftPolicyId, Buffer.from(poolNftName, 'utf8').toString('hex')); liquidityPool.lpToken = new Asset(poolResponse.lockedLQ.asset.currencySymbol, Buffer.from(poolResponse.lockedLQ.asset.tokenName, 'utf8').toString('hex')); liquidityPool.poolFeePercent = (1 - (poolResponse.poolFeeNum / poolResponse.poolFeeDenum)) * 10; liquidityPool.identifier = liquidityPool.lpToken.identifier(); diff --git a/src/dex/models/liquidity-pool.ts b/src/dex/models/liquidity-pool.ts index 70703c8..e5039a2 100644 --- a/src/dex/models/liquidity-pool.ts +++ b/src/dex/models/liquidity-pool.ts @@ -12,6 +12,7 @@ export class LiquidityPool { limitOrderAddress: string; lpToken: Asset; + poolNft: Asset; identifier: string = ''; poolFeePercent: number = 0; totalLpTokens: bigint = 0n; diff --git a/src/dex/teddyswap.ts b/src/dex/teddyswap.ts index b8d48fa..3506968 100644 --- a/src/dex/teddyswap.ts +++ b/src/dex/teddyswap.ts @@ -122,12 +122,15 @@ export class TeddySwap extends BaseDex { return assetBalance.asset !== 'lovelace' && assetBalance.asset.policyId === lpTokenPolicyId && assetBalance.asset.nameHex === lpTokenAssetName; - }); + });const nftToken: Asset | undefined = utxo.assetBalances.find((assetBalance) => { + return (assetBalance.asset as Asset).assetName?.toLowerCase()?.endsWith('_nft'); + })?.asset as Asset | undefined; - if (! lpTokenBalance) { + if (! lpTokenBalance || ! nftToken) { return Promise.resolve(undefined); } + liquidityPool.poolNft = nftToken; liquidityPool.lpToken = lpTokenBalance.asset as Asset; liquidityPool.totalLpTokens = MAX_INT - lpTokenBalance.quantity; liquidityPool.identifier = liquidityPool.lpToken.identifier(); @@ -172,12 +175,15 @@ export class TeddySwap extends BaseDex { if (!batcherFee || !deposit || !minReceive) { return Promise.reject('Parameters for datum are not set.'); } + if (! liquidityPool.poolNft) { + return Promise.reject('Pool NFT is required.'); + } const decimalToFractionalImproved = (decimalValue: bigint | number): [bigint, bigint] => { const [whole, decimals = ''] = decimalValue.toString()?.split('.'); let truncatedDecimals = decimals.slice(0, 15); - const denominator = 10n ** BigInt(truncatedDecimals.length); - const numerator = BigInt(whole + truncatedDecimals); + const denominator: bigint = BigInt(10 ** truncatedDecimals.length); + const numerator = BigInt(whole) * denominator + BigInt(decimals); return [numerator, denominator]; } @@ -187,8 +193,8 @@ export class TeddySwap extends BaseDex { swapParameters = { ...swapParameters, - [DatumParameterKey.TokenPolicyId]: liquidityPool.lpToken.policyId, - [DatumParameterKey.TokenAssetName]: liquidityPool.lpToken.nameHex, + [DatumParameterKey.TokenPolicyId]: liquidityPool.poolNft.policyId, + [DatumParameterKey.TokenAssetName]: liquidityPool.poolNft.nameHex, [DatumParameterKey.LpFee]: lpfee, [DatumParameterKey.LpFeeNumerator]: numerator, [DatumParameterKey.LpFeeDenominator]: denominator,