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: errors #20

Merged
merged 5 commits into from
Jun 8, 2023
Merged

feat: errors #20

merged 5 commits into from
Jun 8, 2023

Conversation

julio4
Copy link
Member

@julio4 julio4 commented Jun 6, 2023

Close #6
I'm considering adding another practical example of a simple vault contract with a deposit and withdraw function. Please let me know if you think it's a good idea.

Additionally, I have included some security considerations that I believe are important to keep in mind. Please let me know if you find this kind of content useful.

@julio4 julio4 marked this pull request as ready for review June 6, 2023 09:37
@enitrat
Copy link
Collaborator

enitrat commented Jun 6, 2023

This is very good, however I'm a bit worried that this is a bit out of scope of just CairoByExample and some of this content should be in the cairo book instead. Especially this part

Security Considerations
Transactions in smart contracts are atomic, meaning they either succeed or fail without making any changes.
Think of smart contracts as state machines: they have a set of initial states defined by the constructor constraints, and external function represents a set of possible state transitions. A transaction is nothing more than a state transition.
Using errors to check conditions adds constraints that helps clearly define the boundaries of possible state transitions for each function in your smart contract. These checks ensure that the behavior of the contract stays within the expected limits.
By incorporating these concepts during development, you can create robust and reliable smart contracts. This reduces the chances of unexpected behavior or vulnerabilities.

I think it would be better to have this in-depth explanation part in the Cairo Book directly, and here give a very succint description and advanced example of how to use errors and condition checks in smart contracts.

I'm considering adding another practical example of a simple vault contract with a deposit and withdraw function.

Yes, it's an excellent idea.
Another thing you could do is demonstrate how we could create group errors together in an error module, and then refer to them from there.

mod Errors{
  const BALANCE_TOO_LOW = 'user balance is too low';
}

#[contract]
mod Vault{
 fn withdraw(){
  ///
  assert(amount<=balance, Errors::BALANCE_TOO_LOW);
 }
}

@julio4
Copy link
Member Author

julio4 commented Jun 7, 2023

This is very good, however I'm a bit worried that this is a bit out of scope of just CairoByExample and some of this content should be in the cairo book instead.

I agree. I might want to explain too much because I'm also learning at the same time. I'll try to keep things simple and move in-depth explanations to the Cairo book if I think it's interesting content.

@enitrat
Copy link
Collaborator

enitrat commented Jun 7, 2023

I'll try to keep things simple and move in-depth explanations to the Cairo book if I think it's interesting content.

🔥

src/introduction/errors/vaultErrors.cairo Outdated Show resolved Hide resolved
src/introduction/errors/vaultErrors.cairo Outdated Show resolved Hide resolved
src/introduction/errors/vaultErrors.cairo Outdated Show resolved Hide resolved
src/introduction/errors/vaultErrors.cairo Outdated Show resolved Hide resolved
src/introduction/errors/errors.md Outdated Show resolved Hide resolved
src/introduction/errors/errors.md Outdated Show resolved Hide resolved
src/introduction/errors/errors.md Outdated Show resolved Hide resolved
@julio4 julio4 requested a review from enitrat June 8, 2023 12:49
Copy link
Collaborator

@enitrat enitrat left a comment

Choose a reason for hiding this comment

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

LGTM 🔥

@enitrat enitrat merged commit e613412 into NethermindEth:main Jun 8, 2023
@julio4 julio4 deleted the feat/errors branch June 21, 2023 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: Errors
2 participants