Search code examples
nearprotocol

How can i know the balance of the account where the contract is deployed from a method?


I´m trying to write a function that returns the balance of the account where the contract is deployed.

Something with the following form:

pub fn get_balance(&self) -> Balance {
       
        env::account_balance()
    }

The above code returns the balance of the account that is calling the method, the signer. Is there a way to make it returns the balance of the contract account?


Solution

  • I got the answer. near_sdk:env::account_balance() already do what i was looking for. It retrieves the balance of the contract account.

    I was lacking a way to check it properly. To do it, i used near state [account_name] on the cli to verify if the balance returned was the master or subaccount (where the contract was deployed) balance.

    So the answer would be:

    pub fn get_balance(&self) -> Balance {
           
            env::account_balance()
        }
    

    noting that to have the transferible balance, it would have to take into account the min balance used for storage.