From 9a5903c90d0b00f972ee3a0685fc1b08fab4b89f Mon Sep 17 00:00:00 2001 From: Sluder Date: Thu, 14 Dec 2023 16:27:23 -0500 Subject: [PATCH] Pool NFT --- src/dex/api/spectrum-api.ts | 4 +++- src/dex/models/liquidity-pool.ts | 1 + src/dex/spectrum.ts | 21 ++++++++++++++------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/dex/api/spectrum-api.ts b/src/dex/api/spectrum-api.ts index fb4909a..6e82199 100644 --- a/src/dex/api/spectrum-api.ts +++ b/src/dex/api/spectrum-api.ts @@ -50,8 +50,10 @@ export class SpectrumApi extends BaseApi { 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.poolFeePercent = (1 - (poolResponse.poolFeeNum / poolResponse.poolFeeDenum)) * 100; liquidityPool.identifier = liquidityPool.lpToken.identifier(); return liquidityPool; 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/spectrum.ts b/src/dex/spectrum.ts index 9617058..268fdc4 100644 --- a/src/dex/spectrum.ts +++ b/src/dex/spectrum.ts @@ -111,11 +111,15 @@ export class Spectrum extends BaseDex { && 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(); @@ -157,26 +161,29 @@ export class Spectrum extends BaseDex { const deposit: SwapFee | undefined = this.swapOrderFees().find((fee: SwapFee) => fee.id === 'deposit'); const minReceive = swapParameters.MinReceive as bigint; - if (!batcherFee || !deposit || !minReceive) { + 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]; } const batcherFeeForToken = Number(batcherFee.value) / Number(minReceive); const [numerator, denominator] = decimalToFractionalImproved(batcherFeeForToken); - const lpfee: number = 1000 - liquidityPool.poolFeePercent * 10; + const lpfee: bigint = BigInt(1000 - Math.floor(liquidityPool.poolFeePercent * 10)); 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,