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

LiquidityPoolIDDeserializer is missing from the PageDeserializer. #422

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions src/main/java/org/stellar/sdk/responses/PageDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.gson.reflect.TypeToken;

import org.stellar.sdk.Asset;
import org.stellar.sdk.LiquidityPoolID;
import org.stellar.sdk.Predicate;
import org.stellar.sdk.responses.effects.EffectResponse;
import org.stellar.sdk.responses.operations.OperationResponse;
Expand Down Expand Up @@ -45,6 +46,7 @@ public Page<E> deserialize(JsonElement json, Type typeOfT, JsonDeserializationCo
.registerTypeAdapter(EffectResponse.class, new EffectDeserializer())
.registerTypeAdapter(LiquidityPoolResponse.class, new LiquidityPoolDeserializer())
.registerTypeAdapter(TransactionResponse.class, new TransactionDeserializer())
.registerTypeAdapter(LiquidityPoolID.class, new LiquidityPoolIDDeserializer())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I see the same type adapter registration for LiquidityPoolID done up at the main gson instance used by all response handlers:

https://github.com/vinamogit/java-stellar-sdk/blob/fix-lp-id-deserialization-in-page/src/main/java/org/stellar/sdk/responses/GsonSingleton.java#L39

Do you know why this is not getting used by gson to resolve the LiquidityPoolID in TradeResponse during unmarshaling? It looks TradesRequestBuilder.execute uses the ResponseHandler which in turn uses that gson instance which includes the type registration. If re-registering here at page deser is needed, so be it, nice find!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know anything about Gson, my guess is that it's either because of generic type or because a new Gsonbuilder is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that there are several Gson instances, each need to know the deserializers.
We could avoid the double maintenance between GsonSingleton and PageDeserializer by using the JsonAdapter annotation on all the types which are not Page.

The only type that would need to be registered with both GsonBuilders will be the TransactionDeserializer since it uses a third instance of Gson to reconciliate the memo with its memo_type.

I published a branch for comparison:
vinamogit@fe9c9de

.create();

return gson.fromJson(newJson, pageType.getType());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.stellar.sdk.responses;

import org.junit.Test;
import org.stellar.sdk.LiquidityPoolID;

import com.google.common.base.Optional;
import com.google.gson.reflect.TypeToken;

import junit.framework.TestCase;

import org.junit.Test;

public class AccountsPageDeserializerTest extends TestCase {
@Test
public void testDeserialize() {
Expand All @@ -17,6 +19,15 @@ public void testDeserialize() {
assertEquals(accountsPage.getLinks().getPrev().getHref(), "/accounts?order=desc&limit=10&cursor=1");
assertEquals(accountsPage.getLinks().getSelf().getHref(), "/accounts?order=asc&limit=10&cursor=");
}

@Test
public void testDeserializeWithLiquidityPoolBalance() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great test coverage, do you think also worth adding one more check on regression aspect, i.e.:

assertEquals(accountsPage.getRecords().get(0).getBalances()[1].getLiquidityPoolID(), Optional.absent());

style-wise, good to remove the extra lines.

Page<AccountResponse> accountsPage = GsonSingleton.getInstance().fromJson(jsonLiquidityPool, new TypeToken<Page<AccountResponse>>() {}.getType());

assertEquals(accountsPage.getRecords().get(0).getAccountId(), "GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST");
assertEquals(accountsPage.getRecords().get(0).getBalances()[0].getLiquidityPoolID(), Optional.of(new LiquidityPoolID("a468d41d8e9b8f3c7209651608b74b7db7ac9952dcae0cdf24871d1d9c7b0088")));
assertEquals(accountsPage.getRecords().get(0).getBalances()[1].getLiquidityPoolID(), Optional.absent());
}

String json = "{\n" +
" \"_embedded\": {\n" +
Expand Down Expand Up @@ -85,4 +96,116 @@ public void testDeserialize() {
" }\n" +
" }\n" +
"}";

String jsonLiquidityPool = "{\n" +
" \"_links\": {\n" +
" \"self\": {\n" +
" \"href\": \"https://horizon.publicnode.org/accounts/?asset=USDC%3AGA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN&cursor=&limit=1&order=desc\"\n" +
" },\n" +
" \"next\": {\n" +
" \"href\": \"https://horizon.publicnode.org/accounts/?asset=USDC%3AGA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN&cursor=GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST&limit=1&order=desc\"\n" +
" },\n" +
" \"prev\": {\n" +
" \"href\": \"https://horizon.publicnode.org/accounts/?asset=USDC%3AGA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN&cursor=GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST&limit=1&order=asc\"\n" +
" }\n" +
" },\n" +
" \"_embedded\": {\n" +
" \"records\": [\n" +
" {\n" +
" \"_links\": {\n" +
" \"self\": {\n" +
" \"href\": \"https://horizon.publicnode.org/accounts/GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST\"\n" +
" },\n" +
" \"transactions\": {\n" +
" \"href\": \"https://horizon.publicnode.org/accounts/GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST/transactions{?cursor,limit,order}\",\n" +
" \"templated\": true\n" +
" },\n" +
" \"operations\": {\n" +
" \"href\": \"https://horizon.publicnode.org/accounts/GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST/operations{?cursor,limit,order}\",\n" +
" \"templated\": true\n" +
" },\n" +
" \"payments\": {\n" +
" \"href\": \"https://horizon.publicnode.org/accounts/GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST/payments{?cursor,limit,order}\",\n" +
" \"templated\": true\n" +
" },\n" +
" \"effects\": {\n" +
" \"href\": \"https://horizon.publicnode.org/accounts/GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST/effects{?cursor,limit,order}\",\n" +
" \"templated\": true\n" +
" },\n" +
" \"offers\": {\n" +
" \"href\": \"https://horizon.publicnode.org/accounts/GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST/offers{?cursor,limit,order}\",\n" +
" \"templated\": true\n" +
" },\n" +
" \"trades\": {\n" +
" \"href\": \"https://horizon.publicnode.org/accounts/GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST/trades{?cursor,limit,order}\",\n" +
" \"templated\": true\n" +
" },\n" +
" \"data\": {\n" +
" \"href\": \"https://horizon.publicnode.org/accounts/GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST/data/{key}\",\n" +
" \"templated\": true\n" +
" }\n" +
" },\n" +
" \"id\": \"GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST\",\n" +
" \"account_id\": \"GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST\",\n" +
" \"sequence\": \"166506652181200896\",\n" +
" \"subentry_count\": 5,\n" +
" \"last_modified_ledger\": 38767851,\n" +
" \"last_modified_time\": \"2021-12-17T20:22:00Z\",\n" +
" \"thresholds\": {\n" +
" \"low_threshold\": 20,\n" +
" \"med_threshold\": 20,\n" +
" \"high_threshold\": 20\n" +
" },\n" +
" \"flags\": {\n" +
" \"auth_required\": false,\n" +
" \"auth_revocable\": false,\n" +
" \"auth_immutable\": false,\n" +
" \"auth_clawback_enabled\": false\n" +
" },\n" +
" \"balances\": [\n" +
" {\n" +
" \"balance\": \"974462.6017340\",\n" +
" \"liquidity_pool_id\": \"a468d41d8e9b8f3c7209651608b74b7db7ac9952dcae0cdf24871d1d9c7b0088\",\n" +
" \"limit\": \"922337203685.4775807\",\n" +
" \"last_modified_ledger\": 38809512,\n" +
" \"is_authorized\": false,\n" +
" \"is_authorized_to_maintain_liabilities\": false,\n" +
" \"asset_type\": \"liquidity_pool_shares\"\n" +
" },\n" +
" {\n" +
" \"balance\": \"0.0000000\",\n" +
" \"limit\": \"922337203685.4775807\",\n" +
" \"buying_liabilities\": \"0.0000000\",\n" +
" \"selling_liabilities\": \"0.0000000\",\n" +
" \"sponsor\": \"GCZGSFPITKVJPJERJIVLCQK5YIHYTDXCY45ZHU3IRPBP53SXSCALTEST\",\n" +
" \"last_modified_ledger\": 38767851,\n" +
" \"is_authorized\": true,\n" +
" \"is_authorized_to_maintain_liabilities\": true,\n" +
" \"asset_type\": \"credit_alphanum4\",\n" +
" \"asset_code\": \"USDC\",\n" +
" \"asset_issuer\": \"GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN\"\n" +
" },\n" +
" {\n" +
" \"balance\": \"0.0000000\",\n" +
" \"buying_liabilities\": \"0.0000000\",\n" +
" \"selling_liabilities\": \"0.0000000\",\n" +
" \"asset_type\": \"native\"\n" +
" }\n" +
" ],\n" +
" \"signers\": [\n" +
" {\n" +
" \"weight\": 10,\n" +
" \"key\": \"GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST\",\n" +
" \"type\": \"ed25519_public_key\"\n" +
" }\n" +
" ],\n" +
" \"data\": {},\n" +
" \"num_sponsoring\": 0,\n" +
" \"num_sponsored\": 7,\n" +
" \"sponsor\": \"GCZGSFPITKVJPJERJIVLCQK5YIHYTDXCY45ZHU3IRPBP53SXSCALTEST\",\n" +
" \"paging_token\": \"GDZZYLIIJ24HWAVWAQ2PJVNKHUJLJVVLFY2SSLYINBHDY5KZTLPTEST\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import junit.framework.TestCase;
import org.junit.Test;
import org.stellar.sdk.AssetTypeNative;
import org.stellar.sdk.LiquidityPoolID;

import static java.lang.Long.valueOf;
import static org.stellar.sdk.Asset.create;
Expand All @@ -28,9 +29,21 @@ public void testDeserialize() {
assertEquals(tradesPage.getRecords().get(0).getCounterAsset(), create(null,"JPY", "GBVAOIACNSB7OVUXJYC5UE2D4YK2F7A24T7EE5YOMN4CE6GCHUTOUQXM"));
assertEquals(tradesPage.getRecords().get(0).getPrice().getNumerator(), valueOf(267));
assertEquals(tradesPage.getRecords().get(0).getPrice().getDenominator(), valueOf(1000));
assertEquals(tradesPage.getRecords().get(0).getCounterLiquidityPoolID(), Optional.absent());
assertEquals(tradesPage.getRecords().get(0).getBaseLiquidityPoolID(), Optional.absent());

assertEquals(tradesPage.getRecords().get(1).getBaseAccount(), Optional.of("GAVH5JM5OKXGMQDS7YPRJ4MQCPXJUGH26LYQPQJ4SOMOJ4SXY472ZM7G"));
}
@Test
public void testDeserializeLiquidityPool() {
Page<TradeResponse> tradesPage = GsonSingleton.getInstance().fromJson(jsonLiquidityPool, new TypeToken<Page<TradeResponse>>() {}.getType());


assertEquals(tradesPage.getRecords().get(0).getBaseLiquidityPoolID(), Optional.of(new LiquidityPoolID("a468d41d8e9b8f3c7209651608b74b7db7ac9952dcae0cdf24871d1d9c7bbase")));
assertEquals(tradesPage.getRecords().get(0).getCounterLiquidityPoolID(), Optional.absent());
assertEquals(tradesPage.getRecords().get(1).getBaseLiquidityPoolID(), Optional.absent());
assertEquals(tradesPage.getRecords().get(1).getCounterLiquidityPoolID(), Optional.of(new LiquidityPoolID("a468d41d8e9b8f3c7209651608b74b7db7ac9952dcae0cdf24871d1d9c7b0088")));
}

String json = "{\n" +
" \"_links\": {\n" +
Expand Down Expand Up @@ -381,4 +394,97 @@ public void testDeserialize() {
" ]\n" +
" }\n" +
"}";


String jsonLiquidityPool =
"{\n" +
" \"_links\": {\n" +
" \"self\": {\n" +
" \"href\": \"https://horizon.stellar.org/liquidity_pools/a468d41d8e9b8f3c7209651608b74b7db7ac9952dcae0cdf24871d1d9c7b0088/trades?cursor=&limit=10&order=asc\"\n" +
" },\n" +
" \"next\": {\n" +
" \"href\": \"https://horizon.stellar.org/liquidity_pools/a468d41d8e9b8f3c7209651608b74b7db7ac9952dcae0cdf24871d1d9c7b0088/trades?cursor=163706513893126145-0&limit=10&order=asc\"\n" +
" },\n" +
" \"prev\": {\n" +
" \"href\": \"https://horizon.stellar.org/liquidity_pools/a468d41d8e9b8f3c7209651608b74b7db7ac9952dcae0cdf24871d1d9c7b0088/trades?cursor=163706170297630721-3&limit=10&order=desc\"\n" +
" }\n" +
" },\n" +
" \"_embedded\": {\n" +
" \"records\": [\n" +
" {\n" +
" \"_links\": {\n" +
" \"self\": {\n" +
" \"href\": \"\"\n" +
" },\n" +
" \"base\": {\n" +
" \"href\": \"https://horizon.stellar.org/liquidity_pools/a468d41d8e9b8f3c7209651608b74b7db7ac9952dcae0cdf24871d1d9c7b0088\"\n" +
" },\n" +
" \"counter\": {\n" +
" \"href\": \"https://horizon.stellar.org/accounts/GAVYIUWQFQX2GJ7FWNGVXSSLMJO75V5XY5M2YON4DKYD24LPB6QLCERP\"\n" +
" },\n" +
" \"operation\": {\n" +
" \"href\": \"https://horizon.stellar.org/operations/163706170297630721\"\n" +
" }\n" +
" },\n" +
" \"id\": \"163706170297630721-3\",\n" +
" \"paging_token\": \"163706170297630721-3\",\n" +
" \"ledger_close_time\": \"2021-11-03T15:00:52Z\",\n" +
" \"trade_type\": \"liquidity_pool\",\n" +
" \"liquidity_pool_fee_bp\": 30,\n" +
" \"base_liquidity_pool_id\": \"a468d41d8e9b8f3c7209651608b74b7db7ac9952dcae0cdf24871d1d9c7bbase\",\n" +
" \"base_amount\": \"0.0010000\",\n" +
" \"base_asset_type\": \"native\",\n" +
" \"counter_offer_id\": \"4775392188725018625\",\n" +
" \"counter_account\": \"GAVH5JM5OKXGMQDS7YPRJ4MQCPXJUGH26LYQPQJ4SOMOJ4SXY472ZM7G\",\n" +
" \"counter_amount\": \"0.0003679\",\n" +
" \"counter_asset_type\": \"credit_alphanum4\",\n" +
" \"counter_asset_code\": \"USDC\",\n" +
" \"counter_asset_issuer\": \"GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN\",\n" +
" \"base_is_seller\": true,\n" +
" \"price\": {\n" +
" \"n\": \"3679\",\n" +
" \"d\": \"10000\"\n" +
" }\n" +
" },\n" +

" {\n" +
" \"_links\": {\n" +
" \"self\": {\n" +
" \"href\": \"\"\n" +
" },\n" +
" \"base\": {\n" +
" \"href\": \"https://horizon.stellar.org/accounts/GA7BRV2K3OM27NLVY2IJQGYZEQ7AEZSPR3Y3XA6COHPMOPOEUHQBDYBC\"\n" +
" },\n" +
" \"counter\": {\n" +
" \"href\": \"https://horizon.stellar.org/liquidity_pools/a468d41d8e9b8f3c7209651608b74b7db7ac9952dcae0cdf24871d1d9c7b0088\"\n" +
" },\n" +
" \"operation\": {\n" +
" \"href\": \"https://horizon.stellar.org/operations/163706436584558593\"\n" +
" }\n" +
" },\n" +
" \"id\": \"163706436584558593-1\",\n" +
" \"paging_token\": \"163706436584558593-1\",\n" +
" \"ledger_close_time\": \"2021-11-03T15:07:00Z\",\n" +
" \"trade_type\": \"liquidity_pool\",\n" +
" \"liquidity_pool_fee_bp\": 30,\n" +
" \"base_offer_id\": \"4775392455011946497\",\n" +
" \"base_account\": \"GAVH5JM5OKXGMQDS7YPRJ4MQCPXJUGH26LYQPQJ4SOMOJ4SXY472ZM7G\",\n" +
" \"base_amount\": \"0.0002207\",\n" +
" \"base_asset_type\": \"native\",\n" +
" \"counter_liquidity_pool_id\": \"a468d41d8e9b8f3c7209651608b74b7db7ac9952dcae0cdf24871d1d9c7b0088\",\n" +
" \"counter_amount\": \"0.0000831\",\n" +
" \"counter_asset_type\": \"credit_alphanum4\",\n" +
" \"counter_asset_code\": \"USDC\",\n" +
" \"counter_asset_issuer\": \"GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN\",\n" +
" \"base_is_seller\": false,\n" +
" \"price\": {\n" +
" \"n\": \"831\",\n" +
" \"d\": \"2207\"\n" +
" }\n" +
" },\n" +
" ]\n" +
" }\n" +
"}\n" +
"\n";

}