All Downloads are FREE. Search and download functionalities are using the official Maven repository.

software.crldev.elrondspringbootstarterreactive.domain.esdt.ESDTGlobalOp Maven / Gradle / Ivy

Go to download

A SpringBoot Starter solution designed to ensure easy and efficient integration with the Elrond Network using a Reactive API layer.

The newest version!
package software.crldev.elrondspringbootstarterreactive.domain.esdt;

import lombok.NonNull;
import software.crldev.elrondspringbootstarterreactive.domain.account.Address;
import software.crldev.elrondspringbootstarterreactive.domain.common.Balance;
import software.crldev.elrondspringbootstarterreactive.domain.esdt.ESDTTransaction;
import software.crldev.elrondspringbootstarterreactive.domain.esdt.common.TokenIdentifier;
import software.crldev.elrondspringbootstarterreactive.domain.transaction.GasLimit;
import software.crldev.elrondspringbootstarterreactive.domain.transaction.PayloadData;
import software.crldev.elrondspringbootstarterreactive.domain.wallet.Wallet;
import software.crldev.elrondspringbootstarterreactive.error.ErrorMessage;
import software.crldev.elrondspringbootstarterreactive.interactor.transaction.TransactionRequest;
import static io.netty.util.internal.StringUtil.EMPTY_STRING;
import static java.lang.String.format;
import static software.crldev.elrondspringbootstarterreactive.config.constants.ESDTConstants.*;

/**
 * Value object for ESDT global operations
 *
 * @author carlo_stanciu
 */
public final class ESDTGlobalOp implements ESDTTransaction {
    @NonNull
    private final Type type;
    @NonNull
    private final TokenIdentifier tokenIdentifier;
    @NonNull
    private final Address targetAddress;
    @NonNull
    private final GasLimit gasLimit;

    private PayloadData processPayloadData() {
        return PayloadData.fromString(format("%s@%s%s", processType(), tokenIdentifier.getHex(), processAddressArg()));
    }

    @Override
    public TransactionRequest toTransactionRequest(Wallet wallet) {
        return TransactionRequest.builder().receiverAddress(Address.fromBech32(ESDT_ISSUER_BECH32_ADDRESS)).data(processPayloadData()).value(Balance.zero()).gasLimit(gasLimit).build();
    }

    private String processAddressArg() {
        if (type.equals(Type.PAUSE) || type.equals(Type.UNPAUSE)) return EMPTY_STRING;
        return "@" + targetAddress.getHex();
    }

    private String processType() {
        switch (type) {
        case FREEZE: 
            throwIfAddressEmpty();
            return ESDT_FREEZE_CALL;
        case UNFREEZE: 
            throwIfAddressEmpty();
            return ESDT_UNFREEZE_CALL;
        case PAUSE: 
            return ESDT_PAUSE_CALL;
        case UNPAUSE: 
            return ESDT_UNPAUSE_CALL;
        case WIPE: 
            throwIfAddressEmpty();
            return ESDT_WIPE_CALL;
        default: 
            throw new IllegalArgumentException();
        }
    }

    private void throwIfAddressEmpty() {
        if (targetAddress.isZero()) throw new IllegalArgumentException(ErrorMessage.ADDRESS_EMPTY.getValue());
    }


    public enum Type {
        PAUSE, UNPAUSE, FREEZE, UNFREEZE, WIPE;
    }

    private static Address $default$targetAddress() {
        return Address.zero();
    }

    private static GasLimit $default$gasLimit() {
        return GasLimit.defaultEsdtIssuance();
    }

    ESDTGlobalOp(@NonNull final Type type, @NonNull final TokenIdentifier tokenIdentifier, @NonNull final Address targetAddress, @NonNull final GasLimit gasLimit) {
        if (type == null) {
            throw new NullPointerException("type is marked non-null but is null");
        }
        if (tokenIdentifier == null) {
            throw new NullPointerException("tokenIdentifier is marked non-null but is null");
        }
        if (targetAddress == null) {
            throw new NullPointerException("targetAddress is marked non-null but is null");
        }
        if (gasLimit == null) {
            throw new NullPointerException("gasLimit is marked non-null but is null");
        }
        this.type = type;
        this.tokenIdentifier = tokenIdentifier;
        this.targetAddress = targetAddress;
        this.gasLimit = gasLimit;
    }


    public static class ESDTGlobalOpBuilder {
        private Type type;
        private TokenIdentifier tokenIdentifier;
        private boolean targetAddress$set;
        private Address targetAddress$value;
        private boolean gasLimit$set;
        private GasLimit gasLimit$value;

        ESDTGlobalOpBuilder() {
        }

