fix contracts that are instances of

This commit is contained in:
Jonathan Rainville 2018-05-31 09:52:37 -04:00
parent 6712fc294c
commit dbb2352928
2 changed files with 51 additions and 27 deletions

View File

@ -132,6 +132,9 @@ class Test {
}, },
function createContractObject(next) { function createContractObject(next) {
async.each(Object.keys(self.contracts), (contractName, eachCb) => { async.each(Object.keys(self.contracts), (contractName, eachCb) => {
if (!self.engine.contractsManager.contracts[contractName]) {
throw new Error(__('No contract with the name %s', contractName));
}
const contract = self.engine.contractsManager.contracts[contractName]; const contract = self.engine.contractsManager.contracts[contractName];
Object.assign(self.contracts[contractName], new self.web3.eth.Contract(contract.abiDefinition, contract.address, Object.assign(self.contracts[contractName], new self.web3.eth.Contract(contract.abiDefinition, contract.address,
{from: self.web3.eth.defaultAccount, gas: 6000000})); {from: self.web3.eth.defaultAccount, gas: 6000000}));
@ -150,13 +153,29 @@ class Test {
require(module) { require(module) {
if (module.startsWith('Embark/contracts/')) { if (module.startsWith('Embark/contracts/')) {
const contractName = module.substr(17); const contractName = module.substr(17);
if (!this.engine.contractsManager.contracts[contractName]) {
throw new Error(__('No contract with the name %s', contractName));
}
if (this.contracts[contractName]) { if (this.contracts[contractName]) {
return this.contracts[contractName]; return this.contracts[contractName];
} }
const contract = this.engine.contractsManager.contracts[contractName]; let contract = this.engine.contractsManager.contracts[contractName];
if (!contract) {
const contractNames = Object.keys(this.engine.contractsManager.contracts);
// It is probably an instanceof
contractNames.find(contrName => {
// Find a contract with a similar name
if (contractName.indexOf(contrName) > -1) {
contract = this.engine.contractsManager.contracts[contrName];
return true;
}
return false;
});
// If still nothing, assign bogus one, we will redefine it anyway on deploy
if (!contract) {
console.warn(__('Could not recognize the contract name "%s"'));
console.warn(__('If it is an instance of another contract, it will be reassigned on deploy'));
console.warn(__('Otherwise, you can rename the contract to contain the parent contract in the name eg: Token2 for Token'));
contract = this.engine.contractsManager.contracts[contractNames[0]];
}
}
this.contracts[contractName] = new this.web3.eth.Contract(contract.abiDefinition, contract.address, this.contracts[contractName] = new this.web3.eth.Contract(contract.abiDefinition, contract.address,
{from: this.web3.eth.defaultAccount, gas: 6000000}); {from: this.web3.eth.defaultAccount, gas: 6000000});
return this.contracts[contractName]; return this.contracts[contractName];

View File

@ -1,8 +1,11 @@
contract("SomeContract", function() { /*global contract, config, it, embark*/
this.timeout(0); const assert = require('assert');
before(function(done) { const SomeContract = embark.require('Embark/contracts/SomeContract');
this.timeout(0); const SimpleStorage = embark.require('Embark/contracts/SimpleStorage');
var contractsConfig = { const MyToken2 = embark.require('Embark/contracts/MyToken2');
config({
contracts: {
"SimpleStorage": { "SimpleStorage": {
args: [100] args: [100]
}, },
@ -20,10 +23,12 @@ contract("SomeContract", function() {
100 100
] ]
} }
}; }
EmbarkSpec.deployAll(contractsConfig, () => { done() });
}); });
contract("SomeContract", function() {
this.timeout(0);
it("set MyToken2 address", async function() { it("set MyToken2 address", async function() {
let address = await SomeContract.methods.addr_1().call(); let address = await SomeContract.methods.addr_1().call();
assert.equal(address, MyToken2.options.address); assert.equal(address, MyToken2.options.address);