Created
July 8, 2021 15:42
-
-
Save satran004/f22c48bf3439392fbee0c5415c0c7af9 to your computer and use it in GitHub Desktop.
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
import com.bloxbean.cardano.client.account.Account; | |
import com.bloxbean.cardano.client.backend.api.BackendService; | |
import com.bloxbean.cardano.client.backend.api.BlockService; | |
import com.bloxbean.cardano.client.backend.api.helper.FeeCalculationService; | |
import com.bloxbean.cardano.client.backend.api.helper.TransactionHelperService; | |
import com.bloxbean.cardano.client.backend.api.helper.model.TransactionResult; | |
import com.bloxbean.cardano.client.backend.exception.ApiException; | |
import com.bloxbean.cardano.client.backend.gql.GqlBackendService; | |
import com.bloxbean.cardano.client.backend.model.Result; | |
import com.bloxbean.cardano.client.backend.model.TransactionContent; | |
import com.bloxbean.cardano.client.common.model.Networks; | |
import com.bloxbean.cardano.client.exception.AddressExcepion; | |
import com.bloxbean.cardano.client.exception.CborSerializationException; | |
import com.bloxbean.cardano.client.transaction.model.PaymentTransaction; | |
import com.bloxbean.cardano.client.transaction.model.TransactionDetailsParams; | |
import com.bloxbean.cardano.client.util.JsonUtil; | |
import java.math.BigInteger; | |
public class TransferToken { | |
private BackendService backendService; | |
private FeeCalculationService feeCalculationService; | |
private BlockService blockService; | |
private TransactionHelperService transactionHelperService; | |
TransferToken() { | |
//Create GraphQL backend service | |
backendService = | |
new GqlBackendService("https://graphql-api.testnet.dandelion.link/"); | |
feeCalculationService = backendService.getFeeCalculationService(); | |
blockService = backendService.getBlockService(); | |
transactionHelperService = backendService.getTransactionHelperService(); | |
} | |
public void transfer() throws ApiException, AddressExcepion, CborSerializationException { | |
//sender | |
String senderMnemonic = "fashion stool spoil section fault spot basic animal minimum cattle flat off busy dice spell clay gap urge bamboo gap flash blast hybrid youth"; | |
Account sender = new Account(Networks.testnet(), senderMnemonic); | |
String receiver = "addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82"; | |
//Create payment request | |
PaymentTransaction paymentTransaction = | |
PaymentTransaction.builder() | |
.sender(sender) | |
.receiver(receiver) | |
.amount(BigInteger.valueOf(1500000)) | |
.unit("lovelace") | |
.build(); | |
//Calculate TTl | |
long ttl = blockService.getLastestBlock().getValue().getSlot() + 1000; | |
TransactionDetailsParams detailsParams = | |
TransactionDetailsParams.builder() | |
.ttl(ttl) | |
.build(); | |
//Calculate Fee | |
BigInteger fee = feeCalculationService.calculateFee(paymentTransaction, detailsParams | |
, null); | |
paymentTransaction.setFee(fee); | |
//Post Transaction | |
Result<TransactionResult> result = transactionHelperService.transfer(paymentTransaction, detailsParams); | |
if(result.isSuccessful()) | |
System.out.println("Transaction Id: " + result.getValue()); | |
else | |
System.out.println("Transaction failed: " + result); | |
waitForTransaction(result); | |
} | |
protected void waitForTransaction(Result<TransactionResult> result) { | |
try { | |
if (result.isSuccessful()) { //Wait for transaction to be mined | |
int count = 0; | |
while (count < 60) { | |
Result<TransactionContent> txnResult = backendService.getTransactionService() | |
.getTransaction(result.getValue().getTransactionId()); | |
if (txnResult.isSuccessful()) { | |
System.out.println(JsonUtil.getPrettyJson(txnResult.getValue())); | |
break; | |
} else { | |
System.out.println("Waiting for transaction to be mined ...."); | |
} | |
count++; | |
Thread.sleep(2000); | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) throws AddressExcepion, ApiException, CborSerializationException { | |
new TransferToken().transfer(); | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add following dependencies in pom.xml