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

cc.youchain.tx.Contract Maven / Gradle / Ivy

There is a newer version: 1.1.5
Show newest version
package cc.youchain.tx;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import cc.youchain.abi.EventEncoder;
import cc.youchain.abi.EventValues;
import cc.youchain.abi.FunctionEncoder;
import cc.youchain.abi.FunctionReturnDecoder;
import cc.youchain.abi.TypeReference;
import cc.youchain.abi.datatypes.Address;
import cc.youchain.abi.datatypes.Event;
import cc.youchain.abi.datatypes.Function;
import cc.youchain.abi.datatypes.Type;
import cc.youchain.protocol.YOUChain;
import cc.youchain.protocol.core.DefaultBlockParameter;
import cc.youchain.protocol.core.DefaultBlockParameterName;
import cc.youchain.protocol.core.RemoteCall;
import cc.youchain.protocol.core.methods.request.Transaction;
import cc.youchain.protocol.core.methods.response.YOUCall;
import cc.youchain.protocol.core.methods.response.YOUGetCode;
import cc.youchain.protocol.core.methods.response.Log;
import cc.youchain.protocol.core.methods.response.TransactionReceipt;
import cc.youchain.protocol.exceptions.TransactionException;
import cc.youchain.tx.exceptions.ContractCallException;
import cc.youchain.tx.gas.ContractGasProvider;
import cc.youchain.tx.gas.DefaultGasProvider;
import cc.youchain.tx.gas.StaticGasProvider;
import cc.youchain.utils.Numeric;
import cc.youchain.crypto.Credentials;


/**
 * Solidity contract type abstraction for interacting with smart contracts via native Java types.
 */
@SuppressWarnings("WeakerAccess")
public abstract class Contract extends ManagedTransaction {

    //https://www.reddit.com/r/ethereum/comments/5g8ia6/attention_miners_we_recommend_raising_gas_limit/
    /**
     * @see DefaultGasProvider
     * @deprecated ...
     */
    public static final BigInteger GAS_LIMIT = BigInteger.valueOf(4_300_000);

    public static final String BIN_NOT_PROVIDED = "Bin file was not provided";
    public static final String FUNC_DEPLOY = "deploy";

    protected final String contractBinary;
    protected String contractAddress;
    protected ContractGasProvider gasProvider;
    protected TransactionReceipt transactionReceipt;
    protected Map deployedAddresses;
    protected DefaultBlockParameter defaultBlockParameter = DefaultBlockParameterName.LATEST;

    protected Contract(String contractBinary, String contractAddress,
                       YOUChain youChain, TransactionManager transactionManager,
                       ContractGasProvider gasProvider) {
        super(youChain, transactionManager);

        this.contractAddress = contractAddress;

        this.contractBinary = contractBinary;
        this.gasProvider = gasProvider;
    }

    protected Contract(String contractBinary, String contractAddress,
                       YOUChain youChain, Credentials credentials,
                       ContractGasProvider gasProvider) {

        this(contractBinary, contractAddress, youChain,
                new RawTransactionManager(youChain, credentials),
                gasProvider);
    }

    @Deprecated
    protected Contract(String contractBinary, String contractAddress,
                       YOUChain youChain, TransactionManager transactionManager,
                       BigInteger gasPrice, BigInteger gasLimit) {
        this(contractBinary, contractAddress, youChain, transactionManager,
                new StaticGasProvider(gasPrice, gasLimit));
    }

    @Deprecated
    protected Contract(String contractBinary, String contractAddress,
                       YOUChain youChain, Credentials credentials,
                       BigInteger gasPrice, BigInteger gasLimit) {
        this(contractBinary, contractAddress, youChain, new RawTransactionManager(youChain, credentials),
                gasPrice, gasLimit);
    }

    @Deprecated
    protected Contract(String contractAddress,
                       YOUChain youChain, TransactionManager transactionManager,
                       BigInteger gasPrice, BigInteger gasLimit) {
        this("", contractAddress, youChain, transactionManager, gasPrice, gasLimit);
    }

    @Deprecated
    protected Contract(String contractAddress,
                       YOUChain youChain, Credentials credentials,
                       BigInteger gasPrice, BigInteger gasLimit) {
        this("", contractAddress, youChain, new RawTransactionManager(youChain, credentials),
                gasPrice, gasLimit);
    }

    public void setContractAddress(String contractAddress) {
        this.contractAddress = contractAddress;
    }

    public String getContractAddress() {
        return contractAddress;
    }

