I have the transaction instruction which only required the wrap sol to proceed further like depositing it in another vault so, I have to convert the sol to wrapsol first and then proceed with that transaction
2 Answers
const wrapSol = async (
connection: Connection,
wallet: any,
depositAmount: number,
) => {
let createWrapSolAccountTransaction = new Transaction()
const wrapSolAccount = Keypair.generate()
const accountRentExempt = await connection.getMinimumBalanceForRentExemption(AccountLayout.span)
createWrapSolAccountTransaction.add(
SystemProgram.createAccount({
fromPubkey: wallet.publicKey,
newAccountPubkey: wrapSolAccount.publicKey,
lamports: depositAmount * (10 ** 9) + accountRentExempt * 3,
space: AccountLayout.span,
programId: TOKEN_PROGRAM_ID,
})
)
createWrapSolAccountTransaction.add(
Token.createInitAccountInstruction(
TOKEN_PROGRAM_ID,
NATIVE_MINT,
wrapSolAccount.publicKey,
wallet.publicKey,
)
)
let signature: any = await wallet.sendTransaction(
createWrapSolAccountTransaction,
connection,
{signers: [wrapSolAccount] }
)
await connection.confirmTransaction(signature, 'processed')
let balance = await connection.getTokenAccountBalance(wrapSolAccount.publicKey);
console.log("+ User's wrapSol account: ", wrapSolAccount.publicKey.toBase58(), balance.value.uiAmountString)
return {
address: wrapSolAccount.publicKey,
balance: balance.value.uiAmountString,
}
}
Check out this example in Solana Cookbook: https://solanacookbook.com/references/token.html#add-balance