Skip to content

Instantly share code, notes, and snippets.

@satran004
Last active May 28, 2021 14:29
Show Gist options
  • Save satran004/6e05d51537af7a311eedf1d313a86a28 to your computer and use it in GitHub Desktop.
Save satran004/6e05d51537af7a311eedf1d313a86a28 to your computer and use it in GitHub Desktop.
public void transfer() throws ApiException, AddressExcepion, CborSerializationException {
BackendService backendService =
BackendFactory.getBlockfrostBackendService(Constants.BLOCKFROST_TESTNET_URL, Constant.BF_PROJECT_KEY);
FeeCalculationService feeCalculationService = backendService.getFeeCalculationService();
TransactionHelperService transactionHelperService = backendService.getTransactionHelperService();
BlockService blockService = backendService.getBlockService();
String senderMnemonic = "kit color frog trick speak employ suit sort bomb goddess jewel primary spoil fade person useless measure manage warfare reduce few scrub beyond era";
Account sender = new Account(Networks.testnet(), senderMnemonic);
String receiver = "addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82";
PaymentTransaction paymentTransaction =
PaymentTransaction.builder()
.sender(sender) //Sender Account object
.receiver(receiver) // Receiver baseAddress as String
.amount(BigInteger.valueOf(1500000))
.unit(CardanoConstants.LOVELACE)
.build();
long ttl = blockService.getLastestBlock().getValue().getSlot() + 1000;
TransactionDetailsParams detailsParams =
TransactionDetailsParams.builder()
.ttl(ttl)
.build();
BigInteger fee
= feeCalculationService.calculateFee(paymentTransaction, detailsParams, null);
paymentTransaction.setFee(fee);
Result<String> result
= transactionHelperService.transfer(paymentTransaction, detailsParams);
if(result.isSuccessful())
System.out.println("Transaction Id: " + result.getValue());
else
System.out.println("Transaction failed: " + result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment