import { SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID , TOKEN_PROGRAM_ID , } from '@oyster/common' ; import { Keypair , PublicKey } from '@solana/web3.js' ; import { AccountLayout , Token } from '@solana/spl-token' ; import { SystemProgram , TransactionInstruction , Connection , } from '@solana/web3.js' ; param mint param owner return export async function getAssociatedTokenAddress ( mint , owner ) { return ( await PublicKey . findProgramAddress ( [ owner . toBuffer ( ) , TOKEN_PROGRAM_ID . toBuffer ( ) , mint . toBuffer ( ) ] , SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID ) ) [ 0 ] ; } export function createTokenAccount ( instructions , payer , accountRentExempt , mint , owner , signers ) { const account = createUninitializedTokenAccount ( instructions , payer , accountRentExempt , signers ) ; instructions . push ( Token . createInitAccountInstruction ( TOKEN_PROGRAM_ID , mint , account , owner ) ) ; return account ; } export function createUninitializedTokenAccount ( instructions , payer , amount , signers ) { const account = new Keypair ( ) ; instructions . push ( SystemProgram . createAccount ( { fromPubkey : payer , newAccountPubkey : account . publicKey , lamports : amount , space : AccountLayout . span , programId : TOKEN_PROGRAM_ID , } ) ) ; signers . push ( account ) ; return account . publicKey ; } export const withCreateSplTokenAccount = async ( instructions , signers , connection , mint , owner , payer ) => { const tokenAccountRentExempt = await connection . getMinimumBalanceForRentExemption ( AccountLayout . span ) ; const tokenAccountAddress = createTokenAccount ( instructions , payer , tokenAccountRentExempt , mint , owner , signers ) ; return tokenAccountAddress ; } ;