Search in sources :

Example 1 with EthSendTransaction

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

the class EthereumUtils method deployContract.

public static String deployContract(Credentials credentials, Web3j web3j, String contractBinary, int toSendEther, boolean waitForReceipt, int sleepDuration, int attempts) throws Exception {
    BigInteger depositEtherAmountToSend = BigInteger.valueOf(toSendEther);
    RawTransaction rawTransaction = RawTransaction.createContractTransaction(getNextNonce(credentials.getAddress(), web3j), DEFAULT_GAS_PRICE, DEFAULT_GAS_LIMIT, depositEtherAmountToSend, contractBinary);
    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
    String hexValue = Numeric.toHexString(signedMessage);
    EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
    if (waitForReceipt) {
        TransactionReceipt transReceipt = waitForTransactionReceipt(ethSendTransaction.getTransactionHash(), sleepDuration, attempts, web3j);
        if (transReceipt != null) {
            return transReceipt.getContractAddress();
        }
    }
    // we dont have a contract address
    logger.warn("Unable to retrieve contract address.");
    return null;
}
Also used : EthSendTransaction(org.web3j.protocol.core.methods.response.EthSendTransaction) RawTransaction(org.web3j.crypto.RawTransaction) TransactionReceipt(org.web3j.protocol.core.methods.response.TransactionReceipt) EthGetTransactionReceipt(org.web3j.protocol.core.methods.response.EthGetTransactionReceipt) BigInteger(java.math.BigInteger)

Example 2 with EthSendTransaction

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

the class EthereumWorkitemHandlerTest method setUp.

@Before
public void setUp() {
    try {
        when(web3j.ethGetBalance(anyString(), any(DefaultBlockParameterName.class))).thenReturn(balanceRequest);
        when(balanceRequest.send()).thenReturn(ethGetBalance);
        when(ethGetBalance.getBalance()).thenReturn(BigInteger.valueOf(100));
        when(transfer.sendFunds(anyString(), any(BigDecimal.class), any(Unit.class))).thenReturn(transferRequest);
        when(transferRequest.send()).thenReturn(transactionReceipt);
        when(transactionReceipt.getStatus()).thenReturn("testStatus");
        when(transactionReceipt.getBlockHash()).thenReturn("testBlockHash");
        when(transactionReceipt.getBlockNumber()).thenReturn(BigInteger.valueOf(1));
        when(web3j.ethCall(any(Transaction.class), any(DefaultBlockParameterName.class))).thenReturn(ethCallRequest);
        when(ethCallRequest.sendAsync()).thenReturn(ethCallCompletable);
        when(ethCallCompletable.get()).thenReturn(ethCall);
        when(ethCall.getValue()).thenReturn("testResultValue");
        when(web3j.ethGetTransactionCount(anyString(), any(DefaultBlockParameterName.class))).thenReturn(ethCountRequest);
        when(ethCountRequest.sendAsync()).thenReturn(ethCountCompletable);
        when(ethCountCompletable.get()).thenReturn(transactionCount);
        when(transactionCount.getTransactionCount()).thenReturn(BigInteger.valueOf(10));
        when(web3j.ethSendTransaction(any(Transaction.class))).thenReturn(ethSendRequest);
        when(ethSendRequest.sendAsync()).thenReturn(ethSendCompletable);
        when(ethSendCompletable.get()).thenReturn(ethSendTransaction);
        when(ethSendTransaction.getTransactionHash()).thenReturn("testTransactionHash");
        when(web3j.ethSendRawTransaction(anyString())).thenReturn(ethSendRawRequest);
        when(ethSendRawRequest.sendAsync()).thenReturn(ethSendRawCompletable);
        when(ethSendRawCompletable.get()).thenReturn(ethSendRawTransaction);
        when(ethSendRawTransaction.getTransactionHash()).thenReturn("testTransactionHash");
        when(web3j.ethGetTransactionReceipt(anyString())).thenReturn(ethSendTransactionReceipt);
        when(ethSendTransactionReceipt.send()).thenReturn(ethGetTransactionReceipt);
        when(ethGetTransactionReceipt.getTransactionReceipt()).thenReturn(Optional.of(rawTransactionalRecept));
        when(ethGetTransactionReceipt.getResult()).thenReturn(rawTransactionalRecept);
        when(rawTransactionalRecept.getContractAddress()).thenReturn("testContractAddress");
        signalList = new ArrayList();
        doAnswer(new Answer() {

            public Void answer(InvocationOnMock invocation) {
                signalList.add(Arrays.toString(invocation.getArguments()));
                return null;
            }
        }).when(kieSession).signalEvent(anyString(), any(Object.class));
        Observable logObservable = PowerMockito.mock(Observable.class);
        when(web3j.ethLogObservable(any(EthFilter.class))).thenReturn(logObservable);
        when(logObservable.subscribe()).thenReturn(subscription);
        when(logObservable.subscribe(any(Action1.class))).thenReturn(subscription);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : EthFilter(org.web3j.protocol.core.methods.request.EthFilter) Action1(rx.functions.Action1) ArrayList(java.util.ArrayList) Unit(org.web3j.utils.Convert.Unit) BigDecimal(java.math.BigDecimal) Observable(rx.Observable) Answer(org.mockito.stubbing.Answer) Transaction(org.web3j.protocol.core.methods.request.Transaction) EthSendTransaction(org.web3j.protocol.core.methods.response.EthSendTransaction) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultBlockParameterName(org.web3j.protocol.core.DefaultBlockParameterName) Before(org.junit.Before)

Example 3 with EthSendTransaction

use of org.web3j.protocol.core.methods.response.EthSendTransaction 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 4 with EthSendTransaction

use of org.web3j.protocol.core.methods.response.EthSendTransaction 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

EthSendTransaction (org.web3j.protocol.core.methods.response.EthSendTransaction)4 BigInteger (java.math.BigInteger)3 Transaction (org.web3j.protocol.core.methods.request.Transaction)3 BigDecimal (java.math.BigDecimal)2 RawTransaction (org.web3j.crypto.RawTransaction)2 EthGetTransactionReceipt (org.web3j.protocol.core.methods.response.EthGetTransactionReceipt)2 TransactionReceipt (org.web3j.protocol.core.methods.response.TransactionReceipt)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1 Web3jAccount (org.nutz.boot.starter.web3.Web3jAccount)1 NutMap (org.nutz.lang.util.NutMap)1 At (org.nutz.mvc.annotation.At)1 DefaultBlockParameterName (org.web3j.protocol.core.DefaultBlockParameterName)1 EthFilter (org.web3j.protocol.core.methods.request.EthFilter)1 Unit (org.web3j.utils.Convert.Unit)1 Observable (rx.Observable)1 Action1 (rx.functions.Action1)1