Skip to content

Commit

Permalink
Application chapter: grammar, wording, typos, !addr.is_zero -> `add…
Browse files Browse the repository at this point in the history
…r.is_non_zero` (NethermindEth#213)

* upgradeable > use is_non_zero

* simple_vault > grammar

* not is_zero -> is_non_zero

* update sbe links

* staking > wording

* amm > fix brackets + grammar

---------

Co-authored-by: Nenad <[email protected]>
  • Loading branch information
2 people authored and julio4 committed Oct 24, 2024
1 parent 02c599c commit 5cc0c10
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub mod ConstantProductAmm {
assert(reserve0 * amount1 == reserve1 * amount0, 'x / y != dx / dy');
}

// How much shares to mint?
// How many shares to mint?
//
// f(x, y) = value of liquidity
// We will define f(x, y) = sqrt(xy)
Expand Down Expand Up @@ -178,11 +178,11 @@ pub mod ConstantProductAmm {
//
// Multiply by sqrt(x) / sqrt(x)
// Equation 2 = (sqrt(x^2y + 2xydx + dx^2 * y) - sqrt(x^2y)) / sqrt(x^2y)
// = (sqrt(y)(sqrt(x^2 + 2xdx + dx^2) - sqrt(x^2)) / (sqrt(y)sqrt(x^2))
// = (sqrt(y)(sqrt(x^2 + 2xdx + dx^2) - sqrt(x^2))) / (sqrt(y)sqrt(x^2))
// sqrt(y) on top and bottom cancels out
//
// --- Equation 3 ---
// Equation 2 = (sqrt(x^2 + 2xdx + dx^2) - sqrt(x^2)) / (sqrt(x^2)
// Equation 2 = (sqrt(x^2 + 2xdx + dx^2) - sqrt(x^2)) / sqrt(x^2)
// = (sqrt((x + dx)^2) - sqrt(x^2)) / sqrt(x^2)
// = ((x + dx) - x) / x
// = dx / x
Expand Down
8 changes: 4 additions & 4 deletions listings/applications/erc20/src/token.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ pub mod erc20 {
recipient: ContractAddress,
amount: felt252
) {
assert(!sender.is_zero(), Errors::TRANSFER_FROM_ZERO);
assert(!recipient.is_zero(), Errors::TRANSFER_TO_ZERO);
assert(sender.is_non_zero(), Errors::TRANSFER_FROM_ZERO);
assert(recipient.is_non_zero(), Errors::TRANSFER_TO_ZERO);
self.balances.write(sender, self.balances.read(sender) - amount);
self.balances.write(recipient, self.balances.read(recipient) + amount);
self.emit(Transfer { from: sender, to: recipient, value: amount });
Expand All @@ -188,13 +188,13 @@ pub mod erc20 {
spender: ContractAddress,
amount: felt252
) {
assert(!spender.is_zero(), Errors::APPROVE_TO_ZERO);
assert(spender.is_non_zero(), Errors::APPROVE_TO_ZERO);
self.allowances.write((owner, spender), amount);
self.emit(Approval { owner, spender, value: amount });
}

fn mint(ref self: ContractState, recipient: ContractAddress, amount: felt252) {
assert(!recipient.is_zero(), Errors::MINT_TO_ZERO);
assert(recipient.is_non_zero(), Errors::MINT_TO_ZERO);
let supply = self.total_supply.read() + amount;
self.total_supply.write(supply);
let balance = self.balances.read(recipient) + amount;
Expand Down
4 changes: 2 additions & 2 deletions listings/applications/staking/src/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub mod StakingContract {

self.reward_rate.write(rate);

// even if the previous reward duration was not finished, we reset the finish_at variable
// even if the previous reward duration has not finished, we reset the finish_at variable
self.finish_at.write(block_timestamp + self.duration.read());
self.last_updated_at.write(block_timestamp);

Expand Down Expand Up @@ -216,7 +216,7 @@ pub mod StakingContract {
}

fn send_rewards_finished_event(ref self: ContractState) {
// check if we send a RewardsFinished event
// check whether we should send a RewardsFinished event
if self.last_updated_at.read() == self.finish_at.read() {
let total_rewards = self.reward_rate.read() * self.duration.read();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub mod UpgradeableContract_V0 {
impl UpgradeableContract of super::IUpgradeableContract<ContractState> {
// ANCHOR: upgrade
fn upgrade(ref self: ContractState, impl_hash: ClassHash) {
assert(!impl_hash.is_zero(), 'Class hash cannot be zero');
assert(impl_hash.is_non_zero(), 'Class hash cannot be zero');
starknet::syscalls::replace_class_syscall(impl_hash).unwrap_syscall();
self.emit(Event::Upgraded(Upgraded { implementation: impl_hash }))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub mod UpgradeableContract_V1 {
#[abi(embed_v0)]
impl UpgradeableContract of super::IUpgradeableContract<ContractState> {
fn upgrade(ref self: ContractState, impl_hash: ClassHash) {
assert(!impl_hash.is_zero(), 'Class hash cannot be zero');
assert(impl_hash.is_non_zero(), 'Class hash cannot be zero');
starknet::syscalls::replace_class_syscall(impl_hash).unwrap_syscall();
self.emit(Event::Upgraded(Upgraded { implementation: impl_hash }))
}
Expand Down
28 changes: 14 additions & 14 deletions po/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -2746,7 +2746,7 @@ msgid ""
"# impl UpgradeableContract of super::IUpgradeableContract<ContractState> "
"{\n"
" fn upgrade(ref self: ContractState, impl_hash: ClassHash) {\n"
" assert(!impl_hash.is_zero(), 'Class hash cannot be zero');\n"
" assert(impl_hash.is_non_zero(), 'Class hash cannot be zero');\n"
" starknet::replace_class_syscall(impl_hash).unwrap_syscall();\n"
" self.emit(Event::Upgraded(Upgraded { implementation: "
"impl_hash }))\n"
Expand Down Expand Up @@ -2794,7 +2794,7 @@ msgstr ""
"# impl UpgradeableContract of super::IUpgradeableContract<ContractState> "
"{\n"
" fn upgrade(ref self: ContractState, impl_hash: ClassHash) {\n"
" assert(!impl_hash.is_zero(), 'Class hash cannot be zero');\n"
" assert(impl_hash.is_non_zero(), 'Class hash cannot be zero');\n"
" starknet::replace_class_syscall(impl_hash).unwrap_syscall();\n"
" self.emit(Event::Upgraded(Upgraded { implementation: "
"impl_hash }))\n"
Expand Down Expand Up @@ -7780,7 +7780,7 @@ msgid ""
" impl UpgradeableContract of super::IUpgradeableContract<ContractState> "
"{\n"
" fn upgrade(ref self: ContractState, impl_hash: ClassHash) {\n"
" assert(!impl_hash.is_zero(), 'Class hash cannot be zero');\n"
" assert(impl_hash.is_non_zero(), 'Class hash cannot be zero');\n"
" starknet::replace_class_syscall(impl_hash).unwrap_syscall();\n"
" self.emit(Event::Upgraded(Upgraded { implementation: "
"impl_hash }))\n"
Expand Down Expand Up @@ -7826,7 +7826,7 @@ msgstr ""
" impl UpgradeableContract of super::IUpgradeableContract<ContractState> "
"{\n"
" fn upgrade(ref self: ContractState, impl_hash: ClassHash) {\n"
" assert(!impl_hash.is_zero(), 'Class hash cannot be zero');\n"
" assert(impl_hash.is_non_zero(), 'Class hash cannot be zero');\n"
" starknet::replace_class_syscall(impl_hash).unwrap_syscall();\n"
" self.emit(Event::Upgraded(Upgraded { implementation: "
"impl_hash }))\n"
Expand Down Expand Up @@ -7890,7 +7890,7 @@ msgid ""
" impl UpgradeableContract of super::IUpgradeableContract<ContractState> "
"{\n"
" fn upgrade(ref self: ContractState, impl_hash: ClassHash) {\n"
" assert(!impl_hash.is_zero(), 'Class hash cannot be zero');\n"
" assert(impl_hash.is_non_zero(), 'Class hash cannot be zero');\n"
" starknet::replace_class_syscall(impl_hash).unwrap_syscall();\n"
" self.emit(Event::Upgraded(Upgraded { implementation: "
"impl_hash }))\n"
Expand Down Expand Up @@ -7936,7 +7936,7 @@ msgstr ""
" impl UpgradeableContract of super::IUpgradeableContract<ContractState> "
"{\n"
" fn upgrade(ref self: ContractState, impl_hash: ClassHash) {\n"
" assert(!impl_hash.is_zero(), 'Class hash cannot be zero');\n"
" assert(impl_hash.is_non_zero(), 'Class hash cannot be zero');\n"
" starknet::replace_class_syscall(impl_hash).unwrap_syscall();\n"
" self.emit(Event::Upgraded(Upgraded { implementation: "
"impl_hash }))\n"
Expand Down Expand Up @@ -8481,8 +8481,8 @@ msgid ""
" recipient: ContractAddress,\n"
" amount: felt252\n"
" ) {\n"
" assert(!sender.is_zero(), Errors::TRANSFER_FROM_ZERO);\n"
" assert(!recipient.is_zero(), Errors::TRANSFER_TO_ZERO);\n"
" assert(sender.is_non_zero(), Errors::TRANSFER_FROM_ZERO);\n"
" assert(recipient.is_non_zero(), Errors::TRANSFER_TO_ZERO);\n"
" self.balances.write(sender, self.balances.read(sender) - "
"amount);\n"
" self.balances.write(recipient, self.balances.read(recipient) + "
Expand All @@ -8507,14 +8507,14 @@ msgid ""
" spender: ContractAddress,\n"
" amount: felt252\n"
" ) {\n"
" assert(!spender.is_zero(), Errors::APPROVE_TO_ZERO);\n"
" assert(spender.is_non_zero(), Errors::APPROVE_TO_ZERO);\n"
" self.allowances.write((owner, spender), amount);\n"
" self.emit(Approval { owner, spender, value: amount });\n"
" }\n"
"\n"
" fn mint(ref self: ContractState, recipient: ContractAddress, amount: "
"felt252) {\n"
" assert(!recipient.is_zero(), Errors::MINT_TO_ZERO);\n"
" assert(recipient.is_non_zero(), Errors::MINT_TO_ZERO);\n"
" let supply = self.total_supply.read() + amount; // What can go "
"wrong here?\n"
" self.total_supply.write(supply);\n"
Expand Down Expand Up @@ -8682,8 +8682,8 @@ msgstr ""
" recipient: ContractAddress,\n"
" amount: felt252\n"
" ) {\n"
" assert(!sender.is_zero(), Errors::TRANSFER_FROM_ZERO);\n"
" assert(!recipient.is_zero(), Errors::TRANSFER_TO_ZERO);\n"
" assert(sender.is_non_zero(), Errors::TRANSFER_FROM_ZERO);\n"
" assert(recipient.is_non_zero(), Errors::TRANSFER_TO_ZERO);\n"
" self.balances.write(sender, self.balances.read(sender) - "
"amount);\n"
" self.balances.write(recipient, self.balances.read(recipient) + "
Expand All @@ -8708,14 +8708,14 @@ msgstr ""
" spender: ContractAddress,\n"
" amount: felt252\n"
" ) {\n"
" assert(!spender.is_zero(), Errors::APPROVE_TO_ZERO);\n"
" assert(spender.is_non_zero(), Errors::APPROVE_TO_ZERO);\n"
" self.allowances.write((owner, spender), amount);\n"
" self.emit(Approval { owner, spender, value: amount });\n"
" }\n"
"\n"
" fn mint(ref self: ContractState, recipient: ContractAddress, amount: "
"felt252) {\n"
" assert(!recipient.is_zero(), Errors::MINT_TO_ZERO);\n"
" assert(recipient.is_non_zero(), Errors::MINT_TO_ZERO);\n"
" let supply = self.total_supply.read() + amount; // What can go "
"wrong here?\n"
" self.total_supply.write(supply);\n"
Expand Down
24 changes: 12 additions & 12 deletions po/zh-cn.po
Original file line number Diff line number Diff line change
Expand Up @@ -4269,7 +4269,7 @@ msgid ""
" #[abi(embed_v0)]\n"
" impl UpgradeableContract of super::IUpgradeableContract<ContractState> {\n"
" fn upgrade(ref self: ContractState, impl_hash: ClassHash) {\n"
" assert(!impl_hash.is_zero(), 'Class hash cannot be zero');\n"
" assert(impl_hash.is_non_zero(), 'Class hash cannot be zero');\n"
" starknet::replace_class_syscall(impl_hash).unwrap_syscall();\n"
" self.emit(Event::Upgraded(Upgraded { implementation: "
"impl_hash }))\n"
Expand Down Expand Up @@ -4314,7 +4314,7 @@ msgstr ""
" #[abi(embed_v0)]\n"
" impl UpgradeableContract of super::IUpgradeableContract<ContractState> {\n"
" fn upgrade(ref self: ContractState, impl_hash: ClassHash) {\n"
" assert(!impl_hash.is_zero(), 'Class hash cannot be zero');\n"
" assert(impl_hash.is_non_zero(), 'Class hash cannot be zero');\n"
" starknet::replace_class_syscall(impl_hash).unwrap_syscall();\n"
" self.emit(Event::Upgraded(Upgraded { implementation: "
"impl_hash }))\n"
Expand Down Expand Up @@ -4377,7 +4377,7 @@ msgid ""
" #[abi(embed_v0)]\n"
" impl UpgradeableContract of super::IUpgradeableContract<ContractState> {\n"
" fn upgrade(ref self: ContractState, impl_hash: ClassHash) {\n"
" assert(!impl_hash.is_zero(), 'Class hash cannot be zero');\n"
" assert(impl_hash.is_non_zero(), 'Class hash cannot be zero');\n"
" starknet::replace_class_syscall(impl_hash).unwrap_syscall();\n"
" self.emit(Event::Upgraded(Upgraded { implementation: "
"impl_hash }))\n"
Expand Down Expand Up @@ -4422,7 +4422,7 @@ msgstr ""
" #[abi(embed_v0)]\n"
" impl UpgradeableContract of super::IUpgradeableContract<ContractState> {\n"
" fn upgrade(ref self: ContractState, impl_hash: ClassHash) {\n"
" assert(!impl_hash.is_zero(), 'Class hash cannot be zero');\n"
" assert(impl_hash.is_non_zero(), 'Class hash cannot be zero');\n"
" starknet::replace_class_syscall(impl_hash).unwrap_syscall();\n"
" self.emit(Event::Upgraded(Upgraded { implementation: "
"impl_hash }))\n"
Expand Down Expand Up @@ -4961,8 +4961,8 @@ msgid ""
" recipient: ContractAddress,\n"
" amount: felt252\n"
" ) {\n"
" assert(!sender.is_zero(), Errors::TRANSFER_FROM_ZERO);\n"
" assert(!recipient.is_zero(), Errors::TRANSFER_TO_ZERO);\n"
" assert(sender.is_non_zero(), Errors::TRANSFER_FROM_ZERO);\n"
" assert(recipient.is_non_zero(), Errors::TRANSFER_TO_ZERO);\n"
" self.balances.write(sender, self.balances.read(sender) - "
"amount);\n"
" self.balances.write(recipient, self.balances.read(recipient) + "
Expand All @@ -4987,14 +4987,14 @@ msgid ""
" spender: ContractAddress,\n"
" amount: felt252\n"
" ) {\n"
" assert(!spender.is_zero(), Errors::APPROVE_TO_ZERO);\n"
" assert(spender.is_non_zero(), Errors::APPROVE_TO_ZERO);\n"
" self.allowances.write((owner, spender), amount);\n"
" self.emit(Approval { owner, spender, value: amount });\n"
" }\n"
"\n"
" fn mint(ref self: ContractState, recipient: ContractAddress, amount: "
"felt252) {\n"
" assert(!recipient.is_zero(), Errors::MINT_TO_ZERO);\n"
" assert(recipient.is_non_zero(), Errors::MINT_TO_ZERO);\n"
" let supply = self.total_supply.read() + amount; // What can go "
"wrong here?\n"
" self.total_supply.write(supply);\n"
Expand Down Expand Up @@ -5162,8 +5162,8 @@ msgstr ""
" recipient: ContractAddress,\n"
" amount: felt252\n"
" ) {\n"
" assert(!sender.is_zero(), Errors::TRANSFER_FROM_ZERO);\n"
" assert(!recipient.is_zero(), Errors::TRANSFER_TO_ZERO);\n"
" assert(sender.is_non_zero(), Errors::TRANSFER_FROM_ZERO);\n"
" assert(recipient.is_non_zero(), Errors::TRANSFER_TO_ZERO);\n"
" self.balances.write(sender, self.balances.read(sender) - "
"amount);\n"
" self.balances.write(recipient, self.balances.read(recipient) + "
Expand All @@ -5188,14 +5188,14 @@ msgstr ""
" spender: ContractAddress,\n"
" amount: felt252\n"
" ) {\n"
" assert(!spender.is_zero(), Errors::APPROVE_TO_ZERO);\n"
" assert(spender.is_non_zero(), Errors::APPROVE_TO_ZERO);\n"
" self.allowances.write((owner, spender), amount);\n"
" self.emit(Approval { owner, spender, value: amount });\n"
" }\n"
"\n"
" fn mint(ref self: ContractState, recipient: ContractAddress, amount: "
"felt252) {\n"
" assert(!recipient.is_zero(), Errors::MINT_TO_ZERO);\n"
" assert(recipient.is_non_zero(), Errors::MINT_TO_ZERO);\n"
" let supply = self.total_supply.read() + amount; // What can go "
"wrong here?\n"
" self.total_supply.write(supply);\n"
Expand Down
2 changes: 1 addition & 1 deletion src/ch01/constant-product-amm.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Constant Product AMM

This is the Cairo adaptation of the [Solidity by example Constant Product AMM](https://solidity-by-example.org/defi/constant-product-amm/).
This is the Cairo adaptation of the [Solidity by Example - Constant Product AMM](https://solidity-by-example.org/defi/constant-product-amm/).

```rust
{{#include ../../listings/applications/constant_product_amm/src/contracts.cairo:ConstantProductAmmContract}}
Expand Down
4 changes: 2 additions & 2 deletions src/ch01/erc20.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

Contracts that follow the [ERC20 Standard](https://eips.ethereum.org/EIPS/eip-20) are called ERC20 tokens. They are used to represent fungible assets.

To create an ERC20 conctract, it must implement the following interface:
To create an ERC20 contract, it must implement the following interface:

```rust
{{#include ../../listings/applications/erc20/src/token.cairo:interface}}
```

In Starknet, function names should be written in *snake_case*. This is not the case in Solidity, where function names are written in *camelCase*.
In Starknet, function names should be written in _snake_case_. This is not the case in Solidity, where function names are written in _camelCase_.
The Starknet ERC20 interface is therefore slightly different from the Solidity ERC20 interface.

Here's an implementation of the ERC20 interface in Cairo:
Expand Down
4 changes: 2 additions & 2 deletions src/ch01/simple_vault.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Simple Defi Vault

This is the Cairo adaptation of the [Solidity by example Vault](https://solidity-by-example.org/defi/vault/).
This is the Cairo adaptation of the [Solidity by Example - Vault](https://solidity-by-example.org/defi/vault/).
Here's how it works:

- When a user deposits a token, the contract calculates the amount of shares to mint.

- When a user withdraws, the contract burns their shares, calculates the yield, and withdraw both the yield and the initial amount of token deposited.
- When a user withdraws, the contract burns their shares, calculates the yield, and withdraws both the yield and the initial amount of tokens deposited.

```rust
{{#include ../../listings/applications/simple_vault/src/simple_vault.cairo}}
Expand Down
24 changes: 13 additions & 11 deletions src/ch01/staking.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@ The following staking contract is designed to allow users to stake tokens in exc
### Key Features:

1. Token staking and unstaking:
- Users can stake a ERC20 token, specified at deployment.
- Users can withdraw their staked tokens at any time.

- Users can stake an ERC20 token, specified at deployment.
- Users can withdraw their staked tokens at any time.

2. Reward calculation and distribution:
- The rewards are distributed as an ERC20, also specified at deployment (can be different from the staking token).
- Rewards are calculated based on the duration of staking and the amount the user staked relative to the total staked amount by all users.
- A user’s reward accumulates over time up until the reward period's end and can be claimed anytime by the user.

- The rewards are distributed as an ERC20, also specified at deployment (can be different from the staking token).
- Rewards are calculated based on the duration of staking and the amount the user staked relative to the total staked amount by all users.
- A user’s reward accumulates over time up until the reward period's end and can be claimed anytime by the user.

3. Dynamic reward rates:
- The reward rate is determined by the total amount of reward tokens over a set period (duration).
- The reward rate can be adjusted during the rewards period if new rewards are added before the current reward period finishes.
- Even after a reward period finishes, a new reward duration and new rewards can be setup if desired.

- The reward rate is determined by the total amount of reward tokens over a set period (duration).
- The reward rate can be adjusted during the rewards period if new rewards are added before the current reward period finishes.
- Even after a reward period finishes, a new reward duration and new rewards can be set up if desired.

4. Ownership and administration:
- Only the owner of the contract can set the rewards amount and duration.
- Only the owner of the contract can set the rewards amount and duration.

> The reward mechanism ensures that rewards are distributed fairly based on the amount and duration of tokens staked by each user.
The following implementation is the Cairo adaptation of the [Solidity by example Staking Rewards contract](https://solidity-by-example.org/defi/staking-rewards/), with a little adaptation allowing to keep track of the amount of total distributed reward tokens in order to emit an event when the remaining reward tokens amount falls down to 0.
The following implementation is the Cairo adaptation of the [Solidity by Example - Staking Rewards contract](https://solidity-by-example.org/defi/staking-rewards/). It includes a small adaptation to keep track of the amount of total distributed reward tokens and emit an event when the remaining reward token amount reaches 0.

```rust
{{#include ../../listings/applications/staking/src/contract.cairo}}
```

0 comments on commit 5cc0c10

Please sign in to comment.