Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
Caches the contract code and ABI inputs (#221)
Browse files Browse the repository at this point in the history
Saves contract code in memory when getting out of tab: This is particularly useful when you need the address of a contract as one of the parameters for another contract (creating a dao that uses a token for example) and you want to go to another tab to copy it

Suggested on this PR: Add caching to contract #139
  • Loading branch information
Alex Van de Sande authored Jun 27, 2016
1 parent 561128a commit 3428e64
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ app/packages/*
app/public/i18n/*.json
build/
packages/

app/public/i18n/tap-i18n.json
app/public/i18n/de.json
app/public/i18n/es.json

2 changes: 1 addition & 1 deletion app/.meteor/versions
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ [email protected]
ethereum:[email protected]
ethereum:[email protected]
ethereum:[email protected]
ethereum:[email protected].2
ethereum:[email protected].3
ethereum:[email protected]
ethereum:[email protected]
[email protected]
Expand Down
4 changes: 2 additions & 2 deletions app/client/templates/elements/compileContract.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ <h3>{{i18n 'wallet.contracts.selectContract'}}</h3>
<select class="compiled-contracts">
<option disabled selected>{{i18n "wallet.contracts.pickContract"}}</option>
{{#each this}}
<option value="{{name}}">{{toSentence name true}}</option>
<option value="{{name}}" selected="{{selected}}">{{toSentence name true}}</option>
{{/each}}
</select>
{{else}}
{{{i18n "wallet.contracts.noContract"}}}
{{{i18n "wallet.contracts.noContract"}}}
{{/with}}

{{#with selectedContractInputs}}
Expand Down
39 changes: 27 additions & 12 deletions app/client/templates/elements/compileContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Template['elements_compileContract'].onCreated(function() {
TemplateVar.set('value', '');
TemplateVar.set('constructorInputs', []);
TemplateVar.set('selectedType', this.data.onlyByteCode ? 'byte-code' : 'source-code');
TemplateVar.set('compiledContracts', JSON.parse(localStorage['compiledContracts'] || null));
TemplateVar.set('selectedContract', JSON.parse(localStorage['selectedContract'] || null));

// focus the editors
this.autorun(function(c) {
Expand Down Expand Up @@ -54,6 +56,10 @@ Template['elements_compileContract'].onCreated(function() {
// generate new contract code
TemplateVar.set('value', web3.eth.contract(selectedContract.jsonInterface).new.getData.apply(null, constructorInputs));
TemplateVar.set('contract', selectedContract);

// Save data to localstorage
localStorage.setItem('selectedContract', JSON.stringify(selectedContract));

});
});

Expand All @@ -73,12 +79,9 @@ Template['elements_compileContract'].onRendered(function() {
this.aceEditor.$blockScrolling = Infinity;
this.aceEditor.focus();

this.aceEditor.setValue("contract MyContract {\n"+
" /* Constructor */\n"+
" function MyContract() {\n"+
" \n"+
" }\n"+
"}");
var defaultCode = localStorage['contractSource'] || "contract MyContract {\n /* Constructor */\n function MyContract() {\n \n }\n}";

this.aceEditor.setValue(defaultCode);
this.aceEditor.selection.selectTo(0);

