Skip to content

Commit

Permalink
#490 Extract Wallet interface and related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
satran004 committed Feb 26, 2025
1 parent 8da8425 commit 7511055
Show file tree
Hide file tree
Showing 8 changed files with 622 additions and 347 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ public static TxBuilder adjustChangeOutput(String changeAddress, int noOfSigners
return adjustChangeOutput(AddressIterators.of(changeAddress), changeAddress, noOfSigners);
}

/**
* Function to adjust change output in a <code>Transaction</code> to meet min ada requirement.
* Finds a change output specific to given change address.
* If multiple change outputs with less than min required ada are found for the change address, it throws <code>{@link TxBuildException}</code>
* Get additional utxos from sender address and update the change output.
* Re-calculates fee and checks min ada in change output.
* Retry if required, upto 3 times
*
* @param senderAddress Address to select additional utxos
* @param changeAddress Address for change output selection
* @param noOfSigners No of required signers. Required for fee calculation after adjustment
* @return <code>TxBuilder</code> function
* @throws TxBuildException If multiple change outputs with less than min required ada are found for the change address.
* @throws ApiRuntimeException If api call error
*/
public static TxBuilder adjustChangeOutput(String senderAddress, String changeAddress, int noOfSigners) {
return adjustChangeOutput(AddressIterators.of(senderAddress), changeAddress, noOfSigners);
}

/**
* Function to adjust change output in a <code>Transaction</code> to meet min ada requirement.
* Finds a change output specific to given change address.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bloxbean.cardano.client.function.helper;

import com.bloxbean.cardano.client.api.AddressIterator;
import com.bloxbean.cardano.client.api.UtxoSupplier;
import com.bloxbean.cardano.client.api.exception.ApiException;
import com.bloxbean.cardano.client.api.helper.FeeCalculationService;
Expand Down Expand Up @@ -157,7 +158,9 @@ void adjustFor_onlyLovelaceOutput_withMultipleAdditionalUtxos() throws ApiExcept
);

given(utxoSelector.findFirst(eq(changeAddress), any(), any())).willReturn(Optional.empty());
given(utxoSelectionStrategy.selectUtxos(eq(changeAddress), eq(LOVELACE), any(), anySet())).willReturn(additionalUtxos);
given(utxoSelectionStrategy.selectUtxos((AddressIterator) argThat(arg -> arg instanceof AddressIterator),
eq(LOVELACE), any(), anySet()))
.willReturn(additionalUtxos);

Transaction transaction = new Transaction();
//Just adding a random input/output
Expand Down Expand Up @@ -287,7 +290,10 @@ void adjustFor_onlyLovelaceOutput_multipleFeeCalculationRetry() throws ApiExcept
);

given(utxoSelector.findFirst(eq(changeAddress), any(), any())).willReturn(Optional.empty());
given(utxoSelectionStrategy.selectUtxos(eq(changeAddress), eq(LOVELACE), any(), anySet())).willReturn(additionalUtxos);
given(utxoSelectionStrategy.selectUtxos(
(AddressIterator) argThat(arg -> arg instanceof AddressIterator),
eq(LOVELACE), any(), anySet()))
.willReturn(additionalUtxos);

Transaction transaction = new Transaction();
//Just adding a random input/output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ void setup() {

@Test
void alwaysTrueScript() throws ApiException {
Wallet walletA = new Wallet(Networks.testnet());
Wallet walletB = new Wallet(Networks.testnet());
Wallet walletA = Wallet.create(Networks.testnet());
Wallet walletB = Wallet.create(Networks.testnet());

splitPaymentBetweenAddress(topupAccount, walletA, 20, Double.valueOf(3000), false);
payToAddressAt(topupAccount, walletB, 13, Double.valueOf(5));
Expand Down Expand Up @@ -116,9 +116,9 @@ void alwaysTrueScript() throws ApiException {

@Test
void alwaysTrueScript_separateCollateralPayer() throws ApiException {
Wallet walletA = new Wallet(Networks.testnet());
Wallet walletB = new Wallet(Networks.testnet());
Wallet collaterWallet = new Wallet(Networks.testnet());
Wallet walletA = Wallet.create(Networks.testnet());
Wallet walletB = Wallet.create(Networks.testnet());
Wallet collaterWallet = Wallet.create(Networks.testnet());

splitPaymentBetweenAddress(topupAccount, walletA, 20, Double.valueOf(3000), false);
payToAddressAt(topupAccount, walletB, 13, Double.valueOf(5));
Expand Down
Loading

0 comments on commit 7511055

Please sign in to comment.