Skip to content

Commit

Permalink
feat: Take protocol param as parameter to Tx api
Browse files Browse the repository at this point in the history
  • Loading branch information
satran004 committed Jun 26, 2024
1 parent 001e443 commit 250cdbb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.bloxbean.cardano.client.address.Address;
import com.bloxbean.cardano.client.address.Credential;
import com.bloxbean.cardano.client.api.model.Amount;
import com.bloxbean.cardano.client.api.model.ProtocolParams;
import com.bloxbean.cardano.client.function.TxBuilder;
import com.bloxbean.cardano.client.function.exception.TxBuildException;
import com.bloxbean.cardano.client.transaction.spec.TransactionOutput;
Expand All @@ -26,9 +27,13 @@

@Slf4j
public class GovTx {
//TODO -- Read from protocol
public static final BigInteger DREP_REG_DEPOSIT = adaToLovelace(500.0);
public static final Amount DUMMY_MIN_OUTPUT_VAL = Amount.ada(1.0);
private static final BigInteger DEFAULT_DREP_REG_DEPOSIT = adaToLovelace(500.0);
private static final BigInteger DEFAULT_GOV_ACTION_DEPOSIT = adaToLovelace(50000);

private static final Amount DUMMY_MIN_OUTPUT_VAL = Amount.ada(1.0);

protected BigInteger drepRegDeposit;
protected BigInteger govActionDeposit;

protected List<RegDRepCert> dRepRegistrations;
protected List<DRepDeregestrationContext> dRepDeregestrationContexts;
Expand All @@ -37,6 +42,21 @@ public class GovTx {
protected List<VotingProcedureContext> votingProcedureContexts;
protected List<VoteDelegCert> voteDelegCerts;

public GovTx() {
drepRegDeposit = DEFAULT_DREP_REG_DEPOSIT;
govActionDeposit = DEFAULT_GOV_ACTION_DEPOSIT;
}

public GovTx(ProtocolParams protocolParams) {
if (protocolParams != null) {
drepRegDeposit = protocolParams.getDrepDeposit();
govActionDeposit = protocolParams.getGovActionDeposit();
} else {
drepRegDeposit = DEFAULT_DREP_REG_DEPOSIT;
govActionDeposit = DEFAULT_GOV_ACTION_DEPOSIT;
}
}

/**
* Register DRep
* @param drepCredential DRep credential
Expand All @@ -47,7 +67,7 @@ public GovTx registerDRep(@NonNull Credential drepCredential, Anchor anchor) {
var regDRepCert = RegDRepCert.builder()
.drepCredential(drepCredential)
.anchor(anchor)
.coin(DREP_REG_DEPOSIT)
.coin(drepRegDeposit)
.build();

if (dRepRegistrations == null)
Expand Down Expand Up @@ -87,7 +107,7 @@ public GovTx registerDRep(@NonNull Credential drepCredential, @NonNull BigIntege
*/
public GovTx unregisterDRep(@NonNull Credential drepCredential, String refundAddress, BigInteger refundAmount) {
if (refundAmount == null)
refundAmount = DREP_REG_DEPOSIT;
refundAmount = drepRegDeposit;

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

if (dRepRegistrations != null && dRepRegistrations.size() > 0) {
//Dummy pay to fromAddress to add deposit
Amount totalDRepRegistrationDepositAmount = Amount.lovelace(DREP_REG_DEPOSIT.multiply(BigInteger.valueOf(dRepRegistrations.size())));
Amount totalDRepRegistrationDepositAmount = Amount.lovelace(drepRegDeposit.multiply(BigInteger.valueOf(dRepRegistrations.size())));
paymentContexts.add(new GovTx.PaymentContext(fromAddress, totalDRepRegistrationDepositAmount));
}

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

certificates.addAll(dRepRegistrations);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.bloxbean.cardano.client.address.Address;
import com.bloxbean.cardano.client.address.AddressType;
import com.bloxbean.cardano.client.api.model.Amount;
import com.bloxbean.cardano.client.api.model.ProtocolParams;
import com.bloxbean.cardano.client.function.TxBuilder;
import com.bloxbean.cardano.client.function.exception.TxBuildException;
import com.bloxbean.cardano.client.plutus.spec.ExUnits;
Expand Down Expand Up @@ -32,10 +33,12 @@
*/
@Slf4j
class StakeTx {
//TODO -- Read from protocol params
public static final BigInteger STAKE_KEY_REG_DEPOSIT = adaToLovelace(2.0);
private static final BigInteger POOL_REG_DEPOSIT = adaToLovelace(500);
public static final Amount DUMMY_MIN_OUTPUT_VAL = Amount.ada(1.0);
private static final BigInteger DEFAULT_STAKE_KEY_REG_DEPOSIT = adaToLovelace(2.0);
private static final BigInteger DEFAUlT_POOL_REG_DEPOSIT = adaToLovelace(500);
private static final Amount DUMMY_MIN_OUTPUT_VAL = Amount.ada(1.0);

private BigInteger stakeKeyRegDeposit;
private BigInteger poolRegDeposit;

protected List<StakeRegistration> stakeRegistrations;
protected List<StakeKeyDeregestrationContext> stakeDeRegistrationContexts;
Expand All @@ -44,6 +47,21 @@ class StakeTx {
protected List<PoolRegistrationContext> poolRegistrationContexts;
protected List<PoolRetirement> poolRetirements;

public StakeTx() {
stakeKeyRegDeposit = DEFAULT_STAKE_KEY_REG_DEPOSIT;
poolRegDeposit = DEFAUlT_POOL_REG_DEPOSIT;
}

public StakeTx(ProtocolParams protocolParams) {
if (protocolParams != null) {
stakeKeyRegDeposit = new BigInteger(protocolParams.getKeyDeposit());
poolRegDeposit = new BigInteger(protocolParams.getPoolDeposit());
} else {
stakeKeyRegDeposit = DEFAULT_STAKE_KEY_REG_DEPOSIT;
poolRegDeposit = DEFAUlT_POOL_REG_DEPOSIT;
}
}

/**
* Register stake address
*
Expand Down Expand Up @@ -280,7 +298,7 @@ private List<PaymentContext> buildStakePayments(String fromAddress, String chang

if (stakeRegistrations != null && stakeRegistrations.size() > 0) {
//Dummy pay to fromAddress to add deposit
Amount totalStakeDepositAmount = Amount.lovelace(STAKE_KEY_REG_DEPOSIT.multiply(BigInteger.valueOf(stakeRegistrations.size())));
Amount totalStakeDepositAmount = Amount.lovelace(stakeKeyRegDeposit.multiply(BigInteger.valueOf(stakeRegistrations.size())));
paymentContexts.add(new PaymentContext(fromAddress, totalStakeDepositAmount));
}

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

if (poolRegistrations.size() > 0) {
//Dummy pay to fromAddress to add deposit
Amount totalPoolDepositAmount = Amount.lovelace(POOL_REG_DEPOSIT.multiply(BigInteger.valueOf(poolRegistrations.size())));
Amount totalPoolDepositAmount = Amount.lovelace(poolRegDeposit.multiply(BigInteger.valueOf(poolRegistrations.size())));
paymentContexts.add(new PaymentContext(fromAddress, totalPoolDepositAmount));
}
}
Expand Down Expand Up @@ -396,10 +414,10 @@ private TxBuilder buildStakeAddressDeRegistration(TxBuilder txBuilder, String fr
.findFirst()
.ifPresentOrElse(to -> {
//Add deposit amount to the change address
to.getValue().setCoin(to.getValue().getCoin().add(STAKE_KEY_REG_DEPOSIT));
to.getValue().setCoin(to.getValue().getCoin().add(stakeKeyRegDeposit));
}, () -> {
TransactionOutput transactionOutput = new TransactionOutput(stakeKeyDeregestrationContext.refundAddress,
Value.builder().coin(STAKE_KEY_REG_DEPOSIT).build());
Value.builder().coin(stakeKeyRegDeposit).build());
txn.getBody().getOutputs().add(transactionOutput);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.bloxbean.cardano.client.address.Address;
import com.bloxbean.cardano.client.address.Credential;
import com.bloxbean.cardano.client.api.model.Amount;
import com.bloxbean.cardano.client.api.model.ProtocolParams;
import com.bloxbean.cardano.client.api.model.Utxo;
import com.bloxbean.cardano.client.api.util.AssetUtil;
import com.bloxbean.cardano.client.function.TxBuilder;
Expand Down Expand Up @@ -41,6 +42,11 @@ public Tx() {
govTx = new GovTx();
}

public Tx(ProtocolParams protocolParams) {
stakeTx = new StakeTx(protocolParams);
govTx = new GovTx(protocolParams);
}

/**
* Add a mint asset to the transaction. The newly minted asset will be transferred to the defined receivers in payToAddress methods.
* <p>
Expand Down

0 comments on commit 250cdbb

Please sign in to comment.