Adding this question for documentation & as a reference since Address Lookup Tables (aka ALTs/LUTs/Lookup Tables) are frequently discussed on this forum, but there isn't a question concretely defining them so far. What are they?
1 Answer
LUTs allow you to store up to 256 addresses (e.g. abcdef
and ghijkl
) onchain in a special account so that you don't need to pass the whole 32 byte addresses in your transaction when referring to abcdef
and ghijkl
. Instead, we can just pass the LUT account where they are stored and their index within that LUT. Note that they are only supported in Versioned Transactions.
From the docs:
Since each transaction on the Solana blockchain requires a listing of every address that is interacted with as part of the transaction, this listing would effectively be capped at 32 addresses per transaction. With the help of Address Lookup Tables, a transaction would now be able to raise that limit to 256 addresses per transaction.
After all the desired addresses have been stored onchain in an Address Lookup Table, each address can be referenced inside a transaction by its 1-byte index within the table (instead of their full 32-byte address). This lookup method effectively "compresses" a 32-byte address into a 1-byte index value.
We can create them by doing
const web3 = require("@solana/web3.js");
// Create a new LUT
const [lookupTableInst, lookupTableAddress] =
web3.AddressLookupTableProgram.createLookupTable({
authority: payer.publicKey,
payer: payer.publicKey,
recentSlot: slot,
});
// Insert accounts into it
const extendInstruction = web3.AddressLookupTableProgram.extendLookupTable({
payer: payer.publicKey,
authority: payer.publicKey,
lookupTable: lookupTableAddress,
addresses: [
payer.publicKey,
web3.SystemProgram.programId,
// list more `publicKey` addresses here
],
});
We can then use our new LUT by doing (assuming that arrayOfInstructions
contains instructions that contain some accounts that are stored inside the LUT)
const message = new web3.TransactionMessage({
payerKey: payer.publicKey,
recentBlockhash: blockhash,
instructions: arrayOfInstructions,
}).compileToV0Message([lookupTableAccount]);
const transaction = new web3.VersionedTransaction(message);