Skip to content

Commit

Permalink
wip: support for order routing
Browse files Browse the repository at this point in the history
  • Loading branch information
cjkoepke committed Jun 5, 2024
1 parent 09d347f commit 292633b
Show file tree
Hide file tree
Showing 7 changed files with 482 additions and 27 deletions.
10 changes: 5 additions & 5 deletions docs/typescript/core/classes/Lucid.TxBuilderLucidV1.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ TxBuilder.cancel

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:296](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L296)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:415](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L415)

___

Expand Down Expand Up @@ -244,7 +244,7 @@ const migrationResult = await sdk.builder().migrateLiquidityToV3([

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:760](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L760)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:878](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L878)

___

Expand Down Expand Up @@ -363,7 +363,7 @@ TxBuilder.update

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:400](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L400)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:518](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L518)

___

Expand Down Expand Up @@ -405,7 +405,7 @@ TxBuilder.withdraw

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:540](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L540)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:658](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L658)

___

Expand Down Expand Up @@ -447,7 +447,7 @@ TxBuilder.zap

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:592](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L592)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts:710](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts#L710)

___

Expand Down
38 changes: 19 additions & 19 deletions docs/typescript/core/classes/Lucid.TxBuilderLucidV3.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TxBuilder.constructor

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:99](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L99)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:100](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L100)

## Properties

Expand All @@ -54,7 +54,7 @@ TxBuilder.datumBuilder

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:101](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L101)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:102](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L102)

___

Expand All @@ -66,7 +66,7 @@ A configured Lucid instance to use.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:100](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L100)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:101](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L101)

## Methods

Expand Down Expand Up @@ -96,7 +96,7 @@ TxBuilder.cancel

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:560](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L560)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:686](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L686)

___

Expand Down Expand Up @@ -126,7 +126,7 @@ TxBuilder.deposit

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:728](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L728)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:854](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L854)

___

Expand All @@ -151,7 +151,7 @@ The generated Bech32 address.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:959](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L959)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:1085](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L1085)

___

Expand All @@ -169,7 +169,7 @@ using the Lucid provider.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:138](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L138)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:139](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L139)

___

Expand All @@ -187,7 +187,7 @@ using the Lucid provider.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:175](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L175)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:176](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L176)

___

Expand All @@ -207,7 +207,7 @@ The maxScooperFee as defined by the settings UTXO.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:195](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L195)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:196](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L196)

___

Expand All @@ -228,7 +228,7 @@ will re-populate with real data.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:120](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L120)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:121](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L121)

___

Expand All @@ -248,7 +248,7 @@ ___

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:156](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L156)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:157](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L157)

___

Expand All @@ -273,7 +273,7 @@ Throws an error if the retrieval of UTXOs fails or if no UTXOs are available.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:1002](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L1002)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:1128](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L1128)

___

Expand All @@ -297,7 +297,7 @@ before returning a response.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:217](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L217)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:218](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L218)

___

Expand Down Expand Up @@ -327,7 +327,7 @@ Throws an error if the transaction fails to build or submit.

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:286](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L286)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:287](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L287)

___

Expand All @@ -354,7 +354,7 @@ fee payment if a [ITxBuilderReferralFee](../interfaces/Core.ITxBuilderReferralFe

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:240](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L240)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:241](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L241)

___

Expand Down Expand Up @@ -384,7 +384,7 @@ TxBuilder.swap

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:506](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L506)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:507](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L507)

___

Expand Down Expand Up @@ -416,7 +416,7 @@ TxBuilder.update

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:642](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L642)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:768](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L768)

___

Expand Down Expand Up @@ -446,7 +446,7 @@ TxBuilder.withdraw

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:776](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L776)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:902](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L902)

___

Expand Down Expand Up @@ -476,4 +476,4 @@ TxBuilder.zap

#### Defined in

[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:821](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L821)
[packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts:947](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/TxBuilders/TxBuilder.Lucid.V3.class.ts#L947)
1 change: 1 addition & 0 deletions packages/core/src/Abstracts/TxBuilder.abstract.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export abstract class TxBuilder {
abstract newTxInstance(): unknown;

abstract swap(args: unknown): Promise<IComposedTx>;
abstract orderRouteSwap(args: unknown): Promise<IComposedTx>;
abstract deposit(args: unknown): Promise<IComposedTx>;
abstract withdraw(args: unknown): Promise<IComposedTx>;
abstract update(args: unknown): Promise<IComposedTx>;
Expand Down
122 changes: 120 additions & 2 deletions packages/core/src/TxBuilders/TxBuilder.Lucid.V1.class.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AssetAmount, IAssetAmountMetadata } from "@sundaeswap/asset";
import { getTokensForLp } from "@sundaeswap/cpp";
import { getSwapOutput, getTokensForLp } from "@sundaeswap/cpp";
import type {
Assets,
Datum,
Expand Down Expand Up @@ -275,6 +275,125 @@ export class TxBuilderLucidV1 extends TxBuilder {
});
}

async orderRouteSwap(args: {
swapA: ISwapConfigArgs;
swapB: Omit<ISwapConfigArgs, "suppliedAsset">;
}): Promise<
IComposedTx<
unknown,
unknown,
string | undefined,
Record<string, AssetAmount<IAssetAmountMetadata>>
>
> {
const swapA = new SwapConfig(args.swapA).buildArgs();

const [aReserve, bReserve] = SundaeUtils.sortSwapAssetsWithAmounts([
new AssetAmount(
args.swapA.pool.liquidity.aReserve,
args.swapA.pool.assetA
),
new AssetAmount(
args.swapA.pool.liquidity.bReserve,
args.swapA.pool.assetB
),
]);

const { output } = getSwapOutput(
swapA.suppliedAsset.amount,
aReserve.amount,
bReserve.amount,
swapA.pool.currentFee
);
const outputAsset =
swapA.suppliedAsset.metadata.assetId === aReserve.metadata.assetId
? bReserve
: aReserve;

const swapB = new SwapConfig({
...args.swapB,
suppliedAsset: outputAsset,
}).buildArgs();

const isSecondSwapV3 = swapB.pool.version === EContractVersion.V3;

const secondSwapBuilder = isSecondSwapV3
? new TxBuilderLucidV3(this.lucid, new DatumBuilderLucidV3(this.network))
: this;
const secondSwapAddress = isSecondSwapV3
? await (secondSwapBuilder as TxBuilderLucidV3).generateScriptAddress(
"order.spend"
)
: await this.getValidatorScript("escrow.spend").then(({ compiledCode }) =>
this.lucid.utils.validatorToAddress({
type: "PlutusV1",
script: compiledCode,
})
);

swapB.suppliedAsset = outputAsset.withAmount(output);
const secondSwapData = await secondSwapBuilder.swap({
...swapB,
swapType: args.swapB.swapType,
});

let referralFeeAmount = 0n;
if (swapA.referralFee) {
referralFeeAmount += swapA.referralFee.payment.amount;
}

if (swapB.referralFee) {
referralFeeAmount += swapB.referralFee.payment.amount;
}

let mergedReferralFee: ITxBuilderReferralFee | undefined;
if (swapA.referralFee) {
mergedReferralFee = {
...swapA.referralFee,
payment: swapA.referralFee.payment.withAmount(referralFeeAmount),
};
} else if (swapB.referralFee) {
mergedReferralFee = {
...swapB.referralFee,
payment: swapB.referralFee.payment.withAmount(referralFeeAmount),
};
}

const datumHash = this.lucid.utils.datumToHash(
secondSwapData.datum as string
);

const { tx, datum, fees } = await this.swap({
...args.swapA,
orderAddresses: {
...args.swapB.orderAddresses,
DestinationAddress: {
address: secondSwapAddress,
datum: {
type: EDatumType.HASH,
value: datumHash,
},
},
},
referralFee: mergedReferralFee,
});

tx.attachMetadataWithConversion(103251, {
[`0x${datumHash}`]: SundaeUtils.splitMetadataString(
secondSwapData.datum as string,
"0x"
),
});

return this.completeTx({
tx,
datum,
deposit: ORDER_DEPOSIT_DEFAULT * 2n,
referralFee: mergedReferralFee?.payment,
scooperFee: fees.scooperFee.add(secondSwapData.fees.scooperFee).amount,
});
}

/**
* Executes a cancel transaction based on the provided configuration arguments.
* Validates the datum and datumHash, retrieves the necessary UTXO data,
Expand Down Expand Up @@ -337,7 +456,6 @@ export class TxBuilderLucidV1 extends TxBuilder {
).attachSpendingValidator(scriptValidator);

const details = getAddressDetails(ownerAddress);
console.log(details);

const spendingDatum =
utxoToSpend[0]?.datumHash &&
Expand Down
Loading

0 comments on commit 292633b

Please sign in to comment.