You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
219 lines
7.3 KiB
219 lines
7.3 KiB
10 years ago
|
/*
|
||
|
This file is part of ethereum.js.
|
||
|
|
||
|
ethereum.js is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU Lesser General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
ethereum.js is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU Lesser General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Lesser General Public License
|
||
|
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
/** @file contract.js
|
||
|
* @authors:
|
||
|
* Marek Kotewicz <marek@ethdev.com>
|
||
|
* @date 2014
|
||
|
*/
|
||
|
|
||
10 years ago
|
var web3 = require('./web3');
|
||
10 years ago
|
var abi = require('./abi');
|
||
10 years ago
|
var utils = require('./utils');
|
||
10 years ago
|
var eventImpl = require('./event');
|
||
|
|
||
10 years ago
|
var exportNatspecGlobals = function (vars) {
|
||
|
// it's used byt natspec.js
|
||
|
// TODO: figure out better way to solve this
|
||
|
web3._currentContractAbi = vars.abi;
|
||
|
web3._currentContractAddress = vars.address;
|
||
|
web3._currentContractMethodName = vars.method;
|
||
|
web3._currentContractMethodParams = vars.params;
|
||
|
};
|
||
|
|
||
10 years ago
|
var addFunctionRelatedPropertiesToContract = function (contract) {
|
||
|
|
||
|
contract.call = function (options) {
|
||
|
contract._isTransact = false;
|
||
|
contract._options = options;
|
||
|
return contract;
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
contract.transact = function (options) {
|
||
|
contract._isTransact = true;
|
||
|
contract._options = options;
|
||
|
return contract;
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
contract._options = {};
|
||
10 years ago
|
['gas', 'gasPrice', 'value', 'from'].forEach(function(p) {
|
||
10 years ago
|
contract[p] = function (v) {
|
||
|
contract._options[p] = v;
|
||
|
return contract;
|
||
10 years ago
|
};
|
||
|
});
|
||
|
|
||
10 years ago
|
};
|
||
10 years ago
|
|
||
10 years ago
|
var addFunctionsToContract = function (contract, desc, address) {
|
||
|
var inputParser = abi.inputParser(desc);
|
||
|
var outputParser = abi.outputParser(desc);
|
||
|
|
||
|
// create contract functions
|
||
10 years ago
|
utils.filterFunctions(desc).forEach(function (method) {
|
||
10 years ago
|
|
||
10 years ago
|
var displayName = utils.extractDisplayName(method.name);
|
||
|
var typeName = utils.extractTypeName(method.name);
|
||
10 years ago
|
|
||
|
var impl = function () {
|
||
10 years ago
|
/*jshint maxcomplexity:7 */
|
||
10 years ago
|
var params = Array.prototype.slice.call(arguments);
|
||
10 years ago
|
var signature = abi.signatureFromAscii(method.name);
|
||
10 years ago
|
var parsed = inputParser[displayName][typeName].apply(null, params);
|
||
|
|
||
10 years ago
|
var options = contract._options || {};
|
||
10 years ago
|
options.to = address;
|
||
|
options.data = signature + parsed;
|
||
10 years ago
|
|
||
10 years ago
|
var isTransact = contract._isTransact === true || (contract._isTransact !== false && !method.constant);
|
||
10 years ago
|
var collapse = options.collapse !== false;
|
||
10 years ago
|
|
||
|
// reset
|
||
10 years ago
|
contract._options = {};
|
||
|
contract._isTransact = null;
|
||
10 years ago
|
|
||
10 years ago
|
if (isTransact) {
|
||
10 years ago
|
|
||
|
exportNatspecGlobals({
|
||
|
abi: desc,
|
||
|
address: address,
|
||
|
method: method.name,
|
||
|
params: params
|
||
|
});
|
||
10 years ago
|
|
||
10 years ago
|
// transactions do not have any output, cause we do not know, when they will be processed
|
||
|
web3.eth.transact(options);
|
||
|
return;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
var output = web3.eth.call(options);
|
||
10 years ago
|
var ret = outputParser[displayName][typeName](output);
|
||
|
if (collapse)
|
||
|
{
|
||
10 years ago
|
if (ret.length === 1)
|
||
10 years ago
|
ret = ret[0];
|
||
10 years ago
|
else if (ret.length === 0)
|
||
10 years ago
|
ret = null;
|
||
|
}
|
||
|
return ret;
|
||
10 years ago
|
};
|
||
10 years ago
|
|
||
10 years ago
|
if (contract[displayName] === undefined) {
|
||
|
contract[displayName] = impl;
|
||
|
}
|
||
|
|
||
|
contract[displayName][typeName] = impl;
|
||
|
});
|
||
|
};
|
||
|
|
||
|
var addEventRelatedPropertiesToContract = function (contract, desc, address) {
|
||
|
contract.address = address;
|
||
10 years ago
|
contract._onWatchEventResult = function (data) {
|
||
|
var matchingEvent = event.getMatchingEvent(utils.filterEvents(desc));
|
||
|
var parser = eventImpl.outputParser(matchingEvent);
|
||
|
return parser(data);
|
||
|
};
|
||
10 years ago
|
|
||
10 years ago
|
Object.defineProperty(contract, 'topic', {
|
||
10 years ago
|
get: function() {
|
||
10 years ago
|
return utils.filterEvents(desc).map(function (e) {
|
||
10 years ago
|
return abi.eventSignatureFromAscii(e.name);
|
||
10 years ago
|
});
|
||
|
}
|
||
|
});
|
||
|
|
||
|
};
|
||
|
|
||
|
var addEventsToContract = function (contract, desc, address) {
|
||
|
// create contract events
|
||
10 years ago
|
utils.filterEvents(desc).forEach(function (e) {
|
||
10 years ago
|
|
||
|
var impl = function () {
|
||
|
var params = Array.prototype.slice.call(arguments);
|
||
10 years ago
|
var signature = abi.eventSignatureFromAscii(e.name);
|
||
10 years ago
|
var event = eventImpl.inputParser(address, signature, e);
|
||
10 years ago
|
var o = event.apply(null, params);
|
||
10 years ago
|
var outputFormatter = function (data) {
|
||
10 years ago
|
var parser = eventImpl.outputParser(e);
|
||
|
return parser(data);
|
||
|
};
|
||
10 years ago
|
return web3.eth.watch(o, undefined, undefined, outputFormatter);
|
||
10 years ago
|
};
|
||
10 years ago
|
|
||
|
// this property should be used by eth.filter to check if object is an event
|
||
|
impl._isEvent = true;
|
||
10 years ago
|
|
||
10 years ago
|
var displayName = utils.extractDisplayName(e.name);
|
||
|
var typeName = utils.extractTypeName(e.name);
|
||
10 years ago
|
|
||
|
if (contract[displayName] === undefined) {
|
||
|
contract[displayName] = impl;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
contract[displayName][typeName] = impl;
|
||
|
|
||
|
});
|
||
|
};
|
||
|
|
||
|
|
||
|
/**
|
||
|
* This method should be called when we want to call / transact some solidity method from javascript
|
||
|
* it returns an object which has same methods available as solidity contract description
|
||
|
* usage example:
|
||
|
*
|
||
|
* var abi = [{
|
||
|
* name: 'myMethod',
|
||
|
* inputs: [{ name: 'a', type: 'string' }],
|
||
|
* outputs: [{name: 'd', type: 'string' }]
|
||
|
* }]; // contract abi
|
||
|
*
|
||
|
* var myContract = web3.eth.contract('0x0123123121', abi); // creation of contract object
|
||
|
*
|
||
|
* myContract.myMethod('this is test string param for call'); // myMethod call (implicit, default)
|
||
|
* myContract.call().myMethod('this is test string param for 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 desc - abi json description of the contract, which is being created
|
||
|
* @returns contract object
|
||
|
*/
|
||
|
|
||
|
var contract = function (address, desc) {
|
||
10 years ago
|
|
||
10 years ago
|
// workaround for invalid assumption that method.name is the full anonymous prototype of the method.
|
||
|
// it's not. it's just the name. the rest of the code assumes it's actually the anonymous
|
||
|
// prototype, so we make it so as a workaround.
|
||
|
// TODO: we may not want to modify input params, maybe use copy instead?
|
||
|
desc.forEach(function (method) {
|
||
|
if (method.name.indexOf('(') === -1) {
|
||
|
var displayName = method.name;
|
||
|
var typeName = method.inputs.map(function(i){return i.type; }).join();
|
||
|
method.name = displayName + '(' + typeName + ')';
|
||
|
}
|
||
10 years ago
|
});
|
||
10 years ago
|
|
||
10 years ago
|
var result = {};
|
||
|
addFunctionRelatedPropertiesToContract(result);
|
||
|
addFunctionsToContract(result, desc, address);
|
||
|
addEventRelatedPropertiesToContract(result, desc, address);
|
||
|
addEventsToContract(result, desc, address);
|
||
|
|
||
10 years ago
|
return result;
|
||
10 years ago
|
};
|
||
|
|
||
|
module.exports = contract;
|
||
10 years ago
|
|