Skip to content

Commit

Permalink
api: add mainnet vs devnet (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0ece authored May 27, 2024
1 parent 7835b59 commit 50de903
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 18 deletions.
1 change: 1 addition & 0 deletions api/.gcloudignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# below:
.git
.gitignore
.env.local

# Node.js dependencies:
node_modules/
2 changes: 2 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env.local
.env.yaml
4 changes: 2 additions & 2 deletions api/app.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
runtime: nodejs20
env_variables:
SOLANA_RPC: "https://api.devnet.solana.com"
includes:
- .env.yaml
70 changes: 55 additions & 15 deletions api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,32 @@ import { priceHistory, fundPerformance } from "./prices";
import { openfunds } from "./openfunds";
import { marinadeDelayedUnstakeTx, marinadeDelayedUnstakeClaimTx } from "./tx";

if (process.env.NODE_ENV !== "production") {
require("dotenv").config({ path: ".env.local", override: true });
}

const BASE_URL = "https://api.glam.systems";
const SOLANA_RPC = process.env.SOLANA_RPC || "http://localhost:8899";
const SOLANA_MAINNET_KEY = process.env.SOLANA_MAINNET_KEY || "";

const app: Express = express();
app.use(cors({ origin: "*", methods: "GET" }));
app.use(bodyParser.json());
/* GlamClient for devnet and testnet */

const connection = new Connection(SOLANA_RPC, "confirmed");
const provider = new AnchorProvider(connection, null, {
const devnetConnection = new Connection(SOLANA_RPC, "confirmed");
const devnetProvider = new AnchorProvider(devnetConnection, null, {
commitment: "confirmed"
});
const client = new GlamClient({ provider });
const devnetClient = new GlamClient({ provider: devnetProvider });

const mainnetConnection = new Connection(
`https://mainnet.helius-rpc.com/?api-key=${SOLANA_MAINNET_KEY}`,
"confirmed"
);
const mainnetProvider = new AnchorProvider(mainnetConnection, null, {
commitment: "confirmed"
});
const mainnetClient = new GlamClient({ provider: mainnetProvider });

/* Pyth client */

const PYTHNET_CLUSTER_NAME: PythCluster = "pythnet";
const pythClient = new PythHttpClient(
Expand All @@ -39,12 +53,23 @@ const pythClient = new PythHttpClient(
"confirmed"
);

app.use("/assets", express.static(path.join(__dirname, "assets")));
/* Express app */

app.get("/api", (req: Request, res: Response) => {
res.send({ message: "Welcome to Glam!" });
const app: Express = express();
app.use(cors({ origin: "*", methods: "GET" }));
app.use(bodyParser.json());

app.use((req, res, next) => {
if (req.hostname === "api.glam.systems") {
req.client = mainnetClient;
} else {
req.client = devnetClient;
}
next();
});

app.use("/assets", express.static(path.join(__dirname, "assets")));

/*
* Openfunds
*/
Expand All @@ -54,17 +79,23 @@ app.get("/openfunds", async (req, res) => {
req.query.funds.split(","),
req.query.template,
req.query.format,
client,
req.client,
res
);
});

app.get("/openfunds/:pubkey.:ext", async (req, res) => {
return openfunds([req.params.pubkey], "auto", req.params.ext, client, res);
return openfunds(
[req.params.pubkey],
"auto",
req.params.ext,
req.client,
res
);
});

app.get("/openfunds/:pubkey", async (req, res) => {
return openfunds([req.params.pubkey], "auto", "json", client, res);
return openfunds([req.params.pubkey], "auto", "json", req.client, res);
});

/*
Expand All @@ -77,17 +108,26 @@ app.post("/tx/jupiter/swap", async (req, res) => {
});

app.post("/tx/marinade/unstake", async (req, res) => {
return marinadeDelayedUnstakeTx(client, req, res);
return marinadeDelayedUnstakeTx(req.client, req, res);
});

app.post("/tx/marinade/unstake/claim", async (req, res) => {
return marinadeDelayedUnstakeClaimTx(client, req, res);
return marinadeDelayedUnstakeClaimTx(req.client, req, res);
});

/*
* Other
*/

app.get("/api", (req: Request, res: Response) => {
res.send({ message: "Welcome to Glam!" });
});

app.get("/genesis", async (req: Request, res: Response) => {
const genesis = await mainnetClient.provider.connection.getGenesisHash();
res.send({ genesis });
});

app.get("/prices", async (req, res) => {
const data = await pythClient.getData();
res.set("content-type", "application/json");
Expand Down Expand Up @@ -216,7 +256,7 @@ app.get("/image/:pubkey.png", async (req, res) => {

const port = process.env.PORT || 8080;
const server = app.listen(port, () => {
console.log(`Listening at http://localhost:${port}/api`);
console.log(`Api ${process.env.NODE_ENV} at http://localhost:${port}`);
});
server.on("error", console.error);

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"d3-cloud": "^1.2.7",
"d3-sankey": "^0.12.3",
"daisyui": "3.9.3",
"dotenv": "^16.4.5",
"exceljs": "^4.4.0",
"express": "^4.18.1",
"jotai": "2.5.1",
Expand Down
11 changes: 10 additions & 1 deletion pnpm-lock.yaml

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

0 comments on commit 50de903

Please sign in to comment.