Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add aggregation key & double claim check #261

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion frontend/claim_sdk/eventSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ export class TokenDispenserEventSubscriber {
* normalized with decimals 200_000_000_000_000
* @param event
*/
export function formatTxnEventInfo(txnEvnInfo: TxnEventInfo) {
export function formatTxnEventInfo(
txnEvnInfo: TxnEventInfo
): FormattedTxnEventInfo {
let formattedEvent: any = {
signature: txnEvnInfo.signature,
blockTime: txnEvnInfo.blockTime,
Expand All @@ -200,6 +202,15 @@ export function formatTxnEventInfo(txnEvnInfo: TxnEventInfo) {
return formattedEvent
}

export type FormattedTxnEventInfo = {
signature: string
blockTime: number
slot: number
claimant?: string
remainingBalance?: number
claimInfo?: FormattedClaimInfo
}

function formatClaimInfo(
claimInfo: IdlTypes<TokenDispenser>['ClaimInfo']
): FormattedClaimInfo {
Expand Down
123 changes: 99 additions & 24 deletions frontend/scripts/datadog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import { client, v1 } from '@datadog/datadog-api-client'
import {
FormattedClaimInfo,
FormattedTxnEventInfo,
formatTxnEventInfo,
TokenDispenserEventSubscriber,
TxnEventInfo,
Expand All @@ -18,6 +20,7 @@ import {
INFO,
} from '@datadog/datadog-api-client/dist/packages/datadog-api-client-v1/models/EventAlertType'
import { envOrErr } from '../claim_sdk'
import assert from 'assert'
swimricky marked this conversation as resolved.
Show resolved Hide resolved

const ENDPOINT = envOrErr('ENDPOINT')
const PROGRAM_ID = envOrErr('PROGRAM_ID')
Expand All @@ -42,7 +45,28 @@ async function main() {
const { txnEvents, failedTxnInfos } =
await tokenDispenserEventSubscriber.parseTransactionLogs()

const txnEventRequests = createTxnEventRequest(txnEvents)
const formattedTxnEvents = txnEvents
.filter((txnEvent) => txnEvent.event)
.map((txnEvent) => formatTxnEventInfo(txnEvent))

const doubleClaimEventRequests =
createDoubleClaimEventRequest(formattedTxnEvents)
if (doubleClaimEventRequests.length > 0) {
await Promise.all(
doubleClaimEventRequests.map((doubleClaimEventRequest) => {
apiInstance
.createEvent(doubleClaimEventRequest)
.then((data: v1.EventCreateResponse) => {
console.log(
'API called successfully. Returned data: ' + JSON.stringify(data)
)
})
.catch((error: any) => console.error(error))
})
)
}

const txnEventRequests = createTxnEventRequest(formattedTxnEvents)
await Promise.all(
txnEventRequests.map((txnEventRequest) => {
apiInstance
Expand Down Expand Up @@ -72,30 +96,80 @@ async function main() {
}

function createTxnEventRequest(
txnEvents: TxnEventInfo[]
formattedTxnEvents: FormattedTxnEventInfo[]
): v1.EventsApiCreateEventRequest[] {
return txnEvents
.filter((txnEvent) => txnEvent.event) // skip initialize txn
.map((txnEvent) => {
const formattedEvent = formatTxnEventInfo(txnEvent)
const { claimant } = formattedEvent

const { ecosystem, address } = formattedEvent.claimInfo

return {
body: {
title: `${claimant}-${ecosystem}-${address}`,
text: JSON.stringify(formattedEvent),
alertType: INFO,
tags: [
`claimant:${claimant}`,
`ecosystem:${ecosystem}`,
`network:${CLUSTER}`,
`service:token-dispenser-event-subscriber`,
],
},
}
})
return formattedTxnEvents.map((formattedEvent) => {
const { signature, claimant } = formattedEvent

const { ecosystem, address } = formattedEvent.claimInfo!

return {
body: {
aggregationKey: `${signature}`,
title: `${claimant}-${ecosystem}-${address}`,
text: JSON.stringify(formattedEvent),
alertType: INFO,
tags: [
`claimant:${claimant}`,
`ecosystem:${ecosystem}`,
`network:${CLUSTER}`,
`service:token-dispenser-event-subscriber`,
],
},
}
})
}

/**
* Check for double claims and create error events for any detected
* @param formattedTxnEvents
*/
function createDoubleClaimEventRequest(
formattedTxnEvents: FormattedTxnEventInfo[]
): v1.EventsApiCreateEventRequest[] {
const claimInfoMap = new Map<string, Set<FormattedTxnEventInfo>>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need a set here, it's enough to count so a Map<string, number> is enough

Copy link
Contributor Author

@swimricky swimricky Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have a set so i can capture the txn info in the datadog event. it'll be easier to investigate if this actually does get triggered

formattedTxnEvents.forEach((formattedTxnEvent) => {
const claimInfoKey = `${formattedTxnEvent.claimInfo!.ecosystem}-${
formattedTxnEvent.claimInfo!.address
}`

if (!claimInfoMap.get(claimInfoKey)) {
claimInfoMap.set(claimInfoKey, new Set<FormattedTxnEventInfo>())
}
claimInfoMap.get(claimInfoKey)!.add(formattedTxnEvent)
})
console.log(`claimInfoMap.size: ${claimInfoMap.size}`)
const entryGen = claimInfoMap.entries()
let entry = entryGen.next()
const doubleClaimEntries: Array<[string, Set<FormattedTxnEventInfo>]> = []
while (!entry.done) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit dirty, why not for let entry of claimInfoMap

if (entry.value[1].size > 1) {
doubleClaimEntries.push(entry.value)
console.log(`adding doubleClaimEntry`)
}
entry = entryGen.next()
}

return doubleClaimEntries.map(([claimInfoKey, txnEventInfosSet]) => {
const [ecosystem, address] = claimInfoKey.split('-')
const txnEventInfos = Array.from(txnEventInfosSet.values())
return {
body: {
aggregationKey: `DOUBLE-CLAIM-${ecosystem}-${address}`,
title: `DOUBLE-CLAIM-${ecosystem}-${address}`,
text: `
Double Claim detected for ${ecosystem} ${address}
claim events: ${JSON.stringify(txnEventInfos)}
`,
alertType: ERROR,
tags: [
`ecosystem:${ecosystem}`,
`network:${CLUSTER}`,
`service:token-dispenser-event-subscriber`,
],
},
}
})
}

function createFailedTxnEventRequest(
Expand All @@ -104,6 +178,7 @@ function createFailedTxnEventRequest(
return failedTxns.map((errorLog) => {
return {
body: {
aggregationKey: `${errorLog.signature}`,
title: `error-${errorLog.signature}`,
text: JSON.stringify(errorLog),
alertType: ERROR,
Expand Down
Loading