Questions tagged [assembly]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1
vote
0answers
15 views

Smart contract function signature only 1 byte

I have noticed some contract calls only contain the function signature in the 1st byte of the input data. For example: ...
0
votes
1answer
16 views

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

I'm passing some arguments to a function and abi.encode()-ing them. Then I pass this data to another function where I abi.decode() it, but Im not entirely sure how to do it: Arguments I'm encoding: ...
1
vote
1answer
34 views

Understanding Solidity Inline Assembly

I am new to inline assembly. I have tried understanding different opcodes, and understood quite a bit, but still not be able to correlate them in context to STATICCALL opcode. Kindly help me ...
1
vote
1answer
35 views

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

Why would someone use create2() inline assembly code to create a child contract in factory pattern as opposed to using newContract() to create an instance of "newContract"? What are the ...
2
votes
0answers
15 views

Struct getter and Setter using inline assembly in solidity

I have a storage slot defined bytes32 internal constant USER_STRUCT_SLOT = 0xd722976edd762e336e189ef10d2d356b8698dc8a2e03a97b539ab9d3974a1630; struct UserData { uint256 level; ...
1
vote
0answers
19 views

Compile Solidity to assembly keeping a correspondence with the source code

I would like to compile a contract written in Solidity (or even just the part relating to a single method) to study it and understand if I can optimize it. I already managed to get the bytecode as ...
1
vote
1answer
33 views

What does add(source, 32) do in solidity?

I understand what add() does. It used used in the assembly to perform additions. My question is why are we adding 32 here? What is the purpose of doing so? The full code is here
1
vote
0answers
14 views

Inline assembly function parameter has same name with local variable

contract Test { function f() public returns(uint r) { uint x = 100; assembly { function f(x) -> y { y := add(x, 1) } r := f(100) } } } Use ...
2
votes
0answers
20 views

Why use assembly in this operation?

I'm trying to understand why assembly for this sum is required? // 1 _structHash = keccak256( abi.encode( _blockhash, totalAddresses, ...
2
votes
0answers
6 views

Is it possible to call a higher-level function from within assembly?

Take the following code: contract Foo { function increment(uint256 x) external returns (uint256 result) { result = x + 1; } function increment_twice(uint256 x) external ...
1
vote
0answers
13 views

What is the neatest way to get hold of max uint256 in assembly?

Since Solidity v0.6.8, the neatest way to get hold of max uint256 in Solidity is this: uint256 foo = type(uint256).max; Is there something like that for Yul? I couldn't find anything in the docs. I ...
0
votes
1answer
14 views

Do the Solidity v0.8 overflow checks apply to assembly operations too?

Since Solidity v0.8, the compiler has checks for {over,under}flow by default for all arithmetic operations. Do these checks apply to assembly (yul) arithmetic operations too?
1
vote
2answers
40 views

How to assign to negative numbers in yul?

Take this code snippet: function powOfTen(uint256 x) internal pure returns (int256 result) { assembly { switch x case 1 { result := -18 } } } Which doesn't compile: ...
1
vote
0answers
16 views

How to forward returned bytes from calls executed with assembly?

I would like to make an external call with assembly, but am having trouble retrieving the return data as "bytes". The code below currently returns only 32 bytes, which just shows the length ...
0
votes
0answers
65 views

test return value of transaction

contractA function test() internal { stateVariable = 20; uint num = 20; assembly { mstore(0x00, num) return(0x00, 0x20) } } How do I test the return ? I am using hardhat , but i don'...
3
votes
1answer
62 views

Can abi.encodeWithSignature be optimized by pre-compute?

I have a proxy implementation with method exchange and delegator with method exchange2 with the following code: function exchange2(int128 i, int128 j, uint256 dx, uint256 min_dy) public { (...
1
vote
1answer
391 views

What programming languages was Uniswap's open-source code written in?

Uniswap is a decentralized exchange that is actually a decentralized app where people can trade ERC-20 tokens without a middleman. Uniswap's code is open-source: https://github.com/Uniswap Was the ...
0
votes
0answers
16 views

how to access the element of struct in array and mapping using assembly?

how to access the element of struct in array and mapping using assembly? my code case: contract Test { struct User { address addr; uint256 tokenAmount; } mapping (uint256 =...
0
votes
0answers
35 views

staticcall - what does this code do?

I'm lost, does anyone understand this code? assembly { success := staticcall(sub(gas(), 2000), 7, input, 0x80, r, 0x60) // Use "invalid" to make gas ...
1
vote
1answer
395 views

How much C++ is needed to write smart contracts?

Solidity seems to be written in C++ so does that mean that smart contracts require some level of C++ programming skills to get by? How much background knowledge is required for writing a simple one or ...
1
vote
1answer
330 views

Instantiating a smart contract in Uniswap using assembly and create2

I am studying Uniswap source code and there is something I do not understand in this file: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2Factory.sol: This factory can ...
0
votes
1answer
124 views

Why chainid is done in assembly in UniswapV2ERC20

I am studying uniswap solidity source code and there is something i do not understand in UniswapV2ERC20 constructor: uint chainId; assembly { chainId := chainid } (pasted from https://github.com/...
1
vote
0answers
15 views

How do I get solcjs to compile yul

I want to write a smart contract using pure Yul but I have no idea how I'm meant to configure / run solcjs to compile my yul code down to bytecode. Any pointers would be much appreciated.
0
votes
1answer
133 views

Encode data into a single bytes variable using assembly

I'm currently testing a few things using the Solidity assembly. Basically, what I want to do is Pack several variables of different type together into one bytes array i.e. similar to what abi....
0
votes
0answers
52 views

How to pass a non-deployed Smart Contract to a deployed contract and call a function on it?

I'm currently trying to pass a Smart Contract (which is NOT deployed) in byte code format as an argument to another Smart Contract (which is deployed). From within the deployed contract I want to run ...
0
votes
1answer
59 views

Is it possible to access struct field from in-line assembly (Yul) when using ABIEncoderV2?

Is it possible to access struct field from Yul? Ideally it could look like this: pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; contract Sample { struct S { uint256 n; ...
0
votes
2answers
176 views

Parameters for delegatecall in inline assembly

I am trying to perform a delegate call from inline-assembly and I am unable to wrap my head around how to access the memory variables assigned outside the assembly block. function() external payable { ...
0
votes
0answers
12 views

How to deploy a compiled stateless contract and which doesn t have creation code/transaction?

I got a deployed contract bytecode from an internal project which I d like to publish on the mainnet. The problem is it s just the bytecode without the creation code. Is there a simpler way to deploy ...
5
votes
1answer
87 views
+100

Copy and deploy a smart contract from opcodes without source code

I extracted opcodes from an already existing and deployed smart contract and I would like to know if it is possible to deploy an exact copy of this contract from its opcodes? My extracted opcodes ...
0
votes
0answers
32 views

Unable to reference local variable in-line assembly

I am unable to compile a contract that references a local variable in in-line assembly. Here is the code: pragma solidity ^0.5.0; /** * @dev Contract module that helps prevent reentrant calls to a ...
2
votes
1answer
81 views

Most gas efficient way to check if byte array contains zeroes?

I have a contract that I pass data packed into an array of uint256 (as it's apparently cheaper to use uint256 over bytes32) and each byte in the uint256 contains positions referencing another array of ...
0
votes
1answer
325 views

how do i pass by reference?

I have a couple of questions. First: So, In a function, i created memory array. I want to pass this memory by reference to another function.. function test2() public returns(uint a) { uint[2] ...
1
vote
0answers
53 views

copy array from storage to memory

uint[2] testStorage; function assemblyStorage() public returns (uint a, uint b){ testStorage[0] = 5; testStorage[1] = 10; uint[2] memory testMemory = testStorage; ...
0
votes
1answer
65 views

why does this contract ask to increase gas limit?

string test; function assemblyStorage() public payable returns (string memory a){ test = "good"; assembly { a:=sload(0) } } I am trying to return the string ...
1
vote
0answers
22 views

why do i have to specify memory in arguments?

My function body looks like this: function element(uint a, uint b, uint[2] memory c) public returns(uint e, uint f, uint g, uint k) { I am wondering why do I put memory keyword before c ? I know that ...
1
vote
0answers
24 views

why mload(0) returns wrong number?

int x; // Storage slot #0 mapping (uint => uint) y; // Storage slot #1 uint [] z; // Storage slot #2 function element() public returns (uint a, uint b, uint c) { z.push(45);...
1
vote
0answers
74 views

how do storage and memory variable work?

So, in an ordinary programming language, we have stack and heap. Reference types such as array gets stored in heap. and the variable which holds the address(pointer) of this array is stored in stack.. ...
2
votes
1answer
36 views

where does function variables get created?

function element() public returns (uint) { uint test = 20; return test; } Where would test variable be created ? in memory or stack ? The funny thing is from testing assembly, I can see that it'...
1
vote
1answer
96 views

sload. assembly with struct - not working in solidity

struct S { uint128 a; uint128 b; } S test1; function assemblyStorage() public returns (uint a, uint b, uint c, uint d, uint f, uint g){ test1 = S(5,10); assembly { a:=sload(0) } } ...
1
vote
0answers
19 views

Trying to understand the ethereum stack

I've been trying to understand the limits of the stack and how it works. It seems like the "Stack too deep" error only happens if there is an assembly block in the function. Without an ...
0
votes
1answer
30 views

Solidity 0.6.x - when is it mandatory to use assembly (with examples)?

Solidity's development is very active and, if compared to several other codes, stable releases are updated quite frequently. Which cases need assembly as mandatory feature for Solidity release 0.6.x? ...
0
votes
0answers
13 views

Can I put the inline assembly code as function argument string?

Assume there is an example as below: pragma solidity >=0.4.11 <0.6.0; contract C { uint b; function f(uint x) public view returns (uint r) { assembly { r := mul(x, ...
1
vote
0answers
36 views

Conditional in assembly

I am trying to use a conditional (like an if statement) in assembly. The only assembly reference site I have ever been able to find says the opcode JUMPI is the way to do this. Using solc 0.7.0, the ...
0
votes
1answer
101 views

How to get data location for string > 31 length

The documentation says: bytes and string are encoded identically. For byte arrays that store data which is 32 or more bytes long, the main slot stores length * 2 + 1 and the data is stored as usual ...
1
vote
2answers
249 views

In assembly… can't get delegatecall to work

Delegatecall can be used in either solidity or assembly. In solidity, I can get it working with the following code: bytes memory payload = abi.encodeWithSignature ("get_value(uint16,uint16)",...
0
votes
2answers
60 views

How do i retrieve Eth from a contact that i created?

New to blockchain.. havent a clue.. I managed to create a new contract with Eth. I moved money from MyEtherum Wallet to Metamask and into a contract, somehow? I have no coding experience but need ...
2
votes
1answer
125 views

Parameters on evm opcode create

Why the usual way to create a contract with evm opcodes use add and mload, for example: bytes memory bytecode = hex"600580600c6000396000b800602a600052b8"; ... assembly { _address := ...
2
votes
1answer
63 views

Get return value of a low level call

I'd like to get the return value of a low level call. (bool success, ) = address(0x1234...7890).call{value:0}(callData); This post discusses a solution that sounds like it should work, but it doesn'...
1
vote
1answer
77 views

How to call sha256 in assembly

I wanna compute sha256 in Solidity Assembly for hash of getting bytes slice.
3
votes
1answer
252 views

Why does Gnosis' MultiSig passes less gas than available in external_call?

Gnosis' MultiSig Wallet substract a fixed amount of gas (34710) when calling an external address. function external_call(address destination, uint value, uint dataLength, bytes data) internal returns ...