0

i test web3js by geth,i build private net in local.i try to call getPastlog ,but return empty array.

const Web3 = require("web3");
// const web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/v3/770daaf97ee14e0aa77ac105bbcdd79f"));
const web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));

web3.eth.getPastLogs({
    address:"0xF81639558D54a03D6620c5bdb2f1C2e5c87736d0",
    topics: ['0xdd970dd9b5bfe707922155b058a407655cb18288b807e2216442bca8ad83d6b5'] //topics:[web3.utils.sha3("adduintevent(uint256,uint256)")]
})
.then(console.log);

address and topics is right ,because i get them by getPastEvents

contract.getPastEvents('Log', {
    // filter: {myIndexedParam: [20,23], myOtherIndexedParam: '0x123456789...'}, // Using an array means OR: e.g. 20 or 23
    fromBlock: 0,
    toBlock: 'latest'
}, function (error, events) {
    console.log(events[0].raw);
});

===========result ==========
{
  data: '0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000a54686520576f726c642100000000000000000000000000000000000000000000',
  topics: [
    '0xdd970dd9b5bfe707922155b058a407655cb18288b807e2216442bca8ad83d6b5'
  ]
}

so, i want to konw why i can't get anything by pastlog.Who can help me...

1 Answer 1

0

Try to use this syntax:

const Web3 = require('web3');
const node_URL = 'CHAINSTACK_NODE_URL';
const web3 = new Web3(node_URL);

web3.eth.getPastLogs(
  "0xF81639558D54a03D6620c5bdb2f1C2e5c87736d0",
  ["0xdd970dd9b5bfe707922155b058a407655cb18288b807e2216442bca8ad83d6b5"],
  (err, logs) => {
    console.log(logs);
  }
);

And it will return this (only a short sample as it's super long):

 '0x00000000000000000000000000000000000000000000000000000000000017b2'
    ],
    data: '0x',
    blockNumber: 15148918,
    transactionHash: '0x1dc6e2b1b205100f355c9ed23a5692d35814953371f3117c722d2e685434a210',
    transactionIndex: 41,
    blockHash: '0x9740cc9644b12da740fb4bcee5cc7eab47a14a67cac90047bdd38f606af77791',
    logIndex: 95,
    removed: false,
    id: 'log_83c8653c'
  },
  {
    address: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
    topics: [
      '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
      '0x000000000000000000000000b5d85cbf7cb3ee0d56b3bb207d5fc4b82f43f511',
      '0x000000000000000000000000a271fee1580d9eef821c4757fe70ec30a2c0c0f1'
    ],
    data: '0x000000000000000000000000000000000000000000000000000000000ea3d300',
    blockNumber: 15148918,
    transactionHash: '0x845dd44a580847c657e20d1575b4329961b1b53d03aafa6e50fa993bc51f3a2d',
    transactionIndex: 42,
    blockHash: '0x9740cc9644b12da740fb4bcee5cc7eab47a14a67cac90047bdd38f606af77791',
    logIndex: 96,
    removed: false,
    id: 'log_dcfe5163'
  },

You can get more details about this RPC method in the Chainstack docs!

You can use the Node API reference page in the Chainstack docs to find examples of the JSON RPC methods in web3.js, web3.py, eth.rb, and cURL!

4
  • thanks ,but it still return empty array .I can get data form test net ,such as rinkeby,but in my local geth will return empty array . I don't know if my start command lost smoething.
    – 张三次
    Commented Jul 17, 2022 at 2:03
  • This is my start command: geth --http --http.corsdomain '' --networkid 201 --mine --miner.threads 2 --syncmode full --datadir data --http --ws --ws.port "8546" --port "30303" --http.corsdomain "" --nat "any" --http.api eth,web3,personal,net --allow-insecure-unlock --unlock 0,1 --password ./password.sec
    – 张三次
    Commented Jul 17, 2022 at 2:04
  • Could it be the network ID? It should be 1 for mainnet. Once you make it work, you'll need to use the syntax I sent because you will always get an empty array with the original one. Commented Jul 20, 2022 at 20:50
  • thanks,i has resolved question. i need give blocknum as param for from and to ,so that i can get data.
    – 张三次
    Commented Jul 25, 2022 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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