url: https://solidity-by-example.org/constants/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Constants {
// coding convention to uppercase constant variables
address public constant MY_ADDRESS = 0x777788889999AaAAbBbbCcccddDdeeeEfFFfCcCc;
uint public constant MY_UINT = 123;
}
상수를 사용하면 상대적으로 가스 비용을 절약할 수 있다고 한다.
url: https://solidity-by-example.org/immutable/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Immutable {
// coding convention to uppercase constant variables
address public immutable MY_ADDRESS;
uint public immutable MY_UINT;
constructor(uint _myUint) {
MY_ADDRESS = msg.sender;
MY_UINT = _myUint;
}
}
불변성은 Constants (상수)와 유사하지만, Constructor (생성자)를 통해 값을 초기 입력할 수 있다. 이후에는 값 수정 불가능
url: https://solidity-by-example.org/state-variables/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract SimpleStorage {
// State variable to store a number
uint public num;
// You need to send a transaction to write to a state variable.
function set(uint _num) public {
num = _num;
}
// You can read from a state variable without sending a transaction.
function get() public view returns (uint) {
return num;
}
}
블록체인에 저장되어 영속성을 갖는 uint 자료형 num 변수를 선언
매개변수로 uint 자료형 _num 변수를 입력받아, 상태변수 num에 더한다.
일반적으로 매개변수 앞에 _를 붙이는 이유는 매개변수와 다른 변수와 구분하기 위한 규칙이라고 한다. (Not Syntax Error)
url: https://solidity-by-example.org/ether-units/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract EtherUnits {
uint public oneWei = 1 wei;
// 1 wei is equal to 1
bool public isOneWei = 1 wei == 1;
uint public oneEther = 1 ether;
// 1 ether is equal to 10^18 wei
bool public isOneEther = 1 ether == 1e18;
}
bool public isOneWei = 1 wei == 1;
bool public isOneEther = 1 ether == 1e18;
1 wei는 1과 같고, 1 ether는 10^18과 같다.
url: https://solidity-by-example.org/gas/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Gas {
uint public i = 0;
// Using up all of the gas that you send causes your transaction to fail.
// State changes are undone.
// Gas spent are not refunded.
function forever() public {
// Here we run a loop until all of the gas are spent
// and the transaction fails
while (true) {
i += 1;
}
}
}
function forever() public {
while (true) {
i += 1;
}
}
url: https://solidity-by-example.org/if-else/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract IfElse {
function foo(uint x) public pure returns (uint) {
if (x < 10) {
return 0;
} else if (x < 20) {
return 1;
} else {
return 2;
}
}
function ternary(uint _x) public pure returns (uint) {
// if (_x < 10) {
// return 1;
// }
// return 2;
// shorthand way to write if / else statement
// the "?" operator is called the ternary operator
return _x < 10 ? 1 : 2;
}
}
url: https://solidity-by-example.org/loop/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Loop {
function loop() public {
// for loop
for (uint i = 0; i < 10; i++) {
if (i == 3) {
// Skip to next iteration with continue
continue;
}
if (i == 5) {
// Exit loop with break
break;
}
}
// while loop
uint j;
while (j < 10) {
j++;
}
}
}