Constructor

A constructor is a special function that runs exactly once: when your contract is first deployed to the blockchain. Think of it as the setup step that prepares your contract before anyone can use it.

Unlike regular functions, a constructor has no name and there can only be one per contract. Its job is to initialize state, like setting a starting score, assigning an owner, or configuring default values.

Try It Yourself

Complete each task below to see how constructors accept parameters and initialize state.

Task 1 of 3 Accept Funds on Deploy

Make the constructor payable so the contract can receive funds at deployment.

1 Toggle payable
2 Done!
Click the Payable toggle on the Contract Setup node to enable it.
Press enter or space to select a node. You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.
Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.

Payable Constructor

Toggle the Payable switch on the constructor node to let the contract accept native token when it’s deployed. This is useful when you want the contract to start with funds in its balance, like stocking the cash registers on opening day.

A common use case: deploying a treasury or vault contract that needs initial capital to operate.

Constructor Modifiers

The Modifiers field lets you attach guard functions that run during deployment. This is an advanced feature mainly used with proxy patterns, where a modifier like initializer ensures setup logic runs exactly once.

For most contracts you won’t need constructor modifiers, but they’re available when the architecture calls for them.

What You Learned

  • Constructors accept parameters so the deployer can pass in initial values at deploy time
  • Constructors initialize state by wiring parameter data into setVariable nodes, setting your contract’s starting values
  • Payable constructors accept native token on deploy, letting your contract start with funds
  • constructor modifiers add guard checks during deployment (advanced, used in proxy patterns)

Now that you know how to set up a contract with a constructor, you have all the building blocks to create complete smart contracts.