mirror of
https://github.com/0x1d/drift-keeper.git
synced 2025-12-14 18:35:20 +01:00
Add wallet tracker
This commit is contained in:
2
wallet-tracker/.gitignore
vendored
Normal file
2
wallet-tracker/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
node_modules/
|
||||
7
wallet-tracker/Dockerfile
Normal file
7
wallet-tracker/Dockerfile
Normal file
@@ -0,0 +1,7 @@
|
||||
FROM node:18
|
||||
|
||||
WORKDIR /app
|
||||
COPY package.json ./
|
||||
RUN npm install
|
||||
COPY src src
|
||||
CMD ["npm", "start"]
|
||||
1190
wallet-tracker/package-lock.json
generated
Normal file
1190
wallet-tracker/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
wallet-tracker/package.json
Normal file
15
wallet-tracker/package.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "wallet-tracker",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"scripts": {
|
||||
"start": "node --no-warnings=ExperimentalWarning src/main.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"prom-client": "^15.1.0"
|
||||
}
|
||||
}
|
||||
24
wallet-tracker/src/main.js
Normal file
24
wallet-tracker/src/main.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const express = require('express');
|
||||
const { createMetrics } = require('./metrics');
|
||||
const { loadWalletBalance, loadUSDCBalance, extractWalletBalance, extractUSDCBalance } = require('./solana');
|
||||
|
||||
const WALLET_ADDRESS = process.env.WALLET_ADDRESS;
|
||||
const [registry, usdcBalanceMetric, solBalanceMetric] = createMetrics();
|
||||
const app = express();
|
||||
|
||||
app.get('/metrics', async (req, res) => {
|
||||
res.setHeader('Content-Type', registry.contentType);
|
||||
|
||||
let [solBalance, usdcBalance] = await Promise.all([
|
||||
loadWalletBalance(WALLET_ADDRESS),
|
||||
loadUSDCBalance(WALLET_ADDRESS)]);
|
||||
|
||||
solBalanceMetric.set(extractWalletBalance(solBalance));
|
||||
usdcBalanceMetric.set(extractUSDCBalance(usdcBalance));
|
||||
|
||||
res.send(await registry.metrics());
|
||||
});
|
||||
|
||||
app.listen(3000, () => {
|
||||
console.log("Server is running on port 3000");
|
||||
});
|
||||
23
wallet-tracker/src/metrics.js
Normal file
23
wallet-tracker/src/metrics.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const client = require('prom-client');
|
||||
|
||||
const createMetrics = () => {
|
||||
const registry = new client.Registry();
|
||||
|
||||
const solBalanceMetric = new client.Gauge({
|
||||
name: "sol_balance",
|
||||
help: "SOL Balance",
|
||||
});
|
||||
const usdcBalanceMetric = new client.Gauge({
|
||||
name: "usdc_balance",
|
||||
help: "USDC Balance",
|
||||
});
|
||||
|
||||
registry.registerMetric(usdcBalanceMetric);
|
||||
registry.registerMetric(solBalanceMetric);
|
||||
|
||||
return [registry, usdcBalanceMetric, solBalanceMetric];
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
createMetrics
|
||||
}
|
||||
58
wallet-tracker/src/solana.js
Normal file
58
wallet-tracker/src/solana.js
Normal file
@@ -0,0 +1,58 @@
|
||||
const LAMPORTS_PER_SOL = 1000000000;
|
||||
const SOLANA_RPC = "https://api.mainnet-beta.solana.com/";
|
||||
const rpcRequestHeaders = {
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
const rpcRequest = (method, params) => {
|
||||
return {
|
||||
jsonrpc: "2.0",
|
||||
id: 1,
|
||||
method: method,
|
||||
params: params,
|
||||
};
|
||||
};
|
||||
|
||||
// POST request to Solana API to load wallet balance
|
||||
async function loadWalletBalance(walletAddress) {
|
||||
const response = await fetch(SOLANA_RPC, {
|
||||
method: "POST",
|
||||
headers: rpcRequestHeaders,
|
||||
body: JSON.stringify(rpcRequest("getBalance", [walletAddress])),
|
||||
});
|
||||
const json = await response.json();
|
||||
return json;
|
||||
}
|
||||
|
||||
// POST request to Solana API to load USDC balance
|
||||
async function loadUSDCBalance(walletAddress) {
|
||||
const response = await fetch(SOLANA_RPC, {
|
||||
method: "POST",
|
||||
headers: rpcRequestHeaders,
|
||||
body: JSON.stringify(
|
||||
rpcRequest("getTokenAccountsByOwner", [
|
||||
walletAddress,
|
||||
{ "mint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" },
|
||||
{ "encoding": "jsonParsed" }])
|
||||
),
|
||||
});
|
||||
const json = await response.json();
|
||||
return json;
|
||||
}
|
||||
|
||||
// extract wallet balance
|
||||
function extractWalletBalance(walletBalance) {
|
||||
return walletBalance.result.value / LAMPORTS_PER_SOL;
|
||||
}
|
||||
|
||||
// extract USDC balance
|
||||
function extractUSDCBalance(usdcBalance) {
|
||||
return usdcBalance.result.value[0].account.data.parsed.info.tokenAmount.uiAmount;
|
||||
}
|
||||
|
||||
// export functions
|
||||
module.exports = {
|
||||
loadWalletBalance,
|
||||
loadUSDCBalance,
|
||||
extractWalletBalance,
|
||||
extractUSDCBalance
|
||||
};
|
||||
Reference in New Issue
Block a user