    public void setTransactionReceipt(TransactionReceipt transactionReceipt) {
        this.transactionReceipt = transactionReceipt;
    }

    public String getContractBinary() {
        return contractBinary;
    }

    public void setGasProvider(ContractGasProvider gasProvider) {
        this.gasProvider = gasProvider;
    }

    /**
     * Allow {@code gasPrice} to be set.
     *
     * @param newPrice gas price to use for subsequent transactions
     * @deprecated use ContractGasProvider
     */
    public void setGasPrice(BigInteger newPrice) {
        this.gasProvider = new StaticGasProvider(newPrice, gasProvider.getGasLimit());
    }

    /**
     * Get the current {@code gasPrice} value this contract uses when executing transactions.
     *
     * @return the gas price set on this contract
     * @deprecated use ContractGasProvider
     */
    public BigInteger getGasPrice() {
        return gasProvider.getGasPrice();
    }

    /**
     * Check that the contract deployed at the address associated with this smart contract wrapper
     * is in fact the contract you believe it is.
     *
     * 

This method uses the you_getCode method * to get the contract byte code and validates it against the byte code stored in this smart * contract wrapper. * * @return true if the contract is valid * @throws IOException if unable to connect to YOUChain node */ public boolean isValid() throws IOException { if (contractBinary.equals(BIN_NOT_PROVIDED)) { throw new UnsupportedOperationException( "Contract binary not present in contract wrapper, " + "please generate your wrapper using -abiFile="); } if (contractAddress.equals("")) { throw new UnsupportedOperationException( "Contract binary not present, you will need to regenerate your smart " + "contract wrapper with youchain v1.1.0+"); } YOUGetCode youGetCode = youChain .youGetCode(contractAddress, DefaultBlockParameterName.LATEST) .send(); if (youGetCode.hasError()) { return false; } String code = Numeric.cleanHexPrefix(youGetCode.getCode()); // There may be multiple contracts in the Solidity bytecode, hence we only check for a // match with a subset return !code.isEmpty() && contractBinary.contains(code); } /** * If this Contract instance was created at deployment, the TransactionReceipt associated * with the initial creation will be provided, e.g. via a deploy method. This will * not persist for Contracts instances constructed via a load method. * * @return the TransactionReceipt generated at contract deployment */ public Optional getTransactionReceipt() { return Optional.ofNullable(transactionReceipt); } /** * Sets the default block parameter. This use useful if one wants to query * historical state of a contract. * * @param defaultBlockParameter the default block parameter */ public void setDefaultBlockParameter(DefaultBlockParameter defaultBlockParameter) { this.defaultBlockParameter = defaultBlockParameter; } /** * Execute constant function call - i.e. a call that does not change state of the contract * * @param function to call * @return {@link List} of values returned by function call */ private List executeCall( Function function) throws IOException { String encodedFunction = FunctionEncoder.encode(function); YOUCall youCall = youChain.youCall( Transaction.createYOUCallTransaction( transactionManager.getFromAddress(), contractAddress, encodedFunction), defaultBlockParameter) .send(); String value = youCall.getValue(); return FunctionReturnDecoder.decode(value, function.getOutputParameters()); } @SuppressWarnings("unchecked") protected T executeCallSingleValueReturn( Function function) throws IOException { List values = executeCall(function); if (!values.isEmpty()) { return (T) values.get(0); } else { return null; } } @SuppressWarnings("unchecked") protected R executeCallSingleValueReturn( Function function, Class returnType) throws IOException { T result = executeCallSingleValueReturn(function); if (result == null) { throw new ContractCallException("Empty value (0x) returned from contract"); } Object value = result.getValue(); if (returnType.isAssignableFrom(value.getClass())) { return (R) value; } else if (result.getClass().equals(Address.class) && returnType.equals(String.class)) { return (R) result.toString(); // cast isn't necessary } else { throw new ContractCallException( "Unable to convert response: " + value + " to expected type: " + returnType.getSimpleName()); } } protected List executeCallMultipleValueReturn( Function function) throws IOException { return executeCall(function); } protected TransactionReceipt executeTransaction( Function function) throws IOException, TransactionException { return executeTransaction(function, BigInteger.ZERO); } private TransactionReceipt executeTransaction( Function function, BigInteger luValue) throws IOException, TransactionException { return executeTransaction(FunctionEncoder.encode(function), luValue, function.getName()); } /** * Given the duration required to execute a transaction. * * @param data to send in transaction * @param luValue in Lu to send in transaction * @return {@link Optional} containing our transaction receipt * @throws IOException if the call to the node fails * @throws TransactionException if the transaction was not mined while waiting */ TransactionReceipt executeTransaction( String data, BigInteger luValue, String funcName) throws TransactionException, IOException { TransactionReceipt receipt = send(contractAddress, data, luValue, gasProvider.getGasPrice(funcName), gasProvider.getGasLimit(funcName)); if (!receipt.isStatusOK()) { throw new TransactionException( String.format( "Transaction has failed with status: %s. " + "Gas used: %d. (not-enough gas?)", receipt.getStatus(), receipt.getGasUsed())); } return receipt; } protected RemoteCall executeRemoteCallSingleValueReturn(Function function) { return new RemoteCall<>(() -> executeCallSingleValueReturn(function)); } protected RemoteCall executeRemoteCallSingleValueReturn( Function function, Class returnType) { return new RemoteCall<>(() -> executeCallSingleValueReturn(function, returnType)); } protected RemoteCall> executeRemoteCallMultipleValueReturn(Function function) { return new RemoteCall<>(() -> executeCallMultipleValueReturn(function)); } protected RemoteCall executeRemoteCallTransaction(Function function) { return new RemoteCall<>(() -> executeTransaction(function)); } protected RemoteCall executeRemoteCallTransaction( Function function, BigInteger luValue) { return new RemoteCall<>(() -> executeTransaction(function, luValue)); } private static T create( T contract, String binary, String encodedConstructor, BigInteger value) throws IOException, TransactionException { TransactionReceipt transactionReceipt = contract.executeTransaction(binary + encodedConstructor, value, FUNC_DEPLOY); String contractAddress = transactionReceipt.getContractAddress(); if (contractAddress == null) { throw new RuntimeException("Empty contract address returned"); } contract.setContractAddress(contractAddress); contract.setTransactionReceipt(transactionReceipt); return contract; } protected static T deploy( Class type, YOUChain youChain, Credentials credentials, ContractGasProvider contractGasProvider, String binary, String encodedConstructor, BigInteger value) throws RuntimeException, TransactionException { try { Constructor constructor = type.getDeclaredConstructor( String.class, YOUChain.class, Credentials.class, ContractGasProvider.class); constructor.setAccessible(true); // we want to use null here to ensure that "to" parameter on message is not populated T contract = constructor.newInstance(null, youChain, credentials, contractGasProvider); return create(contract, binary, encodedConstructor, value); } catch (TransactionException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } protected static T deploy( Class type, YOUChain youChain, TransactionManager transactionManager, ContractGasProvider contractGasProvider, String binary, String encodedConstructor, BigInteger value) throws RuntimeException, TransactionException { try { Constructor constructor = type.getDeclaredConstructor( String.class, YOUChain.class, TransactionManager.class, ContractGasProvider.class); constructor.setAccessible(true); // we want to use null here to ensure that "to" parameter on message is not populated T contract = constructor.newInstance( null, youChain, transactionManager, contractGasProvider); return create(contract, binary, encodedConstructor, value); } catch (TransactionException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } @Deprecated protected static T deploy( Class type, YOUChain youChain, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, String binary, String encodedConstructor, BigInteger value) throws RuntimeException, TransactionException { return deploy(type, youChain, credentials, new StaticGasProvider(gasPrice, gasLimit), binary, encodedConstructor, value); } @Deprecated protected static T deploy( Class type, YOUChain youChain, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, String binary, String encodedConstructor, BigInteger value) throws RuntimeException, TransactionException { return deploy(type, youChain, transactionManager, new StaticGasProvider(gasPrice, gasLimit), binary, encodedConstructor, value); } public static RemoteCall deployRemoteCall( Class type, YOUChain youChain, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, String binary, String encodedConstructor, BigInteger value) { return new RemoteCall<>(() -> deploy( type, youChain, credentials, gasPrice, gasLimit, binary, encodedConstructor, value)); } public static RemoteCall deployRemoteCall( Class type, YOUChain youChain, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, String binary, String encodedConstructor) { return deployRemoteCall( type, youChain, credentials, gasPrice, gasLimit, binary, encodedConstructor, BigInteger.ZERO); } public static RemoteCall deployRemoteCall( Class type, YOUChain youChain, Credentials credentials, ContractGasProvider contractGasProvider, String binary, String encodedConstructor, BigInteger value) { return new RemoteCall<>(() -> deploy( type, youChain, credentials, contractGasProvider, binary, encodedConstructor, value)); } public static RemoteCall deployRemoteCall( Class type, YOUChain youChain, Credentials credentials, ContractGasProvider contractGasProvider, String binary, String encodedConstructor) { return new RemoteCall<>(() -> deploy( type, youChain, credentials, contractGasProvider, binary, encodedConstructor, BigInteger.ZERO)); } public static RemoteCall deployRemoteCall( Class type, YOUChain youChain, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, String binary, String encodedConstructor, BigInteger value) { return new RemoteCall<>(() -> deploy( type, youChain, transactionManager, gasPrice, gasLimit, binary, encodedConstructor, value)); } public static RemoteCall deployRemoteCall( Class type, YOUChain youChain, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, String binary, String encodedConstructor) { return deployRemoteCall( type, youChain, transactionManager, gasPrice, gasLimit, binary, encodedConstructor, BigInteger.ZERO); } public static RemoteCall deployRemoteCall( Class type, YOUChain youChain, TransactionManager transactionManager, ContractGasProvider contractGasProvider, String binary, String encodedConstructor, BigInteger value) { return new RemoteCall<>(() -> deploy( type, youChain, transactionManager, contractGasProvider, binary, encodedConstructor, value)); } public static RemoteCall deployRemoteCall( Class type, YOUChain youChain, TransactionManager transactionManager, ContractGasProvider contractGasProvider, String binary, String encodedConstructor) { return new RemoteCall<>(() -> deploy( type, youChain, transactionManager, contractGasProvider, binary, encodedConstructor, BigInteger.ZERO)); } public static EventValues staticExtractEventParameters( Event event, Log log) { final List topics = log.getTopics(); String encodedEventSignature = EventEncoder.encode(event); if (topics == null || topics.size() == 0 || !topics.get(0).equals(encodedEventSignature)) { return null; } List indexedValues = new ArrayList<>(); List nonIndexedValues = FunctionReturnDecoder.decode( log.getData(), event.getNonIndexedParameters()); List> indexedParameters = event.getIndexedParameters(); for (int i = 0; i < indexedParameters.size(); i++) { Type value = FunctionReturnDecoder.decodeIndexedValue( topics.get(i + 1), indexedParameters.get(i)); indexedValues.add(value); } return new EventValues(indexedValues, nonIndexedValues); } protected EventValues extractEventParameters(Event event, Log log) { return staticExtractEventParameters(event, log); } protected List extractEventParameters( Event event, TransactionReceipt transactionReceipt) { return transactionReceipt.getLogs().stream() .map(log -> extractEventParameters(event, log)) .filter(Objects::nonNull) .collect(Collectors.toList()); } protected EventValuesWithLog extractEventParametersWithLog(Event event, Log log) { final EventValues eventValues = staticExtractEventParameters(event, log); return (eventValues == null) ? null : new EventValuesWithLog(eventValues, log); } protected List extractEventParametersWithLog( Event event, TransactionReceipt transactionReceipt) { return transactionReceipt.getLogs().stream() .map(log -> extractEventParametersWithLog(event, log)) .filter(Objects::nonNull) .collect(Collectors.toList()); } /** * Subclasses should implement this method to return pre-existing addresses for deployed * contracts. * * @param networkId the network id, for example "1" for the main-net, "3" for ropsten, etc. * @return the deployed address of the contract, if known, and null otherwise. */ protected String getStaticDeployedAddress(String networkId) { return null; } public final void setDeployedAddress(String networkId, String address) { if (deployedAddresses == null) { deployedAddresses = new HashMap<>(); } deployedAddresses.put(networkId, address); } public final String getDeployedAddress(String networkId) { String addr = null; if (deployedAddresses != null) { addr = deployedAddresses.get(networkId); } return addr == null ? getStaticDeployedAddress(networkId) : addr; } /** * Adds a log field to {@link EventValues}. */ public static class EventValuesWithLog { private final EventValues eventValues; private final Log log; private EventValuesWithLog(EventValues eventValues, Log log) { this.eventValues = eventValues; this.log = log; } public List getIndexedValues() { return eventValues.getIndexedValues(); } public List getNonIndexedValues() { return eventValues.getNonIndexedValues(); } public Log getLog() { return log; } } @SuppressWarnings("unchecked") protected static List convertToNative(List arr) { List out = new ArrayList(); for (Iterator it = arr.iterator(); it.hasNext(); ) { out.add((T) it.next().getValue()); } return out; } }