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

io.web3service.web3j.contract.ContractClient Maven / Gradle / Ivy

The newest version!
package io.web3service.web3j.contract;

import io.web3service.web3j.core.Web3jNetworkService;
import lombok.SneakyThrows;
import org.springframework.util.Assert;
import org.web3j.protocol.Web3j;
import org.web3j.tx.Contract;
import org.web3j.tx.ReadonlyTransactionManager;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.DefaultGasProvider;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;

/**
  * @author   github.com/x-saofen
  */
public class ContractClient {

    @SneakyThrows({NoSuchMethodException.class, InvocationTargetException.class , InstantiationException.class, IllegalAccessException.class})
    public static  T loadContract(Web3jNetworkService web3j, String contractAddress, Class type)  {
        Optional contractBinary = web3j.getContractBinary(contractAddress);
        Assert.isTrue(contractBinary.isPresent(), "Failed to get contract bytecode.");
        Constructor constructor = type.getConstructor(String.class, String.class, Web3j.class, TransactionManager.class, ContractGasProvider.class);
        return (T) constructor.newInstance(contractBinary.get(), contractAddress, web3j, new ReadonlyTransactionManager(web3j, contractAddress), new DefaultGasProvider());
    }



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy