在本系列關(guān)于使用以太坊構(gòu)建DApps教程的第3部分中,我們構(gòu)建并將我們的代幣部署到以太坊測試網(wǎng)絡(luò)Rinkeby。在這部分中,我們將開始編寫Story DAO代碼。
我們將使用第1部分中列出的條件來做指導。
合約大綱
讓我們用這個骨架創(chuàng)建一個新的合約StoryDao.sol:
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;
}
}
我們正在導入SafeMath以便再次進行安全計算,但這次我們還使用了Zeppelin的Ownable合約,該合約允許某人“擁有”故事并執(zhí)行某些僅限管理員的功能。簡單地說我們的StoryDao is Ownable就夠了;隨時檢查合約,看看它是如何工作的。
我們還使用此合約中的onlyOwner修飾符。函數(shù)修飾符基本上是函數(shù)擴展和插件。onlyOwner修飾符如下所示:
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
當onlyOwner被添加到一個函數(shù)中時,那個函數(shù)的體被粘貼到_;所在的部分,并且它比其他的一切內(nèi)容都先執(zhí)行。因此,通過使用此修飾符,該函數(shù)會自動檢查郵件發(fā)件人是否也是合約的所有者,然后照常繼續(xù)(如果是)。如果沒有,它會崩潰。
通過在改變我們的Story DAO的費用和其他參數(shù)的函數(shù)上使用onlyOwner修飾符,我們確保只有管理員才能進行這些更改。
測試
讓我們測試一下初始函數(shù)。
如果文件夾test不存在,請創(chuàng)建它。然后在其中創(chuàng)建文件TestStoryDao.sol和TestStoryDao.js。因為在Truffle中沒有本地方法來測試異常,所以也可以使用以下內(nèi)容創(chuàng)建helpers/expectThrow.js:
export default async promise => {
try {
await promise;
} catch (error) {
const invalidOpcode = error.message.search('invalid opcode') >= 0;
const outOfGas = error.message.search('out of gas') >= 0;
const revert = error.message.search('revert') >= 0;
assert(
invalidOpcode || outOfGas || revert,
'Expected throw, got \'' + error + '\' instead',
);
return;
}
assert.fail('Expected throw not received');
};
注意:Solidity測試通常用于測試基于合約的低級函數(shù),即智能合約的內(nèi)部。JS測試通常用于測試合約是否可以與外部進行正確的交互,這是我們最終用戶將要做的事情。
在TestStoryDao.sol,輸入以下內(nèi)容:
pragma solidity ^0.4.24;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/StoryDao.sol";
contract TestStoryDao {
function testDeploymentIsFine() public {
StoryDao sd = StoryDao(DeployedAddresses.StoryDao());
uint256 daofee = 100; // hundredths of a percent, i.e. 100 is 1%
uint256 whitelistfee = 10000000000000000; // in Wei, this is 0.01 ether
uint256 durationDays = 21; // duration of story's chapter in days
uint256 durationSubmissions = 1000; // duration of story's chapter in entries
Assert.equal(sd.daofee(), daofee, "Initial DAO fee should be 100");
Assert.equal(sd.whitelistfee(), whitelistfee, "Initial whitelisting fee should be 0.01 ether");
Assert.equal(sd.durationDays(), durationDays, "Initial day duration should be set to 3 weeks");
Assert.equal(sd.durationSubmissions(), durationSubmissions, "Initial submission duration should be set to 1000 entries");
}
}
這將檢查StoryDao合約是否正確部署,并提供正確的費用和持續(xù)時間。第一行確保通過從已部署地址列表中讀取它來部署它,并且最后一節(jié)做了一些斷言——檢查聲明是真還是假。在我們的例子中,我們將數(shù)字與已部署合約的初始值進行比較。每當它為“true”時,Assert.equals部分將發(fā)出一個“True”的事件,這是Truffle在測試時正在監(jiān)聽的事件。
在TestStoryDao.js,輸入以下內(nèi)容:
import expectThrow from './helpers/expectThrow';
const StoryDao = artifacts.require("StoryDao");
contract('StoryDao Test', async (accounts) => {
it("should make sure environment is OK by checking that the first 3 accounts have over 20 eth", async () =>{
assert.equal(web3.eth.getBalance(accounts[0]).toNumber() > 2e+19, true, "Account 0 has more than 20 eth");
assert.equal(web3.eth.getBalance(accounts[1]).toNumber() > 2e+19, true, "Account 1 has more than 20 eth");
assert.equal(web3.eth.getBalance(accounts[2]).toNumber() > 2e+19, true, "Account 2 has more than 20 eth");
});
it("should make the deployer the owner", async () => {
let instance = await StoryDao.deployed();
assert.equal(await instance.owner(), accounts[0]);
});
it("should let owner change fee and duration", async () => {
let instance = await StoryDao.deployed();
let newDaoFee = 50;
let newWhitelistFee = 1e+10; // 1 ether
let newDayDuration = 42;
let newSubsDuration = 1500;
instance.changedaofee(newDaoFee, {from: accounts[0]});
instance.changewhitelistfee(newWhitelistFee, {from: accounts[0]});
instance.changedurationdays(newDayDuration, {from: accounts[0]});
instance.changedurationsubmissions(newSubsDuration, {from: accounts[0]});
assert.equal(await instance.daofee(), newDaoFee);
assert.equal(await instance.whitelistfee(), newWhitelistFee);
assert.equal(await instance.durationDays(), newDayDuration);
assert.equal(await instance.durationSubmissions(), newSubsDuration);
});
it("should forbid non-owners from changing fee and duration", async () => {
let instance = await StoryDao.deployed();
let newDaoFee = 50;
let newWhitelistFee = 1e+10; // 1 ether
let newDayDuration = 42;
let newSubsDuration = 1500;
await expectThrow(instance.changedaofee(newDaoFee, {from: accounts[1]}));
await expectThrow(instance.changewhitelistfee(newWhitelistFee, {from: accounts[1]}));
await expectThrow(instance.changedurationdays(newDayDuration, {from: accounts[1]}));
await expectThrow(instance.changedurationsubmissions(newSubsDuration, {from: accounts[1]}));
});
it("should make sure the owner can only change fees and duration to valid values", async () =>{
let instance = await StoryDao.deployed();
let invalidDaoFee = 20000;
let invalidDayDuration = 0;
let invalidSubsDuration = 98;
await expectThrow(instance.changedaofee(invalidDaoFee, {from: accounts[0]}));
await expectThrow(instance.changedurationdays(invalidDayDuration, {from: accounts[0]}));
await expectThrow(instance.changedurationsubmissions(invalidSubsDuration, {from: accounts[0]}));
})
});
為了使我們的測試成功運行,我們還需要告訴Truffle我們想要部署StoryDao——因為它不會為我們做。因此,讓我們在migrations創(chuàng)建3_deploy_storydao.js,其內(nèi)容幾乎與我們之前編寫的遷移相同:
var Migrations = artifacts.require("./Migrations.sol");
var StoryDao = artifacts.require("./StoryDao.sol");
module.exports = function(deployer, network, accounts) {
if (network == "development") {
deployer.deploy(StoryDao, {from: accounts[0]});
} else {
deployer.deploy(StoryDao);
}
};
此時,我們還應(yīng)該在項目文件夾的根目錄中更新(或創(chuàng)建,如果它不存在)package.json文件,其中包含我們目前所需的依賴項,并且可能在不久的將來需要:
{
"name": "storydao",
"devDependencies": {
"babel-preset-es2015": "^6.18.0",
"babel-preset-stage-2": "^6.24.1",
"babel-preset-stage-3": "^6.17.0",
"babel-polyfill": "^6.26.0",
"babel-register": "^6.23.0",
"dotenv": "^6.0.0",
"truffle": "^4.1.12",
"openzeppelin-solidity": "^1.10.0",
"openzeppelin-solidity-metadata": "^1.2.0",
"openzeppelin-zos": "",
"truffle-wallet-provider": "^0.0.5",
"ethereumjs-wallet": "^0.6.0",
"web3": "^1.0.0-beta.34",
"truffle-assertions": "^0.3.1"
}
}
和.babelrc文件的內(nèi)容:
{
"presets": ["es2015", "stage-2", "stage-3"]
}
我們還需要在我們的Truffle配置中要求Babel,因此它知道它應(yīng)該在編譯時使用它。
注意:Babel是NodeJS的一個附加組件,它允許我們在當前一代NodeJS中使用下一代JavaScript,因此我們可以編寫諸如import。如果這超出了你的理解范圍,只需忽略它,然后只需逐字粘貼即可。在以這種方式安裝后,你可能永遠不必再處理這個問題。
require('dotenv').config();
================== ADD THESE TWO LINES ================
require('babel-register');
require('babel-polyfill');
=======================================================
const WalletProvider = require("truffle-wallet-provider");
const Wallet = require('ethereumjs-wallet');
// ...
現(xiàn)在,終于進行truffle test。輸出應(yīng)該類似于這個:

