Blockchain Discord Bot Notifications for Polygon event (donation). This project is from a Moralis Web3 tutorial found here.
-
Clone project and setup ENV:
-
Copy
.env-sample
as.env
, and enter your info for each ENV variable:-
MORALIS_APIKEY
:Can be found by logging into your Moralis account and going to the Web3 APIs page.
-
DISCORD_BOT_TOKEN
:If you don't already know your Discord Bots token, you'll need to reset it by:
- Go to your discord bot (discord applications, click on your bot,
then click on
Bot
in the side menu) - Click
Reset Token
to get a new token - Click
Copy
- Go to your discord bot (discord applications, click on your bot,
then click on
-
DISCORD_CHANNEL_ID
:For your Discord server, right-click the channel you want the bot to message in, and click
Copy ID
.- NOTE: if you don't see that option:
- Go to your Discord settings -> Advanced
- Toggle on
Developer Mode
- NOTE: if you don't see that option:
-
-
-
Setup your Discord bot and server:
(Check the tutorial video for exact steps)
- Go to: https://discord.com/developers and setup your new application
-
Run the project:
npm i npm start
-
Run
ngrok
: (so external service can reach your local project)ngrok http 3000
You should see something like the following; the
Forwarding
url you will want to use is thehttps://
version:ngrok by @inconshreveable Session Status online Account Hamster Phomstentein (Plan: Cheesy) [...] Web Interface http://127.0.0.1:4040 Forwarding http://78be-136-175-97-43.ngrok.io -> localhost:3000 Forwarding https://78be-136-175-97-43.ngrok.io -> localhost:3000
-
Test your project is running:
IMPORTANT: The project rejects invalid requests (requests not from > your Moralis stream);
To test this using the below
cURL
(or Postman), comment out theMoralis.Streams.verifySignature({...})
section inindex.js
.You can curl (see below) or use a service like Postman to confirm the endpoints are functioning. I recommend doing this before you set up an external service (like Moralis) to point to your project.
-
Localhost test:
Replace
{{CHECK webhook-body_sample.json FOR EXAMPLE PAYLOAD}}
with > the actual example payload.curl --location --request POST 'localhost:3000/webhook' \ --header 'Content-Type: application/json' \ --data-raw '{{CHECK webhook-body_sample.json FOR EXAMPLE PAYLOAD}}'
-
Ngrok test:
Replace
{{CHECK webhook-body_sample.json FOR EXAMPLE PAYLOAD}}
with > the actual example payload.Get your
https
forwarding url from yourngrok
session, and replace{ngrok_url}
below:curl --location --request POST '{ngrok_url}/webhook' \ --header 'Content-Type: application/json' \ --data-raw '{{CHECK webhook-body_sample.json FOR EXAMPLE PAYLOAD}}'
-
-
Setup Moralis:
-
Go to your Moralis Dashboard
You can create a Moralis account here: https://admin.moralis.io/register
-
Go to Streams:
- Navigate to
"New Stream"
->"Create From Scratch"
- Navigate to
-
Fill in the following Stream Details:
# This will be for an example Polygon donation contract ## To find this manually: ## - Go to Polygonscan (using this contract as example: https://mumbai.polygonscan.com/address/0xbd317b46a004ccf0e1773dbfcee89e77e32d2db9#code) ## - copy the `Contract` address ## (0xbD317B46A004CCF0e1773DBFcEE89E77e32D2dB9) Address: "0xbD317B46A004CCF0e1773DBFcEE89E77e32D2dB9" Listen to all addresses: false Description: "Polygon new donations" # This is the `https` Ngrok url you got from previous steps. ## IMPORTANT: Don't forget to append the `/webhook` to the end! Webhook URL: "https://{your-ngrok-subdomain}.ngrok.io/webhook" Select Networks: - "Polygon Mumbai" Address Activity: - "Native Transactions (txs)" Event Emittance: true # (Application Binary Interface) # You will need this in order to select options under the next step for # "Select Topic0" ## Find this on the contract Polygonscan page: ## - Go to Polygonscan (using this contract as example: https://mumbai.polygonscan.com/address/0xbd317b46a004ccf0e1773dbfcee89e77e32d2db9#code) ## - Scroll down and select the `Contract` tab ## - Scroll down to the `Contract ABI` section ## - Click the "Copy ABI to clipboard" icon ABI: [excluded for brevity] Select Topic0: - "Donate(address,uint256)" # This is where you can add filtering before results hit your project. # NOTE: leave this blank for now. ## Filters: https://github.com/MoralisWeb3/streams-beta#filter-streams Advanced Options (optional): NULL
-
Create Stream and test:
-
Create Stream:
Click
Create Stream
to finish creating the stream -
Test:
-
on the contract Polygonscan page (using this contract as example):
-
Scroll to top and select the
Contract
tab -
Click the
Write Contract
button -
In your personal wallet (MetaMask, WalletConnect):
(I'll be displaying steps for MetaMask)
-
switch to the account you want to test with
-
switch to the
Polygon Mumbai
test network, or add it if needed:- Scroll down to the footer of the Polygonscan page
- Click the
Add Mumbai Network
button - In MetaMask click
Approve
- Allow this site to switch the network?
- Click
Switch network
- Click
-
Get Test MATIC:
If you don't already have a MATIC balance to test with:
-
You can use a Mumbai Faucet to get test MATIC:
NOTE: in the tutorial they send
2 MATIC
, but you likely won't have enough MATIC to send that; try sending something like0.01 MATIC
so you can test this as much as possible.(A "faucet" is a service that will send a small amount of a test token for free; they're not the most reliable, they typically limit the amount they will send you every X hours, and they usually send you VERY little. So make sure in your tests that you pay close attention to how much MATIC you're using.)
-
Some Mumbai MATIC faucets you can use:
(paste your wallet address you're testing with, and they will send you some test MATIC.)
- Alchemy's faucet: https://mumbaifaucet.com/
- Polygon's faucet: https://faucet.polygon.technology/
-
-
-
-
Click
Connect to Web3
:-
Select your wallet
(I'll be displaying steps for MetaMask)
-
Select the account you want to use
-
Click
Next
-
Click
Connect
-
Reload page and click
Connect to Web3
again -
Select
OK
for the popup:"Please take note that this is a beta version feature and is provided on an "as is" and "as available" basis. PolygonScan does not give any warranties and will not be liable for any loss, direct or indirect through continued use of this feature."
-
-
You should now see:
`Connected - Web3[{your-account-address-here}]
-
Expand
1. newDonation
:-
Enter an amount in the
newDonation
field(NOTE: see above "Get Test MATIC" step if you don't already have a test balance to work with.)
-
Click
Write
-
-
-
You should see items streaming in when the transaction finishes
-
-
-
When done:
-
Pause Stream:
When done testing,
Pause Stream
by hovering over the 3 dots (...) icon -
Activate Stream:
To start stream,
Activate Stream
by hovering over the 3 dots (...) icon
-
-