2017-09-19 04:08:02 +02:00
|
|
|
const EthQuery = require('ethjs-query')
|
|
|
|
|
|
|
|
window.addEventListener('load', loadProvider)
|
2017-03-29 19:53:43 +02:00
|
|
|
window.addEventListener('message', console.warn)
|
2016-08-26 20:08:23 +02:00
|
|
|
|
2018-07-03 00:49:33 +02:00
|
|
|
async function loadProvider () {
|
2017-09-19 04:08:02 +02:00
|
|
|
const ethereumProvider = window.metamask.createDefaultProvider({ host: 'http://localhost:9001' })
|
|
|
|
const ethQuery = new EthQuery(ethereumProvider)
|
|
|
|
const accounts = await ethQuery.accounts()
|
2017-10-05 07:09:37 +02:00
|
|
|
window.METAMASK_ACCOUNT = accounts[0] || 'locked'
|
|
|
|
logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined', 'account')
|
|
|
|
setupButtons(ethQuery)
|
2016-08-26 20:08:23 +02:00
|
|
|
}
|
|
|
|
|
2016-09-13 03:30:45 +02:00
|
|
|
|
2018-07-03 00:49:33 +02:00
|
|
|
function logToDom (message, context) {
|
2017-10-05 07:09:37 +02:00
|
|
|
document.getElementById(context).innerText = message
|
2016-09-13 03:30:45 +02:00
|
|
|
console.log(message)
|
2017-03-29 03:02:08 +02:00
|
|
|
}
|
2017-09-19 04:08:02 +02:00
|
|
|
|
2017-10-05 07:09:37 +02:00
|
|
|
function setupButtons (ethQuery) {
|
|
|
|
const accountButton = document.getElementById('action-button-1')
|
|
|
|
accountButton.addEventListener('click', async () => {
|
2017-09-19 04:08:02 +02:00
|
|
|
const accounts = await ethQuery.accounts()
|
2017-10-05 07:09:37 +02:00
|
|
|
window.METAMASK_ACCOUNT = accounts[0] || 'locked'
|
|
|
|
logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined', 'account')
|
|
|
|
})
|
|
|
|
const txButton = document.getElementById('action-button-2')
|
|
|
|
txButton.addEventListener('click', async () => {
|
|
|
|
if (!window.METAMASK_ACCOUNT || window.METAMASK_ACCOUNT === 'locked') return
|
|
|
|
const txHash = await ethQuery.sendTransaction({
|
|
|
|
from: window.METAMASK_ACCOUNT,
|
|
|
|
to: window.METAMASK_ACCOUNT,
|
|
|
|
data: '',
|
|
|
|
})
|
|
|
|
logToDom(txHash, 'cb-value')
|
2017-09-19 04:08:02 +02:00
|
|
|
})
|
2018-07-03 00:49:33 +02:00
|
|
|
}
|