implicit solidity method call

This commit is contained in:
Marek Kotewicz 2015-01-22 14:37:34 +01:00
parent 22c77c607e
commit 0202b05a5d
5 changed files with 84 additions and 62 deletions

69
dist/ethereum.js vendored
View File

@ -447,18 +447,20 @@ var abi = require('./abi');
* *
* var myContract = web3.eth.contract('0x0123123121', abi); // creation of contract object * var myContract = web3.eth.contract('0x0123123121', abi); // creation of contract object
* *
* myContract.myMethod('this is test string param for call').call(); // myMethod call * myContract.myMethod('this is test string param for call'); // myMethod call (implicit, default)
* myContract.myMethod('this is test string param for transact').transact() // myMethod transact * myContract.myMethod('this is test string param for call').call(); // myMethod call (explicit)
* myContract.transact().myMethod('this is test string param for transact'); // myMethod transact
* *
* @param address - address of the contract, which should be called * @param address - address of the contract, which should be called
* @param desc - abi json description of the contract, which is being created * @param desc - abi json description of the contract, which is being created
* @returns contract object * @returns contract object
*/ */
var contract = function (address, desc) {
var contract = function contract (address, desc) {
var inputParser = abi.inputParser(desc); var inputParser = abi.inputParser(desc);
var outputParser = abi.outputParser(desc); var outputParser = abi.outputParser(desc);
var contract = {}; var result = {};
desc.forEach(function (method) { desc.forEach(function (method) {
@ -467,44 +469,53 @@ 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 signature = abi.methodSignature(method.name); var signature = abi.methodSignature(method.name);
var parsed = inputParser[displayName][typeName].apply(null, params);
return { var options = contract._options || {};
call: function (extra) { options.to = address;
extra = extra || {}; options.data = signature + parsed;
extra.to = address;
extra.data = signature + parsed;
var result = web3.eth.call(extra); var output = "";
return outputParser[displayName][typeName](result); if (contract._isTransact) {
}, // it's used byt natspec.js
transact: function (extra) { // TODO: figure out better way to solve this
extra = extra || {}; web3._currentContractAbi = desc;
extra.to = address; web3._currentContractAddress = address;
extra.data = signature + parsed;
/// it's used by natspec.js output = web3.eth.transact(options);
/// TODO: figure a better way to solve this } else {
web3._currentContractAbi = desc; output = web3.eth.call(options);
web3._currentContractAddress = address; }
var result = web3.eth.transact(extra); return outputParser[displayName][typeName](output);
return outputParser[displayName][typeName](result);
}
};
}; };
if (contract[displayName] === undefined) { if (result[displayName] === undefined) {
contract[displayName] = impl; result[displayName] = impl;
} }
contract[displayName][typeName] = impl; result[displayName][typeName] = impl;
});
});
return result;
};
var transact = function (options) {
contract._isTransact = true;
contract._options = options;
return contract; return contract;
}; };
var call = function (options) {
contract._isTransact = false;
contract._options = options;
return contract;
};
contract.transact = transact;
contract.call = call;
module.exports = contract; module.exports = contract;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -53,7 +53,7 @@
var param = parseInt(document.getElementById('value').value); var param = parseInt(document.getElementById('value').value);
// call the contract // call the contract
var res = contract.multiply(param).call(); var res = contract.multiply(param);
document.getElementById('result').innerText = res[0]; document.getElementById('result').innerText = res[0];
} }

View File

@ -36,18 +36,20 @@ var abi = require('./abi');
* *
* var myContract = web3.eth.contract('0x0123123121', abi); // creation of contract object * var myContract = web3.eth.contract('0x0123123121', abi); // creation of contract object
* *
* myContract.myMethod('this is test string param for call').call(); // myMethod call * myContract.myMethod('this is test string param for call'); // myMethod call (implicit, default)
* myContract.myMethod('this is test string param for transact').transact() // myMethod transact * myContract.myMethod('this is test string param for call').call(); // myMethod call (explicit)
* myContract.transact().myMethod('this is test string param for transact'); // myMethod transact
* *
* @param address - address of the contract, which should be called * @param address - address of the contract, which should be called
* @param desc - abi json description of the contract, which is being created * @param desc - abi json description of the contract, which is being created
* @returns contract object * @returns contract object
*/ */
var contract = function (address, desc) {
var contract = function contract (address, desc) {
var inputParser = abi.inputParser(desc); var inputParser = abi.inputParser(desc);
var outputParser = abi.outputParser(desc); var outputParser = abi.outputParser(desc);
var contract = {}; var result = {};
desc.forEach(function (method) { desc.forEach(function (method) {
@ -56,43 +58,52 @@ 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 signature = abi.methodSignature(method.name); var signature = abi.methodSignature(method.name);
var parsed = inputParser[displayName][typeName].apply(null, params);
return { var options = contract._options || {};
call: function (extra) { options.to = address;
extra = extra || {}; options.data = signature + parsed;
extra.to = address;
extra.data = signature + parsed;
var result = web3.eth.call(extra); var output = "";
return outputParser[displayName][typeName](result); if (contract._isTransact) {
}, // it's used byt natspec.js
transact: function (extra) { // TODO: figure out better way to solve this
extra = extra || {}; web3._currentContractAbi = desc;
extra.to = address; web3._currentContractAddress = address;
extra.data = signature + parsed;
/// it's used by natspec.js output = web3.eth.transact(options);
/// TODO: figure a better way to solve this } else {
web3._currentContractAbi = desc; output = web3.eth.call(options);
web3._currentContractAddress = address; }
var result = web3.eth.transact(extra); return outputParser[displayName][typeName](output);
return outputParser[displayName][typeName](result);
}
};
}; };
if (contract[displayName] === undefined) { if (result[displayName] === undefined) {
contract[displayName] = impl; result[displayName] = impl;
} }
contract[displayName][typeName] = impl; result[displayName][typeName] = impl;
});
});
return result;
};
var transact = function (options) {
contract._isTransact = true;
contract._options = options;
return contract; return contract;
}; };
var call = function (options) {
contract._isTransact = false;
contract._options = options;
return contract;
};
contract.transact = transact;
contract.call = call;
module.exports = contract; module.exports = contract;