Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add example for using javascript SDK #832

Merged
merged 45 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
db835ea
add test sdk client
JeffreyDallas Nov 12, 2024
b37203a
save
JeffreyDallas Nov 12, 2024
f2fca67
Fix account test
JeffreyDallas Nov 12, 2024
c78dbb8
add steps to launch solo network
JeffreyDallas Nov 13, 2024
8599657
fix relay test
JeffreyDallas Nov 13, 2024
00eef83
fix change background traffic without using watch command
JeffreyDallas Nov 13, 2024
84d52bd
change local node version
JeffreyDallas Nov 13, 2024
4370b50
add step for using sdk sample
JeffreyDallas Nov 14, 2024
9169373
update relay test flow
JeffreyDallas Nov 14, 2024
8f79b5b
add clean port forward
JeffreyDallas Nov 14, 2024
d6cc4d4
add sdk test
JeffreyDallas Nov 14, 2024
75474db
change script name
JeffreyDallas Nov 14, 2024
6ed3be0
check port forwarding
JeffreyDallas Nov 14, 2024
3bb7aaa
fix task command
JeffreyDallas Nov 15, 2024
ca48359
fix namespace
JeffreyDallas Nov 15, 2024
ed60444
add test sleep
JeffreyDallas Nov 15, 2024
3986031
save
JeffreyDallas Nov 15, 2024
0086fd5
nmp install sdk
JeffreyDallas Nov 15, 2024
fab9f99
add ps ef for debug
JeffreyDallas Nov 15, 2024
09e45eb
capture error
JeffreyDallas Nov 15, 2024
16f7d0b
force port forward
JeffreyDallas Nov 15, 2024
63aec6c
run generate key in a loop
JeffreyDallas Nov 16, 2024
2b14cfa
fix
JeffreyDallas Nov 16, 2024
b9dad57
save
JeffreyDallas Nov 16, 2024
3fcf5c3
save
JeffreyDallas Nov 16, 2024
924b88b
add timeout
JeffreyDallas Nov 16, 2024
c3228f3
run longer
JeffreyDallas Nov 16, 2024
aa67662
update sleep time
JeffreyDallas Nov 16, 2024
efd3957
change round number
JeffreyDallas Nov 18, 2024
a4e1fa2
update retry function
JeffreyDallas Nov 18, 2024
32e863e
set 20 minutes timeout
JeffreyDallas Nov 18, 2024
508a89d
add sleep period
JeffreyDallas Nov 18, 2024
fbfcff6
update task name
JeffreyDallas Nov 18, 2024
efe8846
update readme and smoke test
JeffreyDallas Nov 18, 2024
8bc99c9
use helper taskfile.yml
jeromy-cannon Nov 18, 2024
4c3526c
empty readme
jeromy-cannon Nov 18, 2024
0964f5a
starting a solo network connection ts example
jeromy-cannon Nov 18, 2024
22e34f0
create helper file
jeromy-cannon Nov 18, 2024
9f530e1
fixed bugs
jeromy-cannon Nov 19, 2024
da5daa5
Fix private key extraction
JeffreyDallas Nov 19, 2024
96d06c6
add solo network connection example
jeromy-cannon Nov 19, 2024
cc19979
Merge commit '7fa86ad19c66d2ea98d4d10b0400b1635499c322' into 00816-D-…
JeffreyDallas Nov 21, 2024
2b2406b
Merge branch 'main' into 00816-D-client-doc-example
JeffreyDallas Nov 22, 2024
6d53ab1
revert
JeffreyDallas Nov 22, 2024
f01c890
remove smoke test
JeffreyDallas Nov 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions .github/workflows/flow-task-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ name: "Test Taskfile Commands"
# The purpose of this reusable workflow is to make sure task files are working as expected.

on:
workflow_dispatch:
workflow_call:
pull_request:
types:
Expand All @@ -35,6 +36,7 @@ permissions:

jobs:
example-task-file-test:
timeout-minutes: 20
runs-on: solo-linux-large
steps:
- name: Harden Runner
Expand All @@ -51,6 +53,16 @@ jobs:
node-version: 20
cache: npm

- name: Setup Kind
uses: helm/kind-action@0025e74a8c7512023d06dc019c617aa3cf561fde # v1.10.0
with:
install_only: true
node_image: kindest/node:v1.27.3@sha256:3966ac761ae0136263ffdb6cfd4db23ef8a83cba8a463690e98317add2c9ba72
version: v0.21.0
kubectl_version: v1.28.6
verbosity: 3
wait: 120s

- name: Install Dependencies
id: npm-deps
run: npm ci
Expand All @@ -60,6 +72,7 @@ jobs:

