작성
·
442
1
감사하게도 강좌를 통해 스마트 컨트랙트에 대해서 배우고 있는데요,
Bet 테스트 코드 실행 시, 에러가 발생하고 있는데 어디서 문제인지 파악이 안되서 질문글 올려요
Error: The send transactions "from" field must be defined!
Lottery.sol
pragma solidity >=0.4.22 <0.9.0;
contract Lottery {
struct BetInfo {
uint256 answerBlockNumber;
address payable bettor;
byte challenges; //0xab...
}
address public owner;
uint256 private _tail;
uint256 private _head;
mapping (uint256 => BetInfo) private _bets;
uint256 private _pot;
uint256 constant internal BET_AMOUNT = 5 * 10 ** 15;
uint256 constant internal BET_BLOCK_INTERVAL = 3;
uint256 constant internal BLOCK_LIMIT = 256;
event BET(uint256 index, address bettor, uint256 amount, byte challenges, uint256 answerBlockNumber);
constructor() public {
owner = msg.sender;
}
function getPot() public view returns (uint256 pot) {
return _pot;
}
/**
* @dev 베팅을 한다. 유저는 0.005 ETH를 보내야 하고, 베팅을 1 byte 글자를 보낸다.
* 큐에 저장된 베팅 정보는 이후 distribute 함수에서 해결된다.
* @param challenges 유저가 베팅하는 글자
* @return 함수가 잘 수행되었는지 확인히는 bool 값
*/
function bet(byte challenges) public payable returns (bool result) {
// check the paater ether is sent
require(msg.value == BET_AMOUNT, "Not enoughf ETH");
// push bet to the queue
require(pushBet(challenges), "Fail to add a new Bet Info");
// emit evnet
emit BET(_tail - 1, msg.sender, msg.value, challenges, block.number + BET_BLOCK_INTERVAL);
return true;
}
// save ther bet to the queue
// 결과값 검증
// check the answer
function getBetInfo(uint256 index) public view returns (uint256 answerBlockNumber, address bettor, byte challenges) {
BetInfo memory b = _bets[index];
answerBlockNumber = b.answerBlockNumber;
bettor = b.bettor;
challenges = b.challenges;
}
// function pushBet(byte challenges) public returns (bool) {
function pushBet(byte challenges) internal returns (bool) {
BetInfo memory b;
b.bettor = msg.sender;
b.answerBlockNumber = block.number + BET_BLOCK_INTERVAL;
b.challenges = challenges;
_bets[_tail] = b;
_tail++;
return true;
}
// function popBet(uint256 index) public returns (bool) {
function popBet(uint256 index) internal returns (bool) {
delete _bets[index];
return true;
}
}
lottery.test.js
const assertRevert = require("./assertRevert");
const Lottery = artifacts.require("Lottery");
contract('Lottery', function (deployer, user1, user2) {
let lottery;
beforeEach(async () => {
lottery = await Lottery.new();
})
//it.only('getPot should return current pot', async () => {
it('getPot should return current pot', async () => {
let pot = await lottery.getPot();
assert.equal(pot, 0);
})
describe('Bet', function () {
console.log(user1);
it.only('should fail when the bet money is not 0.005 ETH', async () => {
// Fail transaction
await assertRevert(lottery.bet('0xab', { from: user1, value: 4000000000000000 }))
// transaction object {chainId, value, to, from, gas, gasPrice}
})
it('should put the bet to the bet queue with 1 bet', async () => {
// bet
await lottery.bet('0xab', { from: user1, value: 5000000000000000 })
// check contract balance == 0.005
// check bet info
// check log
})
})
});
중간에 제가 노트북을 리부트해서 서버의 정보가 날라가서 일까요??
ganache-cli -d -m tutorial
로 다시 구동했는데, user1를 출력해보면 undefined가 나옵니다. ㅠㅠ