Merge pull request #753 from embark-framework/dapp_connection

Move dapp connection logic to EmbarkJS
This commit is contained in:
Iuri Matias 2018-09-01 10:00:30 -04:00 committed by GitHub
commit 343bf2489a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 602 additions and 751 deletions

View File

@ -5,4 +5,3 @@ whenEnvIsLoaded(function(){
EmbarkJS.environment = "<%- environment %>"; EmbarkJS.environment = "<%- environment %>";
}); });

View File

@ -1 +1 @@
__mainContext.<%- className %> = new EmbarkJS.Contract({abi: <%- abi %>, address: <%- contractAddress %>, code: '<%- contract.code %>', gasEstimates: <%- gasEstimates %>}); __mainContext.<%- className %> = new EmbarkJS.Blockchain.Contract({abi: <%- abi %>, address: <%- contractAddress %>, code: '<%- contract.code %>', gasEstimates: <%- gasEstimates %>});

View File

@ -1,55 +0,0 @@
function __reduce(arr, memo, iteratee, cb) {
if (typeof cb !== 'function') {
if (typeof memo === 'function' && typeof iteratee === 'function') {
cb = iteratee;
iteratee = memo;
memo = [];
} else {
throw new TypeError('expected callback to be a function');
}
}
if (!Array.isArray(arr)) {
cb(new TypeError('expected an array'));
return;
}
if (typeof iteratee !== 'function') {
cb(new TypeError('expected iteratee to be a function'));
return;
}
(function next(i, acc) {
if (i === arr.length) {
cb(null, acc);
return;
}
iteratee(acc, arr[i], function(err, val) {
if (err) {
cb(err);
return;
}
next(i + 1, val);
});
})(0, memo);
};
function __isNewWeb3_1() {
return (typeof(web3.version) === "string");
};
function __getAccounts(cb) {
if (__isNewWeb3_1()) {
web3.eth.getAccounts().then(function(accounts) {
cb(null, accounts);
return null;
}).catch(function(err) {
cb(err);
return null;
});
return;
}
web3.eth.getAccounts(cb);
};

View File

