Demo repo for Hello World sequences to build on Hedera, using Javascript.
To follow along, please read the accompanying tutorial at docs.hedera.com.
Note that this demo repo is also available in 3 programming languages:
- Javascript (this repo)
- Java
- Go
You may choose to run this demo repo and follow along the tutorial, either: (a) on your own computer (recommended for experienced developers), or (b) using Gitpod (recommended for quick/ easy setup).
To run on your own computer, git clone
this repo,
and follow the instructions in the "pre-requisites" section of the accompanying tutorial.
- Install all the prerequisite software
- Run
./util/00-main.sh
and this script will interactively prompt you, and populate the values needed in the.env
file - Run
./util/03-get-dependencies.sh
and this script will install the required dependencies - Run
./util/04-rpcrelay-run.sh
and this script will run a Hedera JSON-RPC Relay instance- Note that this requires
docker
to be available on your system - Note that you may delay performing this step until later, you only need it for HSCS related sequences
- Note that this requires
- Congratulations, you can now move on to the sequences! 🎉
To run on Gitpod (a cloud development environment), click the button below:
- Wait for Gitpod to load, this should take less than 10 seconds
- In the VS code terminal, you should see 3 terminals,
rpcrelay_pull
,rpcrelay_run
, andmain
- You do not need to use the
rpcrelay_pull
andrpcrelay_run
terminals, let them run in the background - In the
main
terminal, which is the one that displays by default, a script will interactively prompt you - Congratulations, you can now move on to the sequences! 🎉
This repo contains the code required for all of the Hello World sequences. The following sections outline what each sequence will cover. Each one represents the bare minimum required to use various parts of Hedera technology. Note that each sequence is intended to be completed in under 10 minutes when run via Gitpod.
What you will accomplish:
- Answer interactive prompts in a terminal to construct a
.env
file - Generate accounts using a BIP-39 seed phrase (optional)
- Fund one of those accounts using the Hedera Testnet Faucet (optional)
Video:
Steps:
- Enter private key
- Option 1: Input
none
. This will mean that an account generated from a seed phrase will be used (later). - Option 2: Input any ECDSA sec256k1 private key. You may obtain this from portal.hedera.com/dashboard.
- Option 1: Input
- Enter seed phrase
- Option 1: Input nothing. This will generate a new seed BIP-39 phrase at random.
- Option 2: Input any BIP-39 compliant seed phrase You may generate one using any tool that supports BIP-39. If you would like to do this in the browser, you may use iancoleman.io/bip39
- Enter number of accounts
- Input any whole number greater than or equal to
3
. This tutorial requires at least 3 to be generated from the BIP-39 seed phrase.
- Input any whole number greater than or equal to
- Please ensure that you have funded
- To do so, copy the EVM address in the terminal output (starts with
0x
) - Then visit faucet.hedera.com
- Paste the EVM address into the "Enter Wallet Address" text field
- Press the "Receive…" button
- Pass the reCaptcha ("I'm not a robot")
- Press the "Confirm transaction" button
- Wait till you see the "Transaction successful" model dialog
- Switch back to the script
- There is no input required here, simply hit "Enter" or "Return" after funding the account
- To do so, copy the EVM address in the terminal output (starts with
- Enter JSON-RPC URL
- Input nothing to accept the default value suggested by the script.
- If running the script on your own computer, this value defaults to
https://localhost:7546/
- If running the script on Gitpod, this value defaults to something that matches the patterns
https://7546-*.gitpod.io/
- If running the script on your own computer, this value defaults to
- Input nothing to accept the default value suggested by the script.
- Overwrite?
- Input
y
to update the.env
file
- Input
- Open the
.env
file and check that its contents have been updated
Demonstrates: Use of the Hedera network, at a base level.
What you will accomplish:
- Initialise
Client
using the Hedera SDK, by reading in credentials from the.env
file generated earlier. - Send a
TransferTransaction
using the Hedera SDK.- Note that in Hedera, you can have debits and credits from multiple accounts, as long as their total tallies, and all debiting accounts sign the transaction. This is unlike in the EVM, where you would need a smart contract or some other intermediary to accomplish the same task.
.sign
,.execute
, and.getReceipt
are used to complete transaction.
- Send an
AccountBalanceQuery
using the Hedera SDK.- This obtains the fresh balance from one of the recipient accounts.
- Send an HTTP request to a Mirror Node API to query the transaction
- The response is parsed to obtain the amounts transferred.
- View the transfer transaction page in HashScan (the network explorer)
Video:
Steps:
- In the code editor, Open the file
transfer-hbar/script-transfer-hbar.js
. - In the terminal, run these commands:
cd transfer-hbar
./script-transfer-hbar.js
- View the summary statistics (optional)
- The "time to first task completion" displays how long it took between starting up the project (the setup script) through to completing the first task. Note that this does not include the time taken to set up the repo manually (variable), or for Gitpod to spin up (up to 10 seconds).
- The "time taken to complete" displays how long it took for this particular script to run.
- Open the HCS topic and check logs which match you anonymised key. Note that the anonymised key is simply a randomly generated hexadecimal string.
Demonstrates: Use of the Hedera Token Service (HTS).
What you will accomplish:
- Initialise
Client
using the Hedera SDK, by reading in credentials from the.env
file generated earlier. - Send a
TokenCreateTransaction
using the Hedera SDK.- Note that in Hedera, tokens can be created with out using Solidity or smart contracts. Instead, you can create HTS tokens using only SDK methods. These have equivalent functionality to ERC20 or ERC721 tokens, but can be created with much less effort.
.setTokenType
is used to specify that this should be a fungible token..setTokenName
,.setTokenSymbol
,.setDecimals
, and.setInitialSupply
are used to configure the token..sign
,.execute
, and.getReceipt
are used to complete transaction.
- Send an HTTP request to a Mirror Node API to query the transaction
- The response is parsed to obtain the name and total supply of the token.
- View the token page in HashScan (the network explorer)
Video:
Steps:
- In the code editor, Open the file
hts-ft/script-hts-ft.js
. - In the terminal, run these commands:
cd hts-ft
./script-hts-ft.js
- View the summary statistics (optional)
- The "time to first task completion" displays how long it took between starting up the project (the setup script) through to completing the first task. Note that this does not include the time taken to set up the repo manually (variable), or for Gitpod to spin up (up to 10 seconds).
- The "time taken to complete" displays how long it took for this particular script to run.
- Open the HCS topic and check logs which match you anonymised key. Note that the anonymised key is simply a randomly generated hexadecimal string.
Demonstrates: Use of the Hedera Consensus Service (HCS).
What you will accomplish:
- Initialise
Client
using the Hedera SDK, by reading in credentials from the.env
file generated earlier. - Send a
TopicCreateTransaction
using the Hedera SDK.- This registers a new topic, which may be thought of as a pub-sub messaging service.
.setTransactionMemo
and.setTopicMemo
are used to configure the topic..sign
,.execute
, and.getReceipt
are used to complete transaction.
- Send a
TopicMessageSubmitTransaction
using the Hedera SDK.- This adds a message to the freshly created topic.
.setTransactionMemo
,.setTopicId
, and.setMessage
are used to configure the message..sign
,.execute
, and.getReceipt
are used to complete transaction.
- Send an HTTP request to a Mirror Node API to query the transaction
- The response is parsed to obtain the messages in the topic.
- View the topic page in HashScan (the network explorer)
Video:
Steps:
- In the code editor, Open the file
hcs-topic/script-hcs-topic.js
. - In the terminal, run these commands:
cd hcs-topic
./script-hcs-topic.js
- View the summary statistics (optional)
- The "time to first task completion" displays how long it took between starting up the project (the setup script) through to completing the first task. Note that this does not include the time taken to set up the repo manually (variable), or for Gitpod to spin up (up to 10 seconds).
- The "time taken to complete" displays how long it took for this particular script to run.
- Open the HCS topic and check logs which match you anonymised key. Note that the anonymised key is simply a randomly generated hexadecimal string.
Demonstrates: Use of the Hedera Smart Contract Service (HSCS).
What you will accomplish:
- Observe that the RPC Relay has been set up and running in the background.
- Initialise
Wallet
andRpcConnection
using EthersJs, by reading in credentials from the.env
file generated earlier. - Send an EVM smart contract deployment transaction using
ContractFactory
via EthersJs- The
*.abi
and*.bin
files output by the Solidity compiler (solc
) are used as inputs. .deploy
and.wait
are used to complete the transaction.
- The
- View the smart contract in HashScan (the network explorer)
- Invoke the
introduce
function on the smart contract using.functions.introduce
via EthersJs..wait
is used to complete the transaction.- This transaction updates the state of (data stored in) the smart contract.
- Invoke the
greet
function on the smart contract using.functions.greet
via EthersJs.- There is no transaction involved here, as this accesses the state of (data stored in) the smart contract, without updating it.
Video:
Steps:
- In the code editor, open the files
hscs-smart-contract/script-hscs-smart-contract.js
andhscs-smart-contract/my_contract.sol
. - In the terminal, run these commands:
cd hscs-smart-contract
npm install
npx [email protected] --abi --bin my_contract.sol
./script-hscs-smart-contract.js
- View the summary statistics (optional)
- The "time to first task completion" displays how long it took between starting up the project (the setup script) through to completing the first task. Note that this does not include the time taken to set up the repo manually (variable), or for Gitpod to spin up (up to 10 seconds).
- The "time taken to complete" displays how long it took for this particular script to run.
- Open the HCS topic in HashScan and check logs which match you anonymised key. Note that the anonymised key is simply a randomly generated hexadecimal string.
Video:
Steps:
- If you ran this via Gitpod, be sure to "stop workspace".
- You may do so via the menu within the browser-based IDE.
- Alternatively, visit gitpod.io/workspaces to see a list of all workspaces
- Note that Gitpod has a limited number of hours on their free tier, and you pay for hours on their paid tiers, so best to avoid an idling workspace.
- npm install for HSCS automate
- for hscs add [email protected] as dependency
- make log outputs consistent in the HSCS output
- make order of hashscan URL and then mirror node API check consistent across all 4
- solidity file in HSCS HW - update the constant both var name and var value
- rename
hfwId
toscriptId
consistently - change literal amounts specified in constants in transfer HBAR script
- use existing accounts instead of lazy account creation in transfer HBAR script
- add note not using "anon" for public transactions
- set recipient accounts to 0.0.200 and 0.0.201
- filter list of recipient accounts ('transferJsonAccountTransfersFinalAmounts')
- change one of the transfer amounts from 6.62607015 to 2.0
- calculate and display contract deployment and function call fees
- rename directories to: transfer, hts, hcs, hscs
In upstream base template repo
- investigate inconsistencies between account ID and EVM address -> modify SDK to make mirror node queries?
- added suggestion here: hashgraph/hedera-sdk-js#2442 (comment)
- append a suffix '(latest)' to the one just completed in summary metrics
- 'Enter your operator account (hex encoded ECDSA) private key' plus validation of the key and account type
- refactor to rename 'logSectionWithWaitPrompt'
- log errors and restarts more clearly in dotenv setup script
- add script to run RPC relay via NPM instead of docker
- investigate: docs as code/ SSOT for written tutorial
- Q&A for npm pack and !/.gitignore
MIT