Solib Documentation

website·Indexed 31 days ago

https://solib.dev/

A library for communicating with Solana wallets, featuring account and transaction actions, Solana Name Service integration, and state management. Includes documentation on connectors and installation via NPM.

Tokens
3.5K
Snippets
30
Records
32
Agent score
56%

What's inside Solib

  1. Overview of Web3Modal for Solana wallet integration

    Web3Modal is a library used to manage multi-chain wallet connection flows. When used with Solib, it provides a unified interface for connecting Solana mobile, desktop, and injected wallets, offering a customizable and streamlined connection experience for end users.
  2. Deprecation notice for Solib

    Effective February 13th, 2024, Solib has been deprecated. Users impacted by this change should open an issue on GitHub Discussions for assistance.
  3. Switch the active wallet connector

    Use switchConnector to change the active connector used for actions like connect, disconnect, and signMessage. This updates the connectorName originally configured in the init function.
    import { switchConnector, PhantomConnector } from '@walletconnect/solib'
    
    // All subsequent function calls will use PhantomConnector
    switchConnector(PhantomConnector.connectorName())
  4. Initialize Solib state and connectors

    The init function must be called only once to set the initial state and define the available wallet connectors. Connectors can only be defined during this initialization phase. The function takes a configuration callback and a PROJECT_ID.
    import { 
      init, PhantomConnector, WalletConnectConnector, 
      InjectedConnector, mainnetBetaWalletConnect
    } from '@walletconnect/solib'
    
    init(() => ({
        // Define available connector methodologies
        connectors: [
          new PhantomConnector(),
          new InjectedConnector('window.solib'),
          new InjectedConnector('window.solflare'),
          new WalletConnectConnector({
            relayerRegion: 'wss://relay.walletconnect.com',
            metadata: {
              description: 'Test app for solib',
              name: 'Test Solib dApp',
              icons: ['https://avatars.githubusercontent.com/u/37784886'],
              url: 'http://localhost:3000'
            },
            autoconnect: true,
            qrcode: true
          })
        ],
        // Set the default connector to be used (must be registered in connectors array)
        connectorName: WalletConnectConnector.connectorName,
        // Set the cluster and network for communication
        chosenCluster: mainnetBetaWalletConnect(PROJECT_ID)
      })
    }, PROJECT_ID)
  5. Establish a wallet connection and monitor address changes

    Use connect() to initiate the wallet connection process. Use watchAddress() to listen for changes to the connected public key and update the UI accordingly.
    import { connect, watchAddress } from '@walletconnect/solib'
    
    watchAddress(pubkey => {
      if (pubkey) {
        // update UI to reflect successful connection
      } else {
        // Failed connection.
      }
    })
    
    await connect()
  6. Manually sign and send transactions

    For more granular control, you can separate the signing and sending phases. signTransaction signs the transaction using the configured Connector without broadcasting it. sendTransaction then broadcasts the signed transaction to the cluster. Note: Using signAndSendTransaction is generally preferred to prevent failures caused by outdated blockhashes.
    import { signTransaction, sendTransaction } from '@walletconnect/solib'
    
    // Signs the transaction but does not send to cluster
    const transactionSignature = await signTransaction(transactionArgs);
    
    // Sends the signed transaction to the cluster
    const sentTransaction = await sendTransaction(transactionSignature);
  7. Connect a wallet using @walletconnect/solib

    The connect function establishes a connection using the connector defined during init or the one currently active via switchConnector.
    import { connect } from '@walletconnect/solib'
    
    await connect()
  8. Install Solib via npm

    Install the Solib library using npm to begin interacting with the Solana network.
    npm install --save @walletconnect/solib
  9. Sign and send a transaction in one step

    The signAndSendTransaction function is the recommended way to execute transactions. It handles the entire lifecycle: creating the transaction, retrieving the most recent blockhash, signing it via the configured Connector, and broadcasting it to the cluster.
    import { signAndSendTransaction } from '@walletconnect/solib'
    
    const sentTransaction = await signAndSendTransaction(transactionArgs);
  10. Install Solib via NPM or Yarn

    Install the Solib library using either the npm or yarn package manager.
    # Using NPM
    npm install --save @walletconnect/solib
    
    # Using Yarn
    yarn add @walletconnect/solib
  11. Switch the active Solana network cluster

    Use switchNetwork to change the cluster used for communication, which updates the chosenCluster state. Some network functions (like mainnetBetaWalletConnect) are function calls because they require the configured PROJECT_ID.
    import { switchNetwork, mainnetBetaWalletConnect, mainnetBetaProjectSerum } from '@walletconnect/solib'
    
    // Switch to WalletConnect mainnetBeta
    switchNetwork(mainnetBetaWalletConnect())
    
    // Switch to Project Serum mainnetBeta
    switchNetwork(mainnetBetaProjectSerum)
  12. Sign a message with the connected wallet

    The signMessage function allows a user to sign a string message. It automatically utilizes the appropriate Connector configured in init or selected via switchNetwork.
    import { signMessage } from '@walletconnect/solib'
    
    const message = 'Some message'
    const signature = await signMessage(message);