@ -1,37 +1,3 @@
__reduce(<%- connectionList %>,function(prev, value, next) { EmbarkJS.Blockchain.connect(<%- connectionList %>, {warnAboutMetamask: <%= warnAboutMetamask %>}, function(err) {
if (prev === false) {
return next(null, false);
}
if (value === '$WEB3' && (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined')) {
web3.setProvider(web3.givenProvider);
} else if (value !== '$WEB3' && (typeof Web3 !== 'undefined' && ((typeof web3 === 'undefined') || (typeof web3 !== 'undefined' && (!web3.isConnected || (web3.isConnected && !web3.isConnected())))))) {
if (value.indexOf('ws://') >= 0) {
web3.setProvider(new Web3.providers.WebsocketProvider(value));
} else {
web3.setProvider(new Web3.providers.HttpProvider(value));
}
} else if (value === '$WEB3') {
return next(null, '');
}
__getAccounts(function(err, account) {
if (err) {
next(null, true)
} else {
next(null, false)
}
});
}, function(err, _result) {
__getAccounts(function(err, accounts) {
<% if (warnAboutMetamask) { %>
if (web3.eth.currentProvider && web3.eth.currentProvider.isMetaMask) {
console.log("%cNote: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node", "font-size: 2em");
}
<% } %>
if (accounts) {
web3.eth.defaultAccount = accounts[0];
}
<%- done %> <%- done %>
});
}); });

View File

@ -4,7 +4,6 @@ const utils = require('../../utils/utils.js');
require('ejs'); require('ejs');
const Templates = { const Templates = {
utils: require('./code_templates/utils.js.ejs'),
vanilla_contract: require('./code_templates/vanilla-contract.js.ejs'), vanilla_contract: require('./code_templates/vanilla-contract.js.ejs'),
embarkjs_contract: require('./code_templates/embarkjs-contract.js.ejs'), embarkjs_contract: require('./code_templates/embarkjs-contract.js.ejs'),
exec_when_ready: require('./code_templates/exec-when-ready.js.ejs'), exec_when_ready: require('./code_templates/exec-when-ready.js.ejs'),
@ -104,19 +103,11 @@ class CodeGenerator {
}); });
} }
generateContext() {
let result = "";
result += Templates.main_context();
result += Templates.load_manager();
return result;
}
generateProvider(isDeployment) { generateProvider(isDeployment) {
let self = this; let self = this;
let result = ""; let result = "";
let providerPlugins; let providerPlugins;
result += Templates.utils();
result += Templates.main_context(); result += Templates.main_context();
result += Templates.load_manager(); result += Templates.load_manager();
result += Templates.define_when_env_loaded(); result += Templates.define_when_env_loaded();
@ -312,7 +303,7 @@ class CodeGenerator {
}, },
function getImports(web3Location, next) { function getImports(web3Location, next) {
web3Location = web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes web3Location = web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes
code += "\nimport Web3 from '" + web3Location + "';\n"; code += `\nimport Web3 from '${web3Location}';\n`;
code += "\nimport web3 from 'Embark/web3';\n"; code += "\nimport web3 from 'Embark/web3';\n";
code += "\nimport IpfsApi from 'ipfs-api';\n"; code += "\nimport IpfsApi from 'ipfs-api';\n";
@ -348,13 +339,9 @@ class CodeGenerator {
let contractCode = ""; let contractCode = "";
contractCode += "import web3 from 'Embark/web3';\n"; contractCode += "import web3 from 'Embark/web3';\n";
contractCode += "import EmbarkJS from 'Embark/EmbarkJS';\n"; contractCode += "import EmbarkJS from 'Embark/EmbarkJS';\n";
contractCode += "let " + contractName + "JSONConfig = " + JSON.stringify(contractJSON) + ";\n"; contractCode += `let ${contractName}JSONConfig = ${JSON.stringify(contractJSON)};\n`;
contractCode += "let " + contractName + " = new EmbarkJS.Contract(" + contractName + "JSONConfig);\n"; contractCode += `${contractName}JSONConfig.web3 = web3;\n`;
contractCode += `let ${contractName} = new EmbarkJS.Blockchain.Contract(${contractName}JSONConfig);\n`;
contractCode += "\n__embarkContext.execWhenReady(function() {\n";
contractCode += "\n" + contractName + ".setProvider(web3.currentProvider);\n";
contractCode += "\n" + contractName + ".options.from = web3.eth.defaultAccount;\n";
contractCode += "\n});\n";
contractCode += "export default " + contractName + ";\n"; contractCode += "export default " + contractName + ";\n";
cb(contractCode); cb(contractCode);
@ -377,7 +364,7 @@ class CodeGenerator {
}, },
function getImports(web3Location, next) { function getImports(web3Location, next) {
web3Location = web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes web3Location = web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes
code += "\nimport Web3 from '" + web3Location + "';\n"; code += `\nimport Web3 from '${web3Location}';\n`;
code += "\nglobal.Web3 = Web3;\n"; code += "\nglobal.Web3 = Web3;\n";
code += "\n if (typeof web3 === 'undefined') {"; code += "\n if (typeof web3 === 'undefined') {";
@ -387,7 +374,6 @@ class CodeGenerator {
let providerCode = self.generateProvider(false); let providerCode = self.generateProvider(false);
code += providerCode; code += providerCode;
code += "\nglobal.__embarkContext = __mainContext.__loadManagerInstance;\n";
code += "\nexport default web3;\n"; code += "\nexport default web3;\n";
next(null, code); next(null, code);
} }

View File

@ -157,9 +157,9 @@ __embarkENS.setProvider = function (config) {
.then((id) => { .then((id) => {
const registryAddress = self.registryAddresses[id] || config.registryAddress; const registryAddress = self.registryAddresses[id] || config.registryAddress;
self._isAvailable = true; self._isAvailable = true;
self.ens = new EmbarkJS.Contract({abi: config.registryAbi, address: registryAddress, web3: web3}); self.ens = new EmbarkJS.Blockchain.Contract({abi: config.registryAbi, address: registryAddress, web3: web3});
self.registrar = new EmbarkJS.Contract({abi: config.registrarAbi, address: config.registrarAddress, web3: web3}); self.registrar = new EmbarkJS.Blockchain.Contract({abi: config.registrarAbi, address: config.registrarAddress, web3: web3});
self.resolver = new EmbarkJS.Contract({abi: config.resolverAbi, address: config.resolverAddress, web3: web3}); self.resolver = new EmbarkJS.Blockchain.Contract({abi: config.resolverAbi, address: config.resolverAddress, web3: web3});
}) })
.catch(err => { .catch(err => {
if (err.message.indexOf('Provider not set or invalid') > -1) { if (err.message.indexOf('Provider not set or invalid') > -1) {
@ -192,7 +192,7 @@ __embarkENS.resolve = function (name, callback) {
if (resolverAddress === voidAddress) { if (resolverAddress === voidAddress) {
return cb('Name not yet registered'); return cb('Name not yet registered');
} }
let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress, web3: web3}); let resolverContract = new EmbarkJS.Blockchain.Contract({abi: this.resolverInterface, address: resolverAddress, web3: web3});
resolverContract.methods.addr(node).call(cb); resolverContract.methods.addr(node).call(cb);
}); });
}; };
@ -221,7 +221,7 @@ __embarkENS.lookup = function (address, callback) {
if (resolverAddress === voidAddress) { if (resolverAddress === voidAddress) {
return cb('Address not associated to a resolver'); return cb('Address not associated to a resolver');
} }
let resolverContract = new EmbarkJS.Contract({abi: this.resolverInterface, address: resolverAddress, web3: web3}); let resolverContract = new EmbarkJS.Blockchain.Contract({abi: this.resolverInterface, address: resolverAddress, web3: web3});
resolverContract.methods.name(node).call(cb); resolverContract.methods.name(node).call(cb);
}); });
}; };

