embark/lib/modules/webserver/backend/contracts/view.js

52 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-04-18 19:06:41 +00:00
const contractName = location.search.replace(/\?/, '');
let contractDefinition;
let host;
fetch("/embark/console", {
method: "POST",
headers: { 'content-type' : 'application/x-www-form-urlencoded; charset=UTF-8' },
body: "cmd=web3.currentProvider.host"
})
.then(response => response.text())
.then(text => {
host = text;
return fetch("/embark/contract/" + contractName);
})
.then(response => response.json())
.then(_contractDefinition => {
contractDefinition = _contractDefinition;
return fetch("/embark/files/contracts?filename=" + contractDefinition.originalFilename);
})
.then(response => response.text())
.then(contractSource => {
const web3 = new Web3(host);
window.web3 = web3;
let contractObj = new web3.eth.Contract(contractDefinition.abiDefinition);
contractObj.options.data = "0x" + contractDefinition.code;
contractObj.options.address = contractDefinition.deployedAddress;
2018-04-18 19:06:41 +00:00
window[contractDefinition.className] = contractObj;
ReactDOM.render(
<ContractUI name={contractDefinition.className} definition={contractDefinition} contract={contractObj} source={contractSource} />,
document.getElementById('contracts-area')
);
})
.catch(function(err) {
console.log('%cError while rendering UI', 'font-weight: bold');
console.error(err);
ReactDOM.render(
<div>
<h1 className="h2 mb-3">Error rendering the UI</h1>
<p className="h4 text-muted font-weight-normal mb-7">UI for "{contractName}" cannot be generated</p>
</div>,
document.getElementById('contracts-area')
);
2018-04-18 19:06:41 +00:00
});