update & fix onDeploy

This commit is contained in:
Iuri Matias 2017-12-20 14:09:35 -05:00
parent d005d3f668
commit 281ebc643d
4 changed files with 59 additions and 5 deletions

View File

@ -80,7 +80,46 @@ class Deploy {
let cmds = ""; let cmds = "";
cmds += codeGenerator.generateContractCode(contract); cmds += codeGenerator.generateContractCode(contract);
cmds += contract.onDeploy.join(';\n');
let withErrors = false;
let regex = /\$\w+/g;
let onDeployCode = contract.onDeploy.map((cmd) => {
let realCmd = cmd.replace(regex, (match) => {
let referedContractName = match.slice(1);
let referedContract = self.contractsManager.getContract(referedContractName);
if (!referedContract) {
self.logger.error('error executing onDeploy for ' + contract.className);
self.logger.error(referedContractName + ' does not exist');
self.logger.error("error running onDeploy: " + cmd);
withErrors = true;
return;
}
if (referedContract && referedContract.deploy === false) {
self.logger.error('error executing onDeploy for ' + contract.className);
self.logger.error(referedContractName + " exists but has been set to not deploy");
self.logger.error("error running onDeploy: " + cmd);
withErrors = true;
return;
}
if (referedContract && !referedContract.deployedAddress) {
self.logger.error('error executing onDeploy for ' + contract.className);
self.logger.error("couldn't find a valid address for " + referedContractName + ". has it been deployed?");
self.logger.error("error running onDeploy: " + cmd);
withErrors = true;
return;
}
return referedContract.deployedAddress;
});
return realCmd;
});
if (withErrors) {
contract.error = "onDeployCmdError";
return callback(new Error("error running onDeploy"));
}
cmds += onDeployCode.join(';\n');
RunCode.doEval(cmds, self.web3); RunCode.doEval(cmds, self.web3);
} }
@ -180,7 +219,7 @@ class Deploy {
} }
self.logger.info("finished deploying contracts"); self.logger.info("finished deploying contracts");
self.logger.trace(arguments); self.logger.trace(arguments);
done(); done(err);
} }
); );

View File

@ -71,8 +71,11 @@ class DeployManager {
chainConfig: self.chainConfig, chainConfig: self.chainConfig,
env: self.config.env env: self.config.env
}); });
deploy.deployAll(function () { deploy.deployAll(function (err) {
if (!err) {
self.events.emit('contractsDeployed', contractsManager); self.events.emit('contractsDeployed', contractsManager);
}
//callback(err, contractsManager);
callback(null, contractsManager); callback(null, contractsManager);
}); });
} }

View File

@ -36,7 +36,7 @@
}, },
"Test": { "Test": {
"onDeploy": [ "onDeploy": [
"Test.changeAddress(web3.eth.accounts[0])" "Test.changeAddress('$MyToken')"
] ]
}, },
"MyToken": { "MyToken": {

View File

@ -22,6 +22,11 @@ describe("Token", function() {
"AlreadyDeployedToken": { "AlreadyDeployedToken": {
"address": "0x123", "address": "0x123",
instanceOf: "Token" instanceOf: "Token"
},
"Test": {
onDeploy: [
"Test.changeAddress('$MyToken', function(){})"
]
} }
}; };
EmbarkSpec.deployAll(contractsConfig, done); EmbarkSpec.deployAll(contractsConfig, done);
@ -57,4 +62,11 @@ describe("Token", function() {
done(); done();
}); });
it("should use onDeploy", function(done) {
Test.addr(function(err, result) {
assert.equal(result, MyToken.address)
done();
});
});
}); });