-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring Deploy helper and adding new helpers for:
- Validator (AuctionBid, AuctionBidWithdraw, ValidatorDelegation and ValidatorDelegationWithdraw) - Key (createRandom keys)
- Loading branch information
Showing
7 changed files
with
533 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.casper.sdk.helper; | ||
|
||
public enum CasperConstants { | ||
DEFAULT_DEPLOY_TTL((long) 30 * 60 * 1000), | ||
DEFAULT_GAS_PRICE(1), | ||
DEPLOY_TTL_MS_MAX((long) 1000 * 60 * 60 * 24), | ||
MAX_TRANSFER_ID(Long.MAX_VALUE), | ||
MIN_TRANSFER_AMOUNT_MOTES(2500000000L), | ||
STANDARD_PAYMENT_FOR_NATIVE_TRANSFERS((long) 1E8), | ||
STANDARD_PAYMENT_FOR_DELEGATION((long) 5e9), | ||
STANDARD_PAYMENT_FOR_DELEGATION_WITHDRAWAL((long) 5e9), | ||
STANDARD_PAYMENT_FOR_AUCTION_BID((long) 5e9), | ||
STANDARD_PAYMENT_FOR_AUCTION_BID_WITHDRAWAL((long) 5e9); | ||
|
||
public final long value; | ||
|
||
CasperConstants(long value) { | ||
this.value = value; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/com/casper/sdk/helper/CasperTransferHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.casper.sdk.helper; | ||
|
||
import com.casper.sdk.exception.NoSuchTypeException; | ||
import com.casper.sdk.model.clvalue.CLValueOption; | ||
import com.casper.sdk.model.clvalue.CLValuePublicKey; | ||
import com.casper.sdk.model.clvalue.CLValueU512; | ||
import com.casper.sdk.model.clvalue.CLValueU64; | ||
import com.casper.sdk.model.clvalue.cltype.CLTypeOption; | ||
import com.casper.sdk.model.clvalue.cltype.CLTypePublicKey; | ||
import com.casper.sdk.model.clvalue.cltype.CLTypeU512; | ||
import com.casper.sdk.model.common.Digest; | ||
import com.casper.sdk.model.common.Ttl; | ||
import com.casper.sdk.model.deploy.Deploy; | ||
import com.casper.sdk.model.deploy.NamedArg; | ||
import com.casper.sdk.model.deploy.executabledeploy.ModuleBytes; | ||
import com.casper.sdk.model.deploy.executabledeploy.Transfer; | ||
import com.casper.sdk.model.key.PublicKey; | ||
import com.syntifi.crypto.key.AbstractPrivateKey; | ||
import dev.oak3.sbs4j.exception.ValueSerializationException; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.math.BigInteger; | ||
import java.security.GeneralSecurityException; | ||
import java.util.*; | ||
|
||
/** | ||
* Transfer helper provides methods to easily transfer from/to purses | ||
* | ||
* @author Alexandre Carvalho | ||
* @author Andre Bertolace | ||
* @since 0.5.0 | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class CasperTransferHelper { | ||
/** | ||
* Helper method to create a Deploy for a Transfer | ||
* | ||
* @param from private key from sender | ||
* @param to public key from signer | ||
* @param amount amount to transfer | ||
* @param chainName chain name | ||
* @return a transfer deploy | ||
* @throws NoSuchTypeException | ||
* @throws GeneralSecurityException | ||
* @throws ValueSerializationException | ||
*/ | ||
public static Deploy buildTransferDeploy(AbstractPrivateKey from, PublicKey to, | ||
BigInteger amount, String chainName) | ||
throws NoSuchTypeException, GeneralSecurityException, ValueSerializationException { | ||
long id = Math.abs(new Random().nextInt()); | ||
Ttl ttl = Ttl | ||
.builder() | ||
.ttl(CasperConstants.DEFAULT_DEPLOY_TTL.value / 60 / 1000 + "m") | ||
.build(); | ||
BigInteger paymentAmount = BigInteger.valueOf(CasperConstants.STANDARD_PAYMENT_FOR_NATIVE_TRANSFERS.value); | ||
return buildTransferDeploy(from, to, amount, chainName, id, paymentAmount, | ||
CasperConstants.DEFAULT_GAS_PRICE.value, ttl, new Date(), new ArrayList<>()); | ||
} | ||
|
||
/** | ||
* Helper method to create a Deploy for a Transfer | ||
* | ||
* @param signer private key from sender | ||
* @param to public key from signer | ||
* @param amount amount to transfer | ||
* @param chainName chain name | ||
* @param id deploy id | ||
* @param paymentAmount payment amount for processing transfers | ||
* @param gasPrice gas price | ||
* @param ttl time to live | ||
* @param date execution date | ||
* @param dependencies List of digest dependencies | ||
* @return a transfer deploy | ||
* @throws NoSuchTypeException | ||
* @throws GeneralSecurityException | ||
* @throws ValueSerializationException | ||
*/ | ||
public static Deploy buildTransferDeploy(AbstractPrivateKey signer, PublicKey to, BigInteger amount, | ||
String chainName, Long id, BigInteger paymentAmount, | ||
Long gasPrice, Ttl ttl, Date date, List<Digest> dependencies) | ||
throws NoSuchTypeException, GeneralSecurityException, ValueSerializationException { | ||
List<NamedArg<?>> transferArgs = new LinkedList<>(); | ||
NamedArg<CLTypeU512> amountNamedArg = new NamedArg<>("amount", | ||
new CLValueU512(amount)); | ||
transferArgs.add(amountNamedArg); | ||
NamedArg<CLTypePublicKey> publicKeyNamedArg = new NamedArg<>("target", | ||
new CLValuePublicKey(to)); | ||
transferArgs.add(publicKeyNamedArg); | ||
CLValueOption idArg = new CLValueOption(Optional.of( | ||
new CLValueU64(BigInteger.valueOf(id)))); | ||
NamedArg<CLTypeOption> idNamedArg = new NamedArg<>("id", idArg); | ||
transferArgs.add(idNamedArg); | ||
|
||
Transfer session = Transfer | ||
.builder() | ||
.args(transferArgs) | ||
.build(); | ||
ModuleBytes payment = CasperDeployHelper.getPaymentModuleBytes(paymentAmount); | ||
|
||
return CasperDeployHelper.buildDeploy(signer, chainName, session, payment, gasPrice, ttl, | ||
date, dependencies); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.