If anyone wanna try a “Assert” error, can try with this function that i do :
Note : The “assert” is run when some conditions are not expected that could be,
in this case if i have 1 and subtract 1 the expected ( in assert ) is 0, but if i put 1, the
error go be executed because that is not true, this is only for test, in real cases
i think this is a good practice for the most functions, because if somebody wants to hack the function and has the assert … the transaction fails, is not the best for security but solve some cases for unexpected things
sorry for my bad english, i hope anyone can understand
function test() public {
uint256 have = 1;
uint256 sub = 1;
assert(have - sub == 1);
}
Yes… because the condition in assert() should always be true, we can’t actually demonstrate an example of assert() failing unless we “force” the condition to do the opposite… which is what your function does, and you’ve explained its purpose nicely
I would say that assert() is more definite than that, and that it checks for conditions that will never occur, unless our code fundamentally fails to do what it’s been designed to do. This is what’s called an invariance.
Rather than being used to prevent attacks aimed at manipulating weaknesses, ambiguities and/or bugs in how the smart contract code has been written, the job of assert() is more to prevent a catastrophic failure in the code’s own environment, in its “fundamental laws” so to speak.
In terms of when to use it, we need to consider what the consequences of such a fundamental failure would actually be, and weigh that up against the additional cost of gas to execute it.
It’s important to realise that certain attacks could still succeed whether or not there is an assert() statement. So, it’s also important to consider other smart-contract security measures, and not just to rely on assert(). You’ll learn about some of these other security measures later on
Once you’ve corrected the require() statement and deployed and tested your contract, you will find that when the transfer() function is called, the assert() statement is failing when it shouldn’t.
Your code is saying that the SENDER’S previous balance should be equal to their updated balance (after the transfer) LESS the amount transferred. So this will never evaluate to true. Can you see why, and then correct it?
I am currently watching the “require” lecture, and I have a small question: In the lecture, you set owner address by using a constructor and setting the owner variable to msg.sender.
why is it that we have to create a constructor to set the owner address to msg.sender?
and why can’t we just set it on the same line as you set the owner variable?
You can set the owner in both ways, either in constructor, or with address owner = msg.sender;
Constructor is optional, it is just a way to set state variable that you can´t change later. It depends on your needs, whether you want modifiable or immutable values.
The constructor will execute only one time, when the contract is deployed, there you can specify all the initialization arguments that the contract needs to execute itself properly (like who is the owner of this contract, and other stuffs like access control that you will see later in Smart Contract 201)
So in order to specify the owner of the contract, its assigned in the constructor, where msg.sender is the account that execute the contract (at this moment, msg.sender is the deployer of the contract).
You could assign an address directly in the variable declaration.
Like:
address owner = "0x0asdsad...";
It also might work, but if you want to assign the deployer (which ever address might be), its easier to just use the constructor and pass the msg.sender as the owner.