Skip to content

Commit

Permalink
don't re-alert when restarting
Browse files Browse the repository at this point in the history
  • Loading branch information
Beau Shinkle committed Feb 3, 2023
1 parent 5643d50 commit 0dd9240
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
5 changes: 5 additions & 0 deletions brain.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"0x5c9074a80de4f1b810a36e8a2a54c3e36184bc700483daf185e5cd367d2e8ee0": true,
"0xad8ca58d1639fcc8423ae75dd8aa69cdf1e916661a6ce9e01aebe1500f44c3d1": true,
"0x1f0d3098d4e9f13771d972bdf4c5efc8273acf22a848846cf5e666cbed3ae3a5": true
}
49 changes: 31 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const Web3 = require('web3')
const web3 = new Web3(process.env.MAINNET_ENDPOINT)
const axios = require('axios')
const { read, write } = require('./storage')

const bridgeABI = require('./contracts/bridge.json')

const contract = new web3.eth.Contract(bridgeABI, "0x5e4861a80b55f035d899f66772117f00fa0e8e7b")


Array.prototype.total = function() {
let total = 0
for (let i = 0; i < this.length; i++) {
Expand Down Expand Up @@ -41,28 +43,39 @@ let amounts = []
contract.events.DepositRevealed({
filter: {},
fromBlock: 16523905
}).on('data', function(e){
}).on('data', async function(e){
const amount = Number(e.returnValues.amount)
amounts.push(amount)

const btc = (amount / divisor).toFixed(2)
console.log(`${btc} BTC to ${e.returnValues.depositor}`)

if (await read(e.transactionHash)) {
return
}

const z = amounts.zScore(amount)
if (z >= 2) {
const btc = (amount / divisor).toFixed(2)
const payload = {
content: `[${btc}](https://etherscan.io/tx/${e.transactionHash}) BTC to `+
`[${e.returnValues.depositor}](https://etherscan.io/address/${e.returnValues.depositor})`
}

axios({
method: 'post',
url: process.env.WEBHOOK_URL,
data: payload
}).then(function (response) {
console.log(`${btc} BTC to ${e.returnValues.depositor}`)
})
.catch(function (error) {
console.log(error);
})
if (z < 2) {
return
}

write(e.transactionHash, true)

const payload = {
content: `[${btc}](https://etherscan.io/tx/${e.transactionHash}) BTC to `+
`[${e.returnValues.depositor}](https://etherscan.io/address/${e.returnValues.depositor})`
}

axios({
method: 'post',
url: process.env.WEBHOOK_URL,
data: payload
}).then(function (response) {
})
.catch(function (error) {
console.log(error);
})

}).on('changed', function(event){
// remove event from local database
}).on('error', console.error);
35 changes: 35 additions & 0 deletions storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const EventEmitter = require('events')

let brainLock = false
const emitter = new EventEmitter()
const fs = require('fs')

module.exports = {
async read(key) {
if (brainLock) {
await new Promise(resolve => emitter.once('unlocked', resolve))
}
brainLock = true

const data = await fs.promises.readFile("brain.json")
const value = JSON.parse(data.toString())[key]

brainLock = false
emitter.emit('unlocked')
return value
},
async write(key, val) {
if (brainLock) {
await new Promise(resolve => emitter.once('unlocked', resolve))
}
brainLock = true

const data = await fs.promises.readFile("brain.json")
let brain = JSON.parse(data.toString())
brain[key] = val
await fs.promises.writeFile("brain.json", JSON.stringify(brain, null, 2))

brainLock = false
emitter.emit('unlocked')
}
}

0 comments on commit 0dd9240

Please sign in to comment.