Truffle Testing – How to Access Deployed Contract’s Address in Truffle JS Tests

testingtruffletruffle-contract

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

Best Answer

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.

Related Topic