Skip to content

Commit

Permalink
fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
m1n999999 committed Nov 27, 2024
1 parent ec6b7ee commit 408860e
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 83 deletions.
91 changes: 68 additions & 23 deletions docs/transaction.md → docs/dex-transaction.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Minswap AMM V2 & Stableswap Classes Documentation
# Minswap DEX Classes documentation

## Overview

Expand All @@ -9,6 +9,8 @@ This documentation provides details on how to interact with the **Stableswap** a
- **Stableswap class**: Located in `src/stableswap.ts`.
- **AMM V2 class**: Located in `src/dex-v2.ts`.
- **Example file**: Demonstrates usage of both classes, located in `examples/example.ts`.
- **DexV2Worker Class**: Located in `src/dex-v2-worker.ts`.
- **DexV2Worker Example**: Located in `examples/dex-v2-worker-example.ts`.

### Utility Functions

Expand Down Expand Up @@ -54,7 +56,10 @@ const blockfrostAdapter = new BlockfrostAdapter(
const utxos = await lucid.utxosAt(address);

const lpAsset = Asset.fromString("<STABLE_POOL_LP_ASSET>");
const config = StableswapConstant.getConfigByLpAsset(lpAsset, NetworkId.TESTNET);
const config = StableswapConstant.getConfigByLpAsset(
lpAsset,
NetworkId.TESTNET
);

const pool = await blockfrostAdapter.getStablePoolByLpAsset(lpAsset);

Expand Down Expand Up @@ -92,7 +97,9 @@ const txComplete = await new Stableswap(lucid).createBulkOrdersTx({
],
});

const signedTx = await txComplete.signWithPrivateKey("<YOUR_PRIVATE_KEY>").complete();
const signedTx = await txComplete
.signWithPrivateKey("<YOUR_PRIVATE_KEY>")
.complete();
const txId = await signedTx.submit();
console.info(`Transaction submitted successfully: ${txId}`);
```
Expand Down Expand Up @@ -144,27 +151,30 @@ const acceptedAmountOut = Slippage.apply({
type: "down",
});

const txComplete = await new DexV2(lucid, blockfrostAdapter).createBulkOrdersTx({
sender: address,
availableUtxos: utxos,
orderOptions: [
{
type: OrderV2.StepType.SWAP_EXACT_IN,
amountIn: swapAmount,
assetIn: assetA,
direction: OrderV2.Direction.A_TO_B,
minimumAmountOut: acceptedAmountOut,
lpAsset: pool.lpAsset,
isLimitOrder: false,
killOnFailed: false,
},
],
});
const txComplete = await new DexV2(lucid, blockfrostAdapter).createBulkOrdersTx(
{
sender: address,
availableUtxos: utxos,
orderOptions: [
{
type: OrderV2.StepType.SWAP_EXACT_IN,
amountIn: swapAmount,
assetIn: assetA,
direction: OrderV2.Direction.A_TO_B,
minimumAmountOut: acceptedAmountOut,
lpAsset: pool.lpAsset,
isLimitOrder: false,
killOnFailed: false,
},
],
}
);

const signedTx = await txComplete.signWithPrivateKey("<YOUR_PRIVATE_KEY>").complete();
const signedTx = await txComplete
.signWithPrivateKey("<YOUR_PRIVATE_KEY>")
.complete();
const txId = await signedTx.submit();
console.info(`Transaction submitted successfully: ${txId}`);

```

### 3. Create the DEX V2 Liquiditiy Pool
Expand Down Expand Up @@ -204,19 +214,54 @@ const txComplete = await new DexV2(lucid, blockfrostAdapter).createPoolTx({
tradingFeeNumerator: 100n,
});

const signedTx = await txComplete.signWithPrivateKey("<YOUR_PRIVATE_KEY>").complete();
const signedTx = await txComplete
.signWithPrivateKey("<YOUR_PRIVATE_KEY>")
.complete();
const txId = await signedTx.submit();
console.info(`Transaction submitted successfully: ${txId}`);
```

### 4. Run Dex V2 Worker

```ts
const network: Network = "Preprod";
const blockfrostProjectId = "<YOUR_BLOCKFROST_API_KEY>";
const blockfrostUrl = "https://cardano-preprod.blockfrost.io/api/v0";

