Adding notifications for Pending Transactions and Mined Transactions to your dApp
In this tutorial we cover how you can track your transactions throughout their lifecycle. We integrate Twilio to send SMS notifications when a transaction is pending and when it finally gets mined.
While dApps on Ethereum have become incredibly complex, one of the largest pain points for Ethereum users is the lack of transparency and clarity surrounding a transaction's life cycle. Oftentimes, dApp users are left with uncertainty surrounding pending transactions, forcing them to constantly refresh block explorers or their wallet dashboards to check if their transactions have been confirmed/mined.
dapp notifications example
For dApps, simple notifications that track transaction life cycles provide a valuable user experience that allows for higher customer engagement. In particular, they can help alleviate the stress involved in pending transactions as Ethereum's ever-increasing gas fees force users to set lower maxPriorityFeePerGas to save money and inadvertently increase wait times.
While building reliable transaction trackers has traditionally been complicated and unreliable, Alchemy Notify and Alchemy's pending transaction WebSocket allows us to monitor and send sending real-time push notifications regarding transaction life cycles.
At Alchemy, we've just launched Notify v2, which is an improved and more reliable version of Notify v1. You can read more about the announcement here​
In this tutorial, we’ll look at an example of how, with just a few lines of code, your dApp can integrate the power of Alchemy's Enhanced API suite, leveraging multiple Alchemy products to build a single feature to enhance user experience.

Overview

Our Example

For this example, we’ll be creating a notification system that automatically detects a user's address activity and sends an SMS text whenever a pending transaction has been made from their address, along with a follow-up text once that transaction has been mined. For our architecture, we’ll use a WebSocket script to detect pending transactions and a Webhook in conjunction with Alchemy Notify to monitor mined transactions.
NOTE: If you don't want to spend real (mainnet) Ethereum for test notifications in this tutorial, you can use any Ethereum testnet, like Goerli or Rinkeby, so that you can make transactions without paying for gas β›½! For access to free Goerli ETH, use the Alchemy Goerli faucet. For access to free Rinkeby ETH, use the Alchemy Rinkeby faucet.
Our tutorial specifically leverages two different Alchemy products to give a holistic picture of a transaction's inception and its completion.

"Pending transaction" notification workflow:

  1. 1.
    User initiates a transaction on MetaMask, through Alchemy, or any other RPC
  2. 2.
    Pending transaction is picked up by Alchemy
  3. 3.
    WebSocket script receives pending transaction
  4. 4.
    Script sends SMS notification
  5. 5.
    This code for this is in the file sniffer.py

"Mined transaction" notification workflow:

  1. 1.
    A pending transaction initiated by the user has been made and is confirmed by a miner
  2. 2.
    Alchemy Notify API picks up the mined transaction
  3. 3.
    WebHook endpoint is notified of the transaction
  4. 4.
    Webapp sends SMS notification
  5. 5.
    The code for this is in the file app.py
We’ll go through two versions of the tutorial: the first by cloning the Github Repo using Heroku and the second by doing it all from scratch.
Build with Heroku
Build from Scratch

Option 1: Build Heroku-Serviced Project

​

1. Set Up Github Repo & Heroku

​

a) Make a clone of the existing Github Repository​

Navigate to your command line and type:
1
git clone https://github.com/alchemyplatform/Transaction-Lifecycle-via-SMS.git
2
cd Transaction-Lifecycle-via-SMS
Copied!

​

b) Install Heroku-CLI and verify/install dependencies

In this tutorial, we utilize Heroku for hosting a server and website; if you choose to use Heroku, be sure to follow all of the following steps. If you want to use another provider, see Option 2: Build Project From Scratch​
  • Download Heroku-CLI based on your OS. Make sure you download the correct version based on what kind of computer environment you are using!
https://devcenter.heroku.com/articles/heroku-cli#download-and-install
  • After installation, navigate into the file that you just git cloned and run the following command in your command line to login to your Heroku account.
1
heroku login
Copied!
Follow the commands to login into your Heroku account. If you don't have a Heroku account, you can sign up for one for free!
​
  • Let's confirm that you have downloaded the correct version of Node. In your command line run:
1
node --version
Copied!
After running the command, you will either see a version number appear or you will instead get an error message telling you that you do not have Node installed.
Note that Heroku requires users to have any version of Node greater than 10 installed. If you don’t have it or have an older version, install a more recent version of Node.
​
  • Lastly, let's confirm that we also have npm installed properly. Again in your command line, run the following command:
1
npm --version
Copied!
npm is installed with Node, so check that it’s there. If you don’t have it, install a more recent version of Node.

​

c) Initiate Heroku

Now that we have confirmed that Heroku has all the dependencies it needs to run, let's create our Heroku app by running the following command:
1
heroku create
Copied!
​
You should then see something like this pop up:
Make sure you take note of the URL that pops up http://xxxxxxxxx.herokuapp.com/. We'll be using it since it's the URL of our sample dashboard!
NOTE: For more detailed instructions on setting up your environment to be configured for Heroku, check out the official Heroku docs.

​

d) Create a Twilio account

Twilio is an online SMS and voice provider that allows users to send text messages via the Internet. We use Twilio here but there are also other providers that provide similar services.
If you are new to Twilio, sign up for a trial account. With your trial account, you'll have enough credits to power your SMS notifications! Once you've signed up, head over to your Console and grab the below details from the account info section
  • Account SID
  • Auth Token
  • Phone Number
You'll need to plug these values in the code when using the Twilio API.
​
Note that sending messages through Twilio requires a Twilio phone number with SMS capabilities. If you don’t currently own a Twilio phone number with SMS capabilities (i.e. if the Phone Number doesn't show up in the above section), you’ll need to buy one with your provided credits. After navigating to the Buy a Number page, check the 'SMS' box and click 'Search' to find/buy a number that works for you!

2. Alchemy Notify API & Register Webhook Notifications

First, let’s look at how notifications with Alchemy work. There are two ways to create and modify notifications: through the Alchemy Notify dashboard, and through the Alchemy Notify API. For our example, we’ll only be using the dashboard.
NOTE: If you don’t already have one, you’ll first need to create an account on Alchemy. The free version will work fine for getting started.
​
Once you have an account, go to the dashboard and select β€œNotify” from the top menu. Here you’ll see the different kinds of notifications you can set up:
  • Address Activity
  • Dropped Transactions
  • Mined Transaction
  • Gas Price
​
For our example, we’ll use the Address Activity notification, but you should be able to easily swap out any of the others for your own use case.
NOTE: We use "Address Activity" and not "Mined Transaction Notifications" in this example since the "Mined Transaction" webhook only picks up on mined transactions made through the Alchemy API. "Address Activity" allows us to read all transactions via a user-defined address as long as it is posted onto the Ethereum blockchain.
​
In the dashboard, you can create all of your notifications, add the addresses you want to monitor, and add the webhook URL that Alchemy should communicate with when that notification triggers. Alchemy sends all the relevant details to this webhook. Your server needs to simply create the webhook, receive the call, and process the information as needed.

a) Create our example notification by clicking β€œCreate Webhook” on Address Activity.

b) Enter our webhook URL

  • If using Heroku, see Step 1 for the http://xxxxxxxxx.herokuapp.com/ URL that was generated with heroku create
  • If doing from scratch, insert your custom webhook URL from whichever service provider you are using.
​

c) Enter an Ethereum address that you want to monitor

While this tutorial is designed to monitor pending/mined transactions of a user's own wallet, this example can be easily adapted to monitor other addresses of interest!
​

d) Select an app from the dropdown menu

Make sure the app selected is on the Ethereum network you want to test on; if you're testing on Rinkeby, select an app configured for it!
​

e) Click β€œCreate Webhook” and we’re done!

3. Using alchemy_filteredNewFullPendingTransactions to track pending transactions

Once you have an account, you are now able to use thealchemy_filteredNewFullPendingTransactions method which allows you to receive notifications on pending asset transfers for a user-defined address.
For our transaction life cycle tracker, we make use of Alchemy's WebSockets so that we do not need to continuously make requests when you want specific information. WebSockets maintain a network connection for you (if done right) and listen for changes.
​
To get an Alchemy API key for WebSockets you will need to create an App in the Alchemy dashboard; if you created an app in the previous step, you can simply use that one!
NOTE: When you copy your key from the dashboard you should get a full url like this:
Your key is just the last portion in the URL:
Make the following changes to the file sniffer.py
  • On line 10, replace <TWILIO SID> with the Twilio SID you grabbed in step 1(d)
  • On line 11, replace <TWILIO AUTH TOKEN> with the Auth token you grabbed in step 1(d)
  • On line 13, replace <ALCHEMY KEY> with the Alchemy key from your dashboard
  • On line 25, replace the address in the parameters with the Ethereum Address you want to monitor
  • On line 50, replace the from number to the Twilio Number you copied in step 1(d)
  • On line 51, replace with your own phone number (with country code) that you want to receive the SMS on
