Web3js

如何從 TheDAO 中檢索已投票的事件?

  • May 3, 2017

這個問題是@Pablo Yabo在問題的評論部分提出的,如何查看 DAO 提案,包括目前的投票狀態?.

謝謝@BokkyPooBah 的總體構想,但最終程式碼與您編寫的程式碼有些不同。訂閱solidity事件的原型是(我正在使用web3):

SolidityEvent.prototype.execute = function (indexed, options, callback)

因此,第一個參數被索引,我傳遞了一個空對象。選項參數應該有這樣的塊資訊:

var theDAOInterfaceABI = [{ "constant": true, "inputs": [{ "name": "", "type": "uint256" }], "name": "proposals", "outputs": [{ "name": "recipient", "type": "address" }, { "name": "amount", "type": "uint256" }, { "name": "description", "type": "string" }, { "name": "votingDeadline", "type": "uint256" }, { "name": "open", "type": "bool" }, { "name": "proposalPassed", "type": "bool" }, { "name": "proposalHash", "type": "bytes32" }, { "name": "proposalDeposit", "type": "uint256" }, { "name": "newCurator", "type": "bool" }, { "name": "yea", "type": "uint256" }, { "name": "nay", "type": "uint256" }, { "name": "creator", "type": "address" }], "type": "function" }, { "constant": true, "inputs": [], "name": "rewardAccount", "outputs": [{ "name": "", "type": "address" }], "type": "function" }, { "constant": true, "inputs": [], "name": "daoCreator", "outputs": [{ "name": "", "type": "address" }], "type": "function" }, { "constant": false, "inputs": [{ "name": "_proposalID", "type": "uint256" }, { "name": "_transactionData", "type": "bytes" }], "name": "executeProposal", "outputs": [{ "name": "_success", "type": "bool" }], "type": "function" }, { "constant": false, "inputs": [], "name": "unblockMe", "outputs": [{ "name": "", "type": "bool" }], "type": "function" }, { "constant": true, "inputs": [], "name": "totalRewardToken", "outputs": [{ "name": "", "type": "uint256" }], "type": "function" }, { "constant": true, "inputs": [{ "name": "", "type": "address" }], "name": "allowedRecipients", "outputs": [{ "name": "", "type": "bool" }], "type": "function" }, { "constant": false, "inputs": [{ "name": "_to", "type": "address" }, { "name": "_amount", "type": "uint256" }], "name": "transferWithoutReward", "outputs": [{ "name": "success", "type": "bool" }], "type": "function" }, { "constant": false, "inputs": [{ "name": "_recipient", "type": "address" }, { "name": "_amount", "type": "uint256" }, { "name": "_description", "type": "string" }, { "name": "_transactionData", "type": "bytes" }, { "name": "_debatingPeriod", "type": "uint256" }, { "name": "_newCurator", "type": "bool" }], "name": "newProposal", "outputs": [{ "name": "_proposalID", "type": "uint256" }], "type": "function" }, { "constant": true, "inputs": [{ "name": "", "type": "address" }], "name": "DAOpaidOut", "outputs": [{ "name": "", "type": "uint256" }], "type": "function" }, { "constant": true, "inputs": [], "name": "minQuorumDivisor", "outputs": [{ "name": "", "type": "uint256" }], "type": "function" }, { "constant": false, "inputs": [{ "name": "_newContract", "type": "address" }], "name": "newContract", "outputs": [], "type": "function" }, { "constant": false, "inputs": [{ "name": "_recipient", "type": "address" }, { "name": "_allowed", "type": "bool" }], "name": "changeAllowedRecipients", "outputs": [{ "name": "_success", "type": "bool" }], "type": "function" }, { "constant": false, "inputs": [], "name": "halveMinQuorum", "outputs": [{ "name": "_success", "type": "bool" }], "type": "function" }, { "constant": true, "inputs": [{ "name": "", "type": "address" }], "name": "paidOut", "outputs": [{ "name": "", "type": "uint256" }], "type": "function" }, { "constant": false, "inputs": [{ "name": "_proposalID", "type": "uint256" }, { "name": "_newCurator", "type": "address" }], "name": "splitDAO", "outputs": [{ "name": "_success", "type": "bool" }], "type": "function" }, { "constant": true, "inputs": [], "name": "DAOrewardAccount", "outputs": [{ "name": "", "type": "address" }], "type": "function" }, { "constant": true, "inputs": [], "name": "proposalDeposit", "outputs": [{ "name": "", "type": "uint256" }], "type": "function" }, { "constant": true, "inputs": [], "name": "numberOfProposals", "outputs": [{ "name": "_numberOfProposals", "type": "uint256" }], "type": "function" }, { "constant": true, "inputs": [], "name": "lastTimeMinQuorumMet", "outputs": [{ "name": "", "type": "uint256" }], "type": "function" }, { "constant": false, "inputs": [{ "name": "_toMembers", "type": "bool" }], "name": "retrieveDAOReward", "outputs": [{ "name": "_success", "type": "bool" }], "type": "function" }, { "constant": false, "inputs": [], "name": "receiveEther", "outputs": [{ "name": "", "type": "bool" }], "type": "function" }, { "constant": true, "inputs": [{ "name": "_proposalID", "type": "uint256" }], "name": "getNewDAOAddress", "outputs": [{ "name": "_newDAO", "type": "address" }], "type": "function" }, { "constant": false, "inputs": [{ "name": "_proposalID", "type": "uint256" }, { "name": "_supportsProposal", "type": "bool" }], "name": "vote", "outputs": [{ "name": "_voteID", "type": "uint256" }], "type": "function" }, { "constant": false, "inputs": [], "name": "getMyReward", "outputs": [{ "name": "_success", "type": "bool" }], "type": "function" }, { "constant": true, "inputs": [{ "name": "", "type": "address" }], "name": "rewardToken", "outputs": [{ "name": "", "type": "uint256" }], "type": "function" }, { "constant": false, "inputs": [{ "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_amount", "type": "uint256" }], "name": "transferFromWithoutReward", "outputs": [{ "name": "success", "type": "bool" }], "type": "function" }, { "constant": false, "inputs": [{ "name": "_proposalDeposit", "type": "uint256" }], "name": "changeProposalDeposit", "outputs": [], "type": "function" }, { "constant": true, "inputs": [{ "name": "", "type": "address" }], "name": "blocked", "outputs": [{ "name": "", "type": "uint256" }], "type": "function" }, { "constant": true, "inputs": [], "name": "curator", "outputs": [{ "name": "", "type": "address" }], "type": "function" }, { "constant": true, "inputs": [{ "name": "_proposalID", "type": "uint256" }, { "name": "_recipient", "type": "address" }, { "name": "_amount", "type": "uint256" }, { "name": "_transactionData", "type": "bytes" }], "name": "checkProposalCode", "outputs": [{ "name": "_codeChecksOut", "type": "bool" }], "type": "function" }, { "anonymous": false, "inputs": [{ "indexed": true, "name": "proposalID", "type": "uint256" }, { "indexed": false, "name": "recipient", "type": "address" }, { "indexed": false, "name": "amount", "type": "uint256" }, { "indexed": false, "name": "newCurator", "type": "bool" }, { "indexed": false, "name": "description", "type": "string" }], "name": "ProposalAdded", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "name": "proposalID", "type": "uint256" }, { "indexed": false, "name": "position", "type": "bool" }, { "indexed": true, "name": "voter", "type": "address" }], "name": "Voted", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "name": "proposalID", "type": "uint256" }, { "indexed": false, "name": "result", "type": "bool" }, { "indexed": false, "name": "quorum", "type": "uint256" }], "name": "ProposalTallied", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "name": "_newCurator", "type": "address" }], "name": "NewCurator", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "name": "_recipient", "type": "address" }, { "indexed": false, "name": "_allowed", "type": "bool" }], "name": "AllowedRecipientChanged", "type": "event" }];
var theDAOAddress = "0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413";

