Skip to content

Commit

Permalink
refactor(tx-construction): txOutputBuilder satisfies OutputBuilder in…
Browse files Browse the repository at this point in the history
…terface

Using GenericTxBuilder.buildOutput should give access to all properties
of TxOutputBuilder, instead of just OutputBuilder interface.
  • Loading branch information
mirceahasegan committed May 29, 2023
1 parent 78d20c7 commit 2a88f0f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
28 changes: 12 additions & 16 deletions packages/e2e/test/wallet/PersonalWallet/multiAddress.test.ts
Expand Up @@ -65,17 +65,13 @@ describe('PersonalWallet/multiAddress', () => {
);

addressesToBeDiscovered.push(address);

const txOutput = await txBuilder.buildOutput().address(address.address).coin(3_000_000n).build();
txBuilder.addOutput(txOutput);
txBuilder.addOutput(txBuilder.buildOutput().address(address.address).coin(3_000_000n).toTxOut());
}

// Remove duplicates
addressesToBeDiscovered = [...new Set(addressesToBeDiscovered)];

const unsignedTx = txBuilder.build();

const { tx: signedTx } = await unsignedTx.sign();
const { tx: signedTx } = await txBuilder.build().sign();

await wallet.submitTx(signedTx);

Expand Down Expand Up @@ -125,17 +121,17 @@ describe('PersonalWallet/multiAddress', () => {
txBuilder = newWallet.wallet.createTxBuilder();

const fundingWalletAddresses = await firstValueFromTimed(wallet.addresses$);
const returnAdaOutput = await txBuilder
.buildOutput()
.address(fundingWalletAddresses[0].address)
.coin(totalBalance.coins - 1_500_000n) // Let's leave some behind for fees.
.build();

txBuilder.addOutput(returnAdaOutput);

const returnAdaUnsignedTx = await txBuilder.build();

const { tx: returnAdaSignedTx } = await returnAdaUnsignedTx.sign();
const { tx: returnAdaSignedTx } = await txBuilder
.addOutput(
txBuilder
.buildOutput()
.address(fundingWalletAddresses[0].address)
.coin(totalBalance.coins - 1_500_000n) // Let's leave some behind for fees.
.toTxOut()
)
.build()
.sign();
await newWallet.wallet.submitTx(returnAdaSignedTx);

// Search chain history to see if the transaction is there.
Expand Down
12 changes: 6 additions & 6 deletions packages/tx-construction/src/tx-builder/OutputBuilder.ts
Expand Up @@ -81,37 +81,37 @@ export class TxOutputBuilder implements OutputBuilder {
return this.#partialOutput;
}

value(value: Cardano.Value): OutputBuilder {
value(value: Cardano.Value): TxOutputBuilder {
this.#partialOutput = { ...this.#partialOutput, value: { ...value } };
return this;
}

coin(coin: Cardano.Lovelace): OutputBuilder {
coin(coin: Cardano.Lovelace): TxOutputBuilder {
this.#partialOutput = { ...this.#partialOutput, value: { ...this.#partialOutput?.value, coins: coin } };
return this;
}

assets(assets: Cardano.TokenMap): OutputBuilder {
assets(assets: Cardano.TokenMap): TxOutputBuilder {
this.#partialOutput = {
...this.#partialOutput,
value: { ...this.#partialOutput?.value, assets }
};
return this;
}

asset(assetId: Cardano.AssetId, quantity: bigint): OutputBuilder {
asset(assetId: Cardano.AssetId, quantity: bigint): TxOutputBuilder {
const assets: Cardano.TokenMap = new Map(this.#partialOutput?.value?.assets);
quantity === 0n ? assets.delete(assetId) : assets.set(assetId, quantity);

return this.assets(assets);
}

address(address: Cardano.PaymentAddress): OutputBuilder {
address(address: Cardano.PaymentAddress): TxOutputBuilder {
this.#partialOutput = { ...this.#partialOutput, address };
return this;
}

datum(datumHash: Hash32ByteBase16): OutputBuilder {
datum(datumHash: Hash32ByteBase16): TxOutputBuilder {
this.#partialOutput = { ...this.#partialOutput, datumHash };
return this;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/tx-construction/src/tx-builder/TxBuilder.ts
@@ -1,8 +1,8 @@
import * as Crypto from '@cardano-sdk/crypto';
import { Cardano } from '@cardano-sdk/core';
import { Logger } from 'ts-log';
import { OutputBuilderValidator, TxOutputBuilder } from './OutputBuilder';
import {
OutputBuilder,
PartialTx,
PartialTxOut,
RewardAccountMissingError,
Expand All @@ -14,7 +14,6 @@ import {
TxOutValidationError,
UnsignedTx
} from './types';
import { OutputBuilderValidator, TxOutputBuilder } from './OutputBuilder';
import { SelectionSkeleton } from '@cardano-sdk/input-selection';
import { SignTransactionOptions, TransactionSigner } from '@cardano-sdk/key-management';
import { contextLogger, deepEquals } from '@cardano-sdk/util';
Expand Down Expand Up @@ -119,7 +118,7 @@ export class GenericTxBuilder implements TxBuilder {
return this;
}

buildOutput(txOut?: PartialTxOut): OutputBuilder {
buildOutput(txOut?: PartialTxOut): TxOutputBuilder {
return new TxOutputBuilder({
logger: contextLogger(this.#logger, 'outputBuilder'),
outputValidator: this.#outputValidator,
Expand Down

0 comments on commit 2a88f0f

Please sign in to comment.