Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
27a35d8
add auto redeem
mahsamoosavi Jun 23, 2025
c2bf3dd
add private key to the env sample
mahsamoosavi Jun 23, 2025
654a326
mark auto redeemed tickets as Ignore in Notion
mahsamoosavi Jun 23, 2025
cdb6d3e
Revert "mark auto redeemed tickets as Ignore in Notion"
mahsamoosavi Jun 23, 2025
206d1af
update State to "Bot Success" or "Bot Failed" based on auto-redeem re…
mahsamoosavi Jun 23, 2025
75439e3
Update packages/retryable-monitor/handlers/notion/alertUntriagedRetra…
mahsamoosavi Aug 22, 2025
dc1b174
Update packages/retryable-monitor/handlers/notion/alertUntriagedRetra…
mahsamoosavi Aug 22, 2025
3682e0c
Merge remote-tracking branch 'origin/main' into add-auto-redeem
mahsamoosavi Aug 22, 2025
be41b10
Merge branch 'add-auto-redeem' of https://github.com/OffchainLabs/arb…
mahsamoosavi Aug 22, 2025
a47300a
improve retryable alert flow and rename Notion column to Bot Redempti…
mahsamoosavi Aug 22, 2025
fd77cf8
add explicit type for Notion query response to resolve TS7022
mahsamoosavi Aug 22, 2025
2462f48
write 'Bot Redemption Status' on create/update/executed
mahsamoosavi Aug 22, 2025
2bd9b9d
improve redeemRetryable with env check, existing redeem detection, an…
mahsamoosavi Aug 22, 2025
163cc4d
update comments
mahsamoosavi Aug 25, 2025
1508b46
add --autoRedeem flag to enable optional silent redemption of retryables
mahsamoosavi Aug 27, 2025
be12747
add 24–72h Slack alert for Should Redeem when auto-redeem is disabled
mahsamoosavi Aug 27, 2025
f01bdae
Rename env-sample's PRIVATE_KEY
mahsamoosavi Aug 28, 2025
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
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ NODE_ENV=

RETRYABLE_MONITORING_SLACK_TOKEN=
RETRYABLE_MONITORING_SLACK_CHANNEL=
PRIVATE_KEY=

BATCH_POSTER_MONITORING_SLACK_TOKEN=
BATCH_POSTER_MONITORING_SLACK_CHANNEL=
Expand Down
77 changes: 77 additions & 0 deletions packages/retryable-monitor/core/redeemRetryable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { providers, Wallet } from 'ethers'
import {
ParentTransactionReceipt,
ParentToChildMessageStatus,
} from '@arbitrum/sdk'
import { getConfig, DEFAULT_CONFIG_PATH } from '../../utils'
import dotenv from 'dotenv'

dotenv.config()

export const redeemRetryable = async (parentTxHash: string): Promise<string> => {
const config = getConfig({ configPath: DEFAULT_CONFIG_PATH })

const pk = process.env.PRIVATE_KEY
if (!pk) {
throw new Error('PRIVATE_KEY env var is required for redeemRetryable')
}

let lastError: unknown

for (const childChain of config.childChains) {
try {
// 1) Check parent chain for the tx
const parentChainProvider = new providers.JsonRpcProvider(
childChain.parentRpcUrl
)
const receipt = await parentChainProvider.getTransactionReceipt(
parentTxHash
)
if (!receipt) {
// not on this parent chain; try the next one
continue
}

// 2) We found the parent receipt -> attempt on its configured child
const childChainProvider = new providers.JsonRpcProvider(
childChain.orbitRpcUrl
)
const wallet = new Wallet(pk, childChainProvider)

const parentReceipt = new ParentTransactionReceipt(receipt)
const messages = await parentReceipt.getParentToChildMessages(wallet)

if (!messages || messages.length === 0) {
// no L1->L2 messages associated; try next chain
continue
}

// If multiple, redeem the first (adjust selection logic if needed)
const message = messages[0]

// 3) If already redeemed, return its tx hash instead of throwing
const already = await message.getSuccessfulRedeem().catch(() => null)
if (already && already.status === ParentToChildMessageStatus.REDEEMED) {
const existingHash =
(already as any)?.childTxReceipt?.transactionHash ??
(already as any)?.txHash
if (existingHash) return existingHash
}

// 4) Otherwise redeem now
const tx = await message.redeem()
const redeemReceipt = await tx.waitForRedeem()
return redeemReceipt.transactionHash
} catch (err) {
// Record and continue probing other configured (parent, child) pairs
lastError = err
continue
}
}

const suffix =
lastError instanceof Error ? ` Last error: ${lastError.message}` : ''
throw new Error(
`❌ Parent tx ${parentTxHash} not found/redeemable on any configured chain.${suffix}`
)
}
1 change: 1 addition & 0 deletions packages/retryable-monitor/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface OnRetryableFoundParams {
l2CallValue: string
createdAt?: number
decision?: string
botRedemptionStatus?: string

}
}
Expand Down
180 changes: 112 additions & 68 deletions packages/retryable-monitor/handlers/notion/alertUntriagedRetraybles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { notionClient, databaseId } from './createNotionClient'
import { postSlackMessage } from '../slack/postSlackMessage'
import { ChildNetwork } from '../../../utils'
import { redeemRetryable } from '../../core/redeemRetryable'
import type { ChildNetwork } from '../../../utils'

