Search in sources :

Example 1 with TransactionReceipt

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

the class Ethereum method main.

public static void main(String[] args) throws Exception {
    // 初始化Service
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();
    Thread.sleep(3000);
    System.out.println("开始测试...");
    System.out.println("===================================================================");
    logger.info("初始化AOMP的ChannelEthereumService");
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    // 使用AMOP消息信道初始化web3j
    Web3j web3 = Web3j.build(channelEthereumService);
    logger.info("调用web3的getBlockNumber接口");
    EthBlockNumber ethBlockNumber = web3.ethBlockNumber().sendAsync().get();
    logger.info("获取ethBlockNumber:{}", ethBlockNumber);
    // 初始化交易签名私钥
    ECKeyPair keyPair = Keys.createEcKeyPair();
    Credentials credentials = Credentials.create(keyPair);
    // 初始化交易参数
    java.math.BigInteger gasPrice = new BigInteger("30000000");
    java.math.BigInteger gasLimit = new BigInteger("30000000");
    java.math.BigInteger initialWeiValue = new BigInteger("0");
    // 部署合约
    Ok ok = Ok.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("Ok getContractAddress " + ok.getContractAddress());
    // 调用合约接口
    java.math.BigInteger Num = new BigInteger("999");
    Uint256 num = new Uint256(Num);
    TransactionReceipt receipt = ok.trans(num).get();
    System.out.println("receipt transactionHash" + receipt.getTransactionHash());
    // 查询合约数据
    num = ok.get().get();
    System.out.println("ok.get() " + num.getValue());
}
Also used : EthBlockNumber(org.bcos.web3j.protocol.core.methods.response.EthBlockNumber) ECKeyPair(org.bcos.web3j.crypto.ECKeyPair) TransactionReceipt(org.bcos.web3j.protocol.core.methods.response.TransactionReceipt) Service(org.bcos.channel.client.Service) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Web3j(org.bcos.web3j.protocol.Web3j) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) Uint256(org.bcos.web3j.abi.datatypes.generated.Uint256) Credentials(org.bcos.web3j.crypto.Credentials)

Example 2 with TransactionReceipt

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

the class Contract method create.

private static <T extends Contract> T create(T contract, String binary, String encodedConstructor, BigInteger value) throws InterruptedException, IOException, TransactionTimeoutException {
    TransactionReceipt transactionReceipt = contract.executeTransaction(binary + encodedConstructor, value, creatType);
    String contractAddress = transactionReceipt.getContractAddress();
    if (contractAddress == null) {
        throw new RuntimeException("Empty contract address returned");
    }
    contract.setContractAddress(contractAddress);
    contract.setTransactionReceipt(transactionReceipt);
    return contract;
}
Also used : TransactionReceipt(org.bcos.web3j.protocol.core.methods.response.TransactionReceipt)

Example 3 with TransactionReceipt

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

the class LoadTestRunner method main.

