diff --git a/src/routes/index.ts b/src/routes/index.ts index bac3a3c..0771044 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -18,6 +18,8 @@ import { DRIFT_PROGRAM_ID, DriftClient, BulkAccountLoader, + calculateDepositRate, + convertToNumber, } from '@drift-labs/sdk'; import { clamp, @@ -85,8 +87,47 @@ router.get('/blinks/deposit', async (req: Request, res: Response) => { icon = GENERIC_BLINK_IMAGE; } + const { oracleInfos, perpMarketIndexes, spotMarketIndexes } = + getMarketsAndOraclesForSubscription(DRIFT_ENV); + + const connection = new Connection(ENDPOINT, { + commitment: 'confirmed', + }); + + const bulkAccountLoader = new BulkAccountLoader(connection, 'confirmed', 0); + + const walletWrapper = createThrowawayIWallet(); + + const driftClient = new DriftClient({ + connection: connection, + wallet: walletWrapper, + programID: new PublicKey(DRIFT_PROGRAM_ID), + env: DRIFT_ENV, + txVersion: 0, + userStats: false, + perpMarketIndexes: perpMarketIndexes, + spotMarketIndexes: spotMarketIndexes, + oracleInfos: oracleInfos, + accountSubscription: { + type: 'polling', + accountLoader: bulkAccountLoader, + }, + }); + await driftClient.subscribe(); + + let title = `Deposit ${depositToken} into Drift`; + + const spotMarket = driftClient.getSpotMarketAccount(spotMarketConfig.marketIndex); + + if (spotMarket) { + const apr = convertToNumber(calculateDepositRate(spotMarket), new BN(10000)); + + if (apr >= 0.1) { + title = `Deposit ${depositToken} into Drift and earn ${apr}% APR`; + } + } + const label = ''; - const title = `Deposit ${depositToken} into Drift`; const description = ''; const disabled = false; const amountQuery = 'depositAmount'; @@ -116,7 +157,7 @@ router.get('/blinks/deposit', async (req: Request, res: Response) => { }, ], }; - + const response: ActionsSpecGetResponse = { icon, label, @@ -125,6 +166,8 @@ router.get('/blinks/deposit', async (req: Request, res: Response) => { disabled, links, }; + + driftClient.unsubscribe(); return res.json(response); });