How to test contracts in truffle that will be returned by another contracts function as return value?

Hi. Given is a Contract that holds other Contracts, for instance TokenTimelock from OpenZepplin and a function, where such a instance will be returned:

function getTimeLock(uint256 i) public view returns (TokenTimelock) {        
    return _timeLocks[i];
}

When I call this function within a truffle test, only its address will be returned as string.

      const tokenLock = await this.tokenSale.getTimeLock(0);
      console.log(tokenLock);

which gives me just a string:

0xB1d7b638c0A01785e1bE97148D094abec942d286

But I'd like to execute a function on the particular TokenTimelock contract in my tests like its release-function.

How can I do this? Or do I need to go the "native" way like I would do with Web3.js, which needs the ABI AFAIK and makes things more compilcated?

1 Like

Hey @itinance! How are you creating the tokenSale contract in your tests? My gut feeling is that you are using truffle-contracts, based on the snippet you shared. If so, you can do the following to convert the address string into an instance:

const TokenTimelock = artifacts.require("TokenTimelock");
const tokenLockAddress = await this.tokenSale.getTimeLock(0);
const tokenLock = await TokenTimelock.at(tokenLockAddress);

Hope this helps!

2 Likes

Thanks so much @spalladino! That works pretty well and yes, I use truffle-contracts.

Btw, shouldn’t we require beneficiary not to be a zero-address while we already require releaseTime to be a valid time in the future? If you feel so also, I’d like to send a PR

1 Like

Given that TokenVesting.sol has this check in it's constructor then that sounds like a good suggestion for TokenTimelock.sol. A PR would be great for the maintainers to review. Thank you.

require(beneficiary != address(0), "TokenVesting: beneficiary is the zero address");