Skip to content

Commit

Permalink
add get aggregate price mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
nkramer44 committed May 15, 2024
1 parent c7b907c commit 40ef435
Show file tree
Hide file tree
Showing 9 changed files with 540 additions and 0 deletions.
24 changes: 24 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 @@ -72,6 +72,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 Down Expand Up @@ -810,6 +812,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
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,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 Down Expand Up @@ -1087,4 +1089,22 @@ void nftInfo() throws JsonRpcClientErrorException {

assertThat(result).isEqualTo(mockResult);
}

@Test
void getAggregatePrice() throws JsonRpcClientErrorException {
GetAggregatePriceRequestParams params = mock(GetAggregatePriceRequestParams.class);
GetAggregatePriceResult expectedResult = mock(GetAggregatePriceResult.class);

when(jsonRpcClientMock.send(
JsonRpcRequest.builder()
.method(XrplMethods.GET_AGGREGATE_PRICE)
.addParams(params)
.build(),
GetAggregatePriceResult.class
)).thenReturn(expectedResult);

GetAggregatePriceResult result = xrplClient.getAggregatePrice(params);

assertThat(result).isEqualTo(expectedResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,5 @@ public class XrplMethods {
*/
public static final String PING = "ping";

public static final String GET_AGGREGATE_PRICE = "get_aggregate_price";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.xrpl.xrpl4j.model.client.oracle;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.annotations.Beta;
import com.google.common.primitives.UnsignedLong;
import org.immutables.value.Value.Immutable;
import org.immutables.value.Value.Lazy;

import java.math.BigDecimal;

/**
* Statistics from collected oracle prices.
*/
@Beta
@Immutable
@JsonSerialize(as = ImmutableAggregatePriceSet.class)
@JsonDeserialize(as = ImmutableAggregatePriceSet.class)
public interface AggregatePriceSet {

/**
* Construct a {@code AggregatePriceSet} builder.
*
* @return An {@link ImmutableAggregatePriceSet.Builder}.
*/
static ImmutableAggregatePriceSet.Builder builder() {
return ImmutableAggregatePriceSet.builder();
}

/**
* The simple mean price.
*
* @return A {@link String}.
*/
@JsonProperty("mean")
String meanString();

/**
* The simple mean price as a {@link BigDecimal}.
*
* @return A {@link BigDecimal}.
*/
@Lazy
@JsonIgnore
default BigDecimal mean() {
return new BigDecimal(meanString());
}

/**
* The size of the data set to calculate the mean.
*
* @return An {@link UnsignedLong}.
*/
UnsignedLong size();

/**
* The standard deviation.
*
* @return A {@link String}.
*/
@JsonProperty("standard_deviation")
String standardDeviationString();

/**
* The standard deviation as a {@link BigDecimal}.
*
* @return A {@link BigDecimal}.
*/
@Lazy
@JsonIgnore
default BigDecimal standardDeviation() {
return new BigDecimal(standardDeviationString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.xrpl.xrpl4j.model.client.oracle;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.annotations.Beta;
import com.google.common.primitives.UnsignedInteger;
import org.immutables.value.Value.Immutable;
import org.xrpl.xrpl4j.model.client.XrplRequestParams;
import org.xrpl.xrpl4j.model.client.common.LedgerSpecifier;
import org.xrpl.xrpl4j.model.client.ledger.OracleLedgerEntryParams;

import java.util.List;
import java.util.Optional;

/**
* Request parameters for the {@code get_aggregate_price} RPC method.
*/
@Beta
@Immutable
@JsonSerialize(as = ImmutableGetAggregatePriceRequestParams.class)
@JsonDeserialize(as = ImmutableGetAggregatePriceRequestParams.class)
public interface GetAggregatePriceRequestParams extends XrplRequestParams {

/**
* Construct a {@code GetAggregatePriceRequestParams} builder.
*
* @return An {@link ImmutableGetAggregatePriceRequestParams.Builder}.
*/
static ImmutableGetAggregatePriceRequestParams.Builder builder() {
return ImmutableGetAggregatePriceRequestParams.builder();
}

/**
* Specifies the ledger version to request. A ledger version can be specified by ledger hash,
* numerical ledger index, or a shortcut value.
*
* @return A {@link LedgerSpecifier} specifying the ledger version to request.
*/
@JsonUnwrapped
LedgerSpecifier ledgerSpecifier();

/**
* The currency code of the asset to be priced.
*
* @return A {@link String}.
*/
@JsonProperty("base_asset")
String baseAsset();

/**
* The currency code of the asset to quote the price of the base asset.
*
* @return A {@link String}.
*/
@JsonProperty("quote_asset")
String quoteAsset();

/**
* The percentage of outliers to trim. Valid trim range is 1-25. If included, the API returns statistics for the
* trimmed mean.
*
* @return An {@link Optional} {@link UnsignedInteger}.
*/
Optional<UnsignedInteger> trim();

/**
* Defines a time range in seconds for filtering out older price data. Default value is 0, which doesn't filter any
* data.
*
* @return An {@link Optional} {@link UnsignedInteger}.
*/
@JsonProperty("trim_threshold")
Optional<UnsignedInteger> trimThreshold();

/**
* A list of oracle identifiers.
*
* @return A {@link List} of {@link OracleLedgerEntryParams}.
*/
List<OracleLedgerEntryParams> oracles();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package org.xrpl.xrpl4j.model.client.oracle;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.annotations.Beta;
import com.google.common.primitives.UnsignedInteger;
import org.immutables.value.Value;
import org.immutables.value.Value.Immutable;
import org.immutables.value.Value.Lazy;
import org.xrpl.xrpl4j.model.client.XrplResult;
import org.xrpl.xrpl4j.model.client.common.LedgerIndex;
import org.xrpl.xrpl4j.model.transactions.Hash256;

import java.math.BigDecimal;
import java.util.Optional;

/**
* Result object for {@code get_aggregate_price} RPC calls.
*/
@Beta
@Immutable
@JsonSerialize(as = ImmutableGetAggregatePriceResult.class)
@JsonDeserialize(as = ImmutableGetAggregatePriceResult.class)
public interface GetAggregatePriceResult extends XrplResult {

/**
* Construct a {@code GetAggregatePriceResult} builder.
*
* @return An {@link ImmutableGetAggregatePriceResult.Builder}.
*/
static ImmutableGetAggregatePriceResult.Builder builder() {
return ImmutableGetAggregatePriceResult.builder();
}

/**
* The statistics from the collected oracle prices.
*
* @return An {@link AggregatePriceSet}.
*/
@JsonProperty("entire_set")
AggregatePriceSet entireSet();

/**
* The trimmed statistics from the collected oracle prices. Only appears if the trim field was specified in the
* request.
*
* @return An {@link Optional} {@link AggregatePriceSet}.
*/
@JsonProperty("trimmed_set")
Optional<AggregatePriceSet> trimmedSet();

/**
* The median price, as a {@link String}.
*
* @return A {@link String}.
*/
@JsonProperty("median")
String medianString();

/**
* Get the median price as a {@link BigDecimal}.
*
* @return A {@link BigDecimal}.
*/
@Lazy
@JsonIgnore
default BigDecimal median() {
return new BigDecimal(medianString());
}

/**
* The most recent timestamp out of all LastUpdateTime values.
*
* @return An {@link UnsignedInteger}.
*/
UnsignedInteger time();

/**
* The identifying Hash of the ledger version used to generate this response.
*
* @return A {@link Hash256} containing the ledger hash.
*/
@JsonProperty("ledger_hash")
Optional<Hash256> ledgerHash();

/**
* Get {@link #ledgerHash()}, or throw an {@link IllegalStateException} if {@link #ledgerHash()} is empty.
*
* @return The value of {@link #ledgerHash()}.
*
* @throws IllegalStateException If {@link #ledgerHash()} is empty.
*/
@JsonIgnore
@Value.Auxiliary
default Hash256 ledgerHashSafe() {
return ledgerHash()
.orElseThrow(() -> new IllegalStateException("Result did not contain a ledgerHash."));
}

/**
* The Ledger Index of the ledger version used to generate this response. Only present in responses to requests with
* ledger_index = "validated" or "closed".
*
* @return A {@link LedgerIndex}.
*/
@JsonProperty("ledger_index")
Optional<LedgerIndex> ledgerIndex();

/**
* Get {@link #ledgerIndex()}, or throw an {@link IllegalStateException} if {@link #ledgerIndex()} is empty.
*
* @return The value of {@link #ledgerIndex()}.
*
* @throws IllegalStateException If {@link #ledgerIndex()} is empty.
*/
@JsonIgnore
@Value.Auxiliary
default LedgerIndex ledgerIndexSafe() {
return ledgerIndex()
.orElseThrow(() -> new IllegalStateException("Result did not contain a ledgerIndex."));
}

/**
* The ledger index of the current open ledger, which was used when retrieving this information. Only present in
* responses to requests with ledger_index = "current".
*
* @return An optionally-present {@link LedgerIndex} representing the current ledger index.
*/
@JsonProperty("ledger_current_index")
Optional<LedgerIndex> ledgerCurrentIndex();

/**
* Get {@link #ledgerCurrentIndex()}, or throw an {@link IllegalStateException} if {@link #ledgerCurrentIndex()} is
* empty.
*
* @return The value of {@link #ledgerCurrentIndex()}.
*
* @throws IllegalStateException If {@link #ledgerCurrentIndex()} is empty.
*/
@JsonIgnore
@Value.Auxiliary
default LedgerIndex ledgerCurrentIndexSafe() {
return ledgerCurrentIndex()
.orElseThrow(() -> new IllegalStateException("Result did not contain a ledgerCurrentIndex."));
}

/**
* If true, the information in this response comes from a validated ledger version. Otherwise, the information is
* subject to change.
*
* @return {@code true} if the information in this response comes from a validated ledger version, {@code false} if
* not.
*/
@Value.Default
default boolean validated() {
return false;
}
}
Loading

0 comments on commit 40ef435

Please sign in to comment.