The following seems to do the trick
const admin = require(âfirebase-adminâ);
const crypto = require(âcryptoâ);
var contractABI = require("<path_contract_here.json>");
contractABI = contractABI.abi;
var Web3 = require(âweb3â);
var web3 = new Web3(new Web3.providers.HttpProvider(âhttps://polygon-rpc.com/â));
const REACT_APP_CONTRACT_URL = â<contract_address_here>â
const first_block = <int_of_contract_deployment_here>
const last_block = <int_of_last_block_here>
async function loadContract() {
return new web3.eth.Contract(contractABI, REACT_APP_CONTRACT_URL);
}
function generateUniqueString() {
return crypto.randomBytes(20).toString(âhexâ);
}
const AllEvents = async () => {
contract = await loadContract();
var receivedallEvents = await contract.getPastEvents(âallEventsâ, {fromBlock:first_block, toBlock: last_block})
var list_data = []
for (var event of receivedallEvents) {
var tmp_event = {};
tmp_event[ânameâ] = event[âeventâ]
tmp_event[âblockNumberâ] = event[âblockNumberâ]
var block_data = await web3.eth.getBlock(event[âblockNumberâ])
tmp_event[âblockTimestampâ] = block_data[âtimestampâ]
tmp_event[âaddressâ] = event[âaddressâ]
tmp_event[âblockHashâ] = event[âblockHashâ]
tmp_event[âtransactionHashâ] = event[âtransactionHashâ]
tmp_event[âchainIdâ] = 137
tmp_event[âconfirmedâ] = true
tmp_event[âlogIndexâ] = event[âlogIndexâ]
localReturn = event[âreturnValuesâ]
for(var key in localReturn) {
if (isNaN(parseInt(key))) {
tmp_event[key] = localReturn[key]
}
}
list_data.push(tmp_event)
}
return list_data;
};
const result = AllEvents()
// Initialize the Firebase Admin SDK with a service account key file
admin.initializeApp({
credential: admin.credential.cert("<path_firebase_cred_here.json>"),
databaseURL: âhttps://<database_name_here>.firebaseio.comâ
});
const firestore = admin.firestore();
result.then((all_records) => {
// Add data to the Cloud Firestore
for(var local_event in all_records) {
var unique_id = generateUniqueString()
var docRef = firestore.collection(â/moralis/events/Demoâ).doc(unique_id);
docRef.set(all_records[local_event])
}
}
)
Special thanks to ChatGPT