Skip to content

Commit

Permalink
Add support for book_offers RPC (#440)
Browse files Browse the repository at this point in the history
* first pass at book_offers request/result
* javadoc and test
* add XrplClient method and call it in offerIT
* fix javadoc
* make BookOffersRequestParams.ledgerSpecifier non-optional so deserialization works correctly

---------

Co-authored-by: David Fuelling <[email protected]>
  • Loading branch information
nkramer44 and sappenin authored Jul 13, 2023
1 parent 8ae2897 commit 6e085fa
Show file tree
Hide file tree
Showing 13 changed files with 1,055 additions and 56 deletions.
74 changes: 46 additions & 28 deletions xrpl4j-client/src/main/java/org/xrpl/xrpl4j/client/XrplClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.google.common.primitives.UnsignedInteger;
import com.google.common.primitives.UnsignedLong;
import okhttp3.HttpUrl;
import org.immutables.value.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xrpl.xrpl4j.codec.binary.XrplBinaryCodec;
Expand Down Expand Up @@ -67,6 +66,8 @@
import org.xrpl.xrpl4j.model.client.nft.NftBuyOffersResult;
import org.xrpl.xrpl4j.model.client.nft.NftSellOffersRequestParams;
import org.xrpl.xrpl4j.model.client.nft.NftSellOffersResult;
import org.xrpl.xrpl4j.model.client.path.BookOffersRequestParams;
import org.xrpl.xrpl4j.model.client.path.BookOffersResult;
import org.xrpl.xrpl4j.model.client.path.DepositAuthorizedRequestParams;
import org.xrpl.xrpl4j.model.client.path.DepositAuthorizedResult;
import org.xrpl.xrpl4j.model.client.path.RipplePathFindRequestParams;
Expand All @@ -84,34 +85,10 @@
import org.xrpl.xrpl4j.model.client.transactions.TransactionResult;
import org.xrpl.xrpl4j.model.immutables.FluentCompareTo;
import org.xrpl.xrpl4j.model.jackson.ObjectMapperFactory;
import org.xrpl.xrpl4j.model.transactions.AccountDelete;
import org.xrpl.xrpl4j.model.transactions.AccountSet;
import org.xrpl.xrpl4j.model.transactions.Address;
import org.xrpl.xrpl4j.model.transactions.CheckCancel;
import org.xrpl.xrpl4j.model.transactions.CheckCash;
import org.xrpl.xrpl4j.model.transactions.CheckCreate;
import org.xrpl.xrpl4j.model.transactions.DepositPreAuth;
import org.xrpl.xrpl4j.model.transactions.EscrowCancel;
import org.xrpl.xrpl4j.model.transactions.EscrowCreate;
import org.xrpl.xrpl4j.model.transactions.EscrowFinish;
import org.xrpl.xrpl4j.model.transactions.Hash256;
import org.xrpl.xrpl4j.model.transactions.NfTokenAcceptOffer;
import org.xrpl.xrpl4j.model.transactions.NfTokenBurn;
import org.xrpl.xrpl4j.model.transactions.NfTokenCancelOffer;
import org.xrpl.xrpl4j.model.transactions.NfTokenCreateOffer;
import org.xrpl.xrpl4j.model.transactions.NfTokenMint;
import org.xrpl.xrpl4j.model.transactions.OfferCancel;
import org.xrpl.xrpl4j.model.transactions.OfferCreate;
import org.xrpl.xrpl4j.model.transactions.Payment;
import org.xrpl.xrpl4j.model.transactions.PaymentChannelClaim;
import org.xrpl.xrpl4j.model.transactions.PaymentChannelCreate;
import org.xrpl.xrpl4j.model.transactions.PaymentChannelFund;
import org.xrpl.xrpl4j.model.transactions.SetRegularKey;
import org.xrpl.xrpl4j.model.transactions.SignerListSet;
import org.xrpl.xrpl4j.model.transactions.TicketCreate;
import org.xrpl.xrpl4j.model.transactions.Transaction;
import org.xrpl.xrpl4j.model.transactions.TransactionMetadata;
import org.xrpl.xrpl4j.model.transactions.TrustSet;

import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -160,6 +137,7 @@ public XrplClient(final HttpUrl rippledUrl) {
* @param signedTransaction A {@link SingleSignedTransaction} to submit.
*
* @return The {@link SubmitResult} resulting from the submission request.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
* @throws JsonProcessingException if any JSON is invalid.
* @see "https://xrpl.org/submit.html"
Expand Down Expand Up @@ -194,6 +172,7 @@ public <T extends Transaction> SubmitResult<T> submit(final SingleSignedTransact
* @param <T> A type parameter for the type of {@link Transaction} being submitted.
*
* @return A {@link SubmitMultiSignedResult} of type {@link T}.
*
* @throws JsonRpcClientErrorException if {@code jsonRpcClient} throws an error.
*/
public <T extends Transaction> SubmitMultiSignedResult<T> submitMultisigned(MultiSignedTransaction<T> transaction)
Expand All @@ -214,6 +193,7 @@ public <T extends Transaction> SubmitMultiSignedResult<T> submitMultisigned(Mult
* Get the current state of the open-ledger requirements for transaction costs.
*
* @return A {@link FeeResult} containing information about current transaction costs.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
* @see "https://xrpl.org/fee.html"
*/
Expand All @@ -229,6 +209,7 @@ public FeeResult fee() throws JsonRpcClientErrorException {
* Get the ledger index of a tx result response. If not present, throw an exception.
*
* @return A string containing value of last validated ledger index.
*
* @throws JsonRpcClientErrorException when client encounters errors related to calling rippled JSON RPC API..
*/
protected UnsignedInteger getMostRecentlyValidatedLedgerIndex() throws JsonRpcClientErrorException {
Expand Down Expand Up @@ -385,6 +366,7 @@ public Finality isFinal(
* {@link ServerInfo}.
*
* @return A {@link ServerInfoResult} containing information about the server.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
* @see "https://xrpl.org/server_info.html"
*/
Expand All @@ -404,6 +386,7 @@ public ServerInfoResult serverInformation()
* @param params The {@link AccountChannelsRequestParams} to send in the request.
*
* @return The {@link AccountChannelsResult} returned by the account_channels method call.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public AccountChannelsResult accountChannels(AccountChannelsRequestParams params) throws JsonRpcClientErrorException {
Expand All @@ -422,6 +405,7 @@ public AccountChannelsResult accountChannels(AccountChannelsRequestParams params
* @param params The {@link AccountCurrenciesRequestParams} to send in the request.
*
* @return The {@link AccountCurrenciesResult} returned by the account_currencies method call.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public AccountCurrenciesResult accountCurrencies(
Expand All @@ -442,6 +426,7 @@ public AccountCurrenciesResult accountCurrencies(
* @param params The {@link AccountInfoRequestParams} to send in the request.
*
* @return The {@link AccountInfoResult} returned by the account_info method call.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public AccountInfoResult accountInfo(AccountInfoRequestParams params) throws JsonRpcClientErrorException {
Expand All @@ -459,6 +444,7 @@ public AccountInfoResult accountInfo(AccountInfoRequestParams params) throws Jso
* @param account to get the NFTs for.
*
* @return {@link AccountNftsResult} containing list of accounts for an address.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public AccountNftsResult accountNfts(Address account) throws JsonRpcClientErrorException {
Expand All @@ -469,12 +455,13 @@ public AccountNftsResult accountNfts(Address account) throws JsonRpcClientErrorE
}

/**
* Get the {@link AccountNftsResult} for the account specified in {@code params} by making an account_channels
* method call.
* Get the {@link AccountNftsResult} for the account specified in {@code params} by making an account_channels method
* call.
*
* @param params The {@link AccountNftsRequestParams} to send in the request.
*
* @return The {@link AccountNftsResult} returned by the account_nfts method call.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public AccountNftsResult accountNfts(AccountNftsRequestParams params) throws JsonRpcClientErrorException {
Expand All @@ -493,6 +480,7 @@ public AccountNftsResult accountNfts(AccountNftsRequestParams params) throws Jso
* @param params The {@link NftBuyOffersRequestParams} to send in the request.
*
* @return The {@link NftBuyOffersResult} returned by the nft_buy_offers method call.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public NftBuyOffersResult nftBuyOffers(NftBuyOffersRequestParams params) throws JsonRpcClientErrorException {
Expand All @@ -511,6 +499,7 @@ public NftBuyOffersResult nftBuyOffers(NftBuyOffersRequestParams params) throws
* @param params The {@link NftSellOffersRequestParams} to send in the request.
*
* @return The {@link NftSellOffersResult} returned by the nft_sell_offers method call.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public NftSellOffersResult nftSellOffers(NftSellOffersRequestParams params) throws JsonRpcClientErrorException {
Expand All @@ -529,6 +518,7 @@ public NftSellOffersResult nftSellOffers(NftSellOffersRequestParams params) thro
* @param params The {@link AccountObjectsRequestParams} to send in the request.
*
* @return The {@link AccountObjectsResult} returned by the account_objects method call.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public AccountObjectsResult accountObjects(AccountObjectsRequestParams params) throws JsonRpcClientErrorException {
Expand All @@ -546,6 +536,7 @@ public AccountObjectsResult accountObjects(AccountObjectsRequestParams params) t
* @param params The {@link AccountOffersRequestParams} to send in the request.
*
* @return The {@link AccountOffersResult} returned by the account_offers method call.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public AccountOffersResult accountOffers(AccountOffersRequestParams params) throws JsonRpcClientErrorException {
Expand All @@ -563,6 +554,7 @@ public AccountOffersResult accountOffers(AccountOffersRequestParams params) thro
* @param params A {@link DepositAuthorizedRequestParams} to send in the request.
*
* @return The {@link DepositAuthorizedResult} returned by the deposit_authorized method call.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public DepositAuthorizedResult depositAuthorized(DepositAuthorizedRequestParams params)
Expand All @@ -581,6 +573,7 @@ public DepositAuthorizedResult depositAuthorized(DepositAuthorizedRequestParams
* @param address The {@link Address} of the account to request.
*
* @return The {@link AccountTransactionsResult} returned by the account_tx method call.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public AccountTransactionsResult accountTransactions(Address address) throws JsonRpcClientErrorException {
Expand All @@ -596,6 +589,7 @@ public AccountTransactionsResult accountTransactions(Address address) throws Jso
* @param params The {@link AccountTransactionsRequestParams} to send in the request.
*
* @return The {@link AccountTransactionsResult} returned by the account_tx method call.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public AccountTransactionsResult accountTransactions(AccountTransactionsRequestParams params)
Expand All @@ -616,6 +610,7 @@ public AccountTransactionsResult accountTransactions(AccountTransactionsRequestP
* @param <T> Type parameter for the type of {@link Transaction} that the {@link TransactionResult} will
*
* @return A {@link TransactionResult} containing the requested transaction and other metadata.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public <T extends Transaction> TransactionResult<T> transaction(
Expand All @@ -641,6 +636,7 @@ public <T extends Transaction> TransactionResult<T> transaction(
* @param params The {@link LedgerRequestParams} to send in the request.
*
* @return A {@link LedgerResult} containing the ledger details.
*
* @throws JsonRpcClientErrorException if {@code jsonRpcClient} throws an error.
*/
public LedgerResult ledger(LedgerRequestParams params) throws JsonRpcClientErrorException {
Expand All @@ -658,6 +654,7 @@ public LedgerResult ledger(LedgerRequestParams params) throws JsonRpcClientError
* @param params The {@link RipplePathFindRequestParams} to send in the request.
*
* @return A {@link RipplePathFindResult} containing possible paths.
*
* @throws JsonRpcClientErrorException if {@code jsonRpcClient} throws an error.
*/
public RipplePathFindResult ripplePathFind(RipplePathFindRequestParams params) throws JsonRpcClientErrorException {
Expand All @@ -669,12 +666,31 @@ public RipplePathFindResult ripplePathFind(RipplePathFindRequestParams params) t
return jsonRpcClient.send(request, RipplePathFindResult.class);
}

/**
* Send a {@code book_offers} RPC request.
*
* @param params The {@link BookOffersRequestParams} to send in the request.
*
* @return A {@link BookOffersResult}.
*
* @throws JsonRpcClientErrorException If {@code jsonRpcClient} throws an error.
*/
public BookOffersResult bookOffers(BookOffersRequestParams params) throws JsonRpcClientErrorException {
JsonRpcRequest request = JsonRpcRequest.builder()
.method(XrplMethods.BOOK_OFFERS)
.addParams(params)
.build();

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

/**
* Get the trust lines for a given account by sending an account_lines method request.
*
* @param params The {@link AccountLinesRequestParams} to send in the request.
*
* @return The {@link AccountLinesResult} containing the requested trust lines.
*
* @throws JsonRpcClientErrorException if {@code jsonRpcClient} throws an error.
*/
public AccountLinesResult accountLines(AccountLinesRequestParams params) throws JsonRpcClientErrorException {
Expand All @@ -692,6 +708,7 @@ public AccountLinesResult accountLines(AccountLinesRequestParams params) throws
* @param params The {@link ChannelVerifyRequestParams} to send in the request.
*
* @return The result of the request, as a {@link ChannelVerifyResult}.
*
* @throws JsonRpcClientErrorException if {@code jsonRpcClient} throws an error.
*/
public ChannelVerifyResult channelVerify(ChannelVerifyRequestParams params) throws JsonRpcClientErrorException {
Expand All @@ -710,6 +727,7 @@ public ChannelVerifyResult channelVerify(ChannelVerifyRequestParams params) thro
* @param params The {@link GatewayBalancesRequestParams} to send in the request.
*
* @return The result of the request, as a {@link GatewayBalancesResult}.
*
* @throws JsonRpcClientErrorException if {@code jsonRpcClient} throws an error.
*/
public GatewayBalancesResult gatewayBalances(
Expand All @@ -721,7 +739,7 @@ public GatewayBalancesResult gatewayBalances(
.build();
return jsonRpcClient.send(request, GatewayBalancesResult.class);
}

public JsonRpcClient getJsonRpcClient() {
return jsonRpcClient;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.xrpl.xrpl4j.crypto.signing.bc.BcSignatureService;
import org.xrpl.xrpl4j.model.client.FinalityStatus;
import org.xrpl.xrpl4j.model.client.XrplMethods;
import org.xrpl.xrpl4j.model.client.XrplRequestParams;
import org.xrpl.xrpl4j.model.client.XrplResult;
import org.xrpl.xrpl4j.model.client.accounts.AccountChannelsRequestParams;
import org.xrpl.xrpl4j.model.client.accounts.AccountChannelsResult;
Expand Down Expand Up @@ -80,8 +81,11 @@
import org.xrpl.xrpl4j.model.client.nft.NftBuyOffersResult;
import org.xrpl.xrpl4j.model.client.nft.NftSellOffersRequestParams;
import org.xrpl.xrpl4j.model.client.nft.NftSellOffersResult;
import org.xrpl.xrpl4j.model.client.path.BookOffersRequestParams;
import org.xrpl.xrpl4j.model.client.path.BookOffersResult;
import org.xrpl.xrpl4j.model.client.path.DepositAuthorizedRequestParams;
import org.xrpl.xrpl4j.model.client.path.DepositAuthorizedResult;
import org.xrpl.xrpl4j.model.client.path.ImmutableBookOffersRequestParams;
import org.xrpl.xrpl4j.model.client.path.PathCurrency;
import org.xrpl.xrpl4j.model.client.path.RipplePathFindRequestParams;
import org.xrpl.xrpl4j.model.client.path.RipplePathFindResult;
Expand All @@ -97,6 +101,7 @@
import org.xrpl.xrpl4j.model.client.transactions.TransactionResult;
import org.xrpl.xrpl4j.model.flags.AccountRootFlags;
import org.xrpl.xrpl4j.model.ledger.AccountRootObject;
import org.xrpl.xrpl4j.model.ledger.Issue;
import org.xrpl.xrpl4j.model.transactions.Address;
import org.xrpl.xrpl4j.model.transactions.Hash256;
import org.xrpl.xrpl4j.model.transactions.NfTokenId;
Expand Down Expand Up @@ -921,6 +926,33 @@ public void ripplePathFind() throws JsonRpcClientErrorException {
assertThat(jsonRpcRequestArgumentCaptor.getValue().params().get(0)).isEqualTo(ripplePathFindRequestParams);
}

@Test
void bookOffers() throws JsonRpcClientErrorException {
BookOffersRequestParams params = BookOffersRequestParams.builder()
.taker(Address.of("r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59"))
.takerGets(Issue.XRP)
.takerPays(
Issue.builder()
.issuer(Address.of("rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"))
.currency("USD")
.build()
)
.limit(UnsignedInteger.valueOf(10))
.ledgerSpecifier(LedgerSpecifier.CURRENT)
.build();

BookOffersResult resultMock = mock(BookOffersResult.class);
when(jsonRpcClientMock.send(
JsonRpcRequest.builder()
.method(XrplMethods.BOOK_OFFERS)
.addParams(params)
.build(),
BookOffersResult.class
)).thenReturn(resultMock);
BookOffersResult result = xrplClient.bookOffers(params);
assertThat(result).isEqualTo(resultMock);
}

@Test
public void accountLines() throws JsonRpcClientErrorException {
AccountLinesRequestParams accountLinesRequestParams = AccountLinesRequestParams.builder()
Expand Down
Loading

0 comments on commit 6e085fa

Please sign in to comment.