public static void main(String[] args) throws Exception {
    logger.debug("初始化");
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();
    System.out.println("3s后开始测试...");
    Thread.sleep(1000);
    System.out.println("2s后开始测试...");
    Thread.sleep(1000);
    System.out.println("1s后开始测试...");
    Thread.sleep(1000);
    System.out.println("开始测试");
    System.out.println("===================================================================");
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    channelEthereumService.setTimeout(10000);
    Web3j web3 = Web3j.build(channelEthereumService);
    Thread.sleep(2000);
    // 先部署一个测试合约
    ECKeyPair keyPair = Keys.createEcKeyPair();
    Credentials credentials = Credentials.create(keyPair);
    RawTransactionManager rawTransactionManager = new RawTransactionManager(web3, credentials);
    java.math.BigInteger gasPrice = new BigInteger("30000000");
    java.math.BigInteger gasLimit = new BigInteger("30000000");
    java.math.BigInteger initialWeiValue = new BigInteger("0");
    LoadTest loadTest = LoadTest.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("LoadTest getContractAddress " + loadTest.getContractAddress());
    // 发送交易
    for (Integer i = 0; i < 10; ++i) {
        Thread txThread = new Thread() {

            public void run() {
                try {
                    while (true) {
                        Thread.sleep(1000);
                        // TransactionReceipt receipt = loadTest.addCounter(new Utf8String(UUID.randomUUID().toString()), new Int256(1)).get();
                        loadTest.addCounter(new Utf8String(UUID.randomUUID().toString()), new Int256(1));
                    }
                } catch (Exception e) {
                    logger.error("系统错误", e);
                }
            }
        };
        txThread.start();
    }
    // 读取合约
    for (Integer i = 0; i < 10; ++i) {
        Thread callThread = new Thread() {

            public void run() {
                try {
                    while (true) {
                        Address origin = loadTest._origin().get();
                        Address sender = loadTest._sender().get();
                        logger.info("storage sender:{} origin:{}", sender.toString(), origin.toString());
                    }
                } catch (Exception e) {
                    logger.error("系统错误", e);
                }
            }
        };
        callThread.start();
    }
    // 200个线程,发链上链下消息
    for (Integer i = 0; i < 10; ++i) {
        Thread messageThread = new Thread() {

            public void run() {
                try {
                    while (true) {
                        ChannelRequest request = new ChannelRequest();
                        request.setToTopic("test");
                        request.setFromOrg("WB");
                        request.setToOrg("EB");
                        request.setMessageID(service.newSeq());
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < 500; ++i) {
                            sb.append("hello world!:");
                        }
                        request.setContent(sb.toString() + request.getMessageID());
                        request.setTimeout(2000);
                        // ChannelResponse response = service.sendChannelMessage2(request);
                        logger.info("发送链上链下消息:{}", request.getMessageID());
                        ChannelResponse response = service.sendChannelMessage(request);
                        logger.info("收到链上链下响应:{} {}", response.getMessageID(), response.getErrorCode());
                    }
                } catch (Exception e) {
                    logger.error("系统错误", e);
                }
            }
        };
        messageThread.start();
    }
    for (int i = 0; i < 10; ++i) {
        TransactionReceipt receipt = loadTest.addCounter(new Utf8String(UUID.randomUUID().toString()), new Int256(1)).get();
        Address origin = loadTest._origin().get();
        Address sender = loadTest._sender().get();
        List<Type> addresses = loadTest.constGetInf().get();
        logger.info("const sender:{} origin:{}", (((Address) addresses.get(0)).toString()), (((Address) addresses.get(1)).toString()));
        logger.info("storage sender:{} origin:{}", sender.toString(), origin.toString());
    }
}
Also used : ChannelRequest(org.bcos.channel.dto.ChannelRequest) Address(org.bcos.web3j.abi.datatypes.Address) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BigInteger(java.math.BigInteger) ECKeyPair(org.bcos.web3j.crypto.ECKeyPair) TransactionReceipt(org.bcos.web3j.protocol.core.methods.response.TransactionReceipt) Service(org.bcos.channel.client.Service) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) RawTransactionManager(org.bcos.web3j.tx.RawTransactionManager) ChannelResponse(org.bcos.channel.dto.ChannelResponse) BigInteger(java.math.BigInteger) Utf8String(org.bcos.web3j.abi.datatypes.Utf8String) Int256(org.bcos.web3j.abi.datatypes.generated.Int256) Web3j(org.bcos.web3j.protocol.Web3j) Type(org.bcos.web3j.abi.datatypes.Type) BigInteger(java.math.BigInteger) Credentials(org.bcos.web3j.crypto.Credentials)

Example 4 with TransactionReceipt

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

the class SolidityFunctionWrapper method buildEventTransactionReceiptFunction.

static MethodSpec buildEventTransactionReceiptFunction(String responseClassName, String functionName, List<NamedTypeName> indexedParameters, List<NamedTypeName> nonIndexedParameters) throws ClassNotFoundException {
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(List.class), ClassName.get("", responseClassName));
    String generatedFunctionName = "get" + Strings.capitaliseFirstLetter(functionName) + "Events";
    MethodSpec.Builder transactionMethodBuilder = MethodSpec.methodBuilder(generatedFunctionName).addModifiers(Modifier.PUBLIC).addModifiers(Modifier.STATIC).addParameter(TransactionReceipt.class, "transactionReceipt").returns(parameterizedTypeName);
    buildVariableLengthEventConstructor(transactionMethodBuilder, functionName, indexedParameters, nonIndexedParameters);
    transactionMethodBuilder.addStatement("$T valueList = extractEventParameters(event, " + "transactionReceipt)", ParameterizedTypeName.get(List.class, EventValues.class)).addStatement("$1T responses = new $1T(valueList.size())", ParameterizedTypeName.get(ClassName.get(ArrayList.class), ClassName.get("", responseClassName))).beginControlFlow("for ($T eventValues : valueList)", EventValues.class).addStatement("$1T typedResponse = new $1T()", ClassName.get("", responseClassName)).addCode(buildTypedResponse("typedResponse", indexedParameters, nonIndexedParameters)).addStatement("responses.add(typedResponse)").endControlFlow();
    transactionMethodBuilder.addStatement("return responses");
    return transactionMethodBuilder.build();
}
Also used : EventValues(org.bcos.web3j.abi.EventValues) MethodSpec(com.squareup.javapoet.MethodSpec) TransactionReceipt(org.bcos.web3j.protocol.core.methods.response.TransactionReceipt) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 5 with TransactionReceipt

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

the class WalletSendFunds method run.

private void run(String walletFileLocation, String destinationAddress) {
    File walletFile = new File(walletFileLocation);
    Credentials credentials = getCredentials(walletFile);
    console.printf("Wallet for address " + credentials.getAddress() + " loaded\n");
    if (!WalletUtils.isValidAddress(destinationAddress)) {
        exitError("Invalid destination address specified");
    }
    Web3j web3j = getEthereumClient();
    BigDecimal amountToTransfer = getAmountToTransfer();
    Convert.Unit transferUnit = getTransferUnit();
    BigDecimal amountInWei = Convert.toWei(amountToTransfer, transferUnit);
    confirmTransfer(amountToTransfer, transferUnit, amountInWei, destinationAddress);
    TransactionReceipt transactionReceipt = performTransfer(web3j, destinationAddress, credentials, amountInWei);
    console.printf("Funds have been successfully transferred from %s to %s%n" + "Transaction hash: %s%nMined block number: %s%n", credentials.getAddress(), destinationAddress, transactionReceipt.getTransactionHash(), transactionReceipt.getBlockNumber());
}
Also used : Web3j(org.bcos.web3j.protocol.Web3j) Convert(org.bcos.web3j.utils.Convert) TransactionReceipt(org.bcos.web3j.protocol.core.methods.response.TransactionReceipt) File(java.io.File) Credentials(org.bcos.web3j.crypto.Credentials) BigDecimal(java.math.BigDecimal)

Aggregations

TransactionReceipt (org.bcos.web3j.protocol.core.methods.response.TransactionReceipt)5 Credentials (org.bcos.web3j.crypto.Credentials)3 Web3j (org.bcos.web3j.protocol.Web3j)3 BigInteger (java.math.BigInteger)2 Service (org.bcos.channel.client.Service)2 ECKeyPair (org.bcos.web3j.crypto.ECKeyPair)2 ChannelEthereumService (org.bcos.web3j.protocol.channel.ChannelEthereumService)2 ApplicationContext (org.springframework.context.ApplicationContext)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 MethodSpec (com.squareup.javapoet.MethodSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 File (java.io.File)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ChannelRequest (org.bcos.channel.dto.ChannelRequest)1 ChannelResponse (org.bcos.channel.dto.ChannelResponse)1 EventValues (org.bcos.web3j.abi.EventValues)1 Address (org.bcos.web3j.abi.datatypes.Address)1 Type (org.bcos.web3j.abi.datatypes.Type)1