Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from hexeight/dev
Wasabi.js cleaned scope, and removed inject contracts as global variables.
- Loading branch information
Showing
2 changed files
with
46 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,41 @@ | ||
function startApp () { | ||
var xmlHttp = new XMLHttpRequest(); | ||
xmlHttp.onreadystatechange = function() { | ||
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { | ||
var wasabi = JSON.parse(xmlHttp.responseText); | ||
wasabi.forEach(function (contract, index) { | ||
var definition = web3.eth.contract(contract.abi); | ||
var c = definition.at(contract.address); | ||
window[contract.name] = { | ||
deployed: function () { | ||
return c; | ||
}, | ||
at: function (address) { | ||
return definition.at(address); | ||
} | ||
}; | ||
}); | ||
(function () { | ||
Wasabi = {}; | ||
function initWasabi () { | ||
var xmlHttp = new XMLHttpRequest(); | ||
xmlHttp.onreadystatechange = function() { | ||
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { | ||
var config = JSON.parse(xmlHttp.responseText); | ||
config.forEach(function (contract, index) { | ||
var definition = web3.eth.contract(contract.abi); | ||
var c = definition.at(contract.address); | ||
Wasabi[contract.name] = { | ||
deployed: function () { | ||
return c; | ||
}, | ||
at: function (address) { | ||
return definition.at(address); | ||
} | ||
}; | ||
}); | ||
} | ||
} | ||
xmlHttp.open("GET", "/contracts.json", true); | ||
xmlHttp.send(null); | ||
} | ||
xmlHttp.open("GET", "/contracts.json", true); | ||
xmlHttp.send(null); | ||
} | ||
|
||
window.addEventListener('load', function() { | ||
window.addEventListener('web3-loaded', startApp()); | ||
// Checking if Web3 has been injected by the browser (Mist/MetaMask) | ||
if (typeof web3 !== 'undefined') { | ||
// Use Mist/MetaMask's provider | ||
window.web3 = new Web3(web3.currentProvider); | ||
var event = new Event('web3-loaded'); | ||
window.dispatchEvent(event); | ||
} else { | ||
console.error("Web3 is undefined: Your browser does not support dApps. Please use Mist, Metamask of comparable dApp explorer."); | ||
var event = new Event('web3-failed'); | ||
window.dispatchEvent(event); | ||
return; | ||
} | ||
}); | ||
window.addEventListener('load', function() { | ||
window.addEventListener('web3-loaded', initWasabi()); | ||
// Checking if Web3 has been injected by the browser (Mist/MetaMask) | ||
if (typeof web3 !== 'undefined') { | ||
// Use Mist/MetaMask's provider | ||
window.web3 = new Web3(web3.currentProvider); | ||
var event = new Event('web3-loaded'); | ||
window.dispatchEvent(event); | ||
} else { | ||
console.error("Web3 is undefined: Your browser does not support dApps. Please use Mist, Metamask of comparable dApp explorer."); | ||
var event = new Event('web3-failed'); | ||
window.dispatchEvent(event); | ||
return; | ||
} | ||
}); | ||
})(); |