Market Cap: $2.8588T -5.21%
Volume(24h): $157.21B 50.24%
Fear & Greed Index:

38 - Fear

  • Market Cap: $2.8588T -5.21%
  • Volume(24h): $157.21B 50.24%
  • Fear & Greed Index:
  • Market Cap: $2.8588T -5.21%
Cryptos
Topics
Cryptospedia
News
CryptosTopics
Videos
Top Cryptospedia

Select Language

Select Language

Select Currency

Cryptos
Topics
Cryptospedia
News
CryptosTopics
Videos

How to Write Your First Smart Contract with Solidity? (Step-by-Step Tutorial)

Set up Node.js, Truffle, and Ganache; write an ERC-20 token using OpenZeppelin; compile, deploy locally, and test via Truffle console and JavaScript.

Jan 17, 2026 at 12:00 am

Setting Up the Development Environment

1. Install Node.js and npm to manage dependencies required for Solidity tooling.

2. Use npm to install Truffle globally — a widely adopted development framework for Ethereum-based smart contracts.

3. Install Ganache as a personal blockchain for local testing; it provides private accounts with pre-funded ETH.

4. Configure a new Truffle project using truffle init to generate essential directories like contracts/, migrations/, and test/.

5. Verify installation integrity by running truffle version and confirming compatibility with Solidity compiler v0.8.x or higher.

Writing a Basic Token Contract

1. Create a new file named MyToken.sol inside the contracts/ folder.

2. Declare the Solidity version pragma: pragma solidity ^0.8.20; to ensure deterministic compilation behavior.

3. Import OpenZeppelin’s ERC-20 implementation: import '@openzeppelin/contracts/token/ERC-20/ERC-20.sol';

4. Define the contract inheritance: contract MyToken is ERC20 { ... } and initialize name, symbol, and total supply in the constructor.

5. Add a mint function restricted to the owner using onlyOwner modifier from OpenZeppelin’s AccessControl library.

Compiling and Deploying the Contract

1. Write a migration script in migrations/2_deploy_contracts.js to instantiate and deploy MyToken with 1 million tokens.

2. Run truffle compile to generate ABI and bytecode; confirm no syntax or version mismatch errors appear.

3. Launch Ganache and copy its RPC server URL (e.g., http://127.0.0.1:7545) into truffle-config.js under the development network.

4. Execute truffle migrate --network development to deploy the contract and record transaction hashes and contract addresses.

5. Confirm deployment success by checking Ganache’s transaction log and verifying the contract address appears in the console output.

Interacting via Truffle Console

1. Start the Truffle console connected to Ganache: truffle console --network development.

2. Fetch the deployed instance: let instance = await MyToken.deployed();

3. Query total supply using (await instance.totalSupply()).toString() — expect output matching the initial mint value.

4. Transfer tokens between accounts: await instance.transfer('0xAb8483F64d9C6d1EcF9b849Ae677dC320f55a1B5', '1000').

5. Validate balance changes with (await instance.balanceOf('0xAb8483F64d9C6d1EcF9b849Ae677dC320f55a1B5')).toString().

Testing with JavaScript

1. Create test/mytoken.js containing Mocha-style test cases for core functionality.

2. Use contract() to define test context and it() blocks to assert behavior like minting, transferring, and event emission.

3. Assert correct event logs with assert.equal(receipt.logs[0].event, 'Transfer') after calling transfer.

4. Test reversion conditions: attempt to transfer more than balance and verify revert is thrown using expectRevert utility.

5. Run tests with truffle test and confirm all assertions pass without timeout or runtime exceptions.

Frequently Asked Questions

Q: Can I deploy a Solidity contract without using Truffle?A: Yes. Alternatives include Hardhat, Remix IDE, or direct web3.js calls with compiled bytecode and ABI.

Q: What happens if I forget the payable keyword on a function that receives ETH?A: The function will revert any transaction attempting to send ETH, resulting in a failed execution and gas consumption.

Q: Is it safe to use tx.origin for access control?A: No. tx.origin can be manipulated via phishing contracts and must never replace msg.sender in permission checks.

Q: How do I verify my contract source code on Etherscan?A: Submit the exact Solidity source, compiler version, optimization settings, and constructor arguments through Etherscan’s verification form.

Disclaimer:info@kdj.com

The information provided is not trading advice. kdj.com does not assume any responsibility for any investments made based on the information provided in this article. Cryptocurrencies are highly volatile and it is highly recommended that you invest with caution after thorough research!

If you believe that the content used on this website infringes your copyright, please contact us immediately (info@kdj.com) and we will delete it promptly.

Related knowledge

See all articles

User not found or password invalid

Your input is correct