Questions tagged [assembly]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
0answers
7 views

Solidity: Converting a hex-string into bytes efficiently

I need to convert a string variable (passed to a function), containing a hex-string, into bytes in Solidity. In a way, this is the same as the following, just that the string variable will be passed ...
4
votes
3answers
408 views

convert bytes to uint8[8] using mstore()

I know how to convert bytes to uint8[8] using mload but mstore seemed like it could be more gas efficient. Is something like this possible? function bytesToUintArray(bytes input) returns (uint8[8])) ...
2
votes
0answers
22 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: ...
5
votes
1answer
99 views

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
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
38 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 ...
0
votes
1answer
202 views

Return subset of variable-length array

How does one return the first x elements of an array of addresses? I'm pretty sure this can be done in assembly but could not find the reference.
1
vote
1answer
36 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
2
votes
0answers
16 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 ...
2
votes
1answer
122 views

Modifying simple function with inline assembly

I'm trying to write a super simple contract that's gas efficient utilizing inline assembly in solidity. All this function needs to do is create a child smart contract with one function, self destruct....
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 ...
5
votes
3answers
1k views

Understanding solidity inline assembly code

Here is a code from solidity documentation: function sumAsm(uint[] _data) public returns (uint o_sum) { for (uint i = 0; i < _data.length; ++i) { assembly { ...
0
votes
1answer
366 views

Factory contract to clone existing contracts

I am researching about Factory contract to clone existing contracts, First is the implementation present at https://github.com/optionality/clone-factory/blob/master/contracts/CloneFactory.sol. I don't ...
2
votes
1answer
184 views

Using 'call' from a constant function

I am trying to determine the behavior of call when executed from a constant function. Of course, the compiler issues a warning on that: Warning: Function declared as view, but this expression (...
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
15 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: ...
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 ...
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 ...
1
vote
1answer
244 views

Concatenating byte strings in assembly for call input data

I have the following piece of code in my contract. It's supposed to take in a byte string composed of a uint flag that is followed by a variable list of uint numbers and contruct a call using those ...
0
votes
0answers
69 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
65 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
403 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
37 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
414 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
339 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
128 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
16 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
139 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
53 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
60 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
182 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 ...
0
votes
0answers
35 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 ...
0
votes
1answer
346 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] ...
0
votes
1answer
66 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
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; ...
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
25 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);...
2
votes
1answer
37 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
0answers
76 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.. ...
1
vote
1answer
98 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) } } ...
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? ...
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
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, ...