The first post “Installing Ethereum Development tools: geth and truffle on Ubuntu 22” was about preparation of development and testing environment to create first simple smart contract application. Now let us create application itself. Again, everything on Ubuntu 22 machine.
Create working directory and initialize new and empty Ethereum project:
# mkdir -p /workspace/contract1 # cd /workspace/contract1 # truffle init Starting init… ================ > Copying project files to /workspace/contract1 Init successful, sweet! Try our scaffold commands to get started: http://trufflesuite.com/docs # truffle create contract HelloLadyDebug |
Check content of working directory:
# ls -l total 20 drwxr-xr-x 2 root root 4096 Sep 12 16:38 contracts drwxr-xr-x 2 root root 4096 Sep 12 16:38 migrations drwxr-xr-x 2 root root 4096 Sep 12 16:38 test -rw-r–r– 1 root root 5927 Sep 12 16:38 truffle-config.js |
Open truffle-config.js file in text editor and uncomment this section:
development: { host: "127.0.0.1", // Localhost (default: none) port: 8545, // Standard Ethereum port (default: none) network_id: "*", // Any network (default: none) } |
In the same directory, create simple smart contract file HelloLadyDebug.sol in Solidity language:
// SPDX-License-Identifier: MIT pragma solidity 0.8.21; contract HelloLadyDebug { function sayHelloLadyDebug() public pure returns (string memory) { return "Hello Lady Debug"; } } |
Move HelloLadyDebug.sol to ./contracts directory:
# mv HelloLadyDebug.sol ./contracts/ |
Compiling:
# truffle compile Compiling your contracts… |
Compilation result is JSON HelloLadyDebug.json file located in ./build/contracts/ directory:
# ls -l ./build/contracts/ total 48 -rw-r–r– 1 root root 45691 Sep 13 23:22 HelloLadyDebug.json |
In migration subdirectory create 1_deploy_contracts.js file:
var HelloLadyDebug=artifacts.require ("../contracts/HelloLadyDebug.sol"); module.exports = function(deployer) { deployer.deploy(HelloLadyDebug); } |
Open second terminal window, switch to the same directory /workspace/contract1, change node version to 16.20.2 and start ganache-cli which begins to listen on port 8545:
# /usr/local/lib/node_modules/truffle/node_modules/.bin/ganache-cli -d –db=./db ganache v7.9.1 (@ganache/cli: 0.10.1, @ganache/core: 0.10.1) Starting RPC server Available Accounts Private Keys HD Wallet Default Gas Price BlockGas Limit Call Gas Limit Chain RPC Listening on 127.0.0.1:8545 |
Deployment
From first terminal:
# truffle deploy –reset Compiling your contracts… Starting migrations… 1_deploy_contracts.js Deploying ‘HelloLadyDebug’ > Saving artifacts Summary |
Truffle uses the first ganache account – 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1.
Ganache output on second terminal window:
eth_blockNumber net_version eth_accounts eth_getBlockByNumber eth_accounts net_version eth_getBlockByNumber eth_getBlockByNumber net_version eth_getBlockByNumber eth_estimateGas net_version eth_blockNumber eth_getBlockByNumber eth_estimateGas eth_getBlockByNumber eth_gasPrice eth_sendTransaction Transaction: 0xde6abce66639c92a2298bbabcb321284774eb7c810d8cf3de82d8f2f015f01d6 eth_getTransactionReceipt |
Verification:
The truffle console may be used to check result of block deployment, example of some function calls :
# truffle console truffle(development)> HelloLadyDebug.isDeployed() true truffle(development)> web3.eth.isMining() true truffle(development)> web3.eth.getBlockNumber() 9 |
The deployment problem I had:
*** Deployment Failed *** “HelloLadyDebug” could not deploy due to insufficient funds |
Google search shows that this problem is discussed by many developers. In my case, it happened because I started ganache not from different directory than /workspace/contract1.