Hot answers tagged

5

require is for making sure that an illegal input has not been passed to the function. assert is for making sure that something which is never supposed to happen - does not happen. In other words, require is for when you want to protect from a possible scenario, and assert is for when you want to protect from an impossible scenario (i.e., a bug in your code)...


4

Prior to the Byzantium network upgrade, require() and assert() actually behave identically, but their bytecode output is slightly different. assert() uses the 0xfe opcode to cause an error condition require() uses the 0xfd opcode to cause an error condition A very important the primary difference between the opcode is that assert() will cost you gas while ...


3

The values you're showing are 32 bytes long (256 bits), not 64 bits. They're that length because the type is bytes32. The number 1 is different from 10, and the same holds in hexadecimal. You can't just drop the trailing zeros without changing the value. Assuming you're always working with strings, you could use web3.toAscii... e.g. assert.toEqual(web3....


3

Web3 1.2.5 supports the revert messages now, activated by setting web3.eth.handleRevert = true. You can use TestContract.methods.myMethod(myParam).call().catch(console.log) to receive the message without sending the transaction. See https://soliditydeveloper.com/web3-1-2-5-revert-reason-strings for more details.


3

There's no built-in equality test for strings (or other arrays), so a common technique is to compare the hashes of the strings instead: function feedData(string _result) public { // Compare hashes require(keccak256(bytes(_result)) == keccak256("e"), "error"); result = _result; // This line was backwards }


2

This is a misuse of assert because, depending on input, the expressions may evaluate to false. Use require() instead. assert() is meant to check for logical errors in the contract itself. Asserts state facts that must always be true under any circumstances, so any possibility of false would be a logic issue. Consider: uint balanceAlice = 10; uint ...


2

The code works perfectly on my machine. I'm able to migrate and compile the given code. The truffle version I'm using is - 3.2.5 Upgrade to this version, shall fix your issue.


1

You did not approve USDT to the router address before attempting the swap. The USDT contract uses assert instead of revert or throw to cancel the transaction because of the lack of approval. Using assert will consume all the gas left. It's bad practice by Tether to use assert, but it's probably because their contract is really old. EDIT: Also, the USDT ...


1

To begin with, you are not running your script as an unit test, so I guess it isn't meant to be one. If it's not meant to be a unit test, it probably shouldn't have asserts. You should run unit test with the truffle test functionality. Otherwise, in general, as is also said in your link, you should only have one assert per unit test. I personally don't often ...


1

assert.equal will throw an exception when the values are not equal. assert.equal("foo", "bar", "Must be equal"); // AssertionError: Must be equal If it does not throw anything, it means that your the variables are equal. Also note that you don't need a getter for public variables in Solidity. It will automatically create a ...


1

You can throw custom error messages using require() || revert() Example: require(CONDITION, "ERROR_MSG"); revert("ERROR_MSG"); I wouldn't use assert() as they spend all the left over gas. revert() || require() at least refunds any unused gas. Another example that I stole from Read The Docs: function buy(uint amount) public payable { if (...


1

You can modify this helper a little bit to check error.message should contain your message on the require https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/test/helpers/assertRevert.js


1

I fear that there may not be a fool-proof answer to this, for the simple reason that over the course of forks, gas costs can change for certain op codes (as was done to stop spam attacks some time ago). Due to the above, a hardcoded gas check may no longer be valid in the long run. When dealing with a loop function, requiring that your input is under a ...


1

Variables can contain function pointers. If you leave them uninitialized and then try to call them, the call will fail because the variable was zero-initialized and therefore it does not contain a pointer to a callable function. Example: function b(int x, int y) internal pure returns (int) { return x * y; } function test1() external pure returns (int) {...


1

First, if you are interested in checking the return value from ether transferring operation you should use send method instead of transfer which is meant to throw an exception on failure. assert(msg.sender.send(10000000)); Referring to your question: If this throws an exception, will the internal transaction that called contract one also be reversed ...


Only top voted, non community-wiki answers of a minimum length are eligible