​
1
from websocket import create_connection
2
​
3
ALCHEMY_KEY = "<Alchemy Key>"
4
​
5
for i in range(3):
6
try:
7
ws = create_connection("wss://eth-rinkeby.alchemyapi.io/v2/"+ALCHEMY_KEY)
8
print("Connection made")
9
except Exception as error:
10
print('Connection Error: ' + repr(error))
11
time.sleep(3)
12
else:
13
break
14
​
15
ws.send(json.dumps({"jsonrpc":"2.0","method":"eth_subscribe","params":["alchemy_filteredNewFullPendingTransactions", {"address": "0xcF3A24407aae7c87bd800c47928C5F20Cd4764D2"}],"id":1}))
16
print("JSON eth_subscribe sent")
Copied!
Opening a WebSocket connection can be done with a single line; however, in our forkable Heroku tutorial, we include 'retry' logic to ensure that we have a more stable WebSocket connection.
​
In this code snippet, we embed our wss connection in a for loop that runs three times to help ensure that our WebSocket is properly connected upon script initiation.
1
for i in range(3):
2
try:
3
ws = create_connection("wss://eth-rinkeby.alchemyapi.io/v2/"+ALCHEMY_KEY)
4
print("Connection made")
5
except Exception as error:
6
print('Connection Error: ' + repr(error))
7
time.sleep(3)
8
else:
9
break
Copied!
Configure this retry logic to suit your needs!

4. Configure SMS notifications

Make the following changes to the app.py file
  • On line 16, replace <TWILIO SID> with the Twilio SID you grabbed in step 1(d)
  • On line 17, replace <TWILIO AUTH TOKEN> with the Auth token you grabbed in step 1(d)
  • On line 52, replace the from number to the Twilio Number you copied in step 1(d)
  • On line 52, replace with your actual phone number (with country code) that you want to receive the SMS on
Make the SID and Auth Token changes in the below section of your code
1
# Find your Account SID and Auth Token at twilio.com/console
2
# and set the environment variables. See http://twil.io/secure
3
​
4
account_sid = '<TWILIO SID>'
5
auth_token = '<TWILIO AUTH TOKEN>'
6
client = Client(account_sid, auth_token)
Copied!
NOTE: If you are hosting a webapp on cloud computing services and plan to use environment variables, different computing environments have different ways of storing these variables.
​
Make the phone number changes in the below section of your code
1
message = client.messages \
2
.create(
3
body="\n \n PENDING TX! \n\n From: " + from_address + " \n\n To: " + to_address + "\n\n @tx:" + hash,
4
from_='+14435267244',
5
to='+14158230041'
6
)
Copied!

​

5. Deploy Heroku App!

Now, we're in the final steps! Confirm that you are navigated to the file that your Heroku project lives within. Once there, run the following commands to save your changes on Git and deploy the app.
1
git add . // to add changes
2
git commit -m "added Alchemy / Twilio keys" // to add a comment
3
git push heroku master // to push and deploy your heroku app
Copied!
​
With that, we have pushed all changes to Heroku and our app is live!
NOTE: This app has no frontend and is configured for use on a server.
​
To test the app and check on its status, log in to Heroku and navigate the logs for your deployed webapp.
The log should look like the following! Both a worker and web file should be running.
And now, with everything in place, you can test out your dApp!
An alternate way to check your heroku logs is to go to your terminal, type the command heroku logs -t and hit enter
​
You can also use this command or the above dashboard to debug any errors with your heroku deployment
​

Check Your Integration βœ…

