Smart Contract
This section covers writing a custom smart contract and deploying it on harmony testnet.

Writing our first Counter Smart Contract

In the contracts folder create a new file Counter.sol and add the following code block to it.
1
pragma solidity >=0.4.22 <0.8.0;
2
3
contract Counter {
4
uint256 private count = 0;
5
uint256 moneyStored = 0;
6
7
function incrementCounter() public {
8
count += 1;
9
}
10
function decrementCounter() public {
11
count -= 1;
12
}
13
14
function addMoney() payable public {
15
moneyStored += msg.value;
16
}
17
18
function getCount() public view returns (uint256) {
19
return count;
20
}
21
22
function getMoneyStored() public view returns (uint256){
23
return moneyStored;
24
}
25
}
Copied!
In the migrations folder create another new file called 2_Counter.js and add the following code block.
1
const Migrations = artifacts.require("Counter");
2
3
module.exports = function (deployer) {
4
deployer.deploy(Migrations);
5
};
Copied!

Checking the solidity smart contract

1
truffle compile
Copied!
If all done correctly you should see something like this.
1
Compiling your contracts...
2
===========================
3
> Compiling ./contracts/Counter.sol
4
> Compiling ./contracts/Migrations.sol
5
> Compilation warnings encountered:
6
7
/home/rachit/Projects/demo/contracts/Counter.sol: Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
8
9
> Artifacts written to /home/rachit/Projects/demo/build/contracts
10
> Compiled successfully using:
11
- solc: 0.7.0+commit.9e61f92b.Emscripten.clang
Copied!

Deployment

1
truffle migrate --network testnet --reset
Copied!
Last modified 1yr ago