Skip to content

Commit

Permalink
chore(alchemy): env values validation
Browse files Browse the repository at this point in the history
  • Loading branch information
gaboesquivel committed Aug 22, 2024
1 parent a9705fc commit 9342895
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
4 changes: 3 additions & 1 deletion apps/alchemy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
},
"dependencies": {
"alchemy-sdk": "^3.4.1",
"dotenv": "^16.4.5"
"dotenv": "^16.4.5",
"viem": "^2.20.0",
"zod": "^3.23.8"
}
}
24 changes: 24 additions & 0 deletions apps/alchemy/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { z } from 'zod'
import { isAddress } from 'viem'
import dotenv from 'dotenv'

dotenv.config()

const envSchema = z.object({
ALCHEMY_NOTIFY_TOKEN: z.string().min(1, 'Alchemy notify token is required'),
ALCHEMY_ACTIVITY_WEBHOOK_URL: z.string().url('Invalid webhook URL'),
PRESALE_ADDRESS: z.string().refine(isAddress, 'Invalid Ethereum address'),
})

const parsedEnv = envSchema.safeParse(process.env)
if (!parsedEnv.success) {
console.error('Environment validation failed:', parsedEnv.error.format())
process.exit(1)
}


export const appConfig = {
alchemyNotifyToken: parsedEnv.data.ALCHEMY_NOTIFY_TOKEN,
alchemyActivityWebhookUrl: parsedEnv.data.ALCHEMY_ACTIVITY_WEBHOOK_URL,
presaleAddress: parsedEnv.data.PRESALE_ADDRESS,
}
22 changes: 10 additions & 12 deletions apps/alchemy/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
require('dotenv').config()
import { Alchemy, Network, WebhookType } from 'alchemy-sdk'
import { appConfig } from './config'

// authToken is required to use Notify APIs. Found on the top right corner of
// https://dashboard.alchemy.com/notify.
async function createAddressActivityNotification() {
const settings = {
authToken: process.env.ALCHEMY_NOTIFY_TOKEN,
network: Network.MATIC_MAINNET, // Replace with your network.
authToken: appConfig.alchemyNotifyToken,
network: Network.MATIC_MAINNET // Replace with your network.
}

const alchemy = new Alchemy(settings)
const addressActivityWebhook = await alchemy.notify.createWebhook(
// TO DO: You will replace this URL in Step #3 of this guide!
'https://launchpad-indexer-ymrgicuyta-uc.a.run.app/alchemy',
appConfig.alchemyActivityWebhookUrl,
WebhookType.ADDRESS_ACTIVITY,
{
// use any address you want to monitor activity on!
addresses: ['0x6F76670A66e7909Af9B76f0D84E39317FCc0B2e1'],
network: Network.MATIC_MAINNET,
},
addresses: [appConfig.presaleAddress],
network: Network.MATIC_MAINNET
}
)
console.log('Address Activity Webhook Details:')
console.log(JSON.stringify(addressActivityWebhook, null, 2))
console.log(
'Alchemy Notify address activity notification created, go to https://dashboard.alchemy.com/notify to see details of your custom hook.',
'Alchemy Notify address activity notification created, go to https://dashboard.alchemy.com/notify to see details of your custom hook.'
)
}

Expand Down
Binary file modified bun.lockb
Binary file not shown.

0 comments on commit 9342895

Please sign in to comment.