Skip to content

Latest commit

 

History

History
429 lines (330 loc) · 13.3 KB

environment-set-up.md

File metadata and controls

429 lines (330 loc) · 13.3 KB

Environment Setup

Summary

In this section, you will create your new project directory, your .env file to store your, _Hedera Testne_t account ID and private keys and set up your Hedera Testnet client.

Prerequisites:

{% content-ref url="introduction.md" %} introduction.md {% endcontent-ref %}

{% hint style="info" %} Note: You can always check the "Code Check ✅ " section at the bottom of each page to view the entire code if you run into issues. You can also post your issue to the respective SDK channel in our Discord community here or on the GitHub repository here. {% endhint %}

Step 1: Create your project directory

Open your IDE of choice and follow the below steps to create your new project directory.

{% tabs %} {% tab title="Java" %} Create a new Gradle project and name it HederaExamples. Add the following dependencies to your build.gradle file.

{% code title="build.gradle " %}

dependencies {

    implementation 'com.hedera.hashgraph:sdk:2.19.0'
    implementation 'io.grpc:grpc-netty-shaded:1.46.0'
    implementation 'io.github.cdimascio:dotenv-java:2.3.2'
    implementation 'org.slf4j:slf4j-nop:2.0.3'
    implementation 'com.google.code.gson:gson:2.8.8'

}

{% endcode %} {% endtab %}

{% tab title="JavaScript" %} Open your terminal and create a directory called hello-hedera-js-sdk. After you create the project directory navigate to the directory by running the following command:

mkdir hello-hedera-js-sdk && cd hello-hedera-js-sdk

Initialize a node.js project in this new directory by running the following command:

npm init -y

This is what your console should look like after running the command:

