Merge pull request #69 from chriseth/updateWeb3js

Update web3.js and make it work in mist.
pull/1/head
chriseth 9 years ago
commit ae35221c36
  1. 50
      assets/js/universal-dapp.js
  2. 17
      index.html

@ -115,6 +115,7 @@ UniversalDApp.prototype.getInstanceInterface = function (contract, address, $tar
if (a.constant == true) return -1; if (a.constant == true) return -1;
else return 1; else return 1;
}); });
var web3contract = web3.eth.contract(abi);
var funABI = this.getConstructorInterface(abi); var funABI = this.getConstructorInterface(abi);
var $createInterface = $('<div class="createContract"/>'); var $createInterface = $('<div class="createContract"/>');
@ -138,8 +139,7 @@ UniversalDApp.prototype.getInstanceInterface = function (contract, address, $tar
// TODO: parse/use reponse.vm.logs // TODO: parse/use reponse.vm.logs
}); });
} else { } else {
var jsInterface = web3.eth.contract(abi).at(address) var eventFilter = web3contract.at(address).allEvents();
var eventFilter = jsInterface.allEvents();
eventFilter.watch(function(err,response){ eventFilter.watch(function(err,response){
$event = $('<div class="event" />') $event = $('<div class="event" />')
@ -157,8 +157,13 @@ UniversalDApp.prototype.getInstanceInterface = function (contract, address, $tar
$.each(abi, function(i, funABI) { $.each(abi, function(i, funABI) {
if (funABI.type != 'function') return; if (funABI.type != 'function') return;
// @todo getData cannot be used with overloaded functions
$instance.append(self.getCallButton({ $instance.append(self.getCallButton({
abi: funABI, abi: funABI,
encode: function(args) {
var obj = web3contract.at('0x00')[funABI.name];
return obj.getData.apply(obj, args);
},
address: address address: address
})); }));
}); });
@ -168,6 +173,10 @@ UniversalDApp.prototype.getInstanceInterface = function (contract, address, $tar
if (!address || !$target) { if (!address || !$target) {
$createInterface.append( this.getCallButton({ $createInterface.append( this.getCallButton({
abi: funABI, abi: funABI,
encode: function(args) {
var obj = web3contract.new;
return obj.getData.apply(obj, args);
},
contractName: contract.name, contractName: contract.name,
bytecode: contract.bytecode, bytecode: contract.bytecode,
appendFunctions: appendFunctions appendFunctions: appendFunctions
@ -191,18 +200,16 @@ UniversalDApp.prototype.getConstructorInterface = function(abi) {
UniversalDApp.prototype.getCallButton = function(args) { UniversalDApp.prototype.getCallButton = function(args) {
var self = this; var self = this;
// args.abi, args.contractName [constr only], args.bytecode, args.address [fun only] // args.abi, args.encode, args.bytecode [constr only], args.address [fun only]
// args.appendFunctions [constr only] // args.contractName [constr only], args.appendFunctions [constr only]
var isConstructor = args.bytecode !== undefined; var isConstructor = args.bytecode !== undefined;
var lookupOnly = ( args.abi.constant && !isConstructor ); var lookupOnly = ( args.abi.constant && !isConstructor );
var fun = new web3.eth.function(args.abi);
var inputs = ''; var inputs = '';
$.each(args.abi.inputs, function(i, inp) { $.each(args.abi.inputs, function(i, inp) {
if (inputs != '') inputs += ', '; if (inputs != '') inputs += ', ';
inputs += inp.type + ' ' + inp.name; inputs += inp.type + ' ' + inp.name;
}); });
if (!args.bytecode && !fun.displayName()) return;
var inputField = $('<input/>').attr('placeholder', inputs).attr('title', inputs); var inputField = $('<input/>').attr('placeholder', inputs).attr('title', inputs);
var $outputOverride = $('<div class="value" />'); var $outputOverride = $('<div class="value" />');
var outputSpan = $('<div class="output"/>'); var outputSpan = $('<div class="output"/>');
@ -239,10 +246,6 @@ UniversalDApp.prototype.getCallButton = function(args) {
}; };
var handleCallButtonClick = function(ev, $result) { var handleCallButtonClick = function(ev, $result) {
var funArgs = $.parseJSON('[' + inputField.val() + ']');
var data = fun.toPayload(funArgs).data;
if (data.slice(0, 2) == '0x') data = data.slice(2);
if (!$result) { if (!$result) {
$result = getOutput(); $result = getOutput();
if (lookupOnly && !inputs.length) if (lookupOnly && !inputs.length)
@ -250,6 +253,21 @@ UniversalDApp.prototype.getCallButton = function(args) {
else else
outputSpan.append( $result ); outputSpan.append( $result );
} }
var funArgs = '';
try {
funArgs = $.parseJSON('[' + inputField.val() + ']');
} catch (e) {
replaceOutput($result, $('<span/>').text('Error encoding arguments: ' + e));
return;
}
var data = '';
if (!isConstructor || funArgs.length > 0)
data = args.encode(funArgs);
if (data.slice(0, 9) == 'undefined')
data = data.slice(9);
if (data.slice(0, 2) == '0x') data = data.slice(2);
replaceOutput($result, $('<span>Waiting for transaction to be mined...</span>')); replaceOutput($result, $('<span>Waiting for transaction to be mined...</span>'));
if (isConstructor) { if (isConstructor) {
@ -268,17 +286,21 @@ UniversalDApp.prototype.getCallButton = function(args) {
replaceOutput($result, $('<span>Contract needs to be linked to a library, this is only supported in the JavaScript VM for now.</span>')); replaceOutput($result, $('<span>Contract needs to be linked to a library, this is only supported in the JavaScript VM for now.</span>'));
return; return;
} else } else
data = args.bytecode + data.slice(8); data = args.bytecode + data;
} }
self.runTx(data, args, function(err, result) { self.runTx(data, args, function(err, result) {
if (err) { if (err) {
replaceOutput($result, $('<span/>').text(err).addClass('error')); replaceOutput($result, $('<span/>').text(err).addClass('error'));
} else if (self.options.vm && result.vm.return === undefined) {
replaceOutput($result, $('<span/>').text('Exception during execution.').addClass('error'));
} else if (self.options.vm && isConstructor) { } else if (self.options.vm && isConstructor) {
replaceOutput($result, getGasUsedOutput(result)); replaceOutput($result, getGasUsedOutput(result));
args.appendFunctions(result.createdAddress); args.appendFunctions(result.createdAddress);
} else if (self.options.vm){ } else if (self.options.vm){
var outputObj = fun.unpackOutput('0x' + result.vm.return.toString('hex')); //@todo implement once decoder is exposed by web3.js
//var outputObj = fun.unpackOutput('0x' + result.vm.return.toString('hex'));
var outputObj = '0x' + result.vm.return.toString('hex');
clearOutput($result); clearOutput($result);
$result.append(getReturnOutput(outputObj)).append(getGasUsedOutput(result.vm)); $result.append(getReturnOutput(outputObj)).append(getGasUsedOutput(result.vm));
} else if (args.abi.constant && !isConstructor) { } else if (args.abi.constant && !isConstructor) {
@ -312,8 +334,8 @@ UniversalDApp.prototype.getCallButton = function(args) {
var button = $('<button />') var button = $('<button />')
.addClass( 'call' ) .addClass( 'call' )
.attr('title', fun.displayName()) .attr('title', args.abi.name)
.text(args.bytecode ? 'Create' : fun.displayName()) .text(args.bytecode ? 'Create' : args.abi.name)
.click( handleCallButtonClick ); .click( handleCallButtonClick );
if (lookupOnly && !inputs.length) { if (lookupOnly && !inputs.length) {

@ -105,7 +105,7 @@
<label for="web3"> <label for="web3">
<input id="web3" type="radio" value="web3" name="executionContext"> <input id="web3" type="radio" value="web3" name="executionContext">
<strong>Web3 Provider</strong> <strong>Web3 Provider</strong>
<p>Execution environment connects to node at localhost, transactions will be sent to the network and can cause loss of money or worse!</p> <p>Execution environment connects to node at localhost (or via IPC if available), transactions will be sent to the network and can cause loss of money or worse!</p>
<label for="web3Endpoint"> <label for="web3Endpoint">
<strong>Web3 Provider Endpoint</strong>: <input type="text" id="web3Endpoint" value="http://localhost:8545"> <strong>Web3 Provider Endpoint</strong>: <input type="text" id="web3Endpoint" value="http://localhost:8545">
</label> </label>
@ -229,14 +229,25 @@
var $web3Toggle = $('#web3'); var $web3Toggle = $('#web3');
var $web3endpoint = $('#web3Endpoint'); var $web3endpoint = $('#web3Endpoint');
if (typeof web3 !== 'undefined')
{
if (web3.providers && web3.currentProvider instanceof web3.providers.IpcProvider)
$web3endpoint.val('ipc');
web3 = new Web3(web3.currentProvider);
} else
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var executionContext = 'vm'; var executionContext = 'vm';
$vmToggle.get(0).checked = true; $vmToggle.get(0).checked = true;
$vmToggle.on('change', executionContextChange ); $vmToggle.on('change', executionContextChange );
$web3Toggle.on('change', executionContextChange ); $web3Toggle.on('change', executionContextChange );
$web3endpoint.on('change', function() { $web3endpoint.on('change', function() {
var endpoint = $('#web3Endpoint').val(); var endpoint = $web3endpoint.val();
web3.setProvider( new web3.providers.HttpProvider( endpoint ) ); if (endpoint == 'ipc')
web3.setProvider(new Web3.providers.IpcProvider());
else
web3.setProvider(new Web3.providers.HttpProvider(endpoint));
compile(); compile();
}); });

Loading…
Cancel
Save