I want to combine multiple instructions of swap function using addresses of lookup table(LUT). But I stuck with it when combine instructions and sign with addresses in LUT. If you guys how to fix it. Please help me. Thanks all <3
const directionIn = poolKeys.quoteMint.toString() == toToken;
const { minAmountOut, amountIn } = await this.calcAmountOut(
poolKeys,
amount,
slippage,
directionIn
);
// TODO: temp
// https://explorer.solana.com/address/2jXTrPQoZc9j31Ggxu4CCBcGWoRisYZuzu9ntQUQAtnA/entries
const lookupTableAddress = new PublicKey(
"2jXTrPQoZc9j31Ggxu4CCBcGWoRisYZuzu9ntQUQAtnA"
);
const lookupTableAccount = (
await this.connection.getAddressLookupTable(lookupTableAddress)
).value;
const txs: TransactionInstruction[] = [];
for (const address of lookupTableAccount.state.addresses) {
const userTokenAccounts = await this.getOwnerTokenAccounts(address);
const swapTransaction = await Liquidity.makeSwapInstructionSimple({
connection: this.connection,
makeTxVersion: useVersionedTransaction ? 0 : 1,
poolKeys: {
...poolKeys,
},
userKeys: {
tokenAccounts: userTokenAccounts,
owner: address,
},
amountIn: amountIn,
amountOut: minAmountOut,
fixedSide: fixedSide,
config: {
bypassAssociatedCheck: false,
},
computeBudgetConfig: {
microLamports: maxLamports,
},
});
const instructions =
swapTransaction.innerTransactions[0].instructions.filter(Boolean);
txs.push(...instructions);
}
console.log(" ✅ - Length of txs", txs.length);
this.recentBlock = await this.connection.getLatestBlockhash("confirmed");
console.log(
" ✅ - Fetched latest blockhash. Last Valid Height:",
this.recentBlock.lastValidBlockHeight
);
const swapVersionedTransaction = new VersionedTransaction(
new TransactionMessage({
payerKey: this.wallet.publicKey,
recentBlockhash: this.recentBlock.blockhash,
instructions: txs,
}).compileToV0Message([lookupTableAccount])
);
console.log(" ✅ - Compiled Transaction Message");
// TODO: because LUT have 2 addresses, need signing with 2 signers
var signerArr = [
"PK_1", // <--------- Private key of address 1 in LUT
"PK_1", // <--------- Private key of address 2 in LUT
];
var signers: Keypair[] = [];
signerArr.forEach((s) => {
signers.push(Keypair.fromSecretKey(base58.decode(s)));
});
swapVersionedTransaction.sign([...signers]);