Created
December 19, 2023 10:01
-
-
Save danyeah/a8f950cdc5bccfa70e337bbd0decee61 to your computer and use it in GitHub Desktop.
MORALIS GET CONTRACT EVENTS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"anonymous": false, | |
"inputs": [ | |
{ | |
"indexed": true, | |
"internalType": "uint256", | |
"name": "campaignId", | |
"type": "uint256" | |
}, | |
{ | |
"indexed": true, | |
"internalType": "uint256", | |
"name": "orderId", | |
"type": "uint256" | |
}, | |
{ | |
"indexed": false, | |
"internalType": "uint256", | |
"name": "amount", | |
"type": "uint256" | |
}, | |
{ | |
"indexed": false, | |
"internalType": "address", | |
"name": "paymentToken", | |
"type": "address" | |
}, | |
{ | |
"indexed": false, | |
"internalType": "address", | |
"name": "releaseToken", | |
"type": "address" | |
}, | |
{ | |
"indexed": false, | |
"internalType": "address", | |
"name": "buyer", | |
"type": "address" | |
} | |
], | |
"name": "Buy", | |
"type": "event" | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ABI = require("./SPORES.ABI.json"); | |
const topic = "Buy(uint256,uint256,uint256,address,address,address)" | |
const rows = [] | |
const campaignId = "129831418572213715119051634358707111575" | |
const masterKey = "secret_key" | |
async function getBuyEvents() { | |
const contractAddress = "0xbd507588e1579829EEB93684d3e705dF690F3A2d"; | |
await Moralis.start({ | |
apiKey: masterKey | |
}); | |
const options = { | |
chain: "0x38", // bsc | |
address: contractAddress, | |
topic: topic, | |
abi: ABI, | |
limit: 1000 | |
}; | |
const logs = await Moralis.EvmApi.events.getContractEvents(options); | |
return logs; | |
} | |
try { | |
getBuyEvents().then(events =>{ | |
const evs = events.raw.result; | |
evs.forEach(event => { | |
const data = event.data; | |
if (data.campaignId === campaignId) { | |
rows.push({ | |
buyer: data.buyer, | |
amount: ethers.formatEther(data.amount), | |
token: getTokenName(data.paymentToken) | |
}); | |
} | |
}); | |
console.log(rows); | |
const csvWriter = createCsvWriter({ | |
path: 'total.csv', | |
header: [ | |
{id: 'buyer', title: 'BUYER'}, | |
{id: 'amount', title: 'AMOUNT'}, | |
{id: 'token', title: 'TOKEN'}, | |
] | |
}); | |
csvWriter.writeRecords(rows) | |
.then(() => console.log('The CSV file was written successfully')) | |
.catch(err => console.error(err)); | |
}) | |
.catch(err => console.error(err)); | |
} catch (error) { | |
console.error(error); | |
} | |
function getTokenName(address) { | |
if ( address == "0x55d398326f99059ff775485246999027b3197955") { | |
return "BUSD" | |
} else { | |
return address | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment