Hot answers tagged

13

It states in the v0.5 breaking changes list that: Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables. The docs are implicit in stating that strings and bytes are included - as they are array implementations, rather than an error in the compiler.


13

From this: pragma solidity^0.5.0; contract Contract { string public name; function Contracts(string passedName) public { name = passedName; } function setName(string newName) public { name = newName; } } To This: pragma solidity^0.5.0; contract Contract { string public name; function Contracts(string ...


12

As of Solidity 0.5.0. https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables. For example, change uint[] x = m_x to uint[] storage x = m_x, and function f(uint[][] x) to function f(uint[...


8

Since solidity@0.4.22, you can use abi.encodePacked() for this, which returns bytes. For example ; contract C { function toBytes(bytes32 _data) public pure returns (bytes) { return abi.encodePacked(_data); } }


7

this represents a "contract instance" object for the current contract. The balance function is part of the "address" object. You used to be able to do this.balance because solidity would allow implicit access to the address functions through the contract instance object. However, as of Solidity 0.4.22, the recommended practice is to use address(this)....


7

What you've got in bytecode here is the contract's constructor / construction bytecode. When you create a contract, the constructor runs, handles any initial arguments or statements you make, and also "creates" the code of the contract. The way this is done is through a return statement. The deployed contract code will be whatever the constructor returns. ...


7

The reason it is not being read is because this version of Metamask enables privacy mode by default. What this does is require you to approve the application to view/use your wallets on Metamask. For your specific issue, you have not yet approved Remix to use your accounts, therefore it cannot read your addresses, as you are seeing. To fix this, enable ...


6

An uint is short for uint256, so it can store 2^256 values - because it's unsigned the maximum value is 2^256-1 (zero needs one space). What is the maximum input value for function uint256 parameter? An int is short for int256 and it can store the same amount of values - because it's signed the maximum (positive) value is 2^256 / 2 - 1. So the maximum ...


6

You can combine these two values into one hash like this then use it as the mapping key: keccak256(<contractAddress>, <nftID>) Another option is to have mappings of mappings. So: mapping(address => mapping(uint => myStateData))


5

I got help from Veniamin. I had error "Error: Couldn't decode from ABI: 0x." when call get request with wrong contract address I made a mistake... I was using 'account address', instead of 'contract address'! The code works, once correct 'contract address' was used.


4

Here's a function that anyone can call to make themselves owner: function HTX () public { owner = msg.sender; uint256 devTokens = 1000000000e8; distr(owner, devTokens); } This was probably meant to be a constructor. In current versions of Solidity, this should be: constructor() public { ... but it's also still acceptable (but deprecated) ...


4

A little clean-up. You need Test2 defined before Test1 inherits from it, so the order is reversed. Add public to functions to avoid warning about default visibility. Constructor in Test1 has nothing else to do, so removed. Since it inherits from Test2, the Test2 constructor will run. In case it is not clear, Test1 will have a state variable called ...


4

The opcodes that exist to call into a contract (call, delegatecall, staticall) take no parameter to jump to a location in the bytecode. All of these calls will begin execution at the very beginning of the bytecode and there is no way to alter this.


3

web3 also has a function to estimate the gas needed, you could do something like: var gasEstimate = web3.eth.estimateGas({data: bytecode}); var contract = web3.eth.contract(abi); var instance = contract.new({data: bytecode, from: from, gas: gasEstimate}); var receipt = web3.eth.getTransactionReceipt(instance.transactionHash);


3

If the contract is verified in Etherscan, the following script fetches the ABI and saves it to a JSON file. Use it like $ python fetch_abi.py <contract address> -o <target JSON file> fetch_abi.py #!/usr/bin/python import argparse import requests import json # Exports contract ABI in JSON ABI_ENDPOINT = 'https://api.etherscan.io/api?module=...


3

I've upgraded a part of codes from @Richard Horrocks. First, just copy to clipboard this 'localStorage' > copy(localStorage); You can check beautifully json codes. and if you restore your sol files, you can this after pasting localStorage code to input objRemix variable. var objRemix = {} Object.keys(objRemix).forEach(function (k) { localStorage....


3

In your code, the line: target.call(bytes4(keccak256("withdraw()")),amount); Should be: target.call(bytes4(keccak256("withdraw(uint256)")),amount); Hope this helps you.


3

I'm not sure if that kind of construct would work in any language (none that I know of) but I also fail to see what would be the point. I can't see many situations where someone would want to push the same value multiple times into an array. Maybe something like initializing an array with all true boolean values. Because commands are executed from left to ...


3

owner[msg.sender] will give you the uint value from the owner map given the address key. Since you have not set any value, the map will return the default values which is 0 for the uint (alias to uint256) type. msg.sender.balance returns the ETH balance in wei denomination of the sender account


3

It's not really easy to test you contract code over and over again in Remix. You'd have to fill in all the fields and click the buttons every time to do so. Using an IDE like vscode gives you the advantage that you can automate testing. In bigger projects, this is a huge advantage.


3

I fixed the similar error by deleting .json files from build/contracts folder and then running truffle migrate in the terminal. However, the error occurred in Visual Studio Code, not Remix IDE. It appeared after running truffle migrate --reset.


3

The type of check you are performing is not only useless but it's plain wrong. When a user issues a transaction with 90 Ethers as value, the 90 Ethers (plus gas) is immediately deducted from the account. The 90 Ethers becomes then visible in address(this).balance. Therefore, also, the sender's balance is also updated and can be seen with address(msg.sender)....


3

I know you have got the right answer, however just posting it with the code snippet that execute fine, after adding memory keyword - with solidity version 0.5.1: It is both setter and getter pragma solidity '0.5.1'; contract MyFirstContract { uint productid; string public productname; function setname (string memory name1) public { ...


3

Make sure contractAddress is of type address payable. And yes, you'll need to provide an argument. E.g.: address payable contractAddress = ...; contractAddress.call.value(1 ether).gas(53000)("");


3

Check @goodvibration answer. It should work in most instances. I don't recommend the answer that I will share below, it will ignore the EIP-170, hopefully there is a better answer I was only able to make it migrate using: ganache-cli --gasLimit=0x1fffffffffffff --allowUnlimitedContractSize -e 1000000000 --allowUnlimitedContractSize: "Allows unlimited ...


3

Someone might have a better idea, but you could serve Remix locally on your own machine by following the instructions for building here and here. Once you've understood the build flow, you can then tweak the runs value in the compiler-input.js file: module.exports = (sources, opts) => { return JSON.stringify({ language: 'Solidity', sources: ...


3

You don't need to put any name after constructor. Just write it as : constructor (address _owner) payable public Also change your pragma to: pragma solidity ^0.5.0; for better results.


3

Remove parentheses of g function from funcMap[1] = g(); and set funcMap[1] = g;. look this code run without error: pragma solidity ^0.5.2; contract b { mapping(uint => function()) funcMap; function g() public pure { uint x=0; } function update() public { funcMap[1] = g; } }


3

Array userAddresses stored multiple addresses in it. So you can not call send or transfer method with multiple addresses at a time so you have to call like this : userAddresses[i].transfer(value);. So your code should be like this: pragma solidity ^0.4.16; contract init { address[] public userAddresses; function addStudent(address _user) ...


3

This particular code can't do that because <address>.transfer() only makes 2300 gas available to the recipient, which is not enough to make another ether transfer. This could be made to work by providing more gas with the ether transfers, e.g. address(c).call.gas(50000)(""). This forwards 50,000 gas, which should be more than enough to make the ...


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