0

how can we get all the past events of the contract ? I am trying like this :

on web3.js ( "web3": "^1.0.0-beta.34"):

// Add the web3 node module
var Web3 = require('web3');

// Show web3 where it needs to look for the Ethereum node.
web3 = new Web3(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/_ws'));

// Define the contract ABI
var abi = 

// Define the contract ABI and Address
var contract = new web3.eth.Contract(abi, '0x5a3c9A1725BB82690ee0959c89abE96fD1b527ee');

// Fun console text, you can ignore this.
console.log('-----------------------------------');
console.log('Matching Smart Contract Events');
console.log('-----------------------------------');

const filter = { fromBlock: 0, toBlock: 'latest'}; // filter for your address
const events = contract.allEvents(filter); // get all events
console.log(events)

// contract.Transfer({}, { fromBlock: 0, toBlock: 'latest' }).get((error, eventResult) => {
//   if (error)
//     console.log('Error in myEvent event handler: ' + error);
//   else
//     console.log('myEvent: ' + JSON.stringify(eventResult.args));
// });

// contract.allEvents(fromBlock: 0, toBlock: 'latest', (e, res) => console.log(res))

on web3.py (web3==4.3.0) :

import requests
from web3 import Web3,WebsocketProvider

def main():
 web3 = Web3(WebsocketProvider("wss://mainnet.infura.io/_ws"))
 print (web3, "web3")
 contract_address = "0x5a3c9A1725BB82690ee0959c89abE96fD1b527ee"
 contract_abi = 
 contract = web3.eth.contract(address=contract_address, abi=contract_abi)
 print (contract, "contract")

 transferEvent = contract.eventFilter('Transfer', {'fromBlock': 0,'toBlock': 'latest'});
 print (transferEvent, "transferEvent")

 eventlist = transferEvent.get_all_entries()
 print(eventlist, "eventlist")


 # transferEvent = contract.eventFilter('Transfer', fromBlock = 0)
 # transferEvent = contract.events.Transfer.createFilter(fromBlock = 0)
 # print (transferEvent.get_all_entries())

if __name__ == "__main__":
 print ("Script has started...")
 main()
 print ("Script has ended...")
  • 1
    This seems ok, what error are you getting? – Jaime Jun 20 '18 at 7:06
  • on web3.py : *** TypeError: '<=' not supported between instances of 'int' and 'NoneType' – Chirag Maliwal Jun 20 '18 at 7:14
  • on web3.js : TypeError: contract.allEvents is not a function – Chirag Maliwal Jun 20 '18 at 7:14
  • I just came to know , there is a limit on the number of events we can get from infura. – Chirag Maliwal Jun 20 '18 at 7:15
  • is that was the rror consider to add an answer yourself so that the rest know this. – Jaime Jun 20 '18 at 7:18

Your Answer

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

Browse other questions tagged or ask your own question.