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: simple storage with starknet-js #222

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
julio4 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
[package]
name = "simple_storage"
version = "0.1.0"
version.workspace = true
edition = "2023_11"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html
[lib]

[dependencies]
starknet=">=2.4.1"
starknet.workspace = true

[scripts]
test.workspace = true

[[target.starknet-contract]]
2 changes: 1 addition & 1 deletion listings/applications/simple_storage_starknetjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs';
import * as dotenv from 'dotenv';
dotenv.config();

const provider = new RpcProvider({ nodeUrl: 'https://starknet-sepolia.public.blastapi.io' });
const provider = new RpcProvider({ nodeUrl: 'https://free-rpc.nethermind.io/sepolia-juno' });
const accountAddress = '0x067981c7F9f55BCbdD4e0d0a9C5BBCeA77dAcB42cccbf13554A847d6353F728e';
const privateKey = process.env.PRIVATE_KEY;

Expand Down
53 changes: 8 additions & 45 deletions src/applications/simple_storage_starknetjs.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
# Simple Storage (Starknet-js + Cairo)

In this tutorial, you will write and deploy a SimpleStorage Cairo contract on Starknet Sepolia Testnet. Then, you will learn how you can interact with the contract using Starknet-js.

In order to fully understand and implement the content of this tutorial, here are the prerequisites:
- Basic knowledge of Cairo: you can get familiar with the Cairo language, and learn how to build a simple smart contract with Cairo [here](https://book.cairo-lang.org/).

- Scarb for compiling Cairo code and packaging support: follow [here](https://docs.swmansion.com/scarb/download.html).

- Starkli for the declaration and deployment of Cairo contracts: follow [here](https://book.starkli.rs/installation).
In this tutorial, you will write and deploy a SimpleStorage Cairo contract on Starknet Sepolia Testnet. Then, you will learn how you can interact with the contract using Starknet-js. Let's get started!

## Writing SimpleStorage contract in Cairo
The SimpleStorage contract has only one purpose: storing a number. We want the users to interact with the stored number by **viewing** the current stored number and **setting** a new number.
The SimpleStorage contract has only one purpose: storing a number. We want the users to interact with the stored number by **writing** to the currently stored number and **reading** the number in the contract.

Let's create a Scarb package in order to write and compile the SimpleStorage contract. Make sure that Scarb is installed (installation link provided in the prerequisites):
We will use the following SimpleStorage contract. In this [link](https://book.cairo-lang.org/ch13-02-anatomy-of-a-simple-contract.html) from the Cairo book, you can find very detailed explanations for each component of the contract:
julio4 marked this conversation as resolved.
Show resolved Hide resolved

```console
//move to the directory of your choice
$ scarb new simple_storage
```

With this command, a template for Cairo development environment is generated. Under the `src` directory, `lib.cairo` file is created. In order to start writing our contract, follow these steps:
- Under the `src` directory, create a file named `storage.cairo` where we will write our contract.
- Delete the content of the `lib.cairo` file, and add `storage.cairo` as a module for the compilation of our Cairo contract:
```rs
// Directory: src/lib.cairo
mod storage;
```
- You can copy and paste the following Cairo code in `storage.cairo`. In this [link](https://book.cairo-lang.org/ch13-02-anatomy-of-a-simple-contract.html), you can find explanations for each component of the contract:

```rs
// Directory: src/storage.cairo
Expand Down Expand Up @@ -90,7 +70,7 @@ We will utilize Starkli in order to deploy and declare our smart contract on the

For this tutorial, we will create a new account. If you already have an account, you can skip this step and move to the part where we declare our contract.

### Creating a new account:
### Creating a new account
You should move to the directory where you want to access your account keystores, and then create a new folder for the wallet.
```console
$ mkdir ./starkli-wallet
Expand All @@ -117,26 +97,9 @@ $ starkli account deploy ./starkli-wallet/account.json
```
This command wants you to fund the address (given in the instructions below the command) in order to deploy the account on the Starknet Sepolia Testnet. We need testnet Ether on Starknet Sepolia which could be obtained from [this](https://starknet-faucet.vercel.app/) testnet faucet. Once the transaction is confirmed on the faucet page, click ENTER on the command line, and the account will be deployed on Starknet Sepolia! Find your account on the [Voyager Sepolia block explorer](https://sepolia.voyager.online/).

### Declaring & Deploying your Contract:
Firstly, you need to declare your contract which will create a class on Starknet Sepolia. Note that we will use the Sierra program in `./target/simple_storage_SimpleStorage.contract_class.json`.

**Note:** The command below is written to run in the directory of the Scarb folder.
### Declaring & Deploying the SimpleStorage Contract

```console
$ starkli declare --keystore /path/to/starkli-wallet/keystore.json --account /path/to/starkli-wallet/account.json --watch ./target/dev/simple_storage_SimpleStorage.contract_class.json
```

After this command, the class hash is declared. You should be able to find the hash under the command:
```console
Class hash declared:
0x05c8c21062a74e3c8f2015311d7431e820a08a6b0a9571422b607429112d2eb4
```

Now, it's time to deploy the contract. Add the clash hash after `--watch`:
```console
$ starkli deploy --keystore /Users/egeaybars123/keystore/keystore --account /Users/egeaybars123/account/account.json --watch 0x05c8c21062a74e3c8f2015311d7431e820a08a6b0a9571422b607429112d2eb4
```
You should now see the address of the deployed contract. Note down the address of the contract; we will use it for interacting with the contract using Starknet-js.
Please refer to **How To Deploy** in the Getting Started part. Note down the address of your contract, you will need it in Starknet-js in the following part.
julio4 marked this conversation as resolved.
Show resolved Hide resolved

## Interacting with SimpleStorage contract
We will interact with the SimpleStorage contract using Starknet-js. Firstly, create a new folder and inside the directory of the new folder, initialize the npm package (click Enter several items, you can skip adding the package info):
Expand Down Expand Up @@ -195,9 +158,9 @@ Now, let's create a Contract object in order to interact with our contract. In o
Create `abi.json` file in your folder, and copy & paste the contents of `./target/simple_storage_SimpleStorage.contract_class.json` in your Scarb folder. The beginning of the content of the ABI file should look like this:

```json
{"sierra_program":["0x1","0x5","0x0","0x2","0x6","0x3","0x98","0x68","0x18","0x52616e6765436865636b","0x800000000000000100000000000000000000000000000000","0x436f6e7374","0x800000000000000000000000000000000000000000000002","0x1","0x16","0x2","0x53746f726555313238202d206e6f6e2075313238"}
{"sierra_program":["0x1","0x5","0x0","0x2","0x6","0x3","0x98","0x68","0x18","0x52616e6765436865636b","0x800000000000000100000000000000000000000000000000","0x436f6e7374","0x800000000000000000000000000000000000000000000002","0x1","0x16","0x2","0x53746f726555313238202d206e6f6e2075313238",]}

...
//...
```

By importing the content of the ABI and the address of the contract, create a Contract object:
Expand Down
52 changes: 52 additions & 0 deletions src/getting-started/interacting/how_to_deploy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Declaring and Deploying the SimpleStorage contract
julio4 marked this conversation as resolved.
Show resolved Hide resolved
We will utilize Starkli in order to deploy and declare our smart contracts on Starknet. Make sure that Starkli is installed in your device. You can follow [this](https://medium.com/starknet-edu/starkli-the-new-starknet-cli-86ea914a2933) guide for Starkli or check out their documentation given above.
julio4 marked this conversation as resolved.
Show resolved Hide resolved

For this tutorial, we will create a new account. If you already have an account, you can skip this step and move to the part where we declare our contract.

### Creating a new account:
You should move to the directory where you want to access your account keystores, and then create a new folder for the wallet.
```console
$ mkdir ./starkli-wallet
```

Create a new signer. You will be instructed to enter a password to encrypt your private key:
```console
$ starkli signer keystore new ./starkli-wallet/keystore.json
```
After this command, the path of the encrypted keystore file is shown which will be needed during the declaration and deployment of the contract.

Export the keystore path in order not to call --keystore in every command:
```console
$ export STARKNET_KEYSTORE="./starkli-wallet/keystore.json"
```
Initialize the account with the following command using OpenZeppelin's class deployed on Starknet.

```console
$ starkli account oz init ./starkli-wallet/account.json
```
After this command, the address of the account is shown once it is deployed along with the deploy command. Deploy the account:
```console
$ starkli account deploy ./starkli-wallet/account.json
```
This command wants you to fund the address (given in the instructions below the command) in order to deploy the account on the Starknet Sepolia Testnet. We need testnet Ether on Starknet Sepolia which could be obtained from [this](https://starknet-faucet.vercel.app/) testnet faucet. Once the transaction is confirmed on the faucet page, click ENTER on the command line, and the account will be deployed on Starknet Sepolia! Find your account on the [Voyager Sepolia block explorer](https://sepolia.voyager.online/).

### Declaring & Deploying your Contract:
Firstly, you need to declare your contract which will create a class on Starknet Sepolia. Note that we will use the Sierra program in `./target/ProjectName_ContractName.contract_class.json` in your Scarb folder.

**Note:** The command below is written to run in the directory of the Scarb folder.

```console
$ starkli declare --keystore /path/to/starkli-wallet/keystore.json --account /path/to/starkli-wallet/account.json --watch ./target/dev/simple_storage_SimpleStorage.contract_class.json
```

After this command, the class hash for your contract is declared. You should be able to find the hash under the command:
```console
Class hash declared:
0x05c8c21062a74e3c8f2015311d7431e820a08a6b0a9571422b607429112d2eb4
```

Now, it's time to deploy the contract. Add the clash hash given above after `--watch`:
```console
$ starkli deploy --keystore /Users/egeaybars123/keystore/keystore --account /Users/egeaybars123/account/account.json --watch 0x05c8c21062a74e3c8f2015311d7431e820a08a6b0a9571422b607429112d2eb4
```
You should now see the address of the deployed contract. Congratulations, you have deployed your contract on Starknet Sepolia Testnet!