Storing Customer Data on Ethereum: Deploying a Smart Contract (Part 2)

·

Introduction

This guide demonstrates how to use Ethereum's Smart Contracts for decentralized data storage, specifically focusing on customer data management. While Smart Contracts aren't AI-powered, they function as self-executing programs on the Ethereum blockchain, written in Solidity.

Key Objectives


Tools & Prerequisites

Development Tools

  1. Visual Studio Code (Primary IDE)
  2. Node.js (JavaScript runtime)
  3. Truffle Suite (Ethereum development framework)

    npm install -g truffle

Environment Setup

1. Initialize Project

mkdir customers
cd customers
truffle init

This generates the foundational structure for Solidity development.


Smart Contract Development

1. Create DataStorage.sol

Under the contracts folder, create this file with the following structure:

pragma solidity ^0.8.0;

contract DataStorage {
    struct Company {
        string id;
        string name;
        string tel;
        string addr;
    }
    
    mapping(string => Company) public companyHashMap;
    
    function addCompanyData(
        string memory id,
        string memory name,
        string memory tel,
        string memory addr
    ) public {
        companyHashMap[id] = Company(id, name, tel, addr);
    }
}

2. Configure Deployment

Edit migrations/2_deploy_contracts.js:

const DataStorage = artifacts.require("DataStorage");
module.exports = function(deployer) {
  deployer.deploy(DataStorage);
};

3. Network Settings

Update truffle.js with your Ethereum RPC endpoint and network ID:

module.exports = {
  networks: {
    azureNetwork: {
      host: "YOUR_RPC_ENDPOINT",
      port: 8545,
      network_id: "*"
    }
  }
};

Deployment Process

1. Unlock Ethereum Account

Access your Azure-hosted Ethereum node via SSH and run:

geth attach
personal.unlockAccount(personal.listAccounts[0], "YOUR_PASSWORD", 0)

2. Deploy Contract

truffle migrate --reset --network azureNetwork

3. Verify Deployment

Enter the Truffle console:

truffle console --network azureNetwork

Then fetch contract details:

DataStorage.deployed().then(instance => tp=instance)

👉 Learn more about Smart Contract addresses


Critical Information

  1. Contract Address: Found in deployment logs (e.g., 0x123...abc).
  2. Account Address: Retrieve via:

    personal.listAccounts[0]
  3. Function Signature Hash:

    web3.sha3("addCompanyData(string,string,string,string)").substring(0, 10)

    Example output: 0x4d586d83.


FAQs

Q1: Why use Smart Contracts for data storage?

A1: They provide immutability, transparency, and decentralization—ideal for audit trails and trustless systems.

Q2: How do I interact with the deployed contract?

A2: Use libraries like web3.js or ethers.js, or call via JSON-RPC (covered in Part 3).

Q3: Is Solidity the only language for Smart Contracts?

A3: While Solidity is dominant, alternatives like Vyper exist.


Next Steps

In Part 3, we'll explore interacting with the contract using C# and JSON-RPC. 👉 Advanced Ethereum integrations

Always test contracts on a testnet before mainnet deployment!