Skip to content

Commit

Permalink
Add support for parsing ContractCreditedEffect and ContractDebitedEff…
Browse files Browse the repository at this point in the history
…ect. (#535)
  • Loading branch information
overcat authored Sep 19, 2023
1 parent be49025 commit 1e11af9
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ As this project is pre 1.0, breaking changes may happen for minor version bumps.

## 0.41.0-beta.4
* Add support for Soroban Preview 11. ([#530](https://github.com/stellar/java-stellar-sdk/pull/530))
* New effects have been added to support Protocol 20 (Soroban) ([#535](https://github.com/stellar/java-stellar-sdk/pull/535)):
- `ContractCredited` occurs when a Stellar asset moves into its corresponding Stellar Asset Contract instance
- `ContractDebited` occurs when a Stellar asset moves out of its corresponding Stellar Asset Contract instance
* Bump dependencies & Remove unnecessary dependencies. ([#523](https://github.com/stellar/java-stellar-sdk/pull/523))
* No longer provide a shadow jar that has relocated third-party dependencies, but instead default to providing a thin jar.
We also offer an [uber jar](https://docs.gradle.org/current/userguide/working_with_files.html#sec:creating_uber_jar_example) and javadoc jar.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ public EffectResponse deserialize(
return gson.fromJson(json, LiquidityPoolRemovedEffectResponse.class);
case 95:
return gson.fromJson(json, LiquidityPoolRevokedEffectResponse.class);
case 96:
return gson.fromJson(json, ContractCreditedEffectResponse.class);
case 97:
return gson.fromJson(json, ContractDebitedEffectResponse.class);
default:
throw new RuntimeException("Invalid effect type");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.stellar.sdk.responses.effects;

import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* Represents contract_credited effect response.
*
* @see <a
* href="https://github.com/stellar/go/blob/d41faf8cd619718b9801a62254a513591f6cbc0a/protocols/horizon/effects/main.go#L316-L321"
* target="_blank">Effect definition</a>
* @see org.stellar.sdk.requests.EffectsRequestBuilder
* @see org.stellar.sdk.Server#effects()
*/
@AllArgsConstructor
@Getter
public class ContractCreditedEffectResponse extends EffectResponse {
@SerializedName("asset_type")
private final String assetType;

@SerializedName("asset_code")
private final String assetCode;

@SerializedName("asset_issuer")
private final String assetIssuer;

private final String contract;

private final String amount;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.stellar.sdk.responses.effects;

import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* Represents contract_debited effect response.
*
* @see <a
* href="https://github.com/stellar/go/blob/d41faf8cd619718b9801a62254a513591f6cbc0a/protocols/horizon/effects/main.go#L323-L328"
* target="_blank">Effect definition</a>
* @see org.stellar.sdk.requests.EffectsRequestBuilder
* @see org.stellar.sdk.Server#effects()
*/
@AllArgsConstructor
@Getter
public class ContractDebitedEffectResponse extends EffectResponse {
@SerializedName("asset_type")
private final String assetType;

@SerializedName("asset_code")
private final String assetCode;

@SerializedName("asset_issuer")
private final String assetIssuer;

private final String contract;

private final String amount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.stellar.sdk.responses.effects.AccountThresholdsUpdatedEffectResponse;
import org.stellar.sdk.responses.effects.ClaimableBalanceClaimantCreatedEffectResponse;
import org.stellar.sdk.responses.effects.ClaimableBalanceClawedBackEffectResponse;
import org.stellar.sdk.responses.effects.ContractCreditedEffectResponse;
import org.stellar.sdk.responses.effects.ContractDebitedEffectResponse;
import org.stellar.sdk.responses.effects.DataCreatedEffectResponse;
import org.stellar.sdk.responses.effects.DataRemovedEffectResponse;
import org.stellar.sdk.responses.effects.DataUpdatedEffectResponse;
Expand Down Expand Up @@ -1417,4 +1419,86 @@ public void testDeserializeLiquidityPoolRevokedEffect() {
});
assertEquals(effect.getSharesRevoked(), "24452.3233794");
}

@Test
public void testDeserializeContractCreditedEffect() {
String json =
"{\n"
+ " \"_links\": {\n"
+ " \"operation\": {\n"
+ " \"href\": \"http://100.83.15.43:8000/operations/21517786157057\"\n"
+ " },\n"
+ " \"succeeds\": {\n"
+ " \"href\": \"http://100.83.15.43:8000/effects?order=desc&cursor=21517786157057-2\"\n"
+ " },\n"
+ " \"precedes\": {\n"
+ " \"href\": \"http://100.83.15.43:8000/effects?order=asc&cursor=21517786157057-2\"\n"
+ " }\n"
+ " },\n"
+ " \"id\": \"0000021517786157057-0000000002\",\n"
+ " \"paging_token\": \"21517786157057-2\",\n"
+ " \"account\": \"GDAT5HWTGIU4TSSZ4752OUC4SABDLTLZFRPZUJ3D6LKBNEPA7V2CIG54\",\n"
+ " \"type\": \"contract_credited\",\n"
+ " \"type_i\": 96,\n"
+ " \"created_at\": \"2023-09-19T05:43:12Z\",\n"
+ " \"asset_type\": \"native\",\n"
+ " \"contract\": \"CDCYWK73YTYFJZZSJ5V7EDFNHYBG4QN3VUNG2IGD27KJDDPNCZKBCBXK\",\n"
+ " \"amount\": \"100.0000000\"\n"
+ "}\n";

ContractCreditedEffectResponse effect =
(ContractCreditedEffectResponse)
GsonSingleton.getInstance().fromJson(json, EffectResponse.class);

assertEquals(effect.getType(), "contract_credited");
assertEquals(effect.getId(), "0000021517786157057-0000000002");
assertEquals(effect.getPagingToken(), "21517786157057-2");
assertEquals(effect.getAccount(), "GDAT5HWTGIU4TSSZ4752OUC4SABDLTLZFRPZUJ3D6LKBNEPA7V2CIG54");
assertEquals(effect.getCreatedAt(), "2023-09-19T05:43:12Z");
assertEquals(effect.getAmount(), "100.0000000");
assertEquals(effect.getAssetType(), "native");
assertNull(effect.getAssetCode());
assertNull(effect.getAssetIssuer());
}

@Test
public void testDeserializeContractDebitedEffect() {
String json =
"{\n"
+ " \"_links\": {\n"
+ " \"operation\": {\n"
+ " \"href\": \"http://100.83.15.43:8000/operations/21517786157057\"\n"
+ " },\n"
+ " \"succeeds\": {\n"
+ " \"href\": \"http://100.83.15.43:8000/effects?order=desc&cursor=21517786157057-2\"\n"
+ " },\n"
+ " \"precedes\": {\n"
+ " \"href\": \"http://100.83.15.43:8000/effects?order=asc&cursor=21517786157057-2\"\n"
+ " }\n"
+ " },\n"
+ " \"id\": \"0000021517786157057-0000000002\",\n"
+ " \"paging_token\": \"21517786157057-2\",\n"
+ " \"account\": \"GDAT5HWTGIU4TSSZ4752OUC4SABDLTLZFRPZUJ3D6LKBNEPA7V2CIG54\",\n"
+ " \"type\": \"contract_debited\",\n"
+ " \"type_i\": 97,\n"
+ " \"created_at\": \"2023-09-19T05:43:12Z\",\n"
+ " \"asset_type\": \"native\",\n"
+ " \"contract\": \"CDCYWK73YTYFJZZSJ5V7EDFNHYBG4QN3VUNG2IGD27KJDDPNCZKBCBXK\",\n"
+ " \"amount\": \"100.0000000\"\n"
+ "}\n";

ContractDebitedEffectResponse effect =
(ContractDebitedEffectResponse)
GsonSingleton.getInstance().fromJson(json, EffectResponse.class);

assertEquals(effect.getType(), "contract_debited");
assertEquals(effect.getId(), "0000021517786157057-0000000002");
assertEquals(effect.getPagingToken(), "21517786157057-2");
assertEquals(effect.getAccount(), "GDAT5HWTGIU4TSSZ4752OUC4SABDLTLZFRPZUJ3D6LKBNEPA7V2CIG54");
assertEquals(effect.getCreatedAt(), "2023-09-19T05:43:12Z");
assertEquals(effect.getAmount(), "100.0000000");
assertEquals(effect.getAssetType(), "native");
assertNull(effect.getAssetCode());
assertNull(effect.getAssetIssuer());
}
}

0 comments on commit 1e11af9

Please sign in to comment.