5

I've written a test that involves returning the token balance that the contract itself holds. In the contract source file I would access the balance using the function getBalanceOf(this). However, when I include instance.getBalanceOf(this) in my JS tests, I get a new BigNumber() is not a number error.

I've narrowed it down to my tests not having access to the deployed contract's address. I know you can access it in Solidity tests using the DeployedAddresses.sol library, but is there any way to do it in JS tests?

Cheers, Pryce

4
const contract = require('truffle-contract');
const TokenArtifact = require('./../../build/contracts/YourToken.json');

var Token = contract(TokenArtifact);
Token.setProvider(window.web3.currentProvider);
var tokenInstance = await Token.deployed();

Now,

tokenInstance.address

Will give you, your deployed contract's address.

3

I believe what you are looking for is instance.address

2

When you run truffle test ./path/to/test.js, truffle automatically runs the migration script first. This means you can reference those deployed contracts through the artifacts global like so:

const ContractInstance = artifacts.require('ContractInstance.sol')

ContractInstance.deployed()
.then(inst => {
  // do some stuff
})

If you are simply doing a getter request, you should .call() the method instead of making a tx. contract.method.call() will actually return the value where as the transaction contract.method() will return a big tx object.

ContractInstance.deployed()
.then(inst => {
  inst.getBalanceOf.call()
}).then(balance => {
  console.log(balance.toNumber()) // 33
})
  • Cheers for the answer. I've got the contract's instance saved to a variable which I'm using throughout. My getBalanceOf is a also a call, I just neglected to mention it. What I'm really looking for is what to pass as an argument to instance.getBalanceOf.call(address) where address is the address of the deployed contract. I appreciate any insight you can offer. – Pryce Turner Aug 31 '17 at 4:38
0

@PryceTurner :"What I'm really looking for is what to pass as an argument to instance.getBalanceOf.call(address) where address is the address of the deployed contract. I appreciate any insight you can offer."

The answer you're looking for is: instance.getBalanceOf.call(ContractInstance.address)

Notes:

  • ContactInstance being what you defined up top: const ContractInstance = artifacts.require('ContractInstance.sol')
  • In ContactInstance.address 'address' is case sensitive

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.