inflearn logo
강의

Khóa học

Chia sẻ kiến thức

Ethereum đang hoạt động! Phát triển Dapp xổ số cho người mới bắt đầu

Kiểm tra chức năng đặt cược xổ số

Lottery Bet 함수 테스트 강의 중 4:36에서 'from' 관련 오류가 발생합니다.

602

harang

1 câu hỏi đã được viết

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가 나옵니다. ㅠㅠ

dapp ethereum

Câu trả lời 3

0

heom

아 저도 똑같은 에러가 났는데 덕분에 해결했습니다. 감사합니다. 허허. 따봉 누르고 갑니다.

0

akay2018

저는 pushBet함수에서 b.bettor = msg.sender; 이 부분 에러가 나던데요,,,ㅜ

0

harang

아 ㅠㅠㅠ 오타가 있었습니다.

contract('Lottery', function (deployer, user1, user2) {

=> []가 빠져있었네요 ㅠㅠㅠㅠㅠ!!!

contract('Lottery', function ([deployer, user1, user2]) {

 

truffle project 세팅관련

0

302

1

gas 계산하는 부분이 이해가 되지 않습니다.

0

330

1

폴더 안에 파일이 없습니다.

0

347

0

address payable error

0

365

1

migrations 내의 js파일에서 artifacts 객체를 불러올 수 없습니다

1

320

0

안녕하세요 강의 잘 따라와서 끝까지 잘 해냈습니다!!

0

225

0

안녕하세요 강의 정말 잘듣고있습니다.

1

491

1

안녕하세요. Visibility for constructor is ignored. If you want the contract to be non-deployable, making it "abstract" is sufficient.

1

255

0

setState 사용에 대해서 질문입니다.

0

308

1

bootstrap library 설치 error

0

301

0

web3.eth.sendTransaction()

0

367

0

잘 쫓아가고 있습니다만 왜 저는 빨간선이 나올까요?

0

266

0

React js 환경설정 강의 질문

0

294

1

web3오류

0

384

2

코드 원본을 받을 수 있을까요?

0

362

2

ganache-cli 명령어 질문

0

356

1

recent mode 질문

0

272

1

Gas 부족 에 대한 질문입니다.

0

308

2

geth 채굴시 unlock 이외의 방법이 있을까요?

0

299

2

테스트 코드 작성 중 오류가 발생합니다.

0

321

2

truffle migrate

0

285

1

질문입니다

0

313

2

8:22 에러

0

294

2

콘솔창에서 에러..

0

1062

5