Once you have installed truffle and ganache you can write a Solidity smart contract: Create a new file with the .sol extension, e.g., MyContract.sol, in the contracts directory of your Truffle project. Then, write your smart contract using Solidity syntax. Here's a simple example:
pragma solidity ^0.8.0; contract MyContract { uint256 public value; function setValue(uint256 newValue) public { value = newValue; } function getValue() public view returns (uint256) { return value; } }
Compile and deploy the smart contract: In your Truffle project, create a new migration file in the migrations directory, e.g., 2_deploy_contracts.js, and write the deployment code:
const MyContract = artifacts.require("MyContract"); module.exports = function (deployer) { deployer.deploy(MyContract); };
Now, start Ganache to have a local blockchain running. In your project directory, run the following command to compile and deploy the smart contract:
truffle migrate --network development
Interact with the smart contract: You can use the Truffle console to interact with your deployed smart contract:
truffle console --network development
In the console, you can call the smart contract's functions. For example:
const myContract = await MyContract.deployed(); await myContract.setValue(42); const value = await myContract.getValue(); console.log(value.toNumber());Home