1

Sir, I want to read the past event log by using getPastLogs(),but I got the whole log.I hope I can filter some log. I find I can setup the 'topics' in getPastLogs(),but it always fails. How should I setup the 'topics' in getPastLogs()?

Below is my contract:

pragma solidity ^0.4.25;
contract ReadLogPOC
{
    uint gi_sum;
    event adduintevent(uint indexed p_data,uint p_sum);    


    function adduint(uint p_1) external returns(uint){
        gi_sum = 10 + p_1;
        emit adduintevent(p_1,gi_sum);
        return gi_sum;
    }
    function getsum() external returns(uint){
        return gi_sum;
    }    
}

I use Node.js,this code show all past event log. My code is:

var abi = [{"constant":false,"inputs":[{"name":"p_1","type":"uint256"}],"name":"adduint","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"getsum","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"p_data","type":"uint256"},{"indexed":false,"name":"p_sum","type":"uint256"}],"name":"adduintevent","type":"event"}];
var address = "0x61fab6aa9f16369e8e051688ceca7e01a61b9780";
var readlogpocContract = new web3.eth.Contract(abi,'0x61fab6aa9f16369e8e051688ceca7e01a61b9780'); 

web3.eth.getPastLogs(
  {
    address: '0x61fab6aa9f16369e8e051688ceca7e01a61b9780',
    fromBlock: "0x1",
    toBlock: "latest"
      }
).then(console.log).catch(e => console.log(e));

If I run "readlogpoc.adduint.sendTransaction(8,{from: "0x3d626b8fae039078fe5c33964e035c5a9d47b2e9", gas:900000}" in geth,and then I set topics like this:

web3.eth.getPastLogs(
  {
    address: '0x61fab6aa9f16369e8e051688ceca7e01a61b9780',
    fromBlock: "0x1",
    toBlock: "latest",
    topics:[web3.utils.sha3("adduintevent(uint,uint)"),web3.utils.sha3("0x8")]
      }
).then(console.log).catch(e => console.log(e));

the output is []

How should setup the topics? My environment: Web3 version is ^1.0.0-beta.36 Node version is v10.5.0 geth version is Version: 1.8.11-stable Platform :Ubuntu 16.04.4 LTS

Thanks~~

0

the problems is in the line:

topics:[web3.utils.sha3("adduintevent(uint,uint)"),web3.utils.sha3("0x8")]

it should be:

topics:[web3.utils.sha3("adduintevent(uint256,uint256)")]

hope this helps

  • This topics can show all the adduintevent logs. If I call the function 'adduint' with parameter uint 8. How do I show only this event log ? – 黃智祥 Nov 2 '18 at 2:12
  • I do a test. If the topics is :[web3.utils.sha3("adduintevent(uint256,uint256)")],"0x0000000000000000000000000000000000000000000000000000000000000008"),the result is right. – 黃智祥 Nov 2 '18 at 2:40
  • How uint 8 convert to "0x0000000000000000000000000000000000000000000000000000000000000008" using Node.js? – 黃智祥 Nov 2 '18 at 7:52
  • 1
    You just need to convert the number 8 to hex and then lefpad it to complete a 32 bytes word. If this answer helped you to solve the problem, please accept it by clicking on the check mark. – Jaime Nov 2 '18 at 9:22
-1

I got it! The example is below,but the input parameter is uint 10:

const Web3 = require('web3');
const ethereumUri = 'http://192.168.2.17:8545';
let web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(ethereumUri));

var abi = [{"constant":false,"inputs":[{"name":"p_1","type":"uint256"}],"name":"adduint","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"getsum","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"p_data","type":"uint256"},{"indexed":false,"name":"p_sum","type":"uint256"}],"name":"adduintevent","type":"event"}];
var address = "0x61fab6aa9f16369e8e051688ceca7e01a61b9780";
var readlogpocContract = new web3.eth.Contract(abi,'0x61fab6aa9f16369e8e051688ceca7e01a61b9780');  //新版本C要大寫:new web3.eth.Contract

var a = 10
console.log(a); //output :10
var b = web3.utils.toHex(a);  //output :0xa
console.log(b); //output :0xa
var c = web3.utils.padLeft(b, 64);
console.log(c);  //output :0x000000000000000000000000000000000000000000000000000000000000000a
var d = web3.utils.toDecimal(c)
console.log(d); //output :10

web3.eth.getPastLogs(
  {
    address: '0x61fab6aa9f16369e8e051688ceca7e01a61b9780',
    fromBlock: "0x1",
    toBlock: "latest",
    topics:[web3.utils.sha3("adduintevent(uint256,uint256)"),c]   // the second parameter is variable c
   }
).then(console.log).catch(e => console.log(e));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.