Skip to content

Complete StarkNet library in Rust

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

refcell/starknet-rs

 
 

Repository files navigation

Logo

starknet-rs

Complete StarkNet library in Rust

linting-badge crates-badge

Note that starknet-rs is still experimental. Breaking changes will be made before the first stable release. The library is also NOT audited or reviewed for security at the moment. Use at your own risk.

The underlying cryptography library starknet-crypto does NOT provide constant-time guarantees.

Adding starknet-rs to your project

To use the crate from crates.io, add the following to your Cargo.toml file:

[dependencies]
starknet = "0.1.0"

To use from GitHub directly, use this line instead:

[dependencies]
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs" }

Features

  • Sequencer gateway / feeder gateway client
  • Full node JSON-RPC API client
  • Smart contract deployment
  • Signer for using IAccount account contracts
  • Strongly-typed smart contract binding code generation from ABI

Crates

This workspace contains the following crates:

  • starknet: Re-export of other sub-crates (recommended)
  • starknet-core: Core data structures for interacting with StarkNet
  • starknet-providers: Abstraction and implementation of clients for interacting with StarkNet nodes and sequencers
  • starknet-contract: Types for deploying and interacting with StarkNet smart contracts
  • starknet-crypto: Low-level cryptography utilities for StarkNet
  • starknet-signers: StarkNet signer implementations
  • starknet-accounts: Types for handling StarkNet account abstraction
  • starknet-ff: StarkNet field element type

Example

Get the latest block from alpha-goerli testnet

use starknet::{
    core::types::BlockId,
    providers::{Provider, SequencerGatewayProvider},
};

#[tokio::main]
async fn main() {
    let provider = SequencerGatewayProvider::starknet_alpha_goerli();
    let latest_block = provider.get_block(BlockId::Latest).await;
    println!("{:#?}", latest_block);
}

Deploy contract to alpha-goerli testnet

use starknet::{
    contract::ContractFactory,
    core::types::{ContractArtifact, FieldElement},
    providers::SequencerGatewayProvider,
};

#[tokio::main]
async fn main() {
    let contract_artifact: ContractArtifact =
        serde_json::from_reader(std::fs::File::open("/path/to/contract/artifact.json").unwrap())
            .unwrap();
    let provider = SequencerGatewayProvider::starknet_alpha_goerli();

    let contract_factory = ContractFactory::new(contract_artifact, provider).unwrap();
    contract_factory
        .deploy(vec![FieldElement::from_dec_str("123456").unwrap()], None)
        .await
        .expect("Unable to deploy contract");
}

Mint yourself 1,000 TST tokens on alpha-goerli

use starknet::{
    accounts::{Account, Call, SingleOwnerAccount},
    core::{
        types::{BlockId, FieldElement},
        utils::get_selector_from_name,
    },
    providers::SequencerGatewayProvider,
    signers::{LocalWallet, SigningKey},
};

#[tokio::main]
async fn main() {
    let provider = SequencerGatewayProvider::starknet_alpha_goerli();
    let signer = LocalWallet::from(SigningKey::from_secret_scalar(
        FieldElement::from_hex_be("YOUR_PRIVATE_KEY_IN_HEX_HERE").unwrap(),
    ));
    let address = FieldElement::from_hex_be("YOUR_ACCOUNT_CONTRACT_ADDRESS_IN_HEX_HERE").unwrap();
    let tst_token_address = FieldElement::from_hex_be(
        "07394cbe418daa16e42b87ba67372d4ab4a5df0b05c6e554d158458ce245bc10",
    )
    .unwrap();

    let account = SingleOwnerAccount::new(provider, signer, address);
    let nonce = account.get_nonce(BlockId::Latest).await.unwrap();

    let result = account
        .execute(
            &[Call {
                to: tst_token_address,
                selector: get_selector_from_name("mint").unwrap(),
                calldata: vec![
                    address,
                    FieldElement::from_dec_str("1000000000000000000000").unwrap(),
                    FieldElement::ZERO,
                ],
            }],
            nonce,
        )
        .await
        .unwrap();

    dbg!(result);
}

License

Licensed under either of

at your option.

About

Complete StarkNet library in Rust

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 98.2%
  • Shell 1.8%