contract.html example is working with sync api

This commit is contained in:
Marek Kotewicz 2015-01-21 21:12:07 +01:00
parent ceb4357a8d
commit c9693b4746
8 changed files with 60 additions and 62 deletions

37
dist/ethereum.js vendored

@ -33,6 +33,9 @@ BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN });
var ETH_PADDING = 32; var ETH_PADDING = 32;
/// method signature length in bytes
var ETH_METHOD_SIGNATURE_LENGTH = 4;
/// Finds first index of array element matching pattern /// Finds first index of array element matching pattern
/// @param array /// @param array
/// @param callback pattern /// @param callback pattern
@ -390,11 +393,10 @@ var outputParser = function (json) {
return parser; return parser;
}; };
/// @param json abi for contract
/// @param method name for which we want to get method signature /// @param method name for which we want to get method signature
/// @returns (promise) contract method signature for method with given name /// @returns (promise) contract method signature for method with given name
var methodSignature = function (json, name) { var methodSignature = function (name) {
return web3.sha3(web3.fromAscii(name)); return web3.sha3(web3.fromAscii(name)).slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2);
}; };
module.exports = { module.exports = {
@ -432,8 +434,7 @@ module.exports = {
var web3 = require('./web3'); // jshint ignore:line var web3 = require('./web3'); // jshint ignore:line
var abi = require('./abi'); var abi = require('./abi');
/// method signature length in bytes
var ETH_METHOD_SIGNATURE_LENGTH = 4;
/** /**
* This method should be called when we want to call / transact some solidity method from javascript * This method should be called when we want to call / transact some solidity method from javascript
@ -469,29 +470,29 @@ var contract = function (address, desc) {
var impl = function () { var impl = function () {
var params = Array.prototype.slice.call(arguments); var params = Array.prototype.slice.call(arguments);
var parsed = inputParser[displayName][typeName].apply(null, params); var parsed = inputParser[displayName][typeName].apply(null, params);
var signature = abi.methodSignature(method.name);
var onSuccess = function (result) {
return outputParser[displayName][typeName](result);
};
return { return {
call: function (extra) { call: function (extra) {
extra = extra || {}; extra = extra || {};
extra.to = address; extra.to = address;
return abi.methodSignature(desc, method.name).then(function (signature) { extra.data = signature + parsed;
extra.data = signature.slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2) + parsed;
return web3.eth.call(extra).then(onSuccess); var result = web3.eth.call(extra);
}); return outputParser[displayName][typeName](result);
}, },
transact: function (extra) { transact: function (extra) {
extra = extra || {}; extra = extra || {};
extra.to = address; extra.to = address;
return abi.methodSignature(desc, method.name).then(function (signature) { extra.data = signature + parsed;
extra.data = signature.slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2) + parsed;
/// it's used by natspec.js
/// TODO: figure a better way to solve this
web3._currentContractAbi = desc; web3._currentContractAbi = desc;
web3._currentContractAddress = address; web3._currentContractAddress = address;
return web3.eth.transact(extra).then(onSuccess);
}); var result = web3.eth.transact(extra);
return outputParser[displayName][typeName](result);
} }
}; };
}; };
@ -623,7 +624,7 @@ module.exports = Filter;
var HttpSyncProvider = function (host) { var HttpSyncProvider = function (host) {
this.handlers = []; this.handlers = [];
this.host = host; this.host = host || 'http://localhost:8080';
}; };
/// Transforms inner message to proper jsonrpc object /// Transforms inner message to proper jsonrpc object

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -14,18 +14,16 @@
var coinbase = web3.eth.coinbase; var coinbase = web3.eth.coinbase;
var originalBalance = 0; var originalBalance = 0;
web3.eth.balanceAt(coinbase).then(function (balance) { var balance = web3.eth.balanceAt(coinbase);
originalBalance = web3.toDecimal(balance); var originalBalance = web3.toDecimal(balance);
document.getElementById('original').innerText = 'original balance: ' + originalBalance + ' watching...'; document.getElementById('original').innerText = 'original balance: ' + originalBalance + ' watching...';
});
web3.eth.watch({altered: coinbase}).changed(function() { web3.eth.watch({altered: coinbase}).changed(function() {
web3.eth.balanceAt(coinbase).then(function (balance) { balance = web3.eth.balanceAt(coinbase)
var currentBalance = web3.toDecimal(balance); var currentBalance = web3.toDecimal(balance);
document.getElementById("current").innerText = 'current: ' + currentBalance; document.getElementById("current").innerText = 'current: ' + currentBalance;
document.getElementById("diff").innerText = 'diff: ' + (currentBalance - originalBalance); document.getElementById("diff").innerText = 'diff: ' + (currentBalance - originalBalance);
}); });
});
} }
</script> </script>

@ -8,7 +8,7 @@
<script type="text/javascript"> <script type="text/javascript">
var web3 = require('web3'); var web3 = require('web3');
web3.setProvider(new web3.providers.AutoProvider()); web3.setProvider(new web3.providers.HttpSyncProvider());
// solidity source code // solidity source code
var source = "" + var source = "" +
@ -43,10 +43,9 @@
document.getElementById('source').innerText = source; document.getElementById('source').innerText = source;
// create contract // create contract
web3.eth.transact({code: web3.eth.solidity(source)}).then(function (address) { var address = web3.eth.transact({code: web3.eth.solidity(source)});
contract = web3.eth.contract(address, desc); contract = web3.eth.contract(address, desc);
document.getElementById('call').style.visibility = 'visible'; document.getElementById('call').style.visibility = 'visible';
});
} }
function callExampleContract() { function callExampleContract() {
@ -54,9 +53,8 @@
var param = parseInt(document.getElementById('value').value); var param = parseInt(document.getElementById('value').value);
// call the contract // call the contract
contract.multiply(param).call().then(function(res) { var res = contract.multiply(param).call();
document.getElementById('result').innerText = res[0]; document.getElementById('result').innerText = res[0];
});
} }
</script> </script>

@ -32,6 +32,9 @@ BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN });
var ETH_PADDING = 32; var ETH_PADDING = 32;
/// method signature length in bytes
var ETH_METHOD_SIGNATURE_LENGTH = 4;
/// Finds first index of array element matching pattern /// Finds first index of array element matching pattern
/// @param array /// @param array
/// @param callback pattern /// @param callback pattern
@ -389,11 +392,10 @@ var outputParser = function (json) {
return parser; return parser;
}; };
/// @param json abi for contract
/// @param method name for which we want to get method signature /// @param method name for which we want to get method signature
/// @returns (promise) contract method signature for method with given name /// @returns (promise) contract method signature for method with given name
var methodSignature = function (json, name) { var methodSignature = function (name) {
return web3.sha3(web3.fromAscii(name)); return web3.sha3(web3.fromAscii(name)).slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2);
}; };
module.exports = { module.exports = {

@ -23,8 +23,7 @@
var web3 = require('./web3'); // jshint ignore:line var web3 = require('./web3'); // jshint ignore:line
var abi = require('./abi'); var abi = require('./abi');
/// method signature length in bytes
var ETH_METHOD_SIGNATURE_LENGTH = 4;
/** /**
* This method should be called when we want to call / transact some solidity method from javascript * This method should be called when we want to call / transact some solidity method from javascript
@ -60,29 +59,29 @@ var contract = function (address, desc) {
var impl = function () { var impl = function () {
var params = Array.prototype.slice.call(arguments); var params = Array.prototype.slice.call(arguments);
var parsed = inputParser[displayName][typeName].apply(null, params); var parsed = inputParser[displayName][typeName].apply(null, params);
var signature = abi.methodSignature(method.name);
var onSuccess = function (result) {
return outputParser[displayName][typeName](result);
};
return { return {
call: function (extra) { call: function (extra) {
extra = extra || {}; extra = extra || {};
extra.to = address; extra.to = address;
return abi.methodSignature(desc, method.name).then(function (signature) { extra.data = signature + parsed;
extra.data = signature.slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2) + parsed;
return web3.eth.call(extra).then(onSuccess); var result = web3.eth.call(extra);
}); return outputParser[displayName][typeName](result);
}, },
transact: function (extra) { transact: function (extra) {
extra = extra || {}; extra = extra || {};
extra.to = address; extra.to = address;
return abi.methodSignature(desc, method.name).then(function (signature) { extra.data = signature + parsed;
extra.data = signature.slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2) + parsed;
/// it's used by natspec.js
/// TODO: figure a better way to solve this
web3._currentContractAbi = desc; web3._currentContractAbi = desc;
web3._currentContractAddress = address; web3._currentContractAddress = address;
return web3.eth.transact(extra).then(onSuccess);
}); var result = web3.eth.transact(extra);
return outputParser[displayName][typeName](result);
} }
}; };
}; };

@ -23,7 +23,7 @@
var HttpSyncProvider = function (host) { var HttpSyncProvider = function (host) {
this.handlers = []; this.handlers = [];
this.host = host; this.host = host || 'http://localhost:8080';
}; };
/// Transforms inner message to proper jsonrpc object /// Transforms inner message to proper jsonrpc object