{
  "name": "hello-hedera-js-sdk",
  "version": "",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

{% endtab %}

{% tab title="Go" %} Open your terminal and create a project directory called something like hedera-go-examples to store your Go source code.

mkdir hedera-go-examples && cd hedera-go-examples

{% endtab %} {% endtabs %}

Step 2: Install Dependencies and SDKs

{% tabs %} {% tab title="Java" %} {% hint style="info" %} Note: You may choose to install the latest version of the SDK here. {% endhint %}

Create a new Java class and name it something like HederaExamples. Import the following classes to use in your example:

import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.Client;
import com.hedera.hashgraph.sdk.PrivateKey;
import io.github.cdimascio.dotenv.Dotenv;

Within the main method, grab your testnet account ID and __ private key __ from the .env file.

public class HederaExamples {

    public static void main(String[] args) {

        //Grab your Hedera Testnet account ID and private key
        AccountId myAccountId = AccountId.fromString(Dotenv.load().get("MY_ACCOUNT_ID"));
        PrivateKey myPrivateKey = PrivateKey.fromString(Dotenv.load().get("MY_PRIVATE_KEY"));  
    }
}

{% endtab %}

{% tab title="JavaScript" %} Open this project in your favorite IDE/text editor like Visual Studio Code.

Install the JavaScript SDK with your favorite package manager npm or yarn by running the following command:

// install Hedera's JS SDK with NPM
npm install --save @hashgraph/sdk

// Install with Yarn
yarn add @hashgraph/sdk

Install dotenv with your favorite package manager. This will allow our node environment to use your testnet account ID and the private key that we will store in a .env file next.

// install with NPM
npm install dotenv

// Install with Yarn
yarn add dotenv

Navigate to the project root directory and create a index.js file by running the following command:

touch index.js

Your project structure should look something like this:

Grab your Hedera Testnet account ID and private key from the .env file.

{% code title="index.js" %}

const { Client } = require("@hashgraph/sdk");
require("dotenv").config();

async function main() {

    //Grab your Hedera testnet account ID and private key from your .env file
    const myAccountId = process.env.MY_ACCOUNT_ID;
    const myPrivateKey = process.env.MY_PRIVATE_KEY;

    // If we weren't able to grab it, we should throw a new error
    if (!myAccountId || !myPrivateKey) {
        throw new Error("Environment variables MY_ACCOUNT_ID and MY_PRIVATE_KEY must be present");
    }
}
main();

{% endcode %} {% endtab %}

{% tab title="Go" %} Create a hedera_examples.go file in hedera-go-examples directory. You will write all of your code in this file.

{% hint style="info" %} Note: Install the Go SDK here and the DotEnv package here before moving forward. {% endhint %}

Import the following packages to your hedera_examples.go file:

package main

import (
    "fmt"
    "os"

    "github.com/joho/godotenv"
    "github.com/hashgraph/hedera-sdk-go/v2"
)

Next, you will load your account ID and private key variables from the .env file created in the previous step.

package main

import (
    "fmt"
    "os"

    "github.com/joho/godotenv"
    "github.com/hashgraph/hedera-sdk-go/v2"
)

func main() {

    //Loads the .env file and throws an error if it cannot load the variables from that file correctly
    err := godotenv.Load(".env")
    if err != nil {
        panic(fmt.Errorf("Unable to load environment variables from .env file. Error:\n%v\n", err))
    }

    //Grab your testnet account ID and private key from the .env file
    myAccountId, err := hedera.AccountIDFromString(os.Getenv("MY_ACCOUNT_ID"))
    if err != nil {
        panic(err)
    }

    myPrivateKey, err := hedera.PrivateKeyFromString(os.Getenv("MY_PRIVATE_KEY"))
    if err != nil {
        panic(err)
    }

    //Print your testnet account ID and private key to the console to make sure there was no error
    fmt.Printf("The account ID is = %v\n", myAccountId)
    fmt.Printf("The private key is = %v\n", myPrivateKey)
}

In your terminal, enter the following command to create your go.mod file. This module is used for tracking dependencies and is required.

go mod init hedera_examples.go

Run your code to see your testnet account ID and private key printed to the console.

go run hedera_examples.go

{% endtab %} {% endtabs %}

Step 3: Create your .env File

The .env file stores your environment variables, account ID and private key. Create the file in your project's root directory.

{% hint style="info" %} Note: Testnet HBAR is required for this next step. Please follow the instructions to create a Hedera account on the portal before you move on to the next step. {% endhint %}

Hedera developer portal dashboard with your account ID and public/private keys.

This is a screenshot of the portal dashboard where your account ID and private keys are.

Grab the Hedera Testnet account ID and private key from your Hedera portal profile(see screenshot above) and assign them to the MY_ACCOUNT_ID and MY_PRIVATE_KEY environment variables in your .env file:

{% tabs %} {% tab title="Java" %}

MY_ACCOUNT_ID=ENTER TESTNET ACCOUNT ID 
MY_PRIVATE_KEY=ENTER TESTNET PRIVATE KEY

{% endtab %}

{% tab title="JavaScript" %}

MY_ACCOUNT_ID=ENTER TESTNET ACCOUNT ID 
MY_PRIVATE_KEY=ENTER TESTNET PRIVATE KEY

{% endtab %}

{% tab title="Go" %}

MY_ACCOUNT_ID=ENTER TESTNET ACCOUNT ID 
MY_PRIVATE_KEY=ENTER TESTNET PRIVATE KEY

{% endtab %} {% endtabs %}

Step 4: Create your Hedera Testnet client

Create a Hedera Testnet client and set the operator information using the testnet account ID and private key for transaction and query fee authorization. The operator is the default account that will pay for the transaction and query fees in HBAR. You will need to sign the transaction or query with the private key of that account to authorize the payment. In this case, the operator ID is your testnet account ID and the operator private key is the corresponding testnet account private key.

{% tabs %} {% tab title="Java" %}

//Create your Hedera Testnet client
Client client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);

{% hint style="info" %} Note: The client has a default max transaction fee of 100,000,000 tinybars (1 HBAR) and default max query payment of 100,000,000 tinybars (1 HBAR). If you need to change these values, you can use.setDefaultMaxTransactionFee() for a transaction and .setDefaultMaxQueryPayment() for queries. You are only charged the actual cost of the transaction or query. {% endhint %} {% endtab %}

