fix connecting to correct provider

This commit is contained in:
Iuri Matias 2017-10-07 15:20:51 -04:00
parent e5aab5e2ea
commit a9e63069fb
8 changed files with 355 additions and 251 deletions

View File

@ -89,6 +89,14 @@ return /******/ (function(modules) { // webpackBootstrap
var EmbarkJS = {};
EmbarkJS.isNewWeb3 = function() {
var _web3 = 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;
@ -98,6 +106,18 @@ EmbarkJS.Contract = function(options) {
this.code = '0x' + options.code;
this.web3 = options.web3 || web3;
if (EmbarkJS.isNewWeb3()) {
// TODO:
// add default **from** address
// add gasPrice
var ContractClass = new this.web3.eth.Contract(this.abi, this.address);
ContractClass.setProvider(this.web3.currentProvider);
return ContractClass;
} else {
var ContractClass = this.web3.eth.contract(this.abi);
this.eventList = [];
@ -186,6 +206,7 @@ EmbarkJS.Contract = function(options) {
}
return false;
});
}
};
EmbarkJS.Contract.prototype.deploy = function(args, _options) {

View File

@ -7,6 +7,14 @@
var EmbarkJS = {};
EmbarkJS.isNewWeb3 = function() {
var _web3 = 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;
@ -16,6 +24,18 @@ EmbarkJS.Contract = function(options) {
this.code = '0x' + options.code;
this.web3 = options.web3 || web3;
if (EmbarkJS.isNewWeb3()) {
// TODO:
// add default **from** address
// add gasPrice
var ContractClass = new this.web3.eth.Contract(this.abi, this.address);
ContractClass.setProvider(this.web3.currentProvider);
return ContractClass;
} else {
var ContractClass = this.web3.eth.contract(this.abi);
this.eventList = [];
@ -104,6 +124,7 @@ EmbarkJS.Contract = function(options) {
}
return false;
});
}
};
EmbarkJS.Contract.prototype.deploy = function(args, _options) {

1
js/web3-1.0.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -51,6 +51,43 @@ class CodeGenerator {
return "";
}
result += "\nfunction __reduce(arr, memo, iteratee, cb) {";
result += "\n if (typeof cb !== 'function') {";
result += "\n if (typeof memo === 'function' && typeof iteratee === 'function') {";
result += "\n cb = iteratee;";
result += "\n iteratee = memo;";
result += "\n memo = [];";
result += "\n } else {";
result += "\n throw new TypeError('expected callback to be a function');";
result += "\n }";
result += "\n }";
result += "\n";
result += "\n if (!Array.isArray(arr)) {";
result += "\n cb(new TypeError('expected an array'));";
result += "\n return;";
result += "\n }";
result += "\n";
result += "\n if (typeof iteratee !== 'function') {";
result += "\n cb(new TypeError('expected iteratee to be a function'));";
result += "\n return;";
result += "\n }";
result += "\n";
result += "\n (function next(i, acc) {";
result += "\n if (i === arr.length) {";
result += "\n cb(null, acc);";
result += "\n return;";
result += "\n }";
result += "\n";
result += "\n iteratee(acc, arr[i], function(err, val) {";
result += "\n if (err) {";
result += "\n cb(err);";
result += "\n return;";
result += "\n }";
result += "\n next(i + 1, val);";
result += "\n });";
result += "\n })(0, memo);";
result += "\n};";
result += "\nvar whenEnvIsLoaded = function(cb) {";
result += "\n if (typeof document !== 'undefined' && document !== null) {";
result += "\n document.addEventListener('DOMContentLoaded', cb);";
@ -70,29 +107,41 @@ class CodeGenerator {
} else {
result += "\nwhenEnvIsLoaded(function() {\n";
if (isDeployment) {
result += "\nif (typeof window !== 'undefined') { window.web3 = undefined; }";
if (isDeployment) {
let connection = "http://" + this.contractsConfig.deployment.host + ":" + this.contractsConfig.deployment.port;
result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));';
} else {
let connectionCode = this.contractsConfig.dappConnection.map(function(connection) {
let code = "";
if (connection === '$WEB3') {
code += "if (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {";
code += '\n\tweb3 = new Web3(web3.currentProvider);';
code += '\n}';
} else {
code += "if (typeof Web3 !== 'undefined' && ((typeof web3 === 'undefined') || (typeof web3 !== 'undefined' && !web3.isConnected()))) {";
code += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));';
code += '\n}';
}
return code;
});
let connectionCode = "";
connectionCode += "\n__reduce([";
connectionCode += this.contractsConfig.dappConnection.map((x) => '"' + x + '"').join(',');
connectionCode += "], function(prev, value, next) {";
result += connectionCode.join(' ');
}
connectionCode += "\nif (prev === false) {";
connectionCode += "\n return next(null, false);";
connectionCode += "\n}";
connectionCode += "\n if (value === '$WEB3' && (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined')) {";
connectionCode += '\n\tweb3 = new Web3(web3.currentProvider);';
connectionCode += "\n } else if (value !== '$WEB3' && (typeof Web3 !== 'undefined' && ((typeof web3 === 'undefined') || (typeof web3 !== 'undefined' && (!web3.isConnected || (web3.isConnected && !web3.isConnected())))))) {";
connectionCode += "\n\tweb3 = new Web3(new Web3.providers.HttpProvider(value));"
connectionCode += "\n}";
connectionCode += "\nelse if (value === '$WEB3') {";
connectionCode += "\n\treturn next(null, '');";
connectionCode += "\n}";
connectionCode += "\nweb3.eth.getAccounts(function(err, account) { if(err) { next(null, true) } else { next(null, false) }})";
connectionCode += "\n}, function(err, _result) {";
connectionCode += "\n});";
result += connectionCode;
}
result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];";
result += '\n})';
@ -137,8 +186,10 @@ class CodeGenerator {
result += "\n}";
result += "\nwhenEnvIsLoaded(function() {";
result += "\nif (typeof window !== 'undefined') { window." + className + " = undefined; }";
if (useEmbarkJS) {
result += "\n" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: '" + contract.deployedAddress + "', code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});";
let contractAddress = contract.deployedAddress ? ("'" + contract.deployedAddress + "'") : "undefined";
result += "\n" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: " + contractAddress + ", code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});";
} else {
result += "\n" + className + "Abi = " + abi + ";";
result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);";
@ -212,8 +263,8 @@ class CodeGenerator {
result += this.generateProvider(options.deployment);
result += this.generateContracts(options.useEmbarkJS);
result += this.generateStorageInitialization(options.useEmbarkJS);
result += this.generateCommunicationInitialization(options.useEmbarkJS);
//result += this.generateStorageInitialization(options.useEmbarkJS);
//result += this.generateCommunicationInitialization(options.useEmbarkJS);
return result;
}