Load up your MetaMask wallet and make a transfer of testnet ETH from the wallet address that you inputted into sniffer.py .
Upon making the transaction, you should receive a text message stating its pending status.
This text message is sent by the script sniffer.py and can also be sent by your local systems by running the command
python sniffer.py
Similarly, upon miner confirmation of that transaction, the Heroku webapp also sends an SMS alert with the change in transaction status!
πŸŽ‰ Congratulations on your dApp deployment! Feel free to edit your webapp, point the target address at other interesting contracts / public figures, or make improvements to this transaction life cycle tracker!

Option 2: Build project from scratch

In this tutorial, we provide a generalized setup for a Python WebApp that allows you to use a WebSocket/Webhook connection to process/send alerts regarding pending and mined transactions over SMS.

1-2. Complete Steps 1-2 from the Heroku Project.

3. Create WebSocket Connection

For our transaction life cycle tracker, we make use of Alchemy's WebSockets so that we do not need to continuously make requests when you want specific information. WebSockets maintain a network connection for you (if done right) and listen for changes, alerting you in real-time.
For more details on WebSockets vs HTTP requests, use this for reference.
​

a) Install dependencies

To create a WebSocket connection, we use a Python client to help simplify our build. To use the WebSocket client and other Python packages, we have a number of dependencies that must be addressed. Make sure that you have the following dependencies in your environment to follow along.
  • One easy way to install the necessary dependencies is to create a file named requirements.txt with the following items inside of it.