editor = this.aceEditor;
Expand All @@ -87,10 +90,10 @@ Template['elements_compileContract'].onRendered(function() {
this.aceEditor.getSession().on('change', _.debounce(function(e) {
var sourceCode = template.aceEditor.getValue();

localStorage.setItem('contractSource', sourceCode);

TemplateVar.set(template, 'compiling', true);
TemplateVar.set(template, 'compileError', false);
// TemplateVar.set(template, 'compiledContracts', false);
// TemplateVar.set(template, 'selectedContract', false);

Meteor.setTimeout(function(argument) {
web3.eth.compile.solidity(sourceCode, function(error, compiledContracts){
Expand Down Expand Up @@ -133,7 +136,8 @@ Template['elements_compileContract'].onRendered(function() {

TemplateVar.set(template, 'selectedContract', null);
TemplateVar.set(template, 'compiledContracts', compiledContracts);
TemplateVar.set(template, 'constructorInputs', []);
localStorage.setItem('compiledContracts', JSON.stringify(compiledContracts));


} else {
console.log(error);
Expand Down Expand Up @@ -179,7 +183,7 @@ Template['elements_compileContract'].helpers({
@method (selectedContractInputs)
*/
'selectedContractInputs' : function(){
selectedContract = TemplateVar.get('selectedContract');
selectedContract = TemplateVar.get('selectedContract');
return selectedContract ? selectedContract.constructorInputs : [];
}
});
Expand Down Expand Up @@ -218,13 +222,24 @@ Template['elements_compileContract'].events({
@event 'change .contract-functions
*/
'change .compiled-contracts': function(e, template){
// set a contract as selected
var compiledContracts = TemplateVar.get('compiledContracts');

_.each(compiledContracts, function(contract){
contract.selected = contract.name == e.currentTarget.value;
})

// get the correct contract
var selectedContract = _.find(TemplateVar.get('compiledContracts'), function(contract){
return contract.name == e.currentTarget.value;
var selectedContract = _.find(compiledContracts, function(contract){
return contract.selected;
})

// change the inputs and data field
TemplateVar.set('selectedContract', selectedContract);
TemplateVar.set('compiledContracts', compiledContracts);
localStorage.setItem('compiledContracts', JSON.stringify(compiledContracts));

console.log(compiledContracts);

Tracker.afterFlush(function(){
// Run all inputs through formatter to catch bools
Expand Down
1 change: 1 addition & 0 deletions app/client/templates/elements/inputs/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Template['elements_input_address'].helpers({
var attr = _.clone(this);
attr.class = this.class ? this.class + ' abi-input' : 'abi-input';
attr.placeholder = this.placeholder || '0x123456...';
attr.value = this.value;
return attr;
}
})
2 changes: 1 addition & 1 deletion app/client/templates/elements/inputs/bool.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<h4>
{{#if name}}{{{toSentence name}}} <em>-</em> {{/if}}<em>Boolean</em>
</h4>
<input type="checkbox" id="input-bool-{{class}}-{{name}}" name="elements_input_bool" class="abi-input {{class}}">
<input type="checkbox" id="input-bool-{{class}}-{{name}}" name="elements_input_bool" class="abi-input {{class}}" checked="{{value}}">
<label for="input-bool-{{class}}-{{name}}">{{i18n "wallet.contracts.buttons.yes"}}</label>
</template>
2 changes: 1 addition & 1 deletion app/client/templates/elements/inputs/bytes.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<h4>
{{#if name}}{{{toSentence name}}} <em>-</em> {{/if}}<em>{{bits}} Bytes</em>
</h4>
<input type="text" pattern="^(0x)?[0-9a-fA-F]+$" placeholder="0x1234af..." name="elements_input_bytes" class="abi-input {{class}}">
<input type="text" pattern="^(0x)?[0-9a-fA-F]+$" placeholder="0x1234af..." name="elements_input_bytes" class="abi-input {{class}}" value="{{value}}">
</template>
2 changes: 1 addition & 1 deletion app/client/templates/elements/inputs/int.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<h4>
{{#if name}}{{{toSentence name}}} <em>-</em> {{/if}}<em>{{#if bits}}{{bits}} bits{{/if}} signed integer</em>
</h4>
<input type="number" step="1" placeholder="-123" name="elements_input_int" class="abi-input {{class}}">
<input type="number" step="1" placeholder="-123" name="elements_input_int" class="abi-input {{class}}" value="{{value}}">
</template>
2 changes: 1 addition & 1 deletion app/client/templates/elements/inputs/json.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<h4>
{{#if name}}{{{toSentence name}}} <em>-</em> {{/if}}<em>{{type}}</em>
</h4>
<textarea name="elements_input_json" cols="20" rows="5" class="abi-input {{class}}" placeholder="['my text', 12345]"></textarea>
<textarea name="elements_input_json" cols="20" rows="5" class="abi-input {{class}}" placeholder="['my text', 12345]">{{value}}</textarea>
</template>
2 changes: 1 addition & 1 deletion app/client/templates/elements/inputs/string.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<h4>
{{#if name}}{{{toSentence name}}} <em>-</em> {{/if}}<em>String</em>
</h4>
<input type="text" placeholder="MyString" name="elements_input_string" class="abi-input {{class}}">
<input type="text" placeholder="MyString" name="elements_input_string" class="abi-input {{class}}" value="{{value}}">
</template>
2 changes: 1 addition & 1 deletion app/client/templates/elements/inputs/uint.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<h4>
{{#if name}}{{{toSentence name}}} <em>-</em> {{/if}}<em>{{#if bits}}{{bits}} bits{{/if}} unsigned integer</em>
</h4>
<input type="number" step="1" min="0" placeholder="1234" name="elements_input_uint" class="abi-input {{class}}">
<input type="number" step="1" min="0" placeholder="1234" name="elements_input_uint" class="abi-input {{class}}" value="{{value}}">
</template>
8 changes: 8 additions & 0 deletions app/client/templates/views/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,10 @@ Template['views_send'].events({

addTransactionAfterSend(txHash, amount, selectedAccount.address, to, gasPrice, estimatedGas, data);

localStorage.setItem('contractSource', 'contract MyContract {\n /* Constructor */\n function MyContract() {\n \n }\n}');
localStorage.setItem('compiledContracts', null);
localStorage.setItem('selectedContract', null);

FlowRouter.go('dashboard');

} else {
Expand Down Expand Up @@ -647,6 +651,10 @@ Template['views_send'].events({
: data;

addTransactionAfterSend(txHash, amount, selectedAccount.address, to, gasPrice, estimatedGas, data);

localStorage.setItem('contractSource', 'contract MyContract {\n /* Constructor */\n function MyContract() {\n \n }\n}');
localStorage.setItem('compiledContracts', null);
localStorage.setItem('selectedContract', null);

FlowRouter.go('dashboard');
} else {
Expand Down
1 change: 0 additions & 1 deletion app/public/i18n/tap-i18n.json

This file was deleted.

0 comments on commit 3428e64

Please sign in to comment.