{% tab title="JavaScript" %} {% code title="index.js" %}

// Create our connection to the Hedera network
// The Hedera JS SDK makes this really easy!
const client = Client.forTestnet();

client.setOperator(myAccountId, myPrivateKey);

{% endcode %} {% endtab %}

{% tab title="Go" %}

//Create your testnet client
client := hedera.ClientForTestnet()
client.SetOperator(myAccountId, myPrivateKey)

{% hint style="info" %} Note: The client has a default max transaction fee of 100,000,000 tinybars (1 HBAR) and default max query payment of 100,000,000 tinybars (1 HBAR). If you need to change these values, you can use.setDefaultMaxTransactionFee() for transactions and .setDefaultMaxQueryPayment() for queries. You are only charged the actual cost of the transaction or query. {% endhint %} {% endtab %} {% endtabs %}

{% hint style="success" %} Your project environment is now set up to successfully submit transactions and queries to the Hedera test network!

Next, you will learn how to create an account. {% endhint %}

Code Check

This is what your code should look like:

{% tabs %} {% tab title="Java" %} {% code title="HederaExamples.java" %}

import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.Client;
import com.hedera.hashgraph.sdk.PrivateKey;
import io.github.cdimascio.dotenv.Dotenv;
​
public class HederaExamples {
​
    public static void main(String[] args) {
​
        //Grab your Hedera Testnet account ID and private key
        AccountId myAccountId = AccountId.fromString(Dotenv.load().get("MY_ACCOUNT_ID"));+
        PrivateKey myPrivateKey = PrivateKey.fromString(Dotenv.load().get("MY_PRIVATE_KEY"));
​
        //Create your Hedera Testnet client
        Client client = Client.forTestnet();
        client.setOperator(myAccountId, myPrivateKey);
​
    }
}

{% endcode %} {% endtab %}

{% tab title="JavaScript" %} {% code title="index.js" %}

const { Client } = require("@hashgraph/sdk");
require("dotenv").config();

async function main() {

    //Grab your Hedera testnet account ID and private key from your .env file
    const myAccountId = process.env.MY_ACCOUNT_ID;
    const myPrivateKey = process.env.MY_PRIVATE_KEY;

    // If we weren't able to grab it, we should throw a new error
    if (!myAccountId || !myPrivateKey) {
        throw new Error("Environment variables MY_ACCOUNT_ID and MY_PRIVATE_KEY must be present");
    }

    // Create our connection to the Hedera network
    // The Hedera JS SDK makes this really easy!
    const client = Client.forTestnet();
    client.setOperator(myAccountId, myPrivateKey);

}
main().catch(err => console.error(err));

{% endcode %} {% endtab %}

{% tab title="Go" %} {% code title="hedera_examples.go" %}

package main

import (
    "fmt"
    "os"

    "github.com/hashgraph/hedera-sdk-go/v2"
    "github.com/joho/godotenv"
)

func main() {

    //Loads the .env file and throws an error if it cannot load the variables from that file correctly
    err := godotenv.Load(".env")
    if err != nil {
        panic(fmt.Errorf("Unable to load environment variables from .env file. Error:\n%v\n", err))
    }

    //Grab your testnet account ID and private key from the .env file
    myAccountId, err := hedera.AccountIDFromString(os.Getenv("MY_ACCOUNT_ID"))
    if err != nil {
        panic(err)
    }

    myPrivateKey, err := hedera.PrivateKeyFromString(os.Getenv("MY_PRIVATE_KEY"))
    if err != nil {
        panic(err)
    }

    //Print your testnet account ID and private key to the console to make sure there was no error
    fmt.Printf("The account ID is = %v\n", myAccountId)
    fmt.Printf("The private key is = %v\n", myPrivateKey)

    //Create your testnet client
    client := hedera.ClientForTestnet()
    client.SetOperator(myAccountId, myPrivateKey)
}

{% endcode %} {% endtab %} {% endtabs %}

{% hint style="info" %} Have a question? Ask it on StackOverflow {% endhint %}