mirror of https://github.com/embarklabs/embark.git
fixes due to bad rebase
This commit is contained in:
parent
fdb2d4aed3
commit
2e3102444d
403
js/embark.js
403
js/embark.js
|
@ -1,403 +0,0 @@
|
|||
var EmbarkJS = {
|
||||
onReady: function (cb) {
|
||||
if (typeof (__embarkContext) === 'undefined') {
|
||||
return cb();
|
||||
}
|
||||
return __embarkContext.execWhenReady(cb);
|
||||
}
|
||||
};
|
||||
|
||||
EmbarkJS.isNewWeb3 = function (web3Obj) {
|
||||
var _web3 = web3Obj || (new Web3());
|
||||
if (typeof(_web3.version) === "string") {
|
||||
return true;
|
||||
}
|
||||
return parseInt(_web3.version.api.split('.')[0], 10) >= 1;
|
||||
};
|
||||
|
||||
EmbarkJS.Contract = function (options) {
|
||||
var self = this;
|
||||
var i, abiElement;
|
||||
var ContractClass;
|
||||
|
||||
this.abi = options.abi;
|
||||
this.address = options.address;
|
||||
this.gas = options.gas;
|
||||
this.code = '0x' + options.code;
|
||||
//this.web3 = options.web3 || web3;
|
||||
this.web3 = options.web3;
|
||||
if (!this.web3 && typeof (web3) !== 'undefined') {
|
||||
this.web3 = web3;
|
||||
} else if (!this.web3) {
|
||||
this.web3 = window.web3;
|
||||
}
|
||||
|
||||
if (EmbarkJS.isNewWeb3(this.web3)) {
|
||||
ContractClass = new this.web3.eth.Contract(this.abi, this.address);
|
||||
ContractClass.setProvider(this.web3.currentProvider);
|
||||
ContractClass.options.data = this.code;
|
||||
ContractClass.options.from = this.from;
|
||||
ContractClass.abi = ContractClass.options.abi;
|
||||
ContractClass.address = this.address;
|
||||
ContractClass.gas = this.gas;
|
||||
|
||||
let originalMethods = Object.keys(ContractClass);
|
||||
|
||||
ContractClass._jsonInterface.forEach((abi) => {
|
||||
if (originalMethods.indexOf(abi.name) >= 0) {
|
||||
console.log(abi.name + " is a reserved word and cannot be used as a contract method, property or event");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!abi.inputs) {
|
||||
return;
|
||||
}
|
||||
|
||||
let numExpectedInputs = abi.inputs.length;
|
||||
|
||||
if (abi.type === 'function' && abi.constant) {
|
||||
ContractClass[abi.name] = function () {
|
||||
let options = {}, cb = null, args = Array.from(arguments || []).slice(0, numExpectedInputs);
|
||||
if (typeof (arguments[numExpectedInputs]) === 'function') {
|
||||
cb = arguments[numExpectedInputs];
|
||||
} else if (typeof (arguments[numExpectedInputs]) === 'object') {
|
||||
options = arguments[numExpectedInputs];
|
||||
cb = arguments[numExpectedInputs + 1];
|
||||
}
|
||||
|
||||
let ref = ContractClass.methods[abi.name];
|
||||
let call = ref.apply(ref, ...arguments).call;
|
||||
return call.apply(call, []);
|
||||
};
|
||||
} else if (abi.type === 'function') {
|
||||
ContractClass[abi.name] = function () {
|
||||
let options = {}, cb = null, args = Array.from(arguments || []).slice(0, numExpectedInputs);
|
||||
if (typeof (arguments[numExpectedInputs]) === 'function') {
|
||||
cb = arguments[numExpectedInputs];
|
||||
} else if (typeof (arguments[numExpectedInputs]) === 'object') {
|
||||
options = arguments[numExpectedInputs];
|
||||
cb = arguments[numExpectedInputs + 1];
|
||||
}
|
||||
|
||||
let ref = ContractClass.methods[abi.name];
|
||||
let send = ref.apply(ref, args).send;
|
||||
return send.apply(send, [options, cb]);
|
||||
};
|
||||
} else if (abi.type === 'event') {
|
||||
ContractClass[abi.name] = function (options, cb) {
|
||||
let ref = ContractClass.events[abi.name];
|
||||
return ref.apply(ref, [options, cb]);
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
return ContractClass;
|
||||
} else {
|
||||
ContractClass = this.web3.eth.contract(this.abi);
|
||||
|
||||
this.eventList = [];
|
||||
|
||||
if (this.abi) {
|
||||
for (i = 0; i < this.abi.length; i++) {
|
||||
abiElement = this.abi[i];
|
||||
if (abiElement.type === 'event') {
|
||||
this.eventList.push(abiElement.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var messageEvents = function () {
|
||||
this.cb = function () {
|
||||
};
|
||||
};
|
||||
|
||||
messageEvents.prototype.then = function (cb) {
|
||||
this.cb = cb;
|
||||
};
|
||||
|
||||
messageEvents.prototype.error = function (err) {
|
||||
return err;
|
||||
};
|
||||
|
||||
this._originalContractObject = ContractClass.at(this.address);
|
||||
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) {
|
||||
// TODO: check for forbidden properties
|
||||
if (self.eventList.indexOf(p) >= 0) {
|
||||
|
||||
self[p] = function () {
|
||||
var promise = new messageEvents();
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
args.push(function (err, result) {
|
||||
if (err) {
|
||||
promise.error(err);
|
||||
} else {
|
||||
promise.cb(result);
|
||||
}
|
||||
});
|
||||
|
||||
self._originalContractObject[p].apply(self._originalContractObject[p], args);
|
||||
return promise;
|
||||
};
|
||||
return true;
|
||||
} else if (typeof self._originalContractObject[p] === 'function') {
|
||||
self[p] = function (_args) {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var fn = self._originalContractObject[p];
|
||||
var props = self.abi.find((x) => x.name == p);
|
||||
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
args.push(function (err, transaction) {
|
||||
promise.tx = transaction;
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
var getConfirmation = function () {
|
||||
self.web3.eth.getTransactionReceipt(transaction, function (err, receipt) {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
if (receipt !== null) {
|
||||
return resolve(receipt);
|
||||
}
|
||||
|
||||
setTimeout(getConfirmation, 1000);
|
||||
});
|
||||
};
|
||||
|
||||
if (typeof transaction !== "string" || props.constant) {
|
||||
resolve(transaction);
|
||||
} else {
|
||||
getConfirmation();
|
||||
}
|
||||
});
|
||||
|
||||
fn.apply(fn, args);
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
EmbarkJS.Contract.prototype.deploy = function (args, _options) {
|
||||
var self = this;
|
||||
var contractParams;
|
||||
var options = _options || {};
|
||||
|
||||
contractParams = args || [];
|
||||
|
||||
contractParams.push({
|
||||
from: this.web3.eth.accounts[0],
|
||||
data: this.code,
|
||||
gas: options.gas || 800000
|
||||
});
|
||||
|
||||
var contractObject = this.web3.eth.contract(this.abi);
|
||||
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
contractParams.push(function (err, transaction) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else if (transaction.address !== undefined) {
|
||||
resolve(new EmbarkJS.Contract({
|
||||
abi: self.abi,
|
||||
code: self.code,
|
||||
address: transaction.address
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
// returns promise
|
||||
// deploys contract
|
||||
// wraps it around EmbarkJS.Contract
|
||||
contractObject["new"].apply(contractObject, contractParams);
|
||||
});
|
||||
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Contract.prototype.new = EmbarkJS.Contract.prototype.deploy;
|
||||
|
||||
EmbarkJS.Contract.prototype.at = function (address) {
|
||||
return new EmbarkJS.Contract({abi: this.abi, code: this.code, address: address});
|
||||
};
|
||||
|
||||
EmbarkJS.Contract.prototype.send = function (value, unit, _options) {
|
||||
var options, wei;
|
||||
if (typeof unit === 'object') {
|
||||
options = unit;
|
||||
wei = value;
|
||||
} else {
|
||||
options = _options || {};
|
||||
wei = this.web3.toWei(value, unit);
|
||||
}
|
||||
|
||||
options.to = this.address;
|
||||
options.value = wei;
|
||||
|
||||
this.web3.eth.sendTransaction(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage = {};
|
||||
|
||||
EmbarkJS.Storage.Providers = {};
|
||||
|
||||
EmbarkJS.Storage.saveText = function (text) {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.saveText(text);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.get = function (hash) {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.get(hash);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.uploadFile = function (inputSelector) {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.uploadFile(inputSelector);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.getUrl = function (hash) {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.getUrl(hash);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.registerProvider = function (providerName, obj) {
|
||||
EmbarkJS.Storage.Providers[providerName] = obj;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.setProvider = function (provider, options) {
|
||||
let providerObj = this.Providers[provider];
|
||||
|
||||
if (!providerObj) {
|
||||
throw new Error('Unknown storage provider');
|
||||
}
|
||||
|
||||
this.currentStorage = providerObj;
|
||||
|
||||
return providerObj.setProvider(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.isAvailable = function () {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.isAvailable();
|
||||
};
|
||||
|
||||
EmbarkJS.Messages = {};
|
||||
|
||||
EmbarkJS.Messages.Providers = {};
|
||||
|
||||
EmbarkJS.Messages.registerProvider = function (providerName, obj) {
|
||||
EmbarkJS.Messages.Providers[providerName] = obj;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.setProvider = function (provider, options) {
|
||||
let providerObj = this.Providers[provider];
|
||||
|
||||
if (!providerObj) {
|
||||
throw new Error('Unknown messages provider');
|
||||
}
|
||||
|
||||
this.currentMessages = providerObj;
|
||||
|
||||
return providerObj.setProvider(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.isAvailable = function () {
|
||||
return this.currentMessages.isAvailable();
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.sendMessage = function (options) {
|
||||
if (!this.currentMessages) {
|
||||
throw new Error('Messages provider not set; e.g EmbarkJS.Messages.setProvider("whisper")');
|
||||
}
|
||||
return this.currentMessages.sendMessage(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.listenTo = function (options, callback) {
|
||||
if (!this.currentMessages) {
|
||||
throw new Error('Messages provider not set; e.g EmbarkJS.Messages.setProvider("whisper")');
|
||||
}
|
||||
return this.currentMessages.listenTo(options, callback);
|
||||
};
|
||||
|
||||
EmbarkJS.Names = {};
|
||||
|
||||
EmbarkJS.Names.Providers = {};
|
||||
|
||||
EmbarkJS.Names.registerProvider = function (providerName, obj) {
|
||||
EmbarkJS.Names.Providers[providerName] = obj;
|
||||
};
|
||||
|
||||
EmbarkJS.Names.setProvider = function (provider, options) {
|
||||
let providerObj = this.Providers[provider];
|
||||
|
||||
if (!providerObj) {
|
||||
throw new Error('Unknown name system provider');
|
||||
}
|
||||
|
||||
this.currentNameSystems = providerObj;
|
||||
|
||||
return providerObj.setProvider(options);
|
||||
};
|
||||
|
||||
// resolve resolves a name into an identifier of some kind
|
||||
EmbarkJS.Names.resolve = function (name, callback) {
|
||||
if (!this.currentNameSystems) {
|
||||
throw new Error('Name system provider not set; e.g EmbarkJS.Names.setProvider("ens")');
|
||||
}
|
||||
return this.currentNameSystems.resolve(name, callback);
|
||||
};
|
||||
|
||||
// the reverse of resolve, resolves using an identifier to get to a name
|
||||
EmbarkJS.Names.lookup = function (identifier, callback) {
|
||||
if (!this.currentNameSystems) {
|
||||
throw new Error('Name system provider not set; e.g EmbarkJS.Names.setProvider("ens")');
|
||||
}
|
||||
return this.currentNameSystems.lookup(identifier, callback);
|
||||
};
|
||||
|
||||
EmbarkJS.Names.isAvailable = function () {
|
||||
return this.currentNameSystems.isAvailable();
|
||||
};
|
||||
|
||||
// To Implement
|
||||
|
||||
|
||||
// register a name
|
||||
EmbarkJS.Names.register = function(name, options) {
|
||||
if (!this.currentNameSystems) {
|
||||
throw new Error('Name system provider not set; e.g EmbarkJS.Names.setProvider("ens")');
|
||||
}
|
||||
return this.currentNameSystems.register(name, options);
|
||||
};
|
||||
|
||||
EmbarkJS.Utils = {
|
||||
fromAscii: function (str) {
|
||||
var _web3 = new Web3();
|
||||
return _web3.utils ? _web3.utils.fromAscii(str) : _web3.fromAscii(str);
|
||||
},
|
||||
toAscii: function (str) {
|
||||
var _web3 = new Web3();
|
||||
return _web3.utils.toAscii(str);
|
||||
}
|
||||
};
|
||||
|
||||
export default EmbarkJS;
|
|
@ -1,395 +0,0 @@
|
|||
var EmbarkJS = {
|
||||
onReady: function(cb) {
|
||||
if (typeof (__embarkContext) === 'undefined') {
|
||||
return cb();
|
||||
}
|
||||
return __embarkContext.execWhenReady(cb);
|
||||
}
|
||||
};
|
||||
|
||||
EmbarkJS.isNewWeb3 = function(web3Obj) {
|
||||
var _web3 = web3Obj || (new Web3());
|
||||
if (typeof(_web3.version) === "string") {
|
||||
return true;
|
||||
}
|
||||
return parseInt(_web3.version.api.split('.')[0], 10) >= 1;
|
||||
};
|
||||
|
||||
EmbarkJS.Contract = function(options) {
|
||||
var self = this;
|
||||
var i, abiElement;
|
||||
var ContractClass;
|
||||
|
||||
this.abi = options.abi;
|
||||
this.address = options.address;
|
||||
this.gas = options.gas;
|
||||
this.code = '0x' + options.code;
|
||||
//this.web3 = options.web3 || web3;
|
||||
this.web3 = options.web3;
|
||||
if (!this.web3 && typeof (web3) !== 'undefined') {
|
||||
this.web3 = web3;
|
||||
} else if (!this.web3) {
|
||||
this.web3 = window.web3;
|
||||
}
|
||||
|
||||
if (EmbarkJS.isNewWeb3(this.web3)) {
|
||||
ContractClass = new this.web3.eth.Contract(this.abi, this.address);
|
||||
ContractClass.options.data = this.code;
|
||||
ContractClass.options.from = this.from || this.web3.eth.defaultAccount;
|
||||
ContractClass.abi = ContractClass.options.abi;
|
||||
ContractClass.address = this.address;
|
||||
ContractClass.gas = this.gas;
|
||||
|
||||
let originalMethods = Object.keys(ContractClass);
|
||||
|
||||
ContractClass._jsonInterface.forEach((abi) => {
|
||||
if (originalMethods.indexOf(abi.name) >= 0) {
|
||||
console.log(abi.name + " is a reserved word and cannot be used as a contract method, property or event");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!abi.inputs) {
|
||||
return;
|
||||
}
|
||||
|
||||
let numExpectedInputs = abi.inputs.length;
|
||||
|
||||
if (abi.type === 'function' && abi.constant) {
|
||||
ContractClass[abi.name] = function() {
|
||||
let options = {}, cb = null, args = Array.from(arguments || []).slice(0, numExpectedInputs);
|
||||
if (typeof (arguments[numExpectedInputs]) === 'function') {
|
||||
cb = arguments[numExpectedInputs];
|
||||
} else if (typeof (arguments[numExpectedInputs]) === 'object') {
|
||||
options = arguments[numExpectedInputs];
|
||||
cb = arguments[numExpectedInputs + 1];
|
||||
}
|
||||
|
||||
let ref = ContractClass.methods[abi.name];
|
||||
let call = ref.apply(ref, ...arguments).call;
|
||||
return call.apply(call, []);
|
||||
};
|
||||
} else if (abi.type === 'function') {
|
||||
ContractClass[abi.name] = function() {
|
||||
let options = {}, cb = null, args = Array.from(arguments || []).slice(0, numExpectedInputs);
|
||||
if (typeof (arguments[numExpectedInputs]) === 'function') {
|
||||
cb = arguments[numExpectedInputs];
|
||||
} else if (typeof (arguments[numExpectedInputs]) === 'object') {
|
||||
options = arguments[numExpectedInputs];
|
||||
cb = arguments[numExpectedInputs + 1];
|
||||
}
|
||||
|
||||
let ref = ContractClass.methods[abi.name];
|
||||
let send = ref.apply(ref, args).send;
|
||||
return send.apply(send, [options, cb]);
|
||||
};
|
||||
} else if (abi.type === 'event') {
|
||||
ContractClass[abi.name] = function(options, cb) {
|
||||
let ref = ContractClass.events[abi.name];
|
||||
return ref.apply(ref, [options, cb]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return ContractClass;
|
||||
} else {
|
||||
ContractClass = this.web3.eth.contract(this.abi);
|
||||
|
||||
this.eventList = [];
|
||||
|
||||
if (this.abi) {
|
||||
for (i = 0; i < this.abi.length; i++) {
|
||||
abiElement = this.abi[i];
|
||||
if (abiElement.type === 'event') {
|
||||
this.eventList.push(abiElement.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var messageEvents = function() {
|
||||
this.cb = function() {};
|
||||
};
|
||||
|
||||
messageEvents.prototype.then = function(cb) {
|
||||
this.cb = cb;
|
||||
};
|
||||
|
||||
messageEvents.prototype.error = function(err) {
|
||||
return err;
|
||||
};
|
||||
|
||||
this._originalContractObject = ContractClass.at(this.address);
|
||||
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function(p) {
|
||||
// TODO: check for forbidden properties
|
||||
if (self.eventList.indexOf(p) >= 0) {
|
||||
|
||||
self[p] = function() {
|
||||
var promise = new messageEvents();
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
args.push(function(err, result) {
|
||||
if (err) {
|
||||
promise.error(err);
|
||||
} else {
|
||||
promise.cb(result);
|
||||
}
|
||||
});
|
||||
|
||||
self._originalContractObject[p].apply(self._originalContractObject[p], args);
|
||||
return promise;
|
||||
};
|
||||
return true;
|
||||
} else if (typeof self._originalContractObject[p] === 'function') {
|
||||
self[p] = function(_args) {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var fn = self._originalContractObject[p];
|
||||
var props = self.abi.find((x) => x.name == p);
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
args.push(function(err, transaction) {
|
||||
promise.tx = transaction;
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
var getConfirmation = function() {
|
||||
self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
if (receipt !== null) {
|
||||
return resolve(receipt);
|
||||
}
|
||||
|
||||
setTimeout(getConfirmation, 1000);
|
||||
});
|
||||
};
|
||||
|
||||
if (typeof(transaction) !== "string" || props.constant) {
|
||||
resolve(transaction);
|
||||
} else {
|
||||
getConfirmation();
|
||||
}
|
||||
});
|
||||
|
||||
fn.apply(fn, args);
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
||||
var self = this;
|
||||
var contractParams;
|
||||
var options = _options || {};
|
||||
|
||||
contractParams = args || [];
|
||||
|
||||
contractParams.push({
|
||||
from: this.web3.eth.accounts[0],
|
||||
data: this.code,
|
||||
gas: options.gas || 800000
|
||||
});
|
||||
|
||||
var contractObject = this.web3.eth.contract(this.abi);
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
contractParams.push(function(err, transaction) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else if (transaction.address !== undefined) {
|
||||
resolve(new EmbarkJS.Contract({
|
||||
abi: self.abi,
|
||||
code: self.code,
|
||||
address: transaction.address
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
// returns promise
|
||||
// deploys contract
|
||||
// wraps it around EmbarkJS.Contract
|
||||
contractObject["new"].apply(contractObject, contractParams);
|
||||
});
|
||||
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Contract.prototype.new = EmbarkJS.Contract.prototype.deploy;
|
||||
|
||||
EmbarkJS.Contract.prototype.at = function(address) {
|
||||
return new EmbarkJS.Contract({ abi: this.abi, code: this.code, address: address });
|
||||
};
|
||||
|
||||
EmbarkJS.Contract.prototype.send = function(value, unit, _options) {
|
||||
var options, wei;
|
||||
if (typeof unit === 'object') {
|
||||
options = unit;
|
||||
wei = value;
|
||||
} else {
|
||||
options = _options || {};
|
||||
wei = this.web3.toWei(value, unit);
|
||||
}
|
||||
|
||||
options.to = this.address;
|
||||
options.value = wei;
|
||||
|
||||
this.web3.eth.sendTransaction(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage = {};
|
||||
|
||||
EmbarkJS.Storage.Providers = {};
|
||||
|
||||
EmbarkJS.Storage.saveText = function(text) {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.saveText(text);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.get = function(hash) {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.get(hash);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.uploadFile(inputSelector);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.getUrl = function(hash) {
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.getUrl(hash);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.registerProvider = function(providerName, obj) {
|
||||
EmbarkJS.Storage.Providers[providerName] = obj;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||
let providerObj = this.Providers[provider];
|
||||
|
||||
if (!providerObj) {
|
||||
throw new Error('Unknown storage provider');
|
||||
}
|
||||
|
||||
this.currentStorage = providerObj;
|
||||
|
||||
return providerObj.setProvider(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.isAvailable = function(){
|
||||
if (!this.currentStorage) {
|
||||
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
|
||||
}
|
||||
return this.currentStorage.isAvailable();
|
||||
};
|
||||
|
||||
EmbarkJS.Messages = {};
|
||||
|
||||
EmbarkJS.Messages.Providers = {};
|
||||
|
||||
EmbarkJS.Messages.registerProvider = function(providerName, obj) {
|
||||
EmbarkJS.Messages.Providers[providerName] = obj;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.setProvider = function(provider, options) {
|
||||
let providerObj = this.Providers[provider];
|
||||
|
||||
if (!providerObj) {
|
||||
throw new Error('Unknown messages provider');
|
||||
}
|
||||
|
||||
this.currentMessages = providerObj;
|
||||
|
||||
return providerObj.setProvider(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.isAvailable = function(){
|
||||
return this.currentMessages.isAvailable();
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.sendMessage = function(options) {
|
||||
if (!this.currentMessages) {
|
||||
throw new Error('Messages provider not set; e.g EmbarkJS.Messages.setProvider("whisper")');
|
||||
}
|
||||
return this.currentMessages.sendMessage(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.listenTo = function(options, callback) {
|
||||
if (!this.currentMessages) {
|
||||
throw new Error('Messages provider not set; e.g EmbarkJS.Messages.setProvider("whisper")');
|
||||
}
|
||||
return this.currentMessages.listenTo(options, callback);
|
||||
};
|
||||
|
||||
EmbarkJS.Names = {};
|
||||
|
||||
EmbarkJS.Names.Providers = {};
|
||||
|
||||
EmbarkJS.Names.registerProvider = function(providerName, obj) {
|
||||
EmbarkJS.Names.Providers[providerName] = obj;
|
||||
}
|
||||
|
||||
EmbarkJS.Names.setProvider = function(provider, options) {
|
||||
let providerObj = this.Providers[provider];
|
||||
|
||||
if (!providerObj) {
|
||||
throw new Error('Unknown name system provider');
|
||||
}
|
||||
|
||||
this.currentNameSystems = providerObj;
|
||||
|
||||
return providerObj.setProvider(options);
|
||||
};
|
||||
|
||||
// resolve resolves a name into an identifier of some kind
|
||||
EmbarkJS.Names.resolve = function(name) {
|
||||
if (!this.currentNameSystems) {
|
||||
throw new Error('Name system provider not set; e.g EmbarkJS.Names.setProvider("ens")');
|
||||
}
|
||||
return this.currentNameSystems.resolve(name);
|
||||
}
|
||||
|
||||
// the reverse of resolve, resolves using an identifier to get to a name
|
||||
EmbarkJS.Names.lookup = function(identifier) {
|
||||
if (!this.currentNameSystems) {
|
||||
throw new Error('Name system provider not set; e.g EmbarkJS.Names.setProvider("ens")');
|
||||
}
|
||||
return this.currentNameSystems.lookup(identifier);
|
||||
}
|
||||
|
||||
// To Implement
|
||||
|
||||
/*
|
||||
// register a name
|
||||
EmbarkJS.Names.register = function(name, options) {
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
EmbarkJS.Utils = {
|
||||
fromAscii: function(str) {
|
||||
var _web3 = new Web3();
|
||||
return _web3.utils ? _web3.utils.fromAscii(str) : _web3.fromAscii(str);
|
||||
},
|
||||
toAscii: function(str) {
|
||||
var _web3 = new Web3();
|
||||
return _web3.utils.toAscii(str);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = EmbarkJS;
|
|
@ -1,54 +0,0 @@
|
|||
let shelljs = require('shelljs');
|
||||
let proxy = require('../core/proxy');
|
||||
const Ipc = require('../core/ipc');
|
||||
const constants = require('../constants.json');
|
||||
const {defaultHost, dockerHostSwap} = require('../utils/host');
|
||||
|
||||
class Simulator {
|
||||
constructor(options) {
|
||||
this.blockchainConfig = options.blockchainConfig;
|
||||
this.logger = options.logger;
|
||||
}
|
||||
|
||||
run(options) {
|
||||
let cmds = [];
|
||||
|
||||
const ganache = path.join(__dirname, '../../node_modules/.bin/ganache-cli');
|
||||
|
||||
let useProxy = this.blockchainConfig.proxy || false;
|
||||
let host = (dockerHostSwap(options.host || this.blockchainConfig.rpcHost) || defaultHost);
|
||||
let port = (options.port || this.blockchainConfig.rpcPort || 8545);
|
||||
|
||||
cmds.push("-p " + (port + (useProxy ? constants.blockchain.servicePortOnProxy : 0)));
|
||||
if (!ganache) {
|
||||
cmds.push("-h " + host);
|
||||
}
|
||||
cmds.push("-a " + (options.numAccounts || 10));
|
||||
cmds.push("-e " + (options.defaultBalance || 100));
|
||||
cmds.push("-l " + (options.gasLimit || 8000000));
|
||||
|
||||
// adding mnemonic only if it is defined in the blockchainConfig or options
|
||||
let simulatorMnemonic = this.blockchainConfig.simulatorMnemonic || options.simulatorMnemonic;
|
||||
if (simulatorMnemonic) {
|
||||
cmds.push("--mnemonic \"" + (simulatorMnemonic) +"\"");
|
||||
}
|
||||
|
||||
// adding blocktime only if it is defined in the blockchainConfig or options
|
||||
let simulatorBlocktime = this.blockchainConfig.simulatorBlocktime || options.simulatorBlocktime;
|
||||
if (simulatorBlocktime) {
|
||||
cmds.push("-b \"" + (simulatorBlocktime) +"\"");
|
||||
}
|
||||
|
||||
const programName = 'ganache-cli';
|
||||
const program = ganache;
|
||||
console.log(`running: ${programName} ${cmds.join(' ')}`);
|
||||
shelljs.exec(`${program} ${cmds.join(' ')}`, {async : true});
|
||||
|
||||
if(useProxy){
|
||||
let ipcObject = new Ipc({ipcRole: 'client'});
|
||||
proxy.serve(ipcObject, host, port, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Simulator;
|
|
@ -1,96 +0,0 @@
|
|||
let fs = require('../core/fs.js');
|
||||
let utils = require('../utils/utils.js');
|
||||
|
||||
class TemplateGenerator {
|
||||
constructor(templateName) {
|
||||
this.templateName = templateName;
|
||||
}
|
||||
|
||||
downloadAndGenerate(uri, destinationFolder, name) {
|
||||
const self = this;
|
||||
let {url, filePath} = this.getExternalProject(uri);
|
||||
let tmpFilePath = fs.tmpDir(filePath);
|
||||
console.log(__('Installing Template from ' + uri + '....').green);
|
||||
|
||||
fs.mkdirpSync(utils.dirname(tmpFilePath));
|
||||
utils.downloadFile(url, tmpFilePath, () => {
|
||||
let fspath = utils.joinPath(destinationFolder, name);
|
||||
const decompress = require('decompress');
|
||||
|
||||
decompress(tmpFilePath, fspath, {
|
||||
map: file => {
|
||||
let fixed_path = file.path.split('/');
|
||||
fixed_path.shift(); // remove first directory
|
||||
file.path = utils.joinPath(...fixed_path);
|
||||
return file;
|
||||
}
|
||||
}).then(() => {
|
||||
self.installTemplate(fspath, name, true);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
generate(destinationFolder, name) {
|
||||
console.log(__('Initializing Embark Template....').green);
|
||||
|
||||
let templatePath = fs.embarkPath(utils.joinPath('templates', this.templateName));
|
||||
let fspath = utils.joinPath(destinationFolder, name);
|
||||
fs.copySync(templatePath, fspath);
|
||||
|
||||
this.installTemplate(fspath, name, (name === 'embark_demo'));
|
||||
|
||||
if (name === 'embark_demo') {
|
||||
console.log('-------------------'.yellow);
|
||||
console.log(__('Next steps:').green);
|
||||
console.log(('-> ' + ('cd ' + fspath).bold.cyan).green);
|
||||
console.log('-> '.green + 'embark run'.bold.cyan);
|
||||
console.log(__('For more info go to http://embark.status.im').green);
|
||||
}
|
||||
}
|
||||
|
||||
installTemplate(templatePath, name, installPackages) {
|
||||
utils.cd(templatePath);
|
||||
utils.sed('package.json', '%APP_NAME%', name);
|
||||
|
||||
if (installPackages) {
|
||||
console.log(__('Installing packages...').green);
|
||||
utils.runCmd('npm install');
|
||||
}
|
||||
|
||||
console.log(__('Init complete').green);
|
||||
console.log('\n' + __('App ready at ').green + templatePath);
|
||||
}
|
||||
|
||||
getExternalProject(uri) {
|
||||
const constants = require('../constants');
|
||||
const RAW_URL = 'https://github.com/';
|
||||
|
||||
let match = uri.match(
|
||||
/\.[a-z]+\/([-a-zA-Z0-9@:%_+.~#?&\/=]+)/
|
||||
);
|
||||
|
||||
let url, folder;
|
||||
|
||||
if (uri.startsWith('http')) {
|
||||
url = uri + "/archive/master.zip"
|
||||
folder = match[1]
|
||||
} else if (uri.startsWith('github')) {
|
||||
url = "https://" + uri + "/archive/master.zip"
|
||||
folder = match[1]
|
||||
} else if (uri.split('/').length === 2) {
|
||||
url = "https://github.com/" + uri + "/archive/master.zip"
|
||||
folder = uri
|
||||
} else if (uri.indexOf('/') === -1) {
|
||||
url = "https://github.com/embark-framework/embark-" + uri + "-template/archive/master.zip"
|
||||
folder = "embark-framework/embark-" + uri + "-template"
|
||||
}
|
||||
|
||||
return {
|
||||
url,
|
||||
filePath: utils.joinPath(".embark/templates/", folder, "archive.zip")
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = TemplateGenerator;
|
|
@ -36,7 +36,7 @@ class Engine {
|
|||
this.isDev = this.config && this.config.blockchainConfig && (this.config.blockchainConfig.isDev || this.config.blockchainConfig.default);
|
||||
|
||||
if (this.interceptLogs || this.interceptLogs === undefined) {
|
||||
this.doInterceptLogs();
|
||||
utils.interceptLogs(console, this.logger);
|
||||
}
|
||||
|
||||
this.ipc = new IPC({logger: this.logger, ipcRole: this.ipcRole});
|
||||
|
@ -73,10 +73,10 @@ class Engine {
|
|||
"libraryManager": this.libraryManagerService,
|
||||
"processManager": this.processManagerService,
|
||||
"storage": this.storageService,
|
||||
"pluginCommand": this.pluginCommandService
|
||||
"graph": this.graphService,
|
||||
"codeCoverage": this.codeCoverageService,
|
||||
"testRunner": this.testRunnerService,
|
||||
"pluginCommand": this.pluginCommandService
|
||||
"testRunner": this.testRunnerService
|
||||
};
|
||||
|
||||
let service = services[serviceName];
|
||||
|
@ -91,8 +91,8 @@ class Engine {
|
|||
}
|
||||
|
||||
processManagerService(_options) {
|
||||
const ProcessManager = require('../processes/process_manager.js');
|
||||
const processManager = new ProcessManager({
|
||||
const ProcessManager = require('./processes/process_manager.js');
|
||||
this.processManager = new ProcessManager({
|
||||
events: this.events,
|
||||
logger: this.logger,
|
||||
plugins: this.plugins
|
||||
|
@ -143,30 +143,20 @@ class Engine {
|
|||
}
|
||||
|
||||
codeRunnerService(_options) {
|
||||
const CodeRunner = require('../coderunner/codeRunner.js');
|
||||
const CodeRunner = require('./modules/coderunner/codeRunner.js');
|
||||
this.codeRunner = new CodeRunner({
|
||||
config: this.config,
|
||||
plugins: this.plugins,
|
||||
events: this.events,
|
||||
logger: this.logger
|
||||
logger: this.logger,
|
||||
ipc: this.ipc
|
||||
});
|
||||
}
|
||||
|
||||
codeGeneratorService(_options) {
|
||||
let self = this;
|
||||
|
||||
const CodeGenerator = require('../contracts/code_generator.js');
|
||||
this.codeGenerator = new CodeGenerator({
|
||||
blockchainConfig: self.config.blockchainConfig,
|
||||
contractsConfig: self.config.contractsConfig,
|
||||
plugins: self.plugins,
|
||||
storageConfig: self.config.storageConfig,
|
||||
namesystemConfig: self.config.namesystemConfig,
|
||||
communicationConfig: self.config.communicationConfig,
|
||||
events: self.events,
|
||||
env: self.env
|
||||
});
|
||||
this.codeGenerator.listenToCommands();
|
||||
this.registerModule('code_generator', {plugins: self.plugins, env: self.env});
|
||||
|
||||
const generateCode = function (modifiedAssets) {
|
||||
self.events.request("code-generator:embarkjs:build", () => {
|
||||
|
@ -210,6 +200,8 @@ class Engine {
|
|||
self.fileTimeout = setTimeout(() => {
|
||||
// TODO: still need to redeploy contracts because the original contracts
|
||||
// config is being corrupted
|
||||
self.config.reloadConfig();
|
||||
|
||||
if (fileType === 'asset') {
|
||||
// Throttle file changes so we re-write only once for all files
|
||||
self.events.emit('asset-changed', path);
|
||||
|
@ -217,8 +209,6 @@ class Engine {
|
|||
// TODO: for now need to deploy on asset changes as well
|
||||
// because the contractsManager config is corrupted after a deploy
|
||||
if (fileType === 'contract' || fileType === 'config') {
|
||||
self.config.reloadConfig();
|
||||
|
||||
self.events.request('deploy:contracts', () => {});
|
||||
}
|
||||
}, 50);
|
||||
|
@ -255,19 +245,15 @@ class Engine {
|
|||
wait: options.wait
|
||||
});
|
||||
|
||||
this.registerModule('whisper', {
|
||||
// TODO: this should not be needed and should be deducted from the config instead
|
||||
// the eth provider is not necessary the same as the whisper one
|
||||
web3: this.blockchain.web3
|
||||
});
|
||||
this.registerModule('whisper');
|
||||
}
|
||||
|
||||
libraryManagerService(_options) {
|
||||
const LibraryManager = require('../versions/library_manager.js');
|
||||
this.libraryManager = new LibraryManager({
|
||||
plugins: this.plugins,
|
||||
config: this.config
|
||||
});
|
||||
this.registerModule('library_manager');
|
||||
}
|
||||
|
||||
codeCoverageService(_options) {
|
||||
this.registerModule('coverage');
|
||||
}
|
||||
|
||||
testRunnerService(options) {
|
||||
|
|
Loading…
Reference in New Issue