[Solidity] How to get your own contract balance?

This section explains how to get your own contract balance in Solidity.

Syntax

To get your own contract balance in Solidity, do the following.

address(this).balance

Sample Code

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract Test {

    function getMyBalance() public view returns(uint) {
        // get own contract balance
        return address(this).balance;
    }

}

“this” means your own contract address. It is passed to the address function as an argument, and the balance method is used to get the contract balance.

タイトルとURLをコピーしました