I made a Java implementation of a Path Payment.
I created 3 accounts. For example:
- the first plays the role of issuer. He issued 2 assets (USD, EUR)
- The second has USD
- The third, EUR.
I created an offer with the EUR account to have the USD.
Finally, I make a Path Payment from the USD account to EUR, but the response.isSuccess()
method always returns false.
SubmitTransactionResponse response = server.submitTransaction(transaction);
What to do in this case?
public void pathPayment(String from, String to, String assetSource, String assetDest, String maxAmount,
String amountDest, String memo) {
System.out.println(" Transfering from : " + from);
System.out.println(" Transfering to : " + to);
System.out.println(" Transfering amount Source : " + maxAmount);
System.out.println(" Transfering amountDest : " + amountDest);
System.out.println(" Transfering memo : " + memo);
Network.useTestNetwork();
Server server = new Server(network);
KeyPair source = KeyPair.fromSecretSeed(accountRepository.findByAccountName(from).get(0).getPrivateKey());
KeyPair destination = KeyPair.fromAccountId(accountRepository.findByAccountName(to).get(0).getPublicKey());
// First, check to make sure that the destination account exists.
// You could skip this, but if the account does not exist, you will be charged
// the transaction fee when the transaction fails.
// It will throw HttpResponseException if account does not exist or there was another error.
AccountResponse destAccount = null;
try {
destAccount = server.accounts().account(destination);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
AccountResponse sourceAccount = null;
try {
sourceAccount = server.accounts().account(source);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Asset sendAsset = getAsset(assetSource, source.getAccountId());
Asset destAsset = getAsset(assetDest, destination.getAccountId());
//if(sendAsset==null || destAsset==null) {
// return false;
//}
// Start building the transaction.
Transaction transaction = new Transaction.Builder(sourceAccount)
.addOperation(new PathPaymentOperation.Builder(sendAsset, maxAmount, destination, destAsset,
amountDest).build())
.addMemo(Memo.text(memo))
.setTimeout(TIMEOUT_INFINITE)
.build();
// Sign the transaction to prove you are actually the person sending it.
transaction.sign(source);
// And finally, send it off to Stellar!
boolean bool=true;
try {
SubmitTransactionResponse response = server.submitTransaction(transaction);
System.out.print("Was submitTransaction successful : ");
bool = response.isSuccess();
System.out.println(bool + " " + response.getResultXdr()+ " "+response.toString());
} catch (Exception e) {
System.out.println("Something went wrong!");
System.out.println(e.getMessage());
}
}
private Asset getAsset(String assetCode, String accountKey){
if(StringUtils.isEmpty(assetCode) || (!StringUtils.isEmpty(assetCode) && assetCode.equalsIgnoreCase("XLM"))){
return new AssetTypeNative();
}
Network.useTestNetwork();
Server server = new Server(network);
AccountResponse account = null;
KeyPair accountK = KeyPair.fromAccountId(accountKey);
String issuer="";
System.out.println("Using network : " + network);
try {
account = server.accounts().account(accountK);
System.out.println("Balances for account " + accountKey);
for (AccountResponse.Balance balance : account.getBalances()) {
if(balance.getAssetCode().equalsIgnoreCase(assetCode)){
issuer = balance.getAssetIssuer().getAccountId();
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Asset asset=null;
if(!StringUtils.isEmpty(issuer)){
asset = Asset.createNonNativeAsset(assetCode, KeyPair.fromAccountId(issuer));
}
return asset;
}