const formatDate = (iso: string | undefined) => {
if (!iso) return '(unknown)'
Expand All @@ -25,78 +26,121 @@ const isNearExpiry = (iso: string | undefined, hours = 24) => {
return timeLeftMs > 0 && timeLeftMs <= hours * 60 * 60 * 1000
}

type NotionQueryResp = Awaited<ReturnType<typeof notionClient.databases.query>>

export const alertUntriagedNotionRetryables = async (
childChains: ChildNetwork[] = []
) => {
const allowedChainIds = childChains.map(c => c.chainId)
const response = await notionClient.databases.query({
database_id: databaseId,
page_size: 100,
filter: {
and: [
{
or: [
{ property: 'Decision', select: { equals: 'Triage' } },
{ property: 'Decision', select: { equals: 'Should Redeem' } },
],
},
{
property: 'Status',
select: { does_not_equal: 'Executed' },
},
],
},
})

for (const page of response.results) {
const props = (page as any).properties

// skip if chainId not in allowed list
const chainIdRaw = props?.ChainID?.number
if (allowedChainIds.length > 0 && !allowedChainIds.includes(chainIdRaw)) {
continue
}

const status = props?.Status?.select?.name || '(unknown)'
if (status?.toLowerCase() === 'expired') continue

const timeoutRaw = props?.timeoutTimestamp?.date?.start
const timeoutStr = formatDate(timeoutRaw)
const retryableUrl =
props?.ChildTx?.title?.[0]?.text?.content || '(unknown)'
const parentTx =
props?.ParentTx?.rich_text?.[0]?.text?.content || '(unknown)'

const ethDeposit =
props?.TotalRetryableDeposit?.rich_text?.[0]?.text?.content || ''
const tokenDeposit =
props?.TokensDeposited?.rich_text?.[0]?.text?.content || ''
const deposit =
[ethDeposit, tokenDeposit]
.filter(s => s && s !== '0.0 ETH ($0.00)')
.join(' and ') || '(unknown)'

const decision = props?.Decision?.select?.name || '(unknown)'

const now = Date.now()
const expiryTime = timeoutRaw ? new Date(timeoutRaw).getTime() : Infinity
const hoursLeft = (expiryTime - now) / (1000 * 60 * 60)

let message = ''

if (decision === 'Triage') {
if (hoursLeft <= 72) {
message = `🚨🚨 Retryable ticket needs IMMEDIATE triage (expires soon!):\n• Retryable: ${retryableUrl}\n• Timeout: ${timeoutStr}\n• Parent Tx: ${parentTx}\n• Total value deposited: ${deposit}\n→ Please triage urgently.`
} else {
message = `⚠️ Retryable ticket needs triage:\n• Retryable: ${retryableUrl}\n• Timeout: ${timeoutStr}\n• Parent Tx: ${parentTx}\n• Total value deposited: ${deposit}\n→ Please review and decide whether to redeem or ignore.`
let startCursor: string | undefined = undefined
do {
const response: NotionQueryResp = await notionClient.databases.query({
database_id: databaseId,
page_size: 100,
start_cursor: startCursor,
filter: {
and: [
{
or: [
{ property: 'Decision', select: { equals: 'Triage' } },
{ property: 'Decision', select: { equals: 'Should Redeem' } },
],
},
{
property: 'Status',
select: { does_not_equal: 'Executed' },
},
],
},
})

for (const page of response.results) {
const props = (page as any).properties

// chain allowlist
const chainIdRaw = props?.ChainID?.number
if (allowedChainIds.length > 0 && !allowedChainIds.includes(chainIdRaw)) {
continue
}

// expired rows are skipped
const status = props?.Status?.select?.name || '(unknown)'
if (status?.toLowerCase() === 'expired') continue

const timeoutRaw = props?.timeoutTimestamp?.date?.start
const timeoutStr = formatDate(timeoutRaw)
const retryableUrl =
props?.ChildTx?.title?.[0]?.text?.content || '(unknown)'
const parentTx =
props?.ParentTx?.rich_text?.[0]?.text?.content || '(unknown)'

const ethDeposit =
props?.TotalRetryableDeposit?.rich_text?.[0]?.text?.content || ''
const tokenDeposit =
props?.TokensDeposited?.rich_text?.[0]?.text?.content || ''
const deposit =
[ethDeposit, tokenDeposit]
.filter(s => s && s !== '0.0 ETH ($0.00)')
.join(' and ') || '(unknown)'

const decision = props?.Decision?.select?.name || '(unknown)'

// only care about the two tracked decisions
if (decision !== 'Triage' && decision !== 'Should Redeem') continue

const now = Date.now()
const expiryTime = timeoutRaw ? new Date(timeoutRaw).getTime() : Infinity
const hoursLeft = (expiryTime - now) / (1000 * 60 * 60)

// if timeout exists and already past, skip
if (Number.isFinite(hoursLeft) && hoursLeft < 0) continue

let message = ''

if (decision === 'Triage') {
if (hoursLeft <= 72) {
message = `🚨🚨 Retryable ticket needs IMMEDIATE triage (expires soon!):\n• Retryable: ${retryableUrl}\n• Timeout: ${timeoutStr}\n• Parent Tx: ${parentTx}\n• Total value deposited: ${deposit}\n→ Please triage urgently.`
} else {
message = `⚠️ Retryable ticket needs triage:\n• Retryable: ${retryableUrl}\n• Timeout: ${timeoutStr}\n• Parent Tx: ${parentTx}\n• Total value deposited: ${deposit}\n→ Please review and decide whether to redeem or ignore.`
}
} else if (decision === 'Should Redeem') {
const under24HoursLeftToExpire = timeoutRaw ? isNearExpiry(timeoutRaw, 24) : false
const moreThan4DaysLeftToExpire = timeoutRaw ? hoursLeft > 96 : false

if (under24HoursLeftToExpire) {
message = `🚨 Retryable marked for redemption and nearing expiry:\n• Retryable: ${retryableUrl}\n• Timeout: ${timeoutStr}\n• Parent Tx: ${parentTx}\n• Total value deposited: ${deposit}\n→ Check why it hasn't been executed.`
} else if (moreThan4DaysLeftToExpire) {
try {
await redeemRetryable(parentTx)
await notionClient.pages.update({
page_id: page.id,
properties: {
'Bot Redemption Status': { select: { name: 'Bot Success' } },
},
})
} catch (err) {
await notionClient.pages.update({
page_id: page.id,
properties: {
'Bot Redemption Status': { select: { name: 'Bot Failed' } },
},
})
}
continue // no Slack message
} else {
continue // not under 24h or over 4d, skip
}
}

if (!message) continue
try {
await postSlackMessage({ message })
} catch {
// swallow Slack send errors to avoid breaking the loop
}
} else if (decision === 'Should Redeem') {
if (!isNearExpiry(timeoutRaw)) continue
message = `🚨 Retryable marked for redemption and nearing expiry:\n• Retryable: ${retryableUrl}\n• Timeout: ${timeoutStr}\n• Parent Tx: ${parentTx}\n• Total value deposited: ${deposit}\n→ Check why it hasn't been executed.`
} else {
continue
}

await postSlackMessage({ message })
}
}
startCursor = (response as any).has_more ? (response as any).next_cursor : undefined
} while (startCursor)
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export async function syncRetryableToNotion(
rich_text: [{ text: { content: metadata.tokensDeposited } }],
}
}
// NEW: support verbose column for bot redemption outcome if provided
if (metadata.botRedemptionStatus) {
notionProps['Bot Redemption Status'] = {
select: { name: metadata.botRedemptionStatus },
}
}
}

if (isRetryableFoundInNotion) {
Expand Down Expand Up @@ -112,6 +118,13 @@ export async function syncRetryableToNotion(
}
}

// NEW: carry Bot Redemption Status if present on this update
if (metadata?.botRedemptionStatus) {
executedProps['Bot Redemption Status'] = {
select: { name: metadata.botRedemptionStatus },
}
}

await notionClient.pages.update({
page_id: page.id,
properties: executedProps,
Expand Down Expand Up @@ -150,6 +163,14 @@ export async function syncRetryableToNotion(
...(metadata?.decision
? { Decision: { select: { name: metadata.decision } } }
: {}),
// NEW: include Bot Redemption Status on creation when provided
...(metadata?.botRedemptionStatus
? {
'Bot Redemption Status': {
select: { name: metadata.botRedemptionStatus },
},
}
: {}),
...notionProps,
},
})
Expand Down