1
aiohttp==3.7.4.post0
2
async-timeout==3.0.1
3
attrs==21.2.0
4
backports.entry-points-selectable==1.1.0
5
base58==2.1.0
6
bitarray==1.2.2
7
certifi==2021.5.30
8
chardet==4.0.0
9
charset-normalizer==2.0.3
10
cytoolz==0.11.0
11
distlib==0.3.2
12
eth-abi==2.1.1
13
eth-account==0.5.5
14
eth-hash==0.3.1
15
eth-keyfile==0.5.1
16
eth-keys==0.3.3
17
eth-rlp==0.2.1
18
eth-typing==2.2.2
19
eth-utils==1.10.0
20
filelock==3.0.12
21
hexbytes==0.2.1
22
idna==3.2
23
ipfshttpclient==0.7.0
24
jsonschema==3.2.0
25
lru-dict==1.1.7
26
mpmath==1.2.1
27
multiaddr==0.0.9
28
multidict==5.1.0
29
netaddr==0.8.0
30
parsimonious==0.8.1
31
platformdirs==2.1.0
32
protobuf==3.17.3
33
pycryptodome==3.10.1
34
PyJWT==1.7.1
35
pyrsistent==0.18.0
36
pytz==2021.1
37
pywin32==301
38
rd==1.0.0.3
39
requests==2.26.0
40
rlp==2.0.1
41
six==1.16.0
42
sympy==1.8
43
toolz==0.11.1
44
twilio==6.62.1
45
typing-extensions==3.10.0.0
46
urllib3==1.26.6
47
varint==1.0.2
48
virtualenv==20.6.0
49
web3==5.21.0
50
websocket-client==1.1.0
51
websockets==9.1
52
yarl==1.6.3
Copied!
Then, run the following command to install the packages:
1
pip install requirements.txt
Copied!
​
b) Create a file called sniffer.py
This is where our WebSocket script will live and allow us to monitor pending transaction information. We'll also add a few installations and define a few key variables at the top.
Be sure to change the Twilio and Alchemy Keys to reflect your particular Twilio Account SID / Auth Token and the Alchemy Key on your dashboard.
1
import json, time
2
import requests
3
from websocket import create_connection
4
import os
5
from twilio.rest import Client
6
import pickle
7
​
8
# Find your Account SID and Auth Token at twilio.com/console
9
# and set the environment variables. See http://twil.io/secure
10
account_sid = '<TWILIO SID>'
11
auth_token = '<TWILIO AUTH TOKEN>'
12
client = Client(account_sid, auth_token)
13
ALCHEMY_KEY = '<YOUR ALCHEMY KEY>'
Copied!
​
c) Initiate WebSocket connection
In our tutorial, we use WebSockets to receive pending transaction activity from an address that we pass into our wss request.
To initiate our WebSocket connection using the Python client, we can add the following lines to sniffer.py:
1
for i in range(3):
2
try:
3
ws = create_connection("wss://eth-rinkeby.alchemyapi.io/v2/"+ALCHEMY_KEY)
4
print("Connection made")
5
except Exception as error:
6
print('Connection Error: ' + repr(error))
7
time.sleep(3)
8
else:
9
break
Copied!
NOTE: We embed our wss connection in a for loop that runs three times to help ensure that our WebSocket is properly connected upon script initiation.
After initiating the connection, we can then send a JSON message dictating what type of information we want to receive from the WebSocket itself. This can be achieved with a single line:
1
ws.send(json.dumps({"jsonrpc":"2.0","method":"eth_subscribe","params":["alchemy_filteredNewFullPendingTransactions", {"address": "0xcF3A24407aae7c87bd800c47928C5F20Cd4764D2"}],"id":1}))
Copied!
Breaking down our JSON message, we send the following:
1
{
2
"jsonrpc":"2.0",
3
"method":"eth_subscribe",
4
"params":[
5
"alchemy_filteredNewFullPendingTransactions", {
6
"address": "0x6B3595068778DD592e39A122f4f5a5cF09C90fE2"
7
}
8
],
9
"id":1
10
}
Copied!
Notice how we use thealchemy_filteredNewFullPendingTransactions method which allows us to receive notifications on pending asset transfers for a user-defined address.
Remember to change the address field to reflect the address/wallet that you want to monitor!
​
d) Parse WebSocket response & Send SMS text message
Now that we are able to create a WebSocket and send our request, we must listen for a response, parse it, and then act on the notification.
We use a while trueloop to force our script to continuously listen for a response and wrap our parsing code within the loop so that we can interpret the notification. Here, we also use the jsonpackage to allow us to read the information in a structured manner.
Add the following code snippet to your sniffer.py file to parse incoming JSON messages.
1
while True:
2
try:
3
result = ws.recv()
4
result = json.loads(result)
5
from_address = (result["params"]["result"]["from"])
6
to_address = (result["params"]["result"]["to"])
7
hash = (result["params"]["result"]["hash"])
8
blockHash = (result["params"]["result"]["blockNumber"])
9
​
10
print("from:", from_address)
11
print("to:", to_address)
12
print("hash: ", hash)
13
print("blockHash: ", blockHash)
14
​
15
except KeyError as error:
16
print("Check JSON params for parsing")
17
​
18
except Exception as error:
19
print('JSON Error: ' + repr(error))
20
time.sleep(1)
Copied!
Now that we are able to parse and read the JSON response from the WebSocket, we also need to include a few lines of code to enable us to send an SMS with the information to our phone number.
In thesniffer.pyfile within the while loop we started above, include the following lines. Make sure to replace the phone numbers in the Twilio script to reflect the Twilio phone number that you acquired previously in the from field and your own phone number in the to field!
1
message = client.messages \
2
.create(
3
body="\n \n PENDING TX! \n\n From: " + from_address + " \n\n To: " + to_address + "\n\n @tx:" + hash,
4
from_='+14435267244',
5
to='+14158230041'
6
)
7
​
8
print(message.sid)
Copied!
In this snippet, you tell Twilio to send an SMS by sending a message with 3 parameters.
As stated above, the from and to parameters must use E.164 formatting (+ and a country code, e.g., +16175551212). Without the proper formatting, your message will not send properly!
Lastly, we have the body parameter, which contains the content of the SMS we’re going to send. In our example, we have a single string that we format and structure with numerous '\n' to create a newline in our SMS so that our users can more easily understand the information presented.
Our script is ready! Here is the entire sample sniffer.py we have created together:
1
import json, time
2
import requests
3
from websocket import create_connection
4
import os
5
from twilio.rest import Client
6
import pickle
7
​
8
# Find your Account SID and Auth Token at twilio.com/console
9
# and set the environment variables. See http://twil.io/secure
10
account_sid = '<TWILIO SID>'
11
auth_token = '<TWILIO AUTH TOKEN>'
12
client = Client(account_sid, auth_token)
13
ALCHEMY_KEY = '<ALCHEMY KEY>'
14
​
15
for i in range(3):
16
try:
17
ws = create_connection("wss://eth-rinkeby.alchemyapi.io/v2/"+ALCHEMY_KEY)
18
print("Connection made")
19
except Exception as error:
20
print('Connection Error: ' + repr(error))
21
time.sleep(3)
22
else:
23
break
24
​
25
ws.send(json.dumps({"jsonrpc":"2.0","method":"eth_subscribe","params":["alchemy_filteredNewFullPendingTransactions", {"address": "0xccD3dd576e715b0E060169e16C39Cd7E6eEdeF51"}],"id":1}))
26
print("JSON eth_subscribe sent")
27
​
28
while True:
29
try:
30
result = ws.recv()
31
result = json.loads(result)
32
from_address = (result["params"]["result"]["from"])
33
to_address = (result["params"]["result"]["to"])
34
hash = (result["params"]["result"]["hash"])
35
blockHash = (result["params"]["result"]["blockNumber"])
36
​
37
print("from:", from_address)
38
print("to:", to_address)
39
print("hash: ", hash)
40
print("blockHash: ", blockHash)
41
​
42
print("Send Twilio SMS for pending transaction!")
43
message = client.messages \
44
.create(
45
body="\n \n PENDING TX! \n\n From: " + from_address + " \n\n To: " + to_address + "\n\n @tx:" + hash,
46
from_='+14435267244',
47
to='+14158130071'
48
)
49
​
50
print(message.sid)
51
​
52
except KeyError as error:
53
print("Check JSON params for parsing")
54
​
55
except Exception as error:
56
print('JSON Error: ' + repr(error))
57
time.sleep(1)
58
​
59
ws.close()
60
​
Copied!

​

4. Create WebHook Connection

With our WebSocket script above, we are able to receive information about pending transactions. Now, we want to receive information on transactions after they have been mined! For this, we use WebHooks combined with Alchemy Notify.
​
a) Create a file named app.py
This part of the tutorial is primarily built with Python and Flask. Here, we add a few installations and define a few key variables at the top.
Be sure to change the Twilio and Alchemy Keys to reflect your particular Twilio Account SID / Auth Token!
1
import os
2
from flask import Flask
3
from flask import request
4
from webhook import webhook
5
from twilio.rest import Client
6
import json, time
7
import requests
8
from websocket import create_connection
9
import os
10
import pickle
11
​
12
from twilio.rest import Client
13
​
14
# Find your Account SID and Auth Token at twilio.com/console
15
# and set the environment variables. See http://twil.io/secure
16
account_sid = '<TWILIO SID>'
17
auth_token = '<TWILIO AUTH TOKEN>'
18
client = Client(account_sid, auth_token)
Copied!
​
b) Configure Flask routing
To ensure that JSON data that hits our WebHook endpoint triggers an SMS text, we need to route incoming POST requests to trigger the main body of our Python logic.
Add the following to your app.py file:
1
app = Flask(__name__)
2
app.debug = True
3
queue = []
4
​
5
@app.route('/', methods=['POST', 'GET'])
6
​
7
def request_handler():
8
​
9
if request.method == 'POST':
10
data = (request.json)
Copied!
After detecting a POST request, we want to be able to parse the JSON message. Here, we also use the json package to allow us to read the information in a structured manner. By decoding the JSON, we can pull out key pieces of information that users find most useful when tracking transactions- from_address,_ to_address, blockNumber, _and hash.
​
Add the following code snippet to the request handler function:
1
if request.method == 'POST':
2
data = (request.json)
3
if len(data['event']['activity'])==1:
4
timestamp = data['createdAt']
5
from_address = data['event']['activity'][0]['fromAddress']
6
to_address = data['event']['activity'][0]['toAddress']
7
blockNum = data['event']['activity'][0]['blockNum']
8
hash = data['event']['activity'][0]['hash']
9
​
10
​
11
else:
12
for i in range(len(data['event']['activity'])):
13
timestamp = data['createdAt']
14
from_address = data['event']['activity'][i]['fromAddress']
15
to_address = data['event']['activity'][i]['toAddress']
16
blockNum = data['event']['activity'][i]['blockNum']
17
hash = data['event']['activity'][i]['hash']
18
​
19
​
20
print("DATA: ", data)
21
print("HASH: ", hash)
Copied!
Now that we are able to parse and read the JSON response from Alchemy Notify, we also need to include a few lines of code to enable us to send an SMS with the information to our phone number.
Make sure to replace the phone numbers to reflect the Twilio phone number that you acquired previously in the**from** field and your own phone number in the**to **field! Add the following two lines after parsing the POST request.
1
message = client.messages.create(body=" \n\n TX MINED! \n\n From: " + from_address + " \n\n To: " + to_address + " \n\n @#:" + blockNum + " \n Check tx: https://rinkeby.etherscan.io/tx/" +hash ,from_='+14435267241', to='+14158330071')
2
print(message.sid)
Copied!
​
With the main sections of our app.py complete, here's the sample script that we wrote together:
1
# -*- coding: utf-8 -*-
2
import os
3
from flask import Flask
4
from flask import request
5
from twilio.rest import Client
6
import json, time
7
import requests
8
from websocket import create_connection
9
import os
10
import pickle
11
​
12
from twilio.rest import Client
13
​
14
# Find your Account SID and Auth Token at twilio.com/console
15
# and set the environment variables. See http://twil.io/secure
16
account_sid = '<TWILIO SID>'
17
auth_token = '<TWILIO AUTH TOKEN>'
18
client = Client(account_sid, auth_token)
19
​
20
​
21
app = Flask(__name__)
22
app.debug = True
23
queue = []
24
​
25
@app.route('/', methods=['POST', 'GET'])
26
​
27
def request_handler():
28
print("Sending Twilio SMS for Mined transaction if webhook received!")
29
if request.method == 'POST':
30
data = (request.json)
31
if len(data['event']['activity'])==1:
32
timestamp = data['createdAt']
33
from_address = data['event']['activity'][0]['fromAddress']
34
to_address = data['event']['activity'][0]['toAddress']
35
blockNum = data['event']['activity'][0]['blockNum']
36
hash = data['event']['activity'][0]['hash']
37
​
38
​
39
else:
40
for i in range(len(data['event']['activity'])):
41
timestamp = data['createdAt']
42
from_address = data['event']['activity'][i]['fromAddress']
43
to_address = data['event']['activity'][i]['toAddress']
44
blockNum = data['event']['activity'][i]['blockNum']
45
hash = data['event']['activity'][i]['hash']
46
​
47
​
48
print("DATA: ", data)
49
print("HASH: ", hash)
50
​
51
​
52
message = client.messages.create(body=" \n\n TX MINED! \n\n From: " + from_address + " \n\n To: " + to_address + " \n\n @#:" + blockNum + " \n Check tx: https://rinkeby.etherscan.io/tx/" +hash ,from_='+14415267244', to='+14154230071')
53
print(message.sid)
54
​
55
​
56
return ("Ok")
57
#return webhook(session), 200
58
​
59
def run():
60
app.run(host='0.0.0.0', port=5000)
61
​
62
if __name__ == "__main__":
63
port = int(os.environ.get("PORT", 5000))
64
app.run(host='0.0.0.0', port=port)
Copied!

​

5. Deploy App

Now, we're in the final steps! With custom hosting solutions, you have the freedom to either run sniffer.py and app.py in the same environment or in two separate hosting platforms. Deploy both files in your desired environment!
NOTE: This app has no frontend and is configured for use on a server.
​
If you are able to have command line access to your files, take a look at the CLI readouts and make sure that they resemble the following start-up process for the scripts.
And now, with everything in place, you can test out your dApp!
​
πŸŽ‰ Congratulations on your dApp deployment! Feel free to edit your webapp, point the target address at other interesting contracts / public figures, or make improvements to this transaction life cycle tracker!

Conclusion

Real-time information on a transaction life cycle is a small but important part of building a stress-free user experience. While this infrastructure was not previously available, with Alchemy's Enhanced API suite, your users can stay informed and confident about understanding their transaction activity, regardless of whether they're on their desktop madly clicking refresh or out in nature with only a cellular network.
And that's it! You now know how to use Alchemy Notify v2 to add notifications to your dApp! If you enjoyed this tutorial for setting Alchemy Notify on your dApp, give us a tweet @AlchemyPlatform! (Or if you have any questions/feedback give the authors @crypt0zeke and @ankg404 a shoutout!)
Don't forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs! Ready to start using Alchemy Notify? Create a free Alchemy account and do share your project with us
πŸŽ‰
!