Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
📝 edit starkli and connection example
Browse files Browse the repository at this point in the history
  • Loading branch information
omarespejel committed Nov 21, 2023
1 parent b1e5f30 commit 87e07a9
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 121 deletions.
79 changes: 79 additions & 0 deletions src/ch02-11-01-connection-script.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Example - Starknet Connection Script

This section provides step-by-step instructions to create and run custom bash scripts for Starknet interactions.

## Katana Local Node

**Description:** This script connects to the local StarkNet devnet through Katana, retrieves the current chain ID, the latest block number, and the balance of a specified account.

First, ensure that Katana is running (in terminal 1):

```bash
katana
```

Then, create a file named `script_devnet` (in terminal 2):

```bash
touch script_devnet
```

Edit this file with your preferred text editor and insert the following script:

```bash
#!/bin/bash
chain=$(starkli chain-id --rpc http://0.0.0.0:5050)
echo "Connected to the Starknet local devnet with chain id: $chain"

block=$(starkli block-number --rpc http://0.0.0.0:5050)
echo "The latest block number on Katana is: $block"

account1="0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
balance=$(starkli balance $account1 --rpc http://0.0.0.0:5050)
echo "The balance of account $account1 is: $balance ETH"
```

Execute the script with:

```bash
bash script_devnet
```

You will see output details from the devnet.

## Goerli Testnet

**Description**: This script connects to the Goerli testnet, reads the latest block number, and retrieves the transaction receipt for a specific transaction hash.

For Goerli testnet interactions, create a file named `script_testnet`:

```bash
touch script_testnet
```

Edit the file and paste in this script:

```bash
echo "Input your testnet API URL: "
read url
chain=$(starkli chain-id --rpc $url)
echo "Connected to the Starknet testnet with chain id: $chain"

block=$(starkli block-number --rpc $url)
echo "The latest block number on Goerli is: $block"

echo "Input your transaction hash: "
read hash
receipt=$(starkli receipt $hash --rpc $url)
echo "The receipt of transaction $hash is: $receipt"
```

Run the script:

```bash
bash script_testnet
```

You will need to input a `testnet API URL` and a `transaction hash`. Example hash: 0x2dd73eb1802aef84e8d73334ce0e5856b18df6626fe1a67bb247fcaaccaac8c.

These are brief examples but you get the idea. You can create custom Bash scripts to customize your interactions with Starknet.
177 changes: 56 additions & 121 deletions src/ch02-11-starkli.md
Original file line number Diff line number Diff line change
@@ -1,77 +1,87 @@
# Starkli: A CLI interface 🚧
# Starkli: Querying Starknet

