@indigo-labs/dexter
Version:
Customizable Typescript SDK for interacting with Cardano DEXs
28 lines (27 loc) • 1.07 kB
JavaScript
import { Asset } from './models/asset';
import { DatumParameterKey } from '../constants';
import { tokensMatch } from '../utils';
export class BaseDex {
/**
* Adjust the payment for the DEX order address to include the swap in amount.
*/
buildSwapOrderPayment(swapParameters, orderPayment) {
const swapInAmount = swapParameters[DatumParameterKey.SwapInAmount];
const swapInToken = swapParameters[DatumParameterKey.SwapInTokenPolicyId]
? new Asset(swapParameters[DatumParameterKey.SwapInTokenPolicyId], swapParameters[DatumParameterKey.SwapInTokenAssetName])
: 'lovelace';
let assetBalance = orderPayment.assetBalances.find((payment) => {
return tokensMatch(payment.asset, swapInToken);
});
if (!assetBalance) {
orderPayment.assetBalances.push({
asset: swapInToken,
quantity: swapInAmount,
});
}
else {
assetBalance.quantity += swapInAmount;
}
return orderPayment;
}
}