This maybe just a simple Javascript issue, but it's in the context of running under Truffle. I can't seem to access my object variables coming back from my function; thus the tests are failing with error about the toString being undefined. So I added console.log code to show the values...
To avoid rewriting repetitive, code, I created this function:
async function GetAndShowUserBalances(description, userName, userAddr)
{
let balanceTokens, balanceEth
balanceTokens= await exchange.balanceOf(token.address, userAddr);
balanceEth = await exchange.balanceOf(ETHER_ADDRESS, userAddr);
console.log("ShowUserBalances: " + description + " " + userName +
" Ether=" + tokensRev(balanceEth) +
" Tokens=" + tokensRev(balanceTokens));
var result = {};
result.Ether = balanceEth;
result.Tokens = balanceTokens;
console.log('============================================ START result ')
console.log(JSON.stringify(result))
console.log('============================================ END result ')
return result;
}
The results look good as JSON in the above console.log.
Then I call it like this:
describe('depositing tokens', async () => {
let result, user1Balances
const numTokensToDeposit = 8
describe('success', async () => {
beforeEach(async() => {
//approve this smart contract as the exchange address
user1Balances = GetAndShowUserBalances("before depositToken", "user1", user1Addr)
await token.approve(exchange.address, tokens(numTokensToDeposit), { from: user1Addr})
result = await exchange.depositToken(
token.address, tokens(numTokensToDeposit), { from: user1Addr})
console.log("DepositTokens completed: numTokensToDeposit=" + numTokensToDeposit)
})
it('tracks the token deposit',async() => {
user1Balances = GetAndShowUserBalances("after depositToken", "user1", user1Addr)
// check exchange token balance
//balance1 = await token.balanceOf(exchange.address);
console.log('============================================ START user1Balances ')
console.log(JSON.stringify(user1Balances))
console.log('============================================ END user1Balances ')
user1Balances.Tokens.toString().should.equal(tokens(numTokensToDeposit).toString())
//check tokens on exchange for the user
//balance2 = await exchange.tokens(token.address, user1Addr)
user1Balances.Ether.toString().should.equal(tokens(numTokensToDeposit).toString())
})
Console display:
depositing tokens
success
ShowUserBalances: before depositToken user1 Ether=0 Tokens=0
============================================ START result
{"Ether":"0","Tokens":"0"}
============================================ END result
DepositTokens completed: numTokensToDeposit=8
============================================ START user1Balances
{}
============================================ END user1Balances
GetAndShowUserBalances
isasync
, and so you probably want toawait
for its completion whenever you call it.balanceOf
, which AFAIK takes only one parameter.