mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-06 06:47:54 +02:00
made function patterns consistent
This commit is contained in:
@@ -157,14 +157,16 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin
|
|||||||
// @storage_var is a decorator that instructs the compiler the function
|
// @storage_var is a decorator that instructs the compiler the function
|
||||||
// below it is a storage variable.
|
// below it is a storage variable.
|
||||||
@storage_var
|
@storage_var
|
||||||
func balance() -> (res: felt){}
|
func balance() -> (res: felt) {}
|
||||||
|
|
||||||
// @dev Constructor writes the balance variable to 0 on deployment
|
// @dev Constructor writes the balance variable to 0 on deployment
|
||||||
// Constructors sets storage variables on deployment. Can accept arguments too.
|
// Constructors sets storage variables on deployment. Can accept arguments too.
|
||||||
@constructor
|
@constructor
|
||||||
func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
|
func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
|
||||||
range_check_ptr}() {balance.write(0); return();
|
range_check_ptr}() {
|
||||||
}
|
balance.write(0);
|
||||||
|
return();
|
||||||
|
}
|
||||||
|
|
||||||
// @dev increase_balance updates the balance variable
|
// @dev increase_balance updates the balance variable
|
||||||
// @param amount the amount you want to add to balance
|
// @param amount the amount you want to add to balance
|
||||||
@@ -172,23 +174,23 @@ func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
|
|||||||
// function.
|
// function.
|
||||||
@external
|
@external
|
||||||
func increase_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
|
func increase_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*,
|
||||||
range_check_ptr}(amount: felt){
|
range_check_ptr}(amount: felt){
|
||||||
with_attr error_message("Amount must be positive. Got: {amount}.") {
|
with_attr error_message("Amount must be positive. Got: {amount}.") {
|
||||||
assert_nn(amount);
|
assert_nn(amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
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.
|
||||||
@view
|
@view
|
||||||
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) {
|
||||||
let (res) = balance.read();
|
let (res) = balance.read();
|
||||||
return (res,);
|
return (res,);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -449,7 +451,7 @@ const ADMIN = 0x01C6cfC1DB2ae90dACEA243F0a8C2F4e32560F7cDD398e4dA2Cc56B733774E9b
|
|||||||
with_attr error_message("You do not have access to make this action!"){
|
with_attr error_message("You do not have access to make this action!"){
|
||||||
let (caller) = get_caller_address();
|
let (caller) = get_caller_address();
|
||||||
assert ADMIN = caller;
|
assert ADMIN = caller;
|
||||||
}
|
}
|
||||||
|
|
||||||
// using an assert statement throws if condition is not true, thus
|
// using an assert statement throws if condition is not true, thus
|
||||||
// returning the specified error.
|
// returning the specified error.
|
||||||
@@ -810,15 +812,15 @@ range_check_ptr}(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// @dev internal function to get the opposite token type
|
// @dev internal function to get the opposite token type
|
||||||
// @param token_type Token whose opposite pair needs to be gotten
|
// @param token_type Token whose opposite pair needs to be gotten
|
||||||
func get_opposite_token(token_type: felt) -> (t: felt) {
|
func get_opposite_token(token_type: felt) -> (t: felt) {
|
||||||
if(token_type == TOKEN_TYPE_A) {
|
if(token_type == TOKEN_TYPE_A) {
|
||||||
return (t=TOKEN_TYPE_B);
|
return (t=TOKEN_TYPE_B);
|
||||||
} else {
|
} else {
|
||||||
return (t=TOKEN_TYPE_A);
|
return (t=TOKEN_TYPE_A);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Additional Resources
|
## Additional Resources
|
||||||
|
Reference in New Issue
Block a user