美文网首页
[快速入门]使用web3j调用besu上的智能合约

[快速入门]使用web3j调用besu上的智能合约

作者: 张亚伦 | 来源:发表于2021-07-02 11:03 被阅读0次

    原理汇总:

    1. 发送调用合约的交易(eth_sendRawTransaction)
    2. 获取交易的收据(eth_getTransactionReceipt)
    3. 判断操作是否成功 (解析收据里的事件日志内容信息)

    1. 常规调用(同步模式)

    示例:

     private static void Set(String address, String value) throws Exception {
            OperateSimpleStorage demo = new OperateSimpleStorage();
            SimpleStorage contract = demo.loadContract(address);
            printDate("set() -start:");
            TransactionReceipt receipt = contract.set(new BigInteger(value)).send();
            printDate("set() -end:");
            System.out.println(objectToJson(receipt));
            if (receipt.isStatusOK()) {
                printDate("set()-SetEvent -start:");
                List<SimpleStorage.SetEventEventResponse> resp = contract.getSetEventEvents(receipt);
                System.out.println(resp.no);
                printDate("set()-SetEvent -end:");
            }
        }
    

    日志:

    address----------------------------------0xC5bbA68089785E3bC6B4a300C87A88124af21A4f
    09:36:15.851 [main] INFO com.red.udpn.did.service.besuChain.engine.OperateSimpleStorage - loadContract()----------------------address:0xC5bbA68089785E3bC6B4a300C87A88124af21A4f
    set() -start:Fri Jul 02 09:36:15 CST 2021
    
    09:36:30.095 [main] DEBUG org.web3j.protocol.http.HttpService - --> POST http://192.168.1.173:20000/
    09:36:30.095 [main] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json; charset=utf-8
    09:36:30.095 [main] DEBUG org.web3j.protocol.http.HttpService - Content-Length: 125
    09:36:30.095 [main] DEBUG org.web3j.protocol.http.HttpService - 
    09:36:30.097 [main] DEBUG org.web3j.protocol.http.HttpService - {"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0xfe3b557e8fb62b89f4916b721be55ceb828dbd73","pending"],"id":0}
    09:36:30.097 [main] DEBUG org.web3j.protocol.http.HttpService - --> END POST (125-byte body)
    09:36:31.226 [main] DEBUG org.web3j.protocol.http.HttpService - <-- 200 OK http://192.168.1.173:20000/ (1128ms)
    09:36:31.227 [main] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json
    09:36:31.227 [main] DEBUG org.web3j.protocol.http.HttpService - 
    09:36:31.227 [main] DEBUG org.web3j.protocol.http.HttpService - {
      "jsonrpc" : "2.0",
      "id" : 0,
      "result" : "0x216b"
    }
    09:36:31.227 [main] DEBUG org.web3j.protocol.http.HttpService - <-- END HTTP (58-byte body)
    09:36:36.610 [main] DEBUG org.web3j.protocol.http.HttpService - --> POST http://192.168.1.173:20000/
    09:36:36.610 [main] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json; charset=utf-8
    09:36:36.610 [main] DEBUG org.web3j.protocol.http.HttpService - Content-Length: 346
    09:36:36.610 [main] DEBUG org.web3j.protocol.http.HttpService - 
    09:36:36.611 [main] DEBUG org.web3j.protocol.http.HttpService - {"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["0xf88682216b80834c4b4094c5bba68089785e3bc6b4a300c87a88124af21a4f80a460fe47b1000000000000000000000000000000000000000000000000000000000000029a1ba0010e3095a2e6e7bf869bbd03abb8b958f54549274f38c626873306c580aa2b7fa05f11933729a56ec20f1a8988e5cd1fd5e79bbf24f06484457d0c2e4fd6feef83"],"id":1}
    09:36:36.611 [main] DEBUG org.web3j.protocol.http.HttpService - --> END POST (346-byte body)
    09:36:36.829 [main] DEBUG org.web3j.protocol.http.HttpService - <-- 200 OK http://192.168.1.173:20000/ (218ms)
    09:36:36.829 [main] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json
    09:36:36.830 [main] DEBUG org.web3j.protocol.http.HttpService - 
    09:36:36.830 [main] DEBUG org.web3j.protocol.http.HttpService - {
      "jsonrpc" : "2.0",
      "id" : 1,
      "result" : "0x679dc2320db21d595e6619fb11661e74ea03435bc6e9933db02f61653bde86e7"
    }
    09:36:36.830 [main] DEBUG org.web3j.protocol.http.HttpService - <-- END HTTP (118-byte body)
    09:36:47.689 [main] DEBUG org.web3j.protocol.http.HttpService - --> POST http://192.168.1.173:20000/
    09:36:47.689 [main] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json; charset=utf-8
    09:36:47.689 [main] DEBUG org.web3j.protocol.http.HttpService - Content-Length: 141
    09:36:47.689 [main] DEBUG org.web3j.protocol.http.HttpService - 
    09:36:47.689 [main] DEBUG org.web3j.protocol.http.HttpService - {"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0x679dc2320db21d595e6619fb11661e74ea03435bc6e9933db02f61653bde86e7"],"id":2}
    09:36:47.689 [main] DEBUG org.web3j.protocol.http.HttpService - --> END POST (141-byte body)
    09:36:48.409 [main] DEBUG org.web3j.protocol.http.HttpService - <-- 200 OK http://192.168.1.173:20000/ (719ms)
    09:36:48.409 [main] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json
    09:36:48.410 [main] DEBUG org.web3j.protocol.http.HttpService - 
    09:36:48.410 [main] DEBUG org.web3j.protocol.http.HttpService - {
      "jsonrpc" : "2.0",
      "id" : 2,
      "result" : {
        "blockHash" : "0xaf1f7b9ebd97b378bb31c72f9e7612588a5e4805dc4ee407df468ddef802bdcc",
        "blockNumber" : "0x183a01",
        "contractAddress" : null,
        "cumulativeGasUsed" : "0x6deb",
        "from" : "0xfe3b557e8fb62b89f4916b721be55ceb828dbd73",
        "gasUsed" : "0x6deb",
        "logs" : [ {
          "address" : "0xc5bba68089785e3bc6b4a300c87a88124af21a4f",
          "topics" : [ "0x0de2d86113046b9e8bb6b785e96a6228f6803952bf53a40b68a36dce316218c1" ],
          "data" : "0x000000000000000000000000000000000000000000000000000000000000029a",
          "blockNumber" : "0x183a01",
          "transactionHash" : "0x679dc2320db21d595e6619fb11661e74ea03435bc6e9933db02f61653bde86e7",
          "transactionIndex" : "0x0",
          "blockHash" : "0xaf1f7b9ebd97b378bb31c72f9e7612588a5e4805dc4ee407df468ddef802bdcc",
          "logIndex" : "0x0",
          "removed" : false
        } ],
        "logsBloom" : "0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000040000000000000000",
        "status" : "0x1",
        "to" : "0xc5bba68089785e3bc6b4a300c87a88124af21a4f",
        "transactionHash" : "0x679dc2320db21d595e6619fb11661e74ea03435bc6e9933db02f61653bde86e7",
        "transactionIndex" : "0x0"
      }
    }
    09:36:48.410 [main] DEBUG org.web3j.protocol.http.HttpService - <-- END HTTP (1636-byte body)
    set() -end:Fri Jul 02 09:36:48 CST 2021
    
    {"blockHash":"0xaf1f7b9ebd97b378bb31c72f9e7612588a5e4805dc4ee407df468ddef802bdcc","blockNumber":1587713,"blockNumberRaw":"0x183a01","cumulativeGasUsed":28139,"cumulativeGasUsedRaw":"0x6deb","from":"0xfe3b557e8fb62b89f4916b721be55ceb828dbd73","gasUsed":28139,"gasUsedRaw":"0x6deb","logs":[{"address":"0xc5bba68089785e3bc6b4a300c87a88124af21a4f","blockHash":"0xaf1f7b9ebd97b378bb31c72f9e7612588a5e4805dc4ee407df468ddef802bdcc","blockNumber":1587713,"blockNumberRaw":"0x183a01","data":"0x000000000000000000000000000000000000000000000000000000000000029a","logIndex":0,"logIndexRaw":"0x0","removed":false,"topics":["0x0de2d86113046b9e8bb6b785e96a6228f6803952bf53a40b68a36dce316218c1"],"transactionHash":"0x679dc2320db21d595e6619fb11661e74ea03435bc6e9933db02f61653bde86e7","transactionIndex":0,"transactionIndexRaw":"0x0"}],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000040000000000000000","status":"0x1","statusOK":true,"to":"0xc5bba68089785e3bc6b4a300c87a88124af21a4f","transactionHash":"0x679dc2320db21d595e6619fb11661e74ea03435bc6e9933db02f61653bde86e7","transactionIndex":0,"transactionIndexRaw":"0x0"}
    set()-SetEvent -start:Fri Jul 02 09:36:48 CST 2021
    
    1
    set()-SetEvent -end:Fri Jul 02 09:37:53 CST 2021
    
    ---------finished---------
    Disconnected from the target VM, address: '127.0.0.1:59829', transport: 'socket'
    
    Process finished with exit code 0
    
    
    1. 进阶调用(异步模式)

    示例:

       private static void setAsync(String address, String value) throws Exception {
            printDate("sendAsyncsendAsyncsendAsyncsendAsyncsendAsyncsendAsyncsendAsync-------start   ");
            OperateSimpleStorage demo = new OperateSimpleStorage();
            SimpleStorage contract = demo.loadContract(address);
            printDate("sendAsync() -start:");
            CompletableFuture<TransactionReceipt> receipt = contract.set(new BigInteger(value)).sendAsync();
            printDate("sendAsync() -end:");
            // 异步
            receipt.thenAccept((result) -> {
                System.out.println("result:" + result);
            });
            printDate("sendAsyncsendAsyncsendAsyncsendAsyncsendAsyncsendAsyncsendAsync-------end  ");
        }
    
    address----------------------------------0xC5bbA68089785E3bC6B4a300C87A88124af21A4f
    sendAsyncsendAsyncsendAsyncsendAsyncsendAsyncsendAsyncsendAsync-------start   Fri Jul 02 09:56:28 CST 2021
    
    09:56:29.413 [main] INFO com.red.udpn.did.service.besuChain.engine.OperateSimpleStorage - loadContract()----------------------address:0xC5bbA68089785E3bC6B4a300C87A88124af21A4f
    sendAsync() -start:Fri Jul 02 09:56:29 CST 2021
    
    sendAsync() -end:Fri Jul 02 09:56:29 CST 2021
    
    sendAsyncsendAsyncsendAsyncsendAsyncsendAsyncsendAsyncsendAsync-------end  Fri Jul 02 09:56:29 CST 2021
    
    ---------finished---------
    09:56:29.487 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - --> POST http://192.168.1.173:20000/
    09:56:29.487 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json; charset=utf-8
    09:56:29.487 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Length: 125
    09:56:29.487 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - 
    09:56:29.488 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - {"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0xfe3b557e8fb62b89f4916b721be55ceb828dbd73","pending"],"id":0}
    09:56:29.488 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - --> END POST (125-byte body)
    09:56:29.562 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - <-- 200 OK http://192.168.1.173:20000/ (73ms)
    09:56:29.562 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json
    09:56:29.562 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - 
    09:56:29.562 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - {
      "jsonrpc" : "2.0",
      "id" : 0,
      "result" : "0x216c"
    }
    09:56:29.562 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - <-- END HTTP (58-byte body)
    09:56:29.601 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - --> POST http://192.168.1.173:20000/
    09:56:29.601 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json; charset=utf-8
    09:56:29.601 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Length: 346
    09:56:29.601 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - 
    09:56:29.601 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - {"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["0xf88682216c80834c4b4094c5bba68089785e3bc6b4a300c87a88124af21a4f80a460fe47b1000000000000000000000000000000000000000000000000000000000000029a1ca0171cd75805c1856da325ba60898b3d70c5dcdcf061ec4482dc5d26abd5cb909ba039935ab6c15ad1d7bc3d1c7d26c9baab3a10a3faef20e8d6e4b240af674309d2"],"id":1}
    09:56:29.601 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - --> END POST (346-byte body)
    09:56:29.717 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - <-- 200 OK http://192.168.1.173:20000/ (115ms)
    09:56:29.717 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json
    09:56:29.717 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - 
    09:56:29.717 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - {
      "jsonrpc" : "2.0",
      "id" : 1,
      "result" : "0x64f5105055e20f5fb85e1d7396fa47d73b8477dcf1c7f1978aeede5c280547f1"
    }
    09:56:29.717 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - <-- END HTTP (118-byte body)
    09:56:29.719 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - --> POST http://192.168.1.173:20000/
    09:56:29.719 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json; charset=utf-8
    09:56:29.719 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Length: 141
    09:56:29.719 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - 
    09:56:29.719 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - {"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0x64f5105055e20f5fb85e1d7396fa47d73b8477dcf1c7f1978aeede5c280547f1"],"id":2}
    09:56:29.719 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - --> END POST (141-byte body)
    09:56:29.817 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - <-- 200 OK http://192.168.1.173:20000/ (97ms)
    09:56:29.817 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json
    09:56:29.817 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - 
    09:56:29.817 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - {
      "jsonrpc" : "2.0",
      "id" : 2,
      "result" : null
    }
    09:56:34.860 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - --> POST http://192.168.1.173:20000/
    09:56:34.860 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json; charset=utf-8
    09:56:34.860 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Length: 142
    09:56:34.860 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - 
    09:56:34.860 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - {"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0x64f5105055e20f5fb85e1d7396fa47d73b8477dcf1c7f1978aeede5c280547f1"],"id":11}
    09:56:34.860 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - --> END POST (142-byte body)
    09:56:34.926 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - <-- 200 OK http://192.168.1.173:20000/ (66ms)
    09:56:34.926 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - Content-Type: application/json
    09:56:34.926 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - 
    09:56:34.926 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - {
      "jsonrpc" : "2.0",
      "id" : 11,
      "result" : {
        "blockHash" : "0xa26a7668c9d14c24086ffa2efe270fee6ba2ed533ddf003000f603fd30ae7965",
        "blockNumber" : "0x183ba8",
        "contractAddress" : null,
        "cumulativeGasUsed" : "0x6deb",
        "from" : "0xfe3b557e8fb62b89f4916b721be55ceb828dbd73",
        "gasUsed" : "0x6deb",
        "logs" : [ {
          "address" : "0xc5bba68089785e3bc6b4a300c87a88124af21a4f",
          "topics" : [ "0x0de2d86113046b9e8bb6b785e96a6228f6803952bf53a40b68a36dce316218c1" ],
          "data" : "0x000000000000000000000000000000000000000000000000000000000000029a",
          "blockNumber" : "0x183ba8",
          "transactionHash" : "0x64f5105055e20f5fb85e1d7396fa47d73b8477dcf1c7f1978aeede5c280547f1",
          "transactionIndex" : "0x0",
          "blockHash" : "0xa26a7668c9d14c24086ffa2efe270fee6ba2ed533ddf003000f603fd30ae7965",
          "logIndex" : "0x0",
          "removed" : false
        } ],
        "logsBloom" : "0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000040000000000000000",
        "status" : "0x1",
        "to" : "0xc5bba68089785e3bc6b4a300c87a88124af21a4f",
        "transactionHash" : "0x64f5105055e20f5fb85e1d7396fa47d73b8477dcf1c7f1978aeede5c280547f1",
        "transactionIndex" : "0x0"
      }
    }
    09:56:34.926 [pool-1-thread-1] DEBUG org.web3j.protocol.http.HttpService - <-- END HTTP (1637-byte body)
    result:TransactionReceipt{transactionHash='0x64f5105055e20f5fb85e1d7396fa47d73b8477dcf1c7f1978aeede5c280547f1', transactionIndex='0x0', blockHash='0xa26a7668c9d14c24086ffa2efe270fee6ba2ed533ddf003000f603fd30ae7965', blockNumber='0x183ba8', cumulativeGasUsed='0x6deb', gasUsed='0x6deb', contractAddress='null', root='null', status='0x1', from='0xfe3b557e8fb62b89f4916b721be55ceb828dbd73', to='0xc5bba68089785e3bc6b4a300c87a88124af21a4f', logs=[Log{removed=false, logIndex='0x0', transactionIndex='0x0', transactionHash='0x64f5105055e20f5fb85e1d7396fa47d73b8477dcf1c7f1978aeede5c280547f1', blockHash='0xa26a7668c9d14c24086ffa2efe270fee6ba2ed533ddf003000f603fd30ae7965', blockNumber='0x183ba8', address='0xc5bba68089785e3bc6b4a300c87a88124af21a4f', data='0x000000000000000000000000000000000000000000000000000000000000029a', type='null', topics=[0x0de2d86113046b9e8bb6b785e96a6228f6803952bf53a40b68a36dce316218c1]}], logsBloom='0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000040000000000000000', revertReason='null'}
    
    Process finished with exit code -1
    
    

    其他:

       public SimpleStorage loadContract(String address) throws Exception {
            init();//init  parameter
            if (address != null && !address.equals("")) {
                this.address = address;
            }
            logger.info("loadContract()----------------------address:" + this.address);
            SimpleStorage contract = SimpleStorage.load(
                    this.address,
                    this.web3j,
                    (TransactionManager) (new RawTransactionManager(this.web3j, this.credentials, 40, 500)),
                    this.staticGasProvider);
            this.web3j.shutdown();
            return contract;
        }
    

    相关文章

      网友评论

          本文标题:[快速入门]使用web3j调用besu上的智能合约

          本文链接:https://www.haomeiwen.com/subject/lcjeultx.html