本文主要参考:here 和 here,因为参考的这篇文章版本有些旧了,所以是根据比较新的版本写的。
1、 首先初始化环境
truffle init
2、开启testrpc
testrpc //另开窗口
3、部署合约
a. 编写合约代码,保存到contracts/YourContractName.sol文件
例如:Conference.sol
pragma solidity ^0.4.4;
contract Conference { // can be killed, so the owner gets sent the money in the end
address public organizer;
mapping (address => uint) public registrantsPaid;
uint public numRegistrants;
uint public quota;
event Deposit(address _from, uint _amount); // so you can log the event
event Refund(address _to, uint _amount); // so you can log the event
function Conference() {
organizer = msg.sender;
quota = 500;
numRegistrants = 0;
}
function buyTicket() payable public {
if (numRegistrants >= quota) {
throw; // throw ensures funds will be returned
}
registrantsPaid[msg.sender] = msg.value;
numRegistrants++;
Deposit(msg.sender, msg.value);
}
function changeQuota(uint newquota) public {
if (msg.sender != organizer) { return; }
quota = newquota;
}
function refundTicket(address recipient, uint amount) public {
if (msg.sender != organizer) { return; }
if (registrantsPaid[recipient] == amount) {
address myAddress = this;
if (myAddress.balance >= amount) {
recipient.transfer(amount);
Refund(recipient, amount);
registrantsPaid[recipient] = 0;
numRegistrants--;
}
}
return;
}
function destroy() {
if (msg.sender == organizer) { // without this funds could be locked in the contract forever!
suicide(organizer);
}
}
}
b. 删除原有的两个合约文件
MetaCoin.sol 和 ConvertLib.sol
注意: Migrations.sol 不要删除
c. 修改migrations/2_deploy_contracts文件
var Conference = artifacts.require("./Conference.sol");
module.exports = function(deployer) {
deployer.deploy(Conference);
};
d. 编译
truffle compile
e. 部署
truffle migrate 或 truffle migrate --reset
1、删除test/metacoin.js
2、增加conference.js文件
a. 测试初始
var Conference = artifacts.require("./Conference.sol");
contract('Conference', function(accounts) {
var Quato; //限制人数为500
var NumRegistrants; //注册的人数刚开始为应该为0
var Organizer; //组织者地址应该正确
var organizer_address = accounts[0];
it("Initial conference settings should match", function() {
return Conference.deployed().then(function(instance){
meta = instance;
return meta.quota.call();
}).then(function(quota){
Quato = quota;
return meta.organizer.call();
}).then(function(organizer){
Organizer = organizer;
return meta.numRegistrants.call();
}).then(function(numRegistrants){
NumRegistrants = numRegistrants;
assert.equal(Quato, 500, "Quota doesn't match!");
assert.equal(numRegistrants, 0, "Registrants should be zero!");
assert.equal(Organizer, organizer_address, "Owner doesn't match!");
});
});
});
显示结果:
prodeMacBook-Pro:pc6 pro$ truffle test
Using network 'development'.
Compiling ./contracts/Conference.sol...
Contract: Conference
✓ Initial conference settings should match (83ms)
1 passing (107ms)
b.测试交易
//测试交易
it("Should let you buy a ticket", function() {
var user_address = accounts[1];
var ticketPrice = web3.toWei(.05, 'ether');
var initialBalance; //用户初始余额
var newBalance; //用户购买之后余额
var newNumRegistrants; //买票人数
var userPaid; //付款的金额
var difference;
return Conference.deployed().then(function(instance){
meta = instance;
return meta.getBalance.call(user_address);
}).then(function(balance){
initialBalance = balance.toNumber(); //初始金额
return meta.buyTicket({from: user_address, value: ticketPrice}); //买票操作
}).then(function(){
return meta.getBalance.call(user_address);
}).then(function(balance){
newBalance = balance.toNumber(); //买票之后余额
difference = initialBalance - newBalance;
return meta.numRegistrants.call();
}).then(function(numRegistrants){
newNumRegistrants = numRegistrants;
return meta.registrantsPaid.call(user_address);
}).then(function(registrantsPaid){
userPaid = registrantsPaid.toNumber();
assert.equal(userPaid, ticketPrice, "Sender's paid but is not listed");
assert.equal(difference, ticketPrice, "Difference should be what was sent");
assert.equal(newNumRegistrants, 1, "there should be 1 registrant");
});
});
这里碰到了问题,实际上我得到的结果是:
prodeMacBook-Pro:pc6 pro$ truffle test
Using network 'development'.
Compiling ./contracts/Conference.sol...
Contract: Conference
✓ Initial conference settings should match (79ms)
1) Should let you buy a ticket
Events emitted during test:
---------------------------
Deposit(_from: 0xd19b5f640c058856bb2f2e2f75454afa2173d2f8, _amount: 50000000000000000)
---------------------------
1 passing (240ms)
1 failing
1) Contract: Conference Should let you buy a ticket:
AssertionError: Difference should be what was sent: expected 56320100000002050 to equal '50000000000000000'
at test/conference.js:62:20
at process._tickCallback (internal/process/next_tick.js:109:7)
//测试没通过。。我也觉得很奇怪。待解决。。。