I'm trying to test contract which rely on a particular state of other deployed contract.

For example let say i have a contract wanting to test the "transfer" function of an ERC20 contract.

I should make sure that :

  1. The erc20 is deployed
  2. The contract is want to test is using the ERC20 instance
  3. Maybe minting some fund to the msg.sender
  4. That the msg.sender has used the approved() method on the deployed ERC20 contract to use the transferFrom() function.

I'm having a hard time setting this up.

In the docs it is said :

Before each contract() function is run, your contracts are redeployed to the running Ethereum client so the tests within it run with a clean contract state.

In the metacoin Example it seems they are chaining everything:

  return MetaCoin.deployed().then(function(instance) {
    meta = instance;
    return meta.getBalance.call(accounts[0]);
  }).then(function(outCoinBalance) {
    metaCoinBalance = outCoinBalance.toNumber();
    return meta.getBalanceInEth.call(accounts[0]);
  }).then(function(outCoinBalanceEth) {
    metaCoinEthBalance = outCoinBalanceEth.toNumber();
  }).then(function() {
    assert.equal(metaCoinEthBalance, 2 * metaCoinBalance, "Library function returned unexpected function, linkage may be broken");

Is it the direction that should be taken ?

I have kind of pseudo coded it but it looks very tiredsome to write.

it("Custom contract should be able to transfer for user", function() {
  var erc;
  var custom;
    return ERC20.deployed().then(function(instance) {
      erc = instance;
      return erc;
    }).then(function(erc) {
      custom = CustomContract.deploy(erc.address);
      return custom;
    }).then(function(custom) {
    }).then(function() {
      assert.equal(custom.WrappingTransferMethod(),true, "You are not authorized to transfer Fund");

How to do that properly ? Should it be done directly in solidity with the truffle asserts ? It's my first time testing a contract calling other deployed contracts and i'm at lost.

1 Answer 1


Since ES8, you can use async/await. It significantly improves the readability! The great thing is also that it is compatible with promises.

Your pseudo code would look something like this:

it("Custom contract should be able to transfer for user", async function() {
  var erc;
  var custom;

  var instance = await ERC20.deployed();
  var custom = await CustomContract.deploy(erc.address);

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.