0

I'm trying to see how you can get the "ownerof" token like on Ethereum but on the Solana blockchain instead.

For example, I want a user to access a certain part of the website only if they have x token on their phantom wallet (or other Solana wallet).

TylerH
  • 20,799
  • 66
  • 75
  • 101
Relativity
  • 127
  • 2
  • 10

1 Answers1

0

If you want to understand if a user owns a specific token, you'll have to check if they current own that specific mint's token account and have a balance > 0.

Code would be as follows checking amount that address GKNcUmNacSJo4S2Kq3DuYRYRGw3sNUfJ4tyqd198t6vQ owns of USDC:

import {clusterApiUrl, Connection, Keypair, LAMPORTS_PER_SOL, ParsedAccountData, PublicKey} from '@solana/web3.js';
import {ASSOCIATED_TOKEN_PROGRAM_ID, Token, TOKEN_PROGRAM_ID} from "@solana/spl-token";

const publicKey = new PublicKey("GKNcUmNacSJo4S2Kq3DuYRYRGw3sNUfJ4tyqd198t6vQ");
const mint = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");

const connection = new Connection(clusterApiUrl('mainnet-beta'));

const associatedAddress = await Token.getAssociatedTokenAddress(
    ASSOCIATED_TOKEN_PROGRAM_ID,
    TOKEN_PROGRAM_ID,
    mint,
    publicKey
  );

console.log(associatedAddress.toBase58());

const tokenAccountInfo = await connection.getParsedAccountInfo(associatedAddress);

console.log((tokenAccountInfo.value?.data as ParsedAccountData).parsed.info.tokenAmount.amount);
Jacob Creech
  • 1,797
  • 2
  • 11