0
0
Blockchain / Solidityprogramming~10 mins

First smart contract deployment in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Deploying a smart contract puts your code on the blockchain so it can run automatically and securely. It makes your program live and usable by others.

You want to create a simple program that runs on the blockchain, like a digital agreement.
You want to share your contract with others so they can interact with it.
You want to test how your smart contract works on a test network before using real money.
You want to start a decentralized app that needs automatic rules enforced by code.
Syntax
Blockchain / Solidity
contract MyContract {
    // contract code here
}

// Deployment usually done via a tool or script, example in JavaScript:
const MyContract = await ethers.getContractFactory("MyContract");
const contract = await MyContract.deploy();
await contract.deployed();

The contract keyword defines your smart contract in Solidity.

Deployment is done using blockchain tools like Hardhat or Truffle with JavaScript scripts.

Examples
This is a simple contract that stores a message.
Blockchain / Solidity
pragma solidity ^0.8.0;

contract HelloWorld {
    string public message = "Hello, blockchain!";
}
This script deploys the HelloWorld contract and prints its address.
Blockchain / Solidity
// JavaScript deployment example using ethers.js
const HelloWorld = await ethers.getContractFactory("HelloWorld");
const hello = await HelloWorld.deploy();
await hello.deployed();
console.log("Contract deployed at", hello.address);
Sample Program

This Solidity contract stores a number and lets you set or get it. The JavaScript snippet shows how to deploy it.

Blockchain / Solidity
pragma solidity ^0.8.0;

contract SimpleStorage {
    uint256 public storedNumber;

    function setNumber(uint256 num) public {
        storedNumber = num;
    }

    function getNumber() public view returns (uint256) {
        return storedNumber;
    }
}

// Deployment script (JavaScript with ethers.js):
// const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
// const storage = await SimpleStorage.deploy();
// await storage.deployed();
// console.log("Deployed at", storage.address);
OutputSuccess
Important Notes

Always test your contract on a test network before deploying to the main blockchain.

Deployment costs gas (blockchain fees), so keep your contract simple at first.

Use tools like Hardhat or Truffle to make deployment easier and safer.

Summary

Deploying a smart contract makes your code live on the blockchain.

Use Solidity to write contracts and JavaScript tools to deploy them.

Test on test networks to avoid losing real money.