Skip to content

Latest commit

 

History

History
114 lines (99 loc) · 3.15 KB

Deploy_Chaincode.md

File metadata and controls

114 lines (99 loc) · 3.15 KB

Hyperledger Exploration



Hyperledger Besu


Deploying a Smart Contract on a Hyperledger Besu Private Network

Prerequisites

  • Hyperledger Besu Private Network
  • Node.js v14 or higher
curl -fsSL https://deb.nodesource.com/setup_21.x | sudo -E bash -
sudo apt-get install -y nodejs
  • Truffle framework
  • Solidity Smart Contract (Chaincode)

Step 1: Install Truffle and Ganache CLI

First, ensure you have Truffle installed:

npm install -g truffle

Step 2: Initialize a Truffle Project

Create a directory for your Truffle project and initialize it:

mkdir MyContract
cd MyContract
truffle init

truffle_init

Step 3: Write Your Smart Contract

Create a new Solidity file in the contracts directory, e.g., MyContract.sol

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

contract MyContract {
    string public message;

    constructor(string memory _message) {
        message = _message;
    }

    function setMessage(string memory _message) public {
        message = _message;
    }
}

Step 4: Configure Truffle

Update truffle-config.js file MyContract directory to configure the network:

// truffle-config.js
module.exports = {
  networks: {
    besu: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*", // Match any network id
    },
  },
  compilers: {
    solc: {
      version: "0.8.2",
    },
  },
};

Step 5: Compile the Smart Contract

Compile your smart contract using Truffle:

truffle compile

Step 6: Deploy the Smart Contract

Create a migration script in the migrations directory, e.g., deploy_contracts.js:

// migrations/deploy_contracts.js
const MyContract = artifacts.require("MyContract");

module.exports = function (deployer) {
  deployer.deploy(MyContract, "Hello, Besu!");
};

Deploy your contract to the network:

truffle migrate --network development

truffle_deploy

Step 7: Interact with the Smart Contract

You can interact with your deployed contract using Truffle console:

truffle console --network development

In the console, you can interact with your contract

  • Deploying while the Node is online

truffle_success

  • Deploying while the Node is offline

truffle_failure