View File

@ -317,7 +317,7 @@ class Test {
self.contracts[contract.className] = {}; self.contracts[contract.className] = {};
} }
let newContract = new EmbarkJS.Contract({ let newContract = new EmbarkJS.Blockchain.Contract({
abi: contract.abiDefinition, abi: contract.abiDefinition,
address: contract.deployedAddress, address: contract.deployedAddress,
from: self.web3.eth.defaultAccount, from: self.web3.eth.defaultAccount,

1213
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -40,7 +40,7 @@
"decompress": "^4.2.0", "decompress": "^4.2.0",
"deep-equal": "^1.0.1", "deep-equal": "^1.0.1",
"ejs": "^2.5.8", "ejs": "^2.5.8",
"embarkjs": "^0.3.4", "embarkjs": "^0.4.0",
"eth-ens-namehash": "^2.0.8", "eth-ens-namehash": "^2.0.8",
"eth-lib": "^0.2.8", "eth-lib": "^0.2.8",
"ethereumjs-wallet": "0.6.0", "ethereumjs-wallet": "0.6.0",

View File

@ -42,7 +42,7 @@ describe('embark.CodeGenerator', function() {
let withEmbarkJS = true; let withEmbarkJS = true;
it('should generate contract code', function() { it('should generate contract code', function() {
var contractCode = "\n__mainContext.__loadManagerInstance.execWhenReady(function() {\n __mainContext.SimpleStorage = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x123', code: '12345', gasEstimates: 12000});\n\n});\n__mainContext.__loadManagerInstance.execWhenReady(function() {\n __mainContext.Foo = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x124', code: '123456', gasEstimates: 12000});\n\n});\n"; var contractCode = "\n__mainContext.__loadManagerInstance.execWhenReady(function() {\n __mainContext.SimpleStorage = new EmbarkJS.Blockchain.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x123', code: '12345', gasEstimates: 12000});\n\n});\n__mainContext.__loadManagerInstance.execWhenReady(function() {\n __mainContext.Foo = new EmbarkJS.Blockchain.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x124', code: '123456', gasEstimates: 12000});\n\n});\n";
assert.strictEqual(replaceCRLF(generator.generateContracts(contracts, withEmbarkJS)), contractCode); assert.strictEqual(replaceCRLF(generator.generateContracts(contracts, withEmbarkJS)), contractCode);
}); });
}); });

View File

@ -6,7 +6,7 @@
"css/app.css": ["app/css/**"], "css/app.css": ["app/css/**"],
"images/": ["app/images/**"], "images/": ["app/images/**"],
"js/app.js": ["app/js/index.js"], "js/app.js": ["app/js/index.js"],
"js/test.js": ["app/js/_vendor/jquery.min.js", "app/js/_vendor/async.min.js", "app/js/test.js", "app/js/non_existant_file.js"], "js/test.js": ["app/js/_vendor/jquery.min.js", "app/js/_vendor/async.min.js", "app/js/test.js"],
"index.html": "app/index.html", "index.html": "app/index.html",
"test.html": "app/test.html", "test.html": "app/test.html",
"test2.html": "app/test2.html", "test2.html": "app/test2.html",