Skip to content

Commit

Permalink
Merge pull request #339 from paulbellamy/assets-updates
Browse files Browse the repository at this point in the history
Updates for /assets and /claimable_balances/*/(operations|transactions) endpoints
  • Loading branch information
Paul Bellamy authored Apr 26, 2021
2 parents ce7de4f + 04a113a commit 6b7f6df
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 51 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

As this project is pre 1.0, breaking changes may happen for minor version bumps. A breaking change will get clearly notified in this log.

## Unreleased

* Added TransactionsRequestBuilder.forClaimableBalance(), and OperationsRequestBuilder.forClaimableBalance().
* Added support for new `accounts`, `balances`, `claimable_balances_amount`, and `num_claimable_balances` fields on Assets.

## 0.24.0

### Deprecations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ public OperationsRequestBuilder forAccount(String account) {
return this;
}

/**
* Builds request to <code>GET /claimable_balances/{claimable_balance_id}/operations</code>
* @see <a href="https://www.stellar.org/developers/horizon/reference/operations-for-claimable-balance.html">Operations for ClaimableBalance</a>
* @param claimableBalance Claimable Balance for which to get operations
*/
public OperationsRequestBuilder forClaimableBalance(String claimableBalance) {
claimableBalance = checkNotNull(claimableBalance, "claimableBalance cannot be null");
this.setSegments("claimable_balances", claimableBalance, "operations");
return this;
}

/**
* Builds request to <code>GET /ledgers/{ledgerSeq}/operations</code>
* @see <a href="https://www.stellar.org/developers/horizon/reference/operations-for-ledger.html">Operations for Ledger</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public TransactionsRequestBuilder forAccount(String account) {
return this;
}

/**
* Builds request to <code>GET /claimable_balances/{claimable_balance_id}/transactions</code>
* @see <a href="https://www.stellar.org/developers/horizon/reference/transactions-for-claimable-balance.html">Transactions for ClaimableBalance</a>
* @param claimableBalance Claimable Balance for which to get transactions
*/
public TransactionsRequestBuilder forClaimableBalance(String claimableBalance) {
claimableBalance = checkNotNull(claimableBalance, "claimableBalance cannot be null");
this.setSegments("claimable_balances", claimableBalance, "transactions");
return this;
}

/**
* Builds request to <code>GET /ledgers/{ledgerSeq}/transactions</code>
* @see <a href="https://www.stellar.org/developers/horizon/reference/transactions-for-ledger.html">Transactions for Ledger</a>
Expand Down
90 changes: 89 additions & 1 deletion src/main/java/org/stellar/sdk/responses/AssetResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,34 @@ public class AssetResponse extends Response implements Pageable {
private final String assetIssuer;
@SerializedName("paging_token")
private final String pagingToken;
@SerializedName("accounts")
private final AssetResponse.Accounts accounts;
@SerializedName("balances")
private final AssetResponse.Balances balances;
@SerializedName("amount")
private final String amount;
@SerializedName("claimable_balances_amount")
private final String claimableBalancesAmount;
@SerializedName("num_accounts")
private final int numAccounts;
@SerializedName("num_claimable_balances")
private final int numClaimableBalances;
@SerializedName("flags")
private final AssetResponse.Flags flags;
@SerializedName("_links")
private final AssetResponse.Links links;

public AssetResponse(String assetType, String assetCode, String assetIssuer, String pagingToken, String amount, int numAccounts, Flags flags, Links links) {
public AssetResponse(String assetType, String assetCode, String assetIssuer, String pagingToken, Accounts accounts, Balances balances, String amount, String claimableBalancesAmount, int numAccounts, int numClaimableBalances, Flags flags, Links links) {
this.assetType = assetType;
this.assetCode = assetCode;
this.assetIssuer = assetIssuer;
this.pagingToken = pagingToken;
this.accounts = accounts;
this.balances = balances;
this.amount = amount;
this.claimableBalancesAmount = claimableBalancesAmount;
this.numAccounts = numAccounts;
this.numClaimableBalances = numClaimableBalances;
this.flags = flags;
this.links = links;
}
Expand All @@ -52,6 +64,22 @@ public String getPagingToken() {
return pagingToken;
}

public Accounts getAccounts() {
return accounts;
}

public Balances getBalances() {
return balances;
}

public String getClaimableBalancesAmount() {
return claimableBalancesAmount;
}

public int getNumClaimableBalances() {
return numClaimableBalances;
}

public String getAmount() {
return amount;
}
Expand All @@ -68,6 +96,66 @@ public Links getLinks() {
return links;
}

/**
* Accounts describe asset accounts.
*/
public static class Accounts {
@SerializedName("authorized")
private final int authorized;
@SerializedName("authorized_to_maintain_liabilities")
private final int authorizedToMaintainLiabilities;
@SerializedName("unauthorized")
private final int unauthorized;

public Accounts(int authorized, int authorizedToMaintainLiabilities, int unauthorized) {
this.authorized = authorized;
this.authorizedToMaintainLiabilities = authorizedToMaintainLiabilities;
this.unauthorized = unauthorized;
}

public int authorized() {
return authorized;
}

public int authorizedToMaintainLiabilities() {
return authorizedToMaintainLiabilities;
}

public int unauthorized() {
return unauthorized;
}
}

