2017-12-13 21:28:23 +00:00
|
|
|
/*jshint esversion: 6 */
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
var EmbarkJS = {};
|
2016-08-18 11:45:08 +00:00
|
|
|
|
2017-10-07 19:20:51 +00:00
|
|
|
EmbarkJS.isNewWeb3 = function() {
|
2017-12-13 22:10:33 +00:00
|
|
|
var _web3 = new Web3();
|
|
|
|
if (typeof(_web3.version) === "string") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return parseInt(_web3.version.api.split('.')[0], 10) >= 1;
|
2017-10-07 19:20:51 +00:00
|
|
|
};
|
|
|
|
|
2016-08-18 11:45:08 +00:00
|
|
|
EmbarkJS.Contract = function(options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
var self = this;
|
|
|
|
var i, abiElement;
|
2017-10-09 13:11:37 +00:00
|
|
|
var ContractClass;
|
2016-08-18 11:45:08 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
this.abi = options.abi;
|
|
|
|
this.address = options.address;
|
|
|
|
this.code = '0x' + options.code;
|
2017-12-12 17:20:57 +00:00
|
|
|
//this.web3 = options.web3 || web3;
|
|
|
|
this.web3 = options.web3 || window.web3;
|
2016-08-18 11:45:08 +00:00
|
|
|
|
2017-10-07 19:20:51 +00:00
|
|
|
if (EmbarkJS.isNewWeb3()) {
|
|
|
|
// TODO:
|
|
|
|
// add default **from** address
|
|
|
|
// add gasPrice
|
2017-10-09 13:11:37 +00:00
|
|
|
ContractClass = new this.web3.eth.Contract(this.abi, this.address);
|
2017-10-07 19:20:51 +00:00
|
|
|
ContractClass.setProvider(this.web3.currentProvider);
|
|
|
|
|
|
|
|
return ContractClass;
|
|
|
|
} else {
|
2017-10-09 13:11:37 +00:00
|
|
|
ContractClass = this.web3.eth.contract(this.abi);
|
2016-10-30 12:40:37 +00:00
|
|
|
|
2017-10-07 19:20:51 +00:00
|
|
|
this.eventList = [];
|
|
|
|
|
|
|
|
if (this.abi) {
|
2017-03-17 14:02:26 +00:00
|
|
|
for (i = 0; i < this.abi.length; i++) {
|
2017-10-07 19:20:51 +00:00
|
|
|
abiElement = this.abi[i];
|
|
|
|
if (abiElement.type === 'event') {
|
|
|
|
this.eventList.push(abiElement.name);
|
|
|
|
}
|
2017-03-17 14:02:26 +00:00
|
|
|
}
|
2017-10-07 19:20:51 +00:00
|
|
|
}
|
2016-10-30 12:40:37 +00:00
|
|
|
|
2017-10-07 19:20:51 +00:00
|
|
|
var messageEvents = function() {
|
2017-03-17 14:02:26 +00:00
|
|
|
this.cb = function() {};
|
2017-10-07 19:20:51 +00:00
|
|
|
};
|
2017-02-28 01:32:26 +00:00
|
|
|
|
2017-10-07 19:20:51 +00:00
|
|
|
messageEvents.prototype.then = function(cb) {
|
2017-03-17 14:02:26 +00:00
|
|
|
this.cb = cb;
|
2017-10-07 19:20:51 +00:00
|
|
|
};
|
2017-02-28 01:32:26 +00:00
|
|
|
|
2017-10-07 19:20:51 +00:00
|
|
|
messageEvents.prototype.error = function(err) {
|
2017-03-17 14:02:26 +00:00
|
|
|
return err;
|
2017-10-07 19:20:51 +00:00
|
|
|
};
|
2017-02-28 01:32:26 +00:00
|
|
|
|
2017-10-07 19:20:51 +00:00
|
|
|
this._originalContractObject = ContractClass.at(this.address);
|
|
|
|
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function(p) {
|
2017-03-17 14:02:26 +00:00
|
|
|
// TODO: check for forbidden properties
|
|
|
|
if (self.eventList.indexOf(p) >= 0) {
|
|
|
|
|
2017-10-07 19:20:51 +00:00
|
|
|
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) {
|
2017-03-17 14:02:26 +00:00
|
|
|
if (err) {
|
2017-10-07 19:20:51 +00:00
|
|
|
return reject(err);
|
2017-03-17 14:02:26 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 19:20:51 +00:00
|
|
|
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;
|
2017-03-17 14:02:26 +00:00
|
|
|
}
|
|
|
|
return false;
|
2017-10-07 19:20:51 +00:00
|
|
|
});
|
|
|
|
}
|
2016-08-18 11:45:08 +00:00
|
|
|
};
|
|
|
|
|
2017-02-22 11:51:02 +00:00
|
|
|
EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
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
|
2016-08-18 11:45:08 +00:00
|
|
|
});
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
var contractObject = this.web3.eth.contract(this.abi);
|
2016-08-18 11:45:08 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
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
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
2017-03-12 21:59:21 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
// returns promise
|
|
|
|
// deploys contract
|
|
|
|
// wraps it around EmbarkJS.Contract
|
|
|
|
contractObject["new"].apply(contractObject, contractParams);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return promise;
|
2016-08-18 11:45:08 +00:00
|
|
|
};
|
|
|
|
|
2017-04-02 18:12:12 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
EmbarkJS.Storage = {};
|
2017-03-13 13:38:30 +00:00
|
|
|
|
|
|
|
EmbarkJS.Storage.Providers = {
|
2017-03-17 14:02:26 +00:00
|
|
|
SWARM: 'swarm'
|
2017-03-13 13:38:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Storage.saveText = function(text) {
|
2017-03-17 14:02:26 +00:00
|
|
|
return this.currentStorage.saveText(text);
|
2016-08-26 11:01:22 +00:00
|
|
|
};
|
|
|
|
|
2017-03-14 15:39:09 +00:00
|
|
|
EmbarkJS.Storage.get = function(hash) {
|
2017-03-17 14:02:26 +00:00
|
|
|
return this.currentStorage.get(hash);
|
2017-03-14 15:39:09 +00:00
|
|
|
};
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
|
|
|
return this.currentStorage.uploadFile(inputSelector);
|
2017-03-18 14:14:19 +00:00
|
|
|
};
|
2017-03-14 15:39:09 +00:00
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
EmbarkJS.Storage.getUrl = function(hash) {
|
|
|
|
return this.currentStorage.getUrl(hash);
|
2017-03-18 14:14:19 +00:00
|
|
|
};
|
2017-03-14 15:39:09 +00:00
|
|
|
|
2017-12-28 00:10:05 +00:00
|
|
|
EmbarkJS.Storage.registerProvider = function(providerName, obj) {
|
|
|
|
EmbarkJS.Storage.Providers[providerName] = obj;
|
2017-03-14 15:39:09 +00:00
|
|
|
};
|
|
|
|
|
2017-12-28 00:10:05 +00:00
|
|
|
EmbarkJS.Storage.setProvider = function(provider, options) {
|
2017-12-28 01:57:10 +00:00
|
|
|
let providerObj = this.Providers[provider];
|
2017-03-17 14:02:26 +00:00
|
|
|
|
2017-12-28 01:57:10 +00:00
|
|
|
if (!providerObj) {
|
2017-12-28 00:10:05 +00:00
|
|
|
throw new Error('Unknown storage provider');
|
|
|
|
}
|
2016-08-26 11:01:22 +00:00
|
|
|
|
2017-12-28 01:57:10 +00:00
|
|
|
this.currentStorage = providerObj;
|
2016-08-26 11:01:22 +00:00
|
|
|
|
2017-12-28 01:57:10 +00:00
|
|
|
return providerObj.setProvider(options);
|
2016-08-26 11:01:22 +00:00
|
|
|
};
|
|
|
|
|
2017-03-17 14:02:26 +00:00
|
|
|
EmbarkJS.Messages = {};
|
2016-08-21 14:42:42 +00:00
|
|
|
|
2017-12-28 01:19:41 +00:00
|
|
|
EmbarkJS.Messages.Providers = {};
|
|
|
|
|
|
|
|
EmbarkJS.Messages.registerProvider = function(providerName, obj) {
|
|
|
|
EmbarkJS.Messages.Providers[providerName] = obj;
|
2017-07-24 11:27:52 +00:00
|
|
|
};
|
|
|
|
|
2017-01-07 05:03:03 +00:00
|
|
|
EmbarkJS.Messages.setProvider = function(provider, options) {
|
2017-12-28 02:04:12 +00:00
|
|
|
let providerObj = this.Providers[provider];
|
2017-12-28 01:19:41 +00:00
|
|
|
|
2017-12-28 02:04:12 +00:00
|
|
|
if (!providerObj) {
|
|
|
|
throw new Error('Unknown messages provider');
|
|
|
|
}
|
2017-12-28 01:19:41 +00:00
|
|
|
|
2017-12-28 02:04:12 +00:00
|
|
|
this.currentMessages = providerObj;
|
2017-12-28 01:19:41 +00:00
|
|
|
|
2017-12-28 02:04:12 +00:00
|
|
|
return providerObj.setProvider(options);
|
2016-08-21 14:42:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Messages.sendMessage = function(options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
return this.currentMessages.sendMessage(options);
|
2016-08-21 14:42:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Messages.listenTo = function(options) {
|
2017-03-17 14:02:26 +00:00
|
|
|
return this.currentMessages.listenTo(options);
|
2016-08-21 14:42:42 +00:00
|
|
|
};
|
|
|
|
|
2017-07-24 11:27:52 +00:00
|
|
|
EmbarkJS.Utils = {
|
|
|
|
fromAscii: function(str) {
|
|
|
|
var _web3 = new Web3();
|
|
|
|
return _web3.utils ? _web3.utils.fromAscii(str) : _web3.fromAscii(str);
|
2017-10-09 12:59:02 +00:00
|
|
|
},
|
|
|
|
toAscii: function(str) {
|
|
|
|
var _web3 = new Web3();
|
|
|
|
return _web3.utils.toAscii(str);
|
2017-07-24 11:27:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-13 14:01:53 +00:00
|
|
|
export default EmbarkJS;
|