Using "web3": "1.0.0-beta.46" because doing a tutorial, and they recommended using all his versions to make sure everything works together.
Using Gnache and Truffle unit tests, I have tried two helper routines to get Eth balance for an address. URL to source of the code in comments inline below:
const Web3 = require('web3')
const rpcURL = 'https://127.0.0.1:7545'
const objWeb3 = new Web3(rpcURL);
//https://ethereum.stackexchange.com/questions/11444/web3-js-with-promisified-api/24238#24238
const promisify = (inner) =>
new Promise((resolve, reject) =>
inner((err, res) => {
if (err) { reject(err) }
resolve(res);
})
);
export const getBalance = (account, at) =>
promisify(cb => objWeb3.eth.getBalance(account, at, cb)
);
//https://github.com/shawntabrizi/ethbalance/blob/master/index.html
export function getBalance2(address) {
console.log("debug: helpers/getBalance2-A");
try {
objWeb3.eth.getBalance(address, function (error, wei) {
console.log("debug: helpers/getBalance2-B");
if (!error) {
console.log("debug: helpers/getBalance2-C: wei=" + wei)
//var balance = web3.fromWei(wei, 'ether');
return wei;
}
else
{
console.log("debug: helpers/getBalance2-D error=" + error);
}
});
} catch (err) {
console.log("debug: helpers/getBalance2-E error=" + err);
}
}
The promisfy solution gives me a timeout error:
Timeout of 300000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
The getBalance2 routine returns Undefined, as shown by these console.logs:
depositing tokens
success
debug: helpers/getBalance2-A
result.EtherWallet=undefined
1) "before each" hook for "tracks the token deposit"
...
TypeError: Cannot read property 'toString' of undefined
at EthToWei (e:/GitHub/NealWalters/DappUniversity/blockchain-developer-bootcamp/test/helpers.js:13:26)
at _callee2$ (e:/GitHub/NealWalters/DappUniversity/blockchain-developer-bootcamp/test/Exchange.test.js:61:43)
at tryCatch (node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:45:40)
at Generator.invoke [as _invoke] (node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:271:22)
at Generator.prototype.(anonymous function) [as next] (node_modules\@babel\runtime\node_modules\regenerator-runtime\runtime.js:97:21)
at step (test\Exchange.test.js:7:191)
at test\Exchange.test.js:7:361
at process._tickCallback (internal/process/next_tick.js:68:7)
My ultimate goal is a function that shows the balance of user's wallet, then his token and ETH balance in my exchange contract:
async function GetAndShowUserBalances(description, userName, userAddr)
{
var result = {};
result.Ether = await exchange.balanceOf(ETHER_ADDRESS, userAddr);
result.Tokens = await exchange.balanceOf(token.address, userAddr);
result.EtherWallet = await getBalance2(userAddr);
console.log("result.EtherWallet=" + result.EtherWallet);
//result.EtherWallet = await web3.eth.getBalance(userAddr);
result.TokenWallet = await token.balanceOf(user1Addr)
console.log("ShowUserBalances: " + description + " " + userName +
" Contract: Ether=" + EthToWei(result.Ether) +
" Tokens=" + EthToWei(result.Tokens) +
" Wallet: Ether=" + EthToWei(result.EtherWallet) +
" Tokens=" + EthToWei(result.TokenWallet)
);
return result;
}
Update - what is minimal code in a Truffle test to prove this works or not. I tried calling it without "await", but if I use "await", then I have to put in "async" function.
const Web3 = require('web3')
const rpcURL = 'https://127.0.0.1:7545'
const objWeb3 = new Web3(rpcURL);
require('chai')
.use(require('chai-as-promised'))
.should()
async function demo() {
let user1Address = "0x1D41CB7d963a5B96687e80Be1b23320e40176F02"
//let balance = getBalance3(user1Address);
//let balance = objWeb3.eth.getBalance(user1Address).then
// (console.log);
await objWeb3.eth.getBalance(user1Address).then (function (balance) {
console.log("Balance=" + balance);
}).catch (function(e) {
console.log("Error=" + e);
});
await objWeb3.eth.getBalance(user1Address,function(error,result){
if(error){
console.log("Error=" + error)
}
else{
console.log("result=" + result)
}
})
console.log("End Function");
}
demo();
console.log("The end");
The above returns only:
Compiling your contracts...
===========================
> Compiling .\src\contracts\Exchange.sol
> Compiling .\src\contracts\Token.sol
> Artifacts written to C:\Users\nwalt\AppData\Local\Temp\test-119924-47264-1e1elx.7p1ik
> Compiled successfully using:
- solc: 0.5.0+commit.1d4f565a.Emscripten.clang
The end
0 passing (2ms)
Ran the node below: node src/web3demo.js. It also runs without showing anything.
const Web3 = require('web3')
console.log("Got the web3 module")
const rpcURL = 'https://127.0.0.1:7545'
const web3 = new Web3(rpcURL);
console.log("Got web3 connection")
const address = '0x1D41CB7d963a5B96687e80Be1b23320e40176F02'
console.log ("attempting to show balance of ETH address: " + address)
web3.eth.getBalance(address, (err,balance) => console.log("Balance=" + web3.utils.balance))
console.log("The end");
Output:
Got the web3 module
Got web3 connection
attempting to show balance of ETH address: 0x1D41CB7d963a5B96687e80Be1b23320e40176F02
The end