mirror of
https://github.com/status-im/web3.js.git
synced 2025-02-24 03:58:13 +00:00
173 lines
5.8 KiB
JavaScript
173 lines
5.8 KiB
JavaScript
/*
|
|
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
|
|
* @author Marek Kotewicz <marek@ethdev.com>
|
|
* @date 2014
|
|
*/
|
|
|
|
var web3 = require('../web3');
|
|
var solAbi = require('../solidity/abi');
|
|
var utils = require('../utils/utils');
|
|
var solUtils = require('../solidity/utils');
|
|
var eventImpl = require('./event');
|
|
var signature = require('./signature');
|
|
var SolidityFunction = require('./function');
|
|
var SolidityEvent = eventImpl.SolidityEvent;
|
|
|
|
var addFunctionsToContract = function (contract, desc) {
|
|
desc.filter(function (json) {
|
|
return json.type === 'function';
|
|
}).map(function (json) {
|
|
return new SolidityFunction(json);
|
|
}).forEach(function (f) {
|
|
f.attachToContract(contract);
|
|
});
|
|
};
|
|
|
|
var addEventRelatedPropertiesToContract = function (contract, desc, address) {
|
|
contract._onWatchEventResult = function (data) {
|
|
var matchingEvent = event.getMatchingEvent(solUtils.filterEvents(desc));
|
|
var parser = eventImpl.outputParser(matchingEvent);
|
|
return parser(data);
|
|
};
|
|
|
|
Object.defineProperty(contract, 'topics', {
|
|
get: function() {
|
|
return solUtils.filterEvents(desc).map(function (e) {
|
|
return signature.eventSignatureFromAscii(e.name);
|
|
});
|
|
}
|
|
});
|
|
|
|
};
|
|
|
|
var addEventsToContract = function (contract, desc, address) {
|
|
desc.filter(function (json) {
|
|
return json.type === 'event';
|
|
}).map(function (json) {
|
|
return new SolidityEvent(json, address);
|
|
}).forEach(function (e) {
|
|
e.attachToContract(contract);
|
|
});
|
|
// create contract events
|
|
//solUtils.filterEvents(desc).forEach(function (e) {
|
|
|
|
//var impl = function () {
|
|
//var params = Array.prototype.slice.call(arguments);
|
|
//var sign = signature.eventSignatureFromAscii(e.name);
|
|
//var event = eventImpl.inputParser(address, sign, e);
|
|
//var o = event.apply(null, params);
|
|
//var outputFormatter = function (data) {
|
|
//var parser = eventImpl.outputParser(e);
|
|
//return parser(data);
|
|
//};
|
|
//return web3.eth.filter(o, undefined, undefined, outputFormatter);
|
|
//};
|
|
|
|
//// this property should be used by eth.filter to check if object is an event
|
|
//impl._isEvent = true;
|
|
|
|
//var displayName = utils.extractDisplayName(e.name);
|
|
//var typeName = utils.extractTypeName(e.name);
|
|
|
|
//if (contract[displayName] === undefined) {
|
|
//contract[displayName] = impl;
|
|
//}
|
|
|
|
//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(abi); // creation of contract prototype
|
|
*
|
|
* var contractInstance = new MyContract('0x0123123121');
|
|
*
|
|
* contractInstance.myMethod('this is test string param for call'); // myMethod call (implicit, default)
|
|
* contractInstance.call().myMethod('this is test string param for call'); // myMethod call (explicit)
|
|
* contractInstance.sendTransaction().myMethod('this is test string param for transact'); // myMethod sendTransaction
|
|
*
|
|
* @param abi - abi json description of the contract, which is being created
|
|
* @returns contract object
|
|
*/
|
|
var contract = function (abi) {
|
|
|
|
// return prototype
|
|
return Contract.bind(null, abi);
|
|
};
|
|
|
|
var Contract = function (abi, options) {
|
|
|
|
// 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?
|
|
abi.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 + ')';
|
|
}
|
|
});
|
|
|
|
this.address = '';
|
|
this._isTransaction = null;
|
|
this._options = {};
|
|
if (utils.isAddress(options)) {
|
|
this.address = options;
|
|
} else { // is an object!
|
|
// TODO, parse the rest of the args
|
|
options = options || {};
|
|
var args = Array.prototype.slice.call(arguments, 2);
|
|
var bytes = solAbi.formatConstructorParams(abi, args);
|
|
options.data += bytes;
|
|
this.address = web3.eth.sendTransaction(options);
|
|
}
|
|
|
|
addFunctionsToContract(this, abi);
|
|
addEventRelatedPropertiesToContract(this, abi, this.address);
|
|
addEventsToContract(this, abi, this.address);
|
|
};
|
|
|
|
Contract.prototype.call = function (options) {
|
|
this._isTransaction = false;
|
|
this._options = options;
|
|
return this;
|
|
};
|
|
|
|
Contract.prototype.sendTransaction = function (options) {
|
|
this._isTransaction = true;
|
|
this._options = options;
|
|
return this;
|
|
};
|
|
|
|
module.exports = contract;
|
|
|