2021-04-09 17:01:00 -07:00
|
|
|
import { ACCOUNT_LAYOUT } from '@blockworks-foundation/mango-client'
|
|
|
|
import { Connection, PublicKey } from '@solana/web3.js'
|
|
|
|
import * as bs58 from 'bs58'
|
|
|
|
import { TokenInstructions } from '@project-serum/serum'
|
2021-05-20 11:51:00 -07:00
|
|
|
import { WRAPPED_SOL_MINT } from '@project-serum/serum/lib/token-instructions'
|
|
|
|
|
2021-04-09 17:01:00 -07:00
|
|
|
export const TOKEN_PROGRAM_ID = new PublicKey(
|
|
|
|
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
|
|
|
|
)
|
|
|
|
|
|
|
|
export type ProgramAccount<T> = {
|
|
|
|
publicKey: PublicKey
|
|
|
|
account: T
|
|
|
|
}
|
|
|
|
|
2021-05-31 13:30:59 -07:00
|
|
|
export function parseTokenAccountData(data: Buffer): {
|
|
|
|
mint: PublicKey
|
|
|
|
owner: PublicKey
|
|
|
|
amount: number
|
|
|
|
} {
|
2021-04-09 17:01:00 -07:00
|
|
|
const { mint, owner, amount } = ACCOUNT_LAYOUT.decode(data)
|
|
|
|
return {
|
|
|
|
mint: new PublicKey(mint),
|
|
|
|
owner: new PublicKey(owner),
|
|
|
|
amount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 11:51:00 -07:00
|
|
|
export async function getWalletTokenInfo(
|
|
|
|
connection: Connection,
|
|
|
|
ownerPublicKey: PublicKey
|
|
|
|
) {
|
|
|
|
const splAccounts = await getOwnedTokenAccounts(connection, ownerPublicKey)
|
|
|
|
const account = await connection.getAccountInfo(ownerPublicKey)
|
2021-06-05 10:05:40 -07:00
|
|
|
if (!account) return splAccounts
|
2021-06-05 13:45:21 -07:00
|
|
|
return [
|
2021-05-20 11:51:00 -07:00
|
|
|
{
|
|
|
|
publicKey: ownerPublicKey,
|
|
|
|
account: {
|
|
|
|
mint: WRAPPED_SOL_MINT,
|
|
|
|
owner: ownerPublicKey,
|
|
|
|
amount: account.lamports,
|
|
|
|
},
|
|
|
|
},
|
2021-06-05 13:45:21 -07:00
|
|
|
].concat(splAccounts)
|
2021-05-20 11:51:00 -07:00
|
|
|
}
|
|
|
|
|
2021-04-09 17:01:00 -07:00
|
|
|
export async function getOwnedTokenAccounts(
|
|
|
|
connection: Connection,
|
|
|
|
publicKey: PublicKey
|
2021-05-20 11:51:00 -07:00
|
|
|
): Promise<any[]> {
|
2021-04-09 17:01:00 -07:00
|
|
|
const filters = getOwnedAccountsFilters(publicKey)
|
|
|
|
// @ts-ignore
|
|
|
|
const resp = await connection._rpcRequest('getProgramAccounts', [
|
|
|
|
TokenInstructions.TOKEN_PROGRAM_ID.toBase58(),
|
|
|
|
{
|
|
|
|
commitment: connection.commitment,
|
|
|
|
filters,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
if (resp.error) {
|
|
|
|
throw new Error(
|
|
|
|
'failed to get token accounts owned by ' +
|
|
|
|
publicKey.toBase58() +
|
|
|
|
': ' +
|
|
|
|
resp.error.message
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return resp.result.map(({ pubkey, account: { data } }) => {
|
|
|
|
data = bs58.decode(data)
|
|
|
|
return {
|
|
|
|
publicKey: new PublicKey(pubkey),
|
|
|
|
account: parseTokenAccountData(data),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getOwnedAccountsFilters(publicKey: PublicKey) {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
memcmp: {
|
|
|
|
offset: ACCOUNT_LAYOUT.offsetOf('owner'),
|
|
|
|
bytes: publicKey.toBase58(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dataSize: ACCOUNT_LAYOUT.span,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|