Giter VIP home page Giter VIP logo

web3modal-vanilla-js-example's Introduction

This repositor has been archived. Please visit web3modal.com for latest examples.


This is a vanilla JavaScript example how to use Web3modal with different wallet providers.

The purpose of this example is how to connect a cryptocurrency wallet to a website that is build on traditional HTML technologies and does not use any modern JavaScript framework. Uses cases would include: static HTML sites, WordPress, content management systems, tutorials and education.

The example works with

  • In-browser wallets (MetaMask, Opera, Brave),

  • Mobile wallets through QR code scanning (WalletConnect)

  • Account-based wallets (Fortmatic, others)

The code uses simple unprocessed in-browser JavaScript. Dependencies, like the Web3Modal library itself, are loaded over Unpkg CDN. The code is extensively commented and short.

screenshot2

screenshot

What should I do with this

Web3modal) is a vendor-neutral project offering an easy integration of cryptocurrency and decentralised finance for websites. For example, you can use this to start accepting cryptocurrency payments on your site or to start building a decentralised finance application.

Support

Go to Web3modal main Github repository.

Reach to us in Web3modal Discord or open a Github issue.

Web3 wallets and HTTPS hosting limitations

Because of limitations how wallet operate within a web browser and web security, you should not run this example, or any Web3modal code, out of your file system or insecure HTTP protocol (even using localhost).

The APIs of different wallet providers may fail in funny and obscure way. MetaMask does not even show up if you load it over file:// protocol.

The easiest way to get local HTTPS development is to use https-localhost.

Assuming you have a working Node environment set up on a UNIX system you can do:

npm i -g --only=prod https-localhost
sudo serve .

... in the folder of index.html file.

Then you can visit https://localhost to open the example.

Publishing example on Github

To republish the example:

git push origin master:gh-pages

This will push index.html to https://web3modal.github.io/web3modal-vanilla-js-example/

web3modal-vanilla-js-example's People

Contributors

chadyj avatar crisgarner avatar miohtama avatar octavioamu avatar pedrouid avatar polats avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

web3modal-vanilla-js-example's Issues

Fortmatic

