Skip to content

Instantly share code, notes, and snippets.

@imprfekt
Created October 12, 2021 20:55
Show Gist options
  • Save imprfekt/90d35b599ec1016ac9649ab8ac91815b to your computer and use it in GitHub Desktop.
Save imprfekt/90d35b599ec1016ac9649ab8ac91815b to your computer and use it in GitHub Desktop.
function createWrappedSolAccount(){
const balanceNeeded = await Token.getMinBalanceRentForExemptAccount(connection,);
// Create a new account
const newAccount = Keypair.generate(); //todo this is not an associated token account????
const transaction = new Transaction();
transaction.add(
SystemProgram.createAccount({
fromPubkey: wallet?.publicKey as PublicKey,
newAccountPubkey: newAccount.publicKey,
lamports: balanceNeeded,
space: AccountLayout.span,
programId: TOKEN_PROGRAM_ID,
}),
);
// Send lamports to it (these will be wrapped into native tokens by the token program)
transaction.add(
SystemProgram.transfer({
fromPubkey: wallet?.publicKey as PublicKey,
toPubkey: newAccount.publicKey,
lamports: 2 * LAMPORTS_PER_SOL,
}),
);
// Assign the new account to the native token mint.
// the account will be initialized with a balance equal to the native token balance.
// (i.e. amount)
transaction.add(
Token.createInitAccountInstruction(
TOKEN_PROGRAM_ID,
NATIVE_MINT,
newAccount.publicKey,
wallet?.publicKey as PublicKey,
),
);
//this is a custom simple wrapper for sending, signing and confirming transactions. In essence does following:
//transaction.getRecentBlockhash, transaction.feePayer = wallet.publicKey, partialsign with newAccount,
//wallet.signTransaction(transaction); connection.sendRawTransaction(transaction); connection.confirmTransaction
const tx = await sendTransaction(INSTRUCTION_CREATE_STREAM, transaction, connection, wallet, newAccount)
wrappedSolAcc = newAccount.publicKey;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment