99 votes
Accepted

What OPCODES are available for the Ethereum EVM?

All of the opcodes and their complete descriptions are available in the Ethereum Yellow paper. For convenience, though, I've made a handy reference list of them all: 0s: Stop and Arithmetic Operations ...
user avatar
28 votes

understanding mload assembly function

As others have stated, the x m:=mload(0x40) instruction reads the 32 bytes of memory starting at position 0x40. In solidity, the 0x40 slot in memory is special: it contains the "free memory pointer" ...
user avatar
  • 33.9k
21 votes
Accepted

understanding mload assembly function

mload(0xAB) Loads the word (32byte) located at the memory address 0xAB. e.g mload(0x60) loads the word located at 0x60 memory address. let's code to understand more : function f () { ...
user avatar
  • 17.4k
17 votes
Accepted

What are some examples of how inline assembly benefits smart contract development?

As the author of that module, a few things that came up: Inline assembly allows you to read entire words (256 bits) from data types like string and bytes in a single operation. Solidity-stringutils ...
user avatar
  • 7,549
17 votes
Accepted

isContract function using EVM assembly to get the address' code size

EDIT: The function will return false if it is invoked from a contract's constructor (because the contract has not been deployed yet). The code should be used very carefully, if at all, to avoid ...
user avatar
  • 77.9k
15 votes
Accepted

Solidity assembly question - mstore

The explanation is quite simple: In Solidity, bytes is a dynamically-sized byte array: Variables of type bytes and string are special arrays. A bytes is similar to byte[], but it is packed ...
user avatar
  • 818
13 votes
Accepted

Understanding solidity inline assembly code

Within the assembly code, _data is the memory address of the start of the array data. However, the first memory word (32 bytes = 0x20 bytes) is reserved for the length of the array, so we need to ...
user avatar
  • 8,612
11 votes
Accepted

What is the cryptic part at the end of a solidity contract bytecode?

This is the Swarm hash. It is documented at https://solidity.readthedocs.io/en/develop/metadata.html Extract follows Contract Metadata The Solidity compiler automatically generates a JSON file, the ...
user avatar
  • 8,612
11 votes

How could msg.sender != tx.origin AND extcodesize(caller) == 0 be true?

All things must have a beginning I think you're getting called from a contract constructor before it's committed to the chain. At this point, it will have a (theoretical) address but no code yet in ...
user avatar
  • 1,308
9 votes
Accepted

What is the benefit of using create2() to create a smart contract?

CREATE opcode is used by default when deploying smart contracts. The deployed contract address is calculated like this. keccak256(senderAddress, nonce) CREATE2 opcode is introduced later and allows ...
user avatar
8 votes
Accepted

How can a factory contract clone the bytecode of an existing contract?

Martin Holst Swende published an implementation of a cloner contract here! https://gist.github.com/holiman/069de8d056a531575d2b786df3345665 Excerpt: function clone(address a) returns(address){ /...
user avatar
  • 3,420
8 votes

isContract function using EVM assembly to get the address' code size

This code snippet is correct, although it seems important to note that EXTCODESIZE will return 0 when called within the constructor of a contract, since the contract is not created yet. Hence, if msg....
user avatar
  • 472
8 votes
Accepted

Get address from public key in Solidity

This works: return address(keccak256(publicKey) & (2**(8*21)-1)); 2**(8*21)-1 is just a trick to get 0xFFFFFF... (40 Fs) without typing it. :-) EDIT As pointed out by @schnorr, there's no need ...
user avatar
  • 26.2k
7 votes
Accepted

Can I use delegatecall in a transaction?

DELEGATECALL is an instruction interpreted by EVM. So to execute DELEGATECALL you have to have a program (contract) that gets executed on EVM. DELEGATECALL takes six operands, one of them is the ...
user avatar
7 votes
Accepted

How to convert bytes to uint in Solidity

You can use this function to convert bytes to unit: pragma solidity ^0.4.23; contract mycontract { function bytesToUint(bytes b) public returns (uint256){ uint256 number; for(...
user avatar
  • 7,735
7 votes

Difference in "delegate call via assembly"

OpenZeppelin's one is the most generic and addresses contract upgrade scenario. It forwards msg.data as is to called contracts, and returns what called contract returned. MakerDAO's is also generic ...
user avatar
7 votes

Passing constructor arguments to the CREATE assembly instruction in solidity

This worked for CREATE2. Maybe it also works on CREATE? abi.encodePacked(bytecode, abi.encode(arg1, arg2)) Example (CREATE2) pragma solidity ^0.5.11; contract Wallet { address public owner; ...
user avatar
6 votes

Difference between the assembly code generated for uint and uint32?

Others have pointed in the right direction, but let me try to specifically answer the questions. First, each 256-bit word of contract storage is very expensive. When a contract is using smaller ...
user avatar
  • 8,612
6 votes
Accepted

How does this assembly code create a new "memory end"?

It explained a little later in the same documentation: Solidity manages memory in a very simple way: There is a “free memory pointer” at position 0x40 in memory. If you want to allocate memory, ...
user avatar
  • 29.3k
6 votes
Accepted

How to get access to the storage mapping through the solidity assembler?

Solution function getStorageValue(uint num, uint slot) public view returns (address result) { assembly { // Store num in memory scratch space (note: lookup "free memory pointer" if you ...
user avatar
  • 1,575
5 votes
Accepted

Help to understand assembly method

This is designed to convert an address to a dynamic bytes type. Addresses are 20 bytes long, and occupy the right-most 20 bytes of a 32-byte word: ...
user avatar
  • 8,612
5 votes
Accepted

Difference between sending a transaction with no to field and create() opcode?

Both create a contract. An externally owned account (EOA) creates a contract by sending a transaction with no to field. A contract (account) creates another contract by using the CREATE opcode. It'...
user avatar
  • 77.9k
5 votes
Accepted

Parameters on evm opcode create

This is because memory variables are treated as pointers from in solidity assembly - and that pointer points to the start in memory of the variable. The way arrays are stored in memory in Solidity is ...
user avatar
  • 10.5k
5 votes
Accepted

Instantiating a smart contract in Uniswap using assembly and create2

The lines you showed are using the new CREATE2 opcode, instead of the regular CREATE opcode (which would be invoked by using pair = new UniswapV2Pair()). The benefit of using CREATE2 is that the ...
user avatar
5 votes
Accepted

abi.encode and abi.decode arguments, including variable-sized arrays

I'm not sure what are you trying to achieve but if you are using abi.encode then you can use abi.decode. // SPDX-License-Identifier: MIT pragma solidity 0.8.4; contract A { function foo() ...
user avatar
  • 29.3k
5 votes
Accepted

Storage and memory layout of strings

Layout of State Variables in Storage > bytes and string bytes and string are encoded identically. In general, the encoding is similar to bytes1[], in the sense that there is a slot for the array ...
user avatar
  • 1,386
4 votes

What are some examples of how inline assembly benefits smart contract development?

I know this is an old topic, but here's a couple of tutorials I put together to cover assembly in solidity: Functional Assembly - https://www.youtube.com/watch?v=nkGN6GwkMzU Instructional Assembly - ...
user avatar
4 votes
Accepted

Problem using created array from assembly

The author of the github page that you referenced didn't update the free memory pointer after allocating the memory. Also as Tjaden Hess noted in the comments return as an opcode and return as a ...
user avatar
  • 10.2k
4 votes
Accepted

How to pass parameters to Contract constructor using when using assembly?

Constructor arguments are appended to the deployment byte code in the standard ABI format. For example, this contract takes uint constructor argument. contract Foo { uint a; function Foo(...
user avatar
  • 8,612
4 votes

How to convert bytes to uint in Solidity

Slicing an uint out of bytes using inline assembly function sliceUint(bytes bs, uint start) internal pure returns (uint) { require(bs.length >= start + 32, "slicing out of range"); ...
user avatar

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