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

44
dist/web3-light.js vendored
View File

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

44
dist/web3.js vendored
View File

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

View File

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

View File

@ -207,7 +207,7 @@ describe('web3.eth.contract', function () {
var Contract = web3.eth.contract(desc);
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 = 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});
});
});
});