0.2.2 • Published 1 year ago

@walletconnect/solib v0.2.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

Solib

Solana friendly API

Checkout the complete docs!

Current Capabilities

API

  • Connect Wallet:
    • Phantom
    • WalletConnect
  • Get balance
  • Sign Transaction
  • Send Transaction
  • Sign and send Transaction
  • Sign Message
  • Watch Transactions

Init

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
)

Connect Wallet

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()

Watch Address

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()

Get Balance

import { getBalance } from 'solib'

const connectedWalletBalance: number = await getBalance()

Sign Message

import { signMessage } from 'solib'

const signature = await signMessage('Test')

Sign and Send Transaction

import { signAndSendTransaction } from 'solib'

const transactionHash = signAndSendTransaction('transfer', {
  to,
  amountInLamports,
  feePayer: 'from'
})

Watch Transaction

import { signAndSendTransaction, watchTransaction } from 'solib'

const transactionHash = signAndSendTransaction('transfer', {
  to,
  amountInLamports,
  feePayer: 'from'
})

watchTransaction(transactionHash, update => console.log({ update }))

Switch network

import { switchNetwork, mainnetBetaProjectSerum } from 'solib'

switchNetwork(mainnetBetaProjectSerum)

Switch Connector

import { switchConnector, PhantomConnector, connect } from 'solib'

switchConnector(PhantomConnector.connectorName)

const phantonWalletAddress = await connect()

Internals

  • Generic transaction construction
  • Using cluster sendTransaction to avoid depending on aa wallet's implementation, only having to use their signMessage 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

Development

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.

Folders

Example

Example app written in react, for testing

Src

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.

Resources:

0.2.3-canary.26

1 year ago

0.2.3-canary.25

1 year ago

0.2.3-canary.24

1 year ago

0.2.3-canary.23

1 year ago

0.2.3-canary.22

1 year ago

0.2.3-canary.21

1 year ago

0.2.3-canary.20

1 year ago

0.2.3-canary.19

1 year ago

0.2.3-canary.18

1 year ago

0.2.3-canary.17

1 year ago

0.2.3-canary.16

1 year ago

0.2.3-canary.15

1 year ago

0.2.3-canary.14

1 year ago

0.2.3-canary.13

1 year ago

0.2.3-canary.11

1 year ago

0.2.3-canary.10

1 year ago

0.2.3-canary.9

1 year ago

0.2.3-canary.7

1 year ago

0.2.3-canary.6

1 year ago

0.2.3-canary.5

1 year ago

0.2.3-canary.4

1 year ago

0.2.3-canary.3

1 year ago

0.2.3-canary.2

1 year ago

0.2.3-canary.1

1 year ago

0.2.3-canary.0

1 year ago

0.2.2

1 year ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.14

2 years ago

0.1.13

2 years ago

0.1.12

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago