1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 18:41:38 +01:00
metamask-extension/mascara/example/app.js

58 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-08-26 20:08:23 +02:00
window.addEventListener('load', web3Detect)
window.addEventListener('message', console.warn)
2016-08-26 20:08:23 +02:00
function web3Detect() {
if (global.web3) {
logToDom('web3 detected!')
2016-09-01 00:33:24 +02:00
startApp()
2016-08-26 20:08:23 +02:00
} else {
logToDom('no web3 detected!')
2016-08-26 20:08:23 +02:00
}
}
function startApp(){
console.log('app started')
var primaryAccount
2016-09-01 00:33:24 +02:00
console.log('getting main account...')
web3.eth.getAccounts((err, addresses) => {
if (err) console.error(err)
console.log('set address', addresses[0])
2016-09-01 00:33:24 +02:00
primaryAccount = addresses[0]
})
2016-08-26 20:08:23 +02:00
document.querySelector('.action-button-1').addEventListener('click', function(){
console.log('saw click')
console.log('sending tx')
primaryAccount
2016-08-26 20:08:23 +02:00
web3.eth.sendTransaction({
from: primaryAccount,
2016-09-14 00:27:58 +02:00
to: primaryAccount,
2016-08-26 20:08:23 +02:00
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')
2016-08-26 20:08:23 +02:00
setTimeout(function(){
console.log('sending tx')
2016-08-26 20:08:23 +02:00
web3.eth.sendTransaction({
from: primaryAccount,
2016-09-14 00:27:58 +02:00
to: primaryAccount,
2016-08-26 20:08:23 +02:00
value: 0,
}, function(err, txHash){
if (err) throw err
console.log('sendTransaction result:', err || txHash)
})
})
})
}
function logToDom(message){
document.body.appendChild(document.createTextNode(message))
console.log(message)
}