Caution
Effective as of February 13th, 2024, Solib has been deprecated.
If your project is impacted by this change, please open an issue on Github Discussions to address any concerns or seek assistance.
Solana friendly API
Checkout the complete docs!
- Connect Wallet:
- Phantom
- WalletConnect
- Get balance
- Sign Transaction
- Send Transaction
- Sign and send Transaction
- Sign Message
- Watch Transactions
The init function needs to be called to prepare solib
to be able to call all
the functions in its API.
import { init } from 'solib'
init(
{
// The different connector methodologies that will be used.
// PhantomConnector will interact with injected Phantom Wallet using browser
// extension, while WalletConnectConnector can be used to interact with all
// wallets that support the WalletConnect protocol.
connectors: [
new PhantomConnector(),
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
})
],
// Name of the connector to be used.
// The connector needs to be registered in the connectors field above.
// This can be switched later using `switchConnector` function.
connectorName: WalletConnectConnector.connectorName,
// The name of the cluster and network to use.
// Here, `mainnetBeta` refers to the mainnetBeta Solana network, while
// `WalletConnect` is the RPC server thhat will be used to do the communication
chosenCluster: mainnetBetaWalletConnect()
},
WALLETCONNECT_PROJECT_ID
)
The connect function can be used to connect a wallet to a dApp. The wallet
chosen needs to be configured in the init
function above.
import { connect } from 'solib'
const address = await connect()
Instead of retrieving the address once on the connect function, one can globally
watch address changes using the watchAddress
API.
import { watchAddress, connect } from 'solib'
watchAddress(address => {
console.log({ address })
})
connect()
import { getBalance } from 'solib'
const connectedWalletBalance: number = await getBalance()
import { signMessage } from 'solib'
const signature = await signMessage('Test')
import { signAndSendTransaction } from 'solib'
const transactionHash = signAndSendTransaction('transfer', {
to,
amountInLamports,
feePayer: 'from'
})
import { signAndSendTransaction, watchTransaction } from 'solib'
const transactionHash = signAndSendTransaction('transfer', {
to,
amountInLamports,
feePayer: 'from'
})
watchTransaction(transactionHash, update => console.log({ update }))
import { switchNetwork, mainnetBetaProjectSerum } from 'solib'
switchNetwork(mainnetBetaProjectSerum)
import { switchConnector, PhantomConnector, connect } from 'solib'
switchConnector(PhantomConnector.connectorName)
const phantonWalletAddress = await connect()
- Generic transaction construction
- Using cluster
sendTransaction
to avoid depending on aa wallet's implementation, only having to use theirsignMessage
function - Internal store maintaining state
- Base connector to help with making future connectors (Eg: WalletConnect connector)
- Generic typing
- From scratch cluster websocket factory so we can listen to events and attach custom listeners in the future, in a generic manner.
- Unsub functionality
For now when developing, feel free to use the example/dev.sh
to help with
refreshing the cache and installing a fresh local solib
package to test your
changes. TODO: Will look into making this better.
Example app written in react, for testing
Actual source code.
- actions: This where most of the developer public will live. Actions are what developers will use to fetch and manipulate data on the solana blockchain
- connectors: This is where connectors will live. Connectors are basically
adapters using wallet providers (Eg: Phantom).
base.ts
is a base class that has functionality for building connectors, as well as non-wallet-specific actions (eg: fetching wallet balance from the cluster) - defaults: This is where default things will live, like the clusters we have configured
- store: A rudimentary store used for storing address, chosen cluster, etc
- types: Self explanatory. Not all types need to live here, however.
- utils: Self explanatory.