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.
First smart contract deployment in 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.
pragma solidity ^0.8.0; contract HelloWorld { string public message = "Hello, blockchain!"; }
// 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);
This Solidity contract stores a number and lets you set or get it. The JavaScript snippet shows how to deploy it.
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);
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.
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.
Practice
Solution
Step 1: Understand deployment meaning
Deployment means putting the contract code on the blockchain so it can run.Step 2: Differentiate from writing and testing
Writing is coding, testing is checking locally or on test networks, deployment is live on blockchain.Final Answer:
Making the contract code live and usable on the blockchain -> Option BQuick Check:
Deploy = live on blockchain [OK]
- Confusing deployment with just writing code
- Thinking testing is deployment
- Believing deployment deletes contracts
MyContract?Solution
Step 1: Recall Solidity contract declaration
Solidity uses the keywordcontractfollowed by the contract name and braces.Step 2: Check each option's syntax
contract MyContract { } matches Solidity syntax; others use wrong keywords or formats.Final Answer:
contract MyContract { } -> Option AQuick Check:
Solidity contract starts with 'contract' keyword [OK]
- Using 'class' instead of 'contract'
- Writing function instead of contract
- Assigning contract like a variable
contract Counter {
uint count = 0;
function increment() public {
count += 1;
}
function getCount() public view returns (uint) {
return count;
}
}If you call
increment() twice and then getCount(), what is the output?Solution
Step 1: Understand contract state changes
Each call toincrement()adds 1 tocount, starting from 0.Step 2: Calculate after two increments
After two calls,countbecomes 2.getCount()returns this value.Final Answer:
2 -> Option CQuick Check:
2 increments = count 2 [OK]
- Forgetting state changes persist on blockchain
- Assuming count resets each call
- Confusing function names
contract Simple {
uint number
function setNumber(uint num) public {
number = num;
}
}What is the cause of the error?
Solution
Step 1: Check Solidity syntax rules
Variable declarations require a semicolon at the end.Step 2: Identify missing semicolon
uint numberline misses a semicolon, causing syntax error.Final Answer:
Missing semicolon after uint number -> Option DQuick Check:
Missing semicolon = syntax error [OK]
- Thinking function visibility causes syntax error
- Believing contract names must be lowercase
- Expecting return type for void functions
Solution
Step 1: Understand deployment environments
Mainnet uses real money; testnets simulate blockchain without real cost.Step 2: Choose safe testing method
Deploying on testnets like Ropsten or Goerli lets you test without risking real funds.Final Answer:
Deploy on a test network like Ropsten or Goerli first -> Option AQuick Check:
Testnet deployment = safe testing [OK]
- Deploying directly on mainnet without testing
- Not deploying at all and missing testing
- Using private computer without blockchain for deployment
Ready to prove it?
Pass the test and this pattern moves unknown
