I want to build a smart contract which can only be constructed, if there's an amount x of a specific ERC20 token available to spend for this contract.
So either the contract has to be allowed to spend this amount in the name of the creator of the contract or it already has the desired balance itself. I don't think there's really a difference between these 2 options from conceptual perspective but in some way the contract must be able to spend amount x and if amount x is not available for spending than the contract should not be deployed at all. And just for clarification, the amount x would also be passed with the constructor.

Actually i just want to have something like the following which is obviously not really possible:

constructor(uint x, ...) public {
        token.transferFrom(msg.sender, this, x)

Is it possible to achieve this?
It's not really an option to transfer tokens to the address of the contract before it's even deployed i think. Theoretically it could be done this way this since contract addresses are deterministically computed from what i know but i'm really looking for a more handy solution.

Oh, and this ERC20 token is also issued by myself. If it would bring any benfits to extend the token's smart contract, i could do this for sure.

In the end i want to have a contract which can promise to pay out certain participants and i want to have a guarantee from the beginning that this is possible. These certain participants are predetermined as well so imo it would only make sense to have the promised amount x of tokens ready at contract construction.

  • I see no reason why it wouldn't work. But just as a reminder, require() needs a boolean as an argument, so make sure that transferFrom() returns a boolean. Aug 20, 2018 at 21:27

2 Answers 2


Using a standard ERC20 token, yes, you could do this by precomputing the contract address (which is not terribly difficult) and calling approve before deploying the contract.

I can't think of a good way to make this process easier even by extending the token contract with logic specific for this task. The trouble is that you need to authorize the contract, and on what basis will you do that? Contracts can't have secrets, so there's no sort of authentication that makes sense except to authorize by address (which is already what approve does).

So I think you're stuck doing an approve before deploying the contract. An alternative is to just not do the check in the constructor at all and instead let some later task fail after deployment.

  • Thx for your answer. Do you know if there are any proposals on the way which would prevent such problems in the future?
    – nichoio
    Aug 21, 2018 at 10:52
  • I did have one additional thought: you could have the token contract deploy the other contract (and do the transfer as part of that process).
    – user19510
    Aug 21, 2018 at 13:58
  • Ha, nice idea. Unfortunately not suitable for my use case but maybe it will help someone else one day.
    – nichoio
    Aug 21, 2018 at 15:32

To be able to use transferFrom(msg.sender, this, x) from a contract it requires the msg.sender to previously approve the contract address, ie you have to call approve(contactAddress, x) before deploying the contract.

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.