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

software.crldev.elrondspringbootstarterreactive.domain.esdt.ESDTTransfer 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 org.apache.logging.log4j.util.Strings;
import software.crldev.elrondspringbootstarterreactive.domain.account.Address;
import software.crldev.elrondspringbootstarterreactive.domain.common.Balance;
import software.crldev.elrondspringbootstarterreactive.domain.esdt.common.TokenIdentifier;
import software.crldev.elrondspringbootstarterreactive.domain.smartcontract.FunctionArg;
import software.crldev.elrondspringbootstarterreactive.domain.smartcontract.FunctionName;
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.interactor.transaction.TransactionRequest;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static java.lang.String.format;
import static software.crldev.elrondspringbootstarterreactive.config.constants.ESDTConstants.ESDT_TRANSFER_CALL;

/**
 * Value object for ESDT transfer
 *
 * @author carlo_stanciu
 */
public final class ESDTTransfer implements ESDTTransaction {
    @NonNull
    private final Address receiverAddress;
    @NonNull
    private final TokenIdentifier tokenIdentifier;
    @NonNull
    private final Balance amount;
    @NonNull
    private final FunctionName functionName;
    @NonNull
    private final List args;
    @NonNull
    private final GasLimit gasLimit;

    private PayloadData processPayloadData() {
        var functionCall = functionName.isEmpty() ? Strings.EMPTY : "@" + functionName.getHex() + args.stream().map(p -> "@" + p.getHex()).collect(Collectors.joining());
        return PayloadData.fromString(format("%s@%s@%s%s", ESDT_TRANSFER_CALL, tokenIdentifier.getHex(), amount.getHex(), functionCall));
    }

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

    private static FunctionName $default$functionName() {
        return FunctionName.empty();
    }

    private static List $default$args() {
        return Collections.emptyList();
    }

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

    ESDTTransfer(@NonNull final Address receiverAddress, @NonNull final TokenIdentifier tokenIdentifier, @NonNull final Balance amount, @NonNull final FunctionName functionName, @NonNull final List args, @NonNull final GasLimit gasLimit) {
        if (receiverAddress == null) {
            throw new NullPointerException("receiverAddress is marked non-null but is null");
        }
        if (tokenIdentifier == null) {
            throw new NullPointerException("tokenIdentifier is marked non-null but is null");
        }
        if (amount == null) {
            throw new NullPointerException("amount is marked non-null but is null");
        }
        if (functionName == null) {
            throw new NullPointerException("functionName is marked non-null but is null");
        }
        if (args == null) {
            throw new NullPointerException("args is marked non-null but is null");
        }
        if (gasLimit == null) {
            throw new NullPointerException("gasLimit is marked non-null but is null");
        }
        this.receiverAddress = receiverAddress;
        this.tokenIdentifier = tokenIdentifier;
        this.amount = amount;
        this.functionName = functionName;
        this.args = args;
        this.gasLimit = gasLimit;
    }


    public static class ESDTTransferBuilder {
        private Address receiverAddress;
        private TokenIdentifier tokenIdentifier;
        private Balance amount;
        private boolean functionName$set;
        private FunctionName functionName$value;
        private boolean args$set;
        private List args$value;
        private boolean gasLimit$set;
        private GasLimit gasLimit$value;

        ESDTTransferBuilder() {
        }

        /**
         * @return {@code this}.
         */
        public ESDTTransfer.ESDTTransferBuilder receiverAddress(@NonNull final Address receiverAddress) {
            if (receiverAddress == null) {
                throw new NullPointerException("receiverAddress is marked non-null but is null");
            }
            this.receiverAddress = receiverAddress;
            return this;
        }

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

        /**
         * @return {@code this}.
         */
        public ESDTTransfer.ESDTTransferBuilder functionName(@NonNull final FunctionName functionName) {
            if (functionName == null) {
                throw new NullPointerException("functionName is marked non-null but is null");
            }
            this.functionName$value = functionName;
            functionName$set = true;
            return this;
        }

