Skip to content

Commit

Permalink
Enable Script address to use dexV2 tx creation (#34)
Browse files Browse the repository at this point in the history
* Enabling composite transaction for multisig and smart wallets

This will allow creating trasactions that fulfill the requrments of the multisig or smart wallet.

By providing a composable Tx that fufils the requirements of whatever wallet you are developing against you are able to get full support by simply composing this transaction into the target Tx.

* Editing tx creation to allow for script owned address

* Update dex-v2.ts
  • Loading branch information
leo42 authored Aug 22, 2024
1 parent 02bf707 commit 400c777
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 34 additions & 17 deletions src/dex-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Data,
Lucid,
OutRef,
Tx,
TxComplete,
UTxO,
} from "lucid-cardano";
Expand Down Expand Up @@ -52,6 +53,8 @@ export type BulkOrdersOption = {
orderOptions: OrderOptions[];
expiredOptions?: OrderV2.ExpirySetting;
availableUtxos: UTxO[];
composeTx?: Tx;
authorizationMethodType? : OrderV2.AuthorizationMethodType;
};

export type OrderV2SwapRouting = {
Expand Down Expand Up @@ -164,6 +167,8 @@ export type OrderOptions = (

export type CancelBulkOrdersOptions = {
orderOutRefs: OutRef[];
composeTx? : Tx,
AuthorizationMethodType? : OrderV2.AuthorizationMethodType;
};

export class DexV2 {
Expand Down Expand Up @@ -731,7 +736,9 @@ export class DexV2 {
orderOptions,
expiredOptions,
availableUtxos,
}: BulkOrdersOption): Promise<TxComplete> {
composeTx,
authorizationMethodType
}: BulkOrdersOption): Promise<TxComplete > {
// calculate total order value
const totalOrderAssets: Record<string, bigint> = {};
for (const option of orderOptions) {
Expand Down Expand Up @@ -772,17 +779,19 @@ export class DexV2 {
} else {
orderAssets["lovelace"] = totalBatcherFee;
}
const senderPaymentCred =
this.lucid.utils.getAddressDetails(sender).paymentCredential;
invariant(
senderPaymentCred?.type === "Key",
"sender pub key hash not found"
);

const senderPaymentCred = this.lucid.utils.getAddressDetails(sender).paymentCredential;
invariant( senderPaymentCred, "sender address payment credentials not found");

const canceller = authorizationMethodType ? {
type: authorizationMethodType,
hash: senderPaymentCred.hash,
} : {
type: OrderV2.AuthorizationMethodType.SIGNATURE,
hash: senderPaymentCred.hash,
};
const orderDatum: OrderV2.Datum = {
canceller: {
type: OrderV2.AuthorizationMethodType.SIGNATURE,
hash: senderPaymentCred.hash,
},
canceller: canceller,
refundReceiver: sender,
refundReceiverDatum: {
type: OrderV2.ExtraDatumType.NO_DATUM,
Expand Down Expand Up @@ -824,11 +833,18 @@ export class DexV2 {
msg: [metadata],
limitOrders: limitOrderMessage,
});
return lucidTx.payToAddress(sender, reductionAssets).complete();
lucidTx.payToAddress(sender, reductionAssets);
if (composeTx){
lucidTx.compose(composeTx);
}
return lucidTx.complete();

}

async cancelOrder({
orderOutRefs,
composeTx,

}: CancelBulkOrdersOptions): Promise<TxComplete> {
const v2OrderScriptHash = this.getOrderScriptHash();
const orderUtxos = await this.lucid.utxosByOutRef(orderOutRefs);
Expand Down Expand Up @@ -873,11 +889,9 @@ export class DexV2 {
"Utxo without Datum Hash or Inline Datum can not be spent"
);
}
invariant(
datum.canceller.type === OrderV2.AuthorizationMethodType.SIGNATURE,
"only support PubKey canceller on this function"
);
requiredPubKeyHashSet.add(datum.canceller.hash);

if(datum.canceller.type === OrderV2.AuthorizationMethodType.SIGNATURE)
requiredPubKeyHashSet.add(datum.canceller.hash);
}
const redeemer = Data.to(
new Constr(OrderV2.Redeemer.CANCEL_ORDER_BY_OWNER, [])
Expand All @@ -890,6 +904,9 @@ export class DexV2 {
lucidTx.attachMetadata(674, {
msg: [MetadataMessage.CANCEL_ORDER],
});
if (composeTx){
lucidTx.compose(composeTx);
}
return lucidTx.complete();
}
}

0 comments on commit 400c777

Please sign in to comment.