2014-11-17 14:46:46 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2015-04-20 19:59:35 +00:00
|
|
|
/**
|
|
|
|
* @file contract.js
|
|
|
|
* @author Marek Kotewicz <marek@ethdev.com>
|
2014-11-17 14:46:46 +00:00
|
|
|
* @date 2014
|
|
|
|
*/
|
|
|
|
|
2015-03-08 17:18:52 +00:00
|
|
|
var web3 = require('../web3');
|
|
|
|
var utils = require('../utils/utils');
|
2015-05-11 16:02:15 +00:00
|
|
|
var coder = require('../solidity/coder');
|
2015-04-20 20:06:49 +00:00
|
|
|
var SolidityEvent = require('./event');
|
2015-04-20 16:16:15 +00:00
|
|
|
var SolidityFunction = require('./function');
|
2014-11-17 14:46:46 +00:00
|
|
|
|
2015-05-11 16:02:15 +00:00
|
|
|
/**
|
|
|
|
* Should be called to encode constructor params
|
|
|
|
*
|
|
|
|
* @method encodeConstructorParams
|
|
|
|
* @param {Array} abi
|
|
|
|
* @param {Array} constructor params
|
|
|
|
*/
|
|
|
|
var encodeConstructorParams = function (abi, params) {
|
|
|
|
return abi.filter(function (json) {
|
|
|
|
return json.type === 'constructor' && json.inputs.length === params.length;
|
|
|
|
}).map(function (json) {
|
|
|
|
return json.inputs.map(function (input) {
|
|
|
|
return input.type;
|
|
|
|
});
|
|
|
|
}).map(function (types) {
|
|
|
|
return coder.encodeParams(types, params);
|
2015-05-14 10:53:47 +00:00
|
|
|
})[0] || '';
|
2015-05-11 16:02:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should be called to add functions to contract object
|
|
|
|
*
|
|
|
|
* @method addFunctionsToContract
|
|
|
|
* @param {Contract} contract
|
|
|
|
* @param {Array} abi
|
|
|
|
*/
|
|
|
|
var addFunctionsToContract = function (contract, abi) {
|
|
|
|
abi.filter(function (json) {
|
2015-04-20 16:16:15 +00:00
|
|
|
return json.type === 'function';
|
|
|
|
}).map(function (json) {
|
2015-04-21 17:41:57 +00:00
|
|
|
return new SolidityFunction(json, contract.address);
|
2015-04-20 16:16:15 +00:00
|
|
|
}).forEach(function (f) {
|
|
|
|
f.attachToContract(contract);
|
2014-11-17 14:46:46 +00:00
|
|
|
});
|
2015-01-29 12:32:32 +00:00
|
|
|
};
|
2015-01-22 13:37:34 +00:00
|
|
|
|
2015-05-11 16:02:15 +00:00
|
|
|
/**
|
|
|
|
* Should be called to add events to contract object
|
|
|
|
*
|
|
|
|
* @method addEventsToContract
|
|
|
|
* @param {Contract} contract
|
|
|
|
* @param {Array} abi
|
|
|
|
*/
|
|
|
|
var addEventsToContract = function (contract, abi) {
|
|
|
|
abi.filter(function (json) {
|
2015-04-20 19:59:35 +00:00
|
|
|
return json.type === 'event';
|
|
|
|
}).map(function (json) {
|
2015-04-21 17:41:57 +00:00
|
|
|
return new SolidityEvent(json, contract.address);
|
2015-04-20 19:59:35 +00:00
|
|
|
}).forEach(function (e) {
|
|
|
|
e.attachToContract(contract);
|
|
|
|
});
|
2015-01-29 12:32:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-05-11 16:02:15 +00:00
|
|
|
* Should be called to create new ContractFactory
|
2015-01-29 12:32:32 +00:00
|
|
|
*
|
2015-05-11 16:02:15 +00:00
|
|
|
* @method contract
|
|
|
|
* @param {Array} abi
|
|
|
|
* @returns {ContractFactory} new contract factory
|
2015-01-29 12:32:32 +00:00
|
|
|
*/
|
2015-02-24 09:36:54 +00:00
|
|
|
var contract = function (abi) {
|
2015-05-11 16:02:15 +00:00
|
|
|
return new ContractFactory(abi);
|
|
|
|
};
|
2015-02-24 09:36:54 +00:00
|
|
|
|
2015-05-11 16:02:15 +00:00
|
|
|
/**
|
|
|
|
* Should be called to create new ContractFactory instance
|
|
|
|
*
|
|
|
|
* @method ContractFactory
|
|
|
|
* @param {Array} abi
|
|
|
|
*/
|
|
|
|
var ContractFactory = function (abi) {
|
|
|
|
this.abi = abi;
|
2015-02-24 09:36:54 +00:00
|
|
|
};
|
|
|
|
|
2015-05-11 16:02:15 +00:00
|
|
|
/**
|
|
|
|
* Should be called to create new contract on a blockchain
|
|
|
|
*
|
|
|
|
* @method new
|
|
|
|
* @param {Any} contract constructor param1 (optional)
|
|
|
|
* @param {Any} contract constructor param2 (optional)
|
|
|
|
* @param {Object} contract transaction object (required)
|
|
|
|
* @param {Function} callback
|
|
|
|
* @returns {Contract} returns contract if no callback was passed,
|
|
|
|
* otherwise calls callback function (err, contract)
|
|
|
|
*/
|
|
|
|
ContractFactory.prototype.new = function () {
|
|
|
|
// parse arguments
|
|
|
|
var options = {}; // required!
|
|
|
|
var callback;
|
|
|
|
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
if (utils.isFunction(args[args.length - 1])) {
|
|
|
|
callback = args.pop();
|
2015-04-09 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 16:02:15 +00:00
|
|
|
var last = args[args.length - 1];
|
|
|
|
if (utils.isObject(last) && !utils.isArray(last)) {
|
|
|
|
options = args.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// throw an error if there are no options
|
|
|
|
|
2015-05-14 10:53:47 +00:00
|
|
|
var bytes = encodeConstructorParams(this.abi, args);
|
2015-05-11 16:02:15 +00:00
|
|
|
options.data += bytes;
|
|
|
|
|
|
|
|
if (!callback) {
|
|
|
|
var address = web3.eth.sendTransaction(options);
|
2015-05-12 08:41:36 +00:00
|
|
|
return this.at(address);
|
2015-05-11 16:02:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
web3.eth.sendTransaction(options, function (err, address) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
self.at(address, callback);
|
|
|
|
});
|
2015-04-20 16:16:15 +00:00
|
|
|
};
|
2014-11-17 14:46:46 +00:00
|
|
|
|
2015-05-11 16:02:15 +00:00
|
|
|
/**
|
|
|
|
* Should be called to get access to existing contract on a blockchain
|
|
|
|
*
|
|
|
|
* @method at
|
|
|
|
* @param {Address} contract address (required)
|
|
|
|
* @param {Function} callback {optional)
|
|
|
|
* @returns {Contract} returns contract if no callback was passed,
|
|
|
|
* otherwise calls callback function (err, contract)
|
|
|
|
*/
|
|
|
|
ContractFactory.prototype.at = function (address, callback) {
|
|
|
|
// TODO: address is required
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
callback(null, new Contract(this.abi, address));
|
|
|
|
}
|
|
|
|
return new Contract(this.abi, address);
|
2015-04-20 19:59:35 +00:00
|
|
|
};
|
|
|
|
|
2015-05-11 16:02:15 +00:00
|
|
|
/**
|
|
|
|
* Should be called to create new contract instance
|
|
|
|
*
|
|
|
|
* @method Contract
|
|
|
|
* @param {Array} abi
|
|
|
|
* @param {Address} contract address
|
|
|
|
*/
|
|
|
|
var Contract = function (abi, address) {
|
|
|
|
this.address = address;
|
|
|
|
addFunctionsToContract(this, abi);
|
|
|
|
addEventsToContract(this, abi);
|
2015-04-20 19:59:35 +00:00
|
|
|
};
|
|
|
|
|
2014-11-17 14:46:46 +00:00
|
|
|
module.exports = contract;
|
2015-01-14 11:01:11 +00:00
|
|
|
|