Create and prepare a smart contract
First of all, you need to create and prepare your smart contract to work with it. To do this:
-
Install Solidity compiler by running the following command:
npm install -g solc
NoteYou can find the Solidity compiler description and usage here.
-
Ensure you are in
dcloud_example
directory, you've created on step 1 here, or create this directory, if you haven't done it yet. -
Open a smart contract file
greeter.sol
, which contains the following code://SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.13;
contract Greeter {
string private greeting;
constructor() {
greeting = 'Hello World!';
}
function greet() public view returns (string memory) {
return greeting;
}
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
}The main actions in this code example:
-
Declaration of a new contract variable, where the
greeting
variable will be stored:string private greeting;
-
Declaration of contract inner variable, where the
greeting
variable will be stored:constructor() {
greeting = 'Hello World!';
}When you create a smart contract, the
greeting
variable is initialized with a classicalHello World!
text. -
The
greet()
function returns the current greeting. -
The
setGreeting()
function replaces the current greeting with a new one.
-
-
Compile the given code. You will receive an
.abi
file:Run the following command in the directory, where the
greeter.sol
file is stored:-
solcjs --bin greeter.sol
As a result you will get a
greeter_sol_Greeter.bin
file. -
You can also find
greeter_sol_Greeter.json
that you will need in further steps.
-