fix contracts that are instances of

This commit is contained in:
Jonathan Rainville 2018-05-31 09:52:37 -04:00
parent 2ec94feaec
commit a0cfe525a9
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,28 +1,33 @@
/*global contract, config, it, embark*/
const assert = require('assert');
const SomeContract = embark.require('Embark/contracts/SomeContract');
const SimpleStorage = embark.require('Embark/contracts/SimpleStorage');
const MyToken2 = embark.require('Embark/contracts/MyToken2');
config({
contracts: {
"SimpleStorage": {
args: [100]
},
"Token": {
deploy: false,
args: [1000]
},
"MyToken2": {
instanceOf: "Token",
args: [2000]
},
"SomeContract": {
"args": [
["$MyToken2", "$SimpleStorage"],
100
]
}
}
});
contract("SomeContract", function() { contract("SomeContract", function() {
this.timeout(0); this.timeout(0);
before(function(done) {
this.timeout(0);
var contractsConfig = {
"SimpleStorage": {
args: [100]
},
"Token": {
deploy: false,
args: [1000]
},
"MyToken2": {
instanceOf: "Token",
args: [2000]
},
"SomeContract": {
"args": [
["$MyToken2", "$SimpleStorage"],
100
]
}
};
EmbarkSpec.deployAll(contractsConfig, () => { done() });
});
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();