updated cairo.html.markdown

This commit is contained in:
Darlington02 2023-02-06 15:04:37 +01:00
parent aced78c444
commit 741e0f4a7f

View File

@ -180,7 +180,7 @@ func increase_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
let (res) = balance.read(); let (res) = balance.read();
balance.write(res + amount); balance.write(res + amount);
return (); return ();
} }
// @dev returns the balance variable // @dev returns the balance variable
// @view is a decorator that specifies the func below it is a view function. // @view is a decorator that specifies the func below it is a view function.
@ -189,7 +189,7 @@ func get_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
range_check_ptr}() -> (res: felt) { range_check_ptr}() -> (res: felt) {
let (res) = balance.read(); let (res) = balance.read();
return (res,); return (res,);
} }
``` ```
Before proceeding to the main lessons, try to build, deploy and interact with Before proceeding to the main lessons, try to build, deploy and interact with
@ -204,10 +204,10 @@ just a single data type `..felts`. Felts stands for Field elements, and are a
create a `Uint256` in Cairo by utlizing a struct of two 128 bits felts. create a `Uint256` in Cairo by utlizing a struct of two 128 bits felts.
```cairo ```cairo
struct Uint256{ struct Uint256 {
low: felt, // The low 128 bits of the value. low: felt, // The low 128 bits of the value.
high: felt, // The high 128 bits of the value. high: felt, // The high 128 bits of the value.
} }
``` ```
To avoid running into issues with divisions, it's safer to work with the To avoid running into issues with divisions, it's safer to work with the
@ -239,19 +239,19 @@ from starkware.cairo.common.bool import TRUE
+ Storage variables: Cairo's storage is a map with `2^251` slots, where each + Storage variables: Cairo's storage is a map with `2^251` slots, where each
slot is a felt which is initialized to `0`. You create one using the slot is a felt which is initialized to `0`. You create one using the
`@storage_var` decorator `@storage_var` decorator.
```cairo ```cairo
@storage_var @storage_var
func names() -> (name: felt){} func names() -> (name: felt) {}
``` ```
+ Storage mappings: Unlike soldity where mappings have a separate keyword, in + Storage mappings: Unlike Solidity where mappings have a separate keyword, in
Cairo you create mappings using storage variables. Cairo you create mappings using storage variables.
```cairo ```cairo
@storage_var @storage_var
func names(address: felt) -> (name: felt){} func names(address: felt) -> (name: felt) {}
``` ```
+ Structs: are a means to create custom data types in Cairo. A `struct` has a + Structs: are a means to create custom data types in Cairo. A `struct` has a
@ -269,7 +269,7 @@ from starkware.cairo.common.bool import TRUE
+ Constants: Constants are fixed and as such can't be altered after being set. + Constants: Constants are fixed and as such can't be altered after being set.
They evaluate to an integer (field element) at compile time. To create a They evaluate to an integer (field element) at compile time. To create a
constant in Cairo, you use the `const` keyword. Its proper practice to constant in Cairo, you use the `const` keyword. It's proper practice to
capitalize constant names. capitalize constant names.
```cairo ```cairo
@ -479,12 +479,12 @@ contract passing in the contract address as the first parameter like this:
IENS.store_name(contract_address, _name); IENS.store_name(contract_address, _name);
``` ```
Note that Interfaces excludes the function body/logic and the implicit Note that Interfaces exclude the function body/logic and the implicit
arguments. arguments.
### 9. Recursions ### 9. Recursions
Due to the unavailability of loops, Recursions are the go-to for similar Due to the unavailability of loops, Recursion is the go-to for similar
operations. In simple terms, a recursive function is one which calls itself operations. In simple terms, a recursive function is one which calls itself
repeatedly. repeatedly.
@ -494,7 +494,7 @@ fibonacci number:
```cairo ```cairo
@external @external
func fibonacci{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, func fibonacci{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
range_check_ptr}(n : felt) -> (result : felt){ range_check_ptr}(n : felt) -> (result : felt){
alloc_locals; alloc_locals;
if (n == 0){ if (n == 0){
return (0); return (0);
@ -505,14 +505,14 @@ func fibonacci{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
let (local x) = fibonacci(n - 1); let (local x) = fibonacci(n - 1);
let (local y) = fibonacci(n - 2); let (local y) = fibonacci(n - 2);
return (result=(x + y)); return (result=(x + y));
} }
``` ```
The nth fibonacci term is the sum of the `nth - 1` and the `nth - 2` numbers, The nth fibonacci term is the sum of the `nth - 1` and the `nth - 2` numbers,
that's why we get these two as `(x,y)` using recursion. that's why we get these two as `(x,y)` using recursion.
NB: when implementing recursive functions, always remember to implement a base NB: when implementing recursive functions, always remember to implement a base
case (`n==0`, `n==1` in our case), to prevent stack overflow. case (`n==0`, `n==1` in our case), to prevent stack overflows.
### 10. Registers ### 10. Registers
@ -529,8 +529,8 @@ registers:
### 11. Revoked References ### 11. Revoked References
Revoked references occurs when there is a call instruction to another function, Revoked references occur when there is a call instruction to another function,
between the definition of a reference variable that depends on `ap`(temp between the definition of a reference variable that depends on `ap` (temp
variables) and its usage. This occurs as the compiler may not be able to compute variables) and its usage. This occurs as the compiler may not be able to compute
the change of `ap` (as one may jump to the label from another place in the the change of `ap` (as one may jump to the label from another place in the
program, or call a function that might change ap in an unknown way). program, or call a function that might change ap in an unknown way).
@ -540,18 +540,18 @@ Here is an example to demonstrate what I mean:
```cairo ```cairo
@external @external
func get_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, func get_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
range_check_ptr}() -> (res: felt) { range_check_ptr}() -> (res: felt) {
return (res=100); return (res=100);
} }
@external @external
func double_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, func double_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
range_check_ptr}() -> (res: felt) { range_check_ptr}() -> (res: felt) {
let multiplier = 2; let multiplier = 2;
let (balance) = get_balance(); let (balance) = get_balance();
let new_balance = balance * multiplier; let new_balance = balance * multiplier;
return (res=new_balance); return (res=new_balance);
} }
``` ```
If you run that code, you'll run into the revoked reference error as we are If you run that code, you'll run into the revoked reference error as we are
@ -559,20 +559,20 @@ trying to access the `multiplier` variable after calling the `get_balance`
function. function.
In simple cases you can resolve revoked references by adding the keyword In simple cases you can resolve revoked references by adding the keyword
`alloc_locals` within function scopes. In most complex cases you might need to `alloc_locals` within function scopes. In more complex cases you might need to
create a local variable to resolve it. create a local variable to resolve it.
```cairo ```cairo
// resolving the `double_balance` function: // resolving the `double_balance` function:
@external @external
func double_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, func double_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
range_check_ptr}() -> (res: felt) { range_check_ptr}() -> (res: felt) {
alloc_locals; alloc_locals;
let multiplier = 2; let multiplier = 2;
let (balance) = get_balance(); let (balance) = get_balance();
let new_balance = balance * multiplier; let new_balance = balance * multiplier;
return (res=new_balance); return (res=new_balance);
} }
``` ```
### 12. Understanding Cairo's Punctuations ### 12. Understanding Cairo's Punctuations
@ -627,7 +627,7 @@ const ACCOUNT_BALANCE_BOUND = 1073741; // (2 ** 30 / 1000)
// //
// @dev A map from account and token type to corresponding balance // @dev A map from account and token type to corresponding balance
@storage_var @storage_var
func account_balance(account_id: felt, token_type: felt) -> (balance: felt){} func account_balance(account_id: felt, token_type: felt) -> (balance: felt) {}
// @dev a map from token type to corresponding pool balance // @dev a map from token type to corresponding pool balance
@storage_var @storage_var
@ -641,21 +641,21 @@ func pool_balance(token_type: felt) -> (balance: felt) {}
// @param token_type Token to be queried // @param token_type Token to be queried
@view @view
func get_account_token_balance{syscall_ptr: felt*, pedersen_ptr: func get_account_token_balance{syscall_ptr: felt*, pedersen_ptr:
HashBuiltin*, range_check_ptr}( HashBuiltin*, range_check_ptr}(
account_id: felt, token_type: felt account_id: felt, token_type: felt
) -> (balance: felt) { ) -> (balance: felt) {
return account_balance.read(account_id, token_type); return account_balance.read(account_id, token_type);
} }
// @dev return the pool's balance // @dev return the pool's balance
// @param token_type Token type to get pool balance // @param token_type Token type to get pool balance
@view @view
func get_pool_token_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, func get_pool_token_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
range_check_ptr}( range_check_ptr}(
token_type: felt token_type: felt
) -> (balance: felt) { ) -> (balance: felt) {
return pool_balance.read(token_type); return pool_balance.read(token_type);
} }
// EXTERNALS // EXTERNALS
@ -665,7 +665,7 @@ func get_pool_token_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
// @param balance Amount to be set as balance // @param balance Amount to be set as balance
@external @external
func set_pool_token_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, func set_pool_token_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
range_check_ptr}( range_check_ptr}(
token_type: felt, balance: felt token_type: felt, balance: felt
) { ) {
with_attr error_message("exceeds maximum allowed tokens!"){ with_attr error_message("exceeds maximum allowed tokens!"){
@ -674,14 +674,14 @@ func set_pool_token_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
pool_balance.write(token_type, balance); pool_balance.write(token_type, balance);
return (); return ();
} }
// @dev add demo token to the given account // @dev add demo token to the given account
// @param token_a_amount amount of token a to be added // @param token_a_amount amount of token a to be added
// @param token_b_amount amount of token b to be added // @param token_b_amount amount of token b to be added
@external @external
func add_demo_token{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, func add_demo_token{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
range_check_ptr}( range_check_ptr}(
token_a_amount: felt, token_b_amount: felt token_a_amount: felt, token_b_amount: felt
) { ) {
alloc_locals; alloc_locals;
@ -693,14 +693,14 @@ func add_demo_token{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
amount=token_b_amount); amount=token_b_amount);
return (); return ();
} }
// @dev intialize AMM // @dev intialize AMM
// @param token_a amount of token a to be set in pool // @param token_a amount of token a to be set in pool
// @param token_b amount of token b to be set in pool // @param token_b amount of token b to be set in pool
@external @external
func init_pool{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, func init_pool{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
range_check_ptr}( range_check_ptr}(
token_a: felt, token_b: felt token_a: felt, token_b: felt
) { ) {
with_attr error_message("exceeds maximum allowed tokens!"){ with_attr error_message("exceeds maximum allowed tokens!"){
@ -712,7 +712,7 @@ func init_pool{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
set_pool_token_balance(token_type=TOKEN_TYPE_B, balance=token_b); set_pool_token_balance(token_type=TOKEN_TYPE_B, balance=token_b);
return (); return ();
} }
// @dev swaps token between the given account and the pool // @dev swaps token between the given account and the pool
@ -748,7 +748,7 @@ func swap{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
token_to=token_to, amount_from=amount_from); token_to=token_to, amount_from=amount_from);
return (amount_to=amount_to); return (amount_to=amount_to);
} }
// INTERNALS // INTERNALS
@ -771,7 +771,7 @@ range_check_ptr}(
account_balance.write(account_id=account_id, token_type=token_type, account_balance.write(account_id=account_id, token_type=token_type,
value=new_balance); value=new_balance);
return (); return ();
} }
// @dev internal function that swaps tokens between the given account and // @dev internal function that swaps tokens between the given account and
// the pool // the pool
@ -807,7 +807,7 @@ range_check_ptr}(
amount_to)); amount_to));
return (amount_to=amount_to); return (amount_to=amount_to);
} }
// @dev internal function to get the opposite token type // @dev internal function to get the opposite token type