-
bitcoin $87959.907984 USD
1.34% -
ethereum $2920.497338 USD
3.04% -
tether $0.999775 USD
0.00% -
xrp $2.237324 USD
8.12% -
bnb $860.243768 USD
0.90% -
solana $138.089498 USD
5.43% -
usd-coin $0.999807 USD
0.01% -
tron $0.272801 USD
-1.53% -
dogecoin $0.150904 USD
2.96% -
cardano $0.421635 USD
1.97% -
hyperliquid $32.152445 USD
2.23% -
bitcoin-cash $533.301069 USD
-1.94% -
chainlink $12.953417 USD
2.68% -
unus-sed-leo $9.535951 USD
0.73% -
zcash $521.483386 USD
-2.87%
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.
- Trump's Fed Chair Pick: Kevin Warsh Steps Up, Wall Street Watches
- 2026-01-30 22:10:06
- Bitcoin's Digital Gold Dream Tested As Market Shifts And New Cryptocurrencies Catch Fire
- 2026-01-30 22:10:06
- Binance Doubles Down: SAFU Fund Shifts Entirely to Bitcoin, Signaling Deep Conviction
- 2026-01-30 22:05:01
- Chevron's Q4 Results Show EPS Beat Despite Revenue Shortfall, Eyes on Future Growth
- 2026-01-30 22:05:01
- Bitcoin's 2026 Mega Move: Navigating Volatility Towards a New Era
- 2026-01-30 22:00:01
- Cardano (ADA) Price Outlook: Navigating the Trenches of a Potential 2026 Bear Market
- 2026-01-30 22:00:01
Related knowledge
How to Execute a Cross-Chain Message with a LayerZero Contract?
Jan 18,2026 at 01:19pm
Understanding LayerZero Architecture1. LayerZero operates as a lightweight, permissionless interoperability protocol that enables communication betwee...
How to Implement EIP-712 for Secure Signature Verification?
Jan 20,2026 at 10:20pm
EIP-712 Overview and Core Purpose1. EIP-712 defines a standard for typed structured data hashing and signing in Ethereum applications. 2. It enables w...
How to Qualify for Airdrops by Interacting with New Contracts?
Jan 24,2026 at 09:00pm
Understanding Contract Interaction Requirements1. Most airdrop campaigns mandate direct interaction with smart contracts deployed on supported blockch...
How to Monitor a Smart Contract for Security Alerts?
Jan 21,2026 at 07:59am
On-Chain Monitoring Tools1. Blockchain explorers like Etherscan and Blockscout allow real-time inspection of contract bytecode, transaction logs, and ...
How to Set Up and Fund a Contract for Automated Payments?
Jan 26,2026 at 08:59am
Understanding Smart Contract Deployment1. Developers must select a compatible blockchain platform such as Ethereum, Polygon, or Arbitrum based on gas ...
How to Use OpenZeppelin Contracts to Build Secure dApps?
Jan 18,2026 at 11:19am
Understanding OpenZeppelin Contracts Fundamentals1. OpenZeppelin Contracts is a library of reusable, community-audited smart contract components built...
How to Execute a Cross-Chain Message with a LayerZero Contract?
Jan 18,2026 at 01:19pm
Understanding LayerZero Architecture1. LayerZero operates as a lightweight, permissionless interoperability protocol that enables communication betwee...
How to Implement EIP-712 for Secure Signature Verification?
Jan 20,2026 at 10:20pm
EIP-712 Overview and Core Purpose1. EIP-712 defines a standard for typed structured data hashing and signing in Ethereum applications. 2. It enables w...
How to Qualify for Airdrops by Interacting with New Contracts?
Jan 24,2026 at 09:00pm
Understanding Contract Interaction Requirements1. Most airdrop campaigns mandate direct interaction with smart contracts deployed on supported blockch...
How to Monitor a Smart Contract for Security Alerts?
Jan 21,2026 at 07:59am
On-Chain Monitoring Tools1. Blockchain explorers like Etherscan and Blockscout allow real-time inspection of contract bytecode, transaction logs, and ...
How to Set Up and Fund a Contract for Automated Payments?
Jan 26,2026 at 08:59am
Understanding Smart Contract Deployment1. Developers must select a compatible blockchain platform such as Ethereum, Polygon, or Arbitrum based on gas ...
How to Use OpenZeppelin Contracts to Build Secure dApps?
Jan 18,2026 at 11:19am
Understanding OpenZeppelin Contracts Fundamentals1. OpenZeppelin Contracts is a library of reusable, community-audited smart contract components built...
See all articles














