0

When getting contract's balance on Remix, it returns the correct answer. But when the same function is called with JS Test, it returns a lot of junk.

I referred to How to get a contract's balance in Solidity? but it does not work anymore or something is missing in my JS code.

Solidity:

function contractBalance() public view returns(uint) {
    return address(this).balance;
}

Javascript:

it('should return contracts balance', async function() {
    let contractBalance = await this.auction.contractBalance()
    console.log(contractBalance)
})

Output:

TruffleConfig {                                                                                                                                                                                                    
  _deepCopy: [ 'compilers' ],                                                                                                                                                                                      
  _values: {                                                                                                                                                                                                       
    truffle_directory: '/home/satoshi/.nvm/versions/node/v12.13.1/lib/node_modules/truffle',                                                                                                                       
    working_directory: '/home/satoshi/auction',                                                                                                                                                    
    network: 'development',                                                                                                                                                                                        
    networks: { development: [Object] },                                                                                                                                                                           
    verboseRpc: false,                                                                                                                                                                                             
    gas: null,                                                                                                                                                                                                     
    gasPrice: null,                                                                                                                                                                                                
    from: null,                                                                                                                                                                                                    
    confirmations: 0,                                                                                                                                                                                              
    timeoutBlocks: 0,                                                                                                                                                                                              
    production: false,                                                                                                                                                                                             
    skipDryRun: false,                                                                                                                                                                                             
    build: null,                                                                                                                                                                                                   
    resolver: TestResolver {                                                                                                                                                                                       
      resolver: [TestResolver],                                                                                                                                                                                    
      source: [TestSource],                                                                                                                                                                                        
      search_path: '/tmp/test-202034-9962-wwsk5l.xcbo',                                                                                                                                                            
      seen: [],                                     
      require_cache: {},                            
      cache_on: true                                
    },  
    ...
0

Use await this.auction.contractBalance().call().

| improve this answer | |

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.