|
| 1 | +# 🔁 BNB Chain → Discord PingPong Stream |
| 2 | + |
| 3 | +This Firebase Functions project listens for `Pinged` and `Ponged` events from a smart contract via Moralis Streams, decodes logs using the ABI, and forwards only confirmed events to a Discord Webhook. |
| 4 | + |
| 5 | +### 🛠️ Deployment Instructions |
| 6 | + |
| 7 | +- Navigate to the `smart-contract` folder. |
| 8 | +- Deploy the `PingPong` contract using **Remix** or **Foundry** to your target network. |
| 9 | + |
| 10 | +## 🚀 On-chain Deployment |
| 11 | + |
| 12 | +The PingPong smart contract is deployed at: |
| 13 | + |
| 14 | +👉 [0x52943bFb088221cd6E3181fbc19081A6B34be948](https://testnet.bscscan.com/address/0x52943bfb088221cd6e3181fbc19081a6b34be948#code) |
| 15 | + |
| 16 | + |
| 17 | +## 🔔 Notifications |
| 18 | + |
| 19 | +You can view real-time Ping/Pong event notifications in the Discord channel **#onchain-ping-pong-notifications**: |
| 20 | + |
| 21 | +👉👉👉 https://discord.gg/xVzRZ9xjYm |
| 22 | + |
| 23 | +### Example Notifications |
| 24 | + |
| 25 | +> **PingPongNotifier • APP — 7:36 PM** |
| 26 | +> 🏓 **Ping event received!** |
| 27 | +> 👤 Sender: `0xc02024B4d446E91253Da8549805553Ac34F9D572` |
| 28 | +> 🔢 Ping #: 2 |
| 29 | +> 🕒 Time: *July 11, 2025 at 7:35 PM* |
| 30 | +
|
| 31 | +> **PingPongNotifier • APP — 7:48 PM** |
| 32 | +> 🏓 **Pong event received!** |
| 33 | +> 👤 Sender: `0xc02024B4d446E91253Da8549805553Ac34F9D572` |
| 34 | +> 🔢 Pong #: 2 |
| 35 | +> 🕒 Time: *July 11, 2025 at 7:47 PM* |
| 36 | +
|
| 37 | + |
| 38 | + |
| 39 | +## ✅ Features |
| 40 | + |
| 41 | +- **Two HTTPS Functions:** |
| 42 | + - `/pingedStream` for `Pinged` events |
| 43 | + - `/pongedStream` for `Ponged` events |
| 44 | +- Processes only `confirmed: true` events |
| 45 | +- Decodes logs with Moralis SDK and ABI |
| 46 | +- Forwards formatted messages to Discord Webhook |
| 47 | +- Supports `.env` for local development and `firebase functions:config` for production |
| 48 | + |
| 49 | +## 📁 Project Structure |
| 50 | + |
| 51 | +``` |
| 52 | +functions/ |
| 53 | +├── src/ |
| 54 | +│ └── index.ts # Function logic |
| 55 | +├── .env # Local development secrets |
| 56 | +├── package.json |
| 57 | +├── tsconfig.json |
| 58 | +└── README.md |
| 59 | +``` |
| 60 | + |
| 61 | +## ⚙️ Setup & Deployment |
| 62 | + |
| 63 | +### 1. Install dependencies |
| 64 | + |
| 65 | +```bash |
| 66 | +cd functions |
| 67 | +npm install |
| 68 | +``` |
| 69 | + |
| 70 | +### 2. Configure Discord Webhook URL |
| 71 | + |
| 72 | +#### Local development via `.env` |
| 73 | + |
| 74 | +1. Create `functions/.env` with: |
| 75 | + ```env |
| 76 | + DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/XXX/YYY" |
| 77 | + ``` |
| 78 | +2. In `src/index.ts`, at the top add: |
| 79 | + ```ts |
| 80 | + import * as dotenv from "dotenv"; |
| 81 | + dotenv.config(); |
| 82 | + |
| 83 | + const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL; |
| 84 | + if (!DISCORD_WEBHOOK_URL) throw new Error("Missing DISCORD_WEBHOOK_URL"); |
| 85 | + ``` |
| 86 | + |
| 87 | +#### Production via Firebase config |
| 88 | + |
| 89 | +1. Run: |
| 90 | + ```bash |
| 91 | + firebase functions:config:set discord.webhook_url="https://discord.com/api/webhooks/XXX/YYY" |
| 92 | + ``` |
| 93 | +2. In `src/index.ts`, replace loading with: |
| 94 | + ```ts |
| 95 | + import * as functions from "firebase-functions"; |
| 96 | + |
| 97 | + const DISCORD_WEBHOOK_URL = functions.config().discord.webhook_url; |
| 98 | + if (!DISCORD_WEBHOOK_URL) throw new Error("Missing config.discord.webhook_url"); |
| 99 | + ``` |
| 100 | + |
| 101 | +### 3. Deploy |
| 102 | + |
| 103 | +```bash |
| 104 | +firebase deploy --only functions |
| 105 | +``` |
| 106 | + |
| 107 | +### 4. Local testing |
| 108 | + |
| 109 | +```bash |
| 110 | +firebase emulators:start |
| 111 | +``` |
| 112 | + |
| 113 | +Send a POST request with JSON payload including `"confirmed": true` to: |
| 114 | + |
| 115 | +- `http://localhost:5001/<PROJECT_ID>/us-central1/pingedStream` |
| 116 | +- `http://localhost:5001/<PROJECT_ID>/us-central1/pongedStream` |
| 117 | + |
| 118 | +## 🔗 Moralis Stream Setup |
| 119 | + |
| 120 | +1. Log in at [Moralis Admin](https://admin.moralis.io/) → **Streams** → **New Stream** |
| 121 | +2. Create two streams: |
| 122 | +👉 USER YOUR CREATED WEBHOOK FIREBASE URLS |
| 123 | + |
| 124 | + - **Pinged** → Webhook URL: `https://<YOUR_APP>.cloudfunctions.net/pingedStream` |
| 125 | + - **Ponged** → Webhook URL: `https://<YOUR_APP>.cloudfunctions.net/pongedStream` |
| 126 | +3. In each stream’s **ABI** section, paste the corresponding JSON: |
| 127 | + |
| 128 | +<details> |
| 129 | +<summary><strong>Pinged ABI</strong></summary> |
| 130 | + |
| 131 | +```json |
| 132 | +[ |
| 133 | + { |
| 134 | + "anonymous": false, |
| 135 | + "inputs": [ |
| 136 | + { "indexed": true, "internalType": "address", "name": "sender", "type": "address" }, |
| 137 | + { "indexed": false, "internalType": "uint256", "name": "pingCount","type": "uint256" }, |
| 138 | + { "indexed": false, "internalType": "uint256", "name": "timestamp","type": "uint256" } |
| 139 | + ], |
| 140 | + "name": "Pinged", |
| 141 | + "type": "event" |
| 142 | + } |
| 143 | +] |
| 144 | +``` |
| 145 | +</details> |
| 146 | + |
| 147 | +<details> |
| 148 | +<summary><strong>Ponged ABI</strong></summary> |
| 149 | + |
| 150 | +```json |
| 151 | +[ |
| 152 | + { |
| 153 | + "anonymous": false, |
| 154 | + "inputs": [ |
| 155 | + { "indexed": true, "internalType": "address", "name": "sender", "type": "address" }, |
| 156 | + { "indexed": false, "internalType": "uint256", "name": "pongCount", "type": "uint256" }, |
| 157 | + { "indexed": false, "internalType": "uint256", "name": "timestamp", "type": "uint256" } |
| 158 | + ], |
| 159 | + "name": "Ponged", |
| 160 | + "type": "event" |
| 161 | + } |
| 162 | +] |
| 163 | +``` |
| 164 | +</details> |
| 165 | + |
| 166 | +4. Enable **only confirmed transactions will be sent by the webhook**. |
| 167 | + |
| 168 | +## 🧩 Tech Stack |
| 169 | + |
| 170 | +- **Solidity** |
| 171 | +- **Firebase Functions v2** (Node.js 18+) |
| 172 | +- **TypeScript** |
| 173 | +- **Moralis SDK** (`@moralisweb3/core`, `@moralisweb3`) |
| 174 | +- **Axios** |
| 175 | +- **Discord Webhook API** |
| 176 | + |
| 177 | +## 📜 License |
| 178 | + |
| 179 | +MIT |
0 commit comments