I am making an app where you can make battles with other people. I have created a Battle contract that is created for each new battle and I have created a Battles contract to keep track of all battle contract addresses and all player addresses.

Now what is the best way to create a new battle contract and then store that battle contract address and player address in the Battles contract?

I need to make sure that Battles contract which keeps track of all battles and players cannot be altered publicly.

What is the best way to do this?

Should the user create a Battle contract representing a new battle and from within the constructor function of that new contract, send a transaction to Battle contract to store the new battle contract address?

Or should the user make a transaction the the parent Battles contract and the parent Battles contract then makes a transaction to create a new battle? Could the parent Battles contract then receive the newly created contract address immediately for it to store?

Also does this make sense for a contract design to create a new contract for each individual battle and then have one parent contract store addresses for all the individual battle contracts?


1 Answer 1


Ideally you should learn to use Solidity Libraries so you don't have to create a new contract for each battle. Contract creation is expensive, where as libraries allow you to create the contract once but store the data it uses elsewhere. In this case the battle functionality would live in the library contract and the data for each battle would live in the battles contract.

If you don't use libraries, then as you suggested, the battles contract should create individual battle contracts. Executing new Battle(constructor args) within the battles contract will return the address of the new contract. See the doc on creating contracts via new. Note the doc shows casting of the contract directly to the contract type, although you can keep it as an address.

  • The only thing I don't like about this is that all the ether that each player contributes for each battle will be stored in a single contract. Central Point of attack :/ Commented Jan 18, 2018 at 16:54
  • It shouldn't make a difference if the contract is appropriately audited and secure. Ether available to individual players should still only be accessible to the users involved within their games. But you're right, it is a more centralised point of risk.
    – willjgriff
    Commented Jan 18, 2018 at 17:18
  • 1
    Actually, I'm not sure it is any worse, because in the alternative, all the newly created contracts will be the same so any security vulnerabilities any individual one has they will all have.
    – willjgriff
    Commented Jan 18, 2018 at 17:52

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.