Git Product home page Git Product logo

connectors's Introduction

Ronin Connectors

Wagmi v2 Adapter

Installation:

pnpm add @roninbuilders/wagmi-adapter
  • Wagmi package and its peers:
pnpm add wagmi viem @tanstack/react-query

Configuration:

To start with Wagmi we'll replace createConfig function with createRoninConfig. We can pass all the wagmi configuration options except for the connectors, additionally we need to add two WalletConnect options: projectId and metadata.

You can get a project ID from WalletConnect Cloud

import { createRoninConfig } from "@roninbuilders/wagmi-adapter";

import { http } from "viem";
import { ronin, saigon } from "viem/chains";
import { cookieStorage, createStorage } from "wagmi";
import type { WalletConnectParameters } from "wagmi/connectors";

const projectId = process.env.NEXT_PUBLIC_PROJECT_ID
if(!projectId) throw Error("Project ID is undefined - get a project ID from WalletConnect Cloud")

const metadata: WalletConnectParameters['metadata'] = {
  name: 'My Website',
  url: 'https://mywebsite.com',
}

export const config = createRoninConfig({
  projectId,
  metadata,
  chains: [ronin, saigon],
  transports: {
    [ronin.id]: http(),
    [saigon.id]: http(),
  },
})

useRoninConnect

The Wagmi adapter wraps the useConnect hook from Wagmi, you will need to use it to set up the configuration to connect with the Ronin extension wallet and the mobile app wallet:

Note

useRoninConnect is exported from @roninbuilders/wagmi-adapter/hooks

The useRoninConnect hook is an extension of the useConnect hook from Wagmi, so it will bypass most of the returns while overriding others

Extension Browser wallet:

use the connectBrowser to request a connection to the extension wallet, you can also use isBrowser to know if the user has the extension wallet installed or not

'use client'

import { useRoninConnect } from '@roninbuilders/wagmi-adapter/hooks'

function ConnectBrowser() {
  const { connectBrowser, error, isError } = useRoninConnect()

  return (
    <>
      <button onClick={connectBrowser} >Connect Extension Wallet</button>
      {isError && error?.message}
    </>
  )
}

export default ConnectBrowser

Mobile Wallet:

There are two ways to use the mobile wallet connector (WalletConnect v2 protocol), depending whether the user is on a Desktop or mobile environment.

  • For Desktop:
  1. call prepareMobile function to create a connection with the WalletConnect network, this will generate an URI
  2. Once the URI is generated we can create a QR code with it

Here I'm using react-qr-code to generate the QR code from the URI, but you can use any other library.

'use client'

import { useRoninConnect } from "@roninbuilders/wagmi-adapter/hooks"
import QRCode from "react-qr-code"

function ConnectMobile() {
  const { prepareMobile, uri, status } = useRoninConnect()

  return (
    <>
    { /*  1. call prepareMobile function to create a connection with the WalletConnect network, this will generate an URI  */ }
    <button onClick={prepareMobile}>generate URI</button>

    { /*  2. Once the URI is generated we can create a QR code with it  */ }
    { uri ? <QRCode value={uri} bgColor='#141414' fgColor='#ffffff' /> : ( status === 'pending' ? 'Loading...' : 'generate an URI by pressing the "generate URI" button' )}
    </>
  )
}

export default ConnectMobile
  • For Mobile:

You can use the isMobile boolean to know if the user is on mobile or desktop.

  1. call prepareMobile function to create a connection with the WalletConnect network, this will generate an URI
  2. Once the URI is generated we open the Ronin wallet with its deeplink + the uri attached - all this is handled internally by the openMobile function
'use client'

import { useRoninConnect } from "@roninbuilders/wagmi-adapter/hooks"
import QRCode from "react-qr-code"

function ConnectMobile() {
  const { openMobile, isMobileReady, isMobile, prepareMobile, uri, status } = useRoninConnect()

  //Mobile usage
  if(isMobile){
    return (
      <>
        { /* 1. call prepareMobile function to create a connection with the WalletConnect network, this will generate an URI */ }
        <button onClick={prepareMobile} >{ isMobileReady ? "Try again" : "prepare Mobile URI"}</button>

        { /* 2. Once the URI is generated we open the Ronin wallet with its deeplink + the uri attached - all this is handled internally by the openMobile function  */ }
        {isMobileReady && <button onClick={openMobile} >Connect Mobile Wallet</button>}
      </>
    )
  }

  //Desktop usage
  return (
    //...
  )
}

export default ConnectMobile

connectors's People

Contributors

glitch-txs avatar

Watchers

 avatar Alex avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.