Chapter 5
DappTokenSale.sol
pragma solidity >=0.4.22 <0.7.0;
import "./DappToken.sol";
contract DappTokenSale {
address admin;
DappToken public tokenContract;
uint256 public tokenPrice;
constructor(DappToken _tokenContract, uint256 _tokenPrice) public {
// 1. Assign an Admin
admin = msg.sender;
// 2. Token Contract
tokenContract = _tokenContract;
//3. Token Price
tokenPrice = _tokenPrice;
}
}
import "./DappToken.sol";
contract DappTokenSale {
address admin;
DappToken public tokenContract;
uint256 public tokenPrice;
constructor(DappToken _tokenContract, uint256 _tokenPrice) public {
// 1. Assign an Admin
admin = msg.sender;
// 2. Token Contract
tokenContract = _tokenContract;
//3. Token Price
tokenPrice = _tokenPrice;
}
}
2_deploy_contracts.js
const DappToken = artifacts.require("DappToken");
const DappTokenSale = artifacts.require("DappTokenSale");
module.exports = function(deployer) {
deployer.deploy(DappToken, 1000000).then(function() {
// Token price is 0.001 Ether
var tokenPrice = 1000000000000000;
return deployer.deploy(DappTokenSale, DappToken.address, tokenPrice);
});
};
const DappTokenSale = artifacts.require("DappTokenSale");
module.exports = function(deployer) {
deployer.deploy(DappToken, 1000000).then(function() {
// Token price is 0.001 Ether
var tokenPrice = 1000000000000000;
return deployer.deploy(DappTokenSale, DappToken.address, tokenPrice);
});
};
DappTokenSale.js (Tests)
const DappTokenSale = artifacts.require("DappTokenSale");
contract('DappTokenSale', function(accounts) {
var tokenSaleInstance;
var tokenPrice = 1000000000000000 //wei (0.001 ether)
it('initializes the contract with the correct values', function() {
return DappTokenSale.deployed().then(function(instance) {
tokenSaleInstance = instance;
return tokenSaleInstance.address
}).then(function(address) {
assert.notEqual(address, 0x0, 'has contract address')
return tokenSaleInstance.tokenContract();
}).then(function(address) {
assert.notEqual(address, 0x0, 'has contract address');
return tokenSaleInstance.tokenPrice();
}).then(function(price) {
assert.equal(price, tokenPrice, 'token price is correct')
});
});
})
contract('DappTokenSale', function(accounts) {
var tokenSaleInstance;
var tokenPrice = 1000000000000000 //wei (0.001 ether)
it('initializes the contract with the correct values', function() {
return DappTokenSale.deployed().then(function(instance) {
tokenSaleInstance = instance;
return tokenSaleInstance.address
}).then(function(address) {
assert.notEqual(address, 0x0, 'has contract address')
return tokenSaleInstance.tokenContract();
}).then(function(address) {
assert.notEqual(address, 0x0, 'has contract address');
return tokenSaleInstance.tokenPrice();
}).then(function(price) {
assert.equal(price, tokenPrice, 'token price is correct')
});
});
})