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

software.crldev.elrondspringbootstarterreactive.domain.esdt.ESDTRoleAssignment 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.ESDTSpecialRole;
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 java.util.Set;
import java.util.stream.Collectors;
import static java.lang.String.format;
import static software.crldev.elrondspringbootstarterreactive.config.constants.ESDTConstants.*;

/**
 * Value object for ESDT role assignment
 *
 * @author carlo_stanciu
 */
public final class ESDTRoleAssignment implements ESDTTransaction {
    @NonNull
    private final Type type;
    @NonNull
    private final TokenIdentifier tokenIdentifier;
    @NonNull
    private final Address address;
    @NonNull
    private final Set roles;
    @NonNull
    private final GasLimit gasLimit;

    private PayloadData processPayloadData() {
        if (roles.isEmpty()) throw new IllegalArgumentException(ErrorMessage.TOKEN_ROLE.getValue());
        return PayloadData.fromString(format("%s@%s@%s@%s", processType(), tokenIdentifier.getHex(), address.getHex(), roles.stream().map(ESDTSpecialRole::getHex).collect(Collectors.joining("@"))));
    }

    @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 processType() {
        return type.equals(Type.SET) ? ESDT_SET_ROLE_CALL : ESDT_UNSET_ROLE_CALL;
    }


    public enum Type {
        SET, UNSET;
    }

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

    ESDTRoleAssignment(@NonNull final Type type, @NonNull final TokenIdentifier tokenIdentifier, @NonNull final Address address, @NonNull final Set roles, @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 (address == null) {
            throw new NullPointerException("address is marked non-null but is null");
        }
        if (roles == null) {
            throw new NullPointerException("roles 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.address = address;
        this.roles = roles;
        this.gasLimit = gasLimit;
    }


    public static class ESDTRoleAssignmentBuilder {
        private Type type;
        private TokenIdentifier tokenIdentifier;
        private Address address;
        private Set roles;
        private boolean gasLimit$set;
        private GasLimit gasLimit$value;

        ESDTRoleAssignmentBuilder() {
        }

        /**
         * @return {@code this}.
         */
        public ESDTRoleAssignment.ESDTRoleAssignmentBuilder 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 ESDTRoleAssignment.ESDTRoleAssignmentBuilder 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 ESDTRoleAssignment.ESDTRoleAssignmentBuilder address(@NonNull final Address address) {
            if (address == null) {
                throw new NullPointerException("address is marked non-null but is null");
            }
            this.address = address;
            return this;
        }

        /**
         * @return {@code this}.
         */
        public ESDTRoleAssignment.ESDTRoleAssignmentBuilder roles(@NonNull final Set roles) {
            if (roles == null) {
                throw new NullPointerException("roles is marked non-null but is null");
            }
            this.roles = roles;
            return this;
        }

        /**
         * @return {@code this}.
         */
        public ESDTRoleAssignment.ESDTRoleAssignmentBuilder 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 ESDTRoleAssignment build() {
            GasLimit gasLimit$value = this.gasLimit$value;
            if (!this.gasLimit$set) gasLimit$value = ESDTRoleAssignment.$default$gasLimit();
            return new ESDTRoleAssignment(this.type, this.tokenIdentifier, this.address, this.roles, gasLimit$value);
        }

        @Override
        public String toString() {
            return "ESDTRoleAssignment.ESDTRoleAssignmentBuilder(type=" + this.type + ", tokenIdentifier=" + this.tokenIdentifier + ", address=" + this.address + ", roles=" + this.roles + ", gasLimit$value=" + this.gasLimit$value + ")";
        }
    }

    public static ESDTRoleAssignment.ESDTRoleAssignmentBuilder builder() {
        return new ESDTRoleAssignment.ESDTRoleAssignmentBuilder();
    }

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

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

    @NonNull
    public Address getAddress() {
        return this.address;
    }

    @NonNull
    public Set getRoles() {
        return this.roles;
    }

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

    @Override
    public boolean equals(final Object o) {
        if (o == this) return true;
        if (!(o instanceof ESDTRoleAssignment)) return false;
        final ESDTRoleAssignment other = (ESDTRoleAssignment) 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$address = this.getAddress();
        final Object other$address = other.getAddress();
        if (this$address == null ? other$address != null : !this$address.equals(other$address)) return false;
        final Object this$roles = this.getRoles();
        final Object other$roles = other.getRoles();
        if (this$roles == null ? other$roles != null : !this$roles.equals(other$roles)) 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 $address = this.getAddress();
        result = result * PRIME + ($address == null ? 43 : $address.hashCode());
        final Object $roles = this.getRoles();
        result = result * PRIME + ($roles == null ? 43 : $roles.hashCode());
        final Object $gasLimit = this.getGasLimit();
        result = result * PRIME + ($gasLimit == null ? 43 : $gasLimit.hashCode());
        return result;
    }

    @Override
    public String toString() {
        return "ESDTRoleAssignment(type=" + this.getType() + ", tokenIdentifier=" + this.getTokenIdentifier() + ", address=" + this.getAddress() + ", roles=" + this.getRoles() + ", gasLimit=" + this.getGasLimit() + ")";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy