Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[epic] Protocol 20 SDK Support #490

Closed
overcat opened this issue Jul 20, 2023 · 3 comments
Closed

[epic] Protocol 20 SDK Support #490

overcat opened this issue Jul 20, 2023 · 3 comments

Comments

@overcat
Copy link
Member

overcat commented Jul 20, 2023

This epic will be used to track the necessary changes to the SDK / Horizon API pursuant to Soroban / Protocol 20.

### Tasks
- [ ] https://github.com/stellar/java-stellar-sdk/issues/472
- [ ] https://github.com/stellar/java-stellar-sdk/issues/486
- [ ] https://github.com/stellar/java-stellar-sdk/issues/485
- [ ] https://github.com/stellar/java-stellar-sdk/issues/479
- [ ] https://github.com/stellar/java-stellar-sdk/issues/473
- [ ] https://github.com/stellar/java-stellar-sdk/issues/488
- [ ] https://github.com/stellar/java-stellar-sdk/issues/487
- [ ] https://github.com/stellar/java-stellar-sdk/issues/495
- [ ] https://github.com/stellar/java-stellar-sdk/issues/499
- [ ] https://github.com/stellar/java-stellar-sdk/issues/504
- [ ] https://github.com/stellar/java-stellar-sdk/issues/508
@sreuland
Copy link
Contributor

just to confirm, for consistency, development related to these epic tasks should should use the feature branch soroban, at epic completion can merge to mainline.

@overcat
Copy link
Member Author

overcat commented Aug 14, 2023

Example of calling the Soroban contract through Java SDK.

import org.stellar.sdk.AccountNotFoundException;
import org.stellar.sdk.InvokeHostFunctionOperation;
import org.stellar.sdk.KeyPair;
import org.stellar.sdk.MemoText;
import org.stellar.sdk.Network;
import org.stellar.sdk.PrepareTransactionException;
import org.stellar.sdk.SorobanServer;
import org.stellar.sdk.TimeBounds;
import org.stellar.sdk.Transaction;
import org.stellar.sdk.TransactionBuilder;
import org.stellar.sdk.TransactionBuilderAccount;
import org.stellar.sdk.TransactionPreconditions;
import org.stellar.sdk.requests.sorobanrpc.SorobanRpcErrorResponse;
import org.stellar.sdk.responses.sorobanrpc.GetTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.SendTransactionResponse;
import org.stellar.sdk.scval.Scv;
import org.stellar.sdk.xdr.SCVal;
import org.stellar.sdk.xdr.TransactionMeta;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class SorobanJavaExample {
  public static void main(String[] args) throws SorobanRpcErrorResponse, IOException, AccountNotFoundException, PrepareTransactionException {
    SorobanServer server = new SorobanServer("http://127.0.0.1:8000/soroban/rpc");
    Network network = Network.STANDALONE;

    KeyPair sourceKp = KeyPair.fromSecretSeed("SA6Q3PPKGYM4F3NIAMATPMBCV77ULNRZC6QVWZGOMYIT67STOQAUMDK7"); // GAXMREIIQGY4IKQTHH3ANMX2J2TAFTK5Q6IG46QFWPVWI7KEFMGKXB2W
    TransactionBuilderAccount source = server.getAccount(sourceKp.getAccountId());

    String contractId = "CBQHNAXSI55GX2GN6D67GK7BHVPSLJUGZQEU7WJ5LKR5PNUCGLIMAO4K";
    String functionName = "hello";
    List<SCVal> params = Collections.singletonList(Scv.toSymbol("Soroban"));
    InvokeHostFunctionOperation operation = InvokeHostFunctionOperation.invokeContractFunctionOperationBuilder(contractId, functionName, params).build();

    Transaction tx = new TransactionBuilder(source, network)
            .setBaseFee(500)
            .addMemo(new MemoText("Hello Soroban!"))
            .addPreconditions(TransactionPreconditions.builder().timeBounds(TimeBounds.expiresAfter(300)).build())
            .addOperation(operation)
            .build();

    Transaction preparedTx = server.prepareTransaction(tx);
    preparedTx.sign(sourceKp);

    SendTransactionResponse resp = server.sendTransaction(preparedTx);
    System.out.println(resp);

    String txHash = resp.getHash();

    GetTransactionResponse getTransactionResponse;
    while (true) {
      getTransactionResponse = server.getTransaction(txHash);
      if (!getTransactionResponse.getStatus().equals(GetTransactionResponse.GetTransactionStatus.NOT_FOUND)) {
        System.out.println(getTransactionResponse);
        break;
      }
    }

    if (getTransactionResponse.getStatus().equals(GetTransactionResponse.GetTransactionStatus.SUCCESS)) {
      System.out.println("Transaction was successful!");
      TransactionMeta transactionMeta = TransactionMeta.fromXdrBase64(getTransactionResponse.getResultMetaXdr());
      SCVal returnValue = transactionMeta.getV3().getSorobanMeta().getReturnValue();
      Collection<SCVal> parsedReturnValue = Scv.fromVec(returnValue);
      System.out.println("Return value size: " + parsedReturnValue.size());
      SCVal[] parsedReturnValueArray = parsedReturnValue.toArray(new SCVal[0]);
      System.out.println("Return value 0: " + Scv.fromSymbol(parsedReturnValueArray[0]));
      System.out.println("Return value 1: " + Scv.fromSymbol(parsedReturnValueArray[1]));
    } else {
      System.out.println("Transaction failed!");
    }
  }
}

@overcat overcat closed this as completed Aug 19, 2023
@sreuland
Copy link
Contributor

sreuland commented Aug 23, 2023

Example of calling the Soroban contract through Java SDK.

@overcat , This is great, it will save devs a lot of time, I created a placeholder issue to track creating some new docs for devs to understand how to get up and running with soroban contracts in java sdk, which could use this code example for a 'in a nutshell' section:

#516

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants