This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from hermeznetwork/support-wallets
Makes providers more flexible to support other wallets
- Loading branch information
Showing
1 changed file
with
25 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |