8

Is there an ether-pudding way to get the balance of a contract/account, or should I just use web3.eth.getBalance(account)?

  • I'm still having a hard time with this. Something recently changed with the latest truffle release. – Mark Wagner Aug 16 '17 at 22:03
7

Unless something has changed in ether-pudding, it is primarily focused on wrapping/simplifying contract code while also adding javascript promises to deal with asynchronous calls. So I do not believe it has a simpler call to get the balance.

Short answer: use the web3 call you mention.

10

This doesn't exist in truffle-contract. Here's a quick way to wrap web3 calls to make them use promises.

const promisify = (inner) =>
  new Promise((resolve, reject) =>
    inner((err, res) => {
      if (err) { reject(err) }
      resolve(res);
    })
  );

const getBalance = (account, at) =>
  promisify(cb => web3.eth.getBalance(account, at, cb));

// use getBalance
const bigNumberBalance = await getBalance('0xabcdef');
console.log(bigNumberBalance.toString());

You can use this promisify helper to wrap any function which accepts a callback param and handles errors the same way as web3. Here's another example of using it:

const getTransaction = (hash) =>
  promisify(cb => web3.eth.getTransaction(hash, cb));

// use getTransaction
const transaction = await getTransaction('0xcaff');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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