embark-area-51/js/embark.js

336 lines
9.0 KiB
JavaScript
Raw Normal View History

var EmbarkJS = {
onReady: __embarkContext.execWhenReady
};
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
};
EmbarkJS.Contract = function(options) {
var self = this;
var i, abiElement;
2017-10-09 13:11:37 +00:00
var ContractClass;
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;
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);
ContractClass.options.data = this.code;
ContractClass.abi = ContractClass.options.abi;
2018-06-06 16:47:16 +00:00
ContractClass.address = this.address;
2017-10-07 19:20:51 +00:00
return ContractClass;
} else {
ContractClass = this.web3.eth.contract(this.abi);
2017-10-07 19:20:51 +00:00
this.eventList = [];
if (this.abi) {
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-10-07 19:20:51 +00:00
}
2017-10-07 19:20:51 +00:00
var messageEvents = function() {
this.cb = function() {};
2017-10-07 19:20:51 +00:00
};
2017-10-07 19:20:51 +00:00
messageEvents.prototype.then = function(cb) {
this.cb = cb;
2017-10-07 19:20:51 +00:00
};
2017-10-07 19:20:51 +00:00
messageEvents.prototype.error = function(err) {
return err;
2017-10-07 19:20:51 +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) {
// 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) {
if (err) {
2017-10-07 19:20:51 +00:00
return reject(err);
}
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;
}
return false;
2017-10-07 19:20:51 +00:00
});
}
};
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;
};
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);
};
EmbarkJS.Storage = {};
WIP to merge in other swarm changes Adding swarm to embarkjs. WIP. Add 'auto' setting for geth CORS and websockets origin * 'auto' now supported for `rpcCorsDomain` and `wsOrigins` in the blockchain config. * 'auto' set to the default value in blockchain config for test and demo apps. test add config and contract and add test addFileToPipeline test and registerBeforeDeploy with new arg add more registers but generation one fails in run WIP commit Undo changes to test config. Merge pull request #381 from embark-framework/features/cors-auto Add 'auto' setting for geth CORS and websockets origin fix a bug where upload cmd used plugin name don't error if it's an empty dapp with no contracts yet Merge pull request #383 from embark-framework/no_contracts don't error if it's an empty dapp with no contracts yet remove duplicated entry force zepplein version for travis Merge pull request #384 from embark-framework/chores/test-allpligin-apis Small fixes for plugin APIs intercept logs in the app itself - stopgap fix Merge pull request #385 from embark-framework/console_logs_fix intercept logs in the app itself - stopgap fix * removed unneeded provider property. * add 'swarm' as a provider in the storage.config * update method for swarm service check Merge branch 'develop' into features/add-swarm-to-embarkjs More work to add swarm to embarkjs * added eth-lib to parse result of swarm text * changed "currentStorage" and "currentMessages" to "currentProvider" for consistency. * added protocol to storage config * selectively starts storage service depending on which one is configured in the storage config * run service check for ipfs/swarm prior to uploaded * added swarm methods for embarkjs Updated code based on code review check if testrpc is installed and warn if not Merge pull request #386 from embark-framework/bug_fix/test-rpc-not-installed check if testrpc is installed and warn if not Removed timeout Removed spacer Merge pull request #382 from embark-framework/react-demo Updating embark demo to use react instead of jquery fix on contract add Merge pull request #387 from embark-framework/bug_fix/new-contract-in-empty-dapp Fix adding a contract redeploy with right config on config change fix tests reset watchers after build to make sure files remain watch Merge pull request #389 from embark-framework/bug_fix/file-changes-not-watched Fix files not being watched Merge pull request #388 from embark-framework/bug_fix/changing-contract-config Redeploy with right config on config change Added swarm support in embarkjs and isAvailable for messages/storage * reverted currentProvider back to currentStorage and currentMessages * added `EmbarkJS.Storage.isAvailable` and `EmbarkJS.Messages.isAvailable()` and underlying provider functions for Whisper, Orbit, IPFS, and Swarm * Finished swarm implementation in embarkjs plus cleanup * updated test app storage config to swarm to show swarm config option Merge branch 'develop' into features/add-swarm-to-embarkjs
2018-04-30 05:56:43 +00:00
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);
2016-08-26 11:01:22 +00:00
};
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);
2017-03-18 14:14:19 +00:00
};
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);
2017-03-18 14:14:19 +00:00
};
EmbarkJS.Storage.registerProvider = function(providerName, obj) {
EmbarkJS.Storage.Providers[providerName] = obj;
};
EmbarkJS.Storage.setProvider = function(provider, options) {
2017-12-28 01:57:10 +00:00
let providerObj = this.Providers[provider];
2017-12-28 01:57:10 +00:00
if (!providerObj) {
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
};
WIP to merge in other swarm changes Adding swarm to embarkjs. WIP. Add 'auto' setting for geth CORS and websockets origin * 'auto' now supported for `rpcCorsDomain` and `wsOrigins` in the blockchain config. * 'auto' set to the default value in blockchain config for test and demo apps. test add config and contract and add test addFileToPipeline test and registerBeforeDeploy with new arg add more registers but generation one fails in run WIP commit Undo changes to test config. Merge pull request #381 from embark-framework/features/cors-auto Add 'auto' setting for geth CORS and websockets origin fix a bug where upload cmd used plugin name don't error if it's an empty dapp with no contracts yet Merge pull request #383 from embark-framework/no_contracts don't error if it's an empty dapp with no contracts yet remove duplicated entry force zepplein version for travis Merge pull request #384 from embark-framework/chores/test-allpligin-apis Small fixes for plugin APIs intercept logs in the app itself - stopgap fix Merge pull request #385 from embark-framework/console_logs_fix intercept logs in the app itself - stopgap fix * removed unneeded provider property. * add 'swarm' as a provider in the storage.config * update method for swarm service check Merge branch 'develop' into features/add-swarm-to-embarkjs More work to add swarm to embarkjs * added eth-lib to parse result of swarm text * changed "currentStorage" and "currentMessages" to "currentProvider" for consistency. * added protocol to storage config * selectively starts storage service depending on which one is configured in the storage config * run service check for ipfs/swarm prior to uploaded * added swarm methods for embarkjs Updated code based on code review check if testrpc is installed and warn if not Merge pull request #386 from embark-framework/bug_fix/test-rpc-not-installed check if testrpc is installed and warn if not Removed timeout Removed spacer Merge pull request #382 from embark-framework/react-demo Updating embark demo to use react instead of jquery fix on contract add Merge pull request #387 from embark-framework/bug_fix/new-contract-in-empty-dapp Fix adding a contract redeploy with right config on config change fix tests reset watchers after build to make sure files remain watch Merge pull request #389 from embark-framework/bug_fix/file-changes-not-watched Fix files not being watched Merge pull request #388 from embark-framework/bug_fix/changing-contract-config Redeploy with right config on config change Added swarm support in embarkjs and isAvailable for messages/storage * reverted currentProvider back to currentStorage and currentMessages * added `EmbarkJS.Storage.isAvailable` and `EmbarkJS.Messages.isAvailable()` and underlying provider functions for Whisper, Orbit, IPFS, and Swarm * Finished swarm implementation in embarkjs plus cleanup * updated test app storage config to swarm to show swarm config option Merge branch 'develop' into features/add-swarm-to-embarkjs
2018-04-30 05:56:43 +00:00
EmbarkJS.Storage.isAvailable = function(){
if (!this.currentStorage) {
throw new Error('Storage provider not set; e.g EmbarkJS.Storage.setProvider("ipfs")');
}
WIP to merge in other swarm changes Adding swarm to embarkjs. WIP. Add 'auto' setting for geth CORS and websockets origin * 'auto' now supported for `rpcCorsDomain` and `wsOrigins` in the blockchain config. * 'auto' set to the default value in blockchain config for test and demo apps. test add config and contract and add test addFileToPipeline test and registerBeforeDeploy with new arg add more registers but generation one fails in run WIP commit Undo changes to test config. Merge pull request #381 from embark-framework/features/cors-auto Add 'auto' setting for geth CORS and websockets origin fix a bug where upload cmd used plugin name don't error if it's an empty dapp with no contracts yet Merge pull request #383 from embark-framework/no_contracts don't error if it's an empty dapp with no contracts yet remove duplicated entry force zepplein version for travis Merge pull request #384 from embark-framework/chores/test-allpligin-apis Small fixes for plugin APIs intercept logs in the app itself - stopgap fix Merge pull request #385 from embark-framework/console_logs_fix intercept logs in the app itself - stopgap fix * removed unneeded provider property. * add 'swarm' as a provider in the storage.config * update method for swarm service check Merge branch 'develop' into features/add-swarm-to-embarkjs More work to add swarm to embarkjs * added eth-lib to parse result of swarm text * changed "currentStorage" and "currentMessages" to "currentProvider" for consistency. * added protocol to storage config * selectively starts storage service depending on which one is configured in the storage config * run service check for ipfs/swarm prior to uploaded * added swarm methods for embarkjs Updated code based on code review check if testrpc is installed and warn if not Merge pull request #386 from embark-framework/bug_fix/test-rpc-not-installed check if testrpc is installed and warn if not Removed timeout Removed spacer Merge pull request #382 from embark-framework/react-demo Updating embark demo to use react instead of jquery fix on contract add Merge pull request #387 from embark-framework/bug_fix/new-contract-in-empty-dapp Fix adding a contract redeploy with right config on config change fix tests reset watchers after build to make sure files remain watch Merge pull request #389 from embark-framework/bug_fix/file-changes-not-watched Fix files not being watched Merge pull request #388 from embark-framework/bug_fix/changing-contract-config Redeploy with right config on config change Added swarm support in embarkjs and isAvailable for messages/storage * reverted currentProvider back to currentStorage and currentMessages * added `EmbarkJS.Storage.isAvailable` and `EmbarkJS.Messages.isAvailable()` and underlying provider functions for Whisper, Orbit, IPFS, and Swarm * Finished swarm implementation in embarkjs plus cleanup * updated test app storage config to swarm to show swarm config option Merge branch 'develop' into features/add-swarm-to-embarkjs
2018-04-30 05:56:43 +00:00
return this.currentStorage.isAvailable();
};
EmbarkJS.Messages = {};
2017-12-28 01:19:41 +00:00
EmbarkJS.Messages.Providers = {};
EmbarkJS.Messages.registerProvider = function(providerName, obj) {
EmbarkJS.Messages.Providers[providerName] = obj;
};
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);
};
WIP to merge in other swarm changes Adding swarm to embarkjs. WIP. Add 'auto' setting for geth CORS and websockets origin * 'auto' now supported for `rpcCorsDomain` and `wsOrigins` in the blockchain config. * 'auto' set to the default value in blockchain config for test and demo apps. test add config and contract and add test addFileToPipeline test and registerBeforeDeploy with new arg add more registers but generation one fails in run WIP commit Undo changes to test config. Merge pull request #381 from embark-framework/features/cors-auto Add 'auto' setting for geth CORS and websockets origin fix a bug where upload cmd used plugin name don't error if it's an empty dapp with no contracts yet Merge pull request #383 from embark-framework/no_contracts don't error if it's an empty dapp with no contracts yet remove duplicated entry force zepplein version for travis Merge pull request #384 from embark-framework/chores/test-allpligin-apis Small fixes for plugin APIs intercept logs in the app itself - stopgap fix Merge pull request #385 from embark-framework/console_logs_fix intercept logs in the app itself - stopgap fix * removed unneeded provider property. * add 'swarm' as a provider in the storage.config * update method for swarm service check Merge branch 'develop' into features/add-swarm-to-embarkjs More work to add swarm to embarkjs * added eth-lib to parse result of swarm text * changed "currentStorage" and "currentMessages" to "currentProvider" for consistency. * added protocol to storage config * selectively starts storage service depending on which one is configured in the storage config * run service check for ipfs/swarm prior to uploaded * added swarm methods for embarkjs Updated code based on code review check if testrpc is installed and warn if not Merge pull request #386 from embark-framework/bug_fix/test-rpc-not-installed check if testrpc is installed and warn if not Removed timeout Removed spacer Merge pull request #382 from embark-framework/react-demo Updating embark demo to use react instead of jquery fix on contract add Merge pull request #387 from embark-framework/bug_fix/new-contract-in-empty-dapp Fix adding a contract redeploy with right config on config change fix tests reset watchers after build to make sure files remain watch Merge pull request #389 from embark-framework/bug_fix/file-changes-not-watched Fix files not being watched Merge pull request #388 from embark-framework/bug_fix/changing-contract-config Redeploy with right config on config change Added swarm support in embarkjs and isAvailable for messages/storage * reverted currentProvider back to currentStorage and currentMessages * added `EmbarkJS.Storage.isAvailable` and `EmbarkJS.Messages.isAvailable()` and underlying provider functions for Whisper, Orbit, IPFS, and Swarm * Finished swarm implementation in embarkjs plus cleanup * updated test app storage config to swarm to show swarm config option Merge branch 'develop' into features/add-swarm-to-embarkjs
2018-04-30 05:56:43 +00:00
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);
};
2018-05-02 16:10:11 +00:00
EmbarkJS.Messages.listenTo = function(options, callback) {
if (!this.currentMessages) {
throw new Error('Messages provider not set; e.g EmbarkJS.Messages.setProvider("whisper")');
}
2018-05-02 16:10:11 +00:00
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);
2017-10-09 12:59:02 +00:00
},
toAscii: function(str) {
var _web3 = new Web3();
return _web3.utils.toAscii(str);
}
};
2017-12-13 14:01:53 +00:00
export default EmbarkJS;