/
HydraTransactionGenerator.java
47 lines (36 loc) · 2 KB
/
HydraTransactionGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package org.cardanofoundation.hydra.client.helpers;
import com.bloxbean.cardano.client.api.ProtocolParamsSupplier;
import com.bloxbean.cardano.client.api.UtxoSupplier;
import com.bloxbean.cardano.client.exception.CborSerializationException;
import com.bloxbean.cardano.client.function.Output;
import com.bloxbean.cardano.client.function.TxBuilderContext;
import lombok.val;
import org.cardanofoundation.hydra.cardano.client.lib.HydraOperator;
import static com.bloxbean.cardano.client.common.ADAConversionUtil.adaToLovelace;
import static com.bloxbean.cardano.client.common.CardanoConstants.LOVELACE;
import static com.bloxbean.cardano.client.function.helper.BalanceTxBuilders.balanceTx;
import static com.bloxbean.cardano.client.function.helper.InputBuilders.createFromSender;
public class HydraTransactionGenerator {
private final UtxoSupplier utxoSupplier;
private final ProtocolParamsSupplier protocolParamsSupplier;
public HydraTransactionGenerator(UtxoSupplier utxoSupplier,
ProtocolParamsSupplier protocolParamsSupplier) {
this.utxoSupplier = utxoSupplier;
this.protocolParamsSupplier = protocolParamsSupplier;
}
public byte[] simpleTransfer(HydraOperator sender, HydraOperator receiver, int adaAmount) throws CborSerializationException {
var senderAddress = sender.getAddress();
var receiverAddress = receiver.getAddress();
var output = Output.builder()
.address(receiverAddress)
.assetName(LOVELACE)
.qty(adaToLovelace(adaAmount))
.build();
var txBuilder = output.outputBuilder()
.buildInputs(createFromSender(senderAddress, senderAddress))
.andThen(balanceTx(senderAddress, 1));
val txBuilderContext = TxBuilderContext.init(utxoSupplier, protocolParamsSupplier);
val signedTransaction = txBuilderContext.buildAndSign(txBuilder, sender.getTxSigner());
return signedTransaction.serialize();
}
}