        /**
         * @return {@code this}.
         */
        public ESDTGlobalOp.ESDTGlobalOpBuilder type(@NonNull final Type type) {
            if (type == null) {
                throw new NullPointerException("type is marked non-null but is null");
            }
            this.type = type;
            return this;
        }

        /**
         * @return {@code this}.
         */
        public ESDTGlobalOp.ESDTGlobalOpBuilder tokenIdentifier(@NonNull final TokenIdentifier tokenIdentifier) {
            if (tokenIdentifier == null) {
                throw new NullPointerException("tokenIdentifier is marked non-null but is null");
            }
            this.tokenIdentifier = tokenIdentifier;
            return this;
        }

        /**
         * @return {@code this}.
         */
        public ESDTGlobalOp.ESDTGlobalOpBuilder targetAddress(@NonNull final Address targetAddress) {
            if (targetAddress == null) {
                throw new NullPointerException("targetAddress is marked non-null but is null");
            }
            this.targetAddress$value = targetAddress;
            targetAddress$set = true;
            return this;
        }

        /**
         * @return {@code this}.
         */
        public ESDTGlobalOp.ESDTGlobalOpBuilder gasLimit(@NonNull final GasLimit gasLimit) {
            if (gasLimit == null) {
                throw new NullPointerException("gasLimit is marked non-null but is null");
            }
            this.gasLimit$value = gasLimit;
            gasLimit$set = true;
            return this;
        }

        public ESDTGlobalOp build() {
            Address targetAddress$value = this.targetAddress$value;
            if (!this.targetAddress$set) targetAddress$value = ESDTGlobalOp.$default$targetAddress();
            GasLimit gasLimit$value = this.gasLimit$value;
            if (!this.gasLimit$set) gasLimit$value = ESDTGlobalOp.$default$gasLimit();
            return new ESDTGlobalOp(this.type, this.tokenIdentifier, targetAddress$value, gasLimit$value);
        }

        @Override
        public String toString() {
            return "ESDTGlobalOp.ESDTGlobalOpBuilder(type=" + this.type + ", tokenIdentifier=" + this.tokenIdentifier + ", targetAddress$value=" + this.targetAddress$value + ", gasLimit$value=" + this.gasLimit$value + ")";
        }
    }

    public static ESDTGlobalOp.ESDTGlobalOpBuilder builder() {
        return new ESDTGlobalOp.ESDTGlobalOpBuilder();
    }

    @NonNull
    public Type getType() {
        return this.type;
    }

    @NonNull
    public TokenIdentifier getTokenIdentifier() {
        return this.tokenIdentifier;
    }

    @NonNull
    public Address getTargetAddress() {
        return this.targetAddress;
    }

    @NonNull
    public GasLimit getGasLimit() {
        return this.gasLimit;
    }

    @Override
    public boolean equals(final Object o) {
        if (o == this) return true;
        if (!(o instanceof ESDTGlobalOp)) return false;
        final ESDTGlobalOp other = (ESDTGlobalOp) o;
        final Object this$type = this.getType();
        final Object other$type = other.getType();
        if (this$type == null ? other$type != null : !this$type.equals(other$type)) return false;
        final Object this$tokenIdentifier = this.getTokenIdentifier();
        final Object other$tokenIdentifier = other.getTokenIdentifier();
        if (this$tokenIdentifier == null ? other$tokenIdentifier != null : !this$tokenIdentifier.equals(other$tokenIdentifier)) return false;
        final Object this$targetAddress = this.getTargetAddress();
        final Object other$targetAddress = other.getTargetAddress();
        if (this$targetAddress == null ? other$targetAddress != null : !this$targetAddress.equals(other$targetAddress)) return false;
        final Object this$gasLimit = this.getGasLimit();
        final Object other$gasLimit = other.getGasLimit();
        if (this$gasLimit == null ? other$gasLimit != null : !this$gasLimit.equals(other$gasLimit)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $type = this.getType();
        result = result * PRIME + ($type == null ? 43 : $type.hashCode());
        final Object $tokenIdentifier = this.getTokenIdentifier();
        result = result * PRIME + ($tokenIdentifier == null ? 43 : $tokenIdentifier.hashCode());
        final Object $targetAddress = this.getTargetAddress();
        result = result * PRIME + ($targetAddress == null ? 43 : $targetAddress.hashCode());
        final Object $gasLimit = this.getGasLimit();
        result = result * PRIME + ($gasLimit == null ? 43 : $gasLimit.hashCode());
        return result;
    }

    @Override
    public String toString() {
        return "ESDTGlobalOp(type=" + this.getType() + ", tokenIdentifier=" + this.getTokenIdentifier() + ", targetAddress=" + this.getTargetAddress() + ", gasLimit=" + this.getGasLimit() + ")";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy