64
votes
Accepted
What are the virtual and override keywords in Solidity?
As of Solidity 0.6.0, the keywords virtual and override are available natively in Solidity for function overriding. The purpose of these keywords is to be more explicit when overriding a function. ...
56
votes
Accepted
What is the receive keyword in solidity?
TL;DR
receive is a new keyword in Solidity 0.6.x that is used as a fallback function that is only able to receive ether.
receive() external payable — for empty calldata (and any value)
fallback() ...
32
votes
Accepted
What is the difference between the constant and immutable keywords in Solidity?
Both immutable and constant are keywords that can be used on state variables to restrict modifications to their state. The difference is that constant variables can never be changed after compilation, ...
26
votes
Accepted
What is the immutable keyword in Solidity?
The immutable keyword was added to Solidity in 0.6.5.
State variables can be marked immutable which causes them to be read-only, but assignable in the constructor. The value will be stored directly in ...
23
votes
Accepted
Is it possible to perform a try-catch in Solidity?
Solidity 0.6.0 and Greater (Updated 2020)
As of Solidity 0.6.0, there is try-catch functionality built into the language. The syntax is similar to existing languages, however functionality is limited ...
22
votes
Accepted
How can I slice bytes, strings, and arrays in Solidity?
Solidity 0.6.0 and Greater (Updated 2020)
As of Solidity 0.6.0, there is array slice functionality built into Solidity. The syntax is similar to existing languages in that the array takes the ...
16
votes
Accepted
Is it possible to deploy a contract using create2 in Solidity?
Yes. Solidity version 0.6.2 introduced a high-level way to use the create2 opcode. From the release docs:
When creating a contract, you can specify the salt as a "function call option": new ...
9
votes
Accepted
How to reset the length of array in solidity v6.0?
I think that'll do it.
pragma solidity 0.6.1;
contract TestNode {
uint[] nodes;
function createNode(uint data) public {
delete nodes; // This should reset the length to zero
...
8
votes
Accepted
Can anyone explain how bytes memory can be converted to address type in solidity
The expression add(bys, 32):
Returns a pointer to the actual data, which starts after the first 32 bytes in the bys array (those first 32 bytes contain the length of the bys array).
The expression ...
8
votes
Solidity ^0.6.0 Truffle Compile Error: Cannot read property ... of undefined
Turn out that Truffle v5 versions earlier than v5.1.5 won't successfully compile ^0.6.0 solidity contracts, because the solc wrapper Truffle used (directly from solc-js) was only compatible with ^0.5....
7
votes
How to reset the length of array in solidity v6.0?
Since v0.6.0 you can not resize array.length.
Member-access to length of arrays is now always read-only, even for storage arrays. It is no longer possible to resize storage arrays by assigning a new ...
7
votes
Got scammed using bad code to create front running bot
I've seen this before, and I've already answered it. Check here: Missing Eth after sending to contract
The UniswapFrontrunBot contract that you shared is a scam.
Unfortunately, your funds are lost ...
6
votes
What is the difference between the constant and immutable keywords in Solidity?
In both cases, the variables cannot be modified after the contract has been constructed.
a) For constant variables, the value has to be fixed at compile-time.
b) For immutable, value can be assigned ...
5
votes
Accepted
Solidity 0.6.0 address(this).balance throws error invalid opcode
I managed to fix it by explicitly defining the EVM version in my truffle config file. It's still weird because Remix supports this (as seen in the attached image) as well but still fails.
compilers:...
5
votes
Accepted
can anyone explain why call, delegateCall, staticCall functions in solidity are called low level functions?
They allow you to perform dynamic invocation (instead of static invocation) of a contract function.
This is similar to reflection in Java, where you execute a function using the function name string.
...
5
votes
Accepted
Is "type" a reserved keyword in Solidity?
Yes, type is a reserved keyword in Solidity, according to the source code.
However, according to the docs, type is not a reserved keyword.
With that said, the reason it is reserved is for deriving ...
5
votes
NotFoundError: Key not found in database [hn]
I encountered the same error when solving the same Ethernaut level. I'm using Remix IDE. When I changed from deploying with JavaScript VM to deploying with Injected Web3 (there's a dropdown menu in ...
4
votes
Error: Transaction was not mined within 750 seconds, please make sure your transaction was properly sent. Be aware that it might still be mined!
This is a legitimate scenario, and it is typical to cases where transactions with a higher gas price than yours have been submitted around the same time as yours.
You can extend your code to check ...
4
votes
What is the immutable keyword in Solidity?
The answer of the Shane is pretty accurate. To answer the question of the Qwerty:
I was confusing the immutable with the constant.
Here is the brief summary directly from the official Solidity ...
4
votes
delegatecall call internal functions from caller contract
No, it's not possible.
Delegatecall simply calls the function inside the context of the current contract. It's like saying "Run function A and do whatever you want in MY storage".
It has ...
3
votes
Accepted
Overriding a function
The virtual function must be in a parent contract. The override function must be in a child contract. Now you have both the functions in the same contract.
3
votes
Is it possible to deploy a contract using create2 in Solidity?
Yes. Solidity version 0.6.2 you can do it
3
votes
Can anyone explain how bytes memory can be converted to address type in solidity
You may meet the following code in many projects:
contract A {
event Log(address addr);
function() external {
emit Log(bytesToAddress(msg.data));
}
function bytesToAddress(...
3
votes
Accepted
Calling super class external functions
Turns out this is not possible.
Inheriting functions and attempting to call the parent class version is deemed as an internal call. Since an override would replace the entry point for the overridden ...
3
votes
getting data from events
Using events to get return values from functions is not the same as using return in view/pure functions. In fact you shouldn't use the keyword returns when declaring a function that results in a ...
3
votes
Accepted
What is the ':= ' operator?
It is the the variable assignment operator in Yul. From the docs:
Variables can be assigned to after their definition using the :=
operator. It is possible to assign multiple variables at the same
...
3
votes
I wonder where this hack started from
TLDR : cancel your allowance for WETH to contract 0x9bf475e0654ec615b473599c9d39ddd87b01e894 and it wont happen again.
https://polygonscan.com/address/0x9bf475e0654ec615b473599c9d39ddd87b01e894#code ...
2
votes
Accepted
How to convert uint95 to bytes32 in solidity > 5.0?
Try this:
bytes32 b = bytes32(uint256(data));
2
votes
Accepted
Public variable getter shows as transaction in Remix
The problem is related to the ABI JSON produced by the compiler and how the tools use it to determine if a function is view or pure.
As you can see in the docs:
The fields constant and payable ...
2
votes
Solidity 0.6.0 address(this).balance throws error invalid opcode
Summarizing the comments above, the change-log for compiler version 0.5.13 mentions a related fix:
Code Generator: Use SELFBALANCE opcode for address(this).balance if using Istanbul EVM.
So I'm ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
solidity-0.6.x × 109solidity × 74
remix × 17
contract-development × 13
go-ethereum × 11
solidity-0.5.x × 10
web3js × 6
solidity-0.8.x × 6
solidity-0.7.x × 6
truffle × 5
blockchain × 5
contract-design × 5
web3.py × 5
brownie × 5
keyword × 5
contract-deployment × 4
arrays × 4
chainlink × 4
python × 3
solc × 3
fallback-function × 3
truffle-compile × 3
javascript × 2
nodejs × 2
uniswap × 2