Skip to main content

Deploying Your Contract

Now that we have generated the Wasm binary from our source code and started a Substrate node, we want to deploy this contract onto our Substrate blockchain.

Smart contract deployment on Substrate is a little different than on traditional smart contract blockchains.

Whereas a completely new blob of smart contract source code is deployed each time you push a contract on other platforms, Substrate opts to optimize this behavior. For example, the standard ERC20 token has been deployed to Ethereum thousands of times, sometimes only with changes to the initial configuration (through the Solidity constructor function). Each of these instances take up space on the blockchain equivalent to the contract source code size, even though no code was actually changed.

In Substrate, the contract deployment process is split into two halves:

  1. Putting your code on the blockchain
  2. Creating an instance of your contract

With this pattern, contract code like the ERC20 standard can be put on the blockchain a single time, but instantiated any number of times. No need to continually upload the same source code over and waste space on the blockchain.

Fast Track

If you skipped previous steps and just want to see interaction with ink! smart contract, download flipper.wasm and metadata.json

Putting Your Code on the Blockchain

With your Substrate development node running, you can go back to the Polkadot UI where you will be able to interact with your blockchain.

Under the Developer tab click on the specially designed Contracts section of the UI.

In the Contracts section, select "Upload & deploy code".

In the popup, select a deployment account with some account balance, like Dave. For the contract's metadata, select the JSON file. In compiled contract WASM, select the flipper.wasm file we generated. The code bundle name will be "flipper". Once all the parameters are filled, click Next. docsdeploy1

Creating an Instance of Your Contract

Smart contracts exist as an extension of the account system on the blockchain. Thus creating an instance of this contract will create a new AccountId which will store any balance managed by the smart contract and allow us to interact with the contract.

To instantiate our contract we just need to give this contract account an endowment of 10 Units in order to pay the storage rent and set the maximum gas allowed to value(1,000,000):

{% hint style="info" %} Note: As mentioned earlier, contract creation involves creation of a new Account. As such, you must be sure to give the contract account at least the existential deposit defined by your blockchain. We also need to be able to pay the contract's rent (endowment). If we consume all of this deposit, the contract will become invalid. We can always refill the contract's balance and keep it on chain. {% endhint %}

You will then authorize the contract, by signing and submitting. You can also choose to leave a tip for the block author if you'd like.

When you press Deploy, you should see a flurry of events appear including the creation of a new account (system.NewAccount) and the instantiation of the contract (contracts.instantiate):