<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kingsley Abraham</title>
    <description>The latest articles on DEV Community by Kingsley Abraham (@siggiovanni).</description>
    <link>https://dev.to/siggiovanni</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1213733%2F7692d195-d1dc-4c8e-8a43-6b17558826d5.jpg</url>
      <title>DEV Community: Kingsley Abraham</title>
      <link>https://dev.to/siggiovanni</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/siggiovanni"/>
    <language>en</language>
    <item>
      <title>A Developer Guide on Address Lookup Tables (ALTs) in Solana Development</title>
      <dc:creator>Kingsley Abraham</dc:creator>
      <pubDate>Mon, 20 Nov 2023 00:20:53 +0000</pubDate>
      <link>https://dev.to/siggiovanni/a-developer-guide-on-address-lookup-tables-alts-in-solana-development-1b06</link>
      <guid>https://dev.to/siggiovanni/a-developer-guide-on-address-lookup-tables-alts-in-solana-development-1b06</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Welcome, Solana developers! Today, we're diving into the mystical world of Address Lookup Tables, or as we fondly call them, ALTs. It's like having a secret decoder ring but for Solana accounts. Now, if you've ever felt the pain of loading addresses one by one and thought, "There has to be a better way," well, ALTs are here to turn that frown into a cryptographic smile.&lt;/p&gt;

&lt;p&gt;Now, assuming you're not here for the stand-up routine, let's talk about why ALTs are the unsung heroes of Solana development. If you've ever wished your transactions were as smooth as butter and your codebase as organized as a librarian's dream, you're in for a treat. ALTs are about to become your new best code buddy. So, buckle up your seatbelt – or should I say, fasten your seatbelts – and let's ALT-er your Solana coding experience! 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  Why ALTs
&lt;/h2&gt;

&lt;p&gt;Address Lookup Tables (ALTs) in the context of Solana development refer to data structures designed to efficiently manage and retrieve on-chain data by associating unique identifiers with corresponding addresses. These tables are used to optimize the organization and processing of multiple addresses within a Solana smart contract.&lt;/p&gt;

&lt;p&gt;Address Lookup Tables (ALTs) are crafted to tackle numerous challenges and inefficiencies prevalent in the management of on-chain data within the Solana blockchain. Here's a glimpse into some typical issues that ALTs are primed to resolve. Allow me to emphasize – these are the arenas where ALTs truly shine, baby!:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Transaction Efficiency&lt;/strong&gt;: Loading and manipulating multiple addresses individually in a single transaction can be expensive in terms of computation. ALTs provide a mechanism to group related addresses together, enabling developers to perform operations on a batch of addresses in a more efficient manner within a single transaction.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Organization&lt;/strong&gt;: As Solana applications grow in complexity, managing a large number of on-chain accounts becomes challenging. ALTs provide a structured way to organize and access on-chain data, making the codebase more readable and maintainable. This becomes especially crucial when dealing with decentralized applications (DApps) that involve numerous accounts and complex interactions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Atomic Updates&lt;/strong&gt;: When multiple accounts need to be updated together in a transaction, ensuring atomicity is crucial for maintaining data consistency. ALTs provide a way to update related data atomically, ensuring that all changes occur in a single, indivisible transaction.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Enhancing Readability and Maintainability&lt;/strong&gt;: Scattering addresses and their interactions throughout the codebase can make the code less readable and harder to maintain. ALTs centralize the management of addresses, making the codebase more modular and improving developer understanding.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I trust that I have succeeded in persuading you rather than causing any confusion regarding the importance of integrating ALTs into your upcoming versioned transactions. Now, let's delve into the practical details of addressing these challenges.&lt;/p&gt;

&lt;h2&gt;
  
  
  What awesomeness awaits you in these next practical steps?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
