Hot answers tagged

14 votes
Accepted

Solidity initialize memory array of fixed size

To initialize an empty array instead of uint8[12] memory traits = new uint8[12]; use uint8[12] memory traits;. Then you can alter the array in the // alter values in traits ... section. function ...
user avatar
11 votes
Accepted

Truffle + Infura on Mainnet: Nonce too low error

If you're using Truffle with Infura directly (which it sounds like you are, although it's unclear why you mention MetaMask), you need to be aware that Infura is running on a server farm. If you submit ...
user avatar
  • 990
11 votes
Accepted

Truffle Box at URL https://github.com/truffle-box/bare-box.git doesn't exist

I found out on Truffle's Gitter channel that something must have broken in version 4. I upgraded to version 5 and the issue is resolved. How to upgrade (more information here): npm uninstall -g ...
user avatar
9 votes

Solidity initialize memory array of fixed size

In order to initialize an array from memory you have to do it like this: uint8[] memory theArray = new uint8[](12) Where the 12 inside the parenthesis is the array length. You can also initialize ...
user avatar
  • 7,256
8 votes
Accepted

address payable type - store address and send later using Solidity 0.5.0

UPDATE: Solidity 0.6.x now have explicit conversions to payable: payable(userAddress) And for smart contracts: payable(address(smartContract)) Old answer for Solidity 0.5.x: Here is how to cast ...
user avatar
  • 2,176
6 votes

Web3ProviderEngine does not support synchronous requests while : Running migration: 2_deploy_contracts.js

web3.eth.blockNumber is synchronous. You need to change this to web3.eth.getBlockNumber((err, res) => {}) Alternatively you can restructure this chain with promises: const Promise = require('...
user avatar
  • 2,276
6 votes
Accepted

Error starting protocol stack: datadir already used by another process

If you're sure that geth is not already running, check for the presence of a LOCK file in the geth subdirectory of your data directory. Under Ubuntu this will be ~/.ethereum/geth. I have experienced ...
user avatar
  • 176
6 votes
Accepted

assign decimal to a variable in solidity

Right, Solidity only supports integers. Try this instead: uint sales = 100; uint afterCommission = sales * 5 / 100; // 5% If it's a variable amount but a whole number percent: uint ...
user avatar
  • 26.4k
5 votes
Accepted

Contracts will not compile using truffle with the "emit" keyword included in front of events

I'm afraid that truffle is still using the old solc compiler, that's why you get that error. Indeed $ truffle version Truffle v4.1.3 (core: 4.1.3) //this is the last truffle version Solidity v0.4.19 (...
user avatar
  • 4,147
5 votes

Solidity Compiler - Error: Identifier already declared

This is because the method and the property have the same name. Since solidity automatically creates a public getter function for every public variable in the contract, you don't need function ...
user avatar
5 votes
Accepted

Payable function error: The called function should be payable if you send value and the value you send should be less than your current balance

"The called function should be payable" is a wild guess of a hint and usually wrong. The contract has reverted for any number of reasons. You will have to use debugging techniques to zero in ...
user avatar
  • 51.2k
5 votes

NotFoundError: Key not found in database [hn]

I encountered the same error when solving the same Ethernaut level. I'm using Remix IDE. When I changed from deploying with JavaScript VM to deploying with Injected Web3 (there's a dropdown menu in ...
user avatar
4 votes

What is the advantage of using require instead of return or revert for error handling?

Under the hood, require will revert the transaction, just like revert will. This means that the transaction will fail, and your event will not be logged. return will not stop the transaction and will ...
user avatar
  • 26.4k
4 votes

Recover Ethereum holding sent to wrong address due to entry error

Simple! You just brute-force as many random private keys as you can until you find the one that corresponds to the address you accidentally sent your Ether to. Only downside, this will probably take ...
user avatar
  • 645
4 votes
Accepted

Uncaught ReferenceError: Web3 is not defined Solidity

I hope you are using web3 as browser module. As in their documentation, you can add the CDN link using <script> tags as below, <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/...
user avatar
4 votes

Running out of gas during a deploy due to a large number of require() statements?

I was made aware of a flag in the Ganache command line client that allows unlimited contract sizes during development. By using this flag, I can now deploy/migrate to Ganache without getting an out-...
user avatar
4 votes

Invalid bytes32 value

Replace ['Sat', 'Vit'] with: ['Sat', 'Vit'].map(x => web3.fromAscii(x)) if you're on Truffle 4.x (web3 0.x) ['Sat', 'Vit'].map(x => web3.utils.asciiToHex(x)) if you're on Truffle 5.x (web3 1.x)
user avatar
3 votes
Accepted

New Ganache (1.1.0-beta.0) Causes Invalid JSON RPC response

Maintainer of ganache here. This is a known bug in the beta. See https://github.com/trufflesuite/ganache-cli/issues/471
user avatar
  • 173
3 votes

Truffle + Infura on Mainnet: Nonce too low error

Not sure if this is still relevant, but I am going to leave this here for future generations. I've been struggling to deploy with truffle + Infura on mainnet a lot lately. One and the same script ...
user avatar
3 votes
Accepted

Solidity Compiler - Error: Identifier already declared

@Hari GTT Psicolabis' answer is right, but that means that you have to specify the ownerInfo field as public to get the benefit of a public getter. string public ownerInfo = "Contract Owner name XYZ";...
user avatar
  • 4,147
3 votes
Accepted

Unable to execute web3.eth.accounts[0]

geth --rpc --rpcaddr "localhost" --rpcapi "web3,eth" --rpccorsdomain "*" --rpcvhosts "*" Go(https://github.com/ethereum/wiki/wiki/JSON-RPC#go) You can start the HTTP JSON-RPC with the --rpc flag ...
user avatar
3 votes
Accepted

Smart Contract to send ether

The error is that your contract can not receive ether because it doesn't have a fallback function. Just add this to it: function () public payable{ } This will allows your contract to accept ether. ...
user avatar
  • 7,805
3 votes

Expected primary expression error

Your syntax error can easily be fixed by just changing &amp,&amp; to &&. However, this will then give you a call stack size error, since you cannot return an object as large as your ...
user avatar
  • 3,800
3 votes
Accepted

Fatal: Error starting protocol stack: listen tcp: lookup ”localhost”: no such host

While I can't answer why you're getting this specific error, what I can tell you is that specifying --rcpaddr localhost is unnecessary as localhost is the default rpcaddr value. If I were to take a ...
user avatar
  • 291
3 votes

Truffle Box at URL https://github.com/truffle-box/bare-box.git doesn't exist

I had the same issue when my truffle version was 4.1.14. When I upgraded to version 4.1.15 the issue was solved. I just had to run npm i -g truffle@4.1.15
user avatar
3 votes

"before all" hook: prepare suite

npm uninstall -g truffle npm install -g truffle@5.1.10 (always got me back on track)
user avatar
3 votes

ReferenceError: address is not defined

You have two problems: web3.eth.getBalance() returns an promiEvent object, to obtain the expected value you need to await for it Javascript doesn't support large numbers, so values are wrapped in BN ...
user avatar
  • 29.1k
3 votes
Accepted

Solidity Invalid implicit conversion from string memory to bytes memory requested

Argument to sha256 should be in bytes. But you're passing document which is a string. Following would work. function proofFor(bytes memory document) public pure returns(bytes32) { return sha256(...
user avatar
3 votes

Solidity Invalid implicit conversion from string memory to bytes memory requested

Should probably be return sha256(bytes (document));
user avatar
3 votes

Solidity Compatibility Configuration

Maybe it will be easier for you to start with http://remix.ethereum.org and move to ganache after you gather some experience (or just stay in remix). The main problem here is that you will need ...
user avatar
  • 619

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