use requests instead of contractsManager directly

This commit is contained in:
Iuri Matias 2018-05-22 14:49:25 -04:00
parent 5f64376c84
commit 3f9c55131b
3 changed files with 45 additions and 42 deletions

View File

@ -45,33 +45,32 @@ class CodeGenerator {
// deprecated events; to remove in embark 2.7.0
this.events.setCommandHandlerOnce('abi-vanila', function(cb) {
let vanillaABI = self.generateABI({useEmbarkJS: false});
this.events.request("contracts:list", (contractsList) => {
let vanillaABI = self.generateABI(contractsList, {useEmbarkJS: false});
let contractsJSON = self.generateContractsJSON(contractsList);
cb(vanillaABI, contractsJSON);
});
});
this.events.setCommandHandlerOnce('abi', function(cb) {
let embarkJSABI = self.generateABI({useEmbarkJS: true});
this.events.request("contracts:list", (contractsList) => {
let embarkJSABI = self.generateABI(contractsList, {useEmbarkJS: true});
let contractsJSON = self.generateContractsJSON(contractsList);
cb(embarkJSABI, contractsJSON);
});
});
this.events.setCommandHandlerOnce('abi-contracts-vanila', function(cb) {
let vanillaContractsABI = self.generateContracts(false, true, false);
self.events.request("contracts:list", (contractsList) => {
let vanillaContractsABI = self.generateContracts(contractsList, false, true, false);
let contractsJSON = self.generateContractsJSON(contractsList);
cb(vanillaContractsABI, contractsJSON);
});
});
this.events.setCommandHandlerOnce('abi-vanila-deployment', function(cb) {
let vanillaABI = self.generateABI({useEmbarkJS: false, deployment: true});
self.events.request("contracts:list", (contractsList) => {
let vanillaABI = self.generateABI(contractsList, {useEmbarkJS: false, deployment: true});
let contractsJSON = self.generateContractsJSON(contractsList);
cb(vanillaABI, contractsJSON);
});
@ -79,32 +78,32 @@ class CodeGenerator {
// new events
this.events.setCommandHandlerOnce('code-vanila', function(cb) {
let vanillaABI = self.generateABI({useEmbarkJS: false});
self.events.request("contracts:list", (contractsList) => {
let vanillaABI = self.generateABI(contractsList, {useEmbarkJS: false});
let contractsJSON = self.generateContractsJSON(contractsList);
cb(vanillaABI, contractsJSON);
});
});
this.events.setCommandHandlerOnce('code', function(cb) {
let embarkJSABI = self.generateABI({useEmbarkJS: true});
self.events.request("contracts:list", (contractsList) => {
let embarkJSABI = self.generateABI(contractsList, {useEmbarkJS: true});
let contractsJSON = self.generateContractsJSON(contractsList);
cb(embarkJSABI, contractsJSON);
});
});
this.events.setCommandHandlerOnce('code-contracts-vanila', function(cb) {
let vanillaContractsABI = self.generateContracts(false, true, false);
self.events.request("contracts:list", (contractsList) => {
let vanillaContractsABI = self.generateContracts(contractsList, false, true, false);
let contractsJSON = self.generateContractsJSON(contractsList);
cb(vanillaContractsABI, contractsJSON);
});
});
this.events.setCommandHandlerOnce('code-vanila-deployment', function(cb) {
let vanillaABI = self.generateABI({useEmbarkJS: false, deployment: true});
self.events.request("contracts:list", (contractsList) => {
let vanillaABI = self.generateABI(contractsList, {useEmbarkJS: false, deployment: true});
let contractsJSON = self.generateContractsJSON(contractsList);
cb(vanillaABI, contractsJSON);
});
@ -183,16 +182,15 @@ class CodeGenerator {
return result;
}
generateContracts(useEmbarkJS, isDeployment, useLoader) {
generateContracts(contractsList, useEmbarkJS, isDeployment, useLoader) {
let self = this;
let result = "\n";
let contractsPlugins;
if (useLoader === false) {
for (let className in this.contractsManager.contracts) {
let contract = this.contractsManager.contracts[className];
for (let contract of contractsList) {
let abi = JSON.stringify(contract.abiDefinition);
result += Templates.vanilla_contract({className: className, abi: abi, contract: contract, gasLimit: 6000000});
result += Templates.vanilla_contract({className: contract.className, abi: abi, contract: contract, gasLimit: 6000000});
}
return result;
}
@ -207,12 +205,10 @@ class CodeGenerator {
if (this.plugins && contractsPlugins.length > 0) {
contractsPlugins.forEach(function (plugin) {
result += plugin.generateContracts({contracts: self.contractsManager.contracts});
result += plugin.generateContracts({contracts: contractsList});
});
} else {
for (let className in this.contractsManager.contracts) {
let contract = this.contractsManager.contracts[className];
for (let contract of contractsList) {
let abi = JSON.stringify(contract.abiDefinition);
let gasEstimates = JSON.stringify(contract.gasEstimates);
@ -220,9 +216,9 @@ class CodeGenerator {
if (useEmbarkJS) {
let contractAddress = contract.deployedAddress ? ("'" + contract.deployedAddress + "'") : "undefined";
block += Templates.embarkjs_contract({className: className, abi: abi, contract: contract, contractAddress: contractAddress, gasEstimates: gasEstimates});
block += Templates.embarkjs_contract({className: contract.className, abi: abi, contract: contract, contractAddress: contractAddress, gasEstimates: gasEstimates});
} else {
block += Templates.vanilla_contract({className: className, abi: abi, contract: contract, gasLimit: (isDeployment ? 6000000 : false)});
block += Templates.vanilla_contract({className: contract.className, abi: abi, contract: contract, gasLimit: (isDeployment ? 6000000 : false)});
}
result += Templates.exec_when_ready({block: block});
@ -286,11 +282,11 @@ class CodeGenerator {
return result;
}
generateABI(options) {
generateABI(contractsList, options) {
let result = "";
result += this.generateProvider(options.deployment);
result += this.generateContracts(options.useEmbarkJS, options.deployment, true);
result += this.generateContracts(contractsList, options.useEmbarkJS, options.deployment, true);
result += this.generateStorageInitialization(options.useEmbarkJS);
result += this.generateCommunicationInitialization(options.useEmbarkJS);
result += this.generateNamesInitialization(options.useEmbarkJS);

View File

@ -12,20 +12,27 @@ describe('embark.CodeGenerator', function() {
this.timeout(0);
describe('#generateContracts', function() {
let generator = new CodeGenerator({blockchainConfig: {}, contractsManager: {
contracts: {
SimpleStorage: {
let contracts = [
{
className: 'SimpleStorage',
abiDefinition: [{"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"}],
gasEstimates: 12000,
deployedAddress: "0x123",
code: '12345'
},
Foo: {
{
className: 'Foo',
abiDefinition: [{"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"}],
gasEstimates: 12000,
deployedAddress: "0x124",
code: '123456'
}
]
let generator = new CodeGenerator({blockchainConfig: {}, contractsManager: {
contracts: {
SimpleStorage: contracts[0],
Foo: contracts[1]
}
}});
@ -34,7 +41,7 @@ describe('embark.CodeGenerator', 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";
assert.strictEqual(replaceCRLF(generator.generateContracts(withEmbarkJS)), contractCode);
assert.strictEqual(replaceCRLF(generator.generateContracts(contracts, withEmbarkJS)), contractCode);
});
});
@ -43,7 +50,7 @@ describe('embark.CodeGenerator', function() {
it('should generate contract code', function() {
var contractCode = "\n__mainContext.__loadManagerInstance.execWhenReady(function() {\n SimpleStorageAbi = [{\"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\"}];\nSimpleStorage = new web3.eth.Contract(SimpleStorageAbi);\nSimpleStorage.options.address = '0x123';\nSimpleStorage.address = '0x123';\nSimpleStorage.options.from = web3.eth.defaultAccount;\n\n\n});\n__mainContext.__loadManagerInstance.execWhenReady(function() {\n FooAbi = [{\"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\"}];\nFoo = new web3.eth.Contract(FooAbi);\nFoo.options.address = '0x124';\nFoo.address = '0x124';\nFoo.options.from = web3.eth.defaultAccount;\n\n\n});\n";
assert.strictEqual(replaceCRLF(generator.generateContracts(withEmbarkJS)), contractCode);
assert.strictEqual(replaceCRLF(generator.generateContracts(contracts, withEmbarkJS)), contractCode);
});
});

View File

@ -89,7 +89,7 @@
"dom-helpers": {
"version": "3.3.1",
"resolved": "https://registry.npmjs.org/dom-helpers/-/dom-helpers-3.3.1.tgz",
"integrity": "sha1-/BpOFf/fYN3eA6SAqcD+zoId1KY="
"integrity": "sha512-2Sm+JaYn74OiTM2wHvxJOo3roiq/h25Yi69Fqk269cNUwIXsCvATB6CRSFC9Am/20G2b28hGv/+7NiWydIrPvg=="
},
"dotenv": {
"version": "4.0.0",
@ -229,7 +229,7 @@
"node-fetch": {
"version": "1.7.3",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz",
"integrity": "sha1-mA9vcthSEaU0fGsrwYxbhMPrR+8=",
"integrity": "sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==",
"requires": {
"encoding": "0.1.12",
"is-stream": "1.1.0"
@ -252,7 +252,7 @@
"promise": {
"version": "7.3.1",
"resolved": "https://registry.npmjs.org/promise/-/promise-7.3.1.tgz",
"integrity": "sha1-BktyYCsY+Q8pGSuLG8QY/9Hr078=",
"integrity": "sha512-nolQXZ/4L+bP/UGlkfaIujX9BKxGwmQ9OT4mOt5yvy8iK1h3wqTEJCijzGANTCCl9nWjY41juyAn2K3Q1hLLTg==",
"requires": {
"asap": "2.0.6"
}
@ -289,7 +289,7 @@
"react-bootstrap": {
"version": "0.32.1",
"resolved": "https://registry.npmjs.org/react-bootstrap/-/react-bootstrap-0.32.1.tgz",
"integrity": "sha1-YGJMG0ijnXc+9szmQhpPM+zBZrs=",
"integrity": "sha512-RbfzKUbsukWsToWqGHfCCyMFq9QQI0TznutdyxyJw6dih2NvIne25Mrssg8LZsprqtPpyQi8bN0L0Fx3fUsL8Q==",
"requires": {
"babel-runtime": "6.26.0",
"classnames": "2.2.5",
@ -319,7 +319,7 @@
"react-overlays": {
"version": "0.8.3",
"resolved": "https://registry.npmjs.org/react-overlays/-/react-overlays-0.8.3.tgz",
"integrity": "sha1-+tZe6lskMBzKGSoWn13dsLINOsU=",
"integrity": "sha512-h6GT3jgy90PgctleP39Yu3eK1v9vaJAW73GOA/UbN9dJ7aAN4BTZD6793eI1D5U+ukMk17qiqN/wl3diK1Z5LA==",
"requires": {
"classnames": "2.2.5",
"dom-helpers": "3.3.1",
@ -353,7 +353,7 @@
"regenerator-runtime": {
"version": "0.11.1",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz",
"integrity": "sha1-vgWtf5v30i4Fb5cmzuUBf78Z4uk="
"integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg=="
},
"setimmediate": {
"version": "1.0.5",
@ -412,7 +412,7 @@
"zeppelin-solidity": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/zeppelin-solidity/-/zeppelin-solidity-1.8.0.tgz",
"integrity": "sha1-BJ/N59rqn8hSEPjG25+M0auKhTo=",
"integrity": "sha512-7Mxq6Y7EES0PSLrRF6v0EVYqBVRRo8hFrr7m3jEs69VbbQ5kpANzizeEdbP1/PWKSOmBOg208qP2vSA0FlzFLA==",
"requires": {
"dotenv": "4.0.0",
"ethjs-abi": "0.2.1"