發表文章

目前顯示的是有「solidity」標籤的文章

使用Web3j 快速實作 Oracle - II 使用smart contract wrapper

Part II 拖得有點久... 其實產生出wrapper後,直接看source code,就大概懂怎麼使用了,web3j 直接把smart contract的function轉成Java function。接下來會解釋的就是怎麼load contract跟怎麼subscribe events。 備註:這裡的範例是用 web3j 3.1.1 所產生 How to load smart contract wrapper 首先,看到load的方法有兩個,其實只有差在Credentials跟TransactionManager public static Ballot load (String contractAddress , Web3j web3j , Credentials credentials , BigInteger gasPrice , BigInteger gasLimit) { return new Ballot(contractAddress , web3j , credentials , gasPrice , gasLimit) ; } public static Ballot load (String contractAddress , Web3j web3j , TransactionManager transactionManager , BigInteger gasPrice , BigInteger gasLimit) { return new Ballot(contractAddress , web3j , transactionManager , gasPrice , gasLimit) ;}   Credentials :     這是Web3j的基本元件,大概沒什麼好說的(被揍飛 XD),還是附一下範例,基本就是帶入一串私鑰即可 Credentials cred = Credentials. create ( privatekey ); 如果,你的應用沒有需要快速連續送交易,或是不要求在listen event的間隔,其實這樣就可以了。但若有其中一項需求,這個方式就無法滿足,需要用TransactionManager。 TransactionMa...

使用Web3j 快速實作 Oracle -I建立smart contract wrapper

本篇是在介紹,如何使用Web3j能快速開發Oracle,不用一個一個刻跟ABI互動的function 因為整個ethereum都還在發展中,相關的工具也是,所以變動很快,這邊我使用的是 Web3j 2.3.1 ,搭配 Solidity 0.4.17 不清楚Oracle的,可以參考 這篇 ,寫得相當詳細跟清楚 簡單來說,EVM是一個封閉環境,不可跟外界接觸,不過event(可以想作是一般寫程式的log)是會被寫到鏈上的,寫到鏈上就代表是公開網路,大家都可以去讀取,再透過程式(也就是Oracle)去監聽特地的event。然後smart contract裡的function本來就是可以從外部被執行的。所以就是透過監聽event跟執行smart contract的function,進而從外界可以跟smart contract 做互動。 首先,先撰寫你自己的smart contract,下面這是我從官方copy過來的範例,然後加了兩個event,完整的smart contract可以看 這裡 pragma solidity ^ 0 . 4 . 17 ; contract Ballot { event EventVote ( uint id, address voter, uint time); event EventNewVoter ( address voter); function giveRightToVote ( address voter) { require (( msg . sender == chairperson) && ! voters[voter].voted && (voters[voter].weight == 0 )); voters[voter].weight = 1 ; EventNewVoter (voter); } function vote ( uint proposal) { Voter storage sender = voters[ msg . sender ]; require ( ! sender.voted); ...