I'm trying to test my simple coin-based contract in the Truffle test environment by sending funds to the first user's address. My understanding is that the contract is created (thereby calling the constructor) by migrating the contract to the blockchain, which then creates X amount of tokens. These tokens are then owned by me, accessible in the solidity code as "msg.sender." My question is: how do I access the funds located at "my" address?

  • Could you clarify what you're looking to do here and where you're trying to do it? Are you trying to write a JS test suite which uses truffle test, or write a web3 script which makes use of the contract for some other application?
    – ohsully
    Commented Sep 10, 2018 at 16:47
  • @ohsully Sure thing - I'm trying to use a JS test suite that I run via "truffle test", and I want to be able to send tokens to the first account address that is created in the Truffle test environment. Commented Sep 10, 2018 at 17:08
  • Feel free to drop me a comment if my answer didn't address your issue! And if it did, feel free to hit that accept button ;)
    – ohsully
    Commented Sep 10, 2018 at 19:44

1 Answer 1


The truffle test environment provides Ganache addresses inside the contract() block as an argument to the callback, like:

const YourContract = artifacts.require("YourContract.sol");

contract('Your contract test suite', function(accounts){
  const deployer = accounts[0];
  var yourCoin;

  before('Setup contract', async function(){
    yourCoin = await YourContract.deployed()

  it('Your first test...', function(){
    let initialBalance = yourCoin.balanceOf(deployer);

The trick here is that the truffle uses the first address provided by Ganache as the default for all deploying. Same holds true within your tests; if you make a call to a contract without specifying a {from : ...} block, it uses accounts[0].

  • 1
    Excellent! Your response helped me make more sense of how this works. The only element I'm confused about relates to the digital coins that 'deployer' owns. The balance of 'deployer' (when I print this value via console.log) doesn't seem to match the number of coins I mint after the program calls contract.deployed(). In which address (or other location in memory) are the digital coins stored? Commented Sep 11, 2018 at 14:10
  • Ah, now I realize the real issue. You're deploying a coin contract which creates its own coin, whereas the built-in getBalance() function is looking to see the Ethereum balance on the account. Can you link to your contract? It likely has the methods required to check and transfer balances. The big underlying thing here is that your coin contract maintains its own balance records which are distinct from the underlying ethereum ones.
    – ohsully
    Commented Sep 11, 2018 at 17:57
  • That makes sense to me - the contract doesn't handle ethereum, it only tracks each user's balance. It's the contract's "job" to specify how transactions between users are handled. The records of each transaction are then stored in the blockchain. In the contract I'm using, there's a function named 'balanceOf(address)' that returns the balance of the particular user. Looks like that's the one I want to be using. The current issue I'm facing is that I can't seem to call this function from my JS test file... any ideas on what could be going wrong? Commented Sep 11, 2018 at 18:21
  • Have you loaded the contract into your test? You want a before() block which waits for the contract to exist and saves it to a variable for your individual tests to refer to. Check out the edit to my answer, it now includes a line showing how that looks.
    – ohsully
    Commented Sep 11, 2018 at 20:30
  • 1
    Nevermind, I figured it out! Commented Sep 14, 2018 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.