Skip to content

Commit

Permalink
Merge branch 'main' into nk/xrp-fees
Browse files Browse the repository at this point in the history
  • Loading branch information
sappenin committed Sep 20, 2024
2 parents e5a77dd + be893dd commit 070cf5f
Show file tree
Hide file tree
Showing 99 changed files with 29,415 additions and 615 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ current [BOM](https://howtodoinjava.com/maven/maven-bom-bill-of-materials-depend
<dependency>
<groupId>org.xrpl</groupId>
<artifactId>xrpl4j-bom</artifactId>
<version>3.3.0</version>
<version>3.5.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
<property name="target" value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/>
</module>
<module name="JavadocMethod">
<property name="scope" value="public"/>
<property name="accessModifiers" value="public, protected"/>
<property name="allowMissingParamTags" value="true"/>
<property name="allowMissingReturnTag" value="true"/>
<property name="allowedAnnotations" value="Override, Test"/>
Expand Down
40 changes: 16 additions & 24 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.75</version>
<version>1.78.1</version>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
Expand Down Expand Up @@ -367,6 +367,9 @@
</goals>
</execution>
</executions>
<configuration>
<rerunFailingTestsCount>2</rerunFailingTestsCount>
</configuration>
</plugin>

<!-- org.apache.maven.plugins:maven-source-plugin -->
Expand Down Expand Up @@ -432,7 +435,7 @@
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.36.1</version>
<version>9.3</version>
</dependency>
</dependencies>
<executions>
Expand Down Expand Up @@ -498,19 +501,18 @@
</configuration>
</plugin>

<!-- org.sonatype.plugins:nexus-staging-maven-plugin -->
<!-- org.sonatype.central:central-publishing-maven-plugin -->
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.13</version>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.5.0</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh-snapshots</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
<publishingServerId>central</publishingServerId>
</configuration>
</plugin>


<!-- org.codehaus.mojo:license-maven-plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down Expand Up @@ -608,10 +610,10 @@
<artifactId>maven-surefire-plugin</artifactId>
</plugin>

<!-- org.sonatype.plugins:nexus-staging-maven-plugin -->
<!-- org.sonatype.central:central-publishing-maven-plugin -->
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
</plugin>

<plugin>
Expand Down Expand Up @@ -651,7 +653,8 @@
<plugin>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<inputEncoding>UTF-8</inputEncoding>
<outputEncoding>UTF-8</outputEncoding>
<consoleOutput>true</consoleOutput>
<linkXRef>false</linkXRef>
<excludes>**/generated-sources/**/*,**/generated-test-sources/**/*</excludes>
Expand All @@ -678,15 +681,4 @@
</plugins>
</reporting>

<distributionManagement>
<snapshotRepository>
<id>ossrh-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
</distributionManagement>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.annotations.Beta;
import feign.Feign;
import feign.Headers;
import feign.Request.Options;
import feign.RequestLine;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
Expand Down Expand Up @@ -61,21 +62,44 @@ public interface JsonRpcClient {
int SERVICE_UNAVAILABLE_STATUS = 503;
Duration RETRY_INTERVAL = Duration.ofSeconds(1);

String RESULT = "result";
String STATUS = "status";
String ERROR = "error";
String ERROR_EXCEPTION = "error_exception";
String ERROR_MESSAGE = "error_message";
String N_A = "n/a";

/**
* Constructs a new client for the given url.
*
* @param rippledUrl url for the faucet server.
* @param rippledUrl The {@link HttpUrl} of the node to connect to.
*
* @return A {@link JsonRpcClient} that can make request to {@code rippledUrl}
*/
static JsonRpcClient construct(final HttpUrl rippledUrl) {
Objects.requireNonNull(rippledUrl);

return construct(rippledUrl, new Options());
}

/**
* Constructs a new client for the given url with the given client options.
*
* @param rippledUrl The {@link HttpUrl} of the node to connect to.
* @param options An {@link Options}.
*
* @return A {@link JsonRpcClient}.
*/
static JsonRpcClient construct(HttpUrl rippledUrl, Options options) {
Objects.requireNonNull(rippledUrl);
Objects.requireNonNull(options);

return Feign.builder()
.encoder(new JacksonEncoder(objectMapper))
// rate limiting will return a 503 status that can be retried
.errorDecoder(new RetryStatusDecoder(RETRY_INTERVAL, SERVICE_UNAVAILABLE_STATUS))
.decode404()
.options(options)
.decoder(new OptionalDecoder(new JacksonDecoder(objectMapper)))
.target(JsonRpcClient.class, rippledUrl.toString());
}
Expand Down Expand Up @@ -134,7 +158,7 @@ default <T extends XrplResult> T send(
JavaType resultType
) throws JsonRpcClientErrorException {
JsonNode response = postRpcRequest(request);
JsonNode result = response.get("result");
JsonNode result = response.get(RESULT);
checkForError(response);
try {
return objectMapper.readValue(result.toString(), resultType);
Expand All @@ -151,13 +175,25 @@ default <T extends XrplResult> T send(
* @throws JsonRpcClientErrorException If rippled returns an error message.
*/
default void checkForError(JsonNode response) throws JsonRpcClientErrorException {
if (response.has("result")) {
JsonNode result = response.get("result");
if (result.has("error")) {
String errorMessage = Optional.ofNullable(result.get("error_exception"))
.map(JsonNode::asText)
.orElseGet(() -> result.get("error_message").asText());
throw new JsonRpcClientErrorException(errorMessage);
if (response.has(RESULT)) {
JsonNode result = response.get(RESULT);
if (result.has(STATUS)) {
String status = result.get(STATUS).asText();
if (status.equals(ERROR)) { // <-- Only an error if result.status == "error"
if (result.has(ERROR)) {
String errorCode = result.get(ERROR).asText();

final String errorMessage;
if (result.hasNonNull(ERROR_EXCEPTION)) {
errorMessage = result.get(ERROR_EXCEPTION).asText();
} else if (result.hasNonNull(ERROR_MESSAGE)) {
errorMessage = result.get(ERROR_MESSAGE).asText();
} else {
errorMessage = N_A;
}
throw new JsonRpcClientErrorException(String.format("%s (%s)", errorCode, errorMessage));
}
}
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions xrpl4j-client/src/main/java/org/xrpl/xrpl4j/client/XrplClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.google.common.collect.Range;
import com.google.common.primitives.UnsignedInteger;
import com.google.common.primitives.UnsignedLong;
import feign.Request;
import feign.Request.Options;
import okhttp3.HttpUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -72,6 +74,8 @@
import org.xrpl.xrpl4j.model.client.nft.NftInfoResult;
import org.xrpl.xrpl4j.model.client.nft.NftSellOffersRequestParams;
import org.xrpl.xrpl4j.model.client.nft.NftSellOffersResult;
import org.xrpl.xrpl4j.model.client.oracle.GetAggregatePriceRequestParams;
import org.xrpl.xrpl4j.model.client.oracle.GetAggregatePriceResult;
import org.xrpl.xrpl4j.model.client.path.BookOffersRequestParams;
import org.xrpl.xrpl4j.model.client.path.BookOffersResult;
import org.xrpl.xrpl4j.model.client.path.DepositAuthorizedRequestParams;
Expand All @@ -97,8 +101,10 @@
import org.xrpl.xrpl4j.model.transactions.Transaction;
import org.xrpl.xrpl4j.model.transactions.TransactionMetadata;

import java.time.Duration;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
* <p>A client which wraps a rippled network client and is responsible for higher order functionality such as signing
Expand All @@ -125,6 +131,31 @@ public XrplClient(final HttpUrl rippledUrl) {
this(JsonRpcClient.construct(rippledUrl));
}

/**
* Public constructor that allows for configuration of connect and read timeouts.
*
* <p>Note that any {@link Duration} passed in that is less than one millisecond will result in the actual timeout
* being zero milliseconds. It is therefore advised to never set {@code connectTimeout} or {@code readTimeout} to a
* {@link Duration} less than one millisecond.
*
* @param rippledUrl The {@link HttpUrl} of the node to connect to.
* @param connectTimeout A {@link Duration} indicating the client's connect timeout.
* @param readTimeout A {@link Duration} indicating the client's read timeout.
*/
public XrplClient(
HttpUrl rippledUrl,
Duration connectTimeout,
Duration readTimeout
) {
this(
JsonRpcClient.construct(
rippledUrl,
new Options(connectTimeout.toMillis(), TimeUnit.MILLISECONDS, readTimeout.toMillis(), TimeUnit.MILLISECONDS,
true)
)
);
}

/**
* Required-args constructor (exists for testing purposes only).
*
Expand Down Expand Up @@ -810,6 +841,28 @@ public AmmInfoResult ammInfo(
return jsonRpcClient.send(request, AmmInfoResult.class);
}

/**
* Retreive the aggregate price of specified oracle objects, returning three price statistics: mean, median, and
* trimmed mean.
*
* @param params A {@link GetAggregatePriceRequestParams}.
*
* @return A {@link GetAggregatePriceResult}.
*
* @throws JsonRpcClientErrorException if {@code jsonRpcClient} throws an error.
*/
@Beta
public GetAggregatePriceResult getAggregatePrice(
GetAggregatePriceRequestParams params
) throws JsonRpcClientErrorException {
JsonRpcRequest request = JsonRpcRequest.builder()
.method(XrplMethods.GET_AGGREGATE_PRICE)
.addParams(params)
.build();

return jsonRpcClient.send(request, GetAggregatePriceResult.class);
}

public JsonRpcClient getJsonRpcClient() {
return jsonRpcClient;
}
Expand Down
Loading

0 comments on commit 070cf5f

Please sign in to comment.