Before we embark on this adventure, make sure you have the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js (version 16.15 or higher) installed on your machine.&lt;/li&gt;
&lt;li&gt;Typescript experience and ts-node installed.&lt;/li&gt;
&lt;li&gt;Basic or solid experience in running Solana transactions&lt;/li&gt;
&lt;li&gt;A wallet loaded with devnet SOL funds.&lt;/li&gt;
&lt;li&gt;An API endpoint to connect with the Solana network. &lt;a href="https://dashboard.quicknode.com/" rel="noopener noreferrer"&gt;click here to setup one&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let's dive into the action!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project Setup&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;To proceed with this guide, you'll need to incorporate the Solana Web3 library. Open your terminal at the project directory and enter the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add @solana/web3.js
#or
npm install @solana/web3.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After these steps, setup your folder structure to look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ls
app.ts  node_modules/  package.json  package-lock.json  tsconfig.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's time to develop, first thing first, create a devnet wallet and get it funded.&lt;/p&gt;

&lt;p&gt;Set up your API endpoint to connect with the Solana network:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0l87tjjj6zpeqgvtyl8m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0l87tjjj6zpeqgvtyl8m.png" alt="Quick node setup"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open your app.ts and setup your app like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {
  AddressLookupTableProgram,
  Connection,
  Keypair,
  LAMPORTS_PER_SOL,
  PublicKey,
  SystemProgram,
  Transaction,
  TransactionInstruction,
  TransactionMessage,
  VersionedTransaction,
} from "@solana/web3.js";

const secret = [0,...,0]; //Replace with your wallet secret
const SIGNER_WALLET = Keypair.fromSecretKey(new Uint8Array(secret));
const DESTINATION_WALLET = Keypair.generate();
//const LOOKUP_TABLE_ADDRESS = new PublicKey(
); // We will add this later

const QUICKNODE_RPC =
  "https://example.solana-devnet.quiknode.pro/0123456/"; // Replace Your endpoint to connect to solana network
