Skip to content

Commit 250cdbb

Browse files
committed
feat: Take protocol param as parameter to Tx api
1 parent 001e443 commit 250cdbb

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

quicktx/src/main/java/com/bloxbean/cardano/client/quicktx/GovTx.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.bloxbean.cardano.client.address.Address;
44
import com.bloxbean.cardano.client.address.Credential;
55
import com.bloxbean.cardano.client.api.model.Amount;
6+
import com.bloxbean.cardano.client.api.model.ProtocolParams;
67
import com.bloxbean.cardano.client.function.TxBuilder;
78
import com.bloxbean.cardano.client.function.exception.TxBuildException;
89
import com.bloxbean.cardano.client.transaction.spec.TransactionOutput;
@@ -26,9 +27,13 @@
2627

2728
@Slf4j
2829
public class GovTx {
29-
//TODO -- Read from protocol
30-
public static final BigInteger DREP_REG_DEPOSIT = adaToLovelace(500.0);
31-
public static final Amount DUMMY_MIN_OUTPUT_VAL = Amount.ada(1.0);
30+
private static final BigInteger DEFAULT_DREP_REG_DEPOSIT = adaToLovelace(500.0);
31+
private static final BigInteger DEFAULT_GOV_ACTION_DEPOSIT = adaToLovelace(50000);
32+
33+
private static final Amount DUMMY_MIN_OUTPUT_VAL = Amount.ada(1.0);
34+
35+
protected BigInteger drepRegDeposit;
36+
protected BigInteger govActionDeposit;
3237

3338
protected List<RegDRepCert> dRepRegistrations;
3439
protected List<DRepDeregestrationContext> dRepDeregestrationContexts;
@@ -37,6 +42,21 @@ public class GovTx {
3742
protected List<VotingProcedureContext> votingProcedureContexts;
3843
protected List<VoteDelegCert> voteDelegCerts;
3944

45+
public GovTx() {
46+
drepRegDeposit = DEFAULT_DREP_REG_DEPOSIT;
47+
govActionDeposit = DEFAULT_GOV_ACTION_DEPOSIT;
48+
}
49+
50+
public GovTx(ProtocolParams protocolParams) {
51+
if (protocolParams != null) {
52+
drepRegDeposit = protocolParams.getDrepDeposit();
53+
govActionDeposit = protocolParams.getGovActionDeposit();
54+
} else {
55+
drepRegDeposit = DEFAULT_DREP_REG_DEPOSIT;
56+
govActionDeposit = DEFAULT_GOV_ACTION_DEPOSIT;
57+
}
58+
}
59+
4060
/**
4161
* Register DRep
4262
* @param drepCredential DRep credential
@@ -47,7 +67,7 @@ public GovTx registerDRep(@NonNull Credential drepCredential, Anchor anchor) {
4767
var regDRepCert = RegDRepCert.builder()
4868
.drepCredential(drepCredential)
4969
.anchor(anchor)
50-
.coin(DREP_REG_DEPOSIT)
70+
.coin(drepRegDeposit)
5171
.build();
5272

5373
if (dRepRegistrations == null)
@@ -87,7 +107,7 @@ public GovTx registerDRep(@NonNull Credential drepCredential, @NonNull BigIntege
87107
*/
88108
public GovTx unregisterDRep(@NonNull Credential drepCredential, String refundAddress, BigInteger refundAmount) {
89109
if (refundAmount == null)
90-
refundAmount = DREP_REG_DEPOSIT;
110+
refundAmount = drepRegDeposit;
91111

92112
var unregDRepCert = UnregDRepCert.builder()
93113
.drepCredential(drepCredential)
@@ -213,7 +233,7 @@ private List<GovTx.PaymentContext> buildGovernancePayments(String fromAddress, S
213233

214234
if (dRepRegistrations != null && dRepRegistrations.size() > 0) {
215235
//Dummy pay to fromAddress to add deposit
216-
Amount totalDRepRegistrationDepositAmount = Amount.lovelace(DREP_REG_DEPOSIT.multiply(BigInteger.valueOf(dRepRegistrations.size())));
236+
Amount totalDRepRegistrationDepositAmount = Amount.lovelace(drepRegDeposit.multiply(BigInteger.valueOf(dRepRegistrations.size())));
217237
paymentContexts.add(new GovTx.PaymentContext(fromAddress, totalDRepRegistrationDepositAmount));
218238
}
219239

@@ -252,8 +272,6 @@ private TxBuilder buildDRepRegistration(TxBuilder txBuilder, String fromAddress)
252272

253273
certificates.addAll(dRepRegistrations);
254274

255-
String drepRegDepositParam = DREP_REG_DEPOSIT.toString();//context.getProtocolParams().getKeyDeposit(); //TODO -- Get protocol param
256-
BigInteger drepRegDeposit = new BigInteger(drepRegDepositParam);
257275
BigInteger totalDRepRegDeposit = drepRegDeposit.multiply(BigInteger.valueOf(dRepRegistrations.size()));
258276
log.debug("Total stakekey registration deposit: " + totalDRepRegDeposit);
259277

quicktx/src/main/java/com/bloxbean/cardano/client/quicktx/StakeTx.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.bloxbean.cardano.client.address.Address;
44
import com.bloxbean.cardano.client.address.AddressType;
55
import com.bloxbean.cardano.client.api.model.Amount;
6+
import com.bloxbean.cardano.client.api.model.ProtocolParams;
67
import com.bloxbean.cardano.client.function.TxBuilder;
78
import com.bloxbean.cardano.client.function.exception.TxBuildException;
89
import com.bloxbean.cardano.client.plutus.spec.ExUnits;
@@ -32,10 +33,12 @@
3233
*/
3334
@Slf4j
3435
class StakeTx {
35-
//TODO -- Read from protocol params
36-
public static final BigInteger STAKE_KEY_REG_DEPOSIT = adaToLovelace(2.0);
37-
private static final BigInteger POOL_REG_DEPOSIT = adaToLovelace(500);
38-
public static final Amount DUMMY_MIN_OUTPUT_VAL = Amount.ada(1.0);
36+
private static final BigInteger DEFAULT_STAKE_KEY_REG_DEPOSIT = adaToLovelace(2.0);
37+
private static final BigInteger DEFAUlT_POOL_REG_DEPOSIT = adaToLovelace(500);
38+
private static final Amount DUMMY_MIN_OUTPUT_VAL = Amount.ada(1.0);
39+
40+
private BigInteger stakeKeyRegDeposit;
41+
private BigInteger poolRegDeposit;
3942

4043
protected List<StakeRegistration> stakeRegistrations;
4144
protected List<StakeKeyDeregestrationContext> stakeDeRegistrationContexts;
@@ -44,6 +47,21 @@ class StakeTx {
4447
protected List<PoolRegistrationContext> poolRegistrationContexts;
4548
protected List<PoolRetirement> poolRetirements;
4649

50+
public StakeTx() {
51+
stakeKeyRegDeposit = DEFAULT_STAKE_KEY_REG_DEPOSIT;
52+
poolRegDeposit = DEFAUlT_POOL_REG_DEPOSIT;
53+
}
54+
55+
public StakeTx(ProtocolParams protocolParams) {
56+
if (protocolParams != null) {
57+
stakeKeyRegDeposit = new BigInteger(protocolParams.getKeyDeposit());
58+
poolRegDeposit = new BigInteger(protocolParams.getPoolDeposit());
59+
} else {
60+
stakeKeyRegDeposit = DEFAULT_STAKE_KEY_REG_DEPOSIT;
61+
poolRegDeposit = DEFAUlT_POOL_REG_DEPOSIT;
62+
}
63+
}
64+
4765
/**
4866
* Register stake address
4967
*
@@ -280,7 +298,7 @@ private List<PaymentContext> buildStakePayments(String fromAddress, String chang
280298

281299
if (stakeRegistrations != null && stakeRegistrations.size() > 0) {
282300
//Dummy pay to fromAddress to add deposit
283-
Amount totalStakeDepositAmount = Amount.lovelace(STAKE_KEY_REG_DEPOSIT.multiply(BigInteger.valueOf(stakeRegistrations.size())));
301+
Amount totalStakeDepositAmount = Amount.lovelace(stakeKeyRegDeposit.multiply(BigInteger.valueOf(stakeRegistrations.size())));
284302
paymentContexts.add(new PaymentContext(fromAddress, totalStakeDepositAmount));
285303
}
286304

@@ -304,7 +322,7 @@ private List<PaymentContext> buildStakePayments(String fromAddress, String chang
304322

305323
if (poolRegistrations.size() > 0) {
306324
//Dummy pay to fromAddress to add deposit
307-
Amount totalPoolDepositAmount = Amount.lovelace(POOL_REG_DEPOSIT.multiply(BigInteger.valueOf(poolRegistrations.size())));
325+
Amount totalPoolDepositAmount = Amount.lovelace(poolRegDeposit.multiply(BigInteger.valueOf(poolRegistrations.size())));
308326
paymentContexts.add(new PaymentContext(fromAddress, totalPoolDepositAmount));
309327
}
310328
}
@@ -396,10 +414,10 @@ private TxBuilder buildStakeAddressDeRegistration(TxBuilder txBuilder, String fr
396414
.findFirst()
397415
.ifPresentOrElse(to -> {
398416
//Add deposit amount to the change address
399-
to.getValue().setCoin(to.getValue().getCoin().add(STAKE_KEY_REG_DEPOSIT));
417+
to.getValue().setCoin(to.getValue().getCoin().add(stakeKeyRegDeposit));
400418
}, () -> {
401419
TransactionOutput transactionOutput = new TransactionOutput(stakeKeyDeregestrationContext.refundAddress,
402-
Value.builder().coin(STAKE_KEY_REG_DEPOSIT).build());
420+
Value.builder().coin(stakeKeyRegDeposit).build());
403421
txn.getBody().getOutputs().add(transactionOutput);
404422
});
405423
}

quicktx/src/main/java/com/bloxbean/cardano/client/quicktx/Tx.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.bloxbean.cardano.client.address.Address;
55
import com.bloxbean.cardano.client.address.Credential;
66
import com.bloxbean.cardano.client.api.model.Amount;
7+
import com.bloxbean.cardano.client.api.model.ProtocolParams;
78
import com.bloxbean.cardano.client.api.model.Utxo;
89
import com.bloxbean.cardano.client.api.util.AssetUtil;
910
import com.bloxbean.cardano.client.function.TxBuilder;
@@ -41,6 +42,11 @@ public Tx() {
4142
govTx = new GovTx();
4243
}
4344

45+
public Tx(ProtocolParams protocolParams) {
46+
stakeTx = new StakeTx(protocolParams);
47+
govTx = new GovTx(protocolParams);
48+
}
49+
4450
/**
4551
* Add a mint asset to the transaction. The newly minted asset will be transferred to the defined receivers in payToAddress methods.
4652
* <p>

0 commit comments

Comments
 (0)