Create and Deploy Smart Contract

By | September 18, 2023

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:
$ truffle create contract YourContractName # scaffold a contract
$ truffle create test YourTestName          # scaffold a test

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…
===========================
> Compiling ./contracts/HelloLadyDebug.sol
> Artifacts written to /workspace/contract1/build/contracts
> Compiled successfully using:
– solc: 0.8.21+commit.d9974bed.Emscripten.clang

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
==================
(0) 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1 (1000 ETH)
(1) 0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0 (1000 ETH)
(2) 0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b (1000 ETH)
(3) 0xE11BA2b4D45Eaed5996Cd0823791E0C93114882d (1000 ETH)
(4) 0xd03ea8624C8C5987235048901fB614fDcA89b117 (1000 ETH)
(5) 0x95cED938F7991cd0dFcb48F0a06a40FA1aF46EBC (1000 ETH)
(6) 0x3E5e9111Ae8eB78Fe1CC3bb8915d5D461F3Ef9A9 (1000 ETH)
(7) 0x28a8746e75304c0780E011BEd21C72cD78cd535E (1000 ETH)
(8) 0xACa94ef8bD5ffEE41947b4585a84BdA5a3d3DA6E (1000 ETH)
(9) 0x1dF62f291b2E969fB0849d99D9Ce41e2F137006e (1000 ETH)

Private Keys
==================
(0) 0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d
(1) 0x6cbed15c793ce57650b9877cf6fa156fbef513c4e6134f022a85b1ffdd59b2a1
(2) 0x6370fd033278c143179d81c5526140625662b8daa446c22ee2d73db3707e620c
(3) 0x646f1ce2fdad0e6deeeb5c7e8e5543bdde65e86029e2fd9fc169899c440a7913
(4) 0xadd53f9a7e588d003326d1cbf9e4a43c061aadd9bc938c843a79e7b4fd2ad743
(5) 0x395df67f0c2d2d9fe1ad08d1bc8b6627011959b79c53d7dd6a3536a33ab8a4fd
(6) 0xe485d098507f54e7733a205420dfddbe58db035fa577fc294ebd14db90767a52
(7) 0xa453611d9419d0e56f499079478fd72c37b251a94bfde4d19872c44cf65386e3
(8) 0x829e924fdf021ba3dbbc4225edfece9aca04b929d6e75613329ca6f1d31c0bb4
(9) 0xb0057716d5917badaf911b193b12b910811c1497b5bada8d7711f758981c3773

HD Wallet
==================
Mnemonic:       myth like bonus scare over problem client lizard pioneer submit female collect
Base HD Path:  m/44’/60’/0’/0/{account_index}

Default Gas Price
==================
2000000000

BlockGas Limit
==================
30000000

Call Gas Limit
==================
50000000

Chain
==================
Hardfork: shanghai
Id:        1337

RPC Listening on 127.0.0.1:8545

Deployment
From first terminal:


# truffle deploy –reset

Compiling your contracts…
===========================
> Everything is up to date, there is nothing to compile.

Starting migrations…
======================
> Network name:    ‘development’
> Network id:      1695008062341
> Block gas limit: 30000000 (0x1c9c380)

1_deploy_contracts.js
=====================

   Deploying ‘HelloLadyDebug’
   ————————–
   > transaction hash: 0xde6abce66639c92a2298bbabcb321284774eb7c810d8cf3de82d8f2f015f01d6
   > Blocks: 0         Seconds: 0
   > contract address: 0xD833215cBcc3f914bD1C9ece3EE7BF8B14f841bb
   > block number:     9
   > block timestamp:  1695008070
   > account:          0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
   > balance:          999999.99730812913423284
   > gas used:         152911 (0x2554f)
   > gas price:        2.804178482 gwei
   > value sent:       0 ETH
   > total cost:       0.000428789735861102 ETH

   > Saving artifacts
   ————————————-
   > Total cost:       0.000428789735861102 ETH

Summary
=======
> Total deployments:   1
> Final cost:          0.000428789735861102 ETH

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
  Contract created: 0xd833215cbcc3f914bd1c9ece3ee7bf8b14f841bb
  Gas usage: 152911
  Block number: 9
  Block time: Sun Sep 17 2023 23:34:30 GMT-0400 (Eastern Daylight Saving Time)

eth_getTransactionReceipt
eth_getCode
eth_getTransactionByHash
eth_getBlockByNumber
eth_getBalance

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
  * Account: 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
  * Balance: 0 wei
  * Message: insufficient funds for gas * price + value
  * Try:
      + Using an adequately funded account
      + If you are using a local Geth node, verify that your node is synced.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *