Search in sources :

Example 1 with Transaction

use of org.web3j.protocol.core.methods.request.Transaction in project jbpm-work-items by kiegroup.

the class EthereumUtils method queryExistingContract.

public static Object queryExistingContract(Credentials credentials, Web3j web3j, String contractAddress, String contractMethodName, List<Type> contractMethodInputTypes, List<TypeReference<?>> contractMethodOutputTypes) throws Exception {
    Function function = getFunction(contractMethodName, contractMethodInputTypes, contractMethodOutputTypes);
    Transaction transaction = Transaction.createEthCallTransaction(credentials.getAddress(), contractAddress, getEncodedFunction(function));
    EthCall response = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get();
    List<Type> responseTypeList = FunctionReturnDecoder.decode(response.getValue(), function.getOutputParameters());
    if (responseTypeList != null && responseTypeList.size() > 0) {
        return responseTypeList.get(0).getValue();
    } else {
        return null;
    }
}
Also used : Function(org.web3j.abi.datatypes.Function) Type(org.web3j.abi.datatypes.Type) Transaction(org.web3j.protocol.core.methods.request.Transaction) RawTransaction(org.web3j.crypto.RawTransaction) EthSendTransaction(org.web3j.protocol.core.methods.response.EthSendTransaction) EthCall(org.web3j.protocol.core.methods.response.EthCall)

Example 2 with Transaction

use of org.web3j.protocol.core.methods.request.Transaction in project nutzboot by nutzam.

the class EthModule method sendTransaction.

// @POST
@At("/eth/sendTransaction/?/?")
public NutMap sendTransaction(String from, String to, @Param("wei") double wei) {
    // from 必须是本地账号
    // to 必须是address
    NutMap re = new NutMap();
    // 检查转账金额
    if (wei < 0.01) {
        re.put("msg", "起码转账 0.01 eth");
        return re;
    }
    if (wei > 100) {
        re.put("msg", "最多转账 100 eth");
        return re;
    }
    Web3jAccount account = web3jCredentials.get(from);
    if (account == null) {
        return re.setv("msg", "不存在这个本地账号: " + from);
    }
    // 发起转账
    BigInteger value = Convert.toWei(new BigDecimal(wei), Convert.Unit.ETHER).toBigInteger();
    Transaction transaction = Transaction.createEtherTransaction(account.getAddress(), null, null, null, to, value);
    try {
        EthSendTransaction est = web3jAdmin.personalSendTransaction(transaction, account.getPassword()).send();
        String hash = est.getTransactionHash();
        return re.setv("ok", true).setv("hash", hash);
    } catch (Exception e) {
        log.warn("转账失败!!!", e);
        return re.setv("msg", e.getMessage());
    }
}
Also used : EthSendTransaction(org.web3j.protocol.core.methods.response.EthSendTransaction) Web3jAccount(org.nutz.boot.starter.web3.Web3jAccount) Transaction(org.web3j.protocol.core.methods.request.Transaction) EthSendTransaction(org.web3j.protocol.core.methods.response.EthSendTransaction) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) IOException(java.io.IOException) NutMap(org.nutz.lang.util.NutMap) At(org.nutz.mvc.annotation.At)

Example 3 with Transaction

use of org.web3j.protocol.core.methods.request.Transaction in project jbpm-work-items by kiegroup.

the class EthereumUtils method transactExistingContract.

public static TransactionReceipt transactExistingContract(Credentials credentials, Web3j web3j, int etherAmount, BigInteger gasPrice, BigInteger gasLimit, String toAddress, String methodName, List<Type> methodInputTypes, List<TypeReference<?>> methodOutputTypes, boolean waitForReceipt, int sleepDuration, int attempts) throws Exception {
    BigInteger etherAmountToSend = BigInteger.valueOf(etherAmount);
    Transaction transaction = Transaction.createFunctionCallTransaction(credentials.getAddress(), getNextNonce(credentials.getAddress(), web3j), gasPrice, gasLimit, toAddress, etherAmountToSend, getEncodedFunction(methodName, methodInputTypes, methodOutputTypes));
    EthSendTransaction transactionResponse = web3j.ethSendTransaction(transaction).sendAsync().get();
    if (waitForReceipt) {
        TransactionReceipt transReceipt = waitForTransactionReceipt(transactionResponse.getTransactionHash(), sleepDuration, attempts, web3j);
        return transReceipt;
    }
    // we dont have a transaction receipt
    logger.warn("Unable to retrieve transaction receipt.");
    return null;
}
Also used : EthSendTransaction(org.web3j.protocol.core.methods.response.EthSendTransaction) Transaction(org.web3j.protocol.core.methods.request.Transaction) RawTransaction(org.web3j.crypto.RawTransaction) EthSendTransaction(org.web3j.protocol.core.methods.response.EthSendTransaction) TransactionReceipt(org.web3j.protocol.core.methods.response.TransactionReceipt) EthGetTransactionReceipt(org.web3j.protocol.core.methods.response.EthGetTransactionReceipt) BigInteger(java.math.BigInteger)

Aggregations

Transaction (org.web3j.protocol.core.methods.request.Transaction)3 EthSendTransaction (org.web3j.protocol.core.methods.response.EthSendTransaction)3 BigInteger (java.math.BigInteger)2 RawTransaction (org.web3j.crypto.RawTransaction)2 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 Web3jAccount (org.nutz.boot.starter.web3.Web3jAccount)1 NutMap (org.nutz.lang.util.NutMap)1 At (org.nutz.mvc.annotation.At)1 Function (org.web3j.abi.datatypes.Function)1 Type (org.web3j.abi.datatypes.Type)1 EthCall (org.web3j.protocol.core.methods.response.EthCall)1 EthGetTransactionReceipt (org.web3j.protocol.core.methods.response.EthGetTransactionReceipt)1 TransactionReceipt (org.web3j.protocol.core.methods.response.TransactionReceipt)1