Skip to content

Instantly share code, notes, and snippets.

@nullstyle
Last active December 16, 2015 19:47
Show Gist options
  • Save nullstyle/db1e5742819b7eb3f5f9 to your computer and use it in GitHub Desktop.
Save nullstyle/db1e5742819b7eb3f5f9 to your computer and use it in GitHub Desktop.
package somepackage;
import java.util.Optional;
import org.stellar.sdk.Account;
import org.stellar.sdk.Operation;
import org.stellar.sdk.Server;
import org.stellar.sdk.SubmissionResponse;
import org.stellar.sdk.SubmissionSuccessResponse;
public class Main {
// A simple example that illustrates refunding the last payment sent to
// an address
public static void main(String[] args) {
String id = "GCEJJI5DOVSLDVXROAU6J66T67YONNWUU2BTDQZDXXQM4NJ4JMNEASVA";
Server s = Server.testNet();
Account a = s.getAccount(id);
// reading data from horizon is simple (assuming java 8 here and us
// implementing Stream<T> for Page<T>).
Optional<Operation> lastReceived = a.payments()
.order("DESC")
.execute()
.stream()
.filter(p -> p.getTo() == id)
.findFirst()
if (!lastReceived.isPresent()) {
return;
}
// the account object provides a method into the authoring portions of the
// library, returning a builder that we can call methods on to construct a
// new payments.
SubmissionResponse resp = a.newPayment()
.destination(lastReceived.getFrom())
.asset(lastReceived.getAsset())
.amount(lastReceived.getAmount())
.memoReturn(lastReceived.getTxHash())
.sign("SCZJN2SXIIVJ5TCN72ZPNMSGMOMXB3XP3UC66OPQ3YP6GZNGFL2HVFZA")
.submit();
if (resp.isFailure()) {
// TODO
return
}
SubmissionSuccessResponse success = resp.getSuccess();
System.out.println("tx:" + success.getTxHash() + " paging_token: " + success.getTransaction().getPagingToken());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment