-
Notifications
You must be signed in to change notification settings - Fork 159
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
Comments
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. |
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 , 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: |
This epic will be used to track the necessary changes to the SDK / Horizon API pursuant to Soroban / Protocol 20.
The text was updated successfully, but these errors were encountered: