/web3-wallets

Primary LanguageJavaScriptMIT LicenseMIT

Quickstart

yarn add @depay/web3-wallets

or

npm install --save @depay/web3-wallets
import { getWallets } from '@depay/web3-wallets'

let wallets = getWallets()
let wallet = wallets[0]

wallet.name // MetaMask
wallet.connect()

Demo

https://depayfi.github.io/web3-wallets/demo.html

Support

This library supports the following blockchains:

This library supports the following wallets:

100+ different wallets via WalletConnect, such as:

Functionalities

getWallets

getWallets: Returns an array of available/connectable wallets.

let wallets = getWallets();
// [<Wallet name='MetaMask'>, <Wallet name='Phantom'>]
let wallets = getWallets();
// [] no wallets detected. (you can still try WalletConnect or WalletLink)
{ getWallets, wallets } from "@depay/web3-wallets"

let foundWallets = getWallets()

let wallet
if(foundWallets.length == 1) {
  wallet = foundWallets[0]
} else if(foundWallets.length > 1) {
  wallet = foundWallets[parseInt(prompt('Which wallet do you want to connect?'), 10)]
} else {
  // Let the user choose:
  // you can still try to connect via wallets.WalletConnect.connect()
  // or wallets.WalletLink.connect()
  wallet = wallets.WalletLink
}

Name

name:string: Returns the name of the wallet.

wallet.name // 'MetaMask'

Logo

logo:string: Returns the logo of the wallet as PNG base64-encoded.

wallet.logo // 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAclBMVEVHcEyBTif0snbypF3nhijulD7cq4Hv1b/wrW3dvqSieVvWyL2PXjvJuazndgD5hQB3PQ/PYQDkdADCrp3YwrPsegAVFRZyOg7ZawDzgQD/iQAeMklxNQMMDQ+3XwiLRw2hVAyCdmxPQz7dqoAyKiSgkoj9gMFYAAAADnRSTlMA8X2g78dnGzZPp57O1Hi2/psAAApFSURBVHja7Z2LVqM6FIZ7pUUrtbQRqEyLHX3/Vzy5soHcICSd41r5z3EcFUL+7m/vXKDOIioqKioqKioqKioqKioqKioqKioqKurXKnlJd4t/rB3uxGKuXur7Zr1f/EPt15t7/TK3ld2yKN5vaLv6R2HZrbbo9l4Uy7nXT+t33EyTZ5ttsls8Wbtku8nypihwH9LZZ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vq9LH4Rdq9abH6PeHgQVHrV4UjKioqKioqKioqKioqKioqKioqKioqKirq/6z/AMhLOEXbTKvCAAAAAElFTkSuQmCC'

Account

async account():string: Gets the currently connected and active account (without prompting a connect screen). Returns undefined if no account is connected.

await wallet.account() // '0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B'

Connect an account

async connect():array: Connects accounts. Potentially opens wallet connect screen. Provides connected accounts in async return. If wallet fails to connect, also returns an empty array [].

await wallet.connect() // '0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B'

Supported Blockchains

blockchains:Array: Array containing the names of supported blockchains

wallet.name // MetaMask
wallet.blockchains // ['ethereum', 'bsc', 'polygon']

Check if wallet is connected to a specific blockchain

async connectedTo(blockchain):Boolean: Checks if wallet is connected to a specific blockchain.

await wallet.connectedTo('ethereum') // true

If no param is given it well tell you to which blockchain the wallet is connected to:

await wallet.connectedTo() // 'bsc'

Receive wallet events

on(string, function):undefined: Register a callback function for given events.

wallet.on('account', (newAccount)=>{
  doSomething(newAccount)
})

Events

on('account', (newAccount)=>{}): Triggers when user changes the connected/active wallet account.

Deregister wallet events

.on returns a callback function that needs to be passed to .off if you want to deregister the event listener:

let callback = wallet.on('account', (newAccount)=>{
  doSomething(newAccount)
})

//...

wallet.off('account', callback) // removes listener

Switch blockchain/network

async switchTo(blockchain): Changes wallet connection to a specific network (adds it to the wallet in case it's missing)

await wallet.switchTo('bsc')

Transaction

EVM: Transaction

Instances of Transaction (e.g. via sendTransaction, or sent, succeeded or failed callback) have the following format for EVM blockchains:

blockchain: string: Blockchain the transaction belongs to.

id: string: Unique identifier of the transaction, also known as transaction hash/signature, only populated if transaction has been submitted to the network.

url: string: A url to display the transaction status in a browser on a blockchain explorer.

from: string: Address the transaction is sent from.

nonce: Number: The number of the sent transactions (from the given address).

to: string: Address the transaction is interacting with.

api: array: Api of a contract the transaction is interacting with.

method: string: The method name of the contract the transaction is interacting with.

params: object or array: Params the transaction is passing to the contract method.

value: string: Amount/value of the native token the transaction is forwarding as part of the interaction.

confirmation: Promise: Returns a promise that resolves once the transaction confirms.

failure: Promise: Returns a promise that resolves once the transaction fails.

sendTransaction

EVM: sendTransaction

Available arguments for EVM blockchains:

blockchain: String: Name of the blockchain e.g. 'ethereum'.

to String: Address of the contract to be transacted with.

api: Array: Api of the contract (e.g. abi for Ethereum).

method: String: Name of the contract method to be called.

params: Object or Array: Parameters passed to the method.

value: Number or BigNumber as String: Value of the transaction (amount of the native blockchain currency sent along with the transaction).

sent: Function (transaction)=>{}: Callback to be executed if transaction has been sent to the network.

succeeded: Function (transaction)=>{}: Callback to be executed if transaction was successful and has been confirmed once by the network.

failed: Function (transaction, error)=>{}: Callback to be executed if transaction failed (e.g. reverted).

EVM: Simple value transfer

e.g. sending 0.01 ETH on Ethereum:

let sentTransaction = await wallet.sendTransaction({
  blockchain: 'ethereum',
  to: '0xae60aC8e69414C2Dc362D0e6a03af643d1D85b92',
  value: 0.01,
  sent: function(transaction){},
  succeeded: function(transaction){},
  failed: function(transaction, error){}
})
EVM: Smart contract interaction
let sentTransaction = await wallet.sendTransaction({
  blockchain: 'ethereum',
  to: '0xae60aC8e69414C2Dc362D0e6a03af643d1D85b92',
  api: [{"inputs":[{"internalType":"address","name":"_configuration","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"ETH","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"configuration","outputs":[{"internalType":"contract DePayRouterV1Configuration","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"pluginAddress","type":"address"}],"name":"isApproved","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint256[]","name":"amounts","type":"uint256[]"},{"internalType":"address[]","name":"addresses","type":"address[]"},{"internalType":"address[]","name":"plugins","type":"address[]"},{"internalType":"string[]","name":"data","type":"string[]"}],"name":"route","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"withdraw","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}],
  method: 'route',
  params: {
    path: ["0xb056c38f6b7Dc4064367403E26424CD2c60655e1","0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb"],
    amounts: ["11275067000000000000000","100000000000000000000", "1632063302"],
    addresses: ["0x39794c3171d4D82eB9C6FBb764749Eb7ED92881d", "0x39794c3171d4D82eB9C6FBb764749Eb7ED92881d"],
    plugins: ["0xe04b08Dfc6CaA0F4Ec523a3Ae283Ece7efE00019", "0x99F3F4685a7178F26EB4F4Ca8B75a1724F1577B9"],
    data: []
  },
  value: "0",
  sent: function(transaction){},
  succeeded: function(transaction){},
  failed: function(transaction, error){}
})

Solana: sendTransaction

Available arguments for Solana blockchains:

blockchain: String: Name of the blockchain e.g. 'solana'.

sent: Function (transaction)=>{}: Callback to be executed if transaction has been sent to the network.

succeeded: Function (transaction)=>{}: Callback to be executed if transaction was successful and has been confirmed once by the network.

failed: Function (transaction, error)=>{}: Callback to be executed if transaction failed (e.g. reverted).

Solana: Simple value transfer

e.g. send 0.01 SOL on Solana:

to String: Address of the receiver.

value: Number or BigNumber as String: Value of the transaction (only needed for simple SOL transfers).

let sentTransaction = await wallet.sendTransaction({
  blockchain: 'solana',
  to: '2UgCJaHU5y8NC4uWQcZYeV9a5RyYLF7iKYCybCsdFFD1',
  value: 0.01,
  sent: function(transaction){},
  succeeded: function(transaction){},
  failed: function(transaction, error){}
})
Solana: Sign and send instructions

instructions Array: A set of TransactionInstructions

e.g. Send 1 USDC:

import { Token } from '@depay/web3-tokens'

let sentTransaction = await wallet.sendTransaction({
  blockchain: 'solana',
  instructions: Token.solana.createTransferInstructions({
    token: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 
    amount: '1000000',
    from: await wallet.account(),
    to: '5AcFMJZkXo14r3Hj99iYd1HScPiM4hAcLZf552DfZkxas'
  }),
  sent: function(transaction){},
  succeeded: function(transaction){},
  failed: function(transaction, error){}
})

value

If value is passed as a number it's gonna be converted into a big number applying the individual blockhain's default decimals:

let transaction = new Transaction({
  ...,
  value: 1
})

transaction.value // '1000000000000000000'

If value is passed as a string or as a BigNumber, value is used just as provided:

let transaction = new Transaction({
  ...,
  value: '1000000000000000000'
})

transaction.value // '1000000000000000000'

wrong network

sendTransaction rejects with:

{ code: 'WRONG_NETWORK' }

in case wallet is connected to the wrong network and network cant be switched automatically.

Sign messages

let signature = await wallet.sign("This is a message to be signed")

Logos

Conversion

Use https://codebeautify.org

Development

Get started

yarn install
yarn dev

Release

npm publish