Skip to content

Instantly share code, notes, and snippets.

@gotjoshua
Created June 15, 2021 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gotjoshua/86a4da9ec175067eed162d7d177e9d28 to your computer and use it in GitHub Desktop.
Save gotjoshua/86a4da9ec175067eed162d7d177e9d28 to your computer and use it in GitHub Desktop.
moralis real time sync
Moralis.Cloud.beforeConsume('Shipped', (event) => {
if (!event.confirmed) return false
delete event.address
delete event.confirmed
delete event.block_hash
log(`beforeConsume - ${event}`)
return true
})
Moralis.Cloud.afterSave('Shipped', async (request) => {
const config = await Moralis.Config.get({ useMasterKey: true })
const CHAIN_ID = config.get('CHAIN_ID')
const VERITAS_CONTRACT = config.get('VERITAS_CONTRACT')
const VERITAS_ABI = JSON.parse(config.get('VERITAS_ABI'))
const web3 = Moralis.web3ByChain(CHAIN_ID)
const txHash = request.object.get('transaction_hash')
const tokenId = request.object.get('tokenId')
const txObj = await web3.eth.getTransaction(txHash)
const shipper = txObj.from
const etherscan = `${ETHERSCAN_RINKEBY}${txHash}`
const contract = new web3.eth.Contract(VERITAS_ABI, VERITAS_CONTRACT)
const balance = await contract.methods.balanceOf(shipper).call()
const msg = `Shipped: #${tokenId}\naddress: ${formatAddressShort(shipper)}\nholding: ${balance} VERITAS total\n[etherscan link](${etherscan})\n==========`
log(msg)
await sendToDiscord(DISCORD_CONTRACT_INTERACTIONS, msg)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment