How to Send a Discord Message About a New Uniswap Pool
This tutorial shows you how to use a Web3 Action to send a message to a Discord channel when a new pool is created on Uniswap V3. You can use this Web3 Action to notify your community when something happens on the chain.
The entire code is available in this Github repo. Note, it’s in a separate branch at the moment.

Project Overview

The goal of this project is to teach you how to use Web3 Actions to send a Discord message whenever a Uniswap Pool is made. We’ll show you two approaches - the more convenient one is to use PoolCreated event as a trigger for our Web3 Action (see the Uniswap docs).

The Plan

  • Add contracts to your Tenderly project - UniswapV3ContractFactory. It’s already verified and present in Tenderly, so you just need to add them to your project.
  • Store the Discord Webhook URL in your Web3 Action Secrets.
  • Create a Web3 Action that will respond to the [PoolCreated](<https://docs.uniswap.org/protocol/reference/core/interfaces/IUniswapV3Factory#poolcreated>)event.

1: Add Uniswap Contracts to Your Tenderly Project

Web3 Actions require that smart contracts used to specify triggers are verified in Tenderly.
Web3 Actions require that smart contracts used to specify triggers are verified in Tenderly. Since we’re using existing contracts, you can find them using the search bar by typing in the contract name or address.
Alternatively, go to UniswapV2ContractFactory and click “Add to Project”. Do the same for UniswapV2Pair.

2: Create a Web3 Action for the PoolCreated Event

Below you’ll find the code for our Web3 Action as well as an explanation of how it works:
1
import {
2
ActionFn,
3
Context,
4
Event,
5
TransactionEvent,
6
} from '@tenderly/actions'
7
import axios from 'axios';
8
import { ethers } from 'ethers';
9
10
import UniswapV3FactoryAbi from './UniswapV3FactoryAbi.json'
11
12
export const onPoolCreatedEventEmitted: ActionFn = async (context: Context, event: Event) => {
13
try {
14
const txEvent = event as TransactionEvent;
15
16
const eventLog = await getPoolCreatedEvent(txEvent);
17
18
const tokensData = await getTokensData(eventLog.token0, eventLog.token1);
19
console.log("Received Tokens Data: ", tokensData);
20
21
await notifyDiscord(`${tokensData.token0.name} ↔️ ${tokensData.token1.name}`, context);
22
23
} catch (error) {
24
console.error(error);
25
}
26
}
27
28
const getPoolCreatedEvent = async (txEvent: TransactionEvent) => {
29
const ifc = new ethers.utils.Interface(UniswapV3FactoryAbi);
30
const poolCreatedTopic = ifc.getEventTopic("PoolCreated");
31
32
const poolCreatedEventLog = txEvent.logs.find(log => {
33
return log.topics.find(topic => topic == poolCreatedTopic) !== undefined
34
})
35
36
if (poolCreatedEventLog == undefined) {
37
throw Error("PoolCreatedEvent missing")
38
}
39
return ifc.decodeEventLog("PoolCreated",
40
poolCreatedEventLog.data,
41
poolCreatedEventLog.topics) as unknown as UniswapPool;
42
}
43
44
const getTokensData = async (token0: string, token1: string) => {
45
const tokenFields = `id, name, symbol, totalValueLocked, totalSupply, derivedETH`;
46
const theGraphQuery = `
47
{
48
token0: token(id:"${token0.toLowerCase()}"){${tokenFields}},
49
token1: token(id:"${token1.toLowerCase()}"){${tokenFields}}
50
}`;
51
52
const UNISWAP_THE_GRAPH = "https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3";
53
const resp = await axios.post(UNISWAP_THE_GRAPH, {
54
query: theGraphQuery
55
});
56
57
return resp.data.data as unknown as {
58
token0: UniwswapToken,
59
token1: UniwswapToken
60
}
61
}
62
63
const notifyDiscord = async (text: string, context: Context) => {
64
console.log('Sending to Discord:', `🐥 ${text}`)
65
const webhookLink = await context.secrets.get("discord.uniswapChannelWebhook");
66
await axios.post(
67
webhookLink,
68
{
69
'content': `🐥 ${text}`
70
},
71
{
72
headers: {
73
'Accept': 'application/json',
74
'Content-Type': 'application/json'
75
}
76
}
77
);
78
79
}
80
81
type UniswapPool = {
82
token0: string,
83
token1: string,
84
fee: number,
85
tickSpacing: number,
86
pool: number
87
}
88
89
type UniwswapToken = {
90
id: string,
91
name: string,
92
symbol: string,
93
tokenValueLocked: number,
94
totalSupply: number,
95
derivedEth: number
96
}
Copied!
Obtain Tokens in the Newly Created Pool. To get information about two tokens in the new pool, we need to search through the transaction logs in the getEventLog function. We’re using Ethers and the ABI of the contract: UniswapV3FactoryAbi. Go to the UniswapV3Factory contract in Tenderly, click “View ABI” and copy/paste it to a local file in the actions directory.
Essentially, we’re looking for the log entry in the topic PoolCreate. First, we’re taking a reference of that topic (poolCreatedTopic = ifc.getEventTopic("PoolCreated")) and then we’re filtering the actual logs (tx.logs) until we find the entry referencing that topic. Lastly, we’re decoding the log we found (poolCreatedEventLog) using Ethers. This is the event we’re looking for, and it contains the addresses of the tokens: token0 and token1.
Obtain Token Data Using Uniswap’s Graph API on TheGraph. Uniswap exposes access to its data through TheGraph. We’re using the getTokensData function to query TheGraph and get several fields for the token0 and token1 IDs – the ones present in the PoolCreated event from the first step.
Notify the Discord Community. Having gathered all the relevant information, we are ready to send a message to our Discord channel. With the notifyDiscord function, we need to get the Webhook URL from the secrets. We’re using Axios to make an HTTP post request to run the Webhook and pass the content.

3: Configure Your Web3 Action

In the tenderly.yaml file, we’re need to specify that we want to have uniswapActions:onPoolCreatedEventEmitted ran when an event is fired from the UniswapV3Factory contract (on address 0xd849b2af570ffa3033973ea11be6e01b7ba661d9) within a transaction that is mined on the Mainnet (network 1).
1
account_id: ""
2
actions:
3
YOUR_USERNAME/YOUR_PROJECT_SLUG:
4
runtime: v1
5
sources: actions
6
specs:
7
uniswapNewPool:
8
description: Runs when a new swap is made on Uniswap
9
function: uniswapActions:onPoolCreatedEventEmitted
10
trigger:
11
type: transaction
12
transaction:
13
status:
14
- mined
15
filters:
16
- network: 1
17
eventEmitted:
18
contract:
19
address: 0xd849b2af570ffa3033973ea11be6e01b7ba661d9
20
name: PoolCreated
21
project_slug: ""
Copied!

4: Store the Discord Webhook URL in Secrets

Before running our Web3 Action in Tenderly, we have to set the value for the Discord Webhook.
In the Tenderly Dashboard, go to the Actions page of your project and click Secrets. Add a new secret named discord.uniswapChannelWebhook and paste your Discord channel’s Webhook.

5: Deploy and Try Out Your Web3 Action

Deploy your Web3 Action by running this command:
1
tenderly actions deploy
Copied!
To try out if your Web3 Action is working, go to the Actions page in your Tenderly Dashboard. Select your Action and click Manual Trigger.
That’s all! Hopefully, you’ve received a message on your Discord channel 🎉