        /**
         * @return {@code this}.
         */
        public ESDTTransfer.ESDTTransferBuilder args(@NonNull final List args) {
            if (args == null) {
                throw new NullPointerException("args is marked non-null but is null");
            }
            this.args$value = args;
            args$set = true;
            return this;
        }

        /**
         * @return {@code this}.
         */
        public ESDTTransfer.ESDTTransferBuilder 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 ESDTTransfer build() {
            FunctionName functionName$value = this.functionName$value;
            if (!this.functionName$set) functionName$value = ESDTTransfer.$default$functionName();
            List args$value = this.args$value;
            if (!this.args$set) args$value = ESDTTransfer.$default$args();
            GasLimit gasLimit$value = this.gasLimit$value;
            if (!this.gasLimit$set) gasLimit$value = ESDTTransfer.$default$gasLimit();
            return new ESDTTransfer(this.receiverAddress, this.tokenIdentifier, this.amount, functionName$value, args$value, gasLimit$value);
        }

        @Override
        public String toString() {
            return "ESDTTransfer.ESDTTransferBuilder(receiverAddress=" + this.receiverAddress + ", tokenIdentifier=" + this.tokenIdentifier + ", amount=" + this.amount + ", functionName$value=" + this.functionName$value + ", args$value=" + this.args$value + ", gasLimit$value=" + this.gasLimit$value + ")";
        }
    }

    public static ESDTTransfer.ESDTTransferBuilder builder() {
        return new ESDTTransfer.ESDTTransferBuilder();
    }

    @NonNull
    public Address getReceiverAddress() {
        return this.receiverAddress;
    }

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

    @NonNull
    public Balance getAmount() {
        return this.amount;
    }

    @NonNull
    public FunctionName getFunctionName() {
        return this.functionName;
    }

    @NonNull
    public List getArgs() {
        return this.args;
    }

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

    @Override
    public boolean equals(final Object o) {
        if (o == this) return true;
        if (!(o instanceof ESDTTransfer)) return false;
        final ESDTTransfer other = (ESDTTransfer) o;
        final Object this$receiverAddress = this.getReceiverAddress();
        final Object other$receiverAddress = other.getReceiverAddress();
        if (this$receiverAddress == null ? other$receiverAddress != null : !this$receiverAddress.equals(other$receiverAddress)) 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$amount = this.getAmount();
        final Object other$amount = other.getAmount();
        if (this$amount == null ? other$amount != null : !this$amount.equals(other$amount)) return false;
        final Object this$functionName = this.getFunctionName();
        final Object other$functionName = other.getFunctionName();
        if (this$functionName == null ? other$functionName != null : !this$functionName.equals(other$functionName)) return false;
        final Object this$args = this.getArgs();
        final Object other$args = other.getArgs();
        if (this$args == null ? other$args != null : !this$args.equals(other$args)) 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 $receiverAddress = this.getReceiverAddress();
        result = result * PRIME + ($receiverAddress == null ? 43 : $receiverAddress.hashCode());
        final Object $tokenIdentifier = this.getTokenIdentifier();
        result = result * PRIME + ($tokenIdentifier == null ? 43 : $tokenIdentifier.hashCode());
        final Object $amount = this.getAmount();
        result = result * PRIME + ($amount == null ? 43 : $amount.hashCode());
        final Object $functionName = this.getFunctionName();
        result = result * PRIME + ($functionName == null ? 43 : $functionName.hashCode());
        final Object $args = this.getArgs();
        result = result * PRIME + ($args == null ? 43 : $args.hashCode());
        final Object $gasLimit = this.getGasLimit();
        result = result * PRIME + ($gasLimit == null ? 43 : $gasLimit.hashCode());
        return result;
    }

    @Override
    public String toString() {
        return "ESDTTransfer(receiverAddress=" + this.getReceiverAddress() + ", tokenIdentifier=" + this.getTokenIdentifier() + ", amount=" + this.getAmount() + ", functionName=" + this.getFunctionName() + ", args=" + this.getArgs() + ", gasLimit=" + this.getGasLimit() + ")";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy