1. 程式人生 > >【web3j】智慧合約

【web3j】智慧合約

web3j對於智慧合約有兩種方式

1、第一種:直接使用RawTrasaction進行建立

 1 // using a raw transaction
 2 RawTransaction rawTransaction = RawTransaction.createContractTransaction(
 3         <nonce>,
 4         <gasPrice>,
 5         <gasLimit>,
 6         <value>,
 7         "0x <compiled smart contract code>");
8 // send... 9 10 // get contract address 11 EthGetTransactionReceipt transactionReceipt = 12 web3j.ethGetTransactionReceipt(transactionHash).send(); 13 14 if (transactionReceipt.getTransactionReceipt.isPresent()) { 15 String contractAddress = transactionReceipt.get().getContractAddress();
16 } else { 17 // try again 18 }

2、第二種:將合約程式碼轉換成Java Bean。

(1)首先我們需要一份寫好的智慧合約。

 1 pragma solidity ^0.4.18;
 2 
 3 // Example taken from https://www.ethereum.org/greeter, also used in
 4 // https://github.com/ethereum/go-ethereum/wiki/Contract-Tutorial#your-first-citizen-the-greeter
 5 
 6 contract mortal {
 7
/* Define variable owner of the type address*/ 8 address owner; 9 10 /* this function is executed at initialization and sets the owner of the contract */ 11 function mortal() { owner = msg.sender; } 12 13 /* Function to recover the funds on the contract */ 14 function kill() { if (msg.sender == owner) suicide(owner); } 15 } 16 17 contract greeter is mortal { 18 /* define variable greeting of the type string */ 19 string greeting; 20 21 /* this runs when the contract is executed */ 22 function greeter(string _greeting) public { 23 greeting = _greeting; 24 } 25 26 /* main function */ 27 function greet() constant returns (string) { 28 return greeting; 29 } 30 }

注意:智慧合約的程式碼是solidity寫的,有關solidity語法和使用可以查詢solidity官網

有可能需要FQ,請自行百度科學上網

  

    點選圖示中details按鈕,會出現智慧合約的ABI和BIN

    

    將BYTECODE中的object複製下來儲存成greeter.bin,點選ABI旁邊的複製按鈕將其儲存成greeter.abi

    

        進入目錄bin下,將greeter.bin和greeter.abi,複製到目錄下,在此目錄命令列執行

        web3j solidity generate --javaTypes greeter.bin greeter.abi -o src/ -p com.your.organisation.name

        web3j solidity generate [--javaTypes|--solidityTypes] /path/to/<smart-contract>.bin /path/to/<smart-contract>.abi -o /path/to/src/main/java -p com.your.organisation.name

        -o 是原始碼要放的目錄,-p是包名,--javaTypes也可以選擇--solidityTypes

      生成出來Greeter.java

 1 import org.web3j.abi.FunctionEncoder;
 2 import org.web3j.abi.TypeReference;
 3 import org.web3j.abi.datatypes.Function;
 4 import org.web3j.abi.datatypes.Type;
 5 import org.web3j.abi.datatypes.Utf8String;
 6 import org.web3j.crypto.Credentials;
 7 import org.web3j.protocol.Web3j;
 8 import org.web3j.protocol.core.RemoteCall;
 9 import org.web3j.protocol.core.methods.response.TransactionReceipt;
10 import org.web3j.tx.Contract;
11 import org.web3j.tx.TransactionManager;
12 
13 import java.math.BigInteger;
14 import java.util.Arrays;
15 import java.util.Collections;
16 
17 public class Greeter extends Contract {
18     private static final String BINARY = "6060604052341561000f57600080fd5b6040516103203803806103208339810160405280805160008054600160a060020a03191633600160a060020a03161790559190910190506001818051610059929160200190610060565b50506100fb565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a157805160ff19168380011785556100ce565b828001600101855582156100ce579182015b828111156100ce5782518255916020019190600101906100b3565b506100da9291506100de565b5090565b6100f891905b808211156100da57600081556001016100e4565b90565b6102168061010a6000396000f30060606040526004361061004b5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b58114610050578063cfae321714610065575b600080fd5b341561005b57600080fd5b6100636100ef565b005b341561007057600080fd5b610078610130565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100b457808201518382015260200161009c565b50505050905090810190601f1680156100e15780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000543373ffffffffffffffffffffffffffffffffffffffff9081169116141561012e5760005473ffffffffffffffffffffffffffffffffffffffff16ff5b565b6101386101d8565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101ce5780601f106101a3576101008083540402835291602001916101ce565b820191906000526020600020905b8154815290600101906020018083116101b157829003601f168201915b5050505050905090565b602060405190810160405260008152905600a165627a7a723058209dd925f8a845985cc97b98b1d11e67cb72915d7316a7eeb4e28cec3f5f398c9f0029";
19 
20     protected Greeter(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
21         super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
22     }
23 
24     protected Greeter(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
25         super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
26     }
27 
28     public RemoteCall<TransactionReceipt> kill() {
29         Function function = new Function(
30                 "kill",
31                 Arrays.<Type>asList(),
32                 Collections.<TypeReference<?>>emptyList());
33         return executeRemoteCallTransaction(function);
34     }
35 
36     public RemoteCall<String> greet() {
37         Function function = new Function("greet",
38                 Arrays.<Type>asList(),
39                 Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
40         return executeRemoteCallSingleValueReturn(function, String.class);
41     }
42 
43     public static RemoteCall<Greeter> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, String _greeting) {
44         String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(_greeting)));
45         return deployRemoteCall(Greeter.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor);
46     }
47 
48     public static RemoteCall<Greeter> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, String _greeting) {
49         String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(_greeting)));
50         return deployRemoteCall(Greeter.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor);
51     }
52 
53     public static Greeter load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
54         return new Greeter(contractAddress, web3j, credentials, gasPrice, gasLimit);
55     }
56 
57     public static Greeter load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
58         return new Greeter(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
59     }
60 }

    部署智慧合約

 1 import org.web3j.crypto.Credentials;
 2 import org.web3j.protocol.Web3j;
 3 import org.web3j.protocol.http.HttpService;
 4 import org.web3j.tx.Contract;
 5 
 6 public class SmartContractTest1 {
 7     public static void main(String[] args) throws Exception {
 8         Web3j web3j = Web3j.build(new HttpService("https://kovan.infura.io/<your-token>"));
 9         String ownAddress = "0xD1c82c71cC567d63Fd53D5B91dcAC6156E5B96B3";
10         String toAddress = "0x6e27727bbb9f0140024a62822f013385f4194999";
11         Credentials credentials = Credentials.create("xxxxxxxxxxxxxxxxxxx");
12         //部署智慧合約
13         Greeter greeter = Greeter.deploy(web3j,credentials,Contract.GAS_PRICE,Contract.GAS_LIMIT,"zx").send();
14         System.out.println(greeter.getContractAddress());
       //呼叫智慧合約
15 System.out.println(greeter.greet().send()); 16 17 } 18 }

    個人意見第二種因為比較清晰

相關推薦

web3j智慧合約

web3j對於智慧合約有兩種方式 1、第一種:直接使用RawTrasaction進行建立 1 // using a raw transaction 2 RawTransaction rawTransaction = RawTransaction.createContractTransaction( 3

原創智慧合約安全事故回顧分析(1):The Dao事件

首先需要說明的一點是,這個世界上沒有絕對安全的技術。在區塊鏈發展的十年裡,各種基於區塊鏈的數字貨幣引發的安全事故層出不窮,這些安全威脅主要來源有三個方面: 自身安全機制的問題,類似智慧合約。 生態安全問題,交易所,礦池,網站等等。 使用者安全問題,包括個人賬號密碼的洩露,被釣魚等。

鏈塊技術54期智慧合約基礎語言(九)——Solidity繼承

原文連結:以太坊智慧合約(九):Solidity繼承   本文主要講解了有關智慧合約繼承的概念、繼承的引數傳遞、重寫函式以及Solidity的繼承中的呼叫關係與多繼承。掌握區塊鏈技術,學習智慧合約。   一、目錄 ☞繼承的概念 ☞繼承的引數傳

火爆智慧穿戴回顧=北京少兒智慧科技展明年老國展再相會

【火爆】智慧穿戴回顧=北京少兒智慧科技展明年老國展再相會 近年來,少兒智慧科技產品層出不窮,各種迎合兒童智慧發展、家庭陪伴、家庭娛樂等需求的智慧產品不斷更新換代,少兒智慧科技產品市場的火爆,讓更多的創新型企業看到了無限商機,越來越多的業內人士渴望尋求新技術、新產品,以及新的市場掘金點,在機遇面前,企業又該如何

perl智慧匹配操作符~~

1. 判斷某個元素是否在給定的陣列中 sub test{ if(@array ~~ $value){ print "$value was found!\n" ; } else{ print "$value was not foun

最新智慧樹 知到 形勢與政策期末答案2018形勢與政策智慧樹章節測試答案

智慧樹形勢與政策答案章節測驗答案 1 【判斷題】(2分) 注重思想建黨與理論強黨,是無產階級政黨顯著特徵。A A. 對 B. 錯 2 【判斷題】(2分) 中共在黨的六屆七中全會,毛澤東在這次會上提出了馬克思主義中國化的重要命題。B A. 對 B. 錯

180927智慧象棋遊戲原始碼

一、原始碼特點     採用winform進行開發,象棋遊戲,歡迎下載 二、功能介紹     本原始碼是一箇中國象棋遊戲原始碼,可以單人遊戲,也可以雙人對戰,系統預設有幾種遊戲模式,可以先擇別人未戰完的

C++智慧指標

本篇博文旨在介紹C++中的智慧指標;從為什麼引入它開始,分別實現了auto_ptr,scoped_ptr,unique_ptr,shared_ptr等智慧指標;介紹了各個智慧指標的特點;最後用防函式和

如何用web3j呼叫智慧合約

在用web3j呼叫合約的時候會出現各種莫名其妙的bug,主要的原因有以下幾點:1.區塊沒同步完成:區塊在同步完成之前即在伺服器呼叫eth.syncing返回false之前操作賬戶一般都會有問題2.gas price太低或者gas limit 異常都是因為呼叫的方法不對3.連線

以太坊學習7--Web3j智慧合約

一、Web3j入門 以太坊推出了web3.js的nodejs庫,但是對於學Java出身的而言非常的不習慣,在github中尋找到了Java版本的web3j。輕量級客戶端與以太坊互動的Java庫。 web3j github地址:https://github.c

C++智慧指標(Smart Pointer)

1. 傳統指標存在的問題 傳統指標存在諸多的問題,比如指標所指向的物件的生命週期問題,掛起引用(dangling references),以及記憶體洩露(memory leaks). 如下是一個傳統指標的使用過程 void Foo() {

web3j對於智慧合約有兩種方式

1、第一種:直接使用RawTrasaction進行建立 // using a raw transaction RawTransaction rawTransaction = RawTransaction.createContractTransaction(

人工智慧智慧語音互動技術與應用

課程介紹: 智慧語音互動,是基於語音識別、語音合成、自然語言理解等技術,為企業在多種實際應用場景下,賦予產品“能聽、會說、懂你”式的智慧人機互動體驗。適用於多個應用場景中,包括智慧問答、智慧質檢、法庭庭審實時記錄、實時演講字幕、訪談錄音轉寫等。 本課程主要講解智慧語音

數字化智慧企業架構框架:為企業數字化轉型“奠基”

核心觀點本文構建智慧情景模型,它是構建智慧企業架構框架的思想和靈魂,回答Why的問題;其次討論了智慧企業架構框架,即SEAF v1.0的主要元件,回答What的問題;最後是智慧企業架構規劃指導,即如何基於SEAF v1.0規劃智慧企業架構,回答How的問題。文章提出,企業要完成數字化轉型,首先是管理層的觀念、

C++智慧指標之引用計數的實現

在C++11的標準中,引入了智慧指標的概念。 相比於auto_ptr而言,其主要缺陷在於在進行指標拷貝的時候,會出現管理權轉移的問題,導致原指標最終編成一個懸掛指標(dangling pointer

C++智慧指標簡述(三):scoped_ptr

開發十年,就只剩下這套架構體系了! >>>   

C++智慧指標簡述(四):shared_ptr

開發十年,就只剩下這套架構體系了! >>>   

劉文彬精解EOS智慧合約演練

原文連結:醒者呆的部落格園,https://www.cnblogs.com/Evsward/p/eos-contract.html EOS,智慧合約,abi,wasm,cleos,eosiocpp,開發除錯,錢包,賬戶,簽名許可權 熱身 本文旨在針對EOS智慧合約進

問鏈-EOS公開課第十一課 EOS 智慧合約相互呼叫

EOS中合約之間是可以相互呼叫的,主要通過inline action完成合約之間的呼叫。 譬如在擲骰子游戲中,存在兩個玩家先下注資金,然後比較骰子大小後決定勝負,贏的那一方將獲得所有的下注資金。在eosio原始碼eos/build/contract/dice 智

智慧合約學習筆記關於非對稱加密的公鑰、私鑰和加解密~

  學習了非對稱加密,其中對於公鑰和私鑰在加解密中的角色,還是有些不理解,於是找了點資料,加深下理解。 關於非對稱加解密,私鑰和公鑰到底是誰來加密,誰來解密? 第一種用法:公鑰加密,私鑰解密。---用於加解密 第二種用法:私鑰簽名,公鑰驗籤。---用於簽名 有點混亂