parent
63743b221a
commit
e6adf1da19
@ -0,0 +1,161 @@ |
||||
var $ = require('jquery'); |
||||
|
||||
module.exports = { |
||||
tableRowItems: function (first, second, cls) { |
||||
return $('<div class="crow"/>') |
||||
.addClass(cls) |
||||
.append($('<div class="col1">').append(first)) |
||||
.append($('<div class="col2">').append(second)); |
||||
}, |
||||
|
||||
tableRow: function (description, data) { |
||||
return this.tableRowItems( |
||||
$('<span/>').text(description), |
||||
$('<input readonly="readonly"/>').val(data)); |
||||
}, |
||||
|
||||
textRow: function (description, data, cls) { |
||||
return this.tableRowItems( |
||||
$('<strong/>').text(description), |
||||
$('<textarea readonly="readonly" class="gethDeployText"/>').val(data), |
||||
cls); |
||||
}, |
||||
|
||||
formatAssemblyText: function (asm, prefix, source) { |
||||
var self = this; |
||||
if (typeof asm === typeof '' || asm === null || asm === undefined) { |
||||
return prefix + asm + '\n'; |
||||
} |
||||
var text = prefix + '.code\n'; |
||||
$.each(asm['.code'], function (i, item) { |
||||
var v = item.value === undefined ? '' : item.value; |
||||
var src = ''; |
||||
if (item.begin !== undefined && item.end !== undefined) { |
||||
src = source.slice(item.begin, item.end).replace('\n', '\\n', 'g'); |
||||
} |
||||
if (src.length > 30) { |
||||
src = src.slice(0, 30) + '...'; |
||||
} |
||||
if (item.name !== 'tag') { |
||||
text += ' '; |
||||
} |
||||
text += prefix + item.name + ' ' + v + '\t\t\t' + src + '\n'; |
||||
}); |
||||
text += prefix + '.data\n'; |
||||
if (asm['.data']) { |
||||
$.each(asm['.data'], function (i, item) { |
||||
text += ' ' + prefix + '' + i + ':\n'; |
||||
text += self.formatAssemblyText(item, prefix + ' ', source); |
||||
}); |
||||
} |
||||
return text; |
||||
}, |
||||
|
||||
gethDeploy: function (contractName, jsonInterface, bytecode) { |
||||
var code = ''; |
||||
var funABI = this.getConstructorInterface(JSON.parse(jsonInterface)); |
||||
|
||||
funABI.inputs.forEach(function (inp) { |
||||
code += 'var ' + inp.name + ' = /* var of type ' + inp.type + ' here */ ;\n'; |
||||
}); |
||||
|
||||
code += 'var ' + contractName + 'Contract = web3.eth.contract(' + jsonInterface.replace('\n', '') + ');' + |
||||
'\nvar ' + contractName + ' = ' + contractName + 'Contract.new('; |
||||
|
||||
funABI.inputs.forEach(function (inp) { |
||||
code += '\n ' + inp.name + ','; |
||||
}); |
||||
|
||||
code += '\n {' + |
||||
'\n from: web3.eth.accounts[0], ' + |
||||
"\n data: '" + bytecode + "', " + |
||||
'\n gas: 4700000' + |
||||
'\n }, function (e, contract){' + |
||||
'\n console.log(e, contract);' + |
||||
"\n if (typeof contract.address !== 'undefined') {" + |
||||
"\n console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);" + |
||||
'\n }' + |
||||
'\n })'; |
||||
|
||||
return code; |
||||
}, |
||||
|
||||
getConstructorInterface: function (abi) { |
||||
var funABI = { 'name': '', 'inputs': [], 'type': 'constructor', 'outputs': [] }; |
||||
for (var i = 0; i < abi.length; i++) { |
||||
if (abi[i].type === 'constructor') { |
||||
funABI.inputs = abi[i].inputs || []; |
||||
break; |
||||
} |
||||
} |
||||
return funABI; |
||||
}, |
||||
|
||||
formatGasEstimates: function (data) { |
||||
// FIXME: the whole gasEstimates object should be nil instead
|
||||
if (data.creation === undefined && data.external === undefined && data.internal === undefined) { |
||||
return; |
||||
} |
||||
var gasToText = function (g) { return g === null ? 'unknown' : g; }; |
||||
var text = ''; |
||||
var fun; |
||||
if ('creation' in data) { |
||||
text += 'Creation: ' + gasToText(data.creation[0]) + ' + ' + gasToText(data.creation[1]) + '\n'; |
||||
} |
||||
|
||||
if ('external' in data) { |
||||
text += 'External:\n'; |
||||
for (fun in data.external) { |
||||
text += ' ' + fun + ': ' + gasToText(data.external[fun]) + '\n'; |
||||
} |
||||
} |
||||
|
||||
if ('internal' in data) { |
||||
text += 'Internal:\n'; |
||||
for (fun in data.internal) { |
||||
text += ' ' + fun + ': ' + gasToText(data.internal[fun]) + '\n'; |
||||
} |
||||
} |
||||
return text; |
||||
}, |
||||
|
||||
detailsOpen: {}, |
||||
getDetails: function (contract, source, contractName) { |
||||
var button = $('<button>Toggle Details</button>'); |
||||
var details = $('<div style="display: none;"/>') |
||||
.append(this.tableRow('Solidity Interface', contract.solidity_interface)); |
||||
|
||||
if (contract.opcodes !== '') { |
||||
details.append(this.tableRow('Opcodes', contract.opcodes)); |
||||
} |
||||
|
||||
var funHashes = ''; |
||||
for (var fun in contract.functionHashes) { |
||||
funHashes += contract.functionHashes[fun] + ' ' + fun + '\n'; |
||||
} |
||||
details.append($('<span class="col1">Functions</span>')); |
||||
details.append($('<pre/>').text(funHashes)); |
||||
|
||||
var gasEstimates = this.formatGasEstimates(contract.gasEstimates); |
||||
if (gasEstimates) { |
||||
details.append($('<span class="col1">Gas Estimates</span>')); |
||||
details.append($('<pre/>').text(gasEstimates)); |
||||
} |
||||
|
||||
if (contract.runtimeBytecode && contract.runtimeBytecode.length > 0) { |
||||
details.append(this.tableRow('Runtime Bytecode', contract.runtimeBytecode)); |
||||
} |
||||
|
||||
if (contract.assembly !== null) { |
||||
details.append($('<span class="col1">Assembly</span>')); |
||||
var assembly = $('<pre/>').text(this.formatAssemblyText(contract.assembly, '', source)); |
||||
details.append(assembly); |
||||
} |
||||
|
||||
button.click(function () { this.detailsOpen[contractName] = !this.detailsOpen[contractName]; details.toggle(); }); |
||||
if (this.detailsOpen[contractName]) { |
||||
details.show(); |
||||
} |
||||
return $('<div class="contractDetails"/>').append(button).append(details); |
||||
} |
||||
}; |
@ -0,0 +1,41 @@ |
||||
'use strict'; |
||||
function eventManager () { |
||||
this.registered = {}; |
||||
} |
||||
|
||||
eventManager.prototype.unregister = function (eventName, obj, func) { |
||||
if (obj instanceof Function) { |
||||
func = obj; |
||||
obj = {}; |
||||
} |
||||
for (var reg in this.registered[eventName]) { |
||||
if (this.registered[eventName][reg] && |
||||
this.registered[eventName][reg].obj === obj && (!func || this.registered[eventName][reg].func === func)) { |
||||
this.registered[eventName].splice(reg, 1); |
||||
return; |
||||
} |
||||
} |
||||
}; |
||||
|
||||
eventManager.prototype.register = function (eventName, obj, func) { |
||||
if (!this.registered[eventName]) { |
||||
this.registered[eventName] = []; |
||||
} |
||||
if (obj instanceof Function) { |
||||
func = obj; |
||||
obj = {}; |
||||
} |
||||
this.registered[eventName].push({ |
||||
obj: obj, |
||||
func: func |
||||
}); |
||||
}; |
||||
|
||||
eventManager.prototype.trigger = function (eventName, args) { |
||||
for (var listener in this.registered[eventName]) { |
||||
var l = this.registered[eventName][listener]; |
||||
l.func.apply(l.obj, args); |
||||
} |
||||
}; |
||||
|
||||
module.exports = eventManager; |
@ -0,0 +1,14 @@ |
||||
'use strict'; |
||||
var EventManager = require('./eventManager'); |
||||
|
||||
module.exports = { |
||||
extend: function (destination, source) { |
||||
for (var property in source) { |
||||
destination[property] = source[property]; |
||||
} |
||||
}, |
||||
|
||||
makeEventCapable: function (destination) { |
||||
destination.event = new EventManager(); |
||||
} |
||||
}; |
Loading…
Reference in new issue