var theDAOInterface = web3.eth.contract(theDAOInterfaceABI).at(theDAOAddress);
var theDAOVotedEvent = theDAOInterface.Voted({}, { fromBlock: 1594771, toBlock: 'latest' });
var i = 0;
theDAOVotedEvent.watch(function (error, result) {
   console.log(i++ + ": " + JSON.stringify(result));
});

Geth 控制台中的輸出:

0: ...
...
1386: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":false,"proposalID":"3","voter":"0x89dbadfc48054057b6c9c378f6edf9d15d8b1d68"},"blockHash":"0x8de0d6d13909ff5360aa9cff37de4e5395179dc24d0f04a92140c767637acc31","blockNumber":1630418,"event":"Voted","logIndex":0,"transactionHash":"0x5ff6ab2d46ad270503c55923b14fb431514efeb2d3e9b0140bf2ee20677c06cc","transactionIndex":0}
1387: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":true,"proposalID":"15","voter":"0xcd0a161bc367ae0927a92aac9cf6e5086714efca"},"blockHash":"0x8de0d6d13909ff5360aa9cff37de4e5395179dc24d0f04a92140c767637acc31","blockNumber":1630418,"event":"Voted","logIndex":1,"transactionHash":"0x50425d5be9aabb4186a90ae4461880b8e55f082f01bb48368b07504284b6798d","transactionIndex":1}
1388: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":true,"proposalID":"15","voter":"0x89dbadfc48054057b6c9c378f6edf9d15d8b1d68"},"blockHash":"0x8a520b39314bfa8ccfad64dc7d16c02ef5870db765f1b60bd04d49c3bcac4543","blockNumber":1630422,"event":"Voted","logIndex":0,"transactionHash":"0x51f31081c6cf9ae8a9b183aecd3aee8381eeae0933327c10fb3669faad1b133d","transactionIndex":0}
1389: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":true,"proposalID":"17","voter":"0x89dbadfc48054057b6c9c378f6edf9d15d8b1d68"},"blockHash":"0xb652a73243f01635b8292beb88a6865f160a82efc04823c12e5a6cd0b3f75d7c","blockNumber":1630438,"event":"Voted","logIndex":0,"transactionHash":"0xc35e484949863c524be4a3419946f3f203f08124ca09bda61112c678cb744523","transactionIndex":0}
1390: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":true,"proposalID":"5","voter":"0x1a51e74e25825b4ff2d0110ced7a46e0405259cd"},"blockHash":"0x9297abbd6a0eee95a51fc94b8856d8bdb4a9fe2dabd9c9308cbddec1cfd7c930","blockNumber":1630440,"event":"Voted","logIndex":0,"transactionHash":"0xb92834b3a18b65e990eb47fa6b215d3ab5fe074c92f76d679b2c6a4b40dea23a","transactionIndex":0}
1391: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":true,"proposalID":"5","voter":"0xfe72c77797c23a22b606cd21630366d44a13761b"},"blockHash":"0x9297abbd6a0eee95a51fc94b8856d8bdb4a9fe2dabd9c9308cbddec1cfd7c930","blockNumber":1630440,"event":"Voted","logIndex":1,"transactionHash":"0x378fd02dec8c7b0a5920cb71e23b50f443b7a804968065f93c2d5a5c5c3df852","transactionIndex":1}
1392: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":true,"proposalID":"5","voter":"0xe8be24f289443ee473bc76822f55098d89b91cc5"},"blockHash":"0x5baa04ff8fcc878aa9a4986ec5f197b2f3fca48b41c5bb9130fd358fb4c2500b","blockNumber":1630455,"event":"Voted","logIndex":0,"transactionHash":"0xa91d734fbd55f826c4b87e4dc904b5f1813c52f724cb5a5327654fcec4c5f70f","transactionIndex":1}
1393: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":true,"proposalID":"17","voter":"0xabe6d3b3b88277e5d9d58318be0d66896d806d92"},"blockHash":"0x637d893ec39e9a905d14c14cd6aee4e568dc6dbaa7f66c7ab48f6aa0838d0dd2","blockNumber":1630526,"event":"Voted","logIndex":0,"transactionHash":"0xaa28f6f9ecc6ef5132296f0587c2893a98fada046342dff45e4c9080e5558d90","transactionIndex":2}
1394: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":true,"proposalID":"15","voter":"0xabe6d3b3b88277e5d9d58318be0d66896d806d92"},"blockHash":"0xda2168aaf754bfca5c352cbf8c4d799f7273c789813a4e62920041b8892b5c71","blockNumber":1630534,"event":"Voted","logIndex":0,"transactionHash":"0x22a6ef6405438a236fbedc92970656294f5471ef5084945d9a73267287ec78e8","transactionIndex":1}
1395: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":true,"proposalID":"5","voter":"0x9c42e0a1371b1b1c907540277a11f7728e7a55cb"},"blockHash":"0x682ba2d04c04e78fde0917d48e193232668465e6d16f4559aefce6017d2af1fa","blockNumber":1630584,"event":"Voted","logIndex":0,"transactionHash":"0x4a720cf5fa7734f81716c0bbbeb6b05c76ca9e17b3a7f68fbd0b010f90ea19db","transactionIndex":2}
1396: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":true,"proposalID":"17","voter":"0x9c42e0a1371b1b1c907540277a11f7728e7a55cb"},"blockHash":"0x682ba2d04c04e78fde0917d48e193232668465e6d16f4559aefce6017d2af1fa","blockNumber":1630584,"event":"Voted","logIndex":1,"transactionHash":"0x0abb2eb5df7f409c4eecd35c9b46b283671a1ede9256637f313c2a6444ee9bb9","transactionIndex":4}
1397: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":true,"proposalID":"5","voter":"0xc4e19f0a62a1d3bc6a53ba6c8c62d756aa735858"},"blockHash":"0xe06ec65d4c2d2641216e0c5d3fd0d74225421c795aa33bf6f4ef2ef421e51bf7","blockNumber":1630587,"event":"Voted","logIndex":2,"transactionHash":"0x44f297b84a22c5dc3ea2d48367e990b0a2de132df01789a6d7de33e0a8621a8c","transactionIndex":15}
1398: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":true,"proposalID":"17","voter":"0xc4e19f0a62a1d3bc6a53ba6c8c62d756aa735858"},"blockHash":"0x9b10dbbdcff964636fcdcb9c2456c66e3e11498dd6b37935761a6c0cedcb700c","blockNumber":1630588,"event":"Voted","logIndex":0,"transactionHash":"0x6e310b7fcb24eef068f123712eda95e90e76c35047c3ec85941e2aeb43c2ee2e","transactionIndex":2}
1399: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":true,"proposalID":"17","voter":"0x15914fbb60a61dd8d277a35063e3f3fa7e8dce7f"},"blockHash":"0xc22a3dd53e6c903e004dc3ff890d10535f8b4db76fa16611defc583246f1253c","blockNumber":1630678,"event":"Voted","logIndex":0,"transactionHash":"0x341c98824de2780171ad2292b6796bea7ef1e9fe83b29082128c8b55fdcade99","transactionIndex":4}
1400: {"address":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","args":{"position":false,"proposalID":"11","voter":"0x15914fbb60a61dd8d277a35063e3f3fa7e8dce7f"},"blockHash":"0x711f96cb919f3ae33257aea2109289872eeb99bf5f7e847f6aab82dbaee1c2e8","blockNumber":1630681,"event":"Voted","logIndex":0,"transactionHash":"0x6fb6766f3b10c0e2dcb71a739affdb0e9435af5194c7817ff66342c7385e696a","transactionIndex":0}
{
 callbacks: [function(error, result), function(error, result)],
 filterId: "0x7e76b6fdb1c1d51593815473e094c3a5",
 getLogsCallbacks: [],
 implementation: {
   getLogs: function(),
   newFilter: function(),
   poll: function(),
   uninstallFilter: function()
 },
 options: {
   address: "0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413",
   from: undefined,
   fromBlock: "0x185593",
   to: undefined,
   toBlock: "latest",
   topics: ["0x86abfce99b7dd908bec0169288797f85049ec73cbe046ed9de818fab3a497ae0", null, null]
 },
 pollFilters: [],
 requestManager: {
   polls: {
     0x7e76b6fdb1c1d51593815473e094c3a5: {
       data: {...},
       id: "0x7e76b6fdb1c1d51593815473e094c3a5",
       callback: function(error, messages),
       uninstall: function()
     }
   },
   provider: {
     newAccount: function(),
     send: function(),
     sendAsync: function(),
     unlockAccount: function()
   },
   timeout: {},
   poll: function(),
   reset: function(keepIsSyncing),
   send: function(data),
   sendAsync: function(data, callback),
   sendBatch: function(data, callback),
   setProvider: function(p),
   startPolling: function(data, pollId, callback, uninstall),
   stopPolling: function(pollId)
 },
 formatter: function(),
 get: function(callback),
 stopWatching: function(),
 watch: function(callback)
}

它完美地工作,它從指定的塊間隔(這裡從塊 1594771 到最新)呼叫所有事件。

引用自:https://ethereum.stackexchange.com/questions/4452