Skip to content

Instantly share code, notes, and snippets.

@wiyarmir
Created May 4, 2022 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wiyarmir/6a1b53113ea3676a6cdcd0582651f68f to your computer and use it in GitHub Desktop.
Save wiyarmir/6a1b53113ea3676a6cdcd0582651f68f to your computer and use it in GitHub Desktop.
errors
const newKeypair = Keypair.fromSeed(seed)
const associatedSource = await getAssociatedTokenAddress(usdcPublicKey, walletPublicKey)
const associatedNew = await getAssociatedTokenAddress(usdcPublicKey, newKeypair.publicKey)
const mint = await getMint(connection, usdcPublicKey)
const receiverAccount = await connection.getAccountInfo(associatedNew)
const instructions: TransactionInstruction[] = []
instructions.push(createAssociatedTokenAccountInstruction(walletPublicKey, associatedNew, walletPublicKey, usdcPublicKey))
const lamports = await getMinimumBalanceForRentExemptAccount(connection)
instructions.push(
SystemProgram.createAccount({
fromPubkey: walletPublicKey,
newAccountPubkey: associatedNew,
space: ACCOUNT_SIZE,
lamports,
programId: TOKEN_PROGRAM_ID,
}),
createInitializeAccountInstruction(associatedNew, mint.mintAuthority, newKeypair.publicKey),
)
// TODO properly source
const DECIMALS_IN_USDC = 1000000
const usdcAmount = Math.trunc(amount * DECIMALS_IN_USDC)
instructions.push(createTransferInstruction(associatedSource, associatedNew, walletPublicKey, usdcAmount))
const transaction = new Transaction({
feePayer: walletPublicKey,
recentBlockhash: (await connection.getLatestBlockhash()).blockhash,
}).add(...instructions)
const signed = await signTransaction(transaction)
const rawTransaction = signed.serialize({ requireAllSignatures: false })
const transactionId = await connection.sendRawTransaction(transaction, { skipPreflight: true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment