Created
October 12, 2021 20:54
-
-
Save imprfekt/32404e196a604a8538c8606084e31436 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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