18

For example I have a testing block like this:

contract('CreeptomaPresale', function(accounts) {
    describe("adopted over allow quantity", function () {
        it("test get balance", async function () {
            let instance = await CreeptomaPresale.deployed();
            console.log("deployed address:" +  address)
        });
    });
});

I can get deployed address. But now, I don't know how to get balance of this address or any other addresses.

Thanks

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
hqt
  • 449
  • 1
  • 3
  • 12

2 Answers2

25

Try

contract('CreeptomaPresale', function(accounts) {
    describe("adopted over allow quantity", function () {
        it("test get balance", async function () {
            let instance = await CreeptomaPresale.deployed();
            console.log("deployed address:" +  instance.address);
            let balance = await web3.eth.getBalance(instance.address)
        });
    });
});
rma
  • 103
  • 2
Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
  • thanks. it worked. Can I have a little question: I write a payable function (sending transfer money from sender to contract's owner). My question is: I have list of accounts. which accounts is being used. thanks :D – hqt Apr 18 '18 at 05:03
  • 2
    The first one. Truffle does some setup, and you're neatly passing accounts into your test. Transactions are signed {from: accounts[0]} by default. As a matter of style, I like to set up some vars for roles like owner=accounts[0], funder=, sponsor=, etc. up front and then specify who is talking every step of the way, e.g. {from: funder1} – Rob Hitchens Apr 18 '18 at 05:14
  • thanks so much. I have used this style, but it seems not work. can you take a look for me in this post. thanks. https://ethereum.stackexchange.com/questions/46066/account-balance-does-not-decrease-after-send-ether – hqt Apr 18 '18 at 16:14
  • This should be instance.address not address – chuacw Feb 17 '21 at 08:44
6

For newer versions of Truffle, according to the example, it should actually be:

let balance = await web3.eth.getBalance(instance.address);

Otherwise, you will get the following error:

ReferenceError: address is not defined

Sergi Juanati
  • 3,399
  • 3
  • 17
  • 38