changed the way functions are being called

This commit is contained in:
Marek Kotewicz 2015-04-21 19:41:57 +02:00
parent c8e576893e
commit f84a68cb73
9 changed files with 75 additions and 88 deletions

46
dist/web3-light.js vendored
View File

@ -1603,17 +1603,17 @@ var addFunctionsToContract = function (contract, desc) {
desc.filter(function (json) { desc.filter(function (json) {
return json.type === 'function'; return json.type === 'function';
}).map(function (json) { }).map(function (json) {
return new SolidityFunction(json); return new SolidityFunction(json, contract.address);
}).forEach(function (f) { }).forEach(function (f) {
f.attachToContract(contract); f.attachToContract(contract);
}); });
}; };
var addEventsToContract = function (contract, desc, address) { var addEventsToContract = function (contract, desc) {
desc.filter(function (json) { desc.filter(function (json) {
return json.type === 'event'; return json.type === 'event';
}).map(function (json) { }).map(function (json) {
return new SolidityEvent(json, address); return new SolidityEvent(json, contract.address);
}).forEach(function (e) { }).forEach(function (e) {
e.attachToContract(contract); e.attachToContract(contract);
}); });
@ -1650,8 +1650,6 @@ var contract = function (abi) {
var Contract = function (abi, options) { var Contract = function (abi, options) {
this.address = ''; this.address = '';
this._isTransaction = null;
this._options = {};
if (utils.isAddress(options)) { if (utils.isAddress(options)) {
this.address = options; this.address = options;
} else { // is an object! } else { // is an object!
@ -1664,18 +1662,16 @@ var Contract = function (abi, options) {
} }
addFunctionsToContract(this, abi); addFunctionsToContract(this, abi);
addEventsToContract(this, abi, this.address); addEventsToContract(this, abi);
}; };
Contract.prototype.call = function (options) { Contract.prototype.call = function () {
this._isTransaction = false; console.error('contract.call is deprecated');
this._options = options;
return this; return this;
}; };
Contract.prototype.sendTransaction = function (options) { Contract.prototype.sendTransaction = function () {
this._isTransaction = true; console.error('contract.sendTransact is deprecated');
this._options = options;
return this; return this;
}; };
@ -2599,7 +2595,7 @@ var utils = require('../utils/utils');
/** /**
* This prototype should be used to call/sendTransaction to solidity functions * This prototype should be used to call/sendTransaction to solidity functions
*/ */
var SolidityFunction = function (json) { var SolidityFunction = function (json, address) {
this._inputTypes = json.inputs.map(function (i) { this._inputTypes = json.inputs.map(function (i) {
return i.type; return i.type;
}); });
@ -2608,6 +2604,7 @@ var SolidityFunction = function (json) {
}); });
this._constant = json.constant; this._constant = json.constant;
this._name = utils.transformToFullName(json); this._name = utils.transformToFullName(json);
this._address = address;
}; };
/** /**
@ -2666,17 +2663,16 @@ SolidityFunction.prototype.typeName = function () {
* *
* @method execute * @method execute
*/ */
SolidityFunction.prototype.execute = function (contract) { SolidityFunction.prototype.execute = function () {
var args = Array.prototype.slice.call(arguments, 1); var args = Array.prototype.slice.call(arguments);
var options = contract._options || {}; var options = {};
options.to = contract.address; if (utils.isObject(args[args.length -1])) {
options = args.pop();
}
options.to = this._address;
options.data = '0x' + this.signature() + coder.encodeParams(this._inputTypes, args); options.data = '0x' + this.signature() + coder.encodeParams(this._inputTypes, args);
var transaction = contract._isTransaction === true || (contract._isTransaction !== false && !this._constant); var transaction = !this._constant;
//reset
contract._options = {};
contract._isTransaction = null;
// send transaction // send transaction
if (transaction) { if (transaction) {
return this.sendTransaction(options); return this.sendTransaction(options);
@ -2694,12 +2690,12 @@ SolidityFunction.prototype.execute = function (contract) {
* @param {Contract} * @param {Contract}
*/ */
SolidityFunction.prototype.attachToContract = function (contract) { SolidityFunction.prototype.attachToContract = function (contract) {
var execute = this.execute.bind(this, contract); var execute = this.execute.bind(this);
var displayName = this.displayName(); var displayName = this.displayName();
if (!contract[displayName]) { if (!contract[displayName]) {
contract[displayName] = execute; contract[displayName] = execute;
} }
contract[displayName][this.typeName()] = this.execute.bind(this, contract); contract[displayName][this.typeName()] = this.execute.bind(this);
}; };
module.exports = SolidityFunction; module.exports = SolidityFunction;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

46
dist/web3.js vendored
View File

@ -1603,17 +1603,17 @@ var addFunctionsToContract = function (contract, desc) {
desc.filter(function (json) { desc.filter(function (json) {
return json.type === 'function'; return json.type === 'function';
}).map(function (json) { }).map(function (json) {
return new SolidityFunction(json); return new SolidityFunction(json, contract.address);
}).forEach(function (f) { }).forEach(function (f) {
f.attachToContract(contract); f.attachToContract(contract);
}); });
}; };
var addEventsToContract = function (contract, desc, address) { var addEventsToContract = function (contract, desc) {
desc.filter(function (json) { desc.filter(function (json) {
return json.type === 'event'; return json.type === 'event';
}).map(function (json) { }).map(function (json) {
return new SolidityEvent(json, address); return new SolidityEvent(json, contract.address);
}).forEach(function (e) { }).forEach(function (e) {
e.attachToContract(contract); e.attachToContract(contract);
}); });
@ -1650,8 +1650,6 @@ var contract = function (abi) {
var Contract = function (abi, options) { var Contract = function (abi, options) {
this.address = ''; this.address = '';
this._isTransaction = null;
this._options = {};
if (utils.isAddress(options)) { if (utils.isAddress(options)) {
this.address = options; this.address = options;
} else { // is an object! } else { // is an object!
@ -1664,18 +1662,16 @@ var Contract = function (abi, options) {
} }
addFunctionsToContract(this, abi); addFunctionsToContract(this, abi);
addEventsToContract(this, abi, this.address); addEventsToContract(this, abi);
}; };
Contract.prototype.call = function (options) { Contract.prototype.call = function () {
this._isTransaction = false; console.error('contract.call is deprecated');
this._options = options;
return this; return this;
}; };
Contract.prototype.sendTransaction = function (options) { Contract.prototype.sendTransaction = function () {
this._isTransaction = true; console.error('contract.sendTransact is deprecated');
this._options = options;
return this; return this;
}; };
@ -2599,7 +2595,7 @@ var utils = require('../utils/utils');
/** /**
* This prototype should be used to call/sendTransaction to solidity functions * This prototype should be used to call/sendTransaction to solidity functions
*/ */
var SolidityFunction = function (json) { var SolidityFunction = function (json, address) {
this._inputTypes = json.inputs.map(function (i) { this._inputTypes = json.inputs.map(function (i) {
return i.type; return i.type;
}); });
@ -2608,6 +2604,7 @@ var SolidityFunction = function (json) {
}); });
this._constant = json.constant; this._constant = json.constant;
this._name = utils.transformToFullName(json); this._name = utils.transformToFullName(json);
this._address = address;
}; };
/** /**
@ -2666,17 +2663,16 @@ SolidityFunction.prototype.typeName = function () {
* *
* @method execute * @method execute
*/ */
SolidityFunction.prototype.execute = function (contract) { SolidityFunction.prototype.execute = function () {
var args = Array.prototype.slice.call(arguments, 1); var args = Array.prototype.slice.call(arguments);
var options = contract._options || {}; var options = {};
options.to = contract.address; if (utils.isObject(args[args.length -1])) {
options = args.pop();
}
options.to = this._address;
options.data = '0x' + this.signature() + coder.encodeParams(this._inputTypes, args); options.data = '0x' + this.signature() + coder.encodeParams(this._inputTypes, args);
var transaction = contract._isTransaction === true || (contract._isTransaction !== false && !this._constant); var transaction = !this._constant;
//reset
contract._options = {};
contract._isTransaction = null;
// send transaction // send transaction
if (transaction) { if (transaction) {
return this.sendTransaction(options); return this.sendTransaction(options);
@ -2694,12 +2690,12 @@ SolidityFunction.prototype.execute = function (contract) {
* @param {Contract} * @param {Contract}
*/ */
SolidityFunction.prototype.attachToContract = function (contract) { SolidityFunction.prototype.attachToContract = function (contract) {
var execute = this.execute.bind(this, contract); var execute = this.execute.bind(this);
var displayName = this.displayName(); var displayName = this.displayName();
if (!contract[displayName]) { if (!contract[displayName]) {
contract[displayName] = execute; contract[displayName] = execute;
} }
contract[displayName][this.typeName()] = this.execute.bind(this, contract); contract[displayName][this.typeName()] = this.execute.bind(this);
}; };
module.exports = SolidityFunction; module.exports = SolidityFunction;

6
dist/web3.js.map vendored

File diff suppressed because one or more lines are too long

5
dist/web3.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -30,17 +30,17 @@ var addFunctionsToContract = function (contract, desc) {
desc.filter(function (json) { desc.filter(function (json) {
return json.type === 'function'; return json.type === 'function';
}).map(function (json) { }).map(function (json) {
return new SolidityFunction(json); return new SolidityFunction(json, contract.address);
}).forEach(function (f) { }).forEach(function (f) {
f.attachToContract(contract); f.attachToContract(contract);
}); });
}; };
var addEventsToContract = function (contract, desc, address) { var addEventsToContract = function (contract, desc) {
desc.filter(function (json) { desc.filter(function (json) {
return json.type === 'event'; return json.type === 'event';
}).map(function (json) { }).map(function (json) {
return new SolidityEvent(json, address); return new SolidityEvent(json, contract.address);
}).forEach(function (e) { }).forEach(function (e) {
e.attachToContract(contract); e.attachToContract(contract);
}); });
@ -77,8 +77,6 @@ var contract = function (abi) {
var Contract = function (abi, options) { var Contract = function (abi, options) {
this.address = ''; this.address = '';
this._isTransaction = null;
this._options = {};
if (utils.isAddress(options)) { if (utils.isAddress(options)) {
this.address = options; this.address = options;
} else { // is an object! } else { // is an object!
@ -91,18 +89,16 @@ var Contract = function (abi, options) {
} }
addFunctionsToContract(this, abi); addFunctionsToContract(this, abi);
addEventsToContract(this, abi, this.address); addEventsToContract(this, abi);
}; };
Contract.prototype.call = function (options) { Contract.prototype.call = function () {
this._isTransaction = false; console.error('contract.call is deprecated');
this._options = options;
return this; return this;
}; };
Contract.prototype.sendTransaction = function (options) { Contract.prototype.sendTransaction = function () {
this._isTransaction = true; console.error('contract.sendTransact is deprecated');
this._options = options;
return this; return this;
}; };

View File

@ -27,7 +27,7 @@ var utils = require('../utils/utils');
/** /**
* This prototype should be used to call/sendTransaction to solidity functions * This prototype should be used to call/sendTransaction to solidity functions
*/ */
var SolidityFunction = function (json) { var SolidityFunction = function (json, address) {
this._inputTypes = json.inputs.map(function (i) { this._inputTypes = json.inputs.map(function (i) {
return i.type; return i.type;
}); });
@ -36,6 +36,7 @@ var SolidityFunction = function (json) {
}); });
this._constant = json.constant; this._constant = json.constant;
this._name = utils.transformToFullName(json); this._name = utils.transformToFullName(json);
this._address = address;
}; };
/** /**
@ -94,17 +95,16 @@ SolidityFunction.prototype.typeName = function () {
* *
* @method execute * @method execute
*/ */
SolidityFunction.prototype.execute = function (contract) { SolidityFunction.prototype.execute = function () {
var args = Array.prototype.slice.call(arguments, 1); var args = Array.prototype.slice.call(arguments);
var options = contract._options || {}; var options = {};
options.to = contract.address; if (utils.isObject(args[args.length -1])) {
options = args.pop();
}
options.to = this._address;
options.data = '0x' + this.signature() + coder.encodeParams(this._inputTypes, args); options.data = '0x' + this.signature() + coder.encodeParams(this._inputTypes, args);
var transaction = contract._isTransaction === true || (contract._isTransaction !== false && !this._constant); var transaction = !this._constant;
//reset
contract._options = {};
contract._isTransaction = null;
// send transaction // send transaction
if (transaction) { if (transaction) {
return this.sendTransaction(options); return this.sendTransaction(options);
@ -122,12 +122,12 @@ SolidityFunction.prototype.execute = function (contract) {
* @param {Contract} * @param {Contract}
*/ */
SolidityFunction.prototype.attachToContract = function (contract) { SolidityFunction.prototype.attachToContract = function (contract) {
var execute = this.execute.bind(this, contract); var execute = this.execute.bind(this);
var displayName = this.displayName(); var displayName = this.displayName();
if (!contract[displayName]) { if (!contract[displayName]) {
contract[displayName] = execute; contract[displayName] = execute;
} }
contract[displayName][this.typeName()] = this.execute.bind(this, contract); contract[displayName][this.typeName()] = this.execute.bind(this);
}; };
module.exports = SolidityFunction; module.exports = SolidityFunction;

View File

@ -207,7 +207,7 @@ describe('web3.eth.contract', function () {
var Contract = web3.eth.contract(desc); var Contract = web3.eth.contract(desc);
var contract = new Contract(address); var contract = new Contract(address);
contract.call({from: address, gas: 50000}).balance(address); contract.balance(address, {from: address, gas: 50000});
}); });
@ -243,7 +243,7 @@ describe('web3.eth.contract', function () {
var Contract = web3.eth.contract(desc); var Contract = web3.eth.contract(desc);
var contract = new Contract(address); var contract = new Contract(address);
contract.sendTransaction({from: address, gas: 50000, gasPrice: 3000, value: 10000}).send(address, 17); contract.send(address, 17, {from: address, gas: 50000, gasPrice: 3000, value: 10000});
}); });
}); });
}); });