Skip to content

Commit

Permalink
cache api responses
Browse files Browse the repository at this point in the history
  • Loading branch information
cctdaniel committed Dec 12, 2023
1 parent 7219f0a commit e43a3c1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
27 changes: 17 additions & 10 deletions frontend/pages/api/v1/all_locked_accounts.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -76,15 +76,18 @@ export default async function handlerAllLockedAccounts(
}, new BN(0))
)

res.status(200).json({
const data = {
totalLockedAmount: totalLockedAmount.toString(),
accounts: lockedCustodyAccounts.map((account) => {
return {
custodyAccount: account.pubkey.toBase58(),
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(
Expand Down Expand Up @@ -116,3 +119,7 @@ async function getAllStakeAccounts(connection: Connection) {
return account.pubkey
})
}

export const config = {
runtime: 'experimental-edge',
}
28 changes: 14 additions & 14 deletions frontend/pages/api/v1/locked_accounts.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit e43a3c1

Please sign in to comment.