Last active
May 21, 2024 22:35
-
-
Save NotoriousPyro/5719fb4954dd9ccdd540a5f73e2522e6 to your computer and use it in GitHub Desktop.
Solana example confirm tx and save the created ATAs in a database, so they don't need re-creating
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
await confirmTransaction(signature).then( | |
async (logs) => { | |
logger.log("Confirmed", logs.signature); | |
const createdAtas = mintIdempotentInstructions.map(instruction => { | |
return { | |
ata: instruction.keys[1].pubkey, | |
mint: instruction.keys[3].pubkey, | |
owner: instruction.keys[5].pubkey, | |
}; | |
}) | |
const createdAtaInfo = await getAccountInfos(createdAtas.map(ata => new PublicKey(ata.ata))) | |
createdAtaInfo.forEach((ataInfo, index) => { | |
const { publicKey: ata, accountInfo } = ataInfo; | |
if (!accountInfo || accountInfo.lamports === 0) { | |
return; | |
} | |
const mint = createdAtas[index].mint; | |
const owner = createdAtas[index].owner; | |
void TokenInfoDB.put(mint.toString(), { | |
ata: ata.toString(), | |
ataCreated: true, | |
isTokenProgram2022: getTokenProgramIdFromPublicKey(owner).equals(TOKEN_2022_PROGRAM_ID), | |
}); | |
}); | |
void SwapInfoDB.putSkipPreflight(swapInfoA, swapInfoB); | |
} | |
); | |
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
export async function* generateAccountInfos( | |
publicKeys: PublicKey[], | |
chunkSize: number = 100 | |
): AsyncGenerator<{ publicKey: PublicKey, accountInfo: AccountInfo<Buffer> }> { | |
for (let i = 0; i < publicKeys.length; i += chunkSize) { | |
const chunk = publicKeys.slice(i, i + chunkSize); | |
const accountInfos = await connection.getMultipleAccountsInfo(chunk); | |
for (const [index, accountInfo] of accountInfos.entries()) { | |
yield { publicKey: chunk[index], accountInfo } | |
} | |
} | |
} | |
export const getAccountInfos = async ( | |
publicKeys: PublicKey[], | |
chunkSize: number = 100 | |
): Promise<{ publicKey: PublicKey, accountInfo: AccountInfo<Buffer> }[]> => { | |
const accountInfos = []; | |
for await (const accountInfo of generateAccountInfos(publicKeys, chunkSize)) { | |
accountInfos.push(accountInfo); | |
} | |
return accountInfos; | |
} | |
export const getTokenProgramIdFromPublicKey = (publicKey: PublicKey) => { | |
if (publicKey.equals(TOKEN_2022_PROGRAM_ID)) { | |
return TOKEN_2022_PROGRAM_ID; | |
} | |
if (publicKey.equals(TOKEN_PROGRAM_ID)) { | |
return TOKEN_PROGRAM_ID; | |
} | |
throw new Error("Invalid token program id"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment