EDIT:
The error you're getting is being thrown by this check. The instruction to create a lookup table requires the client to pass in a recent slot as an argument, and checks that that slot is valid(contained in the slot-hashes cache). The definition of "valid" here is a slot during which a block was actually produced.
Hence you should get your recentSlot parameter with:
const currentSlot = await connection.getSlot();
console.log('currentSlot:', currentSlot);
const slots = await connection.getBlocks(currentSlot - 200);
if (slots.length < 100) {
throw new Error(`Could find only ${slots.length} ${slots} on the main fork`);
}
const recentSlot = slots[0];
OR just specify a finalized
commitment for your getSlot()
rpc call.
const recentSlot = await provider.connection.getSlot("finalized"); // note the commitment
INITIAL:
Address Lookup Tables usually need a cooldown period
after being created, before which they are considered unactivated and cannot be used. You can either try waiting an arbitrary amount of time, for example:
const sleep = async (ms = 0): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, ms));
await sleep(10000);
const lutAccount = await provider.connection
.getAddressLookupTable(lookupTable)
.then((res) => res.value);
if (lutAccount === null) {
throw new Error(
"Something went wrong. We don't expect our lookup table to be un-initialized, but it is."
);
}
or to be precise, wait for a new block, which is the criteria for a lookup table's activation:
const waitForNewBlock = async(connection: web3.Connection, targetHeight: number) => {
console.log(`Waiting for ${targetHeight} new blocks`);
return new Promise(async (resolve: any) => {
// Get the last valid block height
const { lastValidBlockHeight } = await connection.getLatestBlockhash();
// Check if at least targetHeight amount of new blocks are generated every 1 second
const intervalId = setInterval(async () => {
const { lastValidBlockHeight: newValidBlockHeight } =
await connection.getLatestBlockhash();
if (newValidBlockHeight > lastValidBlockHeight + targetHeight) {
clearInterval(intervalId);
resolve();
}
}, 1000);
});
}
// Lifted from https://medium.com/@brknarsy/guide-to-optimizing-transactions-with-address-lookup-tables-alts-on-solana-85e654aa6789