From e43a3c1a664fdc56378133d893597dc4ffd64b69 Mon Sep 17 00:00:00 2001 From: Daniel Chew Date: Tue, 12 Dec 2023 15:45:42 +0900 Subject: [PATCH] cache api responses --- frontend/pages/api/v1/all_locked_accounts.ts | 27 ++++++++++++------- frontend/pages/api/v1/locked_accounts.ts | 28 ++++++++++---------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/frontend/pages/api/v1/all_locked_accounts.ts b/frontend/pages/api/v1/all_locked_accounts.ts index 8d40f13e..cf457a73 100644 --- a/frontend/pages/api/v1/all_locked_accounts.ts +++ b/frontend/pages/api/v1/all_locked_accounts.ts @@ -1,15 +1,15 @@ -import { NextApiRequest, NextApiResponse } from 'next' -import { PythBalance } from '@pythnetwork/staking/app/pythBalance' -import BN from 'bn.js' -import { STAKING_ADDRESS } from '@pythnetwork/staking/app/constants' -import { Connection, Keypair } from '@solana/web3.js' -import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes' -import { Program, AnchorProvider, IdlAccounts } from '@coral-xyz/anchor' +import { AnchorProvider, IdlAccounts, Program } from '@coral-xyz/anchor' import NodeWallet from '@coral-xyz/anchor/dist/cjs/nodewallet' +import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes' +import { splTokenProgram } from '@coral-xyz/spl-token' +import { STAKING_ADDRESS } from '@pythnetwork/staking/app/constants' +import { PythBalance } from '@pythnetwork/staking/app/pythBalance' import { Staking } from '@pythnetwork/staking/lib/target/types/staking' import idl from '@pythnetwork/staking/target/idl/staking.json' -import { splTokenProgram } from '@coral-xyz/spl-token' import { TOKEN_PROGRAM_ID } from '@solana/spl-token' +import { Connection, Keypair } from '@solana/web3.js' +import BN from 'bn.js' +import { NextApiRequest, NextApiResponse } from 'next' import { getCustodyAccountAddress, getMetadataAccountAddress, @@ -76,7 +76,7 @@ export default async function handlerAllLockedAccounts( }, new BN(0)) ) - res.status(200).json({ + const data = { totalLockedAmount: totalLockedAmount.toString(), accounts: lockedCustodyAccounts.map((account) => { return { @@ -84,7 +84,10 @@ export default async function handlerAllLockedAccounts( actualAmount: new PythBalance(account.data!.amount).toString(), // ! is safe because of the filter above } }), - }) + } + + res.setHeader('Cache-Control', 'max-age=0, s-maxage=3600') + res.status(200).json(data) } function hasStandardLockup( @@ -116,3 +119,7 @@ async function getAllStakeAccounts(connection: Connection) { return account.pubkey }) } + +export const config = { + runtime: 'experimental-edge', +} diff --git a/frontend/pages/api/v1/locked_accounts.ts b/frontend/pages/api/v1/locked_accounts.ts index eb0253bc..6130a422 100644 --- a/frontend/pages/api/v1/locked_accounts.ts +++ b/frontend/pages/api/v1/locked_accounts.ts @@ -1,15 +1,15 @@ -import { NextApiRequest, NextApiResponse } from 'next' -import { PythBalance } from '@pythnetwork/staking/app/pythBalance' -import BN from 'bn.js' -import { STAKING_ADDRESS } from '@pythnetwork/staking/app/constants' -import { Connection, Keypair, PublicKey } from '@solana/web3.js' -import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes' -import { Program, AnchorProvider } from '@coral-xyz/anchor' +import { AnchorProvider, Program } from '@coral-xyz/anchor' import NodeWallet from '@coral-xyz/anchor/dist/cjs/nodewallet' +import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes' +import { splTokenProgram } from '@coral-xyz/spl-token' +import { STAKING_ADDRESS } from '@pythnetwork/staking/app/constants' +import { PythBalance } from '@pythnetwork/staking/app/pythBalance' import { Staking } from '@pythnetwork/staking/lib/target/types/staking' import idl from '@pythnetwork/staking/target/idl/staking.json' -import { splTokenProgram } from '@coral-xyz/spl-token' import { TOKEN_PROGRAM_ID } from '@solana/spl-token' +import { Connection, Keypair, PublicKey } from '@solana/web3.js' +import BN from 'bn.js' +import { NextApiRequest, NextApiResponse } from 'next' const connection = new Connection(process.env.BACKEND_ENDPOINT!) const provider = new AnchorProvider( @@ -42,13 +42,13 @@ export default async function handlerLockedAccounts( connection, new PublicKey(owner) ) - res.status(200).json( - await Promise.all( - stakeAccounts.map((account) => { - return getStakeAccountDetails(account) - }) - ) + const stakeAccountDetails = await Promise.all( + stakeAccounts.map((account) => { + return getStakeAccountDetails(account) + }) ) + res.setHeader('Cache-Control', 'max-age=0, s-maxage=3600') + res.status(200).json(stakeAccountDetails) } }