Skip to content

Commit

Permalink
Use array for events.
Browse files Browse the repository at this point in the history
  • Loading branch information
starsinmypockets committed Apr 25, 2021
1 parent 6376cc8 commit 50d7507
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,36 @@ Monitors an NFT contract for `Mint` events
### Twitter message

Use the `TWITTER_MESSAGE_TEMPLATE` value in `.env` to format your tweet.
If you need access to additional variables please open an issue or submit a pull request.
You will have access to the `event` object (example below).
The value will be interpreted as a string literal and may include variables of format `${event.transactionHash}`

```javascript
{
removed: false,
logIndex: 342,
transactionIndex: 294,
transactionHash: '0x48378b555048baf27aed8fc7f4e1526a64dd91a2206d0d79690ee77e063ce97e',
blockHash: '0xd0d3af59b2eca4bff3651ee09417f105756d206a4dd84674893f64fd31bf9dbe',
blockNumber: 12310282,
address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
id: 'log_8626df84',
returnValues: Result {
'0': '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',
'1': '6610245104149876',
src: '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',
wad: '6610245104149876'
},
event: 'Withdrawal',
signature: '0x7fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65',
raw: {
data: '0x00000000000000000000000000000000000000000000000000177bfb9f4fa574',
topics: [
'0x7fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65'
'0x0000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d'
]
}
}
```

## Running the app

Expand Down
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ const {
WSURL,
ETHERSCAN_ABI_URL,
ETHERSCAN_API_KEY,
CONTRACT_ADDRESS } = process.env
CONTRACT_ADDRESS,
CONTRACT_EVENTS } = process.env

const CONTRACT_EVENTS_ARRAY = CONTRACT_EVENTS.split(',')
const Web3 = require('web3')
const Twitter = require('twitter')
const restClient = require('node-rest-client-promise').Client();
Expand All @@ -24,10 +26,10 @@ const twitterClient = new Twitter({
access_token_secret: TWITTER_ACCESS_TOKEN_SECRET
})

function postToTwitter(transaction_hash) {
function postToTwitter(event) {
const msg = eval('`'+ TWITTER_MESSAGE_TEMPLATE + '`')
return twitterClient.post('statuses/update', {status: msg}, function(error, tweet, response) {
if(error) return console.log(JSON.stringify(error))
if (error) return console.log(JSON.stringify(error))
});
}

Expand All @@ -44,10 +46,9 @@ async function eventQuery(){
let lastHash
contract.events.allEvents()
.on('data', (event) => {
console.log(event)
if (event.event === 'Deposit' && event.transactionHash !== lastHash) {
if (CONTRACT_EVENTS_ARRAY.includes(event.event) && event.transactionHash !== lastHash) {
lastHash = event.transactionHash // dedupe
postToTwitter(event.transactionHash)
postToTwitter(event)
}
})
.on('error', console.error)
Expand Down
15 changes: 8 additions & 7 deletions sample.env
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
TWITTER_API_KEY=abc123
TWITTER_API_SECRET_KEY=abc123
TWITTER_ACCESS_TOKEN=abc123
TWITTER_ACCESS_TOKEN_SECRET=abc123
TWITTER_MESSAGE_TEMPLATE="Deposit event heard at transaction hash ${transaction_hash}"
TWITTER_API_KEY=YOUR-API-KEY
TWITTER_API_SECRET_KEY=YOUR-API-SECRET
TWITTER_ACCESS_TOKEN=YOUR-ACCESS-TOKEN
TWITTER_ACCESS_TOKEN_SECRET=YOUR-TOKEN-SECRET
TWITTER_MESSAGE_TEMPLATE="${event.event} event heard at transaction hash ${event.transactionHash}, Block number: ${event.blockNumber}"
WSURL=wss://mainnet.infura.io/ws/v3/YOUR-PROJECT-ID
ETHERSCAN_ABI_URL=https://YOUR-api.etherscan.io/api?module=contract&action=getabi&address=
ETHERSCAN_API_KEY=abc123
ETHERSCAN_ABI_URL=https://api-YOUR-ACCOUNT.etherscan.io/api?module=contract&action=getabi&address=
ETHERSCAN_API_KEY=YOUR-API-KEY
CONTRACT_ADDRESS=0xffffffffffffffffffffffffffffffffffffffff
CONTRACT_EVENTS=Deposit,Withdrawal

0 comments on commit 50d7507

Please sign in to comment.