curl -fsSL https://deb.nodesource.com/setup_21.x | sudo -E bash -
sudo apt-get install -y nodejs
First, ensure you have Truffle installed:
npm install -g truffle
Create a directory for your Truffle project and initialize it:
mkdir MyContract
cd MyContract
truffle init
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;
}
}
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",
},
},
};
Compile your smart contract using Truffle:
truffle compile
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
You can interact with your deployed contract using Truffle console:
truffle console --network development
In the console, you can interact with your contract