support for stubs

This commit is contained in:
Iuri Matias 2015-07-31 22:50:51 -04:00
parent 28783e77bb
commit e9fab5451f
6 changed files with 144 additions and 2 deletions

View File

@ -13,6 +13,7 @@ ContractsConfig.prototype.init = function(files) {
this.contractDB = {};
this.contractFiles = files;
this.contractDependencies = {};
this.contractStubs = {};
//TODO: have to specify environment otherwise wouldn't work with staging
if (this.blockchainConfig.config != undefined) {
@ -38,6 +39,15 @@ ContractsConfig.prototype.config = function(env) {
return this.contractConfig[env];
};
ContractsConfig.prototype.is_a_token = function(target, compiled_contracts) {
for (var className in compiled_contracts) {
if (this.contractStubs[className] && this.contractStubs[className].indexOf(target) >= 0) {
return true;
}
}
return false;
};
ContractsConfig.prototype.compileContracts = function(env) {
var contractFile, source, j;
var contractsConfig = this.config(env);
@ -59,6 +69,8 @@ ContractsConfig.prototype.compileContracts = function(env) {
this.contractDependencies[className].push(arg.substr(1));
}
}
this.contractStubs[className] = options.stubs;
}
}
@ -68,10 +80,14 @@ ContractsConfig.prototype.compileContracts = function(env) {
contractFile = this.contractFiles[j];
source = fs.readFileSync(contractFile).toString()
console.log("compiling " + contractFile);
compiled_contracts = this.compiler.compile(source);
for (className in compiled_contracts) {
for (var className in compiled_contracts) {
var contract = compiled_contracts[className];
if (this.is_a_token(className, compiled_contracts)) {
continue;
}
all_compiled_contracts[className] = contract;
this.all_contracts.push(className);
this.contractDB[className] = {

View File

@ -91,6 +91,23 @@ describe('embark.config.contracts', function() {
});
});
context("contracts as arguments to other contracts with stubs", function() {
before(function() {
files = [
'test/support/contracts/crowdsale.sol',
'test/support/contracts/token.sol'
]
contractsConfig = new Config.Contracts(blockchainConfig, compiler);
contractsConfig.loadConfigFile('test/support/arguments2.yml');
contractsConfig.init(files);
contractsConfig.compileContracts('development');
});
it('add contracts to a list', function() {
assert.deepEqual(contractsConfig.all_contracts, [ "token", "Crowdsale" ]);
});
});
});
});

View File

@ -66,6 +66,33 @@ describe('embark.deploy', function() {
});
});
describe('contracts as arguments to other contracts with stubs', function() {
var files = [
'test/support/contracts/crowdsale.sol',
'test/support/contracts/token.sol'
];
describe('#deploy_contracts', function() {
var deploy = setDeployConfig({
files: files,
blockchain: 'test/support/blockchain.yml',
contracts: 'test/support/arguments2.yml'
});
deploy.deploy_contracts("development");
it("should deploy contracts", function() {
var all_contracts = ['token', 'Crowdsale'];
for(var i=0; i < all_contracts.length; i++) {
var className = all_contracts[i];
assert.equal(deploy.deployedContracts.hasOwnProperty(className), true);
}
});
});
});
describe('contracts instances', function() {
var files = [
'test/support/contracts/simple_storage.sol'

View File

@ -0,0 +1,13 @@
development:
token:
args:
Crowdsale:
stubs:
- token
args:
- 0x123
- 100000000000000000000
- 30
- 20000000000000000
- $token
staging:

View File

@ -0,0 +1,51 @@
contract token { mapping (address => uint) public coinBalanceOf; function token() {} function sendCoin(address receiver, uint amount) returns(bool sufficient) { } }
contract Crowdsale {
address public beneficiary;
uint public fundingGoal; uint public amountRaised; uint public deadline; uint public price;
token public tokenReward;
Funder[] public funders;
event FundTransfer(address backer, uint amount, bool isContribution);
/* data structure to hold information about campaign contributors */
struct Funder {
address addr;
uint amount;
}
/* at initialization, setup the owner */
function Crowdsale(address _beneficiary, uint _fundingGoal, uint _duration, uint _price, token _reward) {
beneficiary = _beneficiary;
fundingGoal = _fundingGoal;
deadline = now + _duration * 1 minutes;
price = _price;
tokenReward = token(_reward);
}
/* The function without name is the default function that is called whenever anyone sends funds to a contract */
function () {
uint amount = msg.value;
funders[funders.length++] = Funder({addr: msg.sender, amount: amount});
amountRaised += amount;
tokenReward.sendCoin(msg.sender, amount / price);
FundTransfer(msg.sender, amount, true);
}
modifier afterDeadline() { if (now >= deadline) _ }
/* checks if the goal or time limit has been reached and ends the campaign */
function checkGoalReached() afterDeadline {
if (amountRaised >= fundingGoal){
beneficiary.send(amountRaised);
FundTransfer(beneficiary, amountRaised, false);
} else {
FundTransfer(0, 11, false);
for (uint i = 0; i < funders.length; ++i) {
funders[i].addr.send(funders[i].amount);
FundTransfer(funders[i].addr, funders[i].amount, false);
}
}
suicide(beneficiary);
}
}

View File

@ -0,0 +1,18 @@
contract token {
mapping (address => uint) public coinBalanceOf;
event CoinTransfer(address sender, address receiver, uint amount);
/* Initializes contract with initial supply tokens to the creator of the contract */
function token(uint supply) {
coinBalanceOf[msg.sender] = (supply || 10000);
}
/* Very simple trade function */
function sendCoin(address receiver, uint amount) returns(bool sufficient) {
if (coinBalanceOf[msg.sender] < amount) return false;
coinBalanceOf[msg.sender] -= amount;
coinBalanceOf[receiver] += amount;
CoinTransfer(msg.sender, receiver, amount);
return true;
}
}