npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tronweb3/tronwallet-adapter-vue-hooks

v1.0.2

Published

A `useWallet()` hook to make it easy to interact with Tron wallets.

Downloads

409

Readme

@tronweb3/tronwallet-adapter-vue-hooks

@tronweb3/tronwallet-adapter-vue-hooks provides a useWallet() hook which will make it easy to "Connect Wallet" and listen to the state change for developers.

Installation

npm install @tronweb3/tronwallet-adapter-vue-hooks @tronweb3/tronwallet-abstract-adapter @tronweb3/tronwallet-adapters
# or pnpm install @tronweb3/tronwallet-adapter-vue-hooks @tronweb3/tronwallet-abstract-adapter @tronweb3/tronwallet-adapters
# or yarn install @tronweb3/tronwallet-adapter-vue-hooks @tronweb3/tronwallet-abstract-adapter @tronweb3/tronwallet-adapters

Usage

@tronweb3/tronwallet-adapter-vue-hooks uses Provide / Inject in Vue to maintain a shared data. So developers need to wrap App content within the WalletProvider.

You can provide a onError callback to handle various errors such as WalletConnectionError, WalletNotFoundError.

Here is a Demo project;

<script setup>
    import { defineComponent, h } from 'vue';
    import { WalletProvider, useWallet } from '@tronweb3/tronwallet-adapter-vue-hooks';
    import { TronLinkAdapter } from '@tronweb3/tronwallet-adapters';
    const tronLink = new TronLinkAdapter();

    const adapters = [tronLink];

    function onConnect(address) {
        console.log('[wallet hooks] onConnect: ', address);
    }
    function onDisconnect() {
        console.log('[wallet hooks] onDisconnect');
    }

    const VueComponent = defineComponent({
        setup() {
            // Here you can use `useWallet` API
            const { wallet, connect, signMessage, signTransaction } = useWallet();
            return () =>
                h('div', [
                    h('div', { style: 'color: #222;' }, `Current Adapter: ${(wallet && wallet.adapter.name) || ''}`),
                ]);
        },
    });
</script>

<template>
    <WalletProvider :adapters="adapters" @connect="onConnect" @disconnect="onDisconnect">
        <VueComponent />
    </WalletProvider>
</template>

WalletProvider

WalletProvider and useWallet work together. WalletProvider use provide() in Vue to provide a shared state. useWallet use inject() to get the shared state. Developers need to wrap application components with WalletProvider.

<html>
    <WalletProvider>/* here is application components */</WalletProvider>
</html>
<script setup>
    import { useWallet, WalletProvider } from '@tronweb3/tronwallet-adapter-vue-hooks';
</script>

Props

adapters:

  • Required: false
  • Type: Adapter[]
  • Default: [ new TronLinkAdapter() ]

Used to specify what wallet adapters are supported. All wallet adapters can be imported from @tronweb3/tronwallet-adapters package or their standalone package.

  • Example
    <template>
        <WalletProvider :adapters="adapters">/* here is application components */</WalletProvider>
    </template>
    <script setup>
        import { useWallet, WalletProvider } from '@tronweb3/tronwallet-adapter-vue-hooks';
        import { TronLinkAdapter } from '@tronweb3/tronwallet-adapters';
        const adapters = [new TronLinkAdapter()];
    </script>

autoConnect

  • Required: false
  • Type: boolean
  • Default: true

Whether connect to the specified wallet automatically after a wallet is selected.

disableAutoConnectOnLoad

  • Required: false
  • Type: boolean
  • Default: false

Whether automatically connect to current selected wallet after the page is loaded when autoConnect enabled. If you don't want to connect the wallet when page is first loaded, set disableAutoConnectOnLoad: true.

localStorageKey

  • Required: false
  • Type: string
  • Default: tronAdapterName

Specified the key used to cache wallet name in localStorage. When user select a wallet, applications will cache the wallet name to localStorage.

Events

You can provide event handlers for listen adapter events, such as connect,disconnect,accountsChanged. Available events and their types are as follows:

  • readyStateChanged: (readyState: 'Found' | 'NotFound') => void: Emits when current adapter emits readyStateChanged event.
  • connect: (address: string) => void: Emits when current adapter emits connect event.
  • disconnect: () => void: Emits when current adapter emits disconnect event.
  • accountsChanged: (newAddress: string; preAddress?: string) => void: Emits when current adapter emits accountsChanged event.
  • chainChanged: (chainData: unknow) => void: Emits when current adapter emits chainChanged event.
  • error: (error) => void: Emits when occurs error in methods calls.

An event named adapterChanged is also avaliable to get noticed when selected adapter is changed.

  • adapterChanged: (adapter: Adapter | undefined) => void: Called when current adapter is changed.

Here is an example:

```html
<template>
    <WalletProvider :adapters="adapters" @accountsChanged="onAccountsChanged">/* here is application components */</WalletProvider>
</template>
<script setup>
    import { useWallet, WalletProvider } from '@tronweb3/tronwallet-adapter-vue-hooks';
    import { TronLinkAdapter } from '@tronweb3/tronwallet-adapters';
    const adapters = [new TronLinkAdapter()];

    function onAccountsChanged(curAddress, preAddress) {}
</script>
```

useWallet()

useWallet is a Vue hook providing a set of properties and methods which can be used to select and connect wallet, get wallet state and so on.

useWallet() must be used in the descendant components of WalletProvider!

ReturnedValue

autoConnect

  • Type: ComputedRef<boolean> Synchronous with autoConnect property passed to WalletProvider.

disableAutoConnectOnLoad

  • Type: ComputedRef<boolean> Synchronous with disableAutoConnectOnLoad property passed to WalletProvider.

wallet

  • Type: ComputedRef<Wallet | null> The wallet current selected. If no wallet is selected, the value is null.

Wallet is defined as follow:

interface Wallet {
    adapter: Adapter; // wallet adapter
    state: AdapterState;
}
enum AdapterState {
    NotFound = 'NotFound',
    Disconnect = 'Disconnected',
    Connected = 'Connected',
}

address

  • Type: ComputedRef<string | null> Address of current selected wallet. If no wallet is selected, the value is null.

wallets

  • Type: Ref<Wallet[]> Wallet list based on current used adapters when initial WalletProvider.

connecting

  • Type: Ref<boolean> Indicate if is connecting to the wallet.

connected

  • Type: Ref<boolean> Indicate if is connected with the wallet.

disconnecting

  • Type: Ref<boolean> Indicate if is connecting to the wallet.

Methods

select

  • Type: (walletAdapterName: AdapterName) => void Select a wallet by walletAdapterName. Valid adapters can be found here

connect

  • Type: () => Promise<void> Connect to current selected wallet.

disconnect

  • Type: () => Promise<void> Disconnect from current selected wallet.

signTransaction

  • Type: (transaction: Transaction) => Promise<SignedTransaction> Sign a unsigned transaction. This method is the same as TronWeb API.

signMessage

  • Type: (message: string) => Promise<string> Sign a message.

Example

<template>
    <div>
        <button type="button" @click="() => select('TronLink Adapter')">Select TronLink</button>
        <button type="button" :disabled="connected" @click="connect">Connect</button>
        <button type="button" :disabled="!connected" @click="disconnect">Disconnect</button>
    </div>
</template>
<script setup>
    import { useWallet } from '@tronweb3/tronwallet-adapter-vue-hooks';
    import { AdapterName } from '@tronweb3/tronwallet-abstract-adapter';

    const { connect, disconnect, select, connected } = useWallet();
</script>