Skip to content

Commit

Permalink
feat: init client with mirror network
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Ivanov <[email protected]>
  • Loading branch information
0xivanov committed Nov 13, 2024
1 parent cfbca81 commit 3589ead
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tasks:
for example in examples/*; do
dir_name=$(basename "$example")
# Skip the consensus_pub_sub_chunked directory
if [ "$dir_name" == "consensus_pub_sub_chunked" ]; then
if [ "$dir_name" == "consensus_pub_sub_chunked" ] || [ "$dir_name" == "initialize-client-with-mirror-node-adress-book" ]; then
echo "Skipping $example"
continue
fi
Expand Down
14 changes: 14 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ var mainnetMirror = []string{"mainnet-public.mirrornode.hedera.com:443"}
var testnetMirror = []string{"testnet.mirrornode.hedera.com:443"}
var previewnetMirror = []string{"previewnet.mirrornode.hedera.com:443"}

// ClientForMirrorNetwork constructs a client given a set of mirror network nodes.
func ClientForMirrorNetwork(mirrorNetwork []string) (*Client, error) {
net := _NewNetwork()
client := _NewClient(net, mirrorNetwork, nil)
addressbook, err := NewAddressBookQuery().
SetFileID(FileIDForAddressBook()).
Execute(client)
if err != nil {
return nil, fmt.Errorf("failed to query address book: %v", err)
}
client.SetNetworkFromAddressBook(addressbook)
return client, nil
}

// ClientForNetwork constructs a client given a set of nodes.
func ClientForNetwork(network map[string]AccountID) *Client {
net := _NewNetwork()
Expand Down
12 changes: 12 additions & 0 deletions client_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,15 @@ func DisabledTestIntegrationClientPingAllBadNetwork(t *testing.T) { // nolint
assert.Equal(t, 1, len(tempClient.GetNetwork()))

}

func TestClientInitWithMirrorNetwork(t *testing.T) {
t.Parallel()
mirrorNetworkString := "testnet.mirrornode.hedera.com:443"
client, err := ClientForMirrorNetwork([]string{mirrorNetworkString})
require.NoError(t, err)

mirrorNetwork := client.GetMirrorNetwork()
assert.Equal(t, 1, len(mirrorNetwork))
assert.Equal(t, mirrorNetworkString, mirrorNetwork[0])
assert.NotEmpty(t, client.GetNetwork())
}
56 changes: 56 additions & 0 deletions examples/initialize-client-with-mirror-node-adress-book/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"fmt"
"os"

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

func main() {
var client *hedera.Client
var err error

// Initialize the client with the testnet mirror node. This will also get the address book from the mirror node and
// use it to populate the Client's consensus network.
client, err = hedera.ClientForMirrorNetwork([]string{"testnet.mirrornode.hedera.com:443"})
if err != nil {
panic(fmt.Sprintf("%v : error creating client", err))
}

// Retrieving operator ID from environment variable OPERATOR_ID
operatorAccountID, err := hedera.AccountIDFromString(os.Getenv("OPERATOR_ID"))
if err != nil {
panic(fmt.Sprintf("%v : error converting string to AccountID", err))
}

// Retrieving operator key from environment variable OPERATOR_KEY
operatorKey, err := hedera.PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
if err != nil {
panic(fmt.Sprintf("%v : error converting string to PrivateKey", err))
}

// Setting the client operator ID and key
client.SetOperator(operatorAccountID, operatorKey)

privateKey, err := hedera.PrivateKeyGenerateEcdsa()
if err != nil {
panic(err)
}
publicKey := privateKey.PublicKey()

txResponse, err := hedera.NewAccountCreateTransaction().
SetInitialBalance(hedera.NewHbar(1)).
SetKey(publicKey).
Execute(client)
if err != nil {
panic(fmt.Sprintf("%v : error executing account create transaction", err))
}

receipt, err := txResponse.GetReceipt(client)
if err != nil {
panic(fmt.Sprintf("%v : error getting receipt", err))
}

fmt.Printf("New account id, %s", receipt.AccountID.String())
}

0 comments on commit 3589ead

Please sign in to comment.