/**
* Balances describe asset balances.
*/
public static class Balances {
@SerializedName("authorized")
private final String authorized;
@SerializedName("authorized_to_maintain_liabilities")
private final String authorizedToMaintainLiabilities;
@SerializedName("unauthorized")
private final String unauthorized;

public Balances(String authorized, String authorizedToMaintainLiabilities, String unauthorized) {
this.authorized = authorized;
this.authorizedToMaintainLiabilities = authorizedToMaintainLiabilities;
this.unauthorized = unauthorized;
}

public String authorized() {
return authorized;
}

public String authorizedToMaintainLiabilities() {
return authorizedToMaintainLiabilities;
}

public String unauthorized() {
return unauthorized;
}
}

/**
* Flags describe asset flags.
*/
Expand Down
112 changes: 62 additions & 50 deletions src/test/java/org/stellar/sdk/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,52 +113,54 @@ public class ServerTest {
" \"instance\": \"d3465740-ec3a-4a0b-9d4a-c9ea734ce58a\"\n" +
"}";

private final String operationsPageResponse = "{\n" +
" \"_links\": {\n" +
" \"self\": {\n" +
" \"href\": \"http://horizon-testnet.stellar.org/operations?order=desc\\u0026limit=10\\u0026cursor=\"\n" +
" },\n" +
" \"next\": {\n" +
" \"href\": \"http://horizon-testnet.stellar.org/operations?order=desc\\u0026limit=10\\u0026cursor=3695540185337857\"\n" +
" },\n" +
" \"prev\": {\n" +
" \"href\": \"http://horizon-testnet.stellar.org/operations?order=asc\\u0026limit=10\\u0026cursor=3717508943056897\"\n" +
" }\n" +
" },\n" +
" \"_embedded\": {\n" +
" \"records\": [\n" +
" {\n" +
" \"_links\": {\n" +
" \"self\": {\n" +
" \"href\": \"http://horizon-testnet.stellar.org/operations/3717508943056897\"\n" +
" },\n" +
" \"transaction\": {\n" +
" \"href\": \"http://horizon-testnet.stellar.org/transactions/ce81d957352501a46d9b938462cbef76283dcba8108d2649e0d79279a8f36488\"\n" +
" },\n" +
" \"effects\": {\n" +
" \"href\": \"http://horizon-testnet.stellar.org/operations/3717508943056897/effects\"\n" +
" },\n" +
" \"succeeds\": {\n" +
" \"href\": \"http://horizon-testnet.stellar.org/effects?order=desc\\u0026cursor=3717508943056897\"\n" +
" },\n" +
" \"precedes\": {\n" +
" \"href\": \"http://horizon-testnet.stellar.org/effects?order=asc\\u0026cursor=3717508943056897\"\n" +
" }\n" +
" },\n" +
" \"id\": \"3717508943056897\",\n" +
" \"paging_token\": \"3717508943056897\",\n" +
" \"source_account\": \"GBS43BF24ENNS3KPACUZVKK2VYPOZVBQO2CISGZ777RYGOPYC2FT6S3K\",\n" +
" \"type\": \"create_account\",\n" +
" \"type_i\": 0,\n" +
" \"created_at\": \"2018-01-22T21:30:53Z\",\n" +
" \"transaction_hash\": \"dd9d10c80a344f4464df3ecaa63705a5ef4a0533ff2f2099d5ef371ab5e1c046\","+
" \"starting_balance\": \"10000.0\",\n" +
" \"funder\": \"GBS43BF24ENNS3KPACUZVKK2VYPOZVBQO2CISGZ777RYGOPYC2FT6S3K\",\n" +
" \"account\": \"GDFH4NIYMIIAKRVEJJZOIGWKXGQUF3XHJG6ZM6CEA64AMTVDN44LHOQE\"\n" +
" }\n"+
" ]\n" +
" }\n" +
"}";
private String operationsPageResponse(String baseUrl) {
return "{\n" +
" \"_links\": {\n" +
" \"self\": {\n" +
" \"href\": \""+baseUrl+"/operations?order=desc\\u0026limit=10\\u0026cursor=\"\n" +
" },\n" +
" \"next\": {\n" +
" \"href\": \""+baseUrl+"/operations?order=desc\\u0026limit=10\\u0026cursor=3695540185337857\"\n" +
" },\n" +
" \"prev\": {\n" +
" \"href\": \""+baseUrl+"/operations?order=asc\\u0026limit=10\\u0026cursor=3717508943056897\"\n" +
" }\n" +
" },\n" +
" \"_embedded\": {\n" +
" \"records\": [\n" +
" {\n" +
" \"_links\": {\n" +
" \"self\": {\n" +
" \"href\": \""+baseUrl+"/operations/3717508943056897\"\n" +
" },\n" +
" \"transaction\": {\n" +
" \"href\": \""+baseUrl+"/transactions/ce81d957352501a46d9b938462cbef76283dcba8108d2649e0d79279a8f36488\"\n" +
" },\n" +
" \"effects\": {\n" +
" \"href\": \""+baseUrl+"/operations/3717508943056897/effects\"\n" +
" },\n" +
" \"succeeds\": {\n" +
" \"href\": \""+baseUrl+"/effects?order=desc\\u0026cursor=3717508943056897\"\n" +
" },\n" +
" \"precedes\": {\n" +
" \"href\": \""+baseUrl+"/effects?order=asc\\u0026cursor=3717508943056897\"\n" +
" }\n" +
" },\n" +
" \"id\": \"3717508943056897\",\n" +
" \"paging_token\": \"3717508943056897\",\n" +
" \"source_account\": \"GBS43BF24ENNS3KPACUZVKK2VYPOZVBQO2CISGZ777RYGOPYC2FT6S3K\",\n" +
" \"type\": \"create_account\",\n" +
" \"type_i\": 0,\n" +
" \"created_at\": \"2018-01-22T21:30:53Z\",\n" +
" \"transaction_hash\": \"dd9d10c80a344f4464df3ecaa63705a5ef4a0533ff2f2099d5ef371ab5e1c046\","+
" \"starting_balance\": \"10000.0\",\n" +
" \"funder\": \"GBS43BF24ENNS3KPACUZVKK2VYPOZVBQO2CISGZ777RYGOPYC2FT6S3K\",\n" +
" \"account\": \"GDFH4NIYMIIAKRVEJJZOIGWKXGQUF3XHJG6ZM6CEA64AMTVDN44LHOQE\"\n" +
" }\n"+
" ]\n" +
" }\n" +
"}";
}

Transaction buildTransaction() throws IOException {
return buildTransaction(Network.PUBLIC);
Expand Down Expand Up @@ -308,9 +310,19 @@ public void testSubmitTransactionInternalError() throws IOException {

@Test
public void testNextPage() throws IOException, URISyntaxException {
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(operationsPageResponse));
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(operationsPageResponse));
final MockWebServer mockWebServer = new MockWebServer();
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
String baseUrl = request
.getRequestUrl()
.toString()
.replaceAll(request.getPath(), "");
String body = operationsPageResponse(baseUrl);
return new MockResponse().setResponseCode(200).setBody(body);
}
});

