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

2 Answers 2

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)
        });
    });
});
4
  • 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, 2018 at 5: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} Apr 18, 2018 at 5: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. ethereum.stackexchange.com/questions/46066/…
    – hqt
    Apr 18, 2018 at 16:14
  • This should be instance.address not address
    – chuacw
    Feb 17, 2021 at 8: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.