Skip to content

Commit

Permalink
Optimize the way of parsing memo in TransactionResponse.
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Mar 30, 2024
1 parent bc6a69d commit d50d128
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 135 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ As this project is pre 1.0, breaking changes may happen for minor version bumps.

## Pending
### Update
* Update `org.stellar.sdk.responses`, add missing fields. ([#570](https://github.com/stellar/java-stellar-sdk/pull/570))
* Add `Asset.getContractId()` for calculating the id of the asset contract. ([#574](https://github.com/stellar/java-stellar-sdk/pull/574))
* Update `org.stellar.sdk.responses`, add missing fields. ([#570](https://github.com/lightsail-network/java-stellar-sdk/pull/570))
* Add `Asset.getContractId()` for calculating the id of the asset contract. ([#574](https://github.com/lightsail-network/java-stellar-sdk/pull/574))

### Breaking changes
* `TransactionResponse.getMemo()` no longer returns the memo of `innerTransaction`. ([#582](https://github.com/lightsail-network/java-stellar-sdk/pull/582))

## 0.43.0
### Update
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/stellar/sdk/responses/GsonSingleton.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public static Gson getInstance() {
.registerTypeAdapter(EffectResponse.class, new EffectDeserializer())
.registerTypeAdapter(LiquidityPoolID.class, new LiquidityPoolIDDeserializer())
.registerTypeAdapter(LiquidityPoolType.class, new LiquidityPoolTypeDeserializer())
.registerTypeAdapter(TransactionResponse.class, new TransactionDeserializer())
.registerTypeAdapter(
accountPageType.getType(), new PageDeserializer<>(accountPageType))
.registerTypeAdapter(assetPageType.getType(), new PageDeserializer<>(assetPageType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public OperationResponse deserialize(
new GsonBuilder()
.registerTypeAdapter(Asset.class, new AssetDeserializer())
.registerTypeAdapter(Predicate.class, new PredicateDeserializer())
.registerTypeAdapter(TransactionResponse.class, new TransactionDeserializer())
.registerTypeAdapter(LiquidityPoolID.class, new LiquidityPoolIDDeserializer())
.registerTypeAdapter(LiquidityPoolType.class, new LiquidityPoolTypeDeserializer())
.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public Page<E> deserialize(JsonElement json, Type typeOfT, JsonDeserializationCo
.registerTypeAdapter(OperationResponse.class, new OperationDeserializer())
.registerTypeAdapter(EffectResponse.class, new EffectDeserializer())
.registerTypeAdapter(LiquidityPoolResponse.class, new LiquidityPoolDeserializer())
.registerTypeAdapter(TransactionResponse.class, new TransactionDeserializer())
.registerTypeAdapter(LiquidityPoolID.class, new LiquidityPoolIDDeserializer())
.create();

Expand Down

This file was deleted.

65 changes: 52 additions & 13 deletions src/main/java/org/stellar/sdk/responses/TransactionResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.Value;
import org.stellar.sdk.Base64Factory;
import org.stellar.sdk.Memo;

/**
Expand All @@ -19,89 +19,135 @@
* @see org.stellar.sdk.requests.TransactionsRequestBuilder
* @see org.stellar.sdk.Server#transactions()
*/
@Getter
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class TransactionResponse extends Response implements Pageable {
@Getter
@SerializedName("id")
private final String id;

@Getter
@SerializedName("paging_token")
private final String pagingToken;

@Getter
@SerializedName("successful")
private final Boolean successful;

@Getter
@SerializedName("hash")
private final String hash;

@Getter
@SerializedName("ledger")
private final Long ledger;

@Getter
@SerializedName("created_at")
private final String createdAt;

@Getter
@SerializedName("source_account")
private final String sourceAccount;

@Getter
@SerializedName("account_muxed")
private final String accountMuxed;

@Getter
@SerializedName("account_muxed_id")
private final BigInteger accountMuxedId;

@Getter
@SerializedName("source_account_sequence")
private final Long sourceAccountSequence;

@Getter
@SerializedName("fee_account")
private final String feeAccount;

@SerializedName("fee_account_muxed")
private final String feeAccountMuxed;

@Getter
@SerializedName("fee_account_muxed_id")
private final BigInteger feeAccountMuxedId;

@Getter
@SerializedName("fee_charged")
private final Long feeCharged;

@Getter
@SerializedName("max_fee")
private final Long maxFee;

@Getter
@SerializedName("operation_count")
private final Integer operationCount;

@Getter
@SerializedName("envelope_xdr")
private final String envelopeXdr;

@Getter
@SerializedName("result_xdr")
private final String resultXdr;

@Getter
@SerializedName("result_meta_xdr")
private final String resultMetaXdr;

@Getter
@SerializedName("fee_meta_xdr")
private final String feeMetaXdr;

// GSON won't serialize `transient` variables automatically. We need this behaviour
// because Memo is an abstract class and GSON tries to instantiate it.
private transient Memo memo;

@Getter
@SerializedName("signatures")
private final List<String> signatures;

@SerializedName("preconditions")
private final Preconditions preconditions;

@Getter
@SerializedName("fee_bump_transaction")
private final FeeBumpTransaction feeBumpTransaction;

@Getter
@SerializedName("inner_transaction")
private final InnerTransaction innerTransaction;

@SerializedName("memo_type")
private final String memoType;

@SerializedName("memo_bytes")
private final String memoBytes;

@SerializedName("memo")
private final String memoValue;

@Getter
@SerializedName("_links")
private final Links links;

/**
* @return {@link Memo} object from this transaction.
*/
public Memo getMemo() {
if ("none".equals(memoType)) {
return Memo.none();
} else if ("text".equals(memoType)) {
return Memo.text(Base64Factory.getInstance().decode(memoBytes));
} else if ("id".equals(memoType)) {
return Memo.id(new BigInteger(memoValue));
} else if ("hash".equals(memoType)) {
return Memo.hash(Base64Factory.getInstance().decode(memoValue));
} else if ("return".equals(memoType)) {
return Memo.returnHash(Base64Factory.getInstance().decode(memoValue));
} else {
throw new IllegalArgumentException("Invalid memo type: " + memoType);
}
}

public Optional<MuxedAccount> getSourceAccountMuxed() {
if (this.accountMuxed == null || this.accountMuxed.isEmpty()) {
return Optional.empty();
Expand Down Expand Up @@ -134,13 +180,6 @@ public Boolean isSuccessful() {
return successful;
}

public void setMemo(@NonNull Memo memo) {
if (this.memo != null) {
throw new RuntimeException("Memo has been already set.");
}
this.memo = memo;
}

/**
* Preconditions of a transaction per <a
* href="https://github.com/stellar/stellar-protocol/blob/master/core/cap-0021.md#specification">CAP-21</a>
Expand Down
15 changes: 0 additions & 15 deletions src/test/java/org/stellar/sdk/MemoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.math.BigInteger;
import java.util.Arrays;
import org.junit.Test;
import org.stellar.sdk.responses.TransactionDeserializer;
import org.stellar.sdk.responses.TransactionResponse;
import org.stellar.sdk.xdr.MemoType;

public class MemoTest {
Expand Down Expand Up @@ -68,17 +64,6 @@ public void testMemoId() {
assertEquals("9223372036854775807", memo.toString());
}

@Test
public void testParseMemoId() {
String maxId = "18446744073709551615";
JsonElement element =
JsonParser.parseString(String.format("{ \"memo_type\": \"id\", \"memo\": \"%s\" }", maxId));
TransactionResponse transactionResponse =
new TransactionDeserializer().deserialize(element, null, null);
MemoId memoId = (MemoId) transactionResponse.getMemo();
assertEquals(new BigInteger(maxId), memoId.getId());
}

@Test
public void testMemoNullHexHashSuccess() {
MemoHash memo = Memo.hash("0000000000000000000000000000000000000000000000000000000000000000");
Expand Down
Loading

0 comments on commit d50d128

Please sign in to comment.