Git Product home page Git Product logo

gm-hack's Introduction

GM Hackers (Web3Inbox, Next.js, wagmi, React, TypeScript)

🔗 Live dapp demo - https://hackers.gm.walletconnect.com/
📚 WalletConnect Web3Inbox Docs - https://docs.walletconnect.com/2.0/web3inbox/core-components/usage

Overview

Screenshot 2023-09-22 at 7 29 48 PM

Send notifications to a blockchain account using WalletConnect's Web3Inbox SDK.

This example dapp shows basic dapp usage of the Web3Inbox SDK to send a notification whenever there is a new Ethereum block.

Subscriptions to dapp notifications are synced across all devices that use the same blockchain account.

Hacker guide

Watch the tutorial guide 🖥️

https://www.loom.com/share/a7001711b8a94500b827a9d2655c8654?sid=1a36ccfa-9ee8-42a5-a882-9b78ac6e846a

Deploy the example dapp

  1. Head over WalletConnect Cloud and Sign in or Sign up if you don't have an account.

  2. Create a project and take note of your Project ID.

  3. Deploy your app to a public URL. Note you will need to set the NEXT_PUBLIC_PROJECT_ID environment variable to your Project ID from step #2. Some options to create your repo and deploy to a public URL include:

  4. Back in the WalletConnect Cloud, navigate to your project's APIs tab. Under Notify API Step 1, provide your public URL as the dapp URL. Click Save.

  5. Still on Notify API section, you should see a Notify API Secret. Copy this secret into your deployment as the NOTIFY_API_SECRET environment variable. Make sure to update the environment variables on your local environment as well as on your deployment platform.

  6. Next, you will need to host the two files on this page at the /.well-known/ directory of your public URL. You can do this by saving them to the /public/.well-known/ directory of your fork of this template repo. Note that you will need to overwrite the two files that already exist.

    • Download did.json (Step 2: “Download did:web”) and place it at /public/.well-known/did.json
    • Download wc-notify-config.json (Step 3: “Download template”) to /public/.well-known/wc-notify-config.json
      • Update the description field in wc-notify-config.json to the description of your app.
    • Update your NEXT_PUBLIC_APP_DOMAIN environment variable to include the hostname of your deployment.
    • Deploy your changes in /public/.well-known/ to your public URL (e.g. by committing and pushing).
  7. Once the new files are deployed, on the APIs tab in Cloud, find the toggle switch next to the Notify API section and switch it on. You should see a success toast: "Notify configuration successfully verified"

Managing environment variables

Test and view notifications

Now you should have a fully functioning dapp that is capable of sending notifications with the Web3Inbox SDK.

First, test that you can subscribe to notifications by going to your dapp's public URL, connect your wallet, sign the SIWE message in your wallet, and press subscribe. Approximately every 12 seconds you should see a "New block" notification in the dapp's UI.

To quality for bounties, notifications must be visible app.web3inbox.com or one of our sample wallets that supports Web3Inbox notifications. Note that these automated notifications will only be sent to your account if you have the UI open and with the same account connected.

Sample wallets to test notifications:

Sending notifications

  1. Send test notification button in the gm-hackers dapp.

  2. Use WalletConnect Cloud broadcast feature.

  3. Make a request to Notify API. The example below showcases how to send a notification to all subscribers.

    // Your project ID from WalletConnect Cloud
    const projectId = process.env.NEXT_PUBLIC_PROJECT_ID;
    // notify_api_secret generated in WalletConnect Cloud
    const notifyApiSecret = process.env.NOTIFY_API_SECRET;
    
    const headers = {
      Authorization: `Bearer ${notifyApiSecret}`,
    };
    
    // 1. Get the list of subscribers for your project
    const subscribersRes = await fetch(
      `https://notify.walletconnect.com/${projectId}/subscribers`,
      { headers }
    );
    const subscribers = await subscribersRes.json();
    
    // 2. Send a notification to all your subscribers
    const body = JSON.stringify({
      accounts: subscribers,
      notification: {
        title: "GM Hackers!",
        body: "Enjoy hacking on WalletConnect",
        icon: "https://avatars.githubusercontent.com/u/37784886?s=48&v=4",
        url: "https://hackers.gm.walletconnect.com/",
        type: "promotional",
      },
    });
    
    const notifyRes = await fetch(
      `https://notify.walletconnect.com/${projectId}/notify`,
      {
        method: "POST",
        headers,
        body,
      }
    );
    const result = await notifyRes.json();
    console.log(result);

Running locally

Install the app's dependencies:

npm i

Develop

npm run dev

Build

npm run build

Expose domain

The aforementioned did.json file needs to be hosted on a publically available domain. Although Vercel and the like work great for the actual deployment, any dev working with notify might like a hot-reloadable deployment. This is where tunnels come in.

Follow the following instructions to expose your app from localhost to be publically available:

Instructions adapted from Cloudflare

  1. Download cloudflared utility:
    • MacOS: brew install cloudflared
    • Ubuntu/Debian: apt install cloudflared
    • Windows: Download from here
  2. Run your app in localhost
npm run dev
  1. Expose your app
cloudflared tunnel --url http://localhost:3000

Once you've got that running, you can access your local app from a public domain that looks like:

https://some-combination-of-words.trycloudflare.com

Hack ideas

  • A DeFi app that sends notifications for positions being liquidated, claimable rewards, etc.
  • A PWA that sends desktop notifications.
  • Segment subscribers and send them notifications relevant to their on-chain activity.
  • Use your own database and integrate with our webhook.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.