Search in sources :

Example 1 with SendTransaction

use of org.fisco.bcos.web3j.protocol.core.methods.response.SendTransaction in project web3sdk by FISCO-BCOS.

the class ChannelEthereumService method sendOnly.

@Override
public void sendOnly(Request request) throws IOException {
    byte[] payload = objectMapper.writeValueAsBytes(request);
    BcosRequest fiscoRequest = new BcosRequest();
    fiscoRequest.setKeyID(channelService.getOrgID());
    fiscoRequest.setBankNO("");
    fiscoRequest.setContent(new String(payload));
    fiscoRequest.setMessageID(channelService.newSeq());
    if (timeout != 0) {
        fiscoRequest.setTimeout(timeout);
    }
    if (!request.isNeedTransCallback()) {
        channelService.asyncSendEthereumMessage(fiscoRequest, new BcosResponseCallback() {

            @Override
            public void onResponse(BcosResponse response) {
                try {
                    logger.debug("fisco Request:{} {}", fiscoRequest.getMessageID(), objectMapper.writeValueAsString(request));
                    logger.debug("fisco Response:{} {} {}", fiscoRequest.getMessageID(), response.getErrorCode(), response.getContent());
                    if (response.getErrorCode() != 0) {
                        logger.error("Error: " + response.getErrorCode());
                    }
                } catch (Exception e) {
                    logger.error("Error: ", e);
                }
            }
        });
    } else {
        channelService.asyncSendEthereumMessage(fiscoRequest, new BcosResponseCallback() {

            @Override
            public void onResponse(BcosResponse response) {
                try {
                    logger.debug("fisco Request:{} {}", fiscoRequest.getMessageID(), objectMapper.writeValueAsString(request));
                    logger.debug("fisco Response:{} {} {}", fiscoRequest.getMessageID(), response.getErrorCode(), response.getContent());
                    if (response.getErrorCode() == 0) {
                        // SendTransaction
                        SendTransaction sendTransaction = objectMapper.readValue(response.getContent(), SendTransaction.class);
                        if (sendTransaction.getError() == null) {
                            logger.debug("sendRawTransaction response ok, transaction hash: {} ", sendTransaction.getResult());
                        } else {
                            TransactionReceipt receipt = new TransactionReceipt();
                            receipt.setStatus(String.valueOf(sendTransaction.getError().getCode()));
                            receipt.setMessage(sendTransaction.getError().getMessage());
                            // optional code
                            if (channelService.getThreadPool() == null) {
                                channelService.onReceiveTransactionMessage(fiscoRequest.getMessageID(), receipt);
                            } else {
                                // Execute the callback function in the thread pool
                                channelService.getThreadPool().execute(new Runnable() {

                                    @Override
                                    public void run() {
                                        channelService.onReceiveTransactionMessage(fiscoRequest.getMessageID(), receipt);
                                    }
                                });
                            }
                            logger.debug(" sendRawTransaction response not ok, code: {}, message: {} ", receipt.getStatus(), receipt.getMessage());
                        }
                    }
                } catch (Exception e) {
                    logger.error("Error: ", e);
                }
            }
        }, request.getTransactionSucCallback());
    }
}
Also used : BcosResponseCallback(org.fisco.bcos.channel.client.BcosResponseCallback) BcosResponse(org.fisco.bcos.channel.dto.BcosResponse) TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) SendTransaction(org.fisco.bcos.web3j.protocol.core.methods.response.SendTransaction) BcosRequest(org.fisco.bcos.channel.dto.BcosRequest) IOException(java.io.IOException) MessageDecodingException(org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException) ContractCallException(org.fisco.bcos.web3j.tx.exceptions.ContractCallException)

Example 2 with SendTransaction

use of org.fisco.bcos.web3j.protocol.core.methods.response.SendTransaction in project web3sdk by FISCO-BCOS.

the class ExtendedRawTransactionManager method signAndSend.

public SendTransaction signAndSend(ExtendedRawTransaction rawTransaction) throws IOException {
    String signedTransaction = sign(rawTransaction);
    SendTransaction result = sendTransaction(signedTransaction);
    return result;
}
Also used : SendTransaction(org.fisco.bcos.web3j.protocol.core.methods.response.SendTransaction)

Example 3 with SendTransaction

use of org.fisco.bcos.web3j.protocol.core.methods.response.SendTransaction in project web3sdk by FISCO-BCOS.

the class ExtendedRawTransactionManager method sendTransaction.

@Override
public SendTransaction sendTransaction(String signedTransaction) throws IOException, TxHashMismatchException {
    SendTransaction sendTransaction = web3j.sendRawTransaction(signedTransaction).send();
    if (sendTransaction != null && !sendTransaction.hasError()) {
        String txHashLocal = Hash.sha3(signedTransaction);
        String txHashRemote = sendTransaction.getTransactionHash();
        if (!txHashVerifier.verify(txHashLocal, txHashRemote)) {
            throw new TxHashMismatchException(txHashLocal, txHashRemote);
        }
    }
    return sendTransaction;
}
Also used : SendTransaction(org.fisco.bcos.web3j.protocol.core.methods.response.SendTransaction) TxHashMismatchException(org.fisco.bcos.web3j.tx.exceptions.TxHashMismatchException)

Example 4 with SendTransaction

use of org.fisco.bcos.web3j.protocol.core.methods.response.SendTransaction in project web3sdk by FISCO-BCOS.

the class ManagedTransactionTester method prepareTransactionRequest.

@SuppressWarnings("unchecked")
void prepareTransactionRequest() throws IOException {
    SendTransaction sendTransaction = new SendTransaction();
    sendTransaction.setResult(TRANSACTION_HASH);
    Request<?, SendTransaction> rawTransactionRequest = mock(Request.class);
    when(rawTransactionRequest.send()).thenReturn(sendTransaction);
    when(web3j.sendRawTransaction(any(String.class))).thenReturn((Request) rawTransactionRequest);
}
Also used : SendTransaction(org.fisco.bcos.web3j.protocol.core.methods.response.SendTransaction)

Example 5 with SendTransaction

use of org.fisco.bcos.web3j.protocol.core.methods.response.SendTransaction in project web3sdk by FISCO-BCOS.

the class ExtendedRawTransactionManager method signAndSend.

public SendTransaction signAndSend(ExtendedRawTransaction rawTransaction, TransactionSucCallback callback) throws IOException {
    String signedTransaction = sign(rawTransaction);
    SendTransaction result = sendTransaction(signedTransaction, callback);
    return result;
}
Also used : SendTransaction(org.fisco.bcos.web3j.protocol.core.methods.response.SendTransaction)

Aggregations

SendTransaction (org.fisco.bcos.web3j.protocol.core.methods.response.SendTransaction)6 TxHashMismatchException (org.fisco.bcos.web3j.tx.exceptions.TxHashMismatchException)2 IOException (java.io.IOException)1 BcosResponseCallback (org.fisco.bcos.channel.client.BcosResponseCallback)1 BcosRequest (org.fisco.bcos.channel.dto.BcosRequest)1 BcosResponse (org.fisco.bcos.channel.dto.BcosResponse)1 TransactionReceipt (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt)1 MessageDecodingException (org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException)1 ContractCallException (org.fisco.bcos.web3j.tx.exceptions.ContractCallException)1