Ethereum's DAPP development tool is not that friendly to beginner.
$ node –v # v12.18.4 in my version $ npm -v # 6.14.6 in my version
$ npm install -g truffle $ truffle version # Truffle v5.1.47 (core: 5.1.47) # Solidity v0.5.16 (solc-js) # Node v12.18.4 # Web3.js v1.2.1
# To create a bare Truffle project with no smart contracts included, use `truffle init`. $ truffle init # Once this operation is completed, you will see such project structure under your new created project directory. # contracts/: Directory for Solidity contracts # migrations/: Directory for scriptable deployment files # test/: Directory for test files for testing your application and contracts # truffle-config.js: Truffle configuration file
module.exports = { networks: { development: { host: "127.0.0.1", port: 7545, network_id: "*" // Match any network id } } };
pragma solidity >=0.4.22 <0.7.0; /** * @title Helloworld * @dev Store & retrieve value in a variable */ contract Helloworld { uint256 number; /** * @dev Store value in variable * @param num value to store */ function store(uint256 num) public { number = num; } /** * @dev Return value * @return value of 'number' */ function retrieve() public view returns (uint256){ return number; } }
#make sure you have CD to project directory $ truffle console $ create migration helloworld_migration # #open and paste the following migration script inside it
let Helloworld = artifacts.require("./Helloworld.sol"); module.exports = function(deployer) { deployer.deploy(Helloworld); };
#make sure you have CD to project directory $ Truffle Migrate #You will see a success message showing total deployments: 1 #In ganache, you will find out this new created contract under the "CONTRACTS" tab.
#make sure you have CD to project directory #enter console $ Truffle console #declare a contract instance $ truffle(development)> let instance = await Helloworld.deployed() #store 123 $ truffle(development)> instance.store(123) #retrieve 123 $ truffle(development)> instance.retrieve()