From ce45cab93f077dfcac432040583de2b7f863cc0d Mon Sep 17 00:00:00 2001 From: arihantbansal <17180950+arihantbansal@users.noreply.github.com> Date: Sat, 13 Jul 2024 23:02:42 +0530 Subject: [PATCH 1/3] add apr to title --- src/routes/index.ts | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/routes/index.ts b/src/routes/index.ts index bac3a3c..a1e2edb 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -18,6 +18,9 @@ import { DRIFT_PROGRAM_ID, DriftClient, BulkAccountLoader, + calculateDepositRate, + convertToNumber, + PERCENTAGE_PRECISION, } from '@drift-labs/sdk'; import { clamp, @@ -85,8 +88,48 @@ 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, + }, + }); + + let title = `Deposit ${depositToken} into Drift`; + + const spotMarket = driftClient.getSpotMarketAccount(spotMarketConfig.marketIndex); + + if (spotMarket) { + const apr = convertToNumber(calculateDepositRate(spotMarket), new BN(PERCENTAGE_PRECISION)); + + console.log('APR:', apr); + + 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'; From d76c2a7768b8ac57cc936fbbc204a1f417bb937a Mon Sep 17 00:00:00 2001 From: arihantbansal <17180950+arihantbansal@users.noreply.github.com> Date: Sun, 14 Jul 2024 00:07:50 +0530 Subject: [PATCH 2/3] subscribe to drift client --- src/routes/index.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/routes/index.ts b/src/routes/index.ts index a1e2edb..3b25dca 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -20,7 +20,6 @@ import { BulkAccountLoader, calculateDepositRate, convertToNumber, - PERCENTAGE_PRECISION, } from '@drift-labs/sdk'; import { clamp, @@ -114,15 +113,14 @@ router.get('/blinks/deposit', async (req: Request, res: Response) => { 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(PERCENTAGE_PRECISION)); - - console.log('APR:', apr); + const apr = convertToNumber(calculateDepositRate(spotMarket), new BN(10000)); if (apr >= 0.1) { title = `Deposit ${depositToken} into Drift and earn ${apr}% APR`; From 4ad35e3fdb16e5888102cceecd70f8b97f634747 Mon Sep 17 00:00:00 2001 From: arihantbansal <17180950+arihantbansal@users.noreply.github.com> Date: Sun, 14 Jul 2024 11:47:55 +0530 Subject: [PATCH 3/3] fix: unsubscribe to client --- src/routes/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/routes/index.ts b/src/routes/index.ts index 3b25dca..0771044 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -157,7 +157,7 @@ router.get('/blinks/deposit', async (req: Request, res: Response) => { }, ], }; - + const response: ActionsSpecGetResponse = { icon, label, @@ -166,6 +166,8 @@ router.get('/blinks/deposit', async (req: Request, res: Response) => { disabled, links, }; + + driftClient.unsubscribe(); return res.json(response); });