Skip to content

Commit

Permalink
Pool NFT
Browse files Browse the repository at this point in the history
  • Loading branch information
Sluder committed Dec 14, 2023
1 parent dff0404 commit 9a5903c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/dex/api/spectrum-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/dex/models/liquidity-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class LiquidityPool {
limitOrderAddress: string;

lpToken: Asset;
poolNft: Asset;
identifier: string = '';
poolFeePercent: number = 0;
totalLpTokens: bigint = 0n;
Expand Down
21 changes: 14 additions & 7 deletions src/dex/spectrum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9a5903c

Please sign in to comment.