diff --git a/listings/applications/constant_product_amm/src/contracts.cairo b/listings/applications/constant_product_amm/src/contracts.cairo index 9c93d88c..0b1bd679 100644 --- a/listings/applications/constant_product_amm/src/contracts.cairo +++ b/listings/applications/constant_product_amm/src/contracts.cairo @@ -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) @@ -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 diff --git a/listings/applications/erc20/src/token.cairo b/listings/applications/erc20/src/token.cairo index 51660642..c2477789 100644 --- a/listings/applications/erc20/src/token.cairo +++ b/listings/applications/erc20/src/token.cairo @@ -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 }); @@ -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; diff --git a/listings/applications/staking/src/contract.cairo b/listings/applications/staking/src/contract.cairo index e158c8eb..ed17e14d 100644 --- a/listings/applications/staking/src/contract.cairo +++ b/listings/applications/staking/src/contract.cairo @@ -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); @@ -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(); diff --git a/listings/applications/upgradeable_contract/src/upgradeable_contract_v0.cairo b/listings/applications/upgradeable_contract/src/upgradeable_contract_v0.cairo index e2071b92..c2aac9ca 100644 --- a/listings/applications/upgradeable_contract/src/upgradeable_contract_v0.cairo +++ b/listings/applications/upgradeable_contract/src/upgradeable_contract_v0.cairo @@ -32,7 +32,7 @@ pub mod UpgradeableContract_V0 { impl UpgradeableContract of super::IUpgradeableContract { // 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 })) } diff --git a/listings/applications/upgradeable_contract/src/upgradeable_contract_v1.cairo b/listings/applications/upgradeable_contract/src/upgradeable_contract_v1.cairo index b8402f85..78baa8b1 100644 --- a/listings/applications/upgradeable_contract/src/upgradeable_contract_v1.cairo +++ b/listings/applications/upgradeable_contract/src/upgradeable_contract_v1.cairo @@ -29,7 +29,7 @@ pub mod UpgradeableContract_V1 { #[abi(embed_v0)] impl UpgradeableContract of super::IUpgradeableContract { 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 })) } diff --git a/po/es.po b/po/es.po index 2108a25c..8443d1e9 100644 --- a/po/es.po +++ b/po/es.po @@ -2746,7 +2746,7 @@ msgid "" "# impl UpgradeableContract of super::IUpgradeableContract " "{\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" @@ -2794,7 +2794,7 @@ msgstr "" "# impl UpgradeableContract of super::IUpgradeableContract " "{\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" @@ -7780,7 +7780,7 @@ msgid "" " impl UpgradeableContract of super::IUpgradeableContract " "{\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" @@ -7826,7 +7826,7 @@ msgstr "" " impl UpgradeableContract of super::IUpgradeableContract " "{\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" @@ -7890,7 +7890,7 @@ msgid "" " impl UpgradeableContract of super::IUpgradeableContract " "{\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" @@ -7936,7 +7936,7 @@ msgstr "" " impl UpgradeableContract of super::IUpgradeableContract " "{\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" @@ -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) + " @@ -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" @@ -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) + " @@ -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" diff --git a/po/zh-cn.po b/po/zh-cn.po index a1b8bb7a..90953985 100644 --- a/po/zh-cn.po +++ b/po/zh-cn.po @@ -4269,7 +4269,7 @@ msgid "" " #[abi(embed_v0)]\n" " impl UpgradeableContract of super::IUpgradeableContract {\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" @@ -4314,7 +4314,7 @@ msgstr "" " #[abi(embed_v0)]\n" " impl UpgradeableContract of super::IUpgradeableContract {\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" @@ -4377,7 +4377,7 @@ msgid "" " #[abi(embed_v0)]\n" " impl UpgradeableContract of super::IUpgradeableContract {\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" @@ -4422,7 +4422,7 @@ msgstr "" " #[abi(embed_v0)]\n" " impl UpgradeableContract of super::IUpgradeableContract {\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" @@ -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) + " @@ -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" @@ -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) + " @@ -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" diff --git a/src/ch01/constant-product-amm.md b/src/ch01/constant-product-amm.md index 4b80861e..2ec959da 100644 --- a/src/ch01/constant-product-amm.md +++ b/src/ch01/constant-product-amm.md @@ -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}} diff --git a/src/ch01/erc20.md b/src/ch01/erc20.md index 3eaec60c..af0085ac 100644 --- a/src/ch01/erc20.md +++ b/src/ch01/erc20.md @@ -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: diff --git a/src/ch01/simple_vault.md b/src/ch01/simple_vault.md index ab465fc5..749686c5 100644 --- a/src/ch01/simple_vault.md +++ b/src/ch01/simple_vault.md @@ -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}} diff --git a/src/ch01/staking.md b/src/ch01/staking.md index dab8d3ab..2a0eb83d 100644 --- a/src/ch01/staking.md +++ b/src/ch01/staking.md @@ -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}} ``` -