Skip to content

Commit

Permalink
Refactor TxBuilderContext and QuickTxBuilder
Browse files Browse the repository at this point in the history
Move the transaction building logic into a dedicated private method in `TxBuilderContext`. Refactor `QuickTxBuilder` to use the new build method and streamline the signing process, eliminating redundant code and improving code readability.
  • Loading branch information
satran004 committed Nov 24, 2024
1 parent 588d3ae commit 3894bee
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,7 @@ public static TxBuilderContext init(UtxoSupplier utxoSupplier, ProtocolParamsSup
* @throws com.bloxbean.cardano.client.function.exception.TxBuildException if exception during transaction build
*/
public Transaction build(TxBuilder txBuilder) {
Transaction transaction = new Transaction();
transaction.setEra(getSerializationEra());
txBuilder.apply(this, transaction);
Transaction transaction = buildTransaction(txBuilder);
clearTempStates();
return transaction;
}
Expand All @@ -282,8 +280,10 @@ public Transaction build(TxBuilder txBuilder) {
* @throws com.bloxbean.cardano.client.function.exception.TxBuildException if exception during transaction build
*/
public Transaction buildAndSign(TxBuilder txBuilder, TxSigner signer) {
Transaction transaction = build(txBuilder);
return signer.sign(transaction);
Transaction transaction = buildTransaction(txBuilder);
Transaction signedTransaction = signer.sign(this, transaction);
clearTempStates();
return signedTransaction;
}

/**
Expand All @@ -297,6 +297,13 @@ public void build(Transaction transaction, TxBuilder txBuilder) {
clearTempStates();
}

private Transaction buildTransaction(TxBuilder txBuilder) {
Transaction transaction = new Transaction();
transaction.setEra(getSerializationEra());
txBuilder.apply(this, transaction);
return transaction;
}

private void clearTempStates() {
clearMintMultiAssets();
clearUtxos();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.bloxbean.cardano.client.transaction.spec.Transaction;
import com.bloxbean.cardano.client.transaction.spec.TransactionInput;
import com.bloxbean.cardano.client.util.JsonUtil;
import com.bloxbean.cardano.hdwallet.Wallet;
import com.bloxbean.cardano.client.util.Tuple;
import com.bloxbean.cardano.hdwallet.supplier.WalletUtxoSupplier;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -116,6 +116,12 @@ public QuickTxBuilder(BackendService backendService) {
}
}

/**
* Create a QuickTxBuilder instance with specified BackendService and UtxoSupplier.
*
* @param backendService backend service to get protocol params and submit transactions
* @param utxoSupplier utxo supplier to get utxos
*/
public QuickTxBuilder(BackendService backendService, UtxoSupplier utxoSupplier) {
this(utxoSupplier,
new DefaultProtocolParamsSupplier(backendService.getEpochService()),
Expand Down Expand Up @@ -166,7 +172,6 @@ public class TxContext {
private boolean ignoreScriptCostEvaluationError = true;
private Era serializationEra;
private boolean removeDuplicateScriptWitnesses = false;
private Wallet signerWallet;

TxContext(AbstractTx... txs) {
this.txList = txs;
Expand Down Expand Up @@ -234,6 +239,25 @@ public TxContext additionalSignersCount(int additionalSigners) {
* @return Transaction
*/
public Transaction build() {
Tuple<TxBuilderContext, TxBuilder> tuple = _build();
return tuple._1.build(tuple._2);
}

/**
* Build and sign transaction
*
* @return Transaction
*/
public Transaction buildAndSign() {
Tuple<TxBuilderContext, TxBuilder> tuple = _build();

if (signers != null)
return tuple._1.buildAndSign(tuple._2, signers);
else
throw new IllegalStateException("No signers found");
}

private Tuple<TxBuilderContext, TxBuilder> _build() {
TxBuilder txBuilder = (context, txn) -> {
};
boolean containsScriptTx = false;
Expand Down Expand Up @@ -383,7 +407,8 @@ public Transaction build() {
tx.postBalanceTx(transaction);
}));
}
return txBuilderContext.build(txBuilder);

return new Tuple<>(txBuilderContext, txBuilder);
}

private int getTotalSigners() {
Expand All @@ -394,24 +419,6 @@ private int getTotalSigners() {
return totalSigners;
}

/**
* Build and sign transaction
*
* @return Transaction
*/
public Transaction buildAndSign() {
Transaction transaction = build();
if (signers != null)
transaction = signers.sign(transaction);
if(signerWallet != null) {
if(!(utxoSupplier instanceof WalletUtxoSupplier))
throw new TxBuildException("Provide a WalletUtxoSupplier when using a sender wallet");
transaction = signerWallet.sign(transaction, (WalletUtxoSupplier) utxoSupplier);
}

return transaction;
}

private TxBuilder buildCollateralOutput(String feePayer) {
if (collateralInputs != null && !collateralInputs.isEmpty()) {
List<Utxo> collateralUtxos = collateralInputs.stream()
Expand Down Expand Up @@ -580,15 +587,6 @@ public TxContext withSigner(@NonNull TxSigner signer) {
this.signers = this.signers.andThen(signer);
return this;
}
/**
* Sign transaction with the given wallet
* @param wallet
* @return TxContext
*/
public TxContext withSigner(Wallet wallet) {
this.signerWallet = wallet;
return this;
}

/**
* Add validity start slot to the transaction. This value is set in "validity start from" field of the transaction.
Expand Down

0 comments on commit 3894bee

Please sign in to comment.