diff options
author | frankiebee <frankie.diamond@gmail.com> | 2017-09-19 10:08:02 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2017-09-19 10:08:02 +0800 |
commit | 29dd81d029fb3cb5e0477758a59917b4cdf1091e (patch) | |
tree | bdaa8fe49a3690a1b12a6e9274f1cac8e30ac60a /mascara/example | |
parent | bbae8d975e1800b37b05b35713728cd39f992071 (diff) | |
download | tangerine-wallet-browser-29dd81d029fb3cb5e0477758a59917b4cdf1091e.tar tangerine-wallet-browser-29dd81d029fb3cb5e0477758a59917b4cdf1091e.tar.gz tangerine-wallet-browser-29dd81d029fb3cb5e0477758a59917b4cdf1091e.tar.bz2 tangerine-wallet-browser-29dd81d029fb3cb5e0477758a59917b4cdf1091e.tar.lz tangerine-wallet-browser-29dd81d029fb3cb5e0477758a59917b4cdf1091e.tar.xz tangerine-wallet-browser-29dd81d029fb3cb5e0477758a59917b4cdf1091e.tar.zst tangerine-wallet-browser-29dd81d029fb3cb5e0477758a59917b4cdf1091e.zip |
require metamascara
Diffstat (limited to 'mascara/example')
-rw-r--r-- | mascara/example/app.js | 67 | ||||
-rw-r--r-- | mascara/example/app/index.html | 6 |
2 files changed, 21 insertions, 52 deletions
diff --git a/mascara/example/app.js b/mascara/example/app.js index aae7ccd19..d0cb6ba83 100644 --- a/mascara/example/app.js +++ b/mascara/example/app.js @@ -1,57 +1,26 @@ -window.addEventListener('load', web3Detect) +const EthQuery = require('ethjs-query') + +window.addEventListener('load', loadProvider) window.addEventListener('message', console.warn) -function web3Detect() { - if (global.web3) { - logToDom('web3 detected!') - startApp() - } else { - logToDom('no web3 detected!') - } +async function loadProvider() { + const ethereumProvider = window.metamask.createDefaultProvider({ host: 'http://localhost:9001' }) + const ethQuery = new EthQuery(ethereumProvider) + const accounts = await ethQuery.accounts() + logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined') + setupButton(ethQuery) } -function startApp(){ - console.log('app started') - - var primaryAccount - console.log('getting main account...') - web3.eth.getAccounts((err, addresses) => { - if (err) console.error(err) - console.log('set address', addresses[0]) - primaryAccount = addresses[0] - }) - - document.querySelector('.action-button-1').addEventListener('click', function(){ - console.log('saw click') - console.log('sending tx') - primaryAccount - web3.eth.sendTransaction({ - from: primaryAccount, - to: primaryAccount, - value: 0, - }, function(err, txHash){ - if (err) throw err - console.log('sendTransaction result:', err || txHash) - }) - }) - document.querySelector('.action-button-2').addEventListener('click', function(){ - console.log('saw click') - setTimeout(function(){ - console.log('sending tx') - web3.eth.sendTransaction({ - from: primaryAccount, - to: primaryAccount, - value: 0, - }, function(err, txHash){ - if (err) throw err - console.log('sendTransaction result:', err || txHash) - }) - }) - }) - -} function logToDom(message){ - document.body.appendChild(document.createTextNode(message)) + document.getElementById('account').innerText = message console.log(message) } + +function setupButton (ethQuery) { + const button = document.getElementById('action-button-1') + button.addEventListener('click', async () => { + const accounts = await ethQuery.accounts() + logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined') + }) +}
\ No newline at end of file diff --git a/mascara/example/app/index.html b/mascara/example/app/index.html index 02323e5f9..f3e38877c 100644 --- a/mascara/example/app/index.html +++ b/mascara/example/app/index.html @@ -3,13 +3,13 @@ <html lang="en"> <head> <meta charset="utf-8"> - <title>MetaMask ZeroClient Example</title> <script src="http://localhost:9001/metamascara.js"></script> + <title>MetaMask ZeroClient Example</title> </head> <body> - <button class="action-button-1">SYNC TX</button> - <button class="action-button-2">ASYNC TX</button> + <button id="action-button-1">GET ACCOUNT</button> + <div id="account"></div> <script src="./app.js"></script> </body> </html>
\ No newline at end of file |