Skip to content

Commit

Permalink
added telegram notifier webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Oct 15, 2024
1 parent b25a163 commit 2a0262a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
1 change: 1 addition & 0 deletions config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"daoDaoBase": "https://testnet.daodao.zone",
"discordNotifierApiKey": "key",
"telegramNotifierApiKey": "key",
"inboxSecret": "secret",
"notifierSecret": "secret",
"websocketsSecret": "secret",
Expand Down
File renamed without changes.
6 changes: 4 additions & 2 deletions src/webhooks/webhooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { State, WasmStateEvent } from '@/db'
import { WasmCodeService } from '@/services/wasm-codes'
import { Config, ProcessedWebhook, Webhook, WebhookMaker } from '@/types'

import * as discordNotifier from './discordNotifier'
import * as discord from './discord'
import * as indexerCwReceipt from './indexerCwReceipt'
import * as notify from './notify'
import * as telegram from './telegram'
import * as websockets from './websockets'

let processedWebhooks: ProcessedWebhook<any, any>[] | undefined
Expand All @@ -17,7 +18,8 @@ export const getProcessedWebhooks = (
if (!processedWebhooks) {
const webhookMakers: WebhookMaker<any, any>[] = [
// Add webhook makers here.
...Object.values(discordNotifier),
...Object.values(discord),
...Object.values(telegram),
...Object.values(indexerCwReceipt),
...Object.values(notify),
...Object.values(websockets),
Expand Down
89 changes: 89 additions & 0 deletions src/webhooks/webhooks/telegram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { WasmStateEvent } from '@/db'
import {
activeProposalModules,
config as daoCoreConfig,
} from '@/formulas/formulas/contract/daoCore/base'
import { StatusEnum } from '@/formulas/formulas/contract/proposal/types'
import { WebhookMaker, WebhookType } from '@/types'
import { dbKeyForKeys, dbKeyToKeys } from '@/utils'

import { getDaoAddressForProposalModule } from '../utils'

const CODE_IDS_KEYS = ['dao-proposal-single', 'dao-proposal-multiple']

const KEY_PREFIX_PROPOSALS = dbKeyForKeys('proposals', '')
const KEY_PREFIX_PROPOSALS_V2 = dbKeyForKeys('proposals_v2', '')

// Fire webhook when a proposal is created.
export const makeProposalCreated: WebhookMaker<WasmStateEvent> = (
config,
state
) => ({
filter: {
EventType: WasmStateEvent,
codeIdsKeys: CODE_IDS_KEYS,
matches: (event) =>
// Starts with proposals or proposals_v2.
(event.key.startsWith(KEY_PREFIX_PROPOSALS) ||
event.key.startsWith(KEY_PREFIX_PROPOSALS_V2)) &&
event.valueJson.status === StatusEnum.Open,
},
endpoint: async (event, env) => {
const daoAddress = await getDaoAddressForProposalModule({
...env,
contractAddress: event.contractAddress,
})
if (!daoAddress) {
return
}

return {
type: WebhookType.Url,
url: `https://telegram-notifier.dao-dao.workers.dev/${state.chainId}/${daoAddress}/notify`,
method: 'POST',
}
},
getValue: async (event, getLastEvent, env) => {
// Only fire the webhook the first time this exists.
if ((await getLastEvent()) !== null) {
return
}

// Get DAO config and proposal modules for this DAO so we can retrieve the
// DAO's name and the prefix for this proposal module.
const daoAddress = await getDaoAddressForProposalModule({
...env,
contractAddress: event.contractAddress,
})
if (!daoAddress) {
return
}

const daoConfig = await daoCoreConfig.compute({
...env,
contractAddress: daoAddress,
})
const proposalModules = await activeProposalModules.compute({
...env,
contractAddress: daoAddress,
})
const proposalModule = proposalModules?.find(
(proposalModule) => proposalModule.address === event.contractAddress
)

if (!daoConfig || !proposalModule) {
return
}

// "proposals"|"proposals_v2", proposalNum
const [, proposalNum] = dbKeyToKeys(event.key, [false, true])
const proposalId = `${proposalModule.prefix}${proposalNum}`

return {
apiKey: config.telegramNotifierApiKey,
daoName: daoConfig.name,
proposalId,
url: config.daoDaoBase + `/dao/${daoAddress}/proposals/${proposalId}`,
}
},
})

0 comments on commit 2a0262a

Please sign in to comment.