2

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;
    }

1 Answer 1

1

There are a couple of potential issues.

  • First, Path Payment is an operation that requires 2 steps

    1. Finding the Path
    2. Building the path payment operation
  • The path found in step 1 is used in building the operations

The horizon server has a paths endpoint that you can use to query available paths; see the docs on Finding Path Payments and Payment Path resource for more information.

The Java SDK has the PathsRequestBuilder and the setPath() method for finding and setting a path on an operation. You should check these out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.