Skip to content

Commit

Permalink
Formula fix & pool NFT
Browse files Browse the repository at this point in the history
  • Loading branch information
Sluder committed Dec 14, 2023
1 parent a722128 commit 49f27ba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/dex/api/teddyswap-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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
18 changes: 12 additions & 6 deletions src/dex/teddyswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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];
}

Expand All @@ -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,
Expand Down

0 comments on commit 49f27ba

Please sign in to comment.