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 solcNoteYou can find the Solidity compiler description and usage here.
-
Ensure you are in
dcloud_exampledirectory, 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
greetingvariable will be stored:string private greeting; -
Declaration of contract inner variable, where the
greetingvariable will be stored:constructor() {
greeting = 'Hello World!';
}When you create a smart contract, the
greetingvariable 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
.abifile:Run the following command in the directory, where the
greeter.solfile is stored:-
solcjs --bin greeter.solAs a result you will get a
greeter_sol_Greeter.binfile. -
You can also find
greeter_sol_Greeter.jsonthat you will need in further steps.
-