0

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
  • 1
    Is it possible for depositToken to have failed? The function ´GetAndShowUserBalances´ is declared async but you are not waiting for it to finish. – Ismael Oct 20 at 18:27
  • 1
    Function GetAndShowUserBalances is async, and so you probably want to await for its completion whenever you call it. – goodvibration Oct 20 at 20:13
  • In addition to that (though not directly related to your problem), you are passing two arguments to function balanceOf, which AFAIK takes only one parameter. – goodvibration Oct 20 at 20:13
  • balanceOf in my contract takes two parms, the first is the erc20Token,, the second is the user's address. I added official answer based on both of your comments, thanks! – NealWalters Oct 20 at 21:06
1

Comments above gave answer without the syntax. Need the await in the assignment:

 user1Balances = await GetAndShowUserBalances("before  depositToken", "user1", user1Addr) 

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.