Building Ethereum DApps: Whitelisting & Testing a Story DAO
Original Source: https://www.sitepoint.com/building-ethereum-dapps-whitelisting-testing-story-dao/
In part 3 of this tutorial series on building DApps with Ethereum, we built and deployed our token to the Ethereum testnet Rinkeby. In this part, we’ll start writing the Story DAO code.
We’ll use the conditions laid out in the intro post to guide us.
Contract Outline
Let’s create a new contract, StoryDao.sol, with this skeleton:
pragma solidity ^0.4.24;
import “../node_modules/openzeppelin-solidity/contracts/math/SafeMath.sol”;
import “../node_modules/openzeppelin-solidity/contracts/ownership/Ownable.sol”;
contract StoryDao is Ownable {
using SafeMath for uint256;
mapping(address => bool) whitelist;
uint256 public whitelistedNumber = 0;
mapping(address => bool) blacklist;
event Whitelisted(address addr, bool status);
event Blacklisted(address addr, bool status);
uint256 public daofee = 100; // hundredths of a percent, i.e. 100 is 1%
uint256 public whitelistfee = 10000000000000000; // in Wei, this is 0.01 ether
event SubmissionCommissionChanged(uint256 newFee);
event WhitelistFeeChanged(uint256 newFee);
uint256 public durationDays = 21; // duration of story’s chapter in days
uint256 public durationSubmissions = 1000; // duration of story’s chapter in entries
function changedaofee(uint256 _fee) onlyOwner external {
require(_fee < daofee, “New fee must be lower than old fee.”);
daofee = _fee;
emit SubmissionCommissionChanged(_fee);
}
function changewhitelistfee(uint256 _fee) onlyOwner external {
require(_fee < whitelistfee, “New fee must be lower than old fee.”);
whitelistfee = _fee;
emit WhitelistFeeChanged(_fee);
}
function lowerSubmissionFee(uint256 _fee) onlyOwner external {
require(_fee < submissionZeroFee, “New fee must be lower than old fee.”);
submissionZeroFee = _fee;
emit SubmissionFeeChanged(_fee);
}
function changeDurationDays(uint256 _days) onlyOwner external {
require(_days >= 1);
durationDays = _days;
}
function changeDurationSubmissions(uint256 _subs) onlyOwner external {
require(_subs > 99);
durationSubmissions = _subs;
}
}
We’re importing SafeMath to have safe calculations again, but this time we’re also using Zeppelin’s Ownable contract, which lets someone “own” the story and execute certain admin-only functions. Simply saying that our StoryDao is Ownable is enough; feel free to inspect the contract to see how it works.
We also use the onlyOwner modifier from this contract. Function modifiers are basically extensions, plugins for functions. The onlyOwner modifier looks like this:
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
When onlyOwner is added to a function, then that function’s body is pasted into the part where the _; part is, and everything before it executes first. So by using this modifier, the function automatically checks if the message sender is also the owner of the contract and then continues as usual if so. If not, it crashes.
By using the onlyOwner modifier on the functions that change the fees and other parameters of our story DAO, we make sure that only the admin can do these changes.
The post Building Ethereum DApps: Whitelisting & Testing a Story DAO appeared first on SitePoint.
Leave a Reply
Want to join the discussion?Feel free to contribute!