Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does the library support using UTXOs from different addresses as inputs to transfer ada and tokens? #423

Open
lajiww opened this issue Sep 4, 2024 · 4 comments
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@lajiww
Copy link

lajiww commented Sep 4, 2024

hello.
I have a wallet. it contains many addresses and utxos. I want to use these utxos coming from different addresses to make a transfer. Whether i can use this lib to do this? I only see an example to using utxos coming from one sender.
If it can do, could you give me some examples or code snippets?
Thanks

@lajiww
Copy link
Author

lajiww commented Sep 6, 2024

Hello, could you give a reply?
Thanks

@satran004 satran004 added the question Further information is requested label Sep 7, 2024
@satran004
Copy link
Member

@lajiww

There is no example for this yet, but we will create a new guide following the approach mentioned below.

Currently, the Account API in the library handles a single address, and the underlying UtxoSelectionStrategy api takes one address as input. However, one of the Account API constructors accepts an index parameter. This can be used to create an account object at different indexes for a wallet, and to generate corresponding addresses.

You may then want to use the UtxoSupplier to retrieve UTXOs for each address and build the final transaction. Since you are using UTXOs from different addresses, you'll need to sign the final transaction with the corresponding accounts generated earlier.

So, if you maintain a map of address → Account, you can sign the transaction using the account associated with the UTXOs from that address.

To build transactions from UTXOs, you can use the QuickTx API or other APIs, including lower-level ones.

Here's an example with QuickTx:

Tx tx = new Tx()
           .collectFrom(utxos)
           .payToAddress(receiver, Amount.ada(4.5))
// Then to build and sign
Result<String> result = quickTxBuilder
           .compose(tx1)
           .withSigner(SignerProviders.signerFrom(account1))
           .withSigner(SignerProviders.signerFrom(account2))
           ...

This PR will implement the same functionality with a new HD Wallet API layer, but it will take some time to finalize: #363

@satran004 satran004 added the documentation Improvements or additions to documentation label Sep 7, 2024
@lajiww
Copy link
Author

lajiww commented Sep 7, 2024

Thank for your reply very much.
Now i override the method getAll(String address) of UtxoSupplier to return utxos from different addresses instead of one address when utxoSelectionStrategy selects utxos. The senderAddress seems only influence the selection of utxos. now the code seems ok.
it look like below:

        // Define TxBuilder
        TxBuilder txBuilder = output.outputBuilder()
                .buildInputs(createFromSender(senderAddress, changeAddress))
                .andThen(balanceTx(changeAddress));


        UtxoSupplier utxoSupplier = new MyUtxoSupplier(backendService.getUtxoService(), utxoService);
        ProtocolParamsSupplier protocolParamsSupplier = new DefaultProtocolParamsSupplier(backendService.getEpochService());

        UtxoSelectionStrategy utxoSelectionStrategy = new RandomImproveUtxoSelectionStrategy(utxoSupplier);


        //Build and unsigned the transaction
        Transaction unsignedTransaction = TxBuilderContext.init(utxoSupplier, protocolParamsSupplier)
                .setUtxoSelectionStrategy(utxoSelectionStrategy)
                .build(txBuilder);

        for (TxSigner signer : signerList) {
            signer.sign(unsignedTransaction);
        }

@satran004
Copy link
Member

Thanks! You are using the Composable Functions api. This should work.

The balanceTx() method should automatically determine the number of signers from the utxo set and calculate the fee based on the correct tx size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants