Skip to content

Setup your Cairo Environment for Smart Contract development on Starknet

Notifications You must be signed in to change notification settings

0xCorolaire/Starknet-Cairo-EnvironmentTemplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Starknet-Cairo-EnvironmentTemplate

Setup your Cairo Environment for Smart Contract development on Starknet Follow the steps to initialize your Cairo project environment


1. Creating an account contract & Organizing

⇒ Setup Argent Account

image

image

2. Dependencies installation

Req : Python 3.7

sudo apt install python-dev python3-dev gcc virtualenv python3-virtualenv python3-venv

python3 -m venv ~/cairo_venv
source ~/cairo_venv/bin/activate
pip install --upgrade pip
sudo apt install -y libgmp3-dev
pip3 install cairo-lang
pip3 install cairo-nile
#pip3 install contextvars if using Python 3.6

3. Test your Environment

create test.cairo :

func main():
    [ap] = 1000; ap++
    [ap] = 2000; ap++
    [ap] = [ap - 2] + [ap - 1]; ap++
    ret
end

Compile contract and output :

cairo-compile test.cairo --output test_compiled.json
cairo-run \
  --program=test_compiled.json --print_output \
  --print_info --relocate_prints --tracer

4. Hello World Starknet Contract

Create a contracts folder, and inside of it, a helloworld.cairo file :

# Declare this file as a StarkNet contract.
%lang starknet

from starkware.cairo.common.cairo_builtins import HashBuiltin

# Define a storage variable.
@storage_var
func balance() -> (res : felt):
end

# Increases the balance by the given amount.
@external
func increase_balance{
        syscall_ptr : felt*, pedersen_ptr : HashBuiltin*,
        range_check_ptr}(amount : felt):
    let (res) = balance.read()
    balance.write(res + amount)
    return ()
end

# Returns the current balance.
@view
func get_balance{
        syscall_ptr : felt*, pedersen_ptr : HashBuiltin*,
        range_check_ptr}() -> (res : felt):
    let (res) = balance.read()
    return (res)
end

Then save it and Compile your first contract !

cd contracts
starknet-compile helloworld.cairo \
    --output helloworldcontract_compiled.json \
    --abi helloworldcontract_abi.json

You’ve succed, you have now the abi and compiled file ready to be pushed to network !

Add the network you want to use to the environment variable :

export STARKNET_NETWORK=alpha-goerli

Deploy your first smart contract !

starknet deploy --contract helloworldcontract_compiled.json

Gj sir !

image

5. More with the cli (it’s just a plus)

export CONTRACT_ADDRESS="<address of the previous contract>"
  • You can now interact with the functions of your Smart Contract :
starknet invoke \
    --address ${CONTRACT_ADDRESS} \
    --abi helloworldcontract_abi.json \
    --function increase_balance \
    --inputs 1234
  • Check TX status
starknet tx_status --hash TXHASH
  • Check TX results
starknet get_transaction --hash TXHASH
starknet get_transaction_receipt --hash TXHASH
starknet get_transaction_trace --hash TXHASH# Check emmited events

6. Deploy and manipulate your smart contract

Exists 3 solutions : the CLI we’ve seen right before,

7. Create your first Project with nile

mkdir myproject
cd myproject
nile init
...
✨  Cairo successfully installed!
...
✅ Dependencies successfully installed
🗄  Creating project directory tree
⛵️ Nile project ready! Try running:
  • Compile a Smart contract using Nile
nile compile # compiles all contracts under contracts/
nile compile --directory my_contracts # compiles all contracts under my_contracts/
nile compile contracts/MyContract.cairo # compiles single contract
  • Deploying a Smart Contract using Nile
nile deploy contract --alias my_contract

About

Setup your Cairo Environment for Smart Contract development on Starknet

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published