There is a bug in the vanilla JS example using fortmatic (example.js line 158) provider.on("accountsChanged", (accounts) => {
the fortmatic key I'm using is valid, I've tested also with latest formatic ver.
fortmatic
Thanks

EVM link in index.html doesn't work properly

Causes example to fail if git is cloned. Resolved by replacing:

<script type="text/javascript" src="https://unpkg.com/evm-chains/lib/index.js"></script>

with

<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/umd/index.min.js"></script>

Can't pop up qr code

Hello,

There is an error when trying to pop the qr-code :

Uncaught (in promise) Error: Please pass numbers as strings or BN objects to avoid precision errors.
    fromWei index.js:248
    rowResolvers example.js:112

XHRPOSThttps://mainnet.infura.io/v3/8043bb2cf99347b1bfadfb233c5325c0 [HTTP/2 403 Forbidden 101ms]
(repeat every seconds)

Error at example.js :


var fromWei = function(number, unit) {
    unit = getUnitValue(unit);

    if(!utils.isBN(number) && !_.isString(number)) {
        **throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.');** <---HERE
    }

    return utils.isBN(number) ? ethjsUnit.fromWei(number, unit) : ethjsUnit.fromWei(number, unit).toString(10);
};

The demo dapp does not connect

Trying your online demo with WalletConnect option, my mobile wallet connect but your dapp not show the second screen...

Infura console [[HTTP/2 403 Forbidden 105ms]]


Uncaught (in promise) Error: Please pass numbers as strings or BN objects to avoid precision errors.
    fromWei index.js:248
    rowResolvers example.js:112
    fetchAccountData example.js:108
    refreshAccountData example.js:152
    onConnect example.js:185
    async* example.js:220
    EventListener.handleEvent* example.js:218

ERROR POST https://mainnet.infura.io/v3/ 401 when using BSC

hello,
I have the same problem to configure BSC network as described on github using vanilla JavaScript:
WalletConnect/web3modal#295

My js code:

const providerOptions = {
  walletconnect: {
    package: WalletConnectProvider,
    options: {
      rpc: {
        56: 'https://bsc-dataseed1.binance.org/'
      },
      chainId: 56
    }
  }
}

  web3Modal = new Web3Modal({
    cacheProvider: false, // optional
    providerOptions, // required
    disableInjectedProvider: false, // optional. For MetaMask / Brave / Opera.
  });

ERROR
POST https://mainnet.infura.io/v3/ 401
POST https://mainnet.infura.io/v3/ 401

scripts declaration in html:

<script type="text/javascript" src="https://unpkg.com/web3@latest/dist/web3.min.js%22%3E</script>
<script type="text/javascript" src="https://unpkg.com/web3modal%22%3E</script>
<script type="text/javascript" src="https://unpkg.com/evm-chains%22%3E</script>
<script type="text/javascript" src="https://unpkg.com/@walletconnect/web3-provider%22%3E</script>
<script type="text/javascript" src="./example.js"></script>

Vue/Nuxt doesn't work

When I duplicate the example into a fresh vue/nuxt app, nothing happen (popup doesn't show) I cannot connect to my walllet within Metamask browser or Trust wallet.

The demo works correctly (mobile and desktop), and same when I use the whole code as a simple html/js page on my backend server (mobile and desktop) but when I use it within my nuxt app it only works on desktop with metamask extension.

Repo: https://github.com/juloxrox/test
Example here: https://test-iota-wheat.vercel.app/

Does someone has an idea to make it work?
Thanks in advance

RPC Error: Already processing eth_requestAccounts. Please wait.

Error: inpage.js:1 MetaMask - RPC Error: Already processing eth_requestAccounts. Please wait. {code: -32002, message: 'Already processing eth_requestAccounts. Please wait.'}

Metasask not popup Incase Metamask is not logged in with password in browser extension.

Needs license

Recommend adding MIT or other license to facilitate open source use.

hello

is there anyone here? no one is answering to the previous issues

getChain() fails with Metamask/Ganache

I'm using the official live demo here https://web3modal.github.io/web3modal-vanilla-js-example/
Metamask connected.
If the chain is changed to http://127.0.0.1:8545 (Ganache), I got the following error:

Uncaught (in promise) Error: No chain found matching chainId: 1337
    at Object.i [as getChain] (index.min.js:1)
    at fetchAccountData (example.js:88)
    at async refreshAccountData (example.js:152)
    at async HTMLButtonElement.onConnect (example.js:185)

Everything is file when using Mainnet or Ropsten.

Error connecting to Binance

I am having issues with walletconnect and binance. I get the following error about the chainId and I have my js file set as follows. What am I doing wrong?
image
image

The example is now broken on both my website and the web3modal's example url

Try to go to https://web3modal.github.io/web3modal-vanilla-js-example/ and login from PC using metamask. It won't let you, giving the following errors:

Unchecked runtime.lastError: The message port closed before a response was received.
Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

However, if you then refresh the page and try again, everything works perfectly. Is there any fix to this?

This error seems to occur if you close your browser and try to login. if you then refresh the page weirdly enough it works.

How to add Arkane and Frame?

@pedrouid

Have tried:

const ethProvider = window.frame;

and

frame: {
package: ethProvider // required
},

at example.js, and in index.html:

<script type="text/javascript" src="https://unpkg.com/[email protected]/browser.js"></script>

I think it may need better documentation about vanilla JavaScript and unpkg.

Cannot read property 'getChain' of undefined

Uncaught (in promise) TypeError: Cannot read property 'getChain' of undefined
    at fetchAccountData (example.js:88)
    at async refreshAccountData (example.js:152)
    at async HTMLButtonElement.onConnect (example.js:185)
fetchAccountData @ example.js:88
async function (async)
onConnect @ example.js:164

Modal cause overflow hidden

Hi.
how to prevent modal from disabling dom overflow? after model pops up, all of my elements jump to right. it cause an ugly motion.

Cache provider on page reload

Hello,
to be able to cache the provider I had to move
web3Modal.providerController.cachedProvider = null;
from onConnect() into onDisconnect()

Demo does not work on mobile with WalletConnect and the MetaMask app installed

  1. In my Android phone, I have the MetaMask app installed, latest version.
  2. In Chrome, I go to https://web3modal.github.io/web3modal-vanilla-js-example/
  3. I tap on Connect Wallet
  4. A modal shows up offering me two choices: WalletConnect and Fortmatic.
  5. I tap on Wallet Connect
  6. Another modal, this one belonging to the Android OS, asks me to choose which app to use: Chrome or MetaMask. I choose MetaMask
  7. This gives focus to the MetaMask app, but MetaMask does nothing. It does not ask me to connect, or anything else. It just shows the main screen where I see my balance
  8. I manually come back to Chrome, where I had the above url open. Now I find a blank page in a new tab, so I close it and go back to the tab where I have the above url open
  9. I tap on the "Connect Wallet" button again. I repeat steps 3-5
  10. MetaMask pops up again, and this time does ask me to accept connecting to your webpage. I say yes
  11. It automatically closes Metamask and redirects me back to chrome
  12. again like in step 7, I find myself in a new blank tab, so I close it and go back to the tab with your demo
  13. There's a text that says "No wallet connected. Connect wallet to show accounts and their ETH balance". The "Connect Wallet" button below is greyed out and does nothing.
  14. I refresh the page. There's still the same text but now the "Connect Wallet" button is enabled
  15. I tap on it
  16. the modal shows up like at step 3 asking me to choose between WalletConnect and Fortmatic. I choose WalletConnect
  17. This does nothing. It does not show Android's dialog to choose an app and it does not open MetaMask. It just closes the dialog and I'm still at your page with the text that says "No wallet connected bla bla bla" and the Connect Wallet button is greyed out.

I retried again reloading the page and it always repeats steps 12-16. I never got to open MetaMask again.

Doesn't work on mobile at all

The VanillaJS web3 modal doesn't work at all on mobile, I tested with multiple browsers and multiple wallet provides, none of them seem to work.

How to reproduce:

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.