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

Implement Horizon Protocol 14 API changes #295

Merged
merged 3 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ dependencies {
compile 'com.google.code.gson:gson:2.8.5'
compile 'commons-io:commons-io:2.6'
compile 'net.i2p.crypto:eddsa:0.3.0'

compile 'org.threeten:threetenbp:1.4.4'

testCompile 'org.mockito:mockito-core:2.21.0'
testCompile "com.squareup.okhttp3:mockwebserver:${okhttpclientVersion}"
testCompile 'junit:junit:4.12'
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/stellar/sdk/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
public abstract class Asset {
Asset() {}

public static Asset create(String canonicalForm) {
tamirms marked this conversation as resolved.
Show resolved Hide resolved
if (canonicalForm.equals("native")) {
return new AssetTypeNative();
}
String [] parts = canonicalForm.split(":");
if (parts.length != 2) {
throw new IllegalArgumentException("invalid asset "+ canonicalForm);
}
return Asset.createNonNativeAsset(parts[0], parts[1]);
}

public static Asset create(String type, String code, String issuer) {
if (type.equals("native")) {
return new AssetTypeNative();
Expand Down
255 changes: 255 additions & 0 deletions src/main/java/org/stellar/sdk/Predicate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
package org.stellar.sdk;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import org.stellar.sdk.xdr.ClaimPredicate;
import org.stellar.sdk.xdr.ClaimPredicateType;
import org.stellar.sdk.xdr.Int64;
import org.threeten.bp.Instant;

import java.util.Date;
import java.util.List;

public abstract class Predicate {

private static List<Predicate> convertXDRPredicates(ClaimPredicate[] predicates) {
List<Predicate> list = Lists.newArrayList();
for (ClaimPredicate p : predicates) {
list.add(fromXdr(p));
}
return list;
}

/**
* Generates Asset object from a given XDR object
tamirms marked this conversation as resolved.
Show resolved Hide resolved
* @param xdr XDR object
*/
public static Predicate fromXdr(org.stellar.sdk.xdr.ClaimPredicate xdr) {
switch (xdr.getDiscriminant()) {
case CLAIM_PREDICATE_UNCONDITIONAL:
return new Unconditional();
case CLAIM_PREDICATE_AND:
return new And(convertXDRPredicates(xdr.getAndPredicates()));
case CLAIM_PREDICATE_OR:
return new Or(convertXDRPredicates(xdr.getOrPredicates()));
case CLAIM_PREDICATE_NOT:
return new Not(fromXdr(xdr.getNotPredicate()));
case CLAIM_PREDICATE_BEFORE_RELATIVE_TIME:
return new RelBefore(xdr.getRelBefore().getInt64());
case CLAIM_PREDICATE_BEFORE_ABSOLUTE_TIME:
return new AbsBefore(xdr.getAbsBefore().getInt64());
default:
throw new IllegalArgumentException("Unknown asset type " + xdr.getDiscriminant());
}
}

@Override
public abstract boolean equals(Object object);

/**
* Generates XDR object from a given Asset object
tamirms marked this conversation as resolved.
Show resolved Hide resolved
*/
public abstract org.stellar.sdk.xdr.ClaimPredicate toXdr();

public static class Unconditional extends Predicate {
@Override
public boolean equals(Object o) {
return (this == o) || (getClass() == o.getClass());
}

@Override
public int hashCode() {
return 0;
}

@Override
public ClaimPredicate toXdr() {
org.stellar.sdk.xdr.ClaimPredicate xdr = new org.stellar.sdk.xdr.ClaimPredicate();
xdr.setDiscriminant(ClaimPredicateType.CLAIM_PREDICATE_UNCONDITIONAL);
return xdr;
}
}

public static class Not extends Predicate {
private final Predicate inner;
public Not(Predicate inner) {
this.inner = inner;
}

public Predicate getInner() {
return inner;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
return (getClass() == o.getClass()) && Objects.equal(inner, ((Not)o).inner);
}

@Override
public int hashCode() {
return Objects.hashCode(inner);
}

@Override
public ClaimPredicate toXdr() {
org.stellar.sdk.xdr.ClaimPredicate xdr = new org.stellar.sdk.xdr.ClaimPredicate();
xdr.setDiscriminant(ClaimPredicateType.CLAIM_PREDICATE_NOT);
xdr.setNotPredicate(inner.toXdr());
return xdr;
}
}

public static class Or extends Predicate {
private final List<Predicate> inner;
tamirms marked this conversation as resolved.
Show resolved Hide resolved

public Or(List<Predicate> inner) {
this.inner = inner;
}

public List<Predicate> getInner() {
return inner;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
return (getClass() == o.getClass()) && Objects.equal(inner, ((Or)o).inner);
}

@Override
public int hashCode() {
return Objects.hashCode(inner);
}

@Override
public ClaimPredicate toXdr() {
org.stellar.sdk.xdr.ClaimPredicate xdr = new org.stellar.sdk.xdr.ClaimPredicate();
xdr.setDiscriminant(ClaimPredicateType.CLAIM_PREDICATE_OR);
ClaimPredicate[] xdrInner = new ClaimPredicate[inner.size()];
for (int i = 0; i < inner.size(); i++) {
xdrInner[i] = inner.get(i).toXdr();
}
xdr.setOrPredicates(xdrInner);
return xdr;
}
}


public static class And extends Predicate {
private final List<Predicate> inner;

public And(List<Predicate> inner) {
this.inner = inner;
}

public List<Predicate> getInner() {
return inner;
}


@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
return (getClass() == o.getClass()) && Objects.equal(inner, ((And)o).inner);
}

@Override
public int hashCode() {
return Objects.hashCode(inner);
}

@Override
public ClaimPredicate toXdr() {
org.stellar.sdk.xdr.ClaimPredicate xdr = new org.stellar.sdk.xdr.ClaimPredicate();
xdr.setDiscriminant(ClaimPredicateType.CLAIM_PREDICATE_AND);
ClaimPredicate[] xdrInner = new ClaimPredicate[inner.size()];
for (int i = 0; i < inner.size(); i++) {
xdrInner[i] = inner.get(i).toXdr();
}
xdr.setAndPredicates(xdrInner);
return xdr;
}
}

public static class AbsBefore extends Predicate {
private final long epochSeconds;

public AbsBefore(long epochSeconds) {
this.epochSeconds = epochSeconds;
}

public long getTimestampSeconds() {
return epochSeconds;
}

public Instant getDate() {
return Instant.ofEpochSecond(epochSeconds);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
return (getClass() == o.getClass()) && Objects.equal(epochSeconds, ((AbsBefore)o).epochSeconds);
}

@Override
public int hashCode() {
return Objects.hashCode(epochSeconds);
}

@Override
public ClaimPredicate toXdr() {
org.stellar.sdk.xdr.ClaimPredicate xdr = new org.stellar.sdk.xdr.ClaimPredicate();
xdr.setDiscriminant(ClaimPredicateType.CLAIM_PREDICATE_BEFORE_ABSOLUTE_TIME);
Int64 t = new Int64();
t.setInt64(epochSeconds);
xdr.setAbsBefore(t);
return xdr;
}
}

public static class RelBefore extends Predicate {
private final long secondsSinceClose;

public RelBefore(long secondsSinceClose) {
this.secondsSinceClose = secondsSinceClose;
}

public long getSecondsSinceClose() {
return secondsSinceClose;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
return (getClass() == o.getClass()) && Objects.equal(secondsSinceClose, ((RelBefore)o).secondsSinceClose);
}

@Override
public int hashCode() {
return Objects.hashCode(secondsSinceClose);
}

@Override
public ClaimPredicate toXdr() {
org.stellar.sdk.xdr.ClaimPredicate xdr = new org.stellar.sdk.xdr.ClaimPredicate();
xdr.setDiscriminant(ClaimPredicateType.CLAIM_PREDICATE_BEFORE_RELATIVE_TIME);
Int64 t = new Int64();
t.setInt64(secondsSinceClose);
xdr.setRelBefore(t);
return xdr;
}
}

}
7 changes: 7 additions & 0 deletions src/main/java/org/stellar/sdk/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ public AssetsRequestBuilder assets() {
return new AssetsRequestBuilder(httpClient, serverURI);
}

/**
* Returns {@link ClaimableBalancesRequestBuilder} instance.
*/
public ClaimableBalancesRequestBuilder claimableBalances() {
return new ClaimableBalancesRequestBuilder(httpClient, serverURI);
}

/**
* Returns {@link EffectsRequestBuilder} instance.
*/
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/stellar/sdk/requests/AccountsRequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class AccountsRequestBuilder extends RequestBuilder {
private static final String ASSET_PARAMETER_NAME = "asset";
private static final String SIGNER_PARAMETER_NAME = "signer";
private static final String SPONSOR_PARAMETER_NAME = "sponsor";

public AccountsRequestBuilder(OkHttpClient httpClient, HttpUrl serverURI) {
super(httpClient, serverURI, "accounts");
Expand Down Expand Up @@ -59,6 +60,9 @@ public AccountsRequestBuilder forSigner(String signer) {
if (uriBuilder.build().queryParameter(ASSET_PARAMETER_NAME) != null) {
throw new RuntimeException("cannot set both signer and asset");
}
if (uriBuilder.build().queryParameter(SPONSOR_PARAMETER_NAME) != null) {
throw new RuntimeException("cannot set both signer and sponsor");
}
uriBuilder.setQueryParameter(SIGNER_PARAMETER_NAME, signer);
return this;
}
Expand All @@ -74,10 +78,31 @@ public AccountsRequestBuilder forAsset(AssetTypeCreditAlphaNum asset) {
if (uriBuilder.build().queryParameter(SIGNER_PARAMETER_NAME) != null) {
throw new RuntimeException("cannot set both signer and asset");
}
if (uriBuilder.build().queryParameter(SPONSOR_PARAMETER_NAME) != null) {
throw new RuntimeException("cannot set both sponsor and asset");
}
setAssetParameter(ASSET_PARAMETER_NAME, asset);
return this;
}

/**
* Returns all accounts who are sponsored by a given account or have subentries which are sponsored by a given account.
*
* @param sponsor Account ID
* @return current {@link AccountsRequestBuilder} instance
* @see <a href="https://www.stellar.org/developers/horizon/reference/endpoints/accounts.html">Accounts</a>
*/
public AccountsRequestBuilder forSponsor(String sponsor) {
if (uriBuilder.build().queryParameter(SIGNER_PARAMETER_NAME) != null) {
throw new RuntimeException("cannot set both signer and sponsor");
}
if (uriBuilder.build().queryParameter(ASSET_PARAMETER_NAME) != null) {
throw new RuntimeException("cannot set both asset and sponsor");
}
uriBuilder.setQueryParameter(SPONSOR_PARAMETER_NAME, sponsor);
return this;
}

/**
* Requests specific <code>uri</code> and returns {@link Page} of {@link AccountResponse}.
* This method is helpful for getting the next set of results.
Expand Down
Loading