mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-26 12:29:06 +01:00
mascara:exampl/app - add a send tx button
This commit is contained in:
parent
a142f8913d
commit
e7589a099f
@ -7,20 +7,32 @@ async function loadProvider() {
|
|||||||
const ethereumProvider = window.metamask.createDefaultProvider({ host: 'http://localhost:9001' })
|
const ethereumProvider = window.metamask.createDefaultProvider({ host: 'http://localhost:9001' })
|
||||||
const ethQuery = new EthQuery(ethereumProvider)
|
const ethQuery = new EthQuery(ethereumProvider)
|
||||||
const accounts = await ethQuery.accounts()
|
const accounts = await ethQuery.accounts()
|
||||||
logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined')
|
window.METAMASK_ACCOUNT = accounts[0] || 'locked'
|
||||||
setupButton(ethQuery)
|
logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined', 'account')
|
||||||
|
setupButtons(ethQuery)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function logToDom(message){
|
function logToDom(message, context){
|
||||||
document.getElementById('account').innerText = message
|
document.getElementById(context).innerText = message
|
||||||
console.log(message)
|
console.log(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupButton (ethQuery) {
|
function setupButtons (ethQuery) {
|
||||||
const button = document.getElementById('action-button-1')
|
const accountButton = document.getElementById('action-button-1')
|
||||||
button.addEventListener('click', async () => {
|
accountButton.addEventListener('click', async () => {
|
||||||
const accounts = await ethQuery.accounts()
|
const accounts = await ethQuery.accounts()
|
||||||
logToDom(accounts.length ? accounts[0] : 'LOCKED or undefined')
|
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')
|
||||||
})
|
})
|
||||||
}
|
}
|
@ -10,6 +10,8 @@
|
|||||||
<body>
|
<body>
|
||||||
<button id="action-button-1">GET ACCOUNT</button>
|
<button id="action-button-1">GET ACCOUNT</button>
|
||||||
<div id="account"></div>
|
<div id="account"></div>
|
||||||
|
<button id="action-button-2">SEND TRANSACTION</button>
|
||||||
|
<div id="cb-value" ></div>
|
||||||
<script src="./app.js"></script>
|
<script src="./app.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in New Issue
Block a user