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

Add APR to blink title #2

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Changes from all commits
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
47 changes: 45 additions & 2 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
DRIFT_PROGRAM_ID,
DriftClient,
BulkAccountLoader,
calculateDepositRate,
convertToNumber,
} from '@drift-labs/sdk';
import {
clamp,
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -116,7 +157,7 @@ router.get('/blinks/deposit', async (req: Request, res: Response) => {
},
],
};

const response: ActionsSpecGetResponse = {
icon,
label,
Expand All @@ -125,6 +166,8 @@ router.get('/blinks/deposit', async (req: Request, res: Response) => {
disabled,
links,
};

driftClient.unsubscribe();

return res.json(response);
});
Expand Down