Hot answers tagged

6

Updated 2020 Solidity 0.6.8 introduced min and max keywords that can now natively tell you the min and max of an expected type. From the release page: Implemented type(T).min and type(T).max for every integer type T that returns the smallest and largest value representable by the type. You can try it out with the following code. Note that the uint256 ...


5

As far as I'm aware there is no SHA256 opcode. I tried finding one but couldn't find - not listed at least in, for example, this list: https://github.com/crytic/evm-opcodes Maybe you're mixing it up with sha3 (keccak256)? It's a different algorithm. So if you want to use SHA256 you need to call the precompiled contract. But if you're worried about gas ...


4

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 with the first 0x20 (32) bytes being the length of the data in the array, and then the actual data directly following, which is why add(bytecode, 0x20) is ...


3

In Ethereum 1.0, yes, it is a miner who includes the transaction in the blockchain. Read about Proof-of-work (Pow). In Ethereum 2.0, it is a "validator" who includes the transaction in a block of a shard. Read about Proof-of-stake (PoS). At the time of writing this answer, Ethereum 1.0 is still the "main" network. Ethereum 2.0 kicked off ...


3

Yep, but it is tricky: pragma solidity ^0.5.0; contract D { function g(uint256 a) public view returns(uint256) { return a*2; } } contract A { D public impl; constructor() public { impl = new D(); } function() external { require(msg.sender == address(this)); (bool success, bytes memory data) = ...


3

The gas cost would be 200. There is actually a comment in the source code of Geth referring to this exact behavior. In protocol_params.go on line 46 we see the following line of code: NetSstoreNoopGas uint64 = 200 // Once per SSTORE operation if the value doesn't change. Source: https://github.com/ethereum/go-ethereum/blob/master/params/protocol_params.go


3

First of all, you don't need to add the gas-price into your computation. This factor is chosen by you (the transaction sender), so there are no questions about it. The only part in question is the 64244 gas units used in the transaction. According to the white-paper, you have determined that it should be 21000 + 68 * 7 = 21476: 21000: Paid for every ...


2

An update to @eth's example for solidity v6: function definitions must be public keccak256 in place of sha3 call arguments use abi.encode() address() to get contract address pragma solidity ^0.6.0; contract D { uint public n; address public sender; function callSetN(address _e, uint _n) public { _e.call(abi.encode(bytes4(keccak256("...


2

The following test implies that the cost of storing the same value is 800 gas units: Solidity Contract: pragma solidity 0.6.12; contract MyContract { uint256 public gasUsed; uint256 public storageSlot; function func(uint256 x) public { storageSlot = x; uint256 gasLeft = gasleft(); storageSlot = x; gasUsed = ...


1

I think what you're looking for is something that will create an intermediate representation (IR) between Solidity and EVM bytecode, and which will act as a high-level assembly equivalent which you can instrument, rather than instrumenting the EVM bytecode itself. (There are a few common tools for doing this in the wider world, such as LLVM which is used in ...


1

It works the same as any other transactions: You can call a view or pure function in a transaction, and the transaction can be mined as usual. It just won't do anything of course, since it's a view or pure function.


1

As a general heuristic, ordering strings is something you almost certainly don't want to do in a smart contract. I realize you might be somewhat taken aback by that assertion. This post will explain the reasoning. https://blog.b9lab.com/the-joy-of-minimalism-in-smart-contract-design-2303010c8b09 The gist is that sorting is often part of a strategy to find ...


1

When you submit a transaction to the network, it is distributed to all/most nodes of the network. Some of them are miners. Some of the miners pick your transaction to include it into the next block they intend to propose. In this process, they execute the transaction, including the smart contracts that are called. Then the miners try to find a suitable hash ...


1

I solved the problem. The mistake was that I was getting the wrong address in testing.js before function. Wrong version: before( async() => { instance_factory = await Factory.new() instance_main = await Main.new(Factory.address); }); Correct version: before( async() => { instance_factory = await Factory.new() ...


1

Thank you to the answers by @technicallyty and @goodvibration, and @goodvibration's continuation and approach is correct. Yes, storing the same value costs 800 gas. EIP-2200 and the relevant code is actually the next function gasSStoreEIP2200 More precisely the code: if current == value { // noop (1) return params.SloadGasEIP2200, nil } which is ...


1

Ethereum commonly uses m/44'/60'/0'/0/0, but you can use other derivation paths. Using a BIP-49 or BIP-84 (segwit) derivation path works just like normal, assuming that you have an application that can derive Ethereum addresses from one of those paths. In the end, you still have a normal Ethereum address where you can send ETH to.


1

Sha256 is one of a few precompiled smart contracts: function subSha256(bytes memory data, uint256 offset, uint256 length) public view returns(bytes32) { bytes32[1] memory result; assembly { pop(staticcall(gas, 0x02, add(add(data, 32), offset), length, result, 32)) } return result[0]; }


1

There is a very efficient algorithm for reversing fixed-2^n-length integers: function reverse(uint256 input) internal pure returns (uint256 v) { v = input; // swap bytes v = ((v & 0xFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00) >> 8) | ((v & ...


1

As Laurie mentioned, the effect of selfdestruct() is that it creates a void. Users and contracts may think there is a contract there (because there was) and they may assume that success means the functions worked as expected, but ... As you have discovered on your own, calling the non-existent function in the non-existent contract returns true because ...


1

It is not a single commit in a git, as there are at least two different Ethereum clients (Go Ethereum, Parity) - implementing these changes. I would say a better place to dig into this history is EIPs and see what are finals and which where the forks they were released in: https://eips.ethereum.org/


1

Contracts are compiled to opcodes before they are deployed on the network. You can't deploy Solidity code to the network directly. The different clients only run the code, they don't compile it. The gas cost for each opcode is defined in the Ethereum Yellowpaper (see Appendix G), so the gas cost is the same for the different clients (assuming they are ...


1

No, it is not possible since such opcode doesn't exist. There's no opcode that can identify a transaction within a block from the EVM side.


1

Which storage slots are allocated for which contract variable depends on the language and/or compiler. The Solidity compiler has an option to output the storage layout as a JSON structure: https://solidity.readthedocs.io/en/latest/internals/layout_in_storage.html#json-output In the Solidity language you can use .slot in assembly to get the storage slot of a ...


1

The function changes execution according to signature passed if (bytes(signatures[i]).length == 0) { callData = data[i]; } else { callData = abi.encodePacked(bytes4(keccak256(bytes(signatures[i]))), data[i]); } If it is empty it will assume the function call is contained in data[i]. You would have to use web3.eth.abi....


1

The expression 5 / 6 is interpreted by the compiler as a rational constant (fixed or ufixed). Therefore, such constant value cannot be assigned to an integer variable (uint or int). On the other hand, when you do: uint x = 5; x /= 6; The compiler replaces the / operation with the opcode for integer division, which is perfectly valid. You can read more about ...


1

A contract with two storage variables, one public and one private. pragma solidity 0.7.0; contract Test { uint256 public a = 10; uint256 private b = 20; } Variable a is stored at slot 0 and can be accessed using web3.eth.getStorageAt( "address of contract", 0, ( err, slot_content ) => console.log(err, ...


1

Short answer: No. Long Answer: If you know the code of the function you want to call (e.g. in case of a ERC20 token) you can compile it again to get its signature / part of the ABI. Then just call the function as you normally would.


1

I don't think there is much official information on this as these are business secrets. But the only reasonable way for exchanges to work is something similar to the following. 1) They do not use actual blockchain wallets like you think. When you send tokens to an exchange address it's either a common wallet for many transfers or a pass-through wallet which ...


1

That statement doesn't make much sense. In solidity 0.5 gas is the remaining gas. With sub(gas, 34710) it would seem they want to send all the remaining gas minus 34710 to the called contract, but from the comment it appear they want to send at least 34710. The error is trying to say that in solidity 0.6 you have to use gas() as it were a function. I will ...


1

This is not a direct answer to the ops question, but for all others that stumble over this question while searching for how to get the error reason generated by a failed solidity require in javascript: It depends on wether you are using web3.js or ethers.js to call the contract web3: // invalid index, should trigger: require(_i < Donors.length, "...


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