org.ergoplatform.appkit.Eip4Token Maven / Gradle / Ivy
Show all versions of ergo-appkit_2.11 Show documentation
package org.ergoplatform.appkit;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.ergoplatform.sdk.ErgoToken;
import org.ergoplatform.sdk.JavaHelpers;
import scala.Tuple2;
import sigma.Coll;
/**
* Represents an EIP-4 compliant token.
*
* See https://github.com/ergoplatform/eips/blob/master/eip-0004.md
*/
public class Eip4Token extends ErgoToken {
// required EIP-4 fields from R4, R5, R6
private final String name;
private final String description;
private final int decimals;
// optional, type defines the content. We keep this as an ErgoValue to be compatible with
// future types.
private final ErgoValue> r7;
private final ErgoValue> r8;
private final ErgoValue> r9;
/**
* @see Eip4Token#Eip4Token(String, long, String, String, int, ErgoValue, ErgoValue, ErgoValue)
*/
public Eip4Token(@Nonnull String id, long amount, @Nonnull ErgoValue> r4,
@Nonnull ErgoValue> r5, @Nonnull ErgoValue> r6,
@Nullable ErgoValue> r7, @Nullable ErgoValue> r8, @Nullable ErgoValue> r9) {
this(id, amount,
new String(JavaHelpers.collToByteArray(r4.getValue()), StandardCharsets.UTF_8),
new String(JavaHelpers.collToByteArray(r5.getValue()), StandardCharsets.UTF_8),
Integer.parseInt(new String(JavaHelpers.collToByteArray(r6.getValue()), StandardCharsets.UTF_8)),
r7, r8, r9);
}
/**
* @see Eip4Token#Eip4Token(String, long, String, String, int, ErgoValue, ErgoValue, ErgoValue)
*/
public Eip4Token(@Nonnull String id, long amount, @Nonnull String name,
@Nonnull String description, int decimals) {
this(id, amount, name, description, decimals, null, null, null);
}
/**
* Represents an EIP-4 compliant token according to https://github.com/ergoplatform/eips/blob/master/eip-0004.md
*
* Instead of using this constructor, you can use convenience methods from {@code Eip4TokenBuilder}
* in most cases.
*
* @param id token id
* @param amount token amount this object represents
* @param name token display name
* @param description token description
* @param decimals decimals any amounts regarding this token are formatted with
* @param r7 contents of register 7 (see EIP-4 specification)
* @param r8 contents of register 8 (see EIP-4 specification)
* @param r9 contents of register 9 (see EIP-4 specification)
*/
public Eip4Token(@Nonnull String id, long amount, @Nonnull String name,
@Nonnull String description, int decimals,
@Nullable ErgoValue> r7, @Nullable ErgoValue> r8, @Nullable ErgoValue> r9) {
super(id, amount);
this.name = name;
this.description = description;
this.decimals = decimals;
this.r7 = r7;
this.r8 = r8;
this.r9 = r9;
}
/**
* @return EIP-4 token verbose name (R4)
*/
@Nonnull
public String getTokenName() {
return name;
}
/**
* @return EIP-4 token description (R5)
*/
@Nonnull
public String getTokenDescription() {
return description;
}
/**
* @return EIP-4 token decimals (R6)
*/
public int getDecimals() {
return decimals;
}
/**
* @return token amount taking decimals into account
*/
@Nonnull
public String getAmountFormatted() {
return BigDecimal.valueOf(getValue()).movePointLeft(decimals).toPlainString();
}
/**
* @return type of asset this token represents, see {@link AssetType}
*/
@Nonnull
public AssetType getAssetType() {
byte[] assetType = getR7ByteArrayOrNull();
if (r7 == null) {
return AssetType.NONE;
} else if (assetType == null) {
return AssetType.UNKNOWN;
} else {
for (AssetType type : AssetType.values()) {
byte[] r7EntryForType = type.getR7ByteArrayForType();
if (r7EntryForType != null && Arrays.equals(r7EntryForType, assetType))
return type;
}
return AssetType.UNKNOWN;
}
}
/**
* @return byte content of register r7, or `null` if not set
*/
@Nullable
private byte[] getR7ByteArrayOrNull() {
// r7 specifies the asset type and should be Coll
if (r7 != null && r7.getValue() instanceof Coll) {
Coll> assetType = (Coll>) r7.getValue();
if (assetType.length() > 0 && assetType.tItem() == ErgoType.byteType().getRType()) {
return JavaHelpers.collToByteArray((Coll