Skip to content

Commit

Permalink
chore: add SorobanInvokeContractFunction example.
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Aug 8, 2024
1 parent a9de86a commit 5cb3fe0
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
10 changes: 6 additions & 4 deletions examples/src/main/java/network/lightsail/Payment.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package network.lightsail;

import static org.stellar.sdk.AbstractTransaction.MIN_BASE_FEE;
import static org.stellar.sdk.Transaction.MIN_BASE_FEE;

import org.stellar.sdk.AssetTypeNative;
import java.math.BigDecimal;
import org.stellar.sdk.Asset;
import org.stellar.sdk.KeyPair;
import org.stellar.sdk.Memo;
import org.stellar.sdk.Network;
import org.stellar.sdk.Server;
import org.stellar.sdk.Transaction;
import org.stellar.sdk.TransactionBuilder;
import org.stellar.sdk.TransactionBuilderAccount;
import org.stellar.sdk.exception.BadRequestException;
import org.stellar.sdk.exception.BadResponseException;
import org.stellar.sdk.exception.NetworkException;
Expand Down Expand Up @@ -38,8 +40,8 @@ public static void main(String[] args) {
PaymentOperation paymentOperation =
PaymentOperation.builder()
.destination(destination)
.asset(new AssetTypeNative())
.amount("350.1234567")
.asset(Asset.createNativeAsset())
.amount(new BigDecimal("350.1234567"))
.build();

// Build the transaction
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package network.lightsail;

import java.nio.charset.StandardCharsets;
import java.util.List;
import org.stellar.sdk.KeyPair;
import org.stellar.sdk.Network;
import org.stellar.sdk.SorobanServer;
import org.stellar.sdk.Transaction;
import org.stellar.sdk.TransactionBuilder;
import org.stellar.sdk.TransactionBuilderAccount;
import org.stellar.sdk.exception.NetworkException;
import org.stellar.sdk.exception.PrepareTransactionException;
import org.stellar.sdk.operations.InvokeHostFunctionOperation;
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;

public class SorobanInvokeContractFunction {
public static void main(String[] args) {
String secret = "SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV";
String rpcServerUrl = "https://soroban-testnet.stellar.org:443";
Network network = Network.TESTNET;

KeyPair keyPair = KeyPair.fromSecretSeed(secret);
SorobanServer sorobanServer = new SorobanServer(rpcServerUrl);
TransactionBuilderAccount source = sorobanServer.getAccount(keyPair.getAccountId());

// The invoke host function operation
// https://github.com/stellar/soroban-examples/tree/main/hello_world
String contractId = "CDZJVZWCY4NFGHCCZMX6QW5AK3ET5L3UUAYBVNDYOXDLQXW7PHXGYOBJ";
String functionName = "hello";
List<SCVal> parameters =
List.of(Scv.toString("World!")); // Scv is a helper class to create SCVal objects
InvokeHostFunctionOperation invokeHostFunctionOperation =
InvokeHostFunctionOperation.invokeContractFunctionOperationBuilder(
contractId, functionName, parameters)
.build();

// Build the transaction
Transaction unpreparedTransaction =
new TransactionBuilder(source, network)
.setBaseFee(Transaction.MIN_BASE_FEE)
.addOperation(invokeHostFunctionOperation)
.setTimeout(300)
.build();

// Prepare the transaction
Transaction transaction;
try {
transaction = sorobanServer.prepareTransaction(unpreparedTransaction);
} catch (PrepareTransactionException e) {
throw new RuntimeException("Prepare transaction failed", e);
} catch (NetworkException e) {
throw new RuntimeException("Network error", e);
}

// Sign the transaction
transaction.sign(keyPair);

// Send the transaction
SendTransactionResponse sendTransactionResponse;
try {
sendTransactionResponse = sorobanServer.sendTransaction(transaction);
} catch (NetworkException e) {
throw new RuntimeException("Send transaction failed", e);
}
if (!SendTransactionResponse.SendTransactionStatus.PENDING.equals(
sendTransactionResponse.getStatus())) {
throw new RuntimeException("Send transaction failed: " + sendTransactionResponse);
}

// Check the transaction status
GetTransactionResponse getTransactionResponse;
while (true) {
try {
getTransactionResponse = sorobanServer.getTransaction(sendTransactionResponse.getHash());
} catch (NetworkException e) {
throw new RuntimeException("Get transaction failed", e);
}

if (!GetTransactionResponse.GetTransactionStatus.NOT_FOUND.equals(
getTransactionResponse.getStatus())) {
break;
}
// Wait for 3 seconds before checking the transaction status again
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

if (GetTransactionResponse.GetTransactionStatus.SUCCESS.equals(
getTransactionResponse.getStatus())) {
System.out.println("Transaction succeeded: " + getTransactionResponse);
// parse the function return value
TransactionMeta transactionMeta = getTransactionResponse.parseResultMetaXdr();
SCVal rawReturnValue = transactionMeta.getV3().getSorobanMeta().getReturnValue();
System.out.println("Raw return value: " + rawReturnValue);
List<SCVal> vec = Scv.fromVec(rawReturnValue).stream().toList();
String returnValue0 = new String(Scv.fromString(vec.get(0)), StandardCharsets.UTF_8);
String returnValue1 = new String(Scv.fromString(vec.get(1)), StandardCharsets.UTF_8);
System.out.println("Return value 0: " + returnValue0);
System.out.println("Return value 1: " + returnValue1);
} else {
System.out.println("Transaction failed: " + getTransactionResponse);
}
}
}

0 comments on commit 5cb3fe0

Please sign in to comment.