mirror of https://github.com/status-im/web3.js.git
contract.html example is working with sync api
This commit is contained in:
parent
ceb4357a8d
commit
c9693b4746
|
@ -33,6 +33,9 @@ BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN });
|
|||
|
||||
var ETH_PADDING = 32;
|
||||
|
||||
/// method signature length in bytes
|
||||
var ETH_METHOD_SIGNATURE_LENGTH = 4;
|
||||
|
||||
/// Finds first index of array element matching pattern
|
||||
/// @param array
|
||||
/// @param callback pattern
|
||||
|
@ -390,11 +393,10 @@ var outputParser = function (json) {
|
|||
return parser;
|
||||
};
|
||||
|
||||
/// @param json abi for contract
|
||||
/// @param method name for which we want to get method signature
|
||||
/// @returns (promise) contract method signature for method with given name
|
||||
var methodSignature = function (json, name) {
|
||||
return web3.sha3(web3.fromAscii(name));
|
||||
var methodSignature = function (name) {
|
||||
return web3.sha3(web3.fromAscii(name)).slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
@ -432,8 +434,7 @@ module.exports = {
|
|||
var web3 = require('./web3'); // jshint ignore:line
|
||||
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
|
||||
|
@ -469,29 +470,29 @@ var contract = function (address, desc) {
|
|||
var impl = function () {
|
||||
var params = Array.prototype.slice.call(arguments);
|
||||
var parsed = inputParser[displayName][typeName].apply(null, params);
|
||||
|
||||
var onSuccess = function (result) {
|
||||
return outputParser[displayName][typeName](result);
|
||||
};
|
||||
var signature = abi.methodSignature(method.name);
|
||||
|
||||
return {
|
||||
call: function (extra) {
|
||||
extra = extra || {};
|
||||
extra.to = address;
|
||||
return abi.methodSignature(desc, method.name).then(function (signature) {
|
||||
extra.data = signature.slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2) + parsed;
|
||||
return web3.eth.call(extra).then(onSuccess);
|
||||
});
|
||||
extra.data = signature + parsed;
|
||||
|
||||
var result = web3.eth.call(extra);
|
||||
return outputParser[displayName][typeName](result);
|
||||
},
|
||||
transact: function (extra) {
|
||||
extra = extra || {};
|
||||
extra.to = address;
|
||||
return abi.methodSignature(desc, method.name).then(function (signature) {
|
||||
extra.data = signature.slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2) + parsed;
|
||||
extra.data = signature + parsed;
|
||||
|
||||
/// it's used by natspec.js
|
||||
/// TODO: figure a better way to solve this
|
||||
web3._currentContractAbi = desc;
|
||||
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) {
|
||||
this.handlers = [];
|
||||
this.host = host;
|
||||
this.host = host || 'http://localhost:8080';
|
||||
};
|
||||
|
||||
/// 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 originalBalance = 0;
|
||||
|
||||
web3.eth.balanceAt(coinbase).then(function (balance) {
|
||||
originalBalance = web3.toDecimal(balance);
|
||||
var balance = web3.eth.balanceAt(coinbase);
|
||||
var originalBalance = web3.toDecimal(balance);
|
||||
document.getElementById('original').innerText = 'original balance: ' + originalBalance + ' watching...';
|
||||
});
|
||||
|
||||
web3.eth.watch({altered: coinbase}).changed(function() {
|
||||
web3.eth.balanceAt(coinbase).then(function (balance) {
|
||||
balance = web3.eth.balanceAt(coinbase)
|
||||
var currentBalance = web3.toDecimal(balance);
|
||||
document.getElementById("current").innerText = 'current: ' + currentBalance;
|
||||
document.getElementById("diff").innerText = 'diff: ' + (currentBalance - originalBalance);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
</script>
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<script type="text/javascript">
|
||||
|
||||
var web3 = require('web3');
|
||||
web3.setProvider(new web3.providers.AutoProvider());
|
||||
web3.setProvider(new web3.providers.HttpSyncProvider());
|
||||
|
||||
// solidity source code
|
||||
var source = "" +
|
||||
|
@ -43,10 +43,9 @@
|
|||
document.getElementById('source').innerText = source;
|
||||
|
||||
// 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);
|
||||
document.getElementById('call').style.visibility = 'visible';
|
||||
});
|
||||
}
|
||||
|
||||
function callExampleContract() {
|
||||
|
@ -54,9 +53,8 @@
|
|||
var param = parseInt(document.getElementById('value').value);
|
||||
|
||||
// call the contract
|
||||
contract.multiply(param).call().then(function(res) {
|
||||
var res = contract.multiply(param).call();
|
||||
document.getElementById('result').innerText = res[0];
|
||||
});
|
||||
}
|
||||
|
||||
</script>
|
||||
|
|
|
@ -32,6 +32,9 @@ BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN });
|
|||
|
||||
var ETH_PADDING = 32;
|
||||
|
||||
/// method signature length in bytes
|
||||
var ETH_METHOD_SIGNATURE_LENGTH = 4;
|
||||
|
||||
/// Finds first index of array element matching pattern
|
||||
/// @param array
|
||||
/// @param callback pattern
|
||||
|
@ -389,11 +392,10 @@ var outputParser = function (json) {
|
|||
return parser;
|
||||
};
|
||||
|
||||
/// @param json abi for contract
|
||||
/// @param method name for which we want to get method signature
|
||||
/// @returns (promise) contract method signature for method with given name
|
||||
var methodSignature = function (json, name) {
|
||||
return web3.sha3(web3.fromAscii(name));
|
||||
var methodSignature = function (name) {
|
||||
return web3.sha3(web3.fromAscii(name)).slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
|
|
@ -23,8 +23,7 @@
|
|||
var web3 = require('./web3'); // jshint ignore:line
|
||||
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
|
||||
|
@ -60,29 +59,29 @@ var contract = function (address, desc) {
|
|||
var impl = function () {
|
||||
var params = Array.prototype.slice.call(arguments);
|
||||
var parsed = inputParser[displayName][typeName].apply(null, params);
|
||||
|
||||
var onSuccess = function (result) {
|
||||
return outputParser[displayName][typeName](result);
|
||||
};
|
||||
var signature = abi.methodSignature(method.name);
|
||||
|
||||
return {
|
||||
call: function (extra) {
|
||||
extra = extra || {};
|
||||
extra.to = address;
|
||||
return abi.methodSignature(desc, method.name).then(function (signature) {
|
||||
extra.data = signature.slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2) + parsed;
|
||||
return web3.eth.call(extra).then(onSuccess);
|
||||
});
|
||||
extra.data = signature + parsed;
|
||||
|
||||
var result = web3.eth.call(extra);
|
||||
return outputParser[displayName][typeName](result);
|
||||
},
|
||||
transact: function (extra) {
|
||||
extra = extra || {};
|
||||
extra.to = address;
|
||||
return abi.methodSignature(desc, method.name).then(function (signature) {
|
||||
extra.data = signature.slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2) + parsed;
|
||||
extra.data = signature + parsed;
|
||||
|
||||
/// it's used by natspec.js
|
||||
/// TODO: figure a better way to solve this
|
||||
web3._currentContractAbi = desc;
|
||||
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) {
|
||||
this.handlers = [];
|
||||
this.host = host;
|
||||
this.host = host || 'http://localhost:8080';
|
||||
};
|
||||
|
||||
/// Transforms inner message to proper jsonrpc object
|
||||
|
|
Loading…
Reference in New Issue