diff --git a/examples/example.ts b/examples/example.ts index 6eb5015..1afa936 100644 --- a/examples/example.ts +++ b/examples/example.ts @@ -38,10 +38,11 @@ const MIN: Asset = { async function main(): Promise { const network: Network = "Preprod"; - const blockfrostProjectId = ""; + const blockfrostProjectId = "preprodel6eWcyCZddTV1wezpV1uNlt0GpUVAcw"; const blockfrostUrl = "https://cardano-preprod.blockfrost.io/api/v0"; - const address = ""; + const address = + "addr_test1vrd9v47japxwp8540vsrh4grz4u9urfpfawwy7sf6r0vxqgm7wdxh"; const lucid = await getBackendLucidInstance( network, blockfrostProjectId, @@ -59,19 +60,20 @@ async function main(): Promise { const utxos = await lucid.utxosAt(address); - const txComplete = await _swapExactInTxExample( - network, + const txComplete = await _swapExactOutV2TxExample( lucid, blockfrostAdapter, address, utxos ); const signedTx = await txComplete - .signWithPrivateKey("") + .signWithPrivateKey( + "ed25519e_sk1zplkdlsqnqtrayzgukq262rw203qdvg383rgtnpjxcpj5c2jgfvx8vz5xzkxz9exm2t08v2tfhrazp6qd260wqfs0x5vckxl6zftx7cf9f9e2" + ) .complete(); const txId = await signedTx.submit(); // eslint-disable-next-line no-console - console.log(`Transaction submitted successfully: ${txId}`); + console.info(`Transaction submitted successfully: ${txId}`); } async function getPoolById( @@ -390,7 +392,7 @@ async function _swapExactInV2TxExample( const pool = await blockfrostAdapter.getV2PoolByPair(assetA, assetB); invariant(pool, "could not find pool"); - const swapAmount = 10_000_000n; + const swapAmount = 5_000_000n; const amountOut = DexV2Calculation.calculateAmountOut({ reserveIn: pool.reserveA, reserveOut: pool.reserveB, @@ -423,7 +425,7 @@ async function _swapExactInV2TxExample( }); } -async function _swapExactOutInV2Example( +async function _swapExactOutV2TxExample( lucid: Lucid, blockfrostAdapter: BlockfrostAdapter, address: Address, @@ -432,7 +434,7 @@ async function _swapExactOutInV2Example( const assetA = ADA; const assetB = MIN; - const swapAmount = 8_000_000n; + const swapAmount = 10_000n; const pool = await blockfrostAdapter.getV2PoolByPair(assetA, assetB); invariant(pool, "could not find pool"); diff --git a/package.json b/package.json index f2ba25a..e52ce73 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "lint": "eslint src --ext .ts", "format": "prettier --write **/*.ts && eslint src --fix --ext .ts", "check-format": "prettier --check **/*.ts && eslint src --ext .ts", - "exec": "ts-node --esm --experimental-specifier-resolution=node" + "exec": "ts-node --esm --experimental-specifier-resolution=node", + "example": "ts-node --esm --experimental-specifier-resolution=node examples/example.ts" }, "keywords": [ "minswap", @@ -68,4 +69,4 @@ "ts-node": "^10.9.1", "typescript": "^4.6.3" } -} +} \ No newline at end of file diff --git a/src/dex-v2.ts b/src/dex-v2.ts index d6bcf7a..c1ddbbe 100644 --- a/src/dex-v2.ts +++ b/src/dex-v2.ts @@ -337,9 +337,7 @@ export class DexV2 { } private buildOrderValue(options: OrderOptions): Record { - const orderAssets: Assets = { - lovelace: FIXED_DEPOSIT_ADA, - }; + const orderAssets: Assets = {}; switch (options.type) { case OrderV2.StepType.DEPOSIT: { const { assetA, assetB, amountA, amountB, minimumLPReceived } = options; @@ -353,7 +351,7 @@ export class DexV2 { ); orderAssets[Asset.toString(assetA)] = amountA; orderAssets[Asset.toString(assetB)] = amountB; - return orderAssets; + break; } case OrderV2.StepType.WITHDRAW: { const { @@ -368,7 +366,7 @@ export class DexV2 { "minimum asset received must be positive" ); orderAssets[Asset.toString(lpAsset)] = lpAmount; - return orderAssets; + break; } case OrderV2.StepType.SWAP_EXACT_IN: { const { assetIn, amountIn, minimumAmountOut } = options; @@ -382,7 +380,7 @@ export class DexV2 { invariant(maximumAmountIn > 0n, "amount in must be positive"); invariant(expectedReceived > 0n, "minimum amount out must be positive"); orderAssets[Asset.toString(assetIn)] = maximumAmountIn; - return orderAssets; + break; } case OrderV2.StepType.STOP: { const { assetIn, amountIn, stopAmount } = options; @@ -397,14 +395,14 @@ export class DexV2 { invariant(stopAmount > 0n, "stop amount out must be positive"); invariant(limitAmount > 0n, "limit amount out must be positive"); orderAssets[Asset.toString(assetIn)] = amountIn; - return orderAssets; + break; } case OrderV2.StepType.ZAP_OUT: { const { lpAsset, lpAmount, minimumReceived } = options; invariant(lpAmount > 0n, "lp amount in must be positive"); invariant(minimumReceived > 0n, "minimum amount out must be positive"); orderAssets[Asset.toString(lpAsset)] = lpAmount; - return orderAssets; + break; } case OrderV2.StepType.PARTIAL_SWAP: { const { assetIn, amountIn, expectedInOutRatio } = options; @@ -417,7 +415,7 @@ export class DexV2 { "expected input and output ratio must be positive" ); orderAssets[Asset.toString(assetIn)] = amountIn; - return orderAssets; + break; } case OrderV2.StepType.WITHDRAW_IMBALANCE: { const { lpAsset, lpAmount, ratioAssetA, ratioAssetB, minimumAssetA } = @@ -428,15 +426,21 @@ export class DexV2 { "minimum asset and ratio received must be positive" ); orderAssets[Asset.toString(lpAsset)] = lpAmount; - return orderAssets; + break; } case OrderV2.StepType.SWAP_ROUTING: { const { assetIn, amountIn } = options; invariant(amountIn > 0n, "Amount must be positive"); orderAssets[Asset.toString(assetIn)] = amountIn; - return orderAssets; + break; } } + if ("lovelace" in orderAssets) { + orderAssets["lovelace"] += FIXED_DEPOSIT_ADA; + } else { + orderAssets["lovelace"] = FIXED_DEPOSIT_ADA; + } + return orderAssets; } buildOrderStep(options: OrderOptions, finalBatcherFee: bigint): OrderV2.Step { @@ -733,7 +737,7 @@ export class DexV2 { for (const option of orderOptions) { const orderAssets = this.buildOrderValue(option); for (const [asset, amt] of Object.entries(orderAssets)) { - if (totalOrderAssets[asset]) { + if (asset in totalOrderAssets) { totalOrderAssets[asset] += amt; } else { totalOrderAssets[asset] = amt; @@ -818,7 +822,6 @@ export class DexV2 { msg: [metadata], limitOrders: limitOrderMessage, }); - return lucidTx.payToAddress(sender, reductionAssets).complete(); }