PBL Ⅲ/Ethereum and Solidity

Section3 : Advanced SmartContracts

myejinni 2022. 8. 21. 15:59

65. The Lottery Contract - 66. Lottery Design

enter = send a transaction

pickWinner = picks a winner randomly

 

68. Basic Solidity Types

 

UINT & INT

*cannot store any decimals ; only whole numbers

 

address

주소 저장하는 타입

 

 

69. Starting the Lottery Contract - 70.The Message GLobal Variable

 

Global Variable = message object (MSG)

-> available when creating a new contract

-> available with any function invocation

 

msg.sender 주목

 

 71. Overview of Arrays

fixed array = no change in length

*our project(Lottery contract) = dynamic array

 

*whenever we have an array in solidity, the function that gets generated does not return the entire Array

 

73. Big Solidity Gotcha

 

74. Entering the Lottery

 

75. Validation with Require Statements

 

1. click Deploy btn

 

*require() = make sure that some requirement has been satisfied before allowing the rest of the function to be executed

did not make this min requirement

=> get kicked right of the function

 

 

76. The Remix Debugger - 77.Pseudo Random Number Generator

 

78. Selecting a Winner 

random() = return random num that is probably going to be very large

 

 function pickWinner() public{
        uint index = random() % players.length; //how we can use this index to send the winner some money
    }

 

79. Sending Ether from Contracts

transfer() = attempt to take some amount of money from the current contract and send it to given address(players[index])

function pickWinner() public{
        uint index = random() % players.length; //how we can use this index to send the winner some money
        players[index].transfer(this.balance);
    }

 

80. Resetting Contract State

 

emptying out the list of players

= we can reset the state of our contract and automatically get it ready for another round

= redeploy 없이 가능

 

80. Requiring managers

 

pickwinner() 안에 require() 선언=> only the manager is able to call the pick winner function

function pickWinner() public{
        require(msg.sender == manager);
        ...}

 

82. Fuction Modifiers 

modifier() = used as reducing the amount of code that I have to write

 

83. Returning Players Array

 
function getPlayers() public view returns (address[]){
        return players;
    }

 

84. Contract Review

 

85.- 86. New Test setup

 

87. Test project updates

 

compile.js
deploy.js

 

88. Test helper Review - 89. Asserting Deployment

 

Lottery.test.js

1. $npm run test

 

 

90. Entering the Lottery - 94. End to End Test

Lottery.test.js (2)