- name: Run Example Task File Test
run: |
task readme
task default-with-mirror
task default-with-relay
sleep 15
.github/workflows/script/solo_smoke_test.sh
task clean
82 changes: 0 additions & 82 deletions .github/workflows/script/relay_smoke_test.sh
jeromy-cannon marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

228 changes: 228 additions & 0 deletions .github/workflows/script/solo_smoke_test.sh
jeromy-cannon marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
#!/bin/bash
set -eo pipefail

#
# This script should be called after solo has been deployed with mirror node and relay node deployed,
# and should be called from the root of the solo repository
#
# This uses local node account creation function to repeatedly generate background transactions
# Then run smart contract test, and also javascript sdk sample test to interact with solo network
#

function_name=""

function enable_port_forward ()
{
kubectl port-forward -n solo-e2e svc/haproxy-node1-svc 50211:50211 > /dev/null 2>&1 &
kubectl port-forward -n solo-e2e svc/hedera-explorer 8080:80 > /dev/null 2>&1 &
kubectl port-forward -n solo-e2e svc/relay-node1-hedera-json-rpc-relay 7546:7546 > /dev/null 2>&1 &
}

# calling local node generate-accounts function and extract private key
# from the output, after some manipulation, add the private key to hardhat.config.js
function create_account_and_extract_key ()
jeromy-cannon marked this conversation as resolved.
Show resolved Hide resolved
{
echo "Generate ECDSA keys, extract from output and save to key.txt"
# remove previous generate private key files
rm -rf "private_key*.txt"
npm run generate-accounts 3 > key.log
sed -n 's/.* - \(0x[0-9a-f]*\) - \(0x[0-9a-f]*\) - .*/\1 \2/p' key.log > key.txt

echo "Only keep the private key, the second column of each line of file key.txt"
awk '{print "\"" $2 "\","}' key.txt > private_key_with_quote.txt
awk '{print "" $2 ","}' key.txt > private_key_without_quote.txt

echo "Remove the comma of the last line before add to json file"
sed '$ s/.$//' private_key_with_quote.txt > private_key_with_quote_final.txt
sed '$ s/.$//' private_key_without_quote.txt > private_key_without_quote_final.txt

LOCAL_NODE_KEYS=$(cat private_key_with_quote_final.txt)

echo "Add new keys to hardhat.config.js"
git checkout test/smoke/hardhat.config.js
awk '/accounts: \[/ {print; getline; getline; next} 1' test/smoke/hardhat.config.js > test/smoke/hardhat.config.js.tmp

if [[ "$OSTYPE" == "darwin"* ]]; then
echo "$LOCAL_NODE_KEYS" > temp.txt
sed '/accounts: \[/r temp.txt' test/smoke/hardhat.config.js.tmp > test/smoke/hardhat.config.js
rm temp.txt
else
awk -v new_keys="$LOCAL_NODE_KEYS" '/accounts: \[/ {print; print new_keys; next} 1' test/smoke/hardhat.config.js.tmp > test/smoke/hardhat.config.js || true
fi
echo "Display the new hardhat.config.js"
cat test/smoke/hardhat.config.js

if [ -s "private_key_without_quote_final.txt" ]; then
echo "File private_key_without_quote_final.txt exists and not empty"
return 0
else
echo "File private_key_without_quote_final.txt does not exist or empty"
return 1
fi
}

function clone_smart_contract_repo ()
{
jeromy-cannon marked this conversation as resolved.
Show resolved Hide resolved
echo "Clone hedera-smart-contracts"
if [ -d "hedera-smart-contracts" ]; then
echo "Directory hedera-smart-contracts exists."
else
echo "Directory hedera-smart-contracts does not exist."
git clone https://github.com/hashgraph/hedera-smart-contracts --branch only-erc20-tests
fi
}

function clone_sdk_repo ()
{
jeromy-cannon marked this conversation as resolved.
Show resolved Hide resolved
echo "Clone hedera-sdk-js"
if [ -d "hedera-sdk-js" ]; then
echo "Directory hedera-sdk-js exists."
else
echo "Directory hedera-sdk-js does not exist."
git clone https://github.com/hashgraph/hedera-sdk-js --branch v2.53.0
cd hedera-sdk-js
npm install --save @hashgraph/sdk
cd -
fi
}

# clone hedera local node repo and call function create_account_and_extract_key
# to extract private keys
function clone_local_node_repo ()
{
jeromy-cannon marked this conversation as resolved.
Show resolved Hide resolved
echo "Clone hedera local node"
if [ -d "hedera-local-node" ]; then
echo "Directory hedera-local-node exists."
else
echo "Directory hedera-local-node does not exist."
git clone https://github.com/hashgraph/hedera-local-node --branch v2.32.0
fi
cd hedera-local-node
npm install

function_name=create_account_and_extract_key
retry_function 5
cd -
}

# retry the function saved in function_name for few times
function retry_function ()
{
local num=$1
for ((i=1; i<=$num; i++)); do

Check notice on line 112 in .github/workflows/script/solo_smoke_test.sh

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

.github/workflows/script/solo_smoke_test.sh#L112

$/${} is unnecessary on arithmetic variables.
return_code=$( ( $function_name > retry.log; echo $? ))
# Use the return code
if [[ $return_code -eq 0 ]]; then
echo "Function $function_name is successful"
return
else
echo "Function $function_name failed with return code $return_code"
fi
echo "Retry $function_name in 2 seconds"
sleep 2
done
echo "Function $function_name failed after 5 retries"
exit 1
}

function setup_smart_contract_test ()
{
echo "Setup smart contract test"
CONTRACT_TEST_KEYS=$(cat hedera-local-node/private_key_without_quote_final.txt)

cd hedera-smart-contracts
jeromy-cannon marked this conversation as resolved.
Show resolved Hide resolved

npm install
npx hardhat compile

echo "Remove previous .env file"
rm -f .env

echo "Build .env file"

echo "PRIVATE_KEYS=\"$CONTRACT_TEST_KEYS\"" > .env
echo "RETRY_DELAY=5000 # ms" >> .env
echo "MAX_RETRY=5" >> .env
cat .env
cd -
}

function background_keep_port_forward ()
{
for i in {1..40}; do
echo "Enable port forward round $i"
enable_port_forward
sleep 2
ps -ef |grep port-forward
done &
}

function start_background_transactions ()
{
echo "Start background transaction"
# generate accounts every 3 seconds as background traffic for two minutes
# so record stream files can be kept pushing to mirror node
cd hedera-local-node
jeromy-cannon marked this conversation as resolved.
Show resolved Hide resolved
for i in {1..20}; do
echo "Running background transactions round $i"
npm run generate-accounts 3 > background.log 2>&1
sleep 1
done &
cd -
}

function start_contract_test ()
{
cd hedera-smart-contracts
echo "Wait a few seconds for background transactions to start"
sleep 5
echo "Run smart contract test"
npm run hh:test
jeromy-cannon marked this conversation as resolved.
Show resolved Hide resolved
result=$?

cd -
return $result
}

function retry_contract_test ()
{
function_name="start_contract_test"
retry_function 5
}

function start_sdk_test ()
{
echo "Create test account with solo network"
cd solo

# create new account and extract account id
npm run solo-test -- account create -n solo-e2e --hbar-amount 100 > test.log
export OPERATOR_ID=$(grep "accountId" test.log | awk '{print $2}' | sed 's/"//g'| sed 's/,//g')

# get private key of the account
npm run solo-test -- account get -n solo-e2e --account-id ${OPERATOR_ID} --private-key > test.log

Check warning on line 203 in .github/workflows/script/solo_smoke_test.sh

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

.github/workflows/script/solo_smoke_test.sh#L203

Double quote to prevent globbing and word splitting.
jeromy-cannon marked this conversation as resolved.
Show resolved Hide resolved
export OPERATOR_KEY=$(grep "privateKey" test.log | awk '{print $2}' | sed 's/"//g'| sed 's/,//g')

export HEDERA_NETWORK="local-node"

echo "OPERATOR_ID=${OPERATOR_ID}"
echo "OPERATOR_KEY=${OPERATOR_KEY}"
echo "HEDERA_NETWORK=${HEDERA_NETWORK}"

rm test.log

cd ../hedera-sdk-js
node examples/create-topic.js
cd -
}

echo "Change to parent directory"
cd ../
clone_sdk_repo
background_keep_port_forward
clone_local_node_repo
clone_smart_contract_repo
setup_smart_contract_test
start_background_transactions
retry_contract_test
start_sdk_test
7 changes: 1 addition & 6 deletions .github/workflows/zxc-e2e-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ name: "ZXC: E2E Test"
# - .github/workflows/flow-build-application.yaml

on:
workflow_dispatch:
workflow_call:
inputs:
node-version:
Expand Down Expand Up @@ -181,12 +182,6 @@ jobs:
run: |
${{ env.CG_EXEC }} npm run ${{ inputs.npm-test-script }}

- name: RPC relay smoke test
if: ${{ runner.os == 'linux' && inputs.npm-test-script == 'test-e2e-relay' && !cancelled() && !failure() }}
run: |
echo "Skipped smoke test for relay"
#.github/workflows/script/relay_smoke_test.sh

- name: Upload E2E Logs to GitHub
if: ${{ !cancelled() }}
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
Expand Down
Loading
Loading