const address = "<YOUR_ADDRESS>";
const lucid = await getBackendLucidInstance(
network,
blockfrostProjectId,
blockfrostUrl,
address
);

const blockfrostAdapter = new BlockfrostAdapter(
NetworkId.TESTNET,
new BlockFrostAPI({
projectId: blockfrostProjectId,
network: "preprod",
})
);

const worker = new DexV2Worker({
lucid,
blockfrostAdapter,
privateKey: "<YOUR_PRIVATE_KEY>",
});

await worker.start();
```

## Additional Examples

You can explore more examples in the [Examples](../examples/example.ts) folder to learn how to integrate the Stableswap and DexV2 classes in more complex scenarios.

## Conclusion

The Stableswap and AMM V2 classes offer powerful tools for interacting with Minswap’s decentralized exchange. They allow users to easily manage liquidity pools and make swaps, with built-in support for Minswap Batcher Fee discounts. By utilizing these classes, users can create efficient transactions and leverage the utility of $MIN to reduce costs.

For more details, you can refer to the specific class files:

- [Stableswap class](../src/stableswap.ts)
- [AMM V2 class](../src/dex-v2.ts)
- [AMM V2 class](../src/dex-v2.ts)
44 changes: 0 additions & 44 deletions docs/dex-v2-worker.md

This file was deleted.

25 changes: 12 additions & 13 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,20 +594,19 @@ export class BlockfrostAdapter implements Adapter {
utxo.amount,
utxo.inline_datum
);
} else {
if (utxo.data_hash !== null) {
const orderDatum = await this.blockFrostApi.scriptsDatumCbor(
utxo.data_hash
);
order = new OrderV2.State(
this.networkId,
utxo.address,
{ txHash: utxo.tx_hash, index: utxo.output_index },
utxo.amount,
orderDatum.cbor
);
}
} else if (utxo.data_hash !== null) {
const orderDatum = await this.blockFrostApi.scriptsDatumCbor(
utxo.data_hash
);
order = new OrderV2.State(
this.networkId,
utxo.address,
{ txHash: utxo.tx_hash, index: utxo.output_index },
utxo.amount,
orderDatum.cbor
);
}

if (order === undefined) {
throw new Error(`Cannot find datum of Order V2, tx: ${utxo.tx_hash}`);
}
Expand Down
7 changes: 5 additions & 2 deletions src/dex-v2-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export class DexV2Worker {
}
const receiverDatum = orderDatum.refundReceiverDatum;
let rawDatum: string | undefined = undefined;
if (receiverDatum.type === OrderV2.ExtraDatumType.INLINE_DATUM) {
if (
receiverDatum.type === OrderV2.ExtraDatumType.INLINE_DATUM ||
receiverDatum.type === OrderV2.ExtraDatumType.DATUM_HASH
) {
try {
rawDatum = await this.blockfrostAdapter.getDatumByDatumHash(
receiverDatum.hash
Expand Down Expand Up @@ -104,7 +107,7 @@ export class DexV2Worker {
const txId = await signedTx.submit();
console.info(`Transaction submitted successfully: ${txId}`);
} catch (_err) {
console.log(
console.error(
`Error when the worker runs: orders ${orders.map((order) => `${order.txIn.txHash}#${order.txIn.index}`).join(", ")}`,
_err
);
Expand Down
6 changes: 5 additions & 1 deletion src/dex-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1069,9 +1069,13 @@ export class DexV2 {
break;
}
case OrderV2.ExtraDatumType.DATUM_HASH: {
invariant(
refundDatum.hash in extraDatumMap,
`Can not find refund datum of order ${orderUtxo.txHash}#${orderUtxo.outputIndex}`
);
lucidTx.payToAddressWithData(
datum.refundReceiver,
{ hash: refundDatum.hash },
{ asHash: extraDatumMap[refundDatum.hash] },
outAssets
);
break;
Expand Down

0 comments on commit 408860e

Please sign in to comment.