Skip to content

Commit

Permalink
fix dex v2 tx building
Browse files Browse the repository at this point in the history
  • Loading branch information
baymac committed Aug 5, 2024
1 parent 951978f commit 7e9e7fe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
20 changes: 11 additions & 9 deletions examples/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ const MIN: Asset = {

async function main(): Promise<void> {
const network: Network = "Preprod";
const blockfrostProjectId = "<YOUR_BLOCKFROST_PROJECT_ID>";
const blockfrostProjectId = "preprodel6eWcyCZddTV1wezpV1uNlt0GpUVAcw";
const blockfrostUrl = "https://cardano-preprod.blockfrost.io/api/v0";

const address = "<YOUR_ADDRESS>";
const address =
"addr_test1vrd9v47japxwp8540vsrh4grz4u9urfpfawwy7sf6r0vxqgm7wdxh";
const lucid = await getBackendLucidInstance(
network,
blockfrostProjectId,
Expand All @@ -59,19 +60,20 @@ async function main(): Promise<void> {

const utxos = await lucid.utxosAt(address);

const txComplete = await _swapExactInTxExample(
network,
const txComplete = await _swapExactOutV2TxExample(
lucid,
blockfrostAdapter,
address,
utxos
);
const signedTx = await txComplete
.signWithPrivateKey("<YOUR_PRIVATE_KEY>")
.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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -423,7 +425,7 @@ async function _swapExactInV2TxExample(
});
}

async function _swapExactOutInV2Example(
async function _swapExactOutV2TxExample(
lucid: Lucid,
blockfrostAdapter: BlockfrostAdapter,
address: Address,
Expand All @@ -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");

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -68,4 +69,4 @@
"ts-node": "^10.9.1",
"typescript": "^4.6.3"
}
}
}
29 changes: 16 additions & 13 deletions src/dex-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,7 @@ export class DexV2 {
}

private buildOrderValue(options: OrderOptions): Record<string, bigint> {
const orderAssets: Assets = {
lovelace: FIXED_DEPOSIT_ADA,
};
const orderAssets: Assets = {};
switch (options.type) {
case OrderV2.StepType.DEPOSIT: {
const { assetA, assetB, amountA, amountB, minimumLPReceived } = options;
Expand All @@ -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 {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 } =
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -818,7 +822,6 @@ export class DexV2 {
msg: [metadata],
limitOrders: limitOrderMessage,
});

return lucidTx.payToAddress(sender, reductionAssets).complete();
}

Expand Down

0 comments on commit 7e9e7fe

Please sign in to comment.