Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat constructor #32

Merged
merged 11 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions listings/ch00-introduction/constructor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
10 changes: 10 additions & 0 deletions listings/ch00-introduction/constructor/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "constructor"
version = "0.1.0"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest

[dependencies]
starknet = "1.1.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing [[target.starknet-contract]] line


[[target.starknet-contract]]
13 changes: 13 additions & 0 deletions listings/ch00-introduction/constructor/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[contract]
mod constructor {
StarkFishinator marked this conversation as resolved.
Show resolved Hide resolved
use starknet::ContractAddress;

struct Storage {
_names: LegacyMap::<ContractAddress, felt252>,
}

#[constructor]
fn constructor(name: felt252, address: ContractAddress) {
_names::write(address, name);
}
}
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Summary
- [Write to Any Slot](./ch00-04-write_to_any_slot.md)
- [Storing Arrays](./ch00-05-storing_arrays.md)
- [Mappings](./ch00-06-mappings.md)
- [Constructors](./ch00-07-constructor.md)

- [Applications](./ch01-00-applications.md)
- [Upgradeable Contract](./ch01-01-upgradeable_contract.md)
9 changes: 9 additions & 0 deletions src/ch00-07-constructor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Constructor

Constructors are a special type of function that runs only once when deploying a contract, and can be used to initialize the state of the contract. Your contract must not have more than one constructor, and that constructor function must be annotated with the `#[constructor]` attribute. Also, a good practice consists in naming that function `constructor`.

Here's a simple example that demonstrates how to initialize the state of a contract on deployment by defining logic inside a constructor.

```rust
{{#include ../listings/ch00-introduction/constructor/src/lib.cairo}}
```