createAssociatedTokenAccountInstruction method

Future<TransactionInstruction> createAssociatedTokenAccountInstruction(
  1. Ed25519HDPublicKey funderPubKey,
  2. Ed25519HDPublicKey ownerPubKey,
  3. Ed25519HDPublicKey mintPubKey
)

Create associated token account instruction

Funcionallity

Creates an instruction to create an associated token account.

Params

Ed25519HDPublicKey funderPubKey - The public key of the account funding the creation. Ed25519HDPublicKey ownerPubKey - The public key of the account that will own the newly created token account. Ed25519HDPublicKey mintPubKey - The public key of the token for which an account will be created.

Return

TransactionInstruction class

Implementation

Future<TransactionInstruction> createAssociatedTokenAccountInstruction(
  solana.Ed25519HDPublicKey funderPubKey,
  solana.Ed25519HDPublicKey ownerPubKey,
  solana.Ed25519HDPublicKey mintPubKey,
) async {
  final List<encoder.AccountMeta> keys = [
    encoder.AccountMeta(
      pubKey: funderPubKey,
      isWriteable: true,
      isSigner: true,
    ),
    encoder.AccountMeta(
      pubKey: await findAssociatedTokenAddress(ownerPubKey, mintPubKey),
      isWriteable: false,
      isSigner: false,
    ),
    encoder.AccountMeta(
      pubKey: ownerPubKey,
      isWriteable: false,
      isSigner: false,
    ),
    encoder.AccountMeta(
      pubKey:
          solana.Ed25519HDPublicKey.fromBase58(Constants.SYSTEM_PROGRAM_ID),
      isWriteable: false,
      isSigner: false,
    ),
    encoder.AccountMeta(
      pubKey:
          solana.Ed25519HDPublicKey.fromBase58(Constants.TOKEN_PROGRAM_ID),
      isWriteable: false,
      isSigner: false,
    ),
    encoder.AccountMeta(
      pubKey: solana.Ed25519HDPublicKey.fromBase58(
          Constants.ASSOCIATED_TOKEN_PROGRAM_ID),
      isWriteable: false,
      isSigner: false,
    ),
  ];

  ByteData data = ByteData(1);
  data.setUint8(0, 1);
  Uint8List packedData = data.buffer.asUint8List();

  return TransactionInstruction(
    keys: keys,
    programId: solana.Ed25519HDPublicKey.fromBase58(
        Constants.ASSOCIATED_TOKEN_PROGRAM_ID),
    data: packedData,
  );
}