GithubHelp home page GithubHelp logo

dorucioclea / wen Goto Github PK

View Code? Open in Web Editor NEW

This project forked from guibibeau/wen

0.0 2.0 0.0 70.47 MB

Wen? Now! A library to simplify your Web3 data fetching.

Home Page: https://www.npmjs.com/package/wen-connect

JavaScript 2.17% TypeScript 97.83%

wen's Introduction

Wen Connect? Now!

Version Build Size Downloads

Wen is under active development, thigns can change quickly

Minimalistic library for Web3 user interfaces.

  • Seamless connection flows to Metamask.
  • Stateless sessions that work anywhere (client, serverless, Edge Functions).
  • Respectful of user privacy. Take only the needed information, nothing more. Never saved to a DB.

Wen is about getting you to build Web3 instead of figuring out the patch work of software needed.

Usage


yarn add wen-connect
# or
npm install wen-connect --save

Wen works in all JavaScript applications, but offers different bindings for different frameworks.

React


One single context: WenProvider, and one hook: useWen for everything.

Add WenProvider to your root component:

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { WenProvider } from "wen-connect";

ReactDOM.render(
  <React.StrictMode>
    <WenProvider>
      <App />
    </WenProvider>
  </React.StrictMode>,
  document.getElementById("root")
);

Use the hook to engage with your user:

import { useWen } from "wen-connect";

const MyComponent = () => {
  const { connect, disconnect, wallet } = useWen();

  const handleConnect = () => {
    // Optional argument to specify which chain to get the user connected on.
    connect({ chainId: "0xa86a" });
  };

  const handleDisconnect = () => {
    disconnect();
  };

  return <span>{wallet.address}</span>;
};

wallet contains the following info:

address: string
balance: Hex string
chainId: Hex string
connected: boolean
connector: "injected"

Next.js


Wen also works server-side with Next.js to let you connect. Activate the ssr prop in the provider and add your provider to _app.tsx:

import { WenProvider } from "wen-connect";

function MyApp({ Component, pageProps }) {
  const config = { ssr: true };
  return (
    <WenProvider config={config}>
      <Component {...pageProps} />
    </WenProvider>
  );
}

Create a pages/api/wen.ts for Wen to use JWT sessions:

export { WenConnect as default } from "wen-connect";

Add a WEN_SECRET environment variable in your .env file. It will be used to encrypt the JWT token.

Now, you can get the session in getServerSideprops and use it to hydrate Wen on the client-side first render.

import { getSession, useWen } from "wen-connect";

function Index(props) {
  // Passing the session will avoid empty initial renders, saving your users loading time.
  const { wallet } = useWen(props.session);

  return <div>{wallet.address}</div>;
}

export default Index;

export const getServerSideProps = async (context) => {
  return {
    props: {
      // session.wallet is the same info as const { wallet } = useWen(props session);
      session: getSession(context),
    },
  };
};

Roadmap:

  • Signing in desired user state.
  • Svelte and SvelteKit bindings.
  • Wallet connect.
  • Coinbase wallet.
  • The graph integrations.
  • More examples.

wen's People

Contributors

guibibeau avatar

Watchers

James Cloos avatar  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.