|
| 1 | += Compiling the `HelloStarknet` contract |
| 2 | + |
| 3 | +== Introduction |
| 4 | + |
| 5 | +Welcome to the second installment of the "Hello, Starknet!" quickstart series, the official tutorial for starting your journey as a Starknet developer! 🚀 |
| 6 | + |
| 7 | +Before a contract can be deployed on Starknet, its compiled code needs to be submitted to the network (also known as _declaring_ it). This installment of the series will therefore walk you though compiling Scarb's default `HelloStarknet` contract, which will be used throughout the following installments. |
| 8 | + |
| 9 | +[TIP] |
| 10 | +==== |
| 11 | +To learn more about Starknet smart contracts, see the xref:architecture-and-concepts:smart-contracts/contract-classes.adoc[Contracts section]. |
| 12 | +==== |
| 13 | + |
| 14 | +== Generating `HelloStarknet` |
| 15 | + |
| 16 | +Scarb's default `HelloStarknet` contract can be generated by simply running: |
| 17 | + |
| 18 | +[source,terminal] |
| 19 | +---- |
| 20 | +scarb new hello_starknet |
| 21 | +---- |
| 22 | + |
| 23 | +and selecting to set up the `Starknet Foundry (default)` test runner. |
| 24 | + |
| 25 | +For the purpose of this tutorial, you can ignore all files in the `hello_starknet` directory other than `hello_starknet/src/lib.cairo`, which holds the contract's code: |
| 26 | + |
| 27 | +[#example-cairo-contract] |
| 28 | +[source,cairo] |
| 29 | +---- |
| 30 | +/// Interface representing `HelloContract`. |
| 31 | +/// This interface allows modification and retrieval of the contract balance. |
| 32 | +#[starknet::interface] |
| 33 | +pub trait IHelloStarknet<TContractState> { |
| 34 | + /// Increase contract balance. |
| 35 | + fn increase_balance(ref self: TContractState, amount: felt252); |
| 36 | + /// Retrieve contract balance. |
| 37 | + fn get_balance(self: @TContractState) -> felt252; |
| 38 | +} |
| 39 | +
|
| 40 | +/// Simple contract for managing balance. |
| 41 | +#[starknet::contract] |
| 42 | +mod HelloStarknet { |
| 43 | + use core::starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; |
| 44 | +
|
| 45 | + #[storage] |
| 46 | + struct Storage { |
| 47 | + balance: felt252, |
| 48 | + } |
| 49 | +
|
| 50 | + #[abi(embed_v0)] |
| 51 | + impl HelloStarknetImpl of super::IHelloStarknet<ContractState> { |
| 52 | + fn increase_balance(ref self: ContractState, amount: felt252) { |
| 53 | + assert(amount != 0, 'Amount cannot be 0'); |
| 54 | + self.balance.write(self.balance.read() + amount); |
| 55 | + } |
| 56 | +
|
| 57 | + fn get_balance(self: @ContractState) -> felt252 { |
| 58 | + self.balance.read() |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +---- |
| 63 | + |
| 64 | +As its comments read, this is a simple contract with two basic functions: |
| 65 | + |
| 66 | +* `get_balance`, which reads the contract's current balance from storage. |
| 67 | +* `increase_balance`, which reads the contract's current balance from storage, increases it by `amount` and writes the new balance to storage. |
| 68 | + |
| 69 | +== Compiling `HelloStarknet` |
| 70 | + |
| 71 | +To compile the `HelloStarknet` contract, navigate into the newly created `hello_starknet` directory and run: |
| 72 | + |
| 73 | +[source,terminal] |
| 74 | +---- |
| 75 | +scarb build |
| 76 | +---- |
| 77 | + |
| 78 | +[NOTE] |
| 79 | +==== |
| 80 | +The first time a project is built, some components of Scarb are compiled locally with the Rust toolchain. This process may take a few minutes, but will not happen in subsequent builds. |
| 81 | +==== |
| 82 | + |
| 83 | +The compiled contract should now be saved inside the `hello_starknet/target/dev/` directory as `hello_starknet_HelloStarknet.contract_class.json`. |
0 commit comments