Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #179 from hermeznetwork/support-wallets
Browse files Browse the repository at this point in the history
Makes providers more flexible to support other wallets
  • Loading branch information
AlbertoElias authored Jun 11, 2021
2 parents f48a694 + a161ca2 commit cec0316
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/providers.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,49 @@
import { ethers } from 'ethers'

const PROVIDER_TYPES = {
WEB3: 'web3'
}

let provider

/**
* Set a Provider URL
* @param {String} url - Network url (i.e, http://localhost:8545)
* @param {String|Object} providerData - Network url (i.e, http://localhost:8545) or an Object with the information to set the provider
* @param {String} providerType - A value from the enum PROVIDER_TYPES
*/
function setProvider (url) {
if (url || typeof window === 'undefined') {
provider = ethers.getDefaultProvider(url)
} else {
provider = new ethers.providers.Web3Provider(window.ethereum)
function setProvider (providerData, providerType) {
if (typeof providerData === 'string' || typeof window === 'undefined') {
provider = ethers.getDefaultProvider(providerData)
return
}

switch (providerType) {
case PROVIDER_TYPES.WEB3: {
provider = new ethers.providers.Web3Provider(providerData)
break
}
default: {
provider = new ethers.providers.Web3Provider(window.ethereum)
}
}
}

/**
* Retrieve provider
* @param {String} url - Network url (i.e, http://localhost:8545)
* @param {String|Object} providerData - Network url (i.e, http://localhost:8545) or an Object with the information to set the provider
* @param {String} providerType - A value from the enum PROVIDER_TYPES
* @returns {Object} provider
*/
function getProvider (url) {
function getProvider (providerData, providerType) {
if (!provider) {
setProvider(url)
setProvider(providerData, providerType)
}

return provider
}

export {
PROVIDER_TYPES,
setProvider,
getProvider
}

0 comments on commit cec0316

Please sign in to comment.