File tree Expand file tree Collapse file tree 1 file changed +24
-12
lines changed Expand file tree Collapse file tree 1 file changed +24
-12
lines changed Original file line number Diff line number Diff line change 1
- import { Connection , PublicKey } from '@solana/web3.js'
1
+ import { Connection , Keypair , PublicKey } from '@solana/web3.js'
2
2
import { PythBalance } from '@pythnetwork/staking'
3
3
import { BN } from 'bn.js'
4
+ import {
5
+ ASSOCIATED_TOKEN_PROGRAM_ID ,
6
+ TOKEN_PROGRAM_ID ,
7
+ Token ,
8
+ } from '@solana/spl-token'
4
9
5
10
export const getPythTokenBalance = async (
6
11
connection : Connection ,
7
12
publicKey : PublicKey ,
8
13
pythTokenMint : PublicKey
9
14
) => {
10
- let balance = new BN ( 0 )
15
+ const mint = new Token (
16
+ connection ,
17
+ pythTokenMint ,
18
+ TOKEN_PROGRAM_ID ,
19
+ new Keypair ( )
20
+ )
21
+
22
+ const pythAtaAddress = await Token . getAssociatedTokenAddress (
23
+ ASSOCIATED_TOKEN_PROGRAM_ID ,
24
+ TOKEN_PROGRAM_ID ,
25
+ pythTokenMint ,
26
+ publicKey
27
+ )
28
+
11
29
try {
12
- const tokenAccounts = await connection . getTokenAccountsByOwner ( publicKey , {
13
- mint : pythTokenMint ,
14
- } )
15
- for ( const account of tokenAccounts . value ) {
16
- const test = await connection . getTokenAccountBalance ( account . pubkey )
17
- balance . iadd (
18
- new BN ( test . value . amount ) ? new BN ( test . value . amount ) : new BN ( 0 )
19
- )
20
- }
30
+ const pythAtaAccountInfo = await mint . getAccountInfo ( pythAtaAddress )
31
+ return new PythBalance ( pythAtaAccountInfo . amount )
21
32
} catch ( e ) {
22
33
console . error ( e )
23
34
}
24
- return new PythBalance ( balance )
35
+
36
+ return new PythBalance ( new BN ( 0 ) )
25
37
}
You can’t perform that action at this time.
0 commit comments