2

In my application I need to generate new addresses from time to time. I need them for SOL as well as for the stable coins on Solana. A new address will be assigned to a new user.

Since Solana has separate addresses for tokens, I've become confused with "getAssociatedTokenAddress" function.

a) Will I have to generate an associated addresses for USDT, USDC and probably 1-2 others well-known stable coins when I generate a new SOL address? In order to receive those stable coins.

Or b) could I give a user a new SOL address and he would be able to use it for everything: SOL, USDT, USDC, USDD...? A user would use it by only sending coins to it.

If an associated address for a stable coin is required, how to generate it in web3 out of the main address for SOL?

1 Answer 1

2

Hey yes for every token except native sol that you want to send you need to create a token account if it does not yet exists. Best is to use an associated token account which can be derived from the mint and the wallet pubkey for web3js you can use this:

const tokenAccount = await getOrCreateAssociatedTokenAccount(
  connection,
  wallet.keypair,
  mint,
  wallet.keypair.publicKey
);

But you don't NEED to create it for every new sol address in advance. Most wallets for example do it for you when you send a token first time to a solana address. Here its very well explained: https://spl.solana.com/token

7
  • if it does not yet exists. --> how may it not exist already? Every possible account on most of the blockchains exists already, whether it's already been generated or not yet. If it hasn't been generated by someone, it stil exists, but implicitly. Why generate it?
    – Camila326
    Commented Jan 7, 2023 at 21:27
  • You've contradicted yourself --> yes for every token except native sol that you want to send you NEED to create a token account. Then But you don't NEED to create this.
    – Camila326
    Commented Jan 7, 2023 at 21:30
  • Most wallets for example do it for you when you send --> so what? I'm not using a wallet, nor can be certain that each of my users will
    – Camila326
    Commented Jan 7, 2023 at 21:31
  • A token account only exists when its initialized so it needs to have some sol in it to be rent exempt before you can send a spl token to it. On the other one I didnt really contradict my self :D I said you need it when you want to SEND something, but you dont need to create it just for it to be there for every new address. Sorry for not being clear on that.
    – Jonas H.
    Commented Jan 8, 2023 at 9:42
  • 1
    Check out the metaplex docs which have great explanations/diagrams for understanding the token accounts: docs.metaplex.com/programs/token-metadata/overview Every token account requires SOL for rent, so you only want to create token accounts for tokens a wallet is going to hold. If you send tokens to another wallet that doesn't hold the token, you would pay the rent to create their token account. Token accounts can be closed to retrieve the rent that was used to create the token account. This can be useful if you have token accounts with a 0 balance.
    – john
    Commented Jan 12, 2023 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.