const SOLANA_CONNECTION = new Connection(QUICKNODE_RPC);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Construct a Version 0 Transaction&lt;/strong&gt;&lt;br&gt;
To use Lookup Tables, you must use Version 0 transactions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function createAndSendV0Tx(txInstructions: TransactionInstruction[]) {
    // Fetch Latest Blockhash
    const latestBlockhash = await SOLANA_CONNECTION.getLatestBlockhash('finalized');
    console.log("✅ Fetched latest blockhash. Last valid height:", latestBlockhash.lastValidBlockHeight);

    // Generate Transaction Message
    const messageV0 = new TransactionMessage({
        payerKey: SIGNER_WALLET.publicKey,
        recentBlockhash: latestBlockhash.blockhash,
        instructions: txInstructions
    }).compileToV0Message();
    console.log("✅ Compiled transaction message");

    // Sign the Transaction
    const transaction = new VersionedTransaction(messageV0);
    transaction.sign([SIGNER_WALLET]);
    console.log("✅ Transaction Signed");

    // Send Transaction to the Cluster
    const txid = await SOLANA_CONNECTION.sendTransaction(transaction, { maxRetries: 5 });
    console.log("✅ Transaction sent to network");

    // Confirm Transaction 
    const confirmation = await SOLANA_CONNECTION.confirmTransaction({
        signature: txid,
        blockhash: latestBlockhash.blockhash,
        lastValidBlockHeight: latestBlockhash.lastValidBlockHeight
    });

    if (confirmation.value.err) { throw new Error("❌ Transaction not confirmed.") }
    console.log('🎉 Transaction successfully confirmed!', '\n', `https://explorer.solana.com/tx/${txid}?cluster=devnet`);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Creating an Address Lookup Table&lt;/strong&gt;&lt;br&gt;
We'll lay the foundation by crafting your very own ALT, a secret weapon for efficient address management. Create a new async function, createLookupTable, that will build our transaction instruction and invoke createAndSendV0Tx:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function initializeLookupTable() {
    // Step 1 - Acquire a lookup table address and create lookup table instruction
    const [lookupTableInst, lookupTableAddress] =
        AddressLookupTableProgram.createLookupTable({
            authority: SIGNER_WALLET.publicKey,
            payer: SIGNER_WALLET.publicKey,
            recentSlot: await SOLANA_CONNECTION.getSlot(),
        });

    // Step 2 - Display Lookup Table Address
    console.log("Lookup Table Address:", lookupTableAddress.toBase58());

    // Step 3 - Generate a transaction and dispatch it to the network
    createAndSendV0Tx([lookupTableInst]);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;createLookupTable();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Remember this? The lookup table address earlier commented, now let's add the address we just generated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const LOOKUP_TABLE_ADDRESS = new PublicKey("YOUR_TABLE_ADDRESS_HERE"); 
// e.g., const LOOKUP_TABLE_ADDRESS = new PublicKey("3uBhgRWPTPLfvfqxi4M9eVZC8nS1kDG9XPkdHKgG69nw");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Add Addresses to a Lookup Table&lt;/strong&gt;&lt;br&gt;
Time to populate that ALT! We'll guide you through the process of seamlessly adding addresses, creating a powerhouse of organized on-chain data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function addAddressesToTable() {
    // Step 1 - Create Transaction Instruction
    const addAddressesInstruction = AddressLookupTableProgram.extendLookupTable({
        payer: SIGNER_WALLET.publicKey,
        authority: SIGNER_WALLET.publicKey,
        lookupTable: LOOKUP_TABLE_ADDRESS,
        addresses: [
            Keypair.generate().publicKey,
            Keypair.generate().publicKey,
            Keypair.generate().publicKey,
            Keypair.generate().publicKey,
            Keypair.generate().publicKey
        ],
    });
    // Step 2 - Generate a transaction and send it to the network
    await createAndSendV0Tx([addAddressesInstruction]);
    console.log(`Lookup Table Entries: `,`https://explorer.solana.com/address/${LOOKUP_TABLE_ADDRESS.toString()}/entries?cluster=devnet`)

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;addAddressesToTable();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fetch an Address Lookup Table&lt;/strong&gt;&lt;br&gt;
Unveil the magic! Learn how to retrieve and access your meticulously organized ALT, empowering your code with structured data retrieval.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function findAddressesInTable() {
    // Step 1 - Fetch our address lookup table
    const lookupTableAccount = await SOLANA_CONNECTION.getAddressLookupTable(LOOKUP_TABLE_ADDRESS)
    console.log(`Successfully found lookup table: `, lookupTableAccount.value?.key.toString());

    // Step 2 - Make sure our search returns a valid table
    if (!lookupTableAccount.value) return;

    // Step 3 - Log each table address to console
    for (let i = 0; i &amp;lt; lookupTableAccount.value.state.addresses.length; i++) {
        const address = lookupTableAccount.value.state.addresses[i];
        console.log(`   Address ${(i + 1)}: ${address.toBase58()}`);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;findAddressesInTable();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compare Transaction Sizes&lt;/strong&gt;&lt;br&gt;
The grand finale! Witness the showdown – we'll compare the transaction size of two nearly identical transactions. One armed with the mighty ALT and the other navigating the wild Solana blockchain without it. Spoiler alert: You're in for some sizeable revelations!&lt;br&gt;
Here are the results from my local machine:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frl5k8xpt6n4s2pez45pv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frl5k8xpt6n4s2pez45pv.png" alt="Results"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pratical Use cases
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Token Balances&lt;/strong&gt;: Use ALTs to manage and organize token balances associated with different user addresses efficiently.
Improve the speed and cost-effectiveness of operations involving token transfers and balances.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Function to manage token balances using Address Lookup Tables (ALTs)
async function manageTokenBalances() {
  // Assume ALT is used to store token balances
  const altAddress = getAltAddress(userPublicKey); // Function to get ALT address

  // Update token balance in ALT
  const updateTokenBalance = async (userPublicKey, newBalance) =&amp;gt; {
    const altInst = TokenBalanceProgram.updateBalance({
      altAddress,
      userPublicKey,
      newBalance,
    });
    await createAndSendV0Tx([altInst]);
  };

  // Retrieve token balance from ALT
  const getTokenBalance = async (userPublicKey) =&amp;gt; {
    const altInst = TokenBalanceProgram.getBalance({
      altAddress,
      userPublicKey,
    });
    await createAndSendV0Tx([altInst]);
  };

  // Example Usage:
  const sampleUserPublicKey = Keypair.generate().publicKey;
  await updateTokenBalance(sampleUserPublicKey, 100);
  await getTokenBalance(sampleUserPublicKey);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;User Profiles&lt;/strong&gt;: Store user profiles, settings, or preferences with associated user addresses in an ALT.
Enable quick retrieval and update of user-specific data within smart contracts.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Function to store and retrieve user profiles using Address Lookup Tables (ALTs)
async function manageUserProfiles() {
  // Assume ALT is used to store user profiles
  const altAddress = getAltAddress(userPublicKey); // Function to get ALT address

  // Update user profile in ALT
  const updateUserProfile = async (userPublicKey, profileData) =&amp;gt; {
    const altInst = UserProfileProgram.updateProfile({
      altAddress,
      userPublicKey,
      profileData,
    });
    await createAndSendV0Tx([altInst]);
  };

  // Retrieve user profile from ALT
  const getUserProfile = async (userPublicKey) =&amp;gt; {
    const altInst = UserProfileProgram.getProfile({
      altAddress,
      userPublicKey,
    });
    await createAndSendV0Tx([altInst]);
  };

  // Example Usage:
  const sampleUserProfile = { name: "John Doe", email: "john@example.com" };
  await updateUserProfile(sampleUserPublicKey, sampleUserProfile);
  await getUserProfile(sampleUserPublicKey);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;In this guide, we delved into the powerful realm of Address Lookup Tables (ALTs) within the Solana blockchain ecosystem. ALTs, also known as lookup tables, provide developers with a robust tool for organizing and managing on-chain data efficiently. Leveraging the @solana/web3.js library, we explored practical use cases for ALTs, such as optimizing token balances, user profiles.&lt;/p&gt;

&lt;p&gt;ALTs offer a scalable solution to address various challenges in decentralized application development, enhancing speed, cost-effectiveness, and overall efficiency. By organizing data in a structured manner, ALTs empower developers to streamline operations, leading to improved performance and user experiences.&lt;/p&gt;

&lt;p&gt;Through step-by-step examples and code snippets, we demonstrated how to create ALTs, add and retrieve data, and integrate them into transactions. The comparison of transaction sizes highlighted the tangible benefits of using ALTs, showcasing their impact on reducing on-chain transaction costs.&lt;/p&gt;

&lt;p&gt;As you embark on your Solana development journey, integrating ALTs into your projects can contribute to a more scalable and organized on-chain data management strategy. Experiment with the provided examples, adapt them to your specific use cases, and witness firsthand the transformative potential of Address Lookup Tables in Solana development.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project Repository&lt;/strong&gt;:&lt;br&gt;
Explore the full codebase and experiment with ALTs in Solana by visiting the GitHub repository: &lt;a href="https://github.com/Sig-giovanni/solana-address-lookup-tables" rel="noopener noreferrer"&gt;Solana-ALT-Tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Addition Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.solana.com/de/developing/lookup-tables" rel="noopener noreferrer"&gt;https://docs.solana.com/de/developing/lookup-tables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.quicknode.com/guides/solana-development/accounts-and-data/how-to-use-lookup-tables-on-solana" rel="noopener noreferrer"&gt;https://www.quicknode.com/guides/solana-development/accounts-and-data/how-to-use-lookup-tables-on-solana&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://solanacookbook.com/guides/versioned-transactions.html" rel="noopener noreferrer"&gt;https://solanacookbook.com/guides/versioned-transactions.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>blockchain</category>
      <category>developer</category>
    </item>
  </channel>
</rss>