有關(guān)測試的更多信息,請參閱本教程,該教程專門用于測試智能合約。
在本課程的后續(xù)部分中,我們將跳過測試,因為輸入它們會使教程太長,但請參考項目的最終源代碼來檢查它們。我們剛剛完成的過程已經(jīng)設(shè)置了測試環(huán)境,因此你可以在進一步設(shè)置的情況下編寫測試。
白名單
現(xiàn)在讓我們構(gòu)建一個白名單機制,讓用戶參與構(gòu)建Story。將以下函數(shù)框架添加到StoryDao.sol:
function whitelistAddress(address _add) public payable {
// whitelist sender if enough money was sent
}
function() external payable {
// if not whitelisted, whitelist if paid enough
// if whitelisted, but X tokens at X price for amount
}
未命名的函數(shù)function()被稱為回調(diào)函數(shù),這是在沒有特定指令的情況下將錢發(fā)送到此合約時被調(diào)用的函數(shù)(即,沒有專門調(diào)用其他函數(shù))。這可以讓人們加入StoryDao,只需將以太發(fā)送到DAO并立即將其列入白名單,或者購買代幣,具體取決于它們是否已經(jīng)列入白名單。
whitelistSender功能用于白名單,可以直接調(diào)用,但是如果發(fā)送方尚未列入白名單,我們將確保當收到一些以太時,后備功能會自動調(diào)用它。whitelistAddress函數(shù)被聲明為public因為它也應(yīng)該可以從其他合約中調(diào)用,并且回調(diào)函數(shù)是external函數(shù),因為money將僅從外部地址轉(zhuǎn)到此地址。調(diào)用此合約的合約可以直接輕松調(diào)用所需的功能。
讓我們首先處理回調(diào)函數(shù)。
function() external payable {
if (!whitelist[msg.sender]) {
whitelistAddress(msg.sender);
} else {
// buyTokens(msg.sender, msg.value);
}
}
我們檢查發(fā)件人是否已經(jīng)在白名單中,并將調(diào)用委托給whitelistAddress函數(shù)。請注意,我們已經(jīng)注釋掉了buyTokens函數(shù),因為我們還沒有它。
接下來,讓我們處理白名單。
function whitelistAddress(address _add) public payable {
require(!whitelist[_add], "Candidate must not be whitelisted.");
require(!blacklist[_add], "Candidate must not be blacklisted.");
require(msg.value >= whitelistfee, "Sender must send enough ether to cover the whitelisting fee.");
whitelist[_add] = true;
whitelistedNumber++;
emit Whitelisted(_add, true);
if (msg.value > whitelistfee) {
// buyTokens(_add, msg.value.sub(whitelistfee));
}
}
請注意,此函數(shù)接受地址作為參數(shù),并且不從消息中提取它(來自交易)。如果有人無法承擔加入DAO的費用,這還有一個額外的好處,即人們可以將其他人列入白名單。
我們通過一些健壯性檢查啟動該功能:發(fā)件人不得列入白名單或列入黑名單(禁止),并且必須已發(fā)送足夠的費用以支付費用。如果這些條件令人滿意,則將地址添加到白名單中,發(fā)出白名單事件,最后,如果發(fā)送的以太數(shù)量大于覆蓋白名單費用所需的以太數(shù)量,則剩余部分用于買這些代幣。
注意:我們使用sub而不是-來減,因為這是一個安全計算的SafeMath函數(shù)。
用戶現(xiàn)在可以將自己或其他人列入白名單,只要他們向StoryDao合約發(fā)送0.01以太或更多。
結(jié)論
我們在本教程中構(gòu)建了DAO的初始部分,但還有很多工作要做。請繼續(xù)關(guān)注:在下一部分中,我們將處理為Story添加內(nèi)容的問題!
======================================================================
分享一些以太坊、EOS、比特幣等區(qū)塊鏈相關(guān)的交互式在線編程實戰(zhàn)教程:
- java以太坊開發(fā)教程,主要是針對java和android程序員進行區(qū)塊鏈以太坊開發(fā)的web3j詳解。
- python以太坊,主要是針對python工程師使用web3.py進行區(qū)塊鏈以太坊開發(fā)的詳解。
- php以太坊,主要是介紹使用php進行智能合約開發(fā)交互,進行賬號創(chuàng)建、交易、轉(zhuǎn)賬、代幣開發(fā)以及過濾器和交易等內(nèi)容。
- 以太坊入門教程,主要介紹智能合約與dapp應(yīng)用開發(fā),適合入門。
- 以太坊開發(fā)進階教程,主要是介紹使用node.js、mongodb、區(qū)塊鏈、ipfs實現(xiàn)去中心化電商DApp實戰(zhàn),適合進階。
- C#以太坊,主要講解如何使用C#開發(fā)基于.Net的以太坊應(yīng)用,包括賬戶管理、狀態(tài)與交易、智能合約開發(fā)與交互、過濾器和交易等。
- EOS教程,本課程幫助你快速入門EOS區(qū)塊鏈去中心化應(yīng)用的開發(fā),內(nèi)容涵蓋EOS工具鏈、賬戶與錢包、發(fā)行代幣、智能合約開發(fā)與部署、使用代碼與智能合約交互等核心知識點,最后綜合運用各知識點完成一個便簽DApp的開發(fā)。
- java比特幣開發(fā)教程,本課程面向初學者,內(nèi)容即涵蓋比特幣的核心概念,例如區(qū)塊鏈存儲、去中心化共識機制、密鑰與腳本、交易與UTXO等,同時也詳細講解如何在Java代碼中集成比特幣支持功能,例如創(chuàng)建地址、管理錢包、構(gòu)造裸交易等,是Java工程師不可多得的比特幣開發(fā)學習課程。
- php比特幣開發(fā)教程,本課程面向初學者,內(nèi)容即涵蓋比特幣的核心概念,例如區(qū)塊鏈存儲、去中心化共識機制、密鑰與腳本、交易與UTXO等,同時也詳細講解如何在Php代碼中集成比特幣支持功能,例如創(chuàng)建地址、管理錢包、構(gòu)造裸交易等,是Php工程師不可多得的比特幣開發(fā)學習課程。
- tendermint區(qū)塊鏈開發(fā)詳解,本課程適合希望使用tendermint進行區(qū)塊鏈開發(fā)的工程師,課程內(nèi)容即包括tendermint應(yīng)用開發(fā)模型中的核心概念,例如ABCI接口、默克爾樹、多版本狀態(tài)庫等,也包括代幣發(fā)行等豐富的實操代碼,是go語言工程師快速入門區(qū)塊鏈開發(fā)的最佳選擇。
匯智網(wǎng)原創(chuàng)翻譯,轉(zhuǎn)載請標明出處。這里是原文以太坊構(gòu)建DApps系列教程(四):Story DAO的白名單和測試