Skip to content

Commit

Permalink
Minswap cancel order
Browse files Browse the repository at this point in the history
  • Loading branch information
Sluder committed Jan 4, 2024
1 parent 553abe6 commit 4478976
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/dex/base-dex.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LiquidityPool } from './models/liquidity-pool';
import { BaseDataProvider } from '@providers/data/base-data-provider';
import { Asset, Token } from './models/asset';
import { AssetBalance, DatumParameters, PayToAddress, SwapFee, UTxO } from '@app/types';
import { AssetBalance, DatumParameters, PayToAddress, SpendUTxO, SwapFee, UTxO } from '@app/types';
import { DatumParameterKey } from '@app/constants';
import { tokensMatch } from '@app/utils';
import { BaseApi } from '@dex/api/base-api';
Expand Down Expand Up @@ -46,7 +46,7 @@ export abstract class BaseDex {
/**
* Craft a swap order for this DEX.
*/
abstract buildSwapOrder(liquidityPool: LiquidityPool, swapParameters: DatumParameters, spendUtxos?: UTxO[]): Promise<PayToAddress[]>;
abstract buildSwapOrder(liquidityPool: LiquidityPool, swapParameters: DatumParameters, spendUtxos?: SpendUTxO[]): Promise<PayToAddress[]>;

/**
* Craft a swap order cancellation for this DEX.
Expand Down
17 changes: 14 additions & 3 deletions src/dex/minswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AssetBalance,
DatumParameters, DefinitionConstr, DefinitionField,
PayToAddress,
RequestConfig,
RequestConfig, SpendUTxO,
SwapFee,
UTxO
} from '@app/types';
Expand All @@ -18,6 +18,7 @@ import order from '@dex/definitions/minswap/order';
import { BaseApi } from '@dex/api/base-api';
import { MinswapApi } from '@dex/api/minswap-api';
import pool from '@dex/definitions/minswap/pool';
import { Script } from 'lucid-cardano';

export class Minswap extends BaseDex {

Expand All @@ -32,6 +33,11 @@ export class Minswap extends BaseDex {
public readonly lpTokenPolicyId: string = 'e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d86';
public readonly poolNftPolicyId: string = '0be55d262b29f564998ff81efe21bdc0022621c12f15af08d0f2ddb1';
public readonly poolValidityAsset: string = '13aa2accf2e1561723aa26871e071fdf32c867cff7e7d50ad470d62f4d494e53574150';
public readonly cancelDatum: string = 'd87a80';
public readonly orderScript: Script = {
type: 'PlutusV1',
script: '59014f59014c01000032323232323232322223232325333009300e30070021323233533300b3370e9000180480109118011bae30100031225001232533300d3300e22533301300114a02a66601e66ebcc04800400c5288980118070009bac3010300c300c300c300c300c300c300c007149858dd48008b18060009baa300c300b3754601860166ea80184ccccc0288894ccc04000440084c8c94ccc038cd4ccc038c04cc030008488c008dd718098018912800919b8f0014891ce1317b152faac13426e6a83e06ff88a4d62cce3c1634ab0a5ec133090014a0266008444a00226600a446004602600a601a00626600a008601a006601e0026ea8c03cc038dd5180798071baa300f300b300e3754601e00244a0026eb0c03000c92616300a001375400660106ea8c024c020dd5000aab9d5744ae688c8c0088cc0080080048c0088cc00800800555cf2ba15573e6e1d200201',
};

constructor(requestConfig: RequestConfig = {}) {
super();
Expand Down Expand Up @@ -177,7 +183,7 @@ export class Minswap extends BaseDex {
return Number(priceImpactNumerator * 100n) / Number(priceImpactDenominator);
}

public async buildSwapOrder(liquidityPool: LiquidityPool, swapParameters: DatumParameters, spendUtxos: UTxO[] = []): Promise<PayToAddress[]> {
public async buildSwapOrder(liquidityPool: LiquidityPool, swapParameters: DatumParameters, spendUtxos: SpendUTxO[] = []): Promise<PayToAddress[]> {
const batcherFee: SwapFee | undefined = this.swapOrderFees().find((fee: SwapFee) => fee.id === 'batcherFee');
const deposit: SwapFee | undefined = this.swapOrderFees().find((fee: SwapFee) => fee.id === 'deposit');

Expand Down Expand Up @@ -232,7 +238,12 @@ export class Minswap extends BaseDex {
addressType: AddressType.Base,
assetBalances: relevantUtxo.assetBalances,
isInlineDatum: false,
spendUtxos: [relevantUtxo],
spendUtxos: [{
utxo: relevantUtxo,
redeemer: this.cancelDatum,
validator: this.orderScript,
signer: returnAddress,
}],
}
];
}
Expand Down
42 changes: 29 additions & 13 deletions src/providers/wallet/lucid-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { AssetBalance, BlockfrostConfig, Cip30Api, KupmiosConfig, PayToAddress, UTxO, WalletOptions } from '@app/types';
import {
AssetBalance,
BlockfrostConfig,
Cip30Api,
KupmiosConfig,
PayToAddress,
SpendUTxO,
WalletOptions
} from '@app/types';
import { DexTransaction } from '@dex/models/dex-transaction';
import { BaseWalletProvider } from './base-wallet-provider';
import {
Expand All @@ -7,7 +15,7 @@ import {
Assets,
Blockfrost,
Datum, Kupmios,
Lucid, OutputData,
Lucid,
TxComplete,
TxHash,
TxSigned,
Expand Down Expand Up @@ -91,17 +99,25 @@ export class LucidProvider extends BaseWalletProvider {

// Include UTxOs to spend
if (payToAddress.spendUtxos && payToAddress.spendUtxos.length > 0) {
transaction.providerData.tx.collectFrom(
payToAddress.spendUtxos.map((utxo: UTxO) => {
return {
txHash: utxo.txHash,
outputIndex: utxo.outputIndex,
address: utxo.address,
datumHash: utxo.datumHash,
assets: this.paymentFromAssets(utxo.assetBalances)
};
})
);
payToAddress.spendUtxos.forEach((spendUtxo: SpendUTxO) => {
transaction.providerData.tx.collectFrom([
{
txHash: spendUtxo.utxo.txHash,
outputIndex: spendUtxo.utxo.outputIndex,
address: spendUtxo.utxo.address,
datumHash: spendUtxo.utxo.datumHash,
assets: this.paymentFromAssets(spendUtxo.utxo.assetBalances)
}
], spendUtxo.redeemer);

if (spendUtxo.validator) {
transaction.providerData.tx.attachSpendingValidator(spendUtxo.validator);
}

if (spendUtxo.signer) {
transaction.providerData.tx.addSigner(spendUtxo.signer);
}
});
}

switch (payToAddress.addressType) {
Expand Down
10 changes: 9 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AddressType, DatumParameterKey, TransactionStatus } from './constants';
import { Token } from '@dex/models/asset';
import { BaseDex } from '@dex/base-dex';
import { LiquidityPool } from '@dex/models/liquidity-pool';
import { Script } from 'lucid-cardano';

export interface DexterConfig {
shouldFetchMetadata?: boolean,
Expand Down Expand Up @@ -82,11 +83,18 @@ export type WalletOptions = {
accountIndex?: number,
}

export type SpendUTxO = {
utxo: UTxO,
redeemer?: string,
validator?: Script,
signer?: string,
};

export type PayToAddress = {
address: string,
addressType: AddressType,
assetBalances: AssetBalance[],
spendUtxos?: UTxO[],
spendUtxos?: SpendUTxO[],
datum?: string,
isInlineDatum: boolean,
};
Expand Down

0 comments on commit 4478976

Please sign in to comment.