//1.这里是用gannache测试环境
//定义版本
pragma solidity ^0.4.17;
//编写Lottery智能合约
contract Lottery{
//定义一个部署智能合约的管理者
address public manager;
//定义一个彩民数组
address[] public players;
//定义一个构造函数,获取到manager的address
function Lottery() public{
manager = msg.sender;
}
//定义一个获取manager的方法,类型是address
function getManager() public view returns (address){
return manager;
}
//投注彩票(设定一个账户去deploy,另一个账户去enter)
function enter() public payable {
// require(msg.value == 1 ether);
players.push(msg.sender);
}
//所有的投注彩票的人
function getAllPlayers() public view returns (address[]){
return players;
}
//余额
function getBalance() public view returns(uint){
return this.balance;
}
//彩民数量
function getPlayersCount() public view returns(uint){
return players.length;
}
function random() private view returns (uint){
return uint(keccak256(block.difficulty, now, players));
}
function pickWinner() public onlyManagerCanCall returns (address){
uint index = random() % players.length;
address winner = players[index];
players = new address[](0) ;
winner.transfer(this.balance);
return winner;
}
function refund() public onlyManagerCanCall{
for(uint i = 0;ionlyManagerCanCall(){
require(msg.sender == manager);
_;
}
}
复制代码
//2.这里是一个测试用例,用的是react前端框架
//这是一个测试用例
//构造函数的Web3
const Web3 = require("web3");
//使用构造函数new出来一个Web3的对象,对象放进一个电话卡provider
const web3 = new Web3(ganache.provider());
//拿到测试环境
const ganache = require("ganache-cli");
//测试是否成功,可以看地址是否存在,这里用断言
const assert = require("assert");
const {interface, byteCode} = require("../compile");
let contract;
let accounts;
//部署智能合约的逻辑放在beforeEach这里
beforeEache(async () => {
accounts = await web3.eth.getAccounts();
contract = await web3.eth.Contract(json.parse(interface))
.deploy({
data: byteCode,
}).send({
from: accounts[0],
gas: 3000000
});
});
describe("测试区块链彩票智能合约", () => {
it("测试智能合约的编译和部署", async () => {
//验证地址是否存在
assert.ok(contract.options.address);
});
it("测试彩票智能合约的投注方法,正确案例", async () => {
//看下彩民是否买了彩票,这里的余额是否增加
const beginMoney = await contract.methods.getBalance().call(); //获取余额
//拿到合约的enter方法,调用send方法
await contract.methods.enter().send({ //投注彩票
from: accounts[1],
gas: '1000000', //最大的gas使用量
value: 1000000000000000000 //投注一个以太币
});
const endMoney = await contarct.method.getBalance().call();
console.log(endMoney - beginMoney);
//测试前后的余额差值
assert.equal("1000000000000000000", (endMoney - beginMoney));
});
it("测试彩票智能合约的投注方法,失败案例", async () => {
console.log(begin);
try {
await contract.methods.enter().send({
from: accounts[1],
gas: "1000000",
value: 20000000000000000
});
assert(false);
} catch (error) {
assert.ok(error);
}
});
});
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const {interface,bytecode} = require('../compile');
const assert = require('assert');
let contract;
let accounts;
// user story 用户案例 白盒测试
beforeEach(async ()=>{
accounts = await web3.eth.getAccounts();
contract =await new web3.eth.Contract(JSON.parse(interface))
.deploy({
data:bytecode
}).send({
from:accounts[0],
gas:'3000000'
});
});
//测试智能合约的编译和部署
describe('测试区块链彩票智能合约',()=>{
it('测试智能合约的编译和部署',async ()=>{
assert.ok(contract.options.address);
});
//测试智能合约的投注方法正确的案例
it('测试彩票智能合约的投注方法,正确案例',async ()=>{
const benginMoney = await contract.methods.getBalance().call();
await contract.methods.enter().send({
from:accounts[1],
gas:'1000000',
value: 1000000000000000000
});
const endMoney = await contract.methods.getBalance().call();
console.log(endMoney-benginMoney);
assert.equal('1000000000000000000',(endMoney-benginMoney));
});
// //测试智能合约的投注方法失败的案例
it('测试彩票智能合约的投注方法,失败案例',async ()=>{
console.log('bengin');
try {
await contract.methods.enter().send({
from: accounts[1],
gas: '1000000',
value: 20000000000000000
});
assert(false);
}catch (error){
assert.ok(error);
}
});
});
复制代码
//3.这里是编译智能合约的脚本
//编译的智能合约的脚本
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const srcpath = path.resolve(__dirname,'contracts','Lottery.sol');
const source = fs.readFileSync(srcpath,'utf-8');
const result = solc.compile(source,1);
module.exports = result.contracts[':Lottery'];
复制代码
//4.这里是将智能合约部署到主网上的操作,这里用rankeby网络进行模拟操作
//部署智能合约到真实的rankeby网络
const Web3 = require('web3');
const {interface,bytecode} = require('./compile');
const web3 = new Web3(ganache.provider());
deploy =async ()=>{
const accounts = await web3.eth.getAccounts();
console.log(accounts[0]);
const result =await new web3.eth.Contract(JSON.parse(interface))
.deploy({
data:bytecode
}).send({
from:accounts[0],
gas:'3000000'
});
console.log('address:'+result.options.address);
};
deploy();
复制代码