Sep 7, 2023 by Arnaud Stoz | 910 views
Blockchain Smart Contract Ethereum
https://cylab.be/blog/280/deploy-smart-contract-to-a-local-blockchain
When starting to develop a smart contract it can sometimes be difficult and confusing how to deploy them and test them. You have the well known online tools like remix but sometimes you prefer to do stuff locally.
To develop and deploy your first smart contract we will use the following tools:
To install truffle and ganache you will need to have npm installed.
Once you have npm installed you can install truffle and ganache with the following command:
npm install -g truffle ganache
First create a new directory in the location of your choice
mkdir hello_world && cd hello_world
Then we will use truffle to simplify the development, testing and deployment of smart contract. To start a new project using truffle run
truffle init
It will create 3 different folders and 1 file:
Now that you have successfully written your first smart contract, it’s time to start your local blockchain to test it.
To start the blockchain just run
ganache
If everything works as expected you should see an output similar to the following
By default ganache sets ups 10 accounts all with 100 ETH. The only information really important here is the latest one, RPC listening port and address, the other are out of the scope of this blog post.
Once the blockchain is up and running we need to configure truffle to use that blockchain. Open the file truffle-config.js, uncomment the following block and make sure that it contains the correct value.
networks: {
........
development {
host : "127.0.0.1",
port : 8545,
network_id: "*"
}
.......
}
Once you have done the modification run truffle migrate
and look for the output. If you have a CONNECTION ERROR
check again your truffle-config.js file.
In the contracts folder create a new file called hello_world.sol. Smart contract are written in a language called solidity.
Add the following code to the file
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.5.0 <0.9.0;
contract HelloWorld{
string private greeting;
constructor() {
greeting = "Hello World!";
}
function getGreeting() public view returns(string memory){
return greeting;
}
function setGreeting(string memory _message) public{
greeting = _message;
}
}
The purpose of this blog post is not to explain smart contract development so in short this smart contract does 3 things:
In the migrations folder create a file called 1_helloWorld_migration.js
It is very important to start your filename with the number 1 (if it is the first file in this directory) otherwise truffle will not be able to deploy your smart contract.
add this code in the file
const HelloWorld = artifacts.require("HelloWorld");
module.exports = function(deployer){
deployer.deploy(HelloWorld);
}
Now deploy your smart contract by running truffle migrate
. You should see an output similar to this:
If you are using ganache UI you might face compatibility issue when deploying smart contract. In this case I recommend using ganache from the cli
Congratulation your contract has been published on your local blockchain!
To interact with your contract use the truffle console
command.
Then run
> let instance = await HelloWorld.deployed()
> instance.getGreeting() //show the greeting
> instance.setGreeting("My new greeting") //set greeting
Congratulations you have now interacted with your contract!
If you want to interact more with the blockchain using truffle console
and have a look at web3.
This blog post is licensed under CC BY-SA 4.0
Conference Blockchain Cyber Situation Awareness
Blockchain Ethereum
If you have already played with geth, you probably have already noticed the --dev option. This option is very handy when you want to quickly start a node to test something. However, you might have found that the developer account which is automatically set is a random account. This randomness can be annoying when trying to automate some testing tasks. The good news is that there is a way to set this developer account. That’s what will be explaine...Blockchain APT Detection
A few weeks ago, we had the opportunity to present a short webinar on two topics currently under research in our department: