ReasonJun

Solidity : function / Parameter / wei / ether / gas 본문

Blockchain/Solidity

Solidity : function / Parameter / wei / ether / gas

ReasonJun 2023. 10. 16. 12:33
728x90
// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract Ex1 {

    uint public a = 3;
    uint public  b = 5;

    function myBlock() public returns (uint, uint){
        a = 99;
        b = 0;
        return(a, b);
    }

    function myBlcok2(uint c) public {
        a = c;
    }


    function myBlock3() public pure returns(uint age, uint weight) {
        age = 20;
        weight = 55;
    }

    function getValue(uint e) public pure returns (uint) {
        return e;
    }

    function getReference(string memory f) public pure returns (string memory){
        return f;
    }

    uint public g = 3; // 100
    uint public h = myBlock4();

    function myBlock4() public returns(uint) {
        g = 100;
        return g;
    }

}
// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract Ex2 {
     
     function myBlock(string memory str) public pure returns (uint, string memory, bytes memory) {
        uint num = 99;
        bytes memory byt = hex"01";
        return(num, str, byt); // 99 , wef, 0x01
     }

     function myBlock2(string memory str) external pure returns (string memory) { 
        return str; // external => calldata or memory (Reference type only)
     }
}

wei / ether

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract Ether_1 {
    uint public oneWei = 1 wei;

    uint public oneEther = 1 ether;
    // 1 ehter = 1e18 wei
}

Gas

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

// Pay transaction costs with gas.
// Transaction requests with higher gas prices are processed first
// Refund unused gas

contract Gas_1 {
    uint public i = 0;

     // If all the sending gas is used, the transaction will fail
     // state change canceled
     // Used gas is not refunded

    function forever() public {
       // Run loop until all gas is used up
         // and the transaction fails
        while (true) {
            i += 1;
        }
    }
}

// Gas limit: Maximum gas usage set by the user
// block gas limit: Maximum amount of gas allowed for a block - set by the network
728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : keccak256  (0) 2023.10.16
Solidity : event / emit  (0) 2023.10.16
Solidity : Modifier (pure, view)  (0) 2023.10.16
Solidity : Visibility  (0) 2023.10.15
Solidity : Operator / Shift operator  (1) 2023.10.15
Comments