Skip to content

Commit

Permalink
api: return real time prices (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
yurushao authored Apr 6, 2024
1 parent 83b369b commit ff575c5
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 12 deletions.
2 changes: 2 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"author": "",
"license": "ISC",
"dependencies": {
"@pythnetwork/pyth-evm-js": "^1.38.0",
"@scure/base": "^1.1.6",
"bn.js": "^5.2.1",
"canvas": "^2.11.2",
"cors": "^2.8.5",
"express": "^4.19.2"
Expand Down
140 changes: 134 additions & 6 deletions api/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 28 additions & 4 deletions api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { createCanvas, loadImage } = require("canvas");
const { validatePubkey } = require("./validation");
const { priceHistory, fundPerformance } = require("./prices");
const cors = require("cors");
const pyth = require("@pythnetwork/pyth-evm-js");

BASE_URL = "https://api.glam.systems";

Expand All @@ -23,6 +24,29 @@ app.get("/_/health", (req, res) => {
res.send("ok");
});

app.get("/prices", async (req, res) => {
const connection = new pyth.EvmPriceServiceConnection(
"https://hermes.pyth.network"
);
// You can find the ids of prices at https://pyth.network/developers/price-feed-ids#pyth-evm-stable
const priceIds = [
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43", // BTC/USD price id
"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace", // ETH/USD price id
"0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d", // SOL/USD price id
"0xeaa020c61cc479712813461ce153894a96a6c00b21ed0cfc2798d1f9a9e9c94a" // USDC/USD price id
];
const priceFeeds = await connection.getLatestPriceFeeds(priceIds);
res.set("content-type", "application/json");
res.send(
JSON.stringify({
btc: parseFloat(priceFeeds[0].price.price) / 1e8,
eth: parseFloat(priceFeeds[1].price.price) / 1e8,
usdc: parseFloat(priceFeeds[2].price.price) / 1e8,
sol: parseFloat(priceFeeds[3].price.price) / 1e8
})
);
});

app.get("/fund/:pubkey/perf", async (req, res) => {
const { w_btc = 0.4, w_eth = 0, w_sol = 0.6 } = req.query;
// TODO: validate input
Expand All @@ -38,9 +62,9 @@ app.get("/fund/:pubkey/perf", async (req, res) => {
const { closingPrices: solClosingPrices } = await priceHistory(
"Crypto.SOL/USD"
);
const { closingPrices: usdcClosingPrices } = await priceHistory(
"Crypto.USDC/USD"
);
// const { closingPrices: usdcClosingPrices } = await priceHistory(
// "Crypto.USDC/USD"
// );

const { weightedChanges, btcChanges, ethChanges, solChanges } =
fundPerformance(
Expand All @@ -55,7 +79,7 @@ app.get("/fund/:pubkey/perf", async (req, res) => {
res.send(
JSON.stringify({
timestamps,
usdcClosingPrices,
// usdcClosingPrices,
fundPerformance: weightedChanges,
btcPerformance: btcChanges,
ethPerformance: ethChanges,
Expand Down
21 changes: 19 additions & 2 deletions api/tests/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,33 @@ describe("Test /fund/:pubkey/perf", () => {
server.close();
});

it("", async () => {
it("Expected response", async () => {
const res = await requestWithSupertest.get("/fund/xyz/perf");
expect(res.status).toEqual(200);
expect(res.body).toEqual({
timestamps: expect.any(Array),
fundPerformance: expect.any(Array),
usdcClosingPrices: expect.any(Array),
// usdcClosingPrices: expect.any(Array),
btcPerformance: expect.any(Array),
ethPerformance: expect.any(Array),
solPerformance: expect.any(Array)
});
});
});

describe("Test /prices", () => {
afterAll(() => {
server.close();
});

it("Expected response", async () => {
const res = await requestWithSupertest.get("/prices");
expect(res.status).toEqual(200);
expect(res.body).toEqual({
btc: expect.any(Number),
eth: expect.any(Number),
sol: expect.any(Number),
usdc: expect.any(Number)
});
});
});

0 comments on commit ff575c5

Please sign in to comment.