mockWebServer.start();
HttpUrl baseUrl = mockWebServer.url("");
Server server = new Server(baseUrl.toString());
Expand All @@ -319,7 +331,7 @@ public void testNextPage() throws IOException, URISyntaxException {
assertEquals(1, page.getRecords().size());
assertEquals("dd9d10c80a344f4464df3ecaa63705a5ef4a0533ff2f2099d5ef371ab5e1c046", page.getRecords().get(0).getTransactionHash());
Page<OperationResponse> nextPage = page.getNextPage(server.getHttpClient());
assertEquals(1, page.getRecords().size());
assertEquals(1, nextPage.getRecords().size());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ public void testForAccount() {
assertEquals("https://horizon-testnet.stellar.org/accounts/GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H/operations?limit=200&order=desc", uri.toString());
}

@Test
public void testForClaimableBalance() {
Server server = new Server("https://horizon-testnet.stellar.org");
HttpUrl uri = server.operations()
.forClaimableBalance("00000000846c047755e4a46912336f56096b48ece78ddb5fbf6d90f0eb4ecae5324fbddb")
.limit(200)
.order(RequestBuilder.Order.DESC)
.buildUri();
assertEquals("https://horizon-testnet.stellar.org/claimable_balances/00000000846c047755e4a46912336f56096b48ece78ddb5fbf6d90f0eb4ecae5324fbddb/operations?limit=200&order=desc", uri.toString());
}

@Test
public void testForLedger() {
Server server = new Server("https://horizon-testnet.stellar.org");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public void testForAccount() {
assertEquals("https://horizon-testnet.stellar.org/accounts/GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H/transactions?limit=200&order=desc", uri.toString());
}

@Test
public void testForClaimableBalance() {
Server server = new Server("https://horizon-testnet.stellar.org");
HttpUrl uri = server.transactions()
.forClaimableBalance("00000000846c047755e4a46912336f56096b48ece78ddb5fbf6d90f0eb4ecae5324fbddb")
.limit(200)
.order(RequestBuilder.Order.DESC)
.buildUri();
assertEquals("https://horizon-testnet.stellar.org/claimable_balances/00000000846c047755e4a46912336f56096b48ece78ddb5fbf6d90f0eb4ecae5324fbddb/transactions?limit=200&order=desc", uri.toString());
}

@Test
public void testForLedger() {
Server server = new Server("https://horizon-testnet.stellar.org");
Expand Down
Loading

0 comments on commit 6b7f6df

Please sign in to comment.