0

It is weird that it can get the associated account since it is new generated address.

const account = Keypair.generate();

let address = await Token.getAssociatedTokenAddress(
  ASSOCIATED_TOKEN_PROGRAM_ID,
  TOKEN_PROGRAM_ID,
  token.publicKey,
  account
);
Noldorin Zhang
  • 181
  • 1
  • 2
  • 7
  • Why is it weird? You're generating an account essentially by passing all the required parameters it needs. Accounts can be empty with no data or balance(SOL) in them. Only after one epoch they are cleaned. – munanadi Sep 11 '21 at 05:55
  • I read the source code of `getAssociatedTokenAddress`, and it truely will return a address by calling `findProgramAddress`. It is counter-intuitive with null returning. – Noldorin Zhang Sep 13 '21 at 02:24

1 Answers1

0

The address generated from getAssociatedTokenAddress is deterministic, so given the same public key inputs, it will always have the same output. Additionally, it is a program-derived address, so it can only be "signed for" by the program.

You can find more information about program-derived addresses at https://docs.solana.com/developing/programming-model/calling-between-programs#program-derived-addresses

Jon C
  • 7,019
  • 10
  • 17