View File

@ -256,10 +256,14 @@ Config.prototype.loadFiles = function(files) {
//if (false) {
//readFiles.push(new File({filename: 'web3-' + web3Version + '.js', type: 'custom', resolver: function(callback) {
readFiles.push(new File({filename: 'web3.js', type: 'custom', resolver: function(callback) {
if (web3Version === "1.0.0-beta") {
callback(fs.readFileSync(fs.embarkPath('js/web3-1.0.min.js')).toString());
} else {
let npm = new Npm({logger: self.logger});
npm.getPackageVersion('web3', web3Version, 'dist/web3.min.js', true, function(web3Content) {
callback(web3Content);
});
}
}}));
} else {
readFiles.push(new File({filename: 'web3.js', type: 'embark_internal', path: "js/web3.js"}));

View File

@ -45,6 +45,12 @@ class Npm {
if (getFromGit) {
let repoName = registryJSON.repository.url.replace("git+https://github.com/", "").replace(".git","");
let gitHead = registryJSON.gitHead;
if (!gitHead) {
console.error("Could not download " + packageName + " " + version);
return callback("error");
}
let fileLocation = "https://raw.githubusercontent.com/" + repoName + "/" + gitHead + "/dist/web3.min.js";
let packageDirectory = './.embark/versions/' + packageName + '/' + version + '/';

View File

@ -31,80 +31,80 @@ $(document).ready(function() {
// automatic set if config/storage.json has "enabled": true and "provider": "ipfs"
//EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'});
$("#storage .error").hide();
EmbarkJS.Storage.setProvider('ipfs')
.then(function(){
console.log('Provider set to IPFS');
EmbarkJS.Storage.ipfsConnection.ping()
.then(function(){
$("#status-storage").addClass('status-online');
$("#storage-controls").show();
})
.catch(function(err) {
if(err){
console.log("IPFS Connection Error => " + err.message);
$("#storage .error").show();
$("#status-storage").addClass('status-offline');
$("#storage-controls").hide();
}
});
})
.catch(function(err){
console.log('Failed to set IPFS as Provider:', err.message);
$("#storage .error").show();
$("#status-storage").addClass('status-offline');
$("#storage-controls").hide();
});
//$("#storage .error").hide();
//EmbarkJS.Storage.setProvider('ipfs')
// .then(function(){
// console.log('Provider set to IPFS');
// EmbarkJS.Storage.ipfsConnection.ping()
// .then(function(){
// $("#status-storage").addClass('status-online');
// $("#storage-controls").show();
// })
// .catch(function(err) {
// if(err){
// console.log("IPFS Connection Error => " + err.message);
// $("#storage .error").show();
// $("#status-storage").addClass('status-offline');
// $("#storage-controls").hide();
// }
// });
// })
// .catch(function(err){
// console.log('Failed to set IPFS as Provider:', err.message);
// $("#storage .error").show();
// $("#status-storage").addClass('status-offline');
// $("#storage-controls").hide();
// });
$("#storage button.setIpfsText").click(function() {
var value = $("#storage input.ipfsText").val();
EmbarkJS.Storage.saveText(value).then(function(hash) {
$("span.textHash").html(hash);
$("input.textHash").val(hash);
addToLog("#storage", "EmbarkJS.Storage.saveText('" + value + "').then(function(hash) { })");
})
.catch(function(err) {
if(err){
console.log("IPFS saveText Error => " + err.message);
}
});
});
//$("#storage button.setIpfsText").click(function() {
// var value = $("#storage input.ipfsText").val();
// EmbarkJS.Storage.saveText(value).then(function(hash) {
// $("span.textHash").html(hash);
// $("input.textHash").val(hash);
// addToLog("#storage", "EmbarkJS.Storage.saveText('" + value + "').then(function(hash) { })");
// })
// .catch(function(err) {
// if(err){
// console.log("IPFS saveText Error => " + err.message);
// }
// });
//});
$("#storage button.loadIpfsHash").click(function() {
var value = $("#storage input.textHash").val();
EmbarkJS.Storage.get(value).then(function(content) {
$("span.ipfsText").html(content);
addToLog("#storage", "EmbarkJS.Storage.get('" + value + "').then(function(content) { })");
})
.catch(function(err) {
if(err){
console.log("IPFS get Error => " + err.message);
}
});
});
//$("#storage button.loadIpfsHash").click(function() {
// var value = $("#storage input.textHash").val();
// EmbarkJS.Storage.get(value).then(function(content) {
// $("span.ipfsText").html(content);
// addToLog("#storage", "EmbarkJS.Storage.get('" + value + "').then(function(content) { })");
// })
// .catch(function(err) {
// if(err){
// console.log("IPFS get Error => " + err.message);
// }
// });
//});
$("#storage button.uploadFile").click(function() {
var input = $("#storage input[type=file]");
EmbarkJS.Storage.uploadFile(input).then(function(hash) {
$("span.fileIpfsHash").html(hash);
$("input.fileIpfsHash").val(hash);
addToLog("#storage", "EmbarkJS.Storage.uploadFile($('input[type=file]')).then(function(hash) { })");
})
.catch(function(err) {
if(err){
console.log("IPFS uploadFile Error => " + err.message);
}
});
});
//$("#storage button.uploadFile").click(function() {
// var input = $("#storage input[type=file]");
// EmbarkJS.Storage.uploadFile(input).then(function(hash) {
// $("span.fileIpfsHash").html(hash);
// $("input.fileIpfsHash").val(hash);
// addToLog("#storage", "EmbarkJS.Storage.uploadFile($('input[type=file]')).then(function(hash) { })");
// })
// .catch(function(err) {
// if(err){
// console.log("IPFS uploadFile Error => " + err.message);
// }
// });
//});
$("#storage button.loadIpfsFile").click(function() {
var hash = $("#storage input.fileIpfsHash").val();
var url = EmbarkJS.Storage.getUrl(hash);
var link = '<a href="' + url + '" target="_blank">' + url + '</a>';
$("span.ipfsFileUrl").html(link);
$(".ipfsImage").attr('src', url);
addToLog("#storage", "EmbarkJS.Storage.getUrl('" + hash + "')");
});
//$("#storage button.loadIpfsFile").click(function() {
// var hash = $("#storage input.fileIpfsHash").val();
// var url = EmbarkJS.Storage.getUrl(hash);
// var link = '<a href="' + url + '" target="_blank">' + url + '</a>';
// $("span.ipfsFileUrl").html(link);
// $(".ipfsImage").attr('src', url);
// addToLog("#storage", "EmbarkJS.Storage.getUrl('" + hash + "')");
//});
});
@ -113,37 +113,37 @@ $(document).ready(function() {
// ===========================
$(document).ready(function() {
$("#communication .error").hide();
$("#communication .errorVersion").hide();
web3.version.getWhisper(function(err, version) {
if (err) {
$("#communication .error").show();
$("#communication-controls").hide();
$("#status-communication").addClass('status-offline');
} else if (version >= 5) {
$("#communication .errorVersion").show();
$("#communication-controls").hide();
$("#status-communication").addClass('status-offline');
} else {
EmbarkJS.Messages.setProvider('whisper');
$("#status-communication").addClass('status-online');
}
});
//$("#communication .error").hide();
//$("#communication .errorVersion").hide();
//web3.version.getWhisper(function(err, version) {
// if (err) {
// $("#communication .error").show();
// $("#communication-controls").hide();
// $("#status-communication").addClass('status-offline');
// } else if (version >= 5) {
// $("#communication .errorVersion").show();
// $("#communication-controls").hide();
// $("#status-communication").addClass('status-offline');
// } else {
// EmbarkJS.Messages.setProvider('whisper');
// $("#status-communication").addClass('status-online');
// }
//});
$("#communication button.listenToChannel").click(function() {
var channel = $("#communication .listen input.channel").val();
$("#communication #subscribeList").append("<br> subscribed to " + channel + " now try sending a message");
EmbarkJS.Messages.listenTo({topic: [channel]}).then(function(message) {
$("#communication #messagesList").append("<br> channel: " + channel + " message: " + message);
});
addToLog("#communication", "EmbarkJS.Messages.listenTo({topic: ['" + channel + "']}).then(function(message) {})");
});
//$("#communication button.listenToChannel").click(function() {
// var channel = $("#communication .listen input.channel").val();
// $("#communication #subscribeList").append("<br> subscribed to " + channel + " now try sending a message");
// EmbarkJS.Messages.listenTo({topic: [channel]}).then(function(message) {
// $("#communication #messagesList").append("<br> channel: " + channel + " message: " + message);
// });
// addToLog("#communication", "EmbarkJS.Messages.listenTo({topic: ['" + channel + "']}).then(function(message) {})");
//});
$("#communication button.sendMessage").click(function() {
var channel = $("#communication .send input.channel").val();
var message = $("#communication .send input.message").val();
EmbarkJS.Messages.sendMessage({topic: channel, data: message});
addToLog("#communication", "EmbarkJS.Messages.sendMessage({topic: '" + channel + "', data: '" + message + "'})");
});
//$("#communication button.sendMessage").click(function() {
// var channel = $("#communication .send input.channel").val();
// var message = $("#communication .send input.message").val();
// EmbarkJS.Messages.sendMessage({topic: channel, data: message});
// addToLog("#communication", "EmbarkJS.Messages.sendMessage({topic: '" + channel + "', data: '" + message + "'})");
//});
});

View File

@ -1,7 +1,7 @@
{
"default": {
"versions": {
"web3.js": "1.0.0-beta.18",
"web3.js": "1.0.0-beta",
"solc": "0.4.11"
},
"deployment": {
@ -42,7 +42,7 @@
"args": [200]
},
"AlreadyDeployedToken": {
"address": "0x123",
"address": "0xece374063fe5cc7efbaca0a498477cada94e5ad6",
"instanceOf": "Token"
}
}