-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove expensive call to fetch tokens (#268)
- Loading branch information
Showing
1 changed file
with
24 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,37 @@ | ||
import { Connection, PublicKey } from '@solana/web3.js' | ||
import { Connection, Keypair, PublicKey } from '@solana/web3.js' | ||
import { PythBalance } from '@pythnetwork/staking' | ||
import { BN } from 'bn.js' | ||
import { | ||
ASSOCIATED_TOKEN_PROGRAM_ID, | ||
TOKEN_PROGRAM_ID, | ||
Token, | ||
} from '@solana/spl-token' | ||
|
||
export const getPythTokenBalance = async ( | ||
connection: Connection, | ||
publicKey: PublicKey, | ||
pythTokenMint: PublicKey | ||
) => { | ||
let balance = new BN(0) | ||
const mint = new Token( | ||
connection, | ||
pythTokenMint, | ||
TOKEN_PROGRAM_ID, | ||
new Keypair() | ||
) | ||
|
||
const pythAtaAddress = await Token.getAssociatedTokenAddress( | ||
ASSOCIATED_TOKEN_PROGRAM_ID, | ||
TOKEN_PROGRAM_ID, | ||
pythTokenMint, | ||
publicKey | ||
) | ||
|
||
try { | ||
const tokenAccounts = await connection.getTokenAccountsByOwner(publicKey, { | ||
mint: pythTokenMint, | ||
}) | ||
for (const account of tokenAccounts.value) { | ||
const test = await connection.getTokenAccountBalance(account.pubkey) | ||
balance.iadd( | ||
new BN(test.value.amount) ? new BN(test.value.amount) : new BN(0) | ||
) | ||
} | ||
const pythAtaAccountInfo = await mint.getAccountInfo(pythAtaAddress) | ||
return new PythBalance(pythAtaAccountInfo.amount) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
return new PythBalance(balance) | ||
|
||
return new PythBalance(new BN(0)) | ||
} |
dcacb3c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
staking-devnet – ./
staking-devnet-git-main-pyth-web.vercel.app
governance-nu.vercel.app
staking-devnet-pyth-web.vercel.app
dcacb3c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
staking – ./
staking.pyth.network
staking-pyth-web.vercel.app
staking-rho-nine.vercel.app
staking-git-main-pyth-web.vercel.app