From e3250f6a33a3e59f2ec1f6e38132be03665e3b5d Mon Sep 17 00:00:00 2001 From: Dalmas Ogembo Date: Mon, 16 Oct 2023 03:10:21 +0300 Subject: [PATCH 1/2] Update sncast docs, add a bash script for testing, adding accounts, declaring a contract, deploying the contract and performing a multicall --- src/ch02-12-foundry-cast.md | 160 ++++++++++++++++++++++++++++++++++++ src/ch03-01-transactions.md | 1 + 2 files changed, 161 insertions(+) create mode 100644 src/ch03-01-transactions.md diff --git a/src/ch02-12-foundry-cast.md b/src/ch02-12-foundry-cast.md index 5be1fa3a8..757cb334e 100644 --- a/src/ch02-12-foundry-cast.md +++ b/src/ch02-12-foundry-cast.md @@ -502,3 +502,163 @@ The expected balance, `0x9`, is confirmed. ## Conclusion This guide detailed the use of `sncast`, a robust command-line tool tailored for starknet smart contracts. Its purpose is to make interactions with starknet's smart contracts effortless. Key functionalities include contract deployment, function invocation, and function calling. + + +## SNCAST SCRIPT + +In light of the above documentation, we have also added a script code you can modify and use to your benefit. + +1. Create a `script.sh` file in the root folder of your project. +2. Give proper file permissions to the file + +```sh +chmod +x script.sh +``` + +3. Paste this into the file. + +```sh +#!/usr/bin/bash + +# First, lets delete the open zeppelin accounts json file +file_path="$HOME/.starknet_accounts/starknet_open_zeppelin_accounts.json" +rm -rf $file_path + +# An array of accounts you want to add +accounts_json=$( + cat <&1) +if echo "$testing_result" | grep -q "Failure"; then + echo "Tests failed to pass!!!" + echo "" + snforge + echo " " + echo "Make sure your tests are passing" + echo " " + FAILED_TESTS=true +fi + +if [ "$FAILED_TESTS" != "true" ]; then + echo "Tests passed!!!" + echo " " + echo "Creating account(s)" + + # Step 2: Add a new account(s) + for row in $(echo "${accounts_json}" | jq -c '.[]'); do + # Extract values from JSON + name=$(echo "${row}" | jq -r '.name') + address=$(echo "${row}" | jq -r '.address') + private_key=$(echo "${row}" | jq -r '.private_key') + # Call the sncast command for each account + account_creation_result=$(sncast --url http://localhost:5050/rpc account add --name "$name" --address "$address" --private-key "$private_key" --add-profile 2>&1) + if echo "$account_creation_result" | grep -q "error:"; then + echo "Account $name already exists" + else + echo "Account: $name created successfully." + fi + done + + # Step 3: Build the contract + echo " " + echo "Building the contract" + scarb build + + # Step 4: Declare the contract + echo " " + echo "Declaring the contract..." + declaration_output=$(sncast --profile $PROFILE_NAME --wait declare --contract-name $CONTRACT_NAME 2>&1) + if echo "$declaration_output" | grep -q "error: Class with hash"; then + echo "Class hash already declared" + class_hash=$(echo "$declaration_output" | sed -n 's/.*Class with hash \([^ ]*\).*/\1/p') + CLASS_HASH=$class_hash + else + echo "New class hash declaration" + class_hash=$(echo "$declaration_output" | grep -o 'class_hash: 0x[^ ]*' | sed 's/class_hash: //') + CLASS_HASH=$class_hash + fi + + echo "Class Hash: $CLASS_HASH" + + # Step 5: Deploy the contract + echo " " + echo "Deploying the contract..." + deployment_result=$(sncast --profile $PROFILE_NAME deploy --class-hash "$CLASS_HASH") + CONTRACT_ADDRESS=$(echo "$deployment_result" | grep -o "contract_address: 0x[^ ]*" | awk '{print $2}') + echo "Contract address: $CONTRACT_ADDRESS" + + # Step 6: Perform a multicall + echo " " + echo "Performing a multicall..." + + # Create a multicall .toml file and add some calls there. + MULTICALL_FILE="multicall.toml" + echo "[[call]]" >"$MULTICALL_FILE" + echo "call_type = 'invoke'" >>"$MULTICALL_FILE" + echo "contract_address = '$CONTRACT_ADDRESS'" >>"$MULTICALL_FILE" + echo "function = 'increase_balance'" >>"$MULTICALL_FILE" + echo "inputs = ['0x1']" >>"$MULTICALL_FILE" + + # Create some space between the two calls + echo " " >>"$MULTICALL_FILE" + + echo "[[call]]" >>"$MULTICALL_FILE" + echo "call_type = 'invoke'" >>"$MULTICALL_FILE" + echo "contract_address = '$CONTRACT_ADDRESS'" >>"$MULTICALL_FILE" + echo "function = 'increase_balance'" >>"$MULTICALL_FILE" + echo "inputs = ['0x2']" >>"$MULTICALL_FILE" + + # Run the multicall + sncast --profile $PROFILE_NAME multicall run --path "$MULTICALL_FILE" + + echo " " + + echo "Checking balance" + sncast --profile $PROFILE_NAME call --contract-address $CONTRACT_ADDRESS --function get_balance + echo " " + + # Step 7: Clean up + # Clean up the multicall file by deleting it + [ -e "$MULTICALL_FILE" ] && rm "$MULTICALL_FILE" + + echo "Script completed successfully" + echo " " +fi + +``` + +4. Update the entry point of the script that is the first line by your bash location. To get bash location run and update the line accordingly. + +```sh +which bash +``` + +5. Update the `CONTRACT_NAME` to your contract name + +6. Run the file + +```sh +./script.sh +``` \ No newline at end of file diff --git a/src/ch03-01-transactions.md b/src/ch03-01-transactions.md new file mode 100644 index 000000000..a5fd09c18 --- /dev/null +++ b/src/ch03-01-transactions.md @@ -0,0 +1 @@ +# Transactions From 073a14c406c834ab3567096a8546c9e4ea5f4ddd Mon Sep 17 00:00:00 2001 From: Dalmas Ogembo Date: Mon, 16 Oct 2023 10:16:57 +0300 Subject: [PATCH 2/2] Update ch02-12-foundry-cast.md by running prettier --- src/ch02-12-foundry-cast.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ch02-12-foundry-cast.md b/src/ch02-12-foundry-cast.md index 757cb334e..7f4e2daa8 100644 --- a/src/ch02-12-foundry-cast.md +++ b/src/ch02-12-foundry-cast.md @@ -503,13 +503,12 @@ The expected balance, `0x9`, is confirmed. This guide detailed the use of `sncast`, a robust command-line tool tailored for starknet smart contracts. Its purpose is to make interactions with starknet's smart contracts effortless. Key functionalities include contract deployment, function invocation, and function calling. - ## SNCAST SCRIPT In light of the above documentation, we have also added a script code you can modify and use to your benefit. 1. Create a `script.sh` file in the root folder of your project. -2. Give proper file permissions to the file +2. Give proper file permissions to the file ```sh chmod +x script.sh @@ -657,8 +656,8 @@ which bash 5. Update the `CONTRACT_NAME` to your contract name -6. Run the file +6. Run the file ```sh ./script.sh -``` \ No newline at end of file +```