1

I notice that on the standard tests for crowdsale contracts in Openzeppelin, they transfer the tokens from the Token contract to the Crowdsale Contract:

context('once deployed', async function () {
      beforeEach(async function () {
        this.tokensale = await TokenSale.new(rate, wallet, this.token.address);
        await this.token.transfer(this.tokensale.address, tokenSupply);
      });

However other examples do not do this. So I wonder why this is necessary, if internally the CrowdSale contract uses the token contract to send the tokens:

function _deliverTokens(
        address beneficiary,
        uint256 tokenAmount
    )
    internal
    {
        _token.safeTransfer(beneficiary, tokenAmount);
    }

In the function above _token is the IERC20 interface of the Token contract. Can some one explain this a bit better than this page?

1 Answer 1

1

It is not required to send all tokens to a crowdsale contract upon creation. You may do whatever best fits your business model.

For example, a common scenario is to sell 25% of the tokens in the sale and retain the remainder. In this case, you will create 25% of the tokens into the crowdsale contract and 75% into your own wallet.

The contract above delivers the tokens, as expected. _token.safeTransfer(beneficiary, tokenAmount); is saying that this contract will transfer tokens from this contract to the beneficiary. The key to this statement is that the tokens live in the crowdsale contract, or else safeTransfer() will throw. As you mentioned, _token is the IERC20 interface to the contract.

The test that you referenced is a beforeEach function, which resets the contracts upon before each test. This is simply a convenient way to fill a contract with tokens to test. The tests written for this code are specific to the token distribution, thus it is feasible to send all tokens over.

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.