Skip to content

Commit

Permalink
lightsail-network#396: streamlined Asset creation to be consistent fo…
Browse files Browse the repository at this point in the history
…r all asset types(native, alpha4, alpha12, pool share). Fixed TrustlineCreatedEffectReponse marshaling to work when asset is of pool share type.
  • Loading branch information
sreuland committed Jan 6, 2022
1 parent 6fc49dc commit 0fc1ff1
Show file tree
Hide file tree
Showing 36 changed files with 434 additions and 202 deletions.
75 changes: 44 additions & 31 deletions src/main/java/org/stellar/sdk/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,68 @@ public abstract class Asset implements Comparable<Asset> {
*/
public static Asset create(String canonicalForm) {
if (canonicalForm.equals("native")) {
return new AssetTypeNative();
return create(canonicalForm, null, null);
}
String [] parts = canonicalForm.split(":");
if (parts.length != 2) {
throw new IllegalArgumentException("invalid asset "+ canonicalForm);
}
return Asset.createNonNativeAsset(parts[0], parts[1]);
return create(null, parts[0], parts[1]);
}

/**
* Creates Asset for Alpha4/Alpha5/Native
* Creates Asset for Alpha4/Alpha12/Native
*
* @param type the type of asset can be 'native', 'alpha4', 'alpha12'
* @param code the asset code that conforms to type or null
* @param issuer the asset issuer the conforms to type or null
* @return
* @param type the type of asset. 'native' will generate its respective asset sub-class,
* if null or any other value will attempt to derive the asset sub-class from code and issuer.
* @param code the asset code or null
* @param issuer the asset issuer or null
* @return Asset
*/
public static Asset create(String type, String code, String issuer) {
return create(type, code, issuer, null);
}

/**
* Creates Asset for Alpha4/Alpha5/Native/LiquidityPool
* Creates Asset for Alpha4/Alpha12/Native/LiquidityPool
*
* @param type the type of asset can be 'native', 'alpha4', 'alpha12' or 'liquidity_pool_shares'
* @param code the asset code that conforms to type or null
* @param issuer the asset issuer the conforms to type or null
* @param liquidityPoolID provided only if type is 'liquidity_pool_shares'
* @param type the type of asset. 'native' and 'liquidity_pool_shares' will generate their respective asset sub-classes
* null or any other value will attempt to derive the asset sub-class from code and issuer.
* @param code the asset code or null
* @param issuer the asset issuer or null
* @param liquidityPoolID required only if type is 'liquidity_pool_shares'
* @return Asset
*/
public static Asset create(String type, String code, String issuer, String liquidityPoolID) {
if (type == null) {
return createNonNativeAsset(code, issuer);
}
if (type.equals("native")) {
return new AssetTypeNative();
}
if (type.equals("liquidity_pool_shares")) {
return new AssetTypePoolShare(liquidityPoolID);
}

return Asset.createNonNativeAsset(code, issuer);
return createNonNativeAsset(code, issuer);
}

/**
* Create Asset from a ChangeTrustAsset
* @param wrapped the ChangeTrustAsset wrapper
* @return Asset
*/
public static Asset create(ChangeTrustAsset.Wrapper wrapped) {
return wrapped.getAsset();
}
public static Asset create(TrustLineAsset.Wrapper wrapped) {
return wrapped.getAsset();
}

/**
* Creates one of AssetTypeCreditAlphaNum4 or AssetTypeCreditAlphaNum12 object based on a <code>code</code> length
* @param code Asset code
* @param issuer Asset issuer
* Create Asset from a TrustLineAsset
*
* @param wrapped the TrustLineAsset wrapper
* @return Asset
*/
public static Asset createNonNativeAsset(String code, String issuer) {
if (code.length() >= 1 && code.length() <= 4) {
return new AssetTypeCreditAlphaNum4(code, issuer);
} else if (code.length() >= 5 && code.length() <= 12) {
return new AssetTypeCreditAlphaNum12(code, issuer);
} else {
throw new AssetCodeLengthInvalidException();
}
public static Asset create(TrustLineAsset.Wrapper wrapped) {
return wrapped.getAsset();
}

/**
Expand Down Expand Up @@ -113,13 +114,25 @@ public static Asset fromXdr(org.stellar.sdk.xdr.Asset xdr) {
*/
public abstract String getType();

@Override
public abstract boolean equals(Object object);

/**
* Generates XDR object from a given Asset object
*/
public abstract org.stellar.sdk.xdr.Asset toXdr();

@Override
public abstract boolean equals(Object object);

@Override
public abstract int compareTo(Asset other);

private static Asset createNonNativeAsset(String code, String issuer) {
if (code.length() >= 1 && code.length() <= 4) {
return new AssetTypeCreditAlphaNum4(code, issuer);
} else if (code.length() >= 5 && code.length() <= 12) {
return new AssetTypeCreditAlphaNum12(code, issuer);
} else {
throw new AssetCodeLengthInvalidException();
}
}

}
45 changes: 40 additions & 5 deletions src/main/java/org/stellar/sdk/ChangeTrustAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,68 @@ public abstract class ChangeTrustAsset implements Comparable<ChangeTrustAsset> {
* The asset string is expected to either be "native" or a string of the form "CODE:ISSUER"
*
* @param canonicalForm Canonical string representation of an asset
* @return ChangeTrustAsset
*/
public static ChangeTrustAsset create(String canonicalForm) {
return new Wrapper(Asset.create(canonicalForm));
}

/**
* Create ChangeTrustAsset from asset primitive values
*
* @param type the asset type
* @param code the asset code
* @param issuer the asset issuer
* @return ChangeTrustAsset
*/
public static ChangeTrustAsset create(String type, String code, String issuer) {
return new Wrapper(Asset.create(type, code, issuer));
}

/**
* Create ChangeTrustAsset from another Asset
*
* @param asset the Asset
* @return ChangeTrustAsset
*/
public static ChangeTrustAsset create(Asset asset) {
return new Wrapper(asset);
}

/**
* Create a ChangeTrustAsset from LiquidityPoolParameters
*
* @param params the LiquidityPoolParameters
* @return ChangeTrustAsset
*/
public static ChangeTrustAsset create(LiquidityPoolParameters params) {
return new LiquidityPoolShareChangeTrustAsset(params);
}

/**
* Create a ChangeTrustAsset from TrustLineAsset
*
* @param wrapper the TrustLineAsset wrapper
* @return ChangeTrustAsset
*/
public static ChangeTrustAsset create(TrustLineAsset.Wrapper wrapper) {
return new Wrapper(wrapper.getAsset());
}
// Cannot convert from LiquidityPoolShareTrustLineAsset, as that only has a
// liquidity pool id, not parameters.

/**
* Creates one of AssetTypeCreditAlphaNum4 or AssetTypeCreditAlphaNum12 object based on a <code>code</code> length
* @param code ChangeTrustAsset code
* @param issuer ChangeTrustAsset issuer
*/
public static ChangeTrustAsset createNonNativeAsset(String code, String issuer) {
return ChangeTrustAsset.create(Asset.createNonNativeAsset(code, issuer));
return create(Asset.create(null, code, issuer));
}

/**
* Generates ChangeTrustAsset object from a given XDR object
*
* @param xdr XDR object
* @return ChangeTrustAsset
*/
public static ChangeTrustAsset fromXdr(org.stellar.sdk.xdr.ChangeTrustAsset xdr) {
// TODO: Figure out how we can re-use Asset.fromXdr here
Expand All @@ -68,16 +98,21 @@ public static ChangeTrustAsset fromXdr(org.stellar.sdk.xdr.ChangeTrustAsset xdr)
}
}

public abstract String getType();

@Override
public abstract boolean equals(Object object);

@Override
public abstract int compareTo(ChangeTrustAsset other);

/**
* Get the asset type
* @return the asset type
*/
public abstract String getType();

/**
* Generates XDR object from a given ChangeTrustAsset object
* @return xdr model
*/
public abstract org.stellar.sdk.xdr.ChangeTrustAsset toXdr();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
package org.stellar.sdk;

import com.google.common.base.Objects;
import org.stellar.sdk.xdr.AssetType;

import static com.google.common.base.Preconditions.checkNotNull;

import org.stellar.sdk.xdr.*;

/**
* Class for LiquidityPoolShareChangeTrustAsset
* @see <a href="https://developers.stellar.org/docs/glossary/liquidity-pool/" target="_blank">Liquidity Pool</a>
*/
public final class LiquidityPoolShareChangeTrustAsset extends ChangeTrustAsset {
protected final LiquidityPoolParameters mParams;

/**
* Constructor
*
* @param params the liquidity pool parameters
*/
public LiquidityPoolShareChangeTrustAsset(LiquidityPoolParameters params) {
checkNotNull(params, "params cannot be null");
mParams = params;
}

/**
* Get the liquidity pool id
*
* @return LiquidityPoolID
*/
public LiquidityPoolID getLiquidityPoolID() {
return mParams.getId();
}

/**
* Get the liquidity pool parameters
*
* @return LiquidityPoolParameters
*/
public LiquidityPoolParameters getLiquidityPoolParams() {
return mParams;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
package org.stellar.sdk;

import com.google.common.base.Objects;
import org.stellar.sdk.xdr.AssetType;

import static com.google.common.base.Preconditions.checkNotNull;

import org.stellar.sdk.xdr.*;

/**
* Class for LiquidityPoolShareTrustLineAsset
* @see <a href="https://developers.stellar.org/docs/glossary/liquidity-pool/" target="_blank">Liquidity Pool</a>
*/
public final class LiquidityPoolShareTrustLineAsset extends TrustLineAsset {
protected final LiquidityPoolID mId;

/**
* Constructor
*
* @param params the LiquidityPoolParameters
*/
public LiquidityPoolShareTrustLineAsset(LiquidityPoolParameters params) {
checkNotNull(params, "params cannot be null");
mId = params.getId();
}

/**
* Constructor
*
* @param id the LiquidityPoolID
*/
public LiquidityPoolShareTrustLineAsset(LiquidityPoolID id) {
checkNotNull(id, "id cannot be null");
mId = id;
}

/**
* Get the liquidity pool id
*
* @return LiquidityPoolID
*/
public LiquidityPoolID getLiquidityPoolID() {
return mId;
}
Expand Down
Loading

0 comments on commit 0fc1ff1

Please sign in to comment.