Skip to content

Commit

Permalink
Merge pull request #1 from input-output-hk/feat/lw-10017-specify-vali…
Browse files Browse the repository at this point in the history
…dity-interval-in-tx

[LW-10017] specify validity interval in tx
  • Loading branch information
VanessaPC committed May 9, 2024
2 parents 267cc5c + 335d1e0 commit f499764
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
46 changes: 46 additions & 0 deletions src/components/wallet-actions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
sendSeveralAssets,
singleDelegation,
singleUndelegation,
buildTxWithValidityInterval,
} from "../../features";
import { useEffect, useState } from "preact/hooks";

import "./wallet-actions.css";
import { ObservableWallet } from "@cardano-sdk/wallet";

export const WalletActions = () => {
const [storeState, setStoreState] = useState<Store>(
Expand Down Expand Up @@ -99,6 +101,28 @@ export const WalletActions = () => {
}
};

const handleValidityInterval = async ({
options,
title,
}: {
options: boolean;
title: string;
}) => {
if (!storeState.wallet) {
return null;
}
const { hash, txId } = await buildTxWithValidityInterval({
connectedWallet: storeState.wallet,
expired: options,
});

connectorStore.log({
hash,
title,
txId,
});
};

return (
<div className="actions-container">
<h3>Wallet actions</h3>
Expand All @@ -114,6 +138,28 @@ export const WalletActions = () => {
<button className="wallet-button" onClick={handleSingleUndelegation}>
Single undelegation
</button>
<button
class="wallet-button"
onClick={() =>
handleValidityInterval({
options: true,
title: "Tx with expired validity interval",
})
}
>
Send Tx with expired validity interval
</button>
<button
class="wallet-button"
onClick={() =>
handleValidityInterval({
options: false,
title: "Tx with unlimited validity interval",
})
}
>
Send Tx with unlimited validity interval
</button>
</div>
);
};
44 changes: 44 additions & 0 deletions src/features/buildTxWithValidityInterval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { firstValueFrom } from "rxjs";
import { Cardano } from "@cardano-sdk/core";
import type { ObservableWallet } from "@cardano-sdk/wallet";

import { inspectAndSignTx } from "../utils";

export const buildTxWithValidityInterval = async ({
connectedWallet,
expired,
}: {
connectedWallet: ObservableWallet;
expired: boolean;
}): Promise<{ hash: string; txId: string } | null> => {
if (!connectedWallet) {
return null;
}

const tip = await firstValueFrom(connectedWallet.tip$);

const expiredValidityInterval: Cardano.ValidityInterval = {
invalidHereafter: Cardano.Slot(tip.blockNo),
};

const noLimitValidityInterval: Cardano.ValidityInterval = {
invalidHereafter: undefined,
};

const builder = connectedWallet.createTxBuilder();
const builtTx = builder.addOutput(
await builder.buildOutput().handle("rhys").coin(10_000_000n).build()
);
const expiredValidityIntervalTx = builtTx
.setValidityInterval(
expired ? expiredValidityInterval : noLimitValidityInterval
)
.build();

const { hash, txId } = await inspectAndSignTx({
builtTx: expiredValidityIntervalTx,
connectedWallet,
});

return { hash, txId };
};
9 changes: 5 additions & 4 deletions src/features/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { sendCoins } from './sendCoins';
export { sendSeveralAssets } from './sendSeveralAssets';
export { singleDelegation } from './singleDelegation';
export { singleUndelegation } from './singleUndelegation';
export { sendCoins } from "./sendCoins";
export { sendSeveralAssets } from "./sendSeveralAssets";
export { singleDelegation } from "./singleDelegation";
export { singleUndelegation } from "./singleUndelegation";
export { buildTxWithValidityInterval } from "./buildTxWithValidityInterval";

0 comments on commit f499764

Please sign in to comment.