Skip to content

Commit e9fab54

Browse files
committed
support for stubs
1 parent 28783e7 commit e9fab54

File tree

6 files changed

+144
-2
lines changed

6 files changed

+144
-2
lines changed

lib/config/contracts.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ContractsConfig.prototype.init = function(files) {
1313
this.contractDB = {};
1414
this.contractFiles = files;
1515
this.contractDependencies = {};
16+
this.contractStubs = {};
1617

1718
//TODO: have to specify environment otherwise wouldn't work with staging
1819
if (this.blockchainConfig.config != undefined) {
@@ -38,6 +39,15 @@ ContractsConfig.prototype.config = function(env) {
3839
return this.contractConfig[env];
3940
};
4041

42+
ContractsConfig.prototype.is_a_token = function(target, compiled_contracts) {
43+
for (var className in compiled_contracts) {
44+
if (this.contractStubs[className] && this.contractStubs[className].indexOf(target) >= 0) {
45+
return true;
46+
}
47+
}
48+
return false;
49+
};
50+
4151
ContractsConfig.prototype.compileContracts = function(env) {
4252
var contractFile, source, j;
4353
var contractsConfig = this.config(env);
@@ -59,6 +69,8 @@ ContractsConfig.prototype.compileContracts = function(env) {
5969
this.contractDependencies[className].push(arg.substr(1));
6070
}
6171
}
72+
73+
this.contractStubs[className] = options.stubs;
6274
}
6375
}
6476

@@ -68,10 +80,14 @@ ContractsConfig.prototype.compileContracts = function(env) {
6880
contractFile = this.contractFiles[j];
6981
source = fs.readFileSync(contractFile).toString()
7082

71-
console.log("compiling " + contractFile);
7283
compiled_contracts = this.compiler.compile(source);
73-
for (className in compiled_contracts) {
84+
for (var className in compiled_contracts) {
7485
var contract = compiled_contracts[className];
86+
87+
if (this.is_a_token(className, compiled_contracts)) {
88+
continue;
89+
}
90+
7591
all_compiled_contracts[className] = contract;
7692
this.all_contracts.push(className);
7793
this.contractDB[className] = {

test/config.contracts.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ describe('embark.config.contracts', function() {
9191
});
9292
});
9393

94+
context("contracts as arguments to other contracts with stubs", function() {
95+
before(function() {
96+
files = [
97+
'test/support/contracts/crowdsale.sol',
98+
'test/support/contracts/token.sol'
99+
]
100+
contractsConfig = new Config.Contracts(blockchainConfig, compiler);
101+
contractsConfig.loadConfigFile('test/support/arguments2.yml');
102+
contractsConfig.init(files);
103+
contractsConfig.compileContracts('development');
104+
});
105+
106+
it('add contracts to a list', function() {
107+
assert.deepEqual(contractsConfig.all_contracts, [ "token", "Crowdsale" ]);
108+
});
109+
});
110+
94111
});
95112

96113
});

test/deploy.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,33 @@ describe('embark.deploy', function() {
6666
});
6767
});
6868

69+
describe('contracts as arguments to other contracts with stubs', function() {
70+
var files = [
71+
'test/support/contracts/crowdsale.sol',
72+
'test/support/contracts/token.sol'
73+
];
74+
75+
describe('#deploy_contracts', function() {
76+
var deploy = setDeployConfig({
77+
files: files,
78+
blockchain: 'test/support/blockchain.yml',
79+
contracts: 'test/support/arguments2.yml'
80+
});
81+
deploy.deploy_contracts("development");
82+
83+
it("should deploy contracts", function() {
84+
var all_contracts = ['token', 'Crowdsale'];
85+
for(var i=0; i < all_contracts.length; i++) {
86+
var className = all_contracts[i];
87+
88+
assert.equal(deploy.deployedContracts.hasOwnProperty(className), true);
89+
}
90+
});
91+
92+
});
93+
94+
});
95+
6996
describe('contracts instances', function() {
7097
var files = [
7198
'test/support/contracts/simple_storage.sol'

test/support/arguments2.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
development:
2+
token:
3+
args:
4+
Crowdsale:
5+
stubs:
6+
- token
7+
args:
8+
- 0x123
9+
- 100000000000000000000
10+
- 30
11+
- 20000000000000000
12+
- $token
13+
staging:
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
contract token { mapping (address => uint) public coinBalanceOf; function token() {} function sendCoin(address receiver, uint amount) returns(bool sufficient) { } }
2+
3+
contract Crowdsale {
4+
5+
address public beneficiary;
6+
uint public fundingGoal; uint public amountRaised; uint public deadline; uint public price;
7+
token public tokenReward;
8+
Funder[] public funders;
9+
event FundTransfer(address backer, uint amount, bool isContribution);
10+
11+
/* data structure to hold information about campaign contributors */
12+
struct Funder {
13+
address addr;
14+
uint amount;
15+
}
16+
17+
/* at initialization, setup the owner */
18+
function Crowdsale(address _beneficiary, uint _fundingGoal, uint _duration, uint _price, token _reward) {
19+
beneficiary = _beneficiary;
20+
fundingGoal = _fundingGoal;
21+
deadline = now + _duration * 1 minutes;
22+
price = _price;
23+
tokenReward = token(_reward);
24+
}
25+
26+
/* The function without name is the default function that is called whenever anyone sends funds to a contract */
27+
function () {
28+
uint amount = msg.value;
29+
funders[funders.length++] = Funder({addr: msg.sender, amount: amount});
30+
amountRaised += amount;
31+
tokenReward.sendCoin(msg.sender, amount / price);
32+
FundTransfer(msg.sender, amount, true);
33+
}
34+
35+
modifier afterDeadline() { if (now >= deadline) _ }
36+
37+
/* checks if the goal or time limit has been reached and ends the campaign */
38+
function checkGoalReached() afterDeadline {
39+
if (amountRaised >= fundingGoal){
40+
beneficiary.send(amountRaised);
41+
FundTransfer(beneficiary, amountRaised, false);
42+
} else {
43+
FundTransfer(0, 11, false);
44+
for (uint i = 0; i < funders.length; ++i) {
45+
funders[i].addr.send(funders[i].amount);
46+
FundTransfer(funders[i].addr, funders[i].amount, false);
47+
}
48+
}
49+
suicide(beneficiary);
50+
}
51+
}

test/support/contracts/token.sol

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
contract token {
2+
mapping (address => uint) public coinBalanceOf;
3+
event CoinTransfer(address sender, address receiver, uint amount);
4+
5+
/* Initializes contract with initial supply tokens to the creator of the contract */
6+
function token(uint supply) {
7+
coinBalanceOf[msg.sender] = (supply || 10000);
8+
}
9+
10+
/* Very simple trade function */
11+
function sendCoin(address receiver, uint amount) returns(bool sufficient) {
12+
if (coinBalanceOf[msg.sender] < amount) return false;
13+
coinBalanceOf[msg.sender] -= amount;
14+
coinBalanceOf[receiver] += amount;
15+
CoinTransfer(msg.sender, receiver, amount);
16+
return true;
17+
}
18+
}

0 commit comments

Comments
 (0)