-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
229 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { ProposalStatus } from '@dao-dao/types/protobuf/codegen/cosmos/gov/v1/gov' | ||
import { | ||
getConfiguredChainConfig, | ||
getDisplayNameForChainId, | ||
getImageUrlForChainId, | ||
} from '@dao-dao/utils' | ||
|
||
import { WebhookMaker, WebhookType } from '@/core/types' | ||
import { decodeGovProposal } from '@/core/utils' | ||
import { GovStateEvent } from '@/db' | ||
|
||
// Fire webhook when a gov proposal is created. | ||
export const makeInboxGovProposalCreated: WebhookMaker<GovStateEvent> = ( | ||
config, | ||
state | ||
) => ({ | ||
filter: { | ||
EventType: GovStateEvent, | ||
}, | ||
endpoint: { | ||
type: WebhookType.Url, | ||
url: 'https://notifier.daodao.zone/notify', | ||
method: 'POST', | ||
headers: { | ||
'x-api-key': config.notifierSecret, | ||
}, | ||
}, | ||
getValue: async (event, getLastEvent) => { | ||
// Only fire the webhook the first time this exists. | ||
if ((await getLastEvent()) !== null) { | ||
return | ||
} | ||
|
||
const { proposal, title } = decodeGovProposal(event.data) | ||
|
||
return { | ||
chainId: state.chainId, | ||
type: 'proposal_created', | ||
data: { | ||
chainId: state.chainId, | ||
dao: getConfiguredChainConfig(state.chainId)?.name || 'GOV_PLACEHOLDER', | ||
daoName: getDisplayNameForChainId(state.chainId), | ||
imageUrl: getImageUrlForChainId(state.chainId), | ||
proposalId: event.proposalId, | ||
proposalTitle: proposal ? title : event.proposalId, | ||
fromApprover: false, | ||
}, | ||
} | ||
}, | ||
}) | ||
|
||
// Fire webhook when a gov proposal is passed (or passed + execution failed). | ||
export const makeInboxGovProposalPassed: WebhookMaker<GovStateEvent> = ( | ||
config, | ||
state | ||
) => ({ | ||
filter: { | ||
EventType: GovStateEvent, | ||
}, | ||
endpoint: { | ||
type: WebhookType.Url, | ||
url: 'https://notifier.daodao.zone/notify', | ||
method: 'POST', | ||
headers: { | ||
'x-api-key': config.notifierSecret, | ||
}, | ||
}, | ||
getValue: async (event, getLastEvent) => { | ||
// Only fire the webhook if the last event was not passed. | ||
const lastEvent = await getLastEvent() | ||
const lastDecoded = lastEvent && decodeGovProposal(lastEvent.data) | ||
if ( | ||
lastEvent && | ||
// If could not decode and verify that last event was passed, ignore to | ||
// avoid spamming when something breaks. | ||
(!lastDecoded || | ||
// If last event was passed (or passed + execution failed), ignore | ||
// because already sent. | ||
lastDecoded.status === ProposalStatus.PROPOSAL_STATUS_PASSED || | ||
lastDecoded.status === ProposalStatus.PROPOSAL_STATUS_FAILED) | ||
) { | ||
return | ||
} | ||
|
||
const { proposal, title, status } = decodeGovProposal(event.data) | ||
|
||
return { | ||
chainId: state.chainId, | ||
type: 'proposal_executed', | ||
data: { | ||
chainId: state.chainId, | ||
dao: getConfiguredChainConfig(state.chainId)?.name || 'GOV_PLACEHOLDER', | ||
daoName: getDisplayNameForChainId(state.chainId), | ||
imageUrl: getImageUrlForChainId(state.chainId), | ||
proposalId: event.proposalId, | ||
proposalTitle: proposal ? title : event.proposalId, | ||
fromApprover: false, | ||
failed: status === ProposalStatus.PROPOSAL_STATUS_FAILED, | ||
}, | ||
} | ||
}, | ||
}) | ||
|
||
// Fire webhook when a gov proposal is rejected. | ||
export const makeInboxGovProposalRejected: WebhookMaker<GovStateEvent> = ( | ||
config, | ||
state | ||
) => ({ | ||
filter: { | ||
EventType: GovStateEvent, | ||
}, | ||
endpoint: { | ||
type: WebhookType.Url, | ||
url: 'https://notifier.daodao.zone/notify', | ||
method: 'POST', | ||
headers: { | ||
'x-api-key': config.notifierSecret, | ||
}, | ||
}, | ||
getValue: async (event, getLastEvent) => { | ||
// Only fire the webhook if the last event was not rejected. | ||
const lastEvent = await getLastEvent() | ||
const lastDecoded = lastEvent && decodeGovProposal(lastEvent.data) | ||
if ( | ||
lastEvent && | ||
// If could not decode and verify that last event was rejected, ignore to | ||
// avoid spamming when something breaks. | ||
(!lastDecoded || | ||
// If last event was rejected, ignore because already sent. | ||
lastDecoded.status === ProposalStatus.PROPOSAL_STATUS_REJECTED) | ||
) { | ||
return | ||
} | ||
|
||
const { proposal, title } = decodeGovProposal(event.data) | ||
|
||
return { | ||
chainId: state.chainId, | ||
type: 'proposal_closed', | ||
data: { | ||
chainId: state.chainId, | ||
dao: getConfiguredChainConfig(state.chainId)?.name || 'GOV_PLACEHOLDER', | ||
daoName: getDisplayNameForChainId(state.chainId), | ||
imageUrl: getImageUrlForChainId(state.chainId), | ||
proposalId: event.proposalId, | ||
proposalTitle: proposal ? title : event.proposalId, | ||
fromApprover: false, | ||
}, | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters