function UniversalDApp (contracts, options) { this.options = options || {}; this.$el = $('
'); this.contracts = contracts; if (web3.currentProvider) { } else if (options.vm) { this.stateTrie = new EthVm.Trie(); this.vm = new EthVm.VM(this.stateTrie); //@todo this does not calculate the gas costs correctly but gets the job done. this.identityCode = 'return { gasUsed: 1, return: opts.data, exception: 1 };'; this.identityAddr = ethUtil.pad(new Buffer('04', 'hex'), 20) this.vm.loadPrecompiled(this.identityAddr, this.identityCode); this.secretKey = '3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511' this.publicKey = '0406cc661590d48ee972944b35ad13ff03c7876eae3fd191e8a2f77311b0a3c6613407b5005e63d7d8d76b89d5f900cde691497688bb281e07a5052ff61edebdc0' this.address = ethUtil.pubToAddress(new Buffer(this.publicKey, 'hex')); this.account = new EthVm.Account(); this.account.balance = 'f00000000000000001'; this.nonce = 0; this.stateTrie.put(this.address, this.account.serialize()); } else { var host = options.host || "localhost"; var port = options.port || "8545"; var rpc_url = 'http://' + host + ':' + port; web3.setProvider( new web3.providers.HttpProvider( rpc_url ) ); } } UniversalDApp.prototype.render = function () { if (this.contracts.length == 0) { this.$el.append( this.getABIInputForm() ); } else { for (var c in this.contracts) { var $contractEl = $(''); if (this.contracts[c].address) { this.getInstanceInterface(this.contracts[c], this.contracts[c].address, $contractEl ); } else { var $title = $('').text( this.contracts[c].name ); if (this.contracts[c].bytecode) { $title.append($('').text((this.contracts[c].bytecode.length / 2) + ' bytes')) } $contractEl.append( $title ).append( this.getCreateInterface( $contractEl, this.contracts[c]) ); } this.$el.append( $contractEl ); } } $legend = $('') .append( $('').text('Attach') ) .append( $('').text('Transact') ) .append( $('').text('Call') ) this.$el.append( $('') .html("Universal ÐApp powered by The Blockchain") ) this.$el.append( $legend ) return this.$el; } UniversalDApp.prototype.getABIInputForm = function (cb){ var self = this; var $el = $(''); var $jsonInput = $('') var $createButton = $('').text('Create a Universal ÐApp') $createButton.click(function(ev){ var contracts = $.parseJSON( $jsonInput.val() ); if (cb) { var err = null; var dapp = null; try { dapp = new UniversalDApp( contracts, self.options ); } catch(e) { err = e; } cb( err, dapp ) } else { self.contracts = contracts; self.$el.empty().append( self.render() ) } }) $el.append( $jsonInput ).append( $createButton ) return $el; } UniversalDApp.prototype.getCreateInterface = function ($container, contract) { var self = this; var $createInterface = $(''); if (this.options.removable) { var $close = $('') $close.click( function(){ self.$el.remove(); } ) $createInterface.append( $close ); } var $newButton = this.getInstanceInterface( contract ) var $atButton = $('').text('At Address').click( function(){ self.clickContractAt( self, $container, contract ) } ); $createInterface.append( $atButton ).append( $newButton ); return $createInterface; } UniversalDApp.prototype.getInstanceInterface = function (contract, address, $target) { var self = this; var abi = JSON.parse(contract.interface).sort(function(a,b){ if (a.name > b.name) return -1; else return 1; }).sort(function(a,b){ if (a.constant == true) return -1; else return 1; }); var funABI = this.getConstructorInterface(abi); var $createInterface = $(''); var appendFunctions = function (address, $el){ var $instance = $(''); if (self.options.removable_instances) { var $close = $('') $close.click( function(){ $instance.remove(); } ) $instance.append( $close ); } var $title = $('').text( contract.name + " at " + (self.options.vm ? '0x' : '') + address.toString('hex') ); $title.click(function(){ $instance.toggleClass('hide'); }); $events = $(''); if (!self.options.vm){ var jsInterface = web3.eth.contract(abi).at(address) var eventFilter = jsInterface.allEvents(); eventFilter.watch(function(err,response){ $event = $('') var $close = $('') $close.click( function(){ $event.remove(); } ) $event.append( $('').text(response.event) ) .append( $('').text( JSON.stringify(response.args, null, 2) ) ) .append( $close ); $events.append( $event ) }) } $instance.append( $title ) $.each(abi, function(i, funABI) { if (funABI.type != 'function') return; $instance.append(self.getCallButton({ abi: funABI, address: address })); }); ($el || $createInterface ).append( $instance.append( $events ) ) } if (!address || !$target) { $createInterface.append( this.getCallButton({ abi: funABI, bytecode: contract.bytecode, appendFunctions: appendFunctions })); } else { appendFunctions( address, $target ); } return $createInterface; } UniversalDApp.prototype.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; } UniversalDApp.prototype.getCallButton = function(args) { var self = this; // args.abi, args.bytecode [constr only], args.address [fun only] // args.appendFunctions [constr only] var isConstructor = args.bytecode !== undefined; var lookupOnly = ( args.abi.constant && !isConstructor ); var fun = new web3.eth.function(args.abi); var inputs = ''; $.each(args.abi.inputs, function(i, inp) { if (inputs != '') inputs += ', '; inputs += inp.type + ' ' + inp.name; }); if (!args.bytecode && !fun.displayName()) return; var inputField = $('').attr('placeholder', inputs); var $outputOverride = $(''); var outputSpan = $(''); var getReturnOutput = function(result) { var returnName = lookupOnly ? 'Value' : 'Result'; var returnCls = lookupOnly ? 'value' : 'returned'; return $('