Last active
October 13, 2024 11:33
-
-
Save NotoriousPyro/b72f9ebb5b76549412d661270d5cea19 to your computer and use it in GitHub Desktop.
Withdraw nonce account
This file contains hidden or 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
/** | |
* Withdraw the lamports from the nonce accounts and close them. | |
* @param nonceAccountPublicKeys List of nonce account public keys | |
* @param nonceAccountAuthorisedPubkey Authorised public key for the nonce accounts | |
* @param nonceAccountToPubkey Public key to withdraw the lamports to | |
* @param txPayerAndSigner Transaction payer and signer | |
*/ | |
const withdrawNonceAccount = async ( | |
nonceAccountPublicKeys: PublicKey[], | |
nonceAccountAuthorisedPubkey: PublicKey, | |
nonceAccountToPubkey: PublicKey, | |
txPayerAndSigner: Keypair | |
) => { | |
const chunkSize = 5; | |
for (let i = 0; i < nonceAccountPublicKeys.length; i += chunkSize) { | |
const instruction = new Transaction(); | |
const chunk = nonceAccountPublicKeys.slice(i, i + chunkSize); | |
for (const publicKey of chunk) { | |
const rent = await connection.getMinimumBalanceForRentExemption(NONCE_ACCOUNT_LENGTH) | |
const nonceparams: WithdrawNonceParams = { | |
noncePubkey: publicKey, | |
authorizedPubkey: nonceAccountAuthorisedPubkey, | |
toPubkey: nonceAccountToPubkey, | |
lamports: rent, | |
}; | |
instruction.add( | |
SystemProgram.nonceWithdraw(nonceparams), | |
) | |
} | |
const bhInfo = await connection.getLatestBlockhashAndContext({ commitment: "finalized" }); | |
const messageV0 = new TransactionMessage({ | |
payerKey: txPayerAndSigner.publicKey, | |
recentBlockhash: bhInfo.value.blockhash, | |
instructions: [ | |
...createComputeBudgetInstruction( | |
200_000, 100 | |
), | |
...instruction.instructions | |
], | |
}).compileToV0Message(); | |
const tx = new VersionedTransaction(messageV0); | |
tx.sign([txPayerAndSigner]); | |
const simulation = await connection.simulateTransaction(tx, { commitment: "processed" }); | |
console.log("Simulation: ", simulation.value); | |
if (simulation.value.err === "BlockhashNotFound") { | |
throw new Error("Blockhash not found. Try again."); | |
} | |
if (simulation.value.err) { | |
throw simulation.value.err; | |
} | |
try { | |
const signature = await connection.sendTransaction(tx, { | |
maxRetries: 20, | |
skipPreflight: true, | |
preflightCommitment: "processed", | |
}); | |
const confirmation = await connection.confirmTransaction({ | |
signature, | |
blockhash: bhInfo.value.blockhash, | |
lastValidBlockHeight: bhInfo.value.lastValidBlockHeight, | |
}, "finalized"); | |
if (confirmation.value.err) { | |
throw new Error(`Transaction not confirmed: ${confirmation.value.err.toString()}`); | |
} | |
console.log("Confirmed: ", signature); | |
console.log("Nonce accounts withdrawn", chunk.map(key => key.toString())); | |
} catch (error) { | |
console.error("Failed: ", error); | |
throw error; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment