以太坊中的Event監(jiān)聽

本來是全職做EOS方面工作的,但是,今天有些情況需要和其他公司有交流,他們咨詢關(guān)于以太坊開發(fā)的問題,感覺有些問題還不錯(cuò),做個(gè)整理;但是本篇文章先把一個(gè)比較復(fù)雜的過程介紹下,就是event的事件監(jiān)聽:

前提準(zhǔn)備

( web3有一個(gè)0.1x.0的版本,還有1.0.0 Beta的版本,本文都是使用的1.0.0 Beta;)

首先,通常我們啟動節(jié)點(diǎn),都是啟動rpc連接接口,但是既然是事件監(jiān)聽,短連接的rpc肯定不行,需要使用websocket的方式,在啟動geth的時(shí)候,請使用如下方式:

geth --rpcapi "db,eth,net,web3,personal" --ws --wsaddr "localhost" --wsport "8545" --wsorigins "*" --identity "TestNode" --datadir "./data" --testnet

相應(yīng)的,在使用web3進(jìn)行連接的時(shí)候使用:

const web3 = new Web3(new Web3.providers.WebsocketProvider('http://IP:8545'));

這樣就OK了。

合約的編譯和部署

首先,編寫簡單的測試代碼:

pragma solidity ^0.4.0;

contract Calc{
    /*區(qū)塊鏈存儲*/
    uint count;
    event ReturnValue(address indexed _from, uint _value);

    /*執(zhí)行會寫入數(shù)據(jù),所以需要`transaction`的方式執(zhí)行。*/
    function add(uint a, uint b) public returns(uint) {
        count++;
        emit ReturnValue(msg.sender, a+b);
        return a + b;
    }

    /*執(zhí)行不會寫入數(shù)據(jù),所以允許`call`的方式執(zhí)行。*/
    function getCount() public constant returns (uint) {
        return count;
    }
}

然后,編譯代碼,可以使用solc的編譯工具,也可以使用網(wǎng)站 Remix ;
編譯完成的json abi和二進(jìn)制code就貼上來了。

部署代碼:

//先解鎖你的錢包

var calcContract = new web3.eth.Contract(YouCalcABI, null, {
    from: '0xfaad1dec3e7db9d5a45fdabb90ab56f52a325d84' 
});

calcContract.deploy().send({
    from: '0xfaad1dec3e7db9d5a45fdabb90ab56f52a325d84',
    gas: 1500000,
    gasPrice: '30000000000000'
}, function(error, transactionHash){
    console.log("deploy tx hash:"+transactionHash)
})
.on('error', function(error){ console.error(error) })
.on('transactionHash', function(transactionHash){ console.log("hash:",transactionHash)})
.on('receipt', function(receipt){
   console.log(receipt.contractAddress) // contains the new contract address
})
.on('confirmation', function(confirmationNumber, receipt){console.log("receipt,",receipt)})
.then(function(newContractInstance){
    console.log(newContractInstance.options.address) // instance with the new contract address
});

部署成功之后,會打印結(jié)果,以及合約地址;

然后,以后想獲得合約結(jié)構(gòu),就要用到abi和合約地址,比如:

var calc = new web3.eth.Contract([ abi 內(nèi)容],'0x16706C380579d13eC3d6EBC5E0cbaf35a2bE8DB0');

監(jiān)聽event

監(jiān)聽方式很簡單,看代碼:

calc.events.ReturnValue(
    {
        filter:
            {_from:'0xfaad1dec3e7db9d5a45fdabb90ab56f52a325d84'},
            fromBlock:'latest'
    }, 
    function(error, event) {
        console.log("******result:*******\n"+JSON.stringify(event));
    }
);

然后,你可以在其他地方,發(fā)送合約函數(shù)add:

calc.methods.add(5,4).send({from:coin}).then(function(receipt){console.log("fuck!");console.log(receipt)});

然后,你就能在監(jiān)聽終端看見監(jiān)聽結(jié)果了:
image.png

OK,你已經(jīng)搞定監(jiān)聽了。
當(dāng)然,還有很多方式:比如once、allEvent等,可以去看官方文檔:web3

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容