-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
52 changed files
with
1,459 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
RST='\033[0m' # No Color, Reset. | ||
YEL='\033[1;93m' # Yellow. | ||
VIO='\033[1;95m' # Violet. | ||
ERR='\033[1;31m' # Strong red. | ||
|
||
# Check the operating system | ||
os=$(uname -s) | ||
|
||
# Cairo Version. | ||
version="2.0.0-rc4" | ||
|
||
case "$os" in | ||
|
||
# Mac OS | ||
Darwin) | ||
echo -e "${YEL}--- Installing Cairo ${version} on Mac OS --- ${RST}" | ||
if [[ ! $(command -v rustc) ]]; then | ||
echo -e "${ERR}==>${RST} Rust compiler not found, please install it (homebrew or manually), and run the script again." | ||
exit 1 | ||
fi | ||
echo -e "${VIO}==>${RST} Downloading cairo-lang" | ||
set -x | ||
curl -o /tmp/cairo-lang.rb https://raw.githubusercontent.com/lambdaclass/cairo-by-example/main/build/homebrew/cairo-lang.rb && brew install --formula --build-from-source /tmp/cairo-lang.rb | ||
set +x | ||
corelib="$(brew --cellar cairo-lang)/$(brew list --versions cairo-lang | tr ' ' '\n' | tail -1)/corelib" | ||
if [[ -f /Users/${USER}/.zshrc ]]; then | ||
echo "export CARGO_MANIFEST_DIR=\"${corelib}\"" >> /Users/${USER}/.zshrc | ||
echo "~/.zshrc has been populated with the required environment variable" | ||
echo "For cairo to work in the current shell, you need to set it manually:" | ||
echo "export CARGO_MANIFEST_DIR=\"${corelib}\"" | ||
elif [[ -f /Users/${USER}/.bashrc ]]; then | ||
echo "export CARGO_MANIFEST_DIR=\"${corelib}\"" >> /Users/${USER}/.bashrc | ||
echo "~/.bashrc has been populated with the required environment variable" | ||
echo "For cairo to work in the current shell, you need to set it manually:" | ||
echo "export CARGO_MANIFEST_DIR=\"${corelib}\"" | ||
else | ||
echo "For cairo to work, please set the CARGO_MANIFEST_DIR environment variable in your shell of choice with the following value:" | ||
echo "export CARGO_MANIFEST_DIR=\"${corelib}\"" | ||
fi | ||
;; | ||
|
||
# Linux | ||
Linux) | ||
distro="" | ||
[[ ! -e /etc/os-release ]] && exit 1 | ||
source /etc/os-release | ||
case "$ID" in | ||
# XXX: Script for debian/ubuntu is not working (missing corelib/) | ||
arch) | ||
echo -e "${YEL}--- Installing Cairo ${version} on Arch Linux --- ${RST}" | ||
[[ ! -e /usr/bin/yay ]] && echo -e "${ERR}No yay installation found. If you use another AUR helper, please install the cairo-lang package.${RST}" | ||
set -x | ||
yay -S cairo-lang | ||
set +x | ||
;; | ||
# debian | ubuntu) | ||
# [[ ! -e /etc/os-release ]] && exit 1 | ||
# echo -e "${YEL}--- Installing Cairo ${version} on Debian/Ubuntu --- ${RST}" | ||
# echo -e "${VIO}==>${RST} Downloading .deb file..." | ||
# set -x | ||
# wget -O /tmp/cairo_${version}_amd64.deb https://github.com/lambdaclass/cairo-by-example/releases/download/v${version}/cairo_${version}-1_amd64.deb | ||
# set +x | ||
# echo -e "${VIO}==>${RST} Installing cairo-lang..." | ||
# sudo dpkg -i /tmp/cairo_${version}_amd64.deb | ||
# ;; | ||
*) | ||
echo -e "${YEL}--- Detected installation for unknown Linux distro --- ${RST}" | ||
set -x | ||
curl -L https://github.com/franalgaba/cairo-installer/raw/main/bin/cairo-installer | bash | ||
set +x | ||
;; | ||
esac | ||
;; | ||
|
||
# Windows | ||
CYGWIN* | MINGW* | MSYS*) | ||
echo -e "${YEL}--- Installing Cairo ${version} on Windows --- ${RST}" | ||
set -x | ||
# TODO: Add command for Windows here | ||
set +x | ||
echo -e "${ERR} Windows installation is still WIP." | ||
exit 1 | ||
;; | ||
|
||
*) | ||
echo -e "Unsupported operating system: $os" | ||
exit 1 | ||
;; | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
title: "booleans" | ||
weight: 85 | ||
draft: false | ||
--- | ||
|
||
As with Boolean types in other programming languages, `bool`s have only two possible values: `true` and `false`. | ||
|
||
```rust {.codebox} | ||
fn main() { | ||
let t: bool = true; | ||
let true_expr = 5 == 5; | ||
assert(t == true_expr, 'this is true'); | ||
let f: bool = false; | ||
let false_expr = 7 == 5; | ||
assert(f == false_expr, 'this is false'); | ||
} | ||
``` | ||
As with other scalar types, they are one `felt252` in size. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
--- | ||
title: "contracts" | ||
weight: 360 | ||
draft: false | ||
--- | ||
|
||
#### Contracts | ||
|
||
Contracts can be deployed to Starknet. | ||
|
||
The contract modules are defined by adding a `#[starknet::contract]` attribute above the module definition: | ||
|
||
```rust {.codebox} | ||
#[starknet::contract] | ||
mod Contract { | ||
} | ||
``` | ||
|
||
#### Storage | ||
|
||
Inside a contract definition, you can define a storage, using the `#[storage]` attribute: | ||
|
||
```rust {.codebox} | ||
#[storage] | ||
struct Storage { | ||
token_supply: felt252, | ||
decimals: u8 | ||
} | ||
``` | ||
|
||
#### Interfaces | ||
|
||
In order to interact with your contract, you'll want to define an interface, which gathers external (functions that mutate the storage) and view (functions that don't mutate the storage) function signatures under a trait. Traits use a generic argument, generally referring to the contract state. Static functions that do not use the storage or emit events do not require an additional argument. Interfaces are defined by adding a `#[starknet::interface]` attribute: | ||
|
||
```rust {.codebox} | ||
#[starknet::interface] | ||
trait IContract<TContractState> { | ||
fn increase_token_supply(ref self: TContractState, amount: felt252); | ||
fn increase_decimals(ref self: TContractState, amount: u8); | ||
fn get_token_supply(self: @TContractState) -> felt252; | ||
} | ||
``` | ||
#### Implementations | ||
|
||
Functions that affect the contract state use a mutable reference to the `ContractState`: `ref self: TContractState `; while the functions that don't affect the state use: `self: @TContractState`. | ||
|
||
And to implement this trait inside the contract, we use the `impl` keyword, with a `#[external(v0)]` attribute: | ||
|
||
```rust {.codebox} | ||
mod Contract { | ||
#[storage] | ||
struct Storage { | ||
token_supply: felt252, | ||
decimals: u8 | ||
} | ||
|
||
#[external(v0)] | ||
impl Contract of super::IContract<ContractState> { | ||
fn increase_token_supply(ref self: ContractState, amount: felt252) { ... } | ||
fn increase_decimals(ref self: ContractState, amount: u8) { ... } | ||
fn get_token_supply(self: @ContractState) -> felt252 { ... } | ||
} | ||
} | ||
``` | ||
|
||
#### Storage Access | ||
|
||
In order to interact with the contract state, you can use `read` and `write` functions: | ||
|
||
```rust {.codebox} | ||
let current_balance = self.balance.read(); | ||
self.balance.write(current_balance + amount) | ||
``` | ||
|
||
That can be used inside functions: | ||
|
||
```rust {.codebox} | ||
fn increase_token_supply(ref self: ContractState, amount: felt252) { | ||
let current_token_supply = self.token_supply.read(); | ||
self.token_supply.write(current_token_supply + amount) | ||
} | ||
``` | ||
|
||
#### Constructors | ||
|
||
When you deploy a contract, you can set its initial values using a constructor: | ||
|
||
```rust {.codebox} | ||
mod Contract { | ||
#[constructor] | ||
fn constructor(ref self: ContractState, initial_token_supply: felt252, initial_decimals: u8) { | ||
self.token_supply.write(initial_token_supply); | ||
self.decimals.write(initial_decimals); | ||
} | ||
} | ||
``` | ||
|
||
#### Events definition | ||
|
||
In Cairo2 all the contract events are unified under the `Event` enum: | ||
|
||
```rust {.codebox} | ||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
TokenSupplyIncreased: TokenSupplyIncreased, | ||
DecimalsIncreased: DecimalsIncreased | ||
} | ||
|
||
#[derive(Drop, starknet::Event)] | ||
struct TokenSupplyIncreased { | ||
amount: felt252 | ||
} | ||
|
||
#[derive(Drop, starknet::Event)] | ||
struct DecimalsIncreased { | ||
amount: u8 | ||
} | ||
``` | ||
|
||
#### Emitting Events | ||
|
||
In order to emit events, you can do the following: | ||
|
||
```rust {.codebox} | ||
fn increase_token_supply(ref self: ContractState, amount: felt252) { | ||
... | ||
self.emit(TokenSupplyIncreased { amount }); | ||
} | ||
``` |
Oops, something went wrong.