-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrift.js
93 lines (80 loc) · 2.75 KB
/
drift.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const { Connection, Keypair } = require("@solana/web3.js");
const { AnchorProvider, Program } = require('@project-serum/anchor');
const { DriftClient } = require('@drift-labs/sdk');
const { IDL, VAULT_PROGRAM_ID, VaultClient } = require("@drift-labs/vaults-sdk");
const HyperExpress = require('hyper-express');
const { PublicKey } = require("@solana/web3.js");
let initialized, client, client_jlpdnv1
const JLPDN_PROGAM_ID_V1 = "" // TODO: figure out JLPDN_PROGAM_ID_V1
const vaultTvls = {}
async function getCachedVaultTvl(vault, version) {
const cacheDuration = 20 * 60 * 60 * 1000 // 20 hours in milliseconds
const now = Date.now()
let { cachedTvl, cacheTimestamp } = vaultTvls[vault] || {}
if (cachedTvl && cacheTimestamp && (now - cacheTimestamp < cacheDuration))
return cachedTvl
if (!initialized)
initialized = initialize()
await initialized
cachedTvl = getTvl()
cacheTimestamp = now
vaultTvls[vault] = { cachedTvl, cacheTimestamp }
return cachedTvl
async function getTvl() {
const _client = version == 1 ? client_jlpdnv1 : client
const vaultInstance = await _client.getVault(new PublicKey(vault));
const token_tvl = await _client.calculateVaultEquityInDepositAsset({
address: vault,
vault: vaultInstance,
factorUnrealizedPNL: true,
});
return Number(token_tvl);
}
}
/// INIT DRIFT SDK
const initialize = async () => {
const connection = new Connection(process.env.SOLANA_RPC);
const wallet = Keypair.generate();
const initializeDriftClient = async () => {
const driftClient = new DriftClient({
connection,
wallet,
env: "mainnet-beta",
});
await driftClient.subscribe();
return driftClient;
};
const initializeProgram = (version) => {
const provider = new AnchorProvider(connection, wallet, AnchorProvider.defaultOptions());
return new Program(
IDL,
// version == 1 ? JLPDN_PROGAM_ID_V1 : VAULT_PROGRAM_ID, // figure out JLPDN_PROGAM_ID_V1 & set it here
VAULT_PROGRAM_ID,
provider
);
};
const initializeVaultClient = async (version) => {
return new VaultClient({
driftClient: await initializeDriftClient(),
program: initializeProgram(version),
cliMode: true,
});
};
client = await initializeVaultClient();
client_jlpdnv1 = await initializeVaultClient(1);
};
function setRoutes(routerPrime) {
const router = new HyperExpress.Router()
routerPrime.use('/drift', router)
router.get('/vault_tvl', async (req, res) => {
const { version, vault } = req.query
try {
const tvl = await getCachedVaultTvl(vault, version)
return res.json(tvl)
} catch (error) {
console.error(error)
res.status(500).json({ error: 'Internal server error' })
}
})
}
module.exports = { setRoutes }