remix-project mirror
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
remix-project/index.html

342 lines
11 KiB

<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Solidity realtime compiler and runtime</title>
<link rel="stylesheet" href="stylesheets/styles.css">
<link rel="stylesheet" href="stylesheets/pygment_trac.css">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<style type="text/css">
body {
padding: 0px;
font-size: 12px;
color: #111111;
}
#optimizeBox {
position: absolute;
top: 30px;
left: 720px;
font-size: 14px;
}
#input {
position: absolute;
top: 120px;
left: 0px;
width: 700px;
bottom: 0px;
font-size: 15px;
}
#output {
position: absolute;
top: 120px;
left: 720px;
right: 10px;
bottom: 0px;
font-size: 14px;
overflow: auto;
}
#header {
margin-bottom: 4px;
}
.col1 {
width: 18ex;
display: inline-block;
}
.col2 {
width: 60ex;
}
.runButton {
width: 30ex;
text-align: left;
overflow: hidden;
}
</style>
<script src="libs/jquery-2.1.3.min.js"></script>
<script src="libs/ace.js"></script>
<script src="mode-solidity.js"></script>
<script src="soljson.js"></script>
<script src="ethereumjs-vm.js"></script>
</head>
<body>
<h1 id="header">Solidity realtime compiler and runtime</h1>
Version: <span id="version">(loading)</span><br/>
<b>Note:</b> If Chrome/Chromium reports &quot;Uncaught JavaScript Exception&quot;,
enable the debug console (Ctrl+Shift+i) and reload.
<div id="optimizeBox">
<input id="optimize" type="checkbox"><label for="optimize">optimize</label>
</div>
<div id="input">contract Ballot {
struct Voter {
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal {
uint voteCount;
}
address chairperson;
mapping(address => Voter) voters;
Proposal[] proposals;
// Create a new ballot with $(_numProposals) different proposals.
function Ballot(uint8 _numProposals) {
chairperson = msg.sender;
voters[chairperson].weight = 1;
proposals.length = _numProposals;
}
// Give $(voter) the right to vote on this ballot.
// May only be called by $(chairperson).
function giveRightToVote(address voter) {
if (msg.sender != chairperson || voters[voter].voted) return;
voters[voter].weight = 1;
}
// Delegate your vote to the voter $(to).
function delegate(address to) {
Voter sender = voters[msg.sender]; // assigns reference
if (sender.voted) return;
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
to = voters[to].delegate;
if (to == msg.sender) return;
sender.voted = true;
sender.delegate = to;
Voter delegate = voters[to];
if (delegate.voted)
proposals[delegate.vote].voteCount += sender.weight;
else
delegate.weight += sender.weight;
}
// Give a single vote to proposal $(proposal).
function vote(uint8 proposal) {
Voter sender = voters[msg.sender];
if (sender.voted || proposal >= proposals.length) return;
sender.voted = true;
sender.vote = proposal;
proposals[proposal].voteCount += sender.weight;
}
function winningProposal() constant returns (uint8 winningProposal) {
uint256 winningVoteCount = 0;
for (uint8 proposal = 0; proposal < proposals.length; proposal++) {
if (proposals[proposal].voteCount > winningVoteCount) {
winningVoteCount = proposals[proposal].voteCount;
winningProposal = proposal;
}
++proposal;
}
}
}
</div>
<div id="output"></div>
<div style="height: 100px;"></div>
<p><small>Theme by <a href="https://github.com/orderedlist">orderedlist</a></small></p>
<script>
// ----------------- editor ----------------------
var editor = ace.edit("input");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/javascript");
editor.getSession().setTabSize(4);
editor.getSession().setUseSoftTabs(true);
// ----------------- compiler ----------------------
var compileJSON = Module.cwrap("compileJSON", "string", ["string", "number"]);
var ready = false;
Module['onRuntimeInitialized'] = function() {
ready = true;
$('#version').text(Module.cwrap("version", "string", [])());
onChange();
};
var previousInput = '';
var compile = function() {
if (!ready)
return;
var input = editor.getValue();
var optimize = document.querySelector('#optimize').checked;
try {
var data = $.parseJSON(compileJSON(input, optimize ? 1 : 0));
} catch (exception) {
renderError("Uncaught JavaScript Exception:\n" + exception);
return;
}
if (data['error'] !== undefined)
renderError(data['error']);
else
renderContracts(data, input);
}
var compileTimeout = null;
var onChange = function() {
if (!ready)
return;
var input = editor.getValue();
if (input == previousInput)
return;
previousInput = input;
if (compileTimeout) window.clearTimeout(compileTimeout);
compileTimeout = window.setTimeout(compile, 300);
};
editor.getSession().on('change', onChange);
document.querySelector('#optimize').addEventListener('change', compile);
// ----------------- compiler output renderer ----------------------
var detailsOpen = {};
var renderError = function(message) {
$('#output').empty().append($('<pre></pre>').text(message));
};
var renderContracts = function(data, source) {
$('#output').empty();
for (var contractName in data.contracts) {
var contract = data.contracts[contractName];
var contractOutput = $('<div class="contractOutput"/>')
.append($('<h3/>').text(contractName))
.append(getExecuteInterface(contract, contractName))
.append($('<div/>').text((contract.bytecode.length / 2) + ' bytes'))
.append(tableRow('Bytecode', contract.bytecode))
.append(tableRow('Interface', contract['interface']))
.append(tableRow('Solidity Interface', contract.solidity_interface))
.append(getDetails(contract, source, contractName));
$('#output').append(contractOutput);
}
};
var tableRow = function(description, data) {
return $('<div/>')
.append($('<span class="col1">').text(description))
.append($('<input readonly="readonly" class="col2">').val(data));
};
var getDetails = function(contract, source, contractName) {
var button = $('<button>Details</button>');
var details = $('<div style="display: none;"/>')
.append(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));
details.append($('<span class="col1">Gas Estimates</span>'));
details.append($('<pre/>').text(formatGasEstimates(contract.gasEstimates)));
details.append($('<span class="col1">Assembly</span>'));
var assembly = $('<pre/>').text(formatAssemblyText(contract.assembly, '', source));
details.append(assembly);
button.click(function() { detailsOpen[contractName] = !detailsOpen[contractName]; details.toggle(); });
if (detailsOpen[contractName])
details.show();
return $('<div/>').append(button).append(details);
};
var formatGasEstimates = function(data) {
var gasToText = function(g) { return g === null ? 'unknown' : g; }
var text = '';
if ('creation' in data)
text += 'Creation: ' + gasToText(data.creation[0]) + ' + ' + gasToText(data.creation[1]) + '\n';
text += 'External:\n';
for (var fun in data.external)
text += ' ' + fun + ': ' + gasToText(data.external[fun]) + '\n';
text += 'Internal:\n';
for (var fun in data.internal)
text += ' ' + fun + ': ' + gasToText(data.internal[fun]) + '\n';
return text;
};
var formatAssemblyText = function(asm, prefix, source) {
var text = '';
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 += formatAssemblyText(item, prefix + ' ', source);
});
return text;
};
$('.asmOutput button').click(function() {$(this).parent().find('pre').toggle(); } )
// ----------------- VM ----------------------
var stateTrie = new EthVm.Trie();
var vm = new EthVm.VM(stateTrie);
var secretKey = '3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511'
var publicKey = '0406cc661590d48ee972944b35ad13ff03c7876eae3fd191e8a2f77311b0a3c6613407b5005e63d7d8d76b89d5f900cde691497688bb281e07a5052ff61edebdc0'
var address = ethUtil.pubToAddress(new Buffer(publicKey, 'hex'));
var account = new EthVm.Account();
account.balance = 'f00000000000000001';
var nonce = 0;
stateTrie.put(address, account.serialize());
var runTx = function(data, to, cb) {
var tx = new EthVm.Transaction({
nonce: new Buffer([nonce++]), //@todo count beyond 255
gasPrice: '01',
gasLimit: '3000000',
to: to,
data: data
});
tx.sign(new Buffer(secretKey, 'hex'));
vm.runTx({tx: tx}, cb);
};
var getExecuteInterface = function(contract, name) {
var execInter = $('<div/>');
var createButton = $('<button>Create</button>');
var createResult = $('<span/>');
execInter
.append(createButton)
.append(createResult);
var bytecode = contract.bytecode;
var funHashes = contract.functionHashes;
var abi = $.parseJSON(contract.interface);
createButton.click(function() {
createResult.text(' ...');
//@todo constructor arguments
runTx(bytecode, undefined, function(err, result) {
if (err)
createResult.text(err);
else
{
createResult.text(' Creation used ' + result.vm.gasUsed.toString(10) + ' gas.');
appendFunctions(result.createdAddress);
}
});
});
var appendFunctions = function(address) {
var instance = $('<div/>');
instance.append($('<span/>').text('Contract at address ' + address.toString('hex')));
$.each(funHashes, function(signature, hash) {
var runButton = $('<button class="runButton"/>').text(signature);
var resultField = $('<span/>');
runButton.click(function() {
resultField.text(' ...');
//@todo arguments
runTx(hash, address, function(err, result) {
if (err)
resultField.text(err);
else
resultField.text(' Result: ' + result.vm.return.toString('hex'));
});
});
instance.append($('<div/>').append(runButton).append(resultField));
});
execInter.append(instance);
};
return execInter;
};
</script>
</body>
</html>