-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add wallet address for cron job and test (#4831)
* add wallet address for cron job and test * add test * add to eslint * test alert * update errors * fix webhook * add link to wallet * update cron to use prd secrets * fix typecheck
- Loading branch information
Showing
16 changed files
with
207 additions
and
47 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
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
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
50 changes: 50 additions & 0 deletions
50
...outgamecron/src/tasks/alertLowWalletGasBalance/__tests__/alertLowWalletGasBalance.spec.ts
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,50 @@ | ||
import { jest } from '@jest/globals'; | ||
import { builderCreatorAddress } from '@packages/scoutgame/builderNfts/constants'; | ||
import { createContext } from '@packages/testing/koa/context'; | ||
|
||
jest.unstable_mockModule('@packages/utils/http', () => ({ | ||
POST: jest.fn() | ||
})); | ||
|
||
jest.unstable_mockModule('../getWalletGasBalanceInUSD', () => ({ | ||
getWalletGasBalanceInUSD: jest.fn() | ||
})); | ||
|
||
const { POST } = await import('@packages/utils/http'); | ||
const { getWalletGasBalanceInUSD } = await import('../getWalletGasBalanceInUSD'); | ||
const { alertLowWalletGasBalance } = await import('../index'); | ||
|
||
const discordWebhook = 'https://discord.webhook.url'; | ||
|
||
describe('alertLowWalletGasBalance', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should call the webhook when balance is below threshold', async () => { | ||
(getWalletGasBalanceInUSD as jest.Mock<typeof getWalletGasBalanceInUSD>).mockResolvedValue(20); // Below threshold of 25 | ||
|
||
await alertLowWalletGasBalance(createContext(), discordWebhook); | ||
|
||
expect(getWalletGasBalanceInUSD).toHaveBeenCalledWith(builderCreatorAddress); | ||
expect(POST).toHaveBeenCalledWith( | ||
discordWebhook, | ||
expect.objectContaining({ | ||
content: expect.any(String) | ||
}) | ||
); | ||
}); | ||
|
||
it('should not call the webhook when balance is above threshold', async () => { | ||
(getWalletGasBalanceInUSD as jest.Mock<typeof getWalletGasBalanceInUSD>).mockResolvedValue(30); // Above threshold of 25 | ||
|
||
await alertLowWalletGasBalance(createContext(), discordWebhook); | ||
|
||
expect(getWalletGasBalanceInUSD).toHaveBeenCalledWith(builderCreatorAddress); | ||
expect(POST).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw an error if no discord webhook is provided', async () => { | ||
await expect(alertLowWalletGasBalance(createContext())).rejects.toThrow('No Discord webhook found'); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
...outgamecron/src/tasks/alertLowWalletGasBalance/__tests__/getWalletGasBalanceInUSD.spec.ts
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,33 @@ | ||
import { jest } from '@jest/globals'; | ||
import { builderCreatorAddress } from '@packages/scoutgame/builderNfts/constants'; | ||
|
||
jest.unstable_mockModule('@packages/utils/http', () => ({ | ||
POST: jest.fn(), | ||
GET: jest.fn() | ||
})); | ||
|
||
const { POST, GET } = await import('@packages/utils/http'); | ||
const { getWalletGasBalanceInUSD } = await import('../getWalletGasBalanceInUSD'); | ||
|
||
describe('getWalletGasBalanceInUSD', () => { | ||
it('should return $25 when balance and price are set accordingly', async () => { | ||
// Mock the POST request to Alchemy API | ||
(POST as jest.Mock<typeof POST>).mockResolvedValue({ | ||
result: '0x3635c9adc5dea00000' // 1000000000000000000000 wei (1000 ETH) | ||
}); | ||
|
||
// Mock the GET request to CoinGecko API | ||
(GET as jest.Mock<typeof GET>).mockResolvedValue({ | ||
ethereum: { usd: 0.025 } // $0.025 per ETH | ||
}); | ||
|
||
const balance = await getWalletGasBalanceInUSD(builderCreatorAddress, 'test-api-key'); | ||
|
||
expect(balance).toBeCloseTo(25, 2); // $25 with 2 decimal places precision | ||
expect(POST).toHaveBeenCalledWith( | ||
expect.stringContaining('https://opt-mainnet.g.alchemy.com/v2/test-api-key'), | ||
expect.any(Object) | ||
); | ||
expect(GET).toHaveBeenCalledWith('https://api.coingecko.com/api/v3/simple/price', expect.any(Object)); | ||
}); | ||
}); |
11 changes: 8 additions & 3 deletions
11
apps/scoutgamecron/src/tasks/alertLowWalletGasBalance/getWalletGasBalanceInUSD.ts
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
36 changes: 23 additions & 13 deletions
36
apps/scoutgamecron/src/tasks/alertLowWalletGasBalance/index.ts
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 |
---|---|---|
@@ -1,21 +1,31 @@ | ||
import env from '@beam-australia/react-env'; | ||
import { log } from '@charmverse/core/log'; | ||
import { builderCreatorAddress } from '@packages/scoutgame/builderNfts/constants'; | ||
import { POST } from '@packages/utils/http'; | ||
import type Koa from 'koa'; | ||
|
||
import { getWalletGasBalanceInUSD } from './getWalletGasBalanceInUSD'; | ||
|
||
export async function alertLowWalletGasBalance() { | ||
const discordWebhook = process.env.DISCORD_EVENTS_WEBHOOK || env('DISCORD_EVENTS_WEBHOOK'); | ||
const thresholdUSD = 25; | ||
|
||
try { | ||
const balanceInUSD = await getWalletGasBalanceInUSD(''); | ||
log.info(`Admin wallet has a balance of ${balanceInUSD} USD`); | ||
if (balanceInUSD <= 25) { | ||
await POST(discordWebhook, { | ||
content: `Admin wallet has a balance of ${balanceInUSD} USD` | ||
}); | ||
} | ||
} catch (error) { | ||
log.error('Error alerting low wallet gas balance:', { error }); | ||
export async function alertLowWalletGasBalance( | ||
ctx: Koa.Context, | ||
discordWebhook: string | undefined = process.env.DISCORD_ALERTS_WEBHOOK | ||
) { | ||
if (!discordWebhook) { | ||
throw new Error('No Discord webhook found'); | ||
} | ||
|
||
const balanceInUSD = await getWalletGasBalanceInUSD(builderCreatorAddress); | ||
log.info(`Admin wallet has a balance of ${balanceInUSD} USD`); | ||
if (balanceInUSD <= thresholdUSD) { | ||
await POST(discordWebhook, { | ||
content: `<@&1027309276454207519>: Admin wallet has a low balance: ${balanceInUSD} USD. (Threshold is ${thresholdUSD} USD)`, | ||
embeds: [ | ||
{ | ||
title: `View wallet: ${builderCreatorAddress}`, | ||
url: 'https://optimism.blockscout.com/address/0x518AF6fA5eEC4140e4283f7BDDaB004D45177946' | ||
} | ||
] | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.