[Starkli](https://book.starkli.rs/) is a CLI tool for interacting with Starknet, powered by [starknet-rs](https://github.com/xJonathanLEI/starknet-rs).
Starkli allows you to query Starknet and make transactions with ease. It's fast, easy to install, and intuitive to use.
[Starkli](https://book.starkli.rs/) is a Command Line Interface (CLI) tool designed for Starknet interaction, utilizing the capabilities of [starknet-rs](https://github.com/xJonathanLEI/starknet-rs). This tool simplifies querying and executing transactions on Starknet.

## Basic setup
> NOTE: Before continuing with this chapter, make sure you have completed the Basic Installation subchapter of Chapter 2. This includes the installation of Starkli.
Ensure the following commands run successfully on your system. If not, see the [Basic Installation](ch02-01-basic-installation.md) section:
In the next subchapter we will create a short Bash script using Starkli to query Starknet. It's just an example, however, creating your own Bash scripts to interact with Starknet would be very useful in practice.

## Basic Setup

To ensure a smooth start with Starkli, execute the following command on your system. If you encounter any issues, refer to the [Basic Installation](ch02-01-basic-installation.md) guide for assistance:

```bash
starkli --version # To interact with Starknet
starkli --version # Verifies Starkli installation and interacts with Starknet
```

## Connect to Starknet with Providers

Starkli is centric around JSON-RPC provider. There are a few options to obtain access to a JSON-RPC endpoint:
Starkli primarily operates with a JSON-RPC provider. To access a JSON-RPC endpoint, you have several options:

- For ease of access, consider using a provider such as
[Infura](https://docs.infura.io/networks/starknet/how-to) or
[Alchemy](https://www.alchemy.com/starknet) to get an RPC client.
- For development and testing, a temporary local node such as `katana` can be
used.
- Use services like [Infura](https://docs.infura.io/networks/starknet/how-to) or [Alchemy](https://www.alchemy.com/starknet) for an RPC client.
- Employ a temporary local node like `katana` for development and testing purposes.
- Setup your own node.

### Interact with Katana
### Interacting with Katana

Launch Katana. In a terminal, run:
To start Katana, open a terminal and execute:

```bash
katana
katana
```
Let's retrieve the chain id. To accomplish this we have to pass the URL to the katana JSON-RPC endpoint via the --rpc <URL>. For example, in a new terminal:

To retrieve the chain id from the Katana JSON-RPC endpoint, use the following command:

```bash
starkli chain-id --rpc http://0.0.0.0:5050
starkli chain-id --rpc http://0.0.0.0:5050
```
You will get the following output

This command will output:

```bash
0x4b4154414e41 (KATANA)
0x4b4154414e41 (KATANA)
```

Now let's retrieve the latest block number on Katana.
To obtain the latest block number on Katana, run:

```bash
starkli block-number --rpc http://0.0.0.0:5050
```

You will get the following output
The output will be:

```bash
0
```

Since katana is a temporary local node and its state is ephemeral, the block number is initially 0. Refer to [Introduction to Starkli, Scarb and Katana](ch02-02-starkli-scarb-katana.md) for further details on changing the state of Katana and observing the block number after commands like starkli declare and starkli deploy.

Remember, `katana` is a temporary local node, Katana state right now is ephemeral. We have not changed the state of katana yet, thus, it makes sense the block-number is 0. Let's recap our previous example [Introduction to Starkli, Scarb and Katana](ch02-02-starkli-scarb-katana.md) and retrieve the block number after we input `starkli declare` and `starkli deploy`

Again, to declare your contract, execute:
To declare a contract, execute:

```bash
starkli declare target/dev/my_contract_hello.contract_class.json
starkli declare target/dev/my_contract_hello.contract_class.json
```

After declaring, the output will be:

```bash
Class hash declared: 0x00bfb49ff80fd7ef5e84662d6d256d49daf75e0c5bd279b20a786f058ca21418
Class hash declared: 0x00bfb49ff80fd7ef5e84662d6d256d49daf75e0c5bd279b20a786f058ca21418
```

Now let's retrieve the latest block number on Katana.
Retrieving the latest block number on Katana again:

```bash
starkli block-number
starkli block-number
```

Will result in:

```bash
1
1
```
You aso can check the logs on Katana

Katana logs also reflect these changes:

```bash
2023-11-03T04:38:48.712332Z DEBUG server: method="starknet_chainId"
2023-11-03T04:38:48.725133Z DEBUG server: method="starknet_getClass"
Expand All @@ -85,121 +95,46 @@ You aso can check the logs on Katana
2023-11-03T04:38:48.782112Z TRACE executor: Event emitted keys=[0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9]
2023-11-03T04:38:48.782399Z INFO backend: ⛏️ Block 1 mined with 1 transactions
```
We can see a transaction has been received, we used gas and a new block has been mined. That's why the latest block number is `1`.

Before to deploy, we have to mention that Starkli supports argument resolution, the process of expanding simple, human-readable input into actual field element arguments expected by the network. Remember the constructor inputs are in felt format, but argument resolution makes argument input easier, we can pass `str:<String-value>`
These logs indicate the receipt of a transaction, gas usage, and the mining of a new block, explaining the increment in block number to `1`.

Before deploying a contract, note that Starkli supports argument resolution, simplifying the input process. For instance, constructor inputs in felt format can be easily passed as `str:<String-value>`:

```bash
starkli deploy \
0x00bfb49ff80fd7ef5e84662d6d256d49daf75e0c5bd279b20a786f058ca21418 \
str:starknet-book
```
After this we mined a new block, we deployed our contract and we did not use `to-cairo-string` this time.

### Interact with Testnet
This command deploys the contract without requiring `to-cairo-string`, and a new block is mined as a result.

For this, we have to use a third-party JSON-RPC API provider like Infura, Alchemy. Once you have a URL, you can input the following command:
```bash
starkli block-number --rpc https://starknet-goerli.g.alchemy.com/v2/V0WI...
```
You will get the something like this

```bash
896360
```

At the same time you can verify this result on [Starkscan](https://testnet.starkscan.co/). And You will realize they are the same
### Interacting with Testnet

Starkli also offers simplified invoke commands. For example, we can transfer 1000 Wei of the ETH token to the address 0x1234. Before to do this, make sure you to setup your environment variables.
To interact with the Testnet, use a third-party JSON-RPC API provider like Infura or Alchemy. With your provider URL, execute the following command to get the latest block number:

```bash
export STARKNET_ACCOUNT=~/.starkli-wallets/deployer/my_account_1.json
export STARKNET_KEYSTORE=~/.starkli-wallets/deployer/my_keystore_1.json
starkli block-number --rpc https://starknet-goerli.g.alchemy.com/v2/V0WI...
```

Starkli makes this process a lot easier, you have to input the following command
This command will return a response like:

```bash
starkli invoke eth transfer 0x1234 u256:1000
896360
```

Make sure the `0x1234` address this different than your account in the `my_account_1.json` file
You can confirm this result by checking [Starkscan](https://testnet.starkscan.co/), where you'll find matching data.

### Execute your own bash file
Starkli also streamlines the process of invoking commands. For instance, to transfer 1000 Wei of ETH to address 0x1234, first set up your environment variables:

In this section, we are going to execute a bash file in order to connect with the blockchain

#### Katana

Make sure katana is running. We are going to show a simple way to retrieve information from katana using starkli.

In terminal 1
```bash
katana
export STARKNET_ACCOUNT=~/.starkli-wallets/deployer/my_account_1.json
export STARKNET_KEYSTORE=~/.starkli-wallets/deployer/my_keystore_1.json
```

Now create a file called `script_devnet`, using touch command

In terminal 2
```bash
touch script_devnet
```

Edit the file with the program of your choice. Within the file, paste the following content

```bash
#!/bin/bash
chain="$(starkli chain-id --rpc http://0.0.0.0:5050)"
echo "Where are connected to the starknet local devnet with chain id: $chain"

block="$(starkli block-number --rpc http://0.0.0.0:5050)"
echo "The latest block number on Katana is: $block"

account1="0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
balance="$(starkli balance $account1 --rpc http://0.0.0.0:5050)"
echo "The balance of account $account1 is: $balance eth"
```

Now from the command line, run the script using the bash interpreter:

```bash
bash script_devnet
```

Awesome, you get your output from devnet.

#### Goerli

In this example we are going to connect with Goerli testnet, read the latest block number of the network and search for the transaction receipt of and specific transaction hash.

Create a file called `script_testnet`, using touch command

In a new terminal
```bash
touch script_devnet
```

Edit the file. Within the file, paste the following content

```bash
echo "input your testnet API URL: "
read url
chain="$(starkli chain-id --rpc $url)"
echo "Where are connected to the starknet testnet with chain id: $chain"

block="$(starkli block-number --rpc $url)"
echo "The latest block number on Goerli is: $block"

echo "input your transaction hash: "
read hash
receipt="$(starkli receipt $hash --rpc $url)"
echo "The receipt of transaction $hash is: $receipt"
```
Now from the command line, run the script using the bash interpreter:
Then, use the following command for the transfer:

```bash
bash script_testnet
starkli invoke eth transfer <YOUR-ACCOUNT-ADDRESS> u256:1000
```
This time you have to input first a `testnet API URL`, and then you have to provide a `transaction hash`. You can use this transaction hash: 0x2dd73eb1802aef84e8d73334ce0e5856b18df6626fe1a67bb247fcaaccaac8c

Great, now you can create your own bash file according to your interest.
You can create your own script to connect to Starknet using Starkli. In the next subchapter we will create a short Bash script.

0 